i386系统引导协议 \Documentation\i386\boot.txt

来源:互联网 发布:淘宝优惠券领取地址 编辑:程序博客网 时间:2024/06/07 17:09

THE LINUX/I386 BOOT PROTOCOL

linux之i386引导协议

1、On the i386 platform, the Linux kernel uses a rather complicated boot convention. 在i386中引导linux内很复杂。

主要经历了四个版本:

Currently, four 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.

2、MEMORY LAYOUT内存布局

The traditional memory map for the kernel loader, used for Image or
zImage kernels, typically looks like:


| |
0A0000 +------------------------+
|  Reserved for BIOS|Do not use.  Reserved for BIOS EBDA.
09A000 +------------------------+
|  Stack/heap/cmdline|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:7C00
001000 +------------------------+
|  Reserved for MBR/BIOS |
000800 +------------------------+
|  Typically used by MBR |
000600 +------------------------+ 
|  BIOS use only|
000000 +------------------------+

3、THE REAL-MODE KERNEL HEADER


In the following text, and anywhere in the kernel boot sequence, "a
sector" refers to 512 bytes.  It is independent of the actual sector
size of the underlying medium. 主要是关于bootsect.S的,正常情况下,不会执行该文件生成的代码,也就是该模块不会被BIOS装载到段地址0x7c00。该文件的引导功能被取消,系统借助其他的引导程序引导,如GRUB等。引导程序如GRUB等将bootsect装载到地址0x9000:0000中 ,然后设置下面这些参数,最终内核识别和利用这些参数。下面是这个文件内偏移地址出的含义:


The header looks like:
Offset Proto Name Meaning
/Size


01F1/1 ALL setup_sects The size of the setup in sectors
01F2/2 ALL root_flags If set, the root is mounted readonly
01F4/2 ALL syssize DO NOT USE - for bootsect.S use only
01F6/2 ALL swap_dev DO NOT USE - obsolete
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 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/2 N/A pad1 Unused
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

 ramdisk_image, ramdisk_size:对应上面
If your boot loader has loaded an initial ramdisk (initrd),
set ramdisk_image to the 32-bit pointer to the ramdisk data
and the ramdisk_size to the size of the ramdisk data.


The initrd should typically be located as high in memory as
possible, as it may otherwise get overwritten by the early
kernel initialization sequence.However, it must never be
located above the address specified in the initrd_addr_max
field. The initrd should be at least 4K page aligned.

4、 THE KERNEL COMMAND LINE内核命令行

The kernel command line has become an important way for the boot
loader to communicate with the kernel.  Some of its options are also
relevant to the boot loader itself, see "special command line options"
below.作为引导程序和内核交流的方式

If the boot protocol version is 2.02 or later, the address of the
kernel command line is given by the header field cmd_line_ptr (see
above.)

5、

**** LOADING THE REST OF THE KERNEL加载内核


The non-real-mode kernel starts at offset (setup_sects+1)*512 in the
kernel file (again, if setup_sects == 0 the real value is 4.)  It
should be loaded at address 0x10000 for Image/zImage kernels and
0x100000 for bzImage kernels.非实地址模式下,内核的其实位置,和加载的地址,注意,zImage和bzImage不同


The kernel is a bzImage kernel if the protocol >= 2.00 and the 0x01
bit (LOAD_HIGH) in the loadflags field is set:


is_bzImage = (protocol >= 0x0200) && (loadflags & 0x01);
load_address = is_bzImage ? 0x100000 : 0x10000;

对内核是bzImage判断


Note that Image/zImage kernels can be up to 512K in size, and thus use
the entire 0x10000-0x90000 range of memory.  This means it is pretty
much a requirement for these kernels to load the real-mode part at
0x90000.  bzImage kernels allow much more flexibility.

6、

**** SPECIAL COMMAND LINE OPTIONS启动命令行


If the command line provided by the boot loader is entered by the
user, the user may expect the following command line options to work.如果命令行有使用者自己提供
They should normally not be deleted from the kernel command line even
though not all of them are actually meaningful to the kernel.  Boot
loader authors who need additional command line options for the boot
loader itself should get them registered in
Documentation/kernel-parameters.txt to make sure they will not
conflict with actual kernel options now or in the future.不应该和原来已定义的命令行冲突。
例如:
  initrd=<file>
An initrd should be loaded.  The meaning of <file> is
obviously bootloader-dependent, and some boot loaders
(e.g. LILO) do not have such a command.



7、**** RUNNING THE KERNEL运行内核

这是在实地址模式下的启动。
The kernel is started by jumping to the kernel entry point, which is
located at *segment* offset 0x20 from the start of the real mode
kernel.
 This means that if you loaded your real-mode kernel code at
0x90000, the kernel entry point is 9020:0000.


At entry, ds = es = ss should point to the start of the real-mode
kernel code (0x9000 if the code is loaded at 0x90000), sp should be
set up properly, normally pointing to the top of the heap, and
interrupts should be disabled.  Furthermore, to guard against bugs in
the kernel, it is recommended that the boot loader sets fs = gs = ds =
es = ss.


In our example from above, we would do:


/* Note: in the case of the "old" kernel protocol, base_ptr must
  be == 0x90000 at this point; see the previous sample code */


seg = base_ptr >> 4;


cli(); /* Enter with interrupts disabled! */


/* Set up the real-mode kernel stack */
_SS = seg;
_SP = 0x9000; /* Load SP immediately after loading SS! */


_DS = _ES = _FS = _GS = seg;
jmp_far(seg+0x20, 0);/* Run the kernel */


原创粉丝点击