Writing an OS Kernel from Scratch – Part 3: Multiboot Protocol Explained — The “Universal Language” Between Kernel and Bootloader
Your kernel is written, but how does GRUB “recognize” it? The secret lies in a protocol called Multiboot.
In the first two posts, we successfully ran our kernel on QEMU and understood the linker script and BIOS/GRUB roles. But did you notice a strange “magic number” at the beginning of boot.asm?
Those three lines are the core of the Multiboot protocol — they tell GRUB: “This is a legitimate, bootable OS kernel.”
The Problem: How Do Bootloader and Kernel “Talk”?
Imagine:
- You write a kernel (ELF file)
- You want to boot it with GRUB, LILO, SYSLINUX, or any bootloader
But the problem: every bootloader boots differently! The kernel doesn’t know who loaded it, nor the CPU state, memory layout, or command line parameters.
Thus, the Multiboot specification was born. Multiboot is an open standard proposed by the GNU project (originally for GNU Hurd), aiming to unify the interface between kernel and bootloader.
Multiboot Header
The kernel embeds a “header structure” (Multiboot Header) at the beginning of the ELF file. The bootloader scans this header when loading the kernel.
Requirements:
- Within the first 8192 bytes of the kernel
- 4-byte aligned
- Contains three key fields (12 bytes total): magic (0x1BADB002), flags, checksum
Flags bit 0: MB_PAGE_ALIGN (page-align kernel)
Flags bit 1: MB_MEM_INFO (provide memory info — critical!)
Flags bit 2: MB_VIDEO_MODE (request graphics mode)
Comments