📄 boot.txt
字号:
THE LINUX/I386 BOOT PROTOCOL ---------------------------- H. Peter Anvin <hpa@zytor.com> Last update 2007-05-23On the i386 platform, the Linux kernel uses a rather complicated bootconvention. This has evolved partially due to historical aspects, aswell as the desire in the early days to have the kernel itself be abootable image, the complicated PC memory model and due to changedexpectations in the PC industry caused by the effective demise ofreal-mode DOS as a mainstream operating system.Currently, the following versions of the Linux/i386 boot protocol exist.Old kernels: zImage/Image support only. Some very early kernels may not even support a command line.Protocol 2.00: (Kernel 1.3.73) Added bzImage and initrd support, as well as a formalized way to communicate between the boot loader and the kernel. setup.S made relocatable, although the traditional setup area still assumed writable.Protocol 2.01: (Kernel 1.3.76) Added a heap overrun warning.Protocol 2.02: (Kernel 2.4.0-test3-pre3) New command line protocol. Lower the conventional memory ceiling. No overwrite of the traditional setup area, thus making booting safe for systems which use the EBDA from SMM or 32-bit BIOS entry points. zImage deprecated but still supported.Protocol 2.03: (Kernel 2.4.18-pre1) Explicitly makes the highest possible initrd address available to the bootloader.Protocol 2.04: (Kernel 2.6.14) Extend the syssize field to four bytes.Protocol 2.05: (Kernel 2.6.20) Make protected mode kernel relocatable. Introduce relocatable_kernel and kernel_alignment fields.Protocol 2.06: (Kernel 2.6.22) Added a field that contains the size of the boot command line**** MEMORY LAYOUTThe traditional memory map for the kernel loader, used for Image orzImage kernels, typically looks like: | |0A0000 +------------------------+ | Reserved for BIOS | Do not use. Reserved for BIOS EBDA.09A000 +------------------------+ | Command line | | Stack/heap | For use by the kernel real-mode code.098000 +------------------------+ | Kernel setup | The kernel real-mode code.090200 +------------------------+ | Kernel boot sector | The kernel legacy boot sector.090000 +------------------------+ | Protected-mode kernel | The bulk of the kernel image.010000 +------------------------+ | Boot loader | <- Boot sector entry point 0000:7C00001000 +------------------------+ | Reserved for MBR/BIOS |000800 +------------------------+ | Typically used by MBR |000600 +------------------------+ | BIOS use only |000000 +------------------------+When using bzImage, the protected-mode kernel was relocated to0x100000 ("high memory"), and the kernel real-mode block (boot sector,setup, and stack/heap) was made relocatable to any address between0x10000 and end of low memory. Unfortunately, in protocols 2.00 and2.01 the 0x90000+ memory range is still used internally by the kernel;the 2.02 protocol resolves that problem.It is desirable to keep the "memory ceiling" -- the highest point inlow memory touched by the boot loader -- as low as possible, sincesome newer BIOSes have begun to allocate some rather large amounts ofmemory, called the Extended BIOS Data Area, near the top of lowmemory. The boot loader should use the "INT 12h" BIOS call to verifyhow much low memory is available.Unfortunately, if INT 12h reports that the amount of memory is toolow, there is usually nothing the boot loader can do but to report anerror to the user. The boot loader should therefore be designed totake up as little space in low memory as it reasonably can. ForzImage or old bzImage kernels, which need data written into the0x90000 segment, the boot loader should make sure not to use memoryabove the 0x9A000 point; too many BIOSes will break above that point.For a modern bzImage kernel with boot protocol version >= 2.02, amemory layout like the following is suggested: ~ ~ | Protected-mode kernel |100000 +------------------------+ | I/O memory hole |0A0000 +------------------------+ | Reserved for BIOS | Leave as much as possible unused ~ ~ | Command line | (Can also be below the X+10000 mark)X+10000 +------------------------+ | Stack/heap | For use by the kernel real-mode code.X+08000 +------------------------+ | Kernel setup | The kernel real-mode code. | Kernel boot sector | The kernel legacy boot sector.X +------------------------+ | Boot loader | <- Boot sector entry point 0000:7C00001000 +------------------------+ | Reserved for MBR/BIOS |000800 +------------------------+ | Typically used by MBR |000600 +------------------------+ | BIOS use only |000000 +------------------------+... where the address X is as low as the design of the boot loaderpermits.**** THE REAL-MODE KERNEL HEADERIn the following text, and anywhere in the kernel boot sequence, "asector" refers to 512 bytes. It is independent of the actual sectorsize of the underlying medium.The first step in loading a Linux kernel should be to load thereal-mode code (boot sector and setup code) and then examine thefollowing header at offset 0x01f1. The real-mode code can total up to32K, although the boot loader may choose to load only the first twosectors (1K) and then examine the bootup sector size.The header looks like:Offset Proto Name Meaning/Size01F1/1 ALL(1 setup_sects The size of the setup in sectors01F2/2 ALL root_flags If set, the root is mounted readonly01F4/4 2.04+(2 syssize The size of the 32-bit code in 16-byte paras01F8/2 ALL ram_size DO NOT USE - for bootsect.S use only01FA/2 ALL vid_mode Video mode control01FC/2 ALL root_dev Default root device number01FE/2 ALL boot_flag 0xAA55 magic number0200/2 2.00+ jump Jump instruction0202/4 2.00+ header Magic signature "HdrS"0206/2 2.00+ version Boot protocol version supported0208/4 2.00+ realmode_swtch Boot loader hook (see below)020C/2 2.00+ start_sys The load-low segment (0x1000) (obsolete)020E/2 2.00+ kernel_version Pointer to kernel version string0210/1 2.00+ type_of_loader Boot loader identifier0211/1 2.00+ loadflags Boot protocol option flags0212/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 only0224/2 2.01+ heap_end_ptr Free memory after setup end0226/2 N/A pad1 Unused0228/4 2.02+ cmd_line_ptr 32-bit pointer to the kernel command line022C/4 2.03+ initrd_addr_max Highest legal initrd address0230/4 2.05+ kernel_alignment Physical addr alignment required for kernel0234/1 2.05+ relocatable_kernel Whether kernel is relocatable or not0235/3 N/A pad2 Unused0238/4 2.06+ cmdline_size Maximum size of the kernel command line023C/4 2.07+ hardware_subarch Hardware subarchitecture0240/8 2.07+ hardware_subarch_data Subarchitecture-specific data(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.If the "HdrS" (0x53726448) magic number is not found at offset 0x202,the boot protocol version is "old". Loading an old kernel, thefollowing 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. Whensetting fields in the header, you must make sure only to set fieldssupported by the protocol version in use.**** DETAILS OF HEADER FIELDSFor each field, some are information from the kernel to the bootloader("read"), some are expected to be filled out by the bootloader("write"), and some are expected to be read and modified by thebootloader ("modify").All general purpose boot loaders should write the fields marked(obligatory). Boot loaders who want to load the kernel at anonstandard address should fill in the fields marked (reloc); otherboot loaders can ignore those fields.The byte order of all fields is littleendian (this is x86, after all.)Field name: setup_sectsType: readOffset/size: 0x1f1/1Protocol: ALL The size of the setup code in 512-byte sectors. If this field is 0, the real value is 4. The real-mode code consists of the boot sector (always one 512-byte sector) plus the setup code.Field name: root_flagsType: modify (optional)Offset/size: 0x1f2/2Protocol: ALL If this field is nonzero, the root defaults to readonly. The use of this field is deprecated; use the "ro" or "rw" options on the command line instead.Field name: syssizeType: readOffset/size: 0x1f4/4 (protocol 2.04+) 0x1f4/2 (protocol ALL)Protocol: 2.04+ The size of the protected-mode code in units of 16-byte paragraphs. For protocol versions older than 2.04 this field is only two bytes wide, and therefore cannot be trusted for the size of a kernel if the LOAD_HIGH flag is set.Field name: ram_sizeType: kernel internalOffset/size: 0x1f8/2Protocol: ALL This field is obsolete.Field name: vid_modeType: modify (obligatory)Offset/size: 0x1fa/2 Please see the section on SPECIAL COMMAND LINE OPTIONS.Field name: root_devType: modify (optional)Offset/size: 0x1fc/2Protocol: ALL The default root device device number. The use of this field is deprecated, use the "root=" option on the command line instead.Field name: boot_flagType: readOffset/size: 0x1fe/2Protocol: ALL Contains 0xAA55. This is the closest thing old Linux kernels have to a magic number.Field name: jumpType: readOffset/size: 0x200/2Protocol: 2.00+ Contains an x86 jump instruction, 0xEB followed by a signed offset relative to byte 0x202. This can be used to determine the size of the header.Field name: headerType: readOffset/size: 0x202/4Protocol: 2.00+ Contains the magic number "HdrS" (0x53726448).Field name: versionType: readOffset/size: 0x206/2Protocol: 2.00+ Contains the boot protocol version, in (major << 8)+minor format, e.g. 0x0204 for version 2.04, and 0x0a11 for a hypothetical version 10.17.Field name: readmode_swtchType: modify (optional)Offset/size: 0x208/4Protocol: 2.00+ Boot loader hook (see ADVANCED BOOT LOADER HOOKS below.)Field name: start_sysType: readOffset/size: 0x20c/4Protocol: 2.00+ The load low segment (0x1000). Obsolete.Field name: kernel_versionType: readOffset/size: 0x20e/2Protocol: 2.00+ If set to a nonzero value, contains a pointer to a NUL-terminated human-readable kernel version number string, less 0x200. This can be used to display the kernel version to the user. This value should be less than (0x200*setup_sects). For example, if this value is set to 0x1c00, the kernel version number string can be found at offset 0x1e00 in the kernel file. This is a valid value if and only if the "setup_sects" field contains the value 15 or higher, as: 0x1c00 < 15*0x200 (= 0x1e00) but 0x1c00 >= 14*0x200 (= 0x1c00) 0x1c00 >> 9 = 14, so the minimum value for setup_secs is 15.Field name: type_of_loaderType: write (obligatory)Offset/size: 0x210/1Protocol: 2.00+ If your boot loader has an assigned id (see table below), enter 0xTV here, where T is an identifier for the boot loader and V is a version number. Otherwise, enter 0xFF here. Assigned boot loader ids: 0 LILO (0x00 reserved for pre-2.00 bootloader) 1 Loadlin 2 bootsect-loader (0x20, all other values reserved) 3 SYSLINUX 4 EtherBoot 5 ELILO 7 GRuB 8 U-BOOT 9 Xen A Gujin B Qemu Please contact <hpa@zytor.com> if you need a bootloader ID value assigned.Field name: loadflagsType: modify (obligatory)Offset/size: 0x211/1Protocol: 2.00+ This field is a bitmask. Bit 0 (read): LOADED_HIGH - If 0, the protected-mode code is loaded at 0x10000. - If 1, the protected-mode code is loaded at 0x100000. Bit 6 (write): KEEP_SEGMENTS Protocol: 2.07+ - if 0, reload the segment registers in the 32bit entry point. - if 1, do not reload the segment registers in the 32bit entry point. Assume that %cs %ds %ss %es are all set to flat segments with a base of 0 (or the equivalent for their environment). Bit 7 (write): CAN_USE_HEAP Set this bit to 1 to indicate that the value entered in the heap_end_ptr is valid. If this field is clear, some setup code functionality will be disabled.Field name: setup_move_sizeType: modify (obligatory)Offset/size: 0x212/2Protocol: 2.00-2.01 When using protocol 2.00 or 2.01, if the real mode kernel is not loaded at 0x90000, it gets moved there later in the loading sequence. Fill in this field if you want additional data (such as the kernel command line) moved in addition to the real-mode kernel itself. The unit is bytes starting with the beginning of the boot sector. This field is can be ignored when the protocol is 2.02 or higher, or if the real-mode code is loaded at 0x90000.Field name: code32_startType: modify (optional, reloc)Offset/size: 0x214/4Protocol: 2.00+ The address to jump to in protected mode. This defaults to the load address of the kernel, and can be used by the boot loader to determine the proper load address. This field can be modified for two purposes: 1. as a boot loader hook (see ADVANCED BOOT LOADER HOOKS below.) 2. if a bootloader which does not install a hook loads a relocatable kernel at a nonstandard address it will have to modify this field to point to the load address.Field name: ramdisk_imageType: write (obligatory)Offset/size: 0x218/4Protocol: 2.00+ The 32-bit linear address of the initial ramdisk or ramfs. Leave at zero if there is no initial ramdisk/ramfs.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -