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