Master Boot Record

From Gentoo Wiki
Jump to:navigation Jump to:search

The Master Boot Record (MBR) is the de facto standard boot sector of an IBM PC compatible with BIOS as its system firmware. This includes UEFI on x86 in BIOS compatibility mode (which UEFI technically calls "Compatibility Support Module", or CSM). The Master Boot Record was first introduced to IBM PCs with PC DOS 2.0 in 1983. MS-DOS 2.0, which was available on IBM PC compatible clones as well, is basically identical. The MBR is a 512-byte data structure containing system bootstrap code and a partition table.

Due to the great success of IBM PC compatibles, the MBR is used on most multi-platform devices such as USB pen drives, and at least supported on a great variety of other platforms. Almost every external storage device (USB pen drives, SD cards, external HDDs or SSDs) is either using MBR partitioning or the GUID Partition Table (GPT; the successor of the MBR on the PC platform).

Virtually all partition management tools from fdisk to parted fully support an MBR-style partition scheme. Master Boot Records have a number of limitations:

  • Partitions are limited to 32-bit entries, thus the maximum partition size is circa 2.2 TB.
  • The maximum number of primary partitions is fixed at 4.
  • BIOS implementations will not be able to address data (like a partition) above 2.2 TB.


The 2.2 TB barrier

The Master Boot Record uses 32 bit values to store sector values (LBA). With the standard sector size of 512 bytes, and the highest 32 bit number of 4,294,967,295, the theoretical capacity maximum of addressable sectors is ~2048 GiB or ~2199 GB, or rounded 2.2 TB.[1]

Most if not all BIOS implementations use 32 bit values to access logical sectors (called LBA, as opposed to C/H/S), so any part of a partition that is defined above 2.2 TB will not be accessible and, as a result, also not bootable. Furthermore most legacy operating systems use drivers that only support 32 bit values as well, which makes 2.2 TB the hard limit in any case. No sector beyond that barrier can be accessed safely.

Warning
Any partition crossing the 32 bit LBA barrier of 2.2 TB is prone to data loss!

Some operating systems, however, are able to process larger sector numbers, because they internally use more than 32 bits to store LBA values. Systems known to do that are e.g. Linux and FreeBSD. As for PCs running Microsoft Windows, any system before Windows 7 cannot be used—or at least not used safely—with partitions above the 2.2 TB barrier. Starting with Windows 7 a simple MBR tweak is possible, which is both valid within the MBR limits and the driver limits (when using the standard drivers from Microsoft). The tweak is to define the last partition just inside the 32 bit LBA range, so the start sector is a valid 32 bit LBA number and the resulting MBR data structure is absolutely correct. The partition size itself is also the largest possible 32 bit value, which roughly gives a total of 4 TB usable disk space on at least two partitions. This tweak is hardly worth the risks, considering that incompatible software that uses 32 bit LBA values for disk access is likely to destroy data when trying to access this last partition. Since Linux, FreeBSD and Windows systems, that support >32 bit MBR configurations, also support the GUID Partition Table, the later will be the much safer choice.[2]

Additionally, some systems may be able to handle different sector sizes, like 4096 bytes (also called "4K sectors/blocks" or "Advanced Format") instead of 512 bytes. If this is the case, and with the 32 bit limit of the MBR, the MBR limit is then ~17.5 TB (making one such big data partition possible). The problem hereby is that most BIOS implementations and most (existing, legacy) device drivers do not support other sector sizes than 512 bytes, ruling out the use of 4K sectors for boot drives. Also, most legacy software such as device drivers for IDE/ATA and SATA or system tools (disk or filesystem tools, like fdisk or chkdsk) do not support anything other than 512 byte sectors as well. Certain FireWire and USB enclosures shipped with special device drivers (for Microsoft Windows), allowing a 4K sector size and thus up to ~17.5 TB MBR partitioning. When such drives are connected to an internal ATA/SATA bus, depending on the actual system, existing partitions may not be usable and data may be corrupted.[3]

The Structure of the Master Boot Record

There are several variations of the Master Boot Record, all sharing a similar structure. The MBR introduced with MS-DOS 3.3 can be considered the Standard-MBR.[4] Unfortunately, not all MBR schemas are compatible with all OS implementations.

An early example might look like this:

