Linux/x86启动协议

内存布局

jietu20180811-052925-2x


0x0~0x600

BIOS专用的,1.5KB,1536字节

0x600~0x800

通常由MBR使用,512字节

0x800-0x1000

MBR或BIOS保留,512字节

小结:低2KB都给BIOS或MBR了


0x1000~X

BootLoader使用
此处包括重要的0x7c00
结束地址取决于BootLoader的体积

X~X+0x8000

Kernel启动扇区
Kernel实地址模式代码
动态占用32KB内存

X+0x8000~X+0x10000

动态占用8KB内存,用作内核实地址模式的堆和栈

X+0x10000~0xA0000

命令行与BIOS保留
保留尽可能多的空间给BIOS,尤其是不要占用0x9A000以上空间,否则会与BIOS冲突。

小结:一共640KB-2KB = 638KB,实地址模式内核只占用约64KB。


0xA0000~0x100000

实地址模式一共1MB寻址空间,此处剩余384KB。
用于I/O Memory Hole

小结:1MB用完了
——————————————————————

1MB以上

保护模式内核

——————————————————————

以下内容中sector(扇区)特指512字节,与实际的介质的扇区尺寸无关

加载Linux kernel的第一步是加载real-mode code(boot sector和setup code),然后在偏移0x01f1处检查下面的header。real-mode code最高可达32KB,尽管BootLoader可能选择只加载前2个sector(1KB),然后检查bootup sector的尺寸。

Offset Proto Name Meaning
/Size

01F1/1 ALL(1 setup_sects The size of the setup in sectors
01F2/2 ALL root_flags If set, the root is mounted readonly
01F4/4 2.04+(2 syssize The size of the 32-bit code in 16-byte paras
01F8/2 ALL ram_size DO NOT USE - for bootsect.S use only
01FA/2 ALL vid_mode Video mode control
01FC/2 ALL root_dev Default root device number
01FE/2 ALL boot_flag 0xAA55 magic number
0200/2 2.00+ jump Jump instruction
0202/4 2.00+ header Magic signature "HdrS"
0206/2 2.00+ version Boot protocol version supported
0208/4 2.00+ realmode_swtch Boot loader hook (see below)
020C/2 2.00+ start_sys_seg The load-low segment (0x1000) (obsolete)
020E/2 2.00+ kernel_version Pointer to kernel version string
0210/1 2.00+ type_of_loader Boot loader identifier
0211/1 2.00+ loadflags Boot protocol option flags
0212/2 2.00+ setup_move_size Move to high memory size (used with hooks)
0214/4 2.00+ code32_start Boot loader hook (see below)
0218/4 2.00+ ramdisk_image initrd load address (set by boot loader)
021C/4 2.00+ ramdisk_size initrd size (set by boot loader)
0220/4 2.00+ bootsect_kludge DO NOT USE - for bootsect.S use only
0224/2 2.01+ heap_end_ptr Free memory after setup end
0226/1 2.02+(3 ext_loader_ver Extended boot loader version
0227/1 2.02+(3 ext_loader_type Extended boot loader ID
0228/4 2.02+ cmd_line_ptr 32-bit pointer to the kernel command line
022C/4 2.03+ initrd_addr_max Highest legal initrd address
0230/4 2.05+ kernel_alignment Physical addr alignment required for kernel
0234/1 2.05+ relocatable_kernel Whether kernel is relocatable or not
0235/1 2.10+ min_alignment Minimum alignment, as a power of two
0236/2 2.12+ xloadflags Boot protocol option flags
0238/4 2.06+ cmdline_size Maximum size of the kernel command line
023C/4 2.07+ hardware_subarch Hardware subarchitecture
0240/8 2.07+ hardware_subarch_data Subarchitecture-specific data
0248/4 2.08+ payload_offset Offset of kernel payload
024C/4 2.08+ payload_length Length of kernel payload
0250/8 2.09+ setup_data 64-bit physical pointer to linked list
of struct setup_data
0258/8 2.10+ pref_address Preferred loading address
0260/4 2.10+ init_size Linear memory required during initialization
0264/4 2.11+ handover_offset Offset of handover entry point

(1) For backwards compatibility, if the setup_sects field contains 0, the
real value is 4.

(2) For boot protocol prior to 2.04, the upper two bytes of the syssize
field are unusable, which means the size of a bzImage kernel
cannot be determined.

(3) Ignored, but safe to set, for boot protocols 2.02-2.09.

If the "HdrS" (0x53726448) magic number is not found at offset 0x202,
the boot protocol version is "old". Loading an old kernel, the
following parameters should be assumed:

Image type = zImage
initrd not supported
Real-mode kernel must be located at 0x90000.

Otherwise, the "version" field contains the protocol version,
e.g. protocol version 2.01 will contain 0x0201 in this field. When
setting fields in the header, you must make sure only to set fields
supported by the protocol version in use.