UEFI, ESP, secure boot, and GRUB
I haven’t really thought about how a Linux system booted in years, back in the old MBR days. (“Load the first block and run the code”, more or less). Modern PCs are much more complicated with secure boot and UEFI. Some notes on what I learned reading about it. I’m cranky about all of this complexity; particularly the idea there’s some security layer in my firmware that prevents me from booting disk images I installed on purpose. I realize this is to protect against malware that installs itself in the boot chain, but I’m not honestly that worried about that risk.
Summary: modern motherboards boot with UEFI. The BIOS looks to mount a drive with an EFI System Partition (ESP) on it. The ESP is a fat32 filesystem with signed .efi blobs that are executed. GRUB’s update tool (run in user space when installing itself or new kernels) creates an ESP with GRUB EFIs on it. When loaded, GRUB in turn loads the Linux kernel and runs it. The rest of these notes are more confused because I took them while I was learning all this.
First off, can you just turn all this UEFI stuff off? Maybe. ASUS motherboards have ways to turn off “safe boot” (which I think means ignore keys) and enable “compatibility support module” which disables UEFI. However CSM mode does not work with Intel integrated graphics. The option to do that is greyed out. I also have no option to just turn off safe boot. I think no CSM means I can’t boot from an MBR, I must have GPT partitioned disks. And no way to turn off safe boot means whatever I want to boot via GPT/UEFI must be a signed binary blessed by the spirit of Ballmer himself. I did see some references that if you deleted all the secure keys in ASUS BIOS that would enable booting anything unsigned, but I was too scared to try.
But I got ahead of myself. What are MBR and GPT? Those are two ways to create partition tables on a PC hard drive. MBR is the old way and partitions are marked with very sparse data (like a single byte for type); see BIOS boot partition for more info. (I can swear in the very olden days you didn’t need a boot partition at all; LILO’s bootstrap was tiny but enough to load the kernel off the root partition). GPT is the modern way. It’s the GUID Partition Table, so everything has verbose GUID names. There’s no reason to use MBR any more on PCs. GPT is some 20 years old, ubiquitous, cleaner, has no weird size limits, and is now required for complicated things. In Linux you can see what kind of partition table you have with parted -l.
I just set up Ubuntu on a new hard drive (USB, spinning) and let it install. It created two partitions. /dev/sdb2 is what I expected, a big disk formatted with ext4 with my Linux root filesystem. /dev/sdb1 is a boot partition; 537MB, FAT32, named “EFI System Partition” with the flags “boot, ESP”. That’s the EFI System Partition.
What’s on the ESP? You can mount it just like any fat32 file system. Inside are directories EFI/BOOT and EFI/ubuntu. Apparently this is all UEFI boot stuff, various .EFI files have signatures checked and then are loaded and run. Some details here. The Ubuntu directory has a couple of small GRUB config files and what I think must be a copy of the GRUB executable itself.
So now we get to UEFI itself. Useful resources for that: Ubuntu docs, Arch Wiki, Arch Wiki: GRUB, Wikipedia, Windows Boot and UEFI, this Stack answer. For something more complex see this proposal for a new secure boot.
Greatly simplified, my understanding is UEFI’s job is to find boot code someone installed and run it. The UEFI BIOS finds a boot manager (perhaps chosen from the BIOS boot menu or set up via a command line tool like efibootmgr), which then finds a boot loader like GRUB, which then loads the operating system kernel. In theory all this stuff works on both MBR and GPT file systems but in practice you usually see UEFI used with GPT. Note that UEFI is basically a whole operating system with its own shell. One fancy thing you’ll see is the BIOS logo on the screen along with the Ubuntu screen and a loading spinner as various parts of the boot process are all drawing on the screen.
One Linux I installed ended up telling the existing Windows Boot Manager on my SSD about the GRUB boot loader for my new Ubuntu install on a USB drive. I think Ubuntu chose that because I already had Windows on the system. The BIOS menu let me choose which of the two OSes to launch but the Windows Boot Manager was in the path of the process. In another case I had no OS at all and Ubuntu just put GRUB directly on the USB drive; I guess it was acting both as boot manager and boot loader? “Manager” vs “Loader” is not used consistently in the stuff I’ve read, so I may have the details wrong.
From Linux’ point of view, all this UEFI rigamarole is to boot GRUB. GRUB is basically a mini operating system whose job it is to launch the Linux kernel. (There was an older GRUB v1; all modern systems are using GRUB 2.) I don’t really understand GRUB very well, it has menus you can configure to do complicated things and behind the scenes has enough code to find and load the kernel from a bunch of different device types. So it loads that kernel and hands off control to it.
If this all sounds overly complicated it is. BIOS picks a boot loader which runs GRUB which picks a Linux kernel to load? It’s possible to bypass GRUB entirely and have Linux load directly with EFISTUB. I find GRUB sort of comforting though and see no reason to remove it. It’s the way Ubuntu prefers to let you choose between kernels when booting.
First practical question; is your Linux system booting via UEFI? These docs have a nice hint; look for the presence of a /sys/firmware/efi directory. Another way to tell is use parted and look at flags; my UEFI boot partition is flagged boot, esp whereas my MPT style boot partition is flagged bios_grub.
Second practical question: how do I update / install UEFI booting? There are procedures for converting a system to boot via UEFI, see the Ubuntu docs. They sound scary. They reference a tool called Boot Repair which is a handy rescue disc. It has a sort of wizard which is basically “fix my boot”. It’s also 4 years old but is apparently still the recommended tool? The Ubuntu Server install ISO also has a repair mode.
Third practical question: is secure boot really an obstacle? I don’t think so. Modern Linux distributions have some signed shim that allows the Linux boot code to pass the security checks. I have no idea how this works or why it’s considered acceptable in the face of malware. But I think in practice if a Linux tool installs an ESP at all, it will have the needed signatures.
Update: an August 2022 update has revoked a bunch of signing keys, rendering some systems signed with older keys unbootable. Including Ubuntu 20.04 (reportedly).