Introduction
The Linux kernel for a PC is one file, called bzImage when it is built and vmlinuz where a distribution installs it. Its first
bytes are not a kernel but the boot protocol: a boot sector, a header that a boot loader reads and fills in, and a 16-bit setup program. The
kernel itself follows, usually compressed, with a small decompressor in front of it. The protocol is described in the kernel's own
Documentation/arch/x86/boot.rst, read as raw text for this page (the wording here is our own). The file checked on this page is the Alpine Linux
vmlinuz-lts for x86-64 (14,607,360 bytes, kernel 6.18.60), which file also reads as an x86 bzImage.
All numbers are little-endian. A kernel more than a few years old is also an EFI application: the file starts with MZ, and a PE header sits
in the first sector beside the boot protocol header, so that a UEFI firmware can start the kernel directly.
Layout of the file
| Part | Where | What it is |
|---|---|---|
| Boot sector | 0 to 0x1FF | The 512-byte boot sector. It ends with 0xAA55 at 0x1FE. Its tail is the start of the setup header (from 0x1F1). In an EFI-capable kernel it begins with the DOS and PE headers. |
| Setup header | 0x1F1 to 0x202 plus the jump byte at 0x201 | The boot protocol header, below. 0x26C long in protocol 2.15. |
| Real-mode setup code | to (setup_sects + 1) * 512 | 16-bit code that sets up the machine. setup_sects counts the 512-byte sectors after the boot sector, and a value of 0 means 4. The real file has 39. |
| Protected-mode kernel | from (setup_sects + 1) * 512, syssize paragraphs of 16 bytes | The code the boot loader loads at 0x100000 (or 0x10000 for a zImage-type kernel, loadflags bit 0 clear). It holds the decompressor and, at payload_offset, the compressed kernel. |
In the real file the protected-mode part starts at 20,480 (0x5000); the payload is at 21,188, 708 bytes after it, and is 14,514,245 bytes long. It is a gzip stream (1F 8B 08); the explorer opens it as a file of its own. After the payload come 71,927 more bytes of the protected-mode part, which this page does not describe (by our reading they hold the stub's data and the kernel_info structure: the kernel_info_offset in the file, 14,576,832, falls inside them).
The setup header
Offsets are from the start of the file. "Protocol" is the first boot protocol version with the field; the viewer shows only the fields the file's version and its header length allow.
| Offset | Size | Protocol | Field | What it holds |
|---|---|---|---|---|
0x1F1 | 1 | all | setup_sects | The setup size in 512-byte sectors (0 means 4). |
0x1F2 | 2 | all | root_flags | If set, the root is mounted read-only. |
0x1F4 | 4 | 2.04 | syssize | The size of the protected-mode code in 16-byte paragraphs. Before 2.04 only the low two bytes are usable. |
0x1F8 | 2 | all | ram_size | Do not use. |
0x1FA | 2 | all | vid_mode | Video mode control. |
0x1FC | 2 | all | root_dev | Default root device number. |
0x1FE | 2 | all | boot_flag | 0xAA55. |
0x200 | 2 | 2.00 | jump | A jump: 0xEB and a signed offset from 0x202. It gives the header's size (0x6A in the real file: the header ends at 0x26C). |
0x202 | 4 | 2.00 | header | The magic HdrS (0x53726448). Without it the kernel is "old": a zImage to load low, with no initrd. |
0x206 | 2 | 2.00 | version | (major << 8) + minor: 0x020F is 2.15. |
0x208 | 4 | 2.00 | realmode_swtch | A boot loader hook. |
0x20C | 2 | 2.00 | start_sys_seg | The load-low segment, 0x1000; obsolete. |
0x20E | 2 | 2.00 | kernel_version | A pointer, from 0x200, to the version string: "6.18.60-0-lts (buildozer@build-3-24-x86_64) ..." in the real file. |
0x210 | 1 | 2.00 | type_of_loader | Written by the boot loader: 0xTV, T the loader (0 LILO, 1 Loadlin, 2 bootsect-loader, 3 Syslinux, 4 Etherboot/gPXE/iPXE, 5 ELILO, 7 GRUB, 8 U-Boot, 9 Xen, 0xA Gujin, 0xB Qemu, 0xC uCbootloader, 0xD kexec-tools, 0xE extended: see ext_loader_type, 0xF special: 0xFF is undefined), V its version. |
0x211 | 1 | 2.00 | loadflags | Bit 0 LOADED_HIGH (the protected-mode code is loaded at 0x100000); bit 1 KASLR_FLAG (used inside the kernel); bit 5 QUIET_FLAG; bit 6 KEEP_SEGMENTS (obsolete); bit 7 CAN_USE_HEAP. |
0x212 | 2 | 2.00 | setup_move_size | The move-to-high-memory size, used with the hooks. |
0x214 | 4 | 2.00 | code32_start | A boot loader hook: where the protected-mode code is entered. |
0x218 | 4 | 2.00 | ramdisk_image | The initrd address, written by the loader. |
0x21C | 4 | 2.00 | ramdisk_size | The initrd size, written by the loader. |
0x220 | 4 | 2.00 | bootsect_kludge | Do not use. |
0x224 | 2 | 2.01 | heap_end_ptr | Free memory after the end of the setup. |
0x226 | 1 | 2.02 | ext_loader_ver | More bits of the loader's version. |
0x227 | 1 | 2.02 | ext_loader_type | The loader ID minus 0x10 when type_of_loader says 0xE. |
0x228 | 4 | 2.02 | cmd_line_ptr | A 32-bit pointer to the command line. |
0x22C | 4 | 2.03 | initrd_addr_max | The highest legal initrd address (0x7FFFFFFF in the real file). |
0x230 | 4 | 2.05 | kernel_alignment | The alignment the kernel needs: 16 MiB (0x1000000) in the real file. |
0x234 | 1 | 2.05 | relocatable_kernel | Whether the kernel can be loaded at another address. |
0x235 | 1 | 2.10 | min_alignment | The minimum alignment as a power of two (21, 2 MiB, in the real file). |
0x236 | 2 | 2.12 | xloadflags | Bit 0 XLF_KERNEL_64 (a 64-bit entry point at 0x200), bit 1 XLF_CAN_BE_LOADED_ABOVE_4G, bit 2 XLF_EFI_HANDOVER_32, bit 3 XLF_EFI_HANDOVER_64 (handover at handover_offset + 0x200), bit 4 XLF_EFI_KEXEC. Bits 5 to 7 are named in bootparam.h, not in boot.rst: XLF_5LEVEL, XLF_5LEVEL_ENABLED, XLF_MEM_ENCRYPTION. The real file has 0x7F. |
0x238 | 4 | 2.06 | cmdline_size | The longest command line, without its terminating zero (2047). |
0x23C | 4 | 2.07 | hardware_subarch | 0 default PC, 1 lguest, 2 Xen, 3 Intel MID, 4 CE4100 TV platform. |
0x240 | 8 | 2.07 | hardware_subarch_data | Data for the subarchitecture. |
0x248 | 4 | 2.08 | payload_offset | From the start of the protected-mode code to the payload. The payload's compression is told by its first bytes: gzip 1F 8B (or 1F 9E), bzip2 42 5A, LZMA 5D 00, XZ FD 37, LZ4 02 21, ZSTD 28 B5. |
0x24C | 4 | 2.08 | payload_length | The payload's length. |
0x250 | 8 | 2.09 | setup_data | A 64-bit pointer to a linked list of setup_data structures. |
0x258 | 8 | 2.10 | pref_address | The preferred load address (0x1000000). |
0x260 | 4 | 2.10 | init_size | The memory the kernel needs while it starts (about 44 MiB in the real file). |
0x264 | 4 | 2.11 | handover_offset | The offset of the EFI handover entry point. |
0x268 | 4 | 2.15 | kernel_info_offset | The offset, from the start of the protected-mode code, of the kernel_info structure. |
Protocol versions
As boot.rst lists them: 2.00 (kernel 1.3.73) bzImage and initrd; 2.01 heap overrun warning; 2.02 (2.4.0) the new command line protocol; 2.03 the highest initrd address; 2.04 (2.6.14) four-byte syssize; 2.05 (2.6.20) relocatable kernel; 2.06 command line size; 2.07 (2.6.24) paravirtualised boot; 2.08 (2.6.26) checksum and ELF payload, payload_offset and payload_length; 2.09 setup_data; 2.10 (2.6.31) relaxed alignment, init_size, pref_address; 2.11 (3.6) the EFI handover offset; 2.12 (3.8) xloadflags; 2.13 (3.14) 32- and 64-bit flags together; 2.14 burnt by an incorrect commit (treat as 2.13); 2.15 (5.5) kernel_info.
The protected-mode kernel and its payload
The decompressor and boot code come first, then the payload at payload_offset. The payload is the compressed kernel proper; the explorer shows it as a node that opens as a file of its own (payload.gz for gzip). The unpacked kernel is not looked at on this page.
The EFI stub
When the file starts with MZ a PE header follows (its offset is at 0x3C: 0x40 in the real file). The real file has an x86-64 PE with four sections: .setup, .compat, .text and .data. The viewer reads the machine and the section names. The PE format is described on the EXE pages; this page does not repeat it.
What the viewer shows
The file as boot sector (with the EFI stub's headers when there are any) and setup header, real-mode setup code, protected-mode kernel (decompressor, payload, rest), and anything after the kernel that syssize does not count. Each header field has its value and meaning, flags are spelled out, and the version string is read. The payload opens as a file of its own.
The explorer calls a file an x86 kernel image only when 0xAA55 is at 0x1FE, HdrS at 0x202, the protocol version is 2.x, the byte at 0x200 is 0xEB and the file is long enough for its setup. The .img and .efi rows of the explorer ask for it; a file with no extension is not found by name alone.
Checked on the real Alpine file (fields read with Buffer reads and compared with file's own report, payload gunzipped with Node's zlib), and on built files for every protocol version and every branch of the parser. A strict test is not a promise: over 20,000 random buffers the test claimed none.
Not described
Known, not yet covered
- Viewer: the real-mode setup code and the decompressor are machine code; they are not disassembled.
- Reference and viewer: the
kernel_infostructure (protocol 2.15), thesetup_datalist, the boot parameters (boot_params, the "zero page") the loader fills in and the 32-, 64-bit and EFI entry points are not described. - Reference: the checksum of protocol 2.08 and the ELF payload form are not described; the payload of the real file is gzip, and others were not seen.
- Viewer: the PE sections of the EFI stub are listed by name only (see the EXE pages for the format).
- Viewer: a file with no extension is not recognised by name alone (the explorer's by-magic finder has no clause for it).
Unknown
- Reference: kernels older than boot protocol 2.00 have no
HdrSand no reliable magic; they are not recognised. No real file older than 2.15 was available, so the older versions rest on the document and on built files.
Links
Specifications
- The Linux/x86 Boot Protocol – boot.rst in the kernel tree (GPL-2.0)
- bootparam.h – the structure and the flag names