回答一下问题,GRUB 头中为什么需要 _entry 标号和 _start 标号的地址?
我们定义的 flags 值为:
MBT_HDR_FLAGS EQU 0x00010003
根据 Multiboot Specification 定义的头结构
Offset Type Field Name Note
0 u32 magic required
4 u32 flags required
8 u32 checksum required
12 u32 header_addr if flags[16] is set
16 u32 load_addr if flags[16] is set
20 u32 load_end_addr if flags[16] is set
24 u32 bss_end_addr if flags[16] is set
28 u32 entry_addr if flags[16] is set
32 u32 mode_type if flags[2] is set
36 u32 width if flags[2] is set
40 u32 height if flags[2] is set
44 u32 depth if flags[2] is set
flags[16] 解释如下:
If bit 16 in the ‘flags’ word is set, then the fields at offsets 12-28 in the Multiboot header are valid, and the boot loader should use them instead of the fields in the actual executable header to calculate where to load the OS image. This information does not need to be provided if the kernel image is in ELF format, but it must be provided if the images is in a.out format or in some other format. Compliant boot loaders must be able to load images that either are in ELF format or contain the load address information embedded in the Multiboot header; they may also directly support other executable formats, such as particular a.out variants, but are not required to.
也就是如果我们用的是标准的 ELF 文件就不需要提供额外的地址信息,而我们用的是自己定义的格式就需要人家从哪里加载哪里运行,所以需要将 bit 16 使能,填充相应的字段。
再解释下两个字段的含义:
load_addr
Contains the physical address of the beginning of the text segment. The offset in the OS image file at which to start loading is defined by the offset at which the header was found, minus (header_addr - load_addr). load_addr must be less than or equal to header_addr.
entry_addr
The physical address to which the boot loader should jump in order to start running the operating system.