GRUB 2 Internals
This article is about the internals of GRUB 2. It's far from complete and it is not meant to be complete(I plan to write a GRUB 2 hackers guide some day), although the information below should be improved and extended.
Misc stuff
In GRUB 2 there are some functions you can always use. These functions can be found in `include/grub/misc.h'. You can find functions here for manipulation of strings, memory and formatted printing. Besides that you can use memory management functions from `include/grub/mm.h'.
Disks
In `include/grub/disks.h' you can find the interfaces for disk access. `struct grub_disk_dev' describes one specific class of disks. You can implement new disk drivers by creating a new struct grub_disk_dev and registering it. To use a disk, knowledge of these internals are not required. You can use the exported functions directly and should never operate on the structs directly. You can find the implementations of disks in `disk/'.
Filesystems
Filesystems are similar to disks. The interfaces can be found in `include/grub/fs.h' and `include/grub/file.h'. The latter is the most important one when you want to read from files. The interfaces are easy to understand and require no further explaination. All implementations of filesystems can be found in `fs/'.
Partitions
GRUB supports multiple partition layouts. The implementations can be found in `partmap/', GRUB uses these transparently.
Commands
There are two kind of commands. Commands that run in rescue mode and those that run in normal mode. You can find examples normal mode commands in `commands/'. The most important thing when implementing a command is the `struct grub_arg_option'. It describes the accepted arguments and this is used by the parser to parse commands and generate help output.
Terminals
In `term/' you can find the implementation of the terminal drivers. You can use the terminal functions by including `include/grub/term.h'.
Loader
The directory `loader/' contains all the OS loaders.
Initialization
In `boot/' machine specific code can be found to load and initialize GRUB. After that the code in `kern/' is executed. The first function executed is grub_enter_rescue_mode in `kern/rescue.c'.