说明:
grub分为stage1、stage1.5、stage2三部分,其中stage1.5是可选的。
stage1/stage1.S:被编译成stage1,位于0号扇区(逻辑扇区号),即MBR。
stage1的作用是将1号扇区的start载入到内存0x0000:0x8000处,然后跳到0x0000:0x8000处执行。
stage2/start.S:被编译成start,位于1号扇区,被stage1载入到0x0000:0x8000并跳转执行。
stage2/start.S的作用分两种情况。
I.若定义了STAGE1_5,则它加载stage1_5(2~N号扇区)到内存0x0000:0x2200,并跳到0x0000:0x2200执行。
II.若未定义STAGE1_5,则它加载stage2 (2~N号扇区)到内存0x0000:0x8200,并跳到0x0000:0x8200执行。
stage2/asm.S,由start跳入执行。
stage2/asm.S的作用是:
装载gdt,切换到保护模式,调用函数init_bios_info(至此,代码从汇编转换到C)。
init_bios_info取得内存和磁盘相关的信息之后,调用cmain。
cmain即stage1.5和stage2的分叉点,源代码中有两个cmain函数,对应stage1.5和stage2的实现。
说明:
1. stage2/start.S编译出的512bytes的start会添在stage1.5或者stage2的头部,作为其的一部分。
2. grub中最重要的数据结构:struct builtin;
grub中的每一个命令都是一个builtin结构。
struct builtin
{
/* The command name. */
char *name;
/* The callback function. */
int (*func) (char *, int);
/* The combination of the flags defined above. */
int flags;
/* The short version of the documentation. */
char *short_doc;
/* The long version of the documentation. */
char *long_doc;
};
{
/* The command name. */
char *name;
/* The callback function. */
int (*func) (char *, int);
/* The combination of the flags defined above. */
int flags;
/* The short version of the documentation. */
char *short_doc;
/* The long version of the documentation. */
char *long_doc;
};
name为命令名称。
func为命令执行的功能函数。
flags为命令的一些属性,比如有些命令只在控制台中(grub>)使用,有些命令只在菜单中使用。
short_doc和long_doc是此命令的一些说明。