Typical Early IBM-DOS Master Boot Record
Start Stop Bytes Description
0x00007c00 0x00007dbd 446 System Bootstrap code area.
0x00007dbe 0x00007dcd 16 Partition 1.
0x00007dce 0x00007ddd 16 Partition 2.
0x00007dde 0x00007ded 16 Partition 3.
0x00007dee 0x00007dfd 16 Partition 4.
0x00007dfe 0x00007dff 02 Boot signature 0xaa55, binary on-disk as 55 AA.[5]

A more modern Master Boot Record implementation has several more entries at the cost of a significantly reduced bootstrap code area. This adds support for a disk formatting time-stamp, and a 32-bit disk signature for use by the OS or UEFI if a Protective MBR is required for backwards compatibility.

Typical Modern Master Boot Record
Start Stop Bytes Description
0x00007c00 0x00007cd9 218 Bootstrap code area.
0x00007cda 0x00007cdb 02 Padding 0x0000.
0x00007cdc 0x00007cdc 01 Original first physical drive (0x800xff)
0x00007cdd 0x00007cdd 01 Seconds 0–59
0x00007cde 0x00007cde 01 Minutes 0–59
0x00007cdf 0x00007cdf 01 Hours 0–23
0x00007ce0 0x00007db7 216 Extended bootstrap code
0x00007db8 0x00007dbb 04 Optional disk signature.
0x00007dbc 0x00007dbd 02 Copy protection flag (True: 0x5a5a/False: 0x0000)
0x00007dbe 0x00007dcd 16 Partition 1.
0x00007dce 0x00007ddd 16 Partition 2.
0x00007dde 0x00007ded 16 Partition 3.
0x00007dee 0x00007dfd 16 Partition 4.
0x00007dfe 0x00007dff 02 Boot signature 0xaa55.

Master Boot Record Partitions

Partition Table Layout
Start Stop Bytes Description
0x00 0x00 1 byte Partition boot flag. High bit set when bootable, 0x00 when not; anything else is an error.
0x01 0x03 3 bytes Absolute location of first sector, in Cylinder-Head-Sector format.
0x04 0x04 1 byte Partition type indicator
0x05 0x07 3 bytes Absolute location of last sector, in Cylinder-Head-Sector format.
0x08 0x0b 4 bytes Absolute location of first sector, in Logical Block Addressing format.
0x0c 0x0f 4 bytes Partition sector count.

The structure of individual partition entries is fixed at 16 bytes. This has a number of implications which ultimately limit the MBR format. The first byte indicates whether or not the partition is bootable, but only the 7th bit is significant. No other flags exist. Directions to the first sector of the partition follow, in the antiquated C-H-S format. The partition indicator is only one byte wide, so only 256 unique values are possible. Unfortunately, over the decades many file systems have reserved the same byte value to mean different file systems.

Master Boot Records Nested Within GPT Partitions

It is possible to nest a Master Boot Record — complete with partition table — within an outer GPT partition table. This is nearly always done for backwards compatibility by BIOS-based firmware that happen to directly support GPT partition tables or by BIOS loaded as a UEFI application.

In this case, the GPT table's MBR entry will contain a single partition which will be of the maximum possible partition size. The partition type at offset 0x00007dc1 will of the type 0xee. This is called a Protective Master Boot Record.

An MBR record within a GPT partition table always have the following GUID: 024DEE41-33E7-11D3-9D69-0008C781F39F.

MBR Backup and Recovery

It is possible to backup a Master Boot Record for later recovery with a simple Bash one-liner:

root #dd if=/dev/<disk> of=~/backup.mbr bs=512 count=1

Later recovery is possible with the following command:

root #dd if=backup.mbr of=/dev/<disk>

See Also

  • BIOS — the standard firmware of IBM-PC-compatible computers until it was phased out in 2020.
  • CMOS BIOS Memory — a few bytes of battery-backed SRAM used to preserve BIOS settings and Real Time Clock data when a PC is off.
  • UEFI — a firmware standard for boot ROM designed to provide a stable API for interacting with system hardware. On x86 it replaced the legacy BIOS.
  • GPT — a partitioning scheme widely adopted in contemporary computers to organize and manage data on storage devices.
  • System Initialization of Intel x86 with BIOS Firmware — the process of bringing an Intel x86 system up from a cold start.

References