Writing an OS Kernel from Scratch | MBR Partition: How Disks Are Sliced Into Blocks and How Multi-Boot Systems Coexist

Have you ever had this experience: You buy a new hard drive, plug it into your computer, open the disk management tool, and see a blank slate with “Unallocated” written on it — then you right-click “New Simple Volume,” choose a size, click, and the hard drive gets sliced into several pieces — C: drive, D: drive, E: drive.

This “slicing the hard drive” operation is called partitioning. How does it work? Why can one physical hard drive have both Windows and Linux installed simultaneously? What is MBR? Why is the partition table placed in the very first sector of the disk? Why can’t drives over 2TB use MBR mode?

Today, we’ll break down the complete principles of MBR partitioning — from every byte of the data structures to hands-on multi-boot operations.


MBR Physical Structure: What’s Inside 512 Bytes

The MBR (Master Boot Record) is not a partition; it’s a boot record. It’s fixed at the first sector of the disk (LBA 0), occupying 512 bytes. Within these 512 bytes, only the first 446 bytes are code, the next 64 bytes are the partition table, and the last 2 bytes are the boot signature.

Bash
MBR 512-byte Layout (LBA 0):
┌─────────────────────────────────────────┐
│ 0x0000 - 0x01BD (446 bytes): Bootloader Code    │
│   (GRUB Stage 1 lives here, or Windows' BOOTMGR)│
├─────────────────────────────────────────┤
│ 0x01BE - 0x01FD (64 bytes): Partition Table (4×16 bytes) │
│   Entry 1: 0x01BE - 0x01CD                │
│   Entry 2: 0x01CE - 0x01DD                │
│   Entry 3: 0x01DE - 0x01ED                │
│   Entry 4: 0x01EE - 0x01FD                │
├─────────────────────────────────────────┤
│ 0x01FE - 0x01FF (2 bytes): 0x55 0xAA (Boot Signature)   │
└─────────────────────────────────────────┘

Use hexdump to take a look at your disk’s MBR:

Bash
# Read the MBR of the first hard drive (doesn't need root, just needs read access to /dev/sda)
dd if=/dev/sda bs=1 count=512 2>/dev/null | hexdump -C | head -30

# Sample output (partial):
# 00000000  eb 63 90 00 00 00 00 00  00 00 00 00 00 00 00 00  |.c..............|
# ... boot code ...
# 000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
# 000001c0  01 00 07 fe ff ff 3f 00  00 00 d8 63 19 00 00 00  |.......?...c....|
# 000001d0  80 01 05 0f fe ff ff 57  d8 63 19 00 f8 7f 19 00  |......W.c.......|
# 000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
# 000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
#                                                      ^^^^
#                                                      Boot signature 0x55AA

If you’re using Linux, the MBR bootloader code area might be occupied by GRUB; if it’s an original Windows system, this is usually BOOTMGR (Windows Boot Manager).


Partition… [truncated]

Last modified: 2024年1月2日

Author

Comments

Write a Reply or Comment

Your email address will not be published.