================================================================
		     THE LINUX/I386 BOOT PROTOCOL
================================================================
		    H. Peter Anvin <hpa@zytor.com>
		 Updated as of protocol version 2.02

On the i386 platform, the Linux kernel uses a rather complicated boot
convention.  This has evolved partially due to historical aspects, as
well as the desire in the early days to have the kernel itself be a
bootable image, the complicated PC memory model and due to changed
expectations in the PC industry caused by the effective demise of
real-mode DOS as a mainstream operating system.

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.


**** 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  +------------------------+


When using bzImage, the protected-mode kernel was relocated to
0x100000 ("high memory"), and the kernel real-mode block (boot sector,
setup, and stack/heap) was made relocatable to any address between
0x10000 and end of low memory.  Unfortunately, in protocols 2.00 and
2.01 the command line is still required to live in the 0x9XXXX memory
range, and that memory range is still overwritten by the early kernel.
The 2.02 protocol fixes that.

It is desirable to keep the "memory ceiling" -- the highest point in
low memory touched by the boot loader -- as low as possible, since
some newer BIOSes have begun to allocate some rather large amounts of
memory, called the Extended BIOS Data Area, near the top of low
memory.  The boot loader should use the "INT 12h" BIOS call to verify
how much low memory is available.

Unfortunately, if INT 12h reports that the amount of memory is too
low, there is usually nothing the boot loader can do but to report an
error to the user.  The boot loader should therefore be designed to
take up as little space in low memory as it reasonably can.  For
zImage or old bzImage kernels, which need data written into the
0x90000 segment, the boot loader should make sure not to use memory
above the 0x9A000 point; too many BIOSes will break above that point.


**** 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.

The first step in loading a Linux kernel should be to load the
real-mode code (boot sector and setup code) and then examine the
following header at offset 0x01f1.  The real-mode code can total up to
32K, although the boot loader may choose to load only the first two
sectors (1K) and then examine the bootup sector size.

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/4  2.00+   start_sys       Points 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/4  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

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

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.

Most boot loaders will simply load the kernel at its target address
directly.  Such boot loaders do not need to worry about filling in
most of the fields in the header.  The following fields should be
filled out, however:

  type_of_loader:
	If your boot loader has an identifier assigned in
	arch/i386/boot/setup.S, enter that value.  Otherwise, enter
	0xFF here.

  loadflags, heap_end_ptr:
	If the protocol version is 2.01 or higher, enter the
	offset limit of the setup heap into heap_end_ptr and set the
	0x80 bit (CAN_USE_HEAP) of loadflags.  heap_end_ptr appears to
	be relative to the start of setup (offset 0x0200).

  setup_move_size:
	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.

  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 address 0x3C000000 if you want all kernels to
	read it.

  cmd_line_ptr:
	If the protocol version is 2.02 or higher, this is a 32-bit
	pointer to the kernel command line.  The kernel command line
	can be located anywhere between the end of setup and 0xA0000.
	Fill in this field even if your boot loader does not support a
	command line, in which case you can point this to an empty
	string (or better yet, to the string "auto".)  If this field
	is left at zero, the kernel will assume that your boot loader
	does not support the 2.02 protocol.


**** 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.

The kernel command line is a null-terminated string up to 255
characters long, plus the final null.

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.)

If the protocol version is *not* 2.02 or higher, the kernel
command line is entered using the following protocol:

	At offset 0x0020 (word), "cmd_line_magic", enter the magic
	number 0xA33F.

	At offset 0x0022 (word), "cmd_line_offset", enter the offset
	of the kernel command line (relative to the start of the
	real-mode kernel).

	The kernel command line *must* be within the memory region
	covered by setup_move_size, so you may need to adjust this
	field.


**** SAMPLE BOOT CONFIGURATION

As a sample configuration, assume the following layout of the real
mode segment:

	0x0000-0x7FFF   Real mode kernel
	0x8000-0x8FFF   Stack and heap
	0x9000-0x90FF   Kernel command line

