1*8044SWilliam.Kucharski@Sun.COM@node Internals 2*8044SWilliam.Kucharski@Sun.COM@appendix Hacking GRUB 3*8044SWilliam.Kucharski@Sun.COM 4*8044SWilliam.Kucharski@Sun.COMThis chapter documents the user-invisible aspect of GRUB. 5*8044SWilliam.Kucharski@Sun.COM 6*8044SWilliam.Kucharski@Sun.COMAs a general rule of software development, it is impossible to keep the 7*8044SWilliam.Kucharski@Sun.COMdescriptions of the internals up-to-date, and it is quite hard to 8*8044SWilliam.Kucharski@Sun.COMdocument everything. So refer to the source code, whenever you are not 9*8044SWilliam.Kucharski@Sun.COMsatisfied with this documentation. Please assume that this gives just 10*8044SWilliam.Kucharski@Sun.COMhints to you. 11*8044SWilliam.Kucharski@Sun.COM 12*8044SWilliam.Kucharski@Sun.COM@menu 13*8044SWilliam.Kucharski@Sun.COM* Memory map:: The memory map of various components 14*8044SWilliam.Kucharski@Sun.COM* Embedded data:: Embedded variables in GRUB 15*8044SWilliam.Kucharski@Sun.COM* Filesystem interface:: The generic interface for filesystems 16*8044SWilliam.Kucharski@Sun.COM* Command interface:: The generic interface for built-ins 17*8044SWilliam.Kucharski@Sun.COM* Bootstrap tricks:: The bootstrap mechanism used in GRUB 18*8044SWilliam.Kucharski@Sun.COM* I/O ports detection:: How to probe I/O ports used by INT 13H 19*8044SWilliam.Kucharski@Sun.COM* Memory detection:: How to detect all installed RAM 20*8044SWilliam.Kucharski@Sun.COM* Low-level disk I/O:: INT 13H disk I/O interrupts 21*8044SWilliam.Kucharski@Sun.COM* MBR:: The structure of Master Boot Record 22*8044SWilliam.Kucharski@Sun.COM* Partition table:: The format of partition tables 23*8044SWilliam.Kucharski@Sun.COM* Submitting patches:: Where and how you should send patches 24*8044SWilliam.Kucharski@Sun.COM@end menu 25*8044SWilliam.Kucharski@Sun.COM 26*8044SWilliam.Kucharski@Sun.COM 27*8044SWilliam.Kucharski@Sun.COM@node Memory map 28*8044SWilliam.Kucharski@Sun.COM@section The memory map of various components 29*8044SWilliam.Kucharski@Sun.COM 30*8044SWilliam.Kucharski@Sun.COMGRUB consists of two distinct components, called @dfn{stages}, which are 31*8044SWilliam.Kucharski@Sun.COMloaded at different times in the boot process. Because they run 32*8044SWilliam.Kucharski@Sun.COMmutual-exclusively, sometimes a memory area overlaps with another 33*8044SWilliam.Kucharski@Sun.COMmemory area. And, even in one stage, a single memory area can be used 34*8044SWilliam.Kucharski@Sun.COMfor various purposes, because their usages are mutually exclusive. 35*8044SWilliam.Kucharski@Sun.COM 36*8044SWilliam.Kucharski@Sun.COMHere is the memory map of the various components: 37*8044SWilliam.Kucharski@Sun.COM 38*8044SWilliam.Kucharski@Sun.COM@table @asis 39*8044SWilliam.Kucharski@Sun.COM@item 0 to 4K-1 40*8044SWilliam.Kucharski@Sun.COMBIOS and real mode interrupts 41*8044SWilliam.Kucharski@Sun.COM 42*8044SWilliam.Kucharski@Sun.COM@item 0x07BE to 0x07FF 43*8044SWilliam.Kucharski@Sun.COMPartition table passed to another boot loader 44*8044SWilliam.Kucharski@Sun.COM 45*8044SWilliam.Kucharski@Sun.COM@item down from 8K-1 46*8044SWilliam.Kucharski@Sun.COMReal mode stack 47*8044SWilliam.Kucharski@Sun.COM 48*8044SWilliam.Kucharski@Sun.COM@item 0x2000 to ? 49*8044SWilliam.Kucharski@Sun.COMThe optional Stage 1.5 is loaded here 50*8044SWilliam.Kucharski@Sun.COM 51*8044SWilliam.Kucharski@Sun.COM@item 0x2000 to 0x7FFF 52*8044SWilliam.Kucharski@Sun.COMCommand-line buffer for Multiboot kernels and modules 53*8044SWilliam.Kucharski@Sun.COM 54*8044SWilliam.Kucharski@Sun.COM@item 0x7C00 to 0x7DFF 55*8044SWilliam.Kucharski@Sun.COMStage 1 is loaded here by BIOS or another boot loader 56*8044SWilliam.Kucharski@Sun.COM 57*8044SWilliam.Kucharski@Sun.COM@item 0x7F00 to 0x7F42 58*8044SWilliam.Kucharski@Sun.COMLBA drive parameters 59*8044SWilliam.Kucharski@Sun.COM 60*8044SWilliam.Kucharski@Sun.COM@item 0x8000 to ? 61*8044SWilliam.Kucharski@Sun.COMStage2 is loaded here 62*8044SWilliam.Kucharski@Sun.COM 63*8044SWilliam.Kucharski@Sun.COM@item The end of Stage 2 to 416K-1 64*8044SWilliam.Kucharski@Sun.COMHeap, in particular used for the menu 65*8044SWilliam.Kucharski@Sun.COM 66*8044SWilliam.Kucharski@Sun.COM@item down from 416K-1 67*8044SWilliam.Kucharski@Sun.COMProtected mode stack 68*8044SWilliam.Kucharski@Sun.COM 69*8044SWilliam.Kucharski@Sun.COM@item 416K to 448K-1 70*8044SWilliam.Kucharski@Sun.COMFilesystem buffer 71*8044SWilliam.Kucharski@Sun.COM 72*8044SWilliam.Kucharski@Sun.COM@item 448K to 479.5K-1 73*8044SWilliam.Kucharski@Sun.COMRaw device buffer 74*8044SWilliam.Kucharski@Sun.COM 75*8044SWilliam.Kucharski@Sun.COM@item 479.5K to 480K-1 76*8044SWilliam.Kucharski@Sun.COM512-byte scratch area 77*8044SWilliam.Kucharski@Sun.COM 78*8044SWilliam.Kucharski@Sun.COM@item 480K to 512K-1 79*8044SWilliam.Kucharski@Sun.COMBuffers for various functions, such as password, command-line, cut and 80*8044SWilliam.Kucharski@Sun.COMpaste, and completion. 81*8044SWilliam.Kucharski@Sun.COM 82*8044SWilliam.Kucharski@Sun.COM@item The last 1K of lower memory 83*8044SWilliam.Kucharski@Sun.COMDisk swapping code and data 84*8044SWilliam.Kucharski@Sun.COM@end table 85*8044SWilliam.Kucharski@Sun.COM 86*8044SWilliam.Kucharski@Sun.COMSee the file @file{stage2/shared.h}, for more information. 87*8044SWilliam.Kucharski@Sun.COM 88*8044SWilliam.Kucharski@Sun.COM 89*8044SWilliam.Kucharski@Sun.COM@node Embedded data 90*8044SWilliam.Kucharski@Sun.COM@section Embedded variables in GRUB 91*8044SWilliam.Kucharski@Sun.COM 92*8044SWilliam.Kucharski@Sun.COMStage 1 and Stage 2 have embedded variables whose locations are 93*8044SWilliam.Kucharski@Sun.COMwell-defined, so that the installation can patch the binary file 94*8044SWilliam.Kucharski@Sun.COMdirectly without recompilation of the stages. 95*8044SWilliam.Kucharski@Sun.COM 96*8044SWilliam.Kucharski@Sun.COMIn Stage 1, these are defined: 97*8044SWilliam.Kucharski@Sun.COM 98*8044SWilliam.Kucharski@Sun.COM@table @code 99*8044SWilliam.Kucharski@Sun.COM@item 0x3E 100*8044SWilliam.Kucharski@Sun.COMThe version number (not GRUB's, but the installation mechanism's). 101*8044SWilliam.Kucharski@Sun.COM 102*8044SWilliam.Kucharski@Sun.COM@item 0x40 103*8044SWilliam.Kucharski@Sun.COMThe boot drive. If it is 0xFF, use a drive passed by BIOS. 104*8044SWilliam.Kucharski@Sun.COM 105*8044SWilliam.Kucharski@Sun.COM@item 0x41 106*8044SWilliam.Kucharski@Sun.COMThe flag for if forcing LBA. 107*8044SWilliam.Kucharski@Sun.COM 108*8044SWilliam.Kucharski@Sun.COM@item 0x42 109*8044SWilliam.Kucharski@Sun.COMThe starting address of Stage 2. 110*8044SWilliam.Kucharski@Sun.COM 111*8044SWilliam.Kucharski@Sun.COM@item 0x44 112*8044SWilliam.Kucharski@Sun.COMThe first sector of Stage 2. 113*8044SWilliam.Kucharski@Sun.COM 114*8044SWilliam.Kucharski@Sun.COM@item 0x48 115*8044SWilliam.Kucharski@Sun.COMThe starting segment of Stage 2. 116*8044SWilliam.Kucharski@Sun.COM 117*8044SWilliam.Kucharski@Sun.COM@item 0x1FE 118*8044SWilliam.Kucharski@Sun.COMThe signature (@code{0xAA55}). 119*8044SWilliam.Kucharski@Sun.COM@end table 120*8044SWilliam.Kucharski@Sun.COM 121*8044SWilliam.Kucharski@Sun.COMSee the file @file{stage1/stage1.S}, for more information. 122*8044SWilliam.Kucharski@Sun.COM 123*8044SWilliam.Kucharski@Sun.COMIn the first sector of Stage 1.5 and Stage 2, the block lists are 124*8044SWilliam.Kucharski@Sun.COMrecorded between @code{firstlist} and @code{lastlist}. The address of 125*8044SWilliam.Kucharski@Sun.COM@code{lastlist} is determined when assembling the file 126*8044SWilliam.Kucharski@Sun.COM@file{stage2/start.S}. 127*8044SWilliam.Kucharski@Sun.COM 128*8044SWilliam.Kucharski@Sun.COMThe trick here is that it is actually read backward, and the first 129*8044SWilliam.Kucharski@Sun.COM8-byte block list is not read here, but after the pointer is decremented 130*8044SWilliam.Kucharski@Sun.COM8 bytes, then after reading it, it decrements again, reads, and so on, 131*8044SWilliam.Kucharski@Sun.COMuntil it is finished. The terminating condition is when the number of 132*8044SWilliam.Kucharski@Sun.COMsectors to be read in the next block list is zero. 133*8044SWilliam.Kucharski@Sun.COM 134*8044SWilliam.Kucharski@Sun.COMThe format of a block list can be seen from the example in the code just 135*8044SWilliam.Kucharski@Sun.COMbefore the @code{firstlist} label. Note that it is always from the 136*8044SWilliam.Kucharski@Sun.COMbeginning of the disk, but @emph{not} relative to the partition 137*8044SWilliam.Kucharski@Sun.COMboundaries. 138*8044SWilliam.Kucharski@Sun.COM 139*8044SWilliam.Kucharski@Sun.COMIn the second sector of Stage 1.5 and Stage 2, these are defined: 140*8044SWilliam.Kucharski@Sun.COM 141*8044SWilliam.Kucharski@Sun.COM@table @asis 142*8044SWilliam.Kucharski@Sun.COM@item @code{0x6} 143*8044SWilliam.Kucharski@Sun.COMThe version number (likewise, the installation mechanism's). 144*8044SWilliam.Kucharski@Sun.COM 145*8044SWilliam.Kucharski@Sun.COM@item @code{0x8} 146*8044SWilliam.Kucharski@Sun.COMThe installed partition. 147*8044SWilliam.Kucharski@Sun.COM 148*8044SWilliam.Kucharski@Sun.COM@item @code{0xC} 149*8044SWilliam.Kucharski@Sun.COMThe saved entry number. 150*8044SWilliam.Kucharski@Sun.COM 151*8044SWilliam.Kucharski@Sun.COM@item @code{0x10} 152*8044SWilliam.Kucharski@Sun.COMThe identifier. 153*8044SWilliam.Kucharski@Sun.COM 154*8044SWilliam.Kucharski@Sun.COM@item @code{0x11} 155*8044SWilliam.Kucharski@Sun.COMThe flag for if forcing LBA. 156*8044SWilliam.Kucharski@Sun.COM 157*8044SWilliam.Kucharski@Sun.COM@item @code{0x12} 158*8044SWilliam.Kucharski@Sun.COMThe version string (GRUB's). 159*8044SWilliam.Kucharski@Sun.COM 160*8044SWilliam.Kucharski@Sun.COM@item @code{0x12} + @dfn{the length of the version string} 161*8044SWilliam.Kucharski@Sun.COMThe name of a configuration file. 162*8044SWilliam.Kucharski@Sun.COM@end table 163*8044SWilliam.Kucharski@Sun.COM 164*8044SWilliam.Kucharski@Sun.COMSee the file @file{stage2/asm.S}, for more information. 165*8044SWilliam.Kucharski@Sun.COM 166*8044SWilliam.Kucharski@Sun.COM 167*8044SWilliam.Kucharski@Sun.COM@node Filesystem interface 168*8044SWilliam.Kucharski@Sun.COM@section The generic interface for filesystems 169*8044SWilliam.Kucharski@Sun.COM 170*8044SWilliam.Kucharski@Sun.COMFor any particular partition, it is presumed that only one of the 171*8044SWilliam.Kucharski@Sun.COM@dfn{normal} filesystems such as FAT, FFS, or ext2fs can be used, so 172*8044SWilliam.Kucharski@Sun.COMthere is a switch table managed by the functions in 173*8044SWilliam.Kucharski@Sun.COM@file{disk_io.c}. The notation is that you can only @dfn{mount} one at a 174*8044SWilliam.Kucharski@Sun.COMtime. 175*8044SWilliam.Kucharski@Sun.COM 176*8044SWilliam.Kucharski@Sun.COMThe block list filesystem has a special place in the system. In addition 177*8044SWilliam.Kucharski@Sun.COMto the @dfn{normal} filesystem (or even without one mounted), you can 178*8044SWilliam.Kucharski@Sun.COMaccess disk blocks directly (in the indicated partition) via the block 179*8044SWilliam.Kucharski@Sun.COMlist notation. Using the block list filesystem doesn't effect any other 180*8044SWilliam.Kucharski@Sun.COMfilesystem mounts. 181*8044SWilliam.Kucharski@Sun.COM 182*8044SWilliam.Kucharski@Sun.COMThe variables which can be read by the filesystem backend are: 183*8044SWilliam.Kucharski@Sun.COM 184*8044SWilliam.Kucharski@Sun.COM@vtable @code 185*8044SWilliam.Kucharski@Sun.COM@item current_drive 186*8044SWilliam.Kucharski@Sun.COMThe current BIOS drive number (numbered from 0, if a floppy, and 187*8044SWilliam.Kucharski@Sun.COMnumbered from 0x80, if a hard disk). 188*8044SWilliam.Kucharski@Sun.COM 189*8044SWilliam.Kucharski@Sun.COM@item current_partition 190*8044SWilliam.Kucharski@Sun.COMThe current partition number. 191*8044SWilliam.Kucharski@Sun.COM 192*8044SWilliam.Kucharski@Sun.COM@item current_slice 193*8044SWilliam.Kucharski@Sun.COMThe current partition type. 194*8044SWilliam.Kucharski@Sun.COM 195*8044SWilliam.Kucharski@Sun.COM@item saved_drive 196*8044SWilliam.Kucharski@Sun.COMThe @dfn{drive} part of the root device. 197*8044SWilliam.Kucharski@Sun.COM 198*8044SWilliam.Kucharski@Sun.COM@item saved_partition 199*8044SWilliam.Kucharski@Sun.COMThe @dfn{partition} part of the root device. 200*8044SWilliam.Kucharski@Sun.COM 201*8044SWilliam.Kucharski@Sun.COM@item part_start 202*8044SWilliam.Kucharski@Sun.COMThe current partition starting address, in sectors. 203*8044SWilliam.Kucharski@Sun.COM 204*8044SWilliam.Kucharski@Sun.COM@item part_length 205*8044SWilliam.Kucharski@Sun.COMThe current partition length, in sectors. 206*8044SWilliam.Kucharski@Sun.COM 207*8044SWilliam.Kucharski@Sun.COM@item print_possibilities 208*8044SWilliam.Kucharski@Sun.COMTrue when the @code{dir} function should print the possible completions 209*8044SWilliam.Kucharski@Sun.COMof a file, and false when it should try to actually open a file of that 210*8044SWilliam.Kucharski@Sun.COMname. 211*8044SWilliam.Kucharski@Sun.COM 212*8044SWilliam.Kucharski@Sun.COM@item FSYS_BUF 213*8044SWilliam.Kucharski@Sun.COMFilesystem buffer which is 32K in size, to use in any way which the 214*8044SWilliam.Kucharski@Sun.COMfilesystem backend desires. 215*8044SWilliam.Kucharski@Sun.COM@end vtable 216*8044SWilliam.Kucharski@Sun.COM 217*8044SWilliam.Kucharski@Sun.COMThe variables which need to be written by a filesystem backend are: 218*8044SWilliam.Kucharski@Sun.COM 219*8044SWilliam.Kucharski@Sun.COM@vtable @code 220*8044SWilliam.Kucharski@Sun.COM@item filepos 221*8044SWilliam.Kucharski@Sun.COMThe current position in the file, in sectors. 222*8044SWilliam.Kucharski@Sun.COM 223*8044SWilliam.Kucharski@Sun.COM@strong{Caution:} the value of @var{filepos} can be changed out from 224*8044SWilliam.Kucharski@Sun.COMunder the filesystem code in the current implementation. Don't depend on 225*8044SWilliam.Kucharski@Sun.COMit being the same for later calls into the backend code! 226*8044SWilliam.Kucharski@Sun.COM 227*8044SWilliam.Kucharski@Sun.COM@item filemax 228*8044SWilliam.Kucharski@Sun.COMThe length of the file. 229*8044SWilliam.Kucharski@Sun.COM 230*8044SWilliam.Kucharski@Sun.COM@item disk_read_func 231*8044SWilliam.Kucharski@Sun.COMThe value of @var{disk_read_hook} @emph{only} during reading of data 232*8044SWilliam.Kucharski@Sun.COMfor the file, not any other fs data, inodes, FAT tables, whatever, then 233*8044SWilliam.Kucharski@Sun.COMset to @code{NULL} at all other times (it will be @code{NULL} by 234*8044SWilliam.Kucharski@Sun.COMdefault). If this isn't done correctly, then the @command{testload} and 235*8044SWilliam.Kucharski@Sun.COM@command{install} commands won't work correctly. 236*8044SWilliam.Kucharski@Sun.COM@end vtable 237*8044SWilliam.Kucharski@Sun.COM 238*8044SWilliam.Kucharski@Sun.COMThe functions expected to be used by the filesystem backend are: 239*8044SWilliam.Kucharski@Sun.COM 240*8044SWilliam.Kucharski@Sun.COM@ftable @code 241*8044SWilliam.Kucharski@Sun.COM@item devread 242*8044SWilliam.Kucharski@Sun.COMOnly read sectors from within a partition. Sector 0 is the first sector 243*8044SWilliam.Kucharski@Sun.COMin the partition. 244*8044SWilliam.Kucharski@Sun.COM 245*8044SWilliam.Kucharski@Sun.COM@item grub_read 246*8044SWilliam.Kucharski@Sun.COMIf the backend uses the block list code, then @code{grub_read} can be 247*8044SWilliam.Kucharski@Sun.COMused, after setting @var{block_file} to 1. 248*8044SWilliam.Kucharski@Sun.COM 249*8044SWilliam.Kucharski@Sun.COM@item print_a_completion 250*8044SWilliam.Kucharski@Sun.COMIf @var{print_possibilities} is true, call @code{print_a_completion} for 251*8044SWilliam.Kucharski@Sun.COMeach possible file name. Otherwise, the file name completion won't work. 252*8044SWilliam.Kucharski@Sun.COM@end ftable 253*8044SWilliam.Kucharski@Sun.COM 254*8044SWilliam.Kucharski@Sun.COMThe functions expected to be defined by the filesystem backend are 255*8044SWilliam.Kucharski@Sun.COMdescribed at least moderately in the file @file{filesys.h}. Their usage 256*8044SWilliam.Kucharski@Sun.COMis fairly evident from their use in the functions in @file{disk_io.c}, 257*8044SWilliam.Kucharski@Sun.COMlook for the use of the @var{fsys_table} array. 258*8044SWilliam.Kucharski@Sun.COM 259*8044SWilliam.Kucharski@Sun.COM@strong{Caution:} The semantics are such that then @samp{mount}ing the 260*8044SWilliam.Kucharski@Sun.COMfilesystem, presume the filesystem buffer @code{FSYS_BUF} is corrupted, 261*8044SWilliam.Kucharski@Sun.COMand (re-)load all important contents. When opening and reading a file, 262*8044SWilliam.Kucharski@Sun.COMpresume that the data from the @samp{mount} is available, and doesn't 263*8044SWilliam.Kucharski@Sun.COMget corrupted by the open/read (i.e. multiple opens and/or reads will be 264*8044SWilliam.Kucharski@Sun.COMdone with only one mount if in the same filesystem). 265*8044SWilliam.Kucharski@Sun.COM 266*8044SWilliam.Kucharski@Sun.COM 267*8044SWilliam.Kucharski@Sun.COM@node Command interface 268*8044SWilliam.Kucharski@Sun.COM@section The generic interface for built-ins 269*8044SWilliam.Kucharski@Sun.COM 270*8044SWilliam.Kucharski@Sun.COMGRUB built-in commands are defined in a uniformal interface, whether 271*8044SWilliam.Kucharski@Sun.COMthey are menu-specific or can be used anywhere. The definition of a 272*8044SWilliam.Kucharski@Sun.COMbuiltin command consists of two parts: the code itself and the table of 273*8044SWilliam.Kucharski@Sun.COMthe information. 274*8044SWilliam.Kucharski@Sun.COM 275*8044SWilliam.Kucharski@Sun.COMThe code must be a function which takes two arguments, a command-line 276*8044SWilliam.Kucharski@Sun.COMstring and flags, and returns an @samp{int} value. The @dfn{flags} 277*8044SWilliam.Kucharski@Sun.COMargument specifies how the function is called, using a bit mask. The 278*8044SWilliam.Kucharski@Sun.COMreturn value must be zero if successful, otherwise non-zero. So it is 279*8044SWilliam.Kucharski@Sun.COMnormally enough to return @var{errnum}. 280*8044SWilliam.Kucharski@Sun.COM 281*8044SWilliam.Kucharski@Sun.COMThe table of the information is represented by the structure 282*8044SWilliam.Kucharski@Sun.COM@code{struct builtin}, which contains the name of the command, a pointer 283*8044SWilliam.Kucharski@Sun.COMto the function, flags, a short description of the command and a long 284*8044SWilliam.Kucharski@Sun.COMdescription of the command. Since the descriptions are used only for 285*8044SWilliam.Kucharski@Sun.COMhelp messages interactively, you don't have to define them, if the 286*8044SWilliam.Kucharski@Sun.COMcommand may not be called interactively (such as @command{title}). 287*8044SWilliam.Kucharski@Sun.COM 288*8044SWilliam.Kucharski@Sun.COMThe table is finally registered in the table @var{builtin_table}, so 289*8044SWilliam.Kucharski@Sun.COMthat @code{run_script} and @code{enter_cmdline} can find the 290*8044SWilliam.Kucharski@Sun.COMcommand. See the files @file{cmdline.c} and @file{builtins.c}, for more 291*8044SWilliam.Kucharski@Sun.COMdetails. 292*8044SWilliam.Kucharski@Sun.COM 293*8044SWilliam.Kucharski@Sun.COM 294*8044SWilliam.Kucharski@Sun.COM@node Bootstrap tricks 295*8044SWilliam.Kucharski@Sun.COM@section The bootstrap mechanism used in GRUB 296*8044SWilliam.Kucharski@Sun.COM 297*8044SWilliam.Kucharski@Sun.COMThe disk space can be used in a boot loader is very restricted because 298*8044SWilliam.Kucharski@Sun.COMa MBR (@pxref{MBR}) is only 512 bytes but it also contains a partition 299*8044SWilliam.Kucharski@Sun.COMtable (@pxref{Partition table}) and a BPB. So the question is how to 300*8044SWilliam.Kucharski@Sun.COMmake a boot loader code enough small to be fit in a MBR. 301*8044SWilliam.Kucharski@Sun.COM 302*8044SWilliam.Kucharski@Sun.COMHowever, GRUB is a very large program, so we break GRUB into 2 (or 3) 303*8044SWilliam.Kucharski@Sun.COMdistinct components, @dfn{Stage 1} and @dfn{Stage 2} (and optionally 304*8044SWilliam.Kucharski@Sun.COM@dfn{Stage 1.5}). @xref{Memory map}, for more information. 305*8044SWilliam.Kucharski@Sun.COM 306*8044SWilliam.Kucharski@Sun.COMWe embed Stage 1 in a MBR or in the boot sector of a partition, and 307*8044SWilliam.Kucharski@Sun.COMplace Stage 2 in a filesystem. The optional Stage 1.5 can be installed 308*8044SWilliam.Kucharski@Sun.COMin a filesystem, in the @dfn{boot loader} area in a FFS or a ReiserFS, 309*8044SWilliam.Kucharski@Sun.COMand in the sectors right after a MBR, because Stage 1.5 is enough small 310*8044SWilliam.Kucharski@Sun.COMand the sectors right after a MBR is normally an unused region. The size 311*8044SWilliam.Kucharski@Sun.COMof this region is the number of sectors per head minus 1. 312*8044SWilliam.Kucharski@Sun.COM 313*8044SWilliam.Kucharski@Sun.COMThus, all Stage1 must do is just load Stage2 or Stage1.5. But even if 314*8044SWilliam.Kucharski@Sun.COMStage 1 needs not to support the user interface or the filesystem 315*8044SWilliam.Kucharski@Sun.COMinterface, it is impossible to make Stage 1 less than 400 bytes, because 316*8044SWilliam.Kucharski@Sun.COMGRUB should support both the CHS mode and the LBA mode (@pxref{Low-level 317*8044SWilliam.Kucharski@Sun.COMdisk I/O}). 318*8044SWilliam.Kucharski@Sun.COM 319*8044SWilliam.Kucharski@Sun.COMThe solution used by GRUB is that Stage 1 loads only the first sector of 320*8044SWilliam.Kucharski@Sun.COMStage 2 (or Stage 1.5) and Stage 2 itself loads the rest. The flow of 321*8044SWilliam.Kucharski@Sun.COMStage 1 is: 322*8044SWilliam.Kucharski@Sun.COM 323*8044SWilliam.Kucharski@Sun.COM@enumerate 324*8044SWilliam.Kucharski@Sun.COM@item 325*8044SWilliam.Kucharski@Sun.COMInitialize the system briefly. 326*8044SWilliam.Kucharski@Sun.COM 327*8044SWilliam.Kucharski@Sun.COM@item 328*8044SWilliam.Kucharski@Sun.COMDetect the geometry and the accessing mode of the @dfn{loading drive}. 329*8044SWilliam.Kucharski@Sun.COM 330*8044SWilliam.Kucharski@Sun.COM@item 331*8044SWilliam.Kucharski@Sun.COMLoad the first sector of Stage 2. 332*8044SWilliam.Kucharski@Sun.COM 333*8044SWilliam.Kucharski@Sun.COM@item 334*8044SWilliam.Kucharski@Sun.COMJump to the starting address of the Stage 2. 335*8044SWilliam.Kucharski@Sun.COM@end enumerate 336*8044SWilliam.Kucharski@Sun.COM 337*8044SWilliam.Kucharski@Sun.COMThe flow of Stage 2 (and Stage 1.5) is: 338*8044SWilliam.Kucharski@Sun.COM 339*8044SWilliam.Kucharski@Sun.COM@enumerate 340*8044SWilliam.Kucharski@Sun.COM@item 341*8044SWilliam.Kucharski@Sun.COMLoad the rest of itself to the real starting address, that is, the 342*8044SWilliam.Kucharski@Sun.COMstarting address plus 512 bytes. The block lists are stored in the last 343*8044SWilliam.Kucharski@Sun.COMpart of the first sector. 344*8044SWilliam.Kucharski@Sun.COM 345*8044SWilliam.Kucharski@Sun.COM@item 346*8044SWilliam.Kucharski@Sun.COMLong jump to the real starting address. 347*8044SWilliam.Kucharski@Sun.COM@end enumerate 348*8044SWilliam.Kucharski@Sun.COM 349*8044SWilliam.Kucharski@Sun.COMNote that Stage 2 (or Stage 1.5) does not probe the geometry 350*8044SWilliam.Kucharski@Sun.COMor the accessing mode of the @dfn{loading drive}, since Stage 1 has 351*8044SWilliam.Kucharski@Sun.COMalready probed them. 352*8044SWilliam.Kucharski@Sun.COM 353*8044SWilliam.Kucharski@Sun.COM 354*8044SWilliam.Kucharski@Sun.COM@node I/O ports detection 355*8044SWilliam.Kucharski@Sun.COM@section How to probe I/O ports used by INT 13H 356*8044SWilliam.Kucharski@Sun.COM 357*8044SWilliam.Kucharski@Sun.COMFIXME: I will write this chapter after implementing the new technique. 358*8044SWilliam.Kucharski@Sun.COM 359*8044SWilliam.Kucharski@Sun.COM 360*8044SWilliam.Kucharski@Sun.COM 361*8044SWilliam.Kucharski@Sun.COM@node Memory detection 362*8044SWilliam.Kucharski@Sun.COM@section How to detect all installed RAM 363*8044SWilliam.Kucharski@Sun.COM 364*8044SWilliam.Kucharski@Sun.COMFIXME: I doubt if Erich didn't write this chapter only himself wholly, 365*8044SWilliam.Kucharski@Sun.COMso I will rewrite this chapter. 366*8044SWilliam.Kucharski@Sun.COM 367*8044SWilliam.Kucharski@Sun.COM 368*8044SWilliam.Kucharski@Sun.COM@node Low-level disk I/O 369*8044SWilliam.Kucharski@Sun.COM@section INT 13H disk I/O interrupts 370*8044SWilliam.Kucharski@Sun.COM 371*8044SWilliam.Kucharski@Sun.COMFIXME: I'm not sure where some part of the original chapter is derived, 372*8044SWilliam.Kucharski@Sun.COMso I will rewrite this chapter. 373*8044SWilliam.Kucharski@Sun.COM 374*8044SWilliam.Kucharski@Sun.COM 375*8044SWilliam.Kucharski@Sun.COM@node MBR 376*8044SWilliam.Kucharski@Sun.COM@section The structure of Master Boot Record 377*8044SWilliam.Kucharski@Sun.COM 378*8044SWilliam.Kucharski@Sun.COMFIXME: Likewise. 379*8044SWilliam.Kucharski@Sun.COM 380*8044SWilliam.Kucharski@Sun.COM 381*8044SWilliam.Kucharski@Sun.COM@node Partition table 382*8044SWilliam.Kucharski@Sun.COM@section The format of partition tables 383*8044SWilliam.Kucharski@Sun.COM 384*8044SWilliam.Kucharski@Sun.COMFIXME: Probably the original chapter is derived from "How It Works", so 385*8044SWilliam.Kucharski@Sun.COMI will rewrite this chapter. 386*8044SWilliam.Kucharski@Sun.COM 387*8044SWilliam.Kucharski@Sun.COM 388*8044SWilliam.Kucharski@Sun.COM@node Submitting patches 389*8044SWilliam.Kucharski@Sun.COM@section Where and how you should send patches 390*8044SWilliam.Kucharski@Sun.COM 391*8044SWilliam.Kucharski@Sun.COMWhen you write patches for GRUB, please send them to the mailing list 392*8044SWilliam.Kucharski@Sun.COM@email{bug-grub@@gnu.org}. Here is the list of items of which you 393*8044SWilliam.Kucharski@Sun.COMshould take care: 394*8044SWilliam.Kucharski@Sun.COM 395*8044SWilliam.Kucharski@Sun.COM@itemize @bullet 396*8044SWilliam.Kucharski@Sun.COM@item 397*8044SWilliam.Kucharski@Sun.COMPlease make your patch as small as possible. Generally, it is not a good 398*8044SWilliam.Kucharski@Sun.COMthing to make one big patch which changes many things. Instead, 399*8044SWilliam.Kucharski@Sun.COMsegregate features and produce many patches. 400*8044SWilliam.Kucharski@Sun.COM 401*8044SWilliam.Kucharski@Sun.COM@item 402*8044SWilliam.Kucharski@Sun.COMUse as late code as possible, for the original code. The CVS repository 403*8044SWilliam.Kucharski@Sun.COMalways has the current version (@pxref{Obtaining and Building GRUB}). 404*8044SWilliam.Kucharski@Sun.COM 405*8044SWilliam.Kucharski@Sun.COM@item 406*8044SWilliam.Kucharski@Sun.COMWrite ChangeLog entries. @xref{Change Logs, , Change Logs, standards, 407*8044SWilliam.Kucharski@Sun.COMGNU Coding Standards}, if you don't know how to write ChangeLog. 408*8044SWilliam.Kucharski@Sun.COM 409*8044SWilliam.Kucharski@Sun.COM@item 410*8044SWilliam.Kucharski@Sun.COMMake patches in unified diff format. @samp{diff -urN} is appropriate in 411*8044SWilliam.Kucharski@Sun.COMmost cases. 412*8044SWilliam.Kucharski@Sun.COM 413*8044SWilliam.Kucharski@Sun.COM@item 414*8044SWilliam.Kucharski@Sun.COMDon't make patches reversely. Reverse patches are difficult to read and 415*8044SWilliam.Kucharski@Sun.COMuse. 416*8044SWilliam.Kucharski@Sun.COM 417*8044SWilliam.Kucharski@Sun.COM@item 418*8044SWilliam.Kucharski@Sun.COMBe careful enough of the license term and the copyright. Because GRUB 419*8044SWilliam.Kucharski@Sun.COMis under GNU General Public License, you may not steal code from 420*8044SWilliam.Kucharski@Sun.COMsoftware whose license is incompatible against GPL. And, if you copy 421*8044SWilliam.Kucharski@Sun.COMcode written by others, you must not ignore their copyrights. Feel free 422*8044SWilliam.Kucharski@Sun.COMto ask GRUB maintainers, whenever you are not sure what you should do. 423*8044SWilliam.Kucharski@Sun.COM 424*8044SWilliam.Kucharski@Sun.COM@item 425*8044SWilliam.Kucharski@Sun.COMIf your patch is too large to send in e-mail, put it at somewhere we can 426*8044SWilliam.Kucharski@Sun.COMsee. Usually, you shouldn't send e-mail over 20K. 427*8044SWilliam.Kucharski@Sun.COM@end itemize 428