Linux › Section E · How the Linux kernel boots › Topic E.5
Boot loader details: MBR and UEFI boot
PCs start boot loaders in two very different ways. Legacy BIOS boot squeezes code into the first sector of a disk; UEFI boot reads ordinary files from a small FAT partition and keeps a list of boot entries in firmware memory. Knowing both explains where GRUB really lives and how to repair it.
Two ways to start a PC
The traditional PC BIOS knows nothing about filesystems: after its power-on self-test it reads one sector from a disk and runs it. UEFI, the Unified Extensible Firmware Interface, replaced it on almost all PCs sold in the last decade or so. UEFI firmware understands GPT partition tables and FAT filesystems, so it can open a boot loader as a named file. Many UEFI machines could also emulate a BIOS through a Compatibility Support Module (CSM), though recent machines often omit it. To check how the running system booted, test for /sys/firmware/efi: present means UEFI, absent means legacy BIOS mode.
Inside the 512-byte MBR
On an MBR disk, sector 0 has a fixed layout. The first 446 bytes are the boot code area; on most current disks the last 6 of those hold a 4-byte disk signature and 2 spare bytes, leaving 440 bytes for code. Next comes the partition table: four entries of 16 bytes each, 64 bytes in all. The sector ends with the 2-byte boot signature, the bytes 55 and AA in hexadecimal, which tells the BIOS the sector is bootable. The BIOS loads the whole sector into memory and jumps to its first byte. About 440 bytes is far too little for a boot loader that reads filesystems and draws menus, so MBR boot loaders work in stages.
Worked example: the MBR and the gap
Check the MBR layout and measure the space GRUB uses after it. 1. Add the parts: \(446 + 4 \times 16 + 2 = 446 + 64 + 2 = 512\) bytes, exactly one sector. 2. On a disk whose first partition starts at sector 2048, sectors 1 to 2047 belong to no partition. 3. That gap is \(2047 \times 512 = 1{,}048{,}064\) bytes, about 1,023.5 KiB, just under 1 MiB. 4. GRUB's first stage, boot.img, fits in the code area of sector 0; its core image, core.img, with the drivers needed to read /boot/grub, goes into the gap. 5. Older disks partitioned with the first partition at sector 63 left only 62 sectors, 31 KiB, which is why modern 1 MiB alignment also helps boot loaders.
Multistage MBR boot
GRUB's BIOS boot runs in steps. 1. The BIOS loads sector 0, which contains GRUB's boot.img, and runs it. 2. boot.img knows only the location of core.img and loads it, usually from the gap after the MBR. 3. core.img contains just enough of GRUB, including modules for the partition table and filesystem of /boot, to find the GRUB directory, normally /boot/grub. 4. GRUB loads further modules and grub.cfg from there, shows the menu and loads the kernel. The weak point is that the gap is not protected by any partition, so other software, such as some disk tools or licence managers, can overwrite core.img and leave the machine unbootable.
GPT disks with a BIOS
GPT keeps a protective MBR in sector 0, so a BIOS can still run boot code from it, but the gap trick fails: sectors 1 to 33 hold the GPT header and partition entries. The solution is a BIOS boot partition, a small partition, about 1 MiB, with the type GUID 21686148-6449-6E6F-744E-656564454649 (shown as BIOS boot in fdisk, or set with parted's bios_grub flag). grub-install places core.img there, and it carries no filesystem. A BIOS boot partition is not an EFI system partition; a disk needs both only if it must boot either way, as some cloud images do.
UEFI boot and the EFI system partition
UEFI boot needs no hidden sectors. Boot loaders are ordinary files on the EFI system partition (ESP), a FAT-formatted partition, usually FAT32, with type GUID C12A7328-F81F-11D2-BA4B-00A0C93EC93B (EFI System in fdisk). Linux commonly mounts it at /boot/efi, though some systems use /efi or /boot. Inside, an EFI directory holds one subdirectory per operating system, such as EFI/ubuntu, EFI/fedora or EFI/Microsoft, each with .efi programs like shimx64.efi and grubx64.efi. EFI/BOOT/BOOTX64.EFI is the fallback path that firmware tries on removable media, or when no boot entry works. The loader must be a UEFI build: BIOS boot code cannot run here.
Boot entries in NVRAM
UEFI firmware keeps its boot menu in non-volatile memory (NVRAM) as variables. Each Boot#### variable, with #### a four-digit hexadecimal number, names a label, a disk partition and a loader path. BootOrder lists the entries in the order the firmware tries them, BootNext overrides that order for one boot only and is then deleted, and BootCurrent shows which entry started the running system. Installing a loader therefore has two parts: copying files to the ESP and registering an entry. grub-install does both, calling efibootmgr for the second. If the entry is lost, for example after a firmware reset, the fallback path or a new entry gets the machine booting again.
Worked example: reading efibootmgr
As root, efibootmgr prints (illustrative): BootCurrent: 0001, BootOrder: 0001,0000,0003, then Boot0000* Windows Boot Manager, Boot0001* ubuntu, Boot0003* UEFI USB Stick. 1. BootCurrent is 0001, so this system started from the ubuntu entry. 2. The firmware tries 0001 first, then 0000, then 0003. 3. The asterisk marks an entry as active. 4. To boot the USB stick once without changing the order, run efibootmgr -n 3, which sets BootNext. 5. To try Windows first on every boot, run efibootmgr -o 0,1,3. 6. To delete the USB entry, run efibootmgr -b 3 -B. 7. efibootmgr -v adds each entry's device path and loader file, for example the path to shimx64.efi.
How GRUB gets going, in both modes
Putting it together, GRUB starts in these steps. 1. Firmware initialises the hardware and looks for boot code: sector 0 of a disk under BIOS, or the loader file named by a boot entry under UEFI. 2. GRUB's core is loaded: from the gap or a BIOS boot partition via boot.img under BIOS, or as grubx64.efi from the ESP, perhaps through shim, under UEFI. 3. The core finds the GRUB directory and reads grub.cfg. 4. It loads any extra modules and shows the menu, or waits for the timeout. 5. It runs the chosen entry: the linux and initrd commands load the kernel and initramfs, and boot starts the kernel, as described in "Boot loaders and GRUB".
Common mistakes to avoid
First, installing GRUB in BIOS mode from a live USB that itself booted in legacy mode, then wondering why a UEFI machine will not start it: check /sys/firmware/efi before running grub-install. Second, running grub-install on a partition such as /dev/sda1 instead of the disk /dev/sda for BIOS boot. Third, formatting the ESP by mistake, which removes every operating system's loader. Fourth, confusing the BIOS boot partition with the ESP. Fifth, deleting a Boot#### entry you still need; note the number and the loader path from efibootmgr -v first. Sixth, assuming the MBR's 440 bytes hold all of GRUB.
Key takeaways
Legacy BIOS runs the 512-byte MBR: 446 bytes of boot code area (440 for code), four 16-byte partition entries and the 55 AA signature. GRUB's boot.img there loads core.img from the gap before the first partition, or from a BIOS boot partition on GPT disks. UEFI reads .efi files from the FAT-formatted ESP, usually mounted at /boot/efi, with EFI/BOOT/BOOTX64.EFI as the fallback. Boot#### entries, BootOrder, BootNext and BootCurrent live in NVRAM and are managed with efibootmgr. /sys/firmware/efi shows whether the system booted through UEFI.
Flashcards
Flashcard
Firmware loads and runs sector 0 of a disk
Test your understanding
Question 1 of 5
How many bytes of an MBR does the partition table occupy, and why?
Question 2 of 5
Why does BIOS booting from a GPT disk need a BIOS boot partition?
Question 3 of 5
efibootmgr shows BootOrder: 0002,0000. You want to start entry 0005 for the next boot only. Which command does that?
Question 4 of 5
On a machine that boots through UEFI, where is the boot loader that the firmware starts?
Question 5 of 5
Before running grub-install, how can you tell whether the current system booted through UEFI?