Such a boot loader should enter the following fields in the header:

	unsigned long base_ptr; /* base address for real-mode segment */

	if ( setup_sects == 0 ) {
		setup_sects = 4;
	}

	if ( protocol >= 0x0200 ) {
		type_of_loader = <type code>;
		if ( loading_initrd ) {
			ramdisk_image = <initrd_address>;
			ramdisk_size = <initrd_size>;
		}
		if ( protocol >= 0x0201 ) {
			heap_end_ptr = 0x9000 - 0x200;
			loadflags |= 0x80; /* CAN_USE_HEAP */
		}
		if ( protocol >= 0x0202 ) {
			cmd_line_ptr = base_ptr + 0x9000;
		} else {
			cmd_line_magic  = 0xA33F;
			cmd_line_offset = 0x9000;
			setup_move_size = 0x9100;
		}
	} else {
		/* Very old kernel */

		cmd_line_magic  = 0xA33F;
		cmd_line_offset = 0x9000;

		/* A very old kernel MUST have its real-mode code
		   loaded at 0x90000 */

		if ( base_ptr != 0x90000 ) {
			/* Copy the real-mode kernel */
			memcpy(0x90000, base_ptr, (setup_sects+1)*512);
			/* Copy the command line */
			memcpy(0x99000, base_ptr+0x9000, 256);

			base_ptr = 0x90000;              /* Relocated */
		}

		/* It is recommended to clear memory up to the 32K mark */
		memset(0x90000 + (setup_sects-1)*512, 0,
		       (64-setup_sects-1)*512);
	}


**** 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.

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;

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.


**** 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.

  vga=<mode>
	<mode> here is either an integer (in C notation, either
	decimal, octal, or hexadecimal) or one of the strings
	"normal" (meaning 0xFFFF), "ext" (meaning 0xFFFE) or "ask"
	(meaning 0xFFFD).  This value should be entered into the
	vid_mode field, as it is used by the kernel before the command
	line is parsed.

  mem=<size>
	<size> is an integer in C notation optionally followed by K, M
	or G (meaning << 10, << 20 or << 30).  This specifies to the
	kernel the memory size.  This affects the possible placement
	of an initrd, since an initrd should be placed near end of
	memory.

  initrd=<file>
	An initrd should be loaded.  The meaning of <file> is
	obviously bootloader-dependent.

In addition, some boot loaders add the following options to the
user-specified command line:

  BOOT_IMAGE=<file>
	The boot image which was loaded.  Again, the meaning of <file>
	is obviously bootloader-dependent.

  auto
	The kernel was booted without explicit user intervention.

If these options are added by the boot loader, it is highly
recommended that they are located *first*, before the user-specified
or configuration-specified command line.  Otherwise, "init=/bin/sh"
gets confused by the "auto" option.


**** 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 = fs = gs should point to the start of the
real-mode kernel code (0x9000 if the code is loaded at 0x90000), and
sp should be set up properly, normally pointing to the top of the
heap.  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 right after loading SS! */

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

If your boot sector accesses a floppy drive, it is recommended to
switch off the floppy motor before running the kernel, since the
kernel boot leaves interrupts off and thus the motor will not be
switched off, especially if the loaded kernel has the floppy driver as
a demand-loaded module!


**** ADVANCED BOOT TIME HOOKS

If the boot loader runs in a particularly hostile environment (such as
LOADLIN, which runs under DOS) it may be impossible to follow the
standard memory location requirements.  Such a boot loader may use the
following hooks that, if set, are invoked by the kernel at the
appropriate time.  The use of these hooks should probably be
considered an absolutely last resort!

