Master the fundamental concepts of boot sector development through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksMultiboot lets compliant bootloaders pass metadata to kernels like GRUB does for hobby OS projects. A valid header starts with magic 0x1BADB002, sets flags, and ends with a checksum making the sum of all three dwords zero. For example, flags 0x00000003 requests memory info and page-aligned modules.
nasmLoading…
QEMU can boot a Multiboot kernel with -kernel when the header is valid. GRUB's multiboot command passes a magic value in EAX and a pointer to the info structure in EBX at entry. The header must be dword-aligned and located within the first 8 KB of the file. If QEMU reports invalid magic, double-check endianness and that the checksum sum wraps to zero in 32-bit arithmetic.
You will embed a Multiboot-compliant header in your kernel image and boot it with QEMU/GRUB. This exercise requires the correct magic, flags, and checksum so a Multiboot loader recognizes your binary.
GRUB can boot any kernel that carries a Multiboot header: three little-endian 32-bit words (magic 0x1BADB002, flags, checksum) where magic + flags + checksum ≡ 0 (mod 2^32), 4-byte aligned and entirely within the first 8192 bytes of the file. Write the scanner a bootloader runs: find the header, verify it, decode the flags, work out exactly which file bytes get loaded where, and describe the hand-off.
cLoading…
Tokens are a byte (two hex digits), #XXXXXXXX (a 32-bit word written little-endian), or a double-quoted ASCII string without spaces.
Find: the first offset that is a multiple of 4, with offset + 12 ≤ min(SIZE, 8192), holding the magic. If there is none, look for a Multiboot2 magic 0xE85250D6 at a multiple of 8 with offset + 16 ≤ min(SIZE, 32768). Otherwise report that nothing was found.
Verify the checksum.
Flags:
Any other bit in 0-15 is required, and a loader that does not know it must refuse the kernel. Unknown bits in 17-31 are optional and ignored.
Load (bit 16): the words at +12..+28 are header_addr, load_addr, load_end_addr, bss_end_addr and entry_addr (the header needs 32 bytes).
header_offset − (header_addr − load_addr).load_end_addr − load_addr, or up to the end of the file when load_end_addr is 0.Without bit 16, the kernel is loaded from its ELF program headers.
cLoading…
unsupported required flags 0xNNNN: a bootloader must refuse this kernel (it stops there), or optional flags 0xNNNNNNNN ignored. load: from the kernel's ELF program headers. INVALID (magic + flags + checksum = 0xNNNNNNNN), and nothing more is printed.no Multiboot header in the first 8192 bytes, or no Multiboot header; found a Multiboot2 header at 0xNNNN instead.handoff):
error: header truncated (address fields need 32 bytes);error: load_addr 0x… is above header_addr 0x…;error: load would start N bytes before the file;error: load_end_addr 0x… is not above load_addr;error: load range needs N bytes but the file has M after offset 0xNNNN;error: bss_end_addr 0x… is below the loaded end;error: entry 0x… is outside the loaded image.image prints image: N bytes, and checksum prints checksum for flags 0x00000003 = 0xE4524FFB.error: image size must be 1..65536;error: no image;error: bad offset X;error: bad token X (the rest of the line is skipped);error: write past the end of the image;error: bad flags;error: bad line: LINE.Input:
cLoading…
Output:
cLoading…
Hidden tests cover unaligned and too-late magics, a Multiboot2-only image, required and optional unknown flags, video mode, a load_end of 0 (load to end of file), and every load error.