Skip to content
systemcoding.dev
Assembly

Inside the 512-Byte MBR: Boot Code, Partition Table and Signature

Decode the 512-byte master boot record field by field, from 446 bytes of boot code to four 16-byte partition entries and the 0xAA55 signature, with xxd output.

5 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the Linux, Intel and Python logos.

The first sector of a disk partitioned the traditional PC way is called the master boot record, and it packs three things into 512 bytes: a small program, a table describing up to four partitions and a two-byte marker that says the sector is bootable. Every byte has a fixed offset. The layout has not changed since the 1980s, and every BIOS, boot manager and partitioning tool still reads it. Understanding it is the key to writing a bootloader that coexists with a partition table, to reading a disk image with a hex dump and to knowing why a disk with a hand-written boot sector fails to mount. This post lays out the structure, decodes a sample table by hand and builds one in NASM 2.16, with output you can compare against xxd.

The layout

Offset (hex)Offset (dec)SizeContent
0x0000446Boot code
0x1B84404Optional disk signature (inside the code area)
0x1BC4442Usually zero
0x1BE44616Partition entry 1
0x1CE46216Partition entry 2
0x1DE47816Partition entry 3
0x1EE49416Partition entry 4
0x1FE5102Boot signature 0x55 0xAA

The code area is 446 bytes, and the last six of those are often not code: Windows stores a 32-bit disk signature at offset 440 followed by two zero bytes, so a bootloader that needs to coexist with it should keep its code under 440 bytes. The four partition entries take 64 bytes, and the signature the last two. 446 plus 64 plus 2 is 512.

The partition entry

Each 16-byte entry has the same fields:

Offset in entrySizeMeaning
01Boot flag: 0x80 = active, 0x00 = inactive
13CHS address of the first sector
41Partition type
53CHS address of the last sector
84LBA of the first sector (little-endian)
124Number of sectors (little-endian)

The CHS fields (cylinder, head, sector) are a legacy geometry address that stopped being meaningful when disks grew past what 24 bits can express, around 8 GiB. Modern tools write a placeholder of FE FF FF for the end address when it does not fit, and everything reads the LBA fields instead. The type byte says what the partition holds: 0x83 is Linux, 0x07 is NTFS or exFAT, 0x0B and 0x0C are FAT32, 0x82 is Linux swap and 0xEE marks a protective entry for a GPT disk. The LBA numbers are 32-bit, so an MBR disk addresses at most 2^32 sectors, which is 2 TiB with 512-byte sectors.

Decoding a real entry

Dump the table from a disk image with xxd:

Shell
xxd -s 446 -l 64 disk.img
Text
000001be: 8020 2100 83fe ffff 0008 0000 0050 0000  . !..........P..
000001ce: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001de: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001ee: 0000 0000 0000 0000 0000 0000 0000 0000  ................

The first entry is 80 20 21 00 83 fe ff ff 00 08 00 00 00 50 00 00. Decode it field by field:

  • 80: boot flag set, this partition is marked active.

  • 20 21 00: start CHS. The head is 0x20 = 32, the sector byte is 0x21, whose low six bits give sector 33, and the top two bits plus the third byte give cylinder 0. This is the CHS equivalent of LBA 2048 in the translation tools use, which is what fdisk aligns the first partition to.

  • 83: Linux filesystem.

  • fe ff ff: end CHS saturated, meaning "use the LBA".

  • 00 08 00 00: first sector LBA, little-endian 0x00000800 = 2048. The partition starts at 1 MiB, past the area where bootloaders keep their stage 1.5.

  • 00 50 00 00: size 0x00005000 = 20480 sectors, which is 10 MiB.

The other three entries are all zero, meaning unused. You can cross-check with sfdisk -d disk.img or fdisk -l disk.img, which print the same numbers in decimal, and the values should agree.

Building one in NASM

A bootloader that preserves a partition table writes the table into its own sector. The layout is assembled with times and db:

mbr.asmnasm
bits 16
org 0x7C00

start:
    cli
    xor  ax, ax
    mov  ds, ax
    mov  es, ax
    mov  ss, ax
    mov  sp, 0x7C00
    sti
    mov  si, msg
.print:
    lodsb
    test al, al
    jz   .halt
    mov  ah, 0x0E
    int  0x10
    jmp  .print
.halt:
    cli
    hlt
    jmp  .halt

msg: db "MBR ok", 13, 10, 0

times 440 - ($ - $$) db 0
dd 0x12345678          ; disk signature (offset 440)
dw 0                   ; reserved (offset 444)

; partition entry 1 (offset 446)
db 0x80
db 0x20, 0x21, 0x00
db 0x83
db 0xFE, 0xFF, 0xFF
dd 2048
dd 20480

times 48 db 0          ; entries 2 to 4 are unused
dw 0xAA55

The first times pads the code area to exactly 440 bytes. If the code were longer, NASM would raise a negative-count error, so a build failure is the signal that the code has outgrown the area. dd writes a 32-bit little-endian value, so dd 2048 becomes 00 08 00 00, the bytes you decoded above. The times 48 db 0 fills the three empty entries, 3 * 16 = 48, and dw 0xAA55 closes the sector. Assemble with nasm -f bin mbr.asm -o mbr.bin and check ls -l for 512 bytes.

What the BIOS does with it

The BIOS reads sector 0 of the boot device, checks that the last two bytes are 55 AA and jumps to offset 0 of the loaded copy at 0x7C00, with the boot drive number in dl. It does not look at the partition table. Interpreting it is the job of the boot code. A classic MBR does this: it relocates itself to a lower address like 0x0600 to free 0x7C00, scans the four entries for the active flag, loads that partition's first sector (the volume boot record) to 0x7C00 with an int 0x13 read, and jumps to it. That second sector is the code of the filesystem's own bootloader. This chain-loading is why the active flag matters, and why a table with no active entry gives "Missing operating system" from the standard boot code.

A boot sector for a floppy has no partition table at all: the whole sector is the volume boot record, with a filesystem header. The same 55 AA signature applies, and the same BIOS code loads it. That is why a hobby bootloader can ignore the table, as the earlier posts did, when booting from an image written directly to a raw device.

Limits and what replaced it

The structure has limits worth stating. Four primary partitions, or three plus an extended partition that holds a chain of logical ones. A 32-bit LBA, so disks over 2 TiB cannot be fully addressed. And no redundancy: the table is one sector, and a damaged sector loses it. The GUID Partition Table fixes all three, with 64-bit addressing, 128 entries by default and a backup copy at the end of the disk. A GPT disk still carries a protective MBR with a single entry of type 0xEE that covers the whole disk, so that old tools see a used disk and do not overwrite it. A BIOS can boot from a GPT disk if the first sector contains boot code, as GRUB's bios_grub partition arrangement does.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.