IMPORTANT: All the hooks are required to preserve %ebp, %esi and %edi
across invocation.

  realmode_swtch:
	A 16-bit real mode far subroutine invoked immediately before
	entering protected mode.  The default routine disables NMI, so
	your routine should probably do so, too.

  code32_start:
	A 32-bit flat-mode routine *jumped* to immediately after the
	transition to protected mode, but before the kernel is
	uncompressed.  No segments, except CS, are set up; you should
	set them up to KERNEL_DS (0x18) yourself.

	After completing your hook, you should jump to the address
	that was in this field before your boot loader overwrote it.

================================================================
Summary of empty_zero_page layout (kernel point of view)
     ( collected by Hans Lermen and Martin Mares )
================================================================

The contents of empty_zero_page are used to pass parameters from
the 16-bit realmode code of the kernel to the 32-bit part.
References/settings to it mainly are in:

  arch/i386/boot/setup.S
  arch/i386/boot/video.S
  arch/i386/kernel/head.S
  arch/i386/kernel/setup.c


Offset  Type            Description
------  ----            -----------
    0   32 bytes        struct screen_info, SCREEN_INFO
                        ATTENTION, overlaps the following !!!
    2   unsigned short  EXT_MEM_K, extended memory size in Kb (from int 0x15)
 0x20   unsigned short  CL_MAGIC, commandline magic number (=0xA33F)
 0x22   unsigned short  CL_OFFSET, commandline offset
                        Address of commandline is calculated:
                          0x90000 + contents of CL_OFFSET
                        (only taken, when CL_MAGIC = 0xA33F)
 0x40   20 bytes        struct apm_bios_info, APM_BIOS_INFO
 0x80   16 bytes        hd0-disk-parameter from intvector 0x41
 0x90   16 bytes        hd1-disk-parameter from intvector 0x46

 0xa0   16 bytes        System description table truncated to 16 bytes.
                        ( struct sys_desc_table_struct )
 0xb0 - 0x1df           Free. Add more parameters here if you really need them.

0x1e0   unsigned long   ALT_MEM_K, alternative mem check, in Kb
0x1f1   char            size of setup.S, number of sectors
0x1f2   unsigned short  MOUNT_ROOT_RDONLY (if !=0)
0x1f4   unsigned short  size of compressed kernel-part in the
                        (b)zImage-file (in 16 byte units, rounded up)
0x1f6   unsigned short  swap_dev (unused AFAIK)
0x1f8   unsigned short  RAMDISK_FLAGS
0x1fa   unsigned short  VGA-Mode (old one)
0x1fc   unsigned short  ORIG_ROOT_DEV (high=Major, low=minor)
0x1ff   char            AUX_DEVICE_INFO

0x200   short jump to start of setup code aka "reserved" field.
0x202   4 bytes         Signature for SETUP-header, ="HdrS"
0x206   unsigned short  Version number of header format
                        Current version is 0x0201...
0x208   8 bytes         (used by setup.S for communication with boot loader
                         look there)
0x210   char            LOADER_TYPE, = 0, old one
                        else it is set by the loader:
                        0xTV: T=0 for LILO
                                1 for Loadlin
                                2 for bootsect-loader
                                3 for SYSLINUX
                                4 for ETHERBOOT
                                V = version
0x211   char            loadflags:
                        bit0 = 1: kernel is loaded high (bzImage)
                        bit7 = 1: Heap and pointer (see below) set by boot
                                  loader.
0x212   unsigned short  (setup.S)
0x214   unsigned long   KERNEL_START, where the loader started the kernel
0x218   unsigned long   INITRD_START, address of loaded ramdisk image
0x21c   unsigned long   INITRD_SIZE, size in bytes of ramdisk image
0x220   4 bytes         (setup.S)
0x224   unsigned short  setup.S heap end pointer
0x226 - 0x7ff           setup.S code.

0x800   string, 2K max  COMMAND_LINE, the kernel commandline as
                        copied using CL_OFFSET.
                        Note: this will be copied once more by setup.c
                        into a local buffer which is only 256 bytes long.
                        ( #define COMMAND_LINE_SIZE 256 )
