How UEFI starts

brief introduction

Record how UEFI started and then boot hangs up at least with a clue.

Start process

The partition table format for UEFI is GPT, not MBR for Legacy BIOS.

ESP partition found

At boot-up time, UEFI needs to find the EFI partition (EFI System Partition) in the GPT partition table, which stores the boot files for each system. If your computer has multiple systems, generally a boot file is put into this partition when the operating system is installed.

So how do I determine which ESP partition is it?

The answer is determined by the partition type.

In the GPT partition table, a partition item is structured as follows:

OffsetLength (bytes)Description
0x016Partition Type GUID (zero means unused entry)
0x1016Unique Partition GUID
0x208StartingLBA
0x288EndingLBA
0x308Attributes
0x3872Partition Name

As you can see from the table above, the type of partition is represented by a 16 byte GUID. The corresponding type GUID for an ESP partition is C12A7328-F81F-11D2-BA4B-00A0C93EC93B

Note: There is no uniform standard for this type of GUID, but GUIDs used internally by different systems are specified. See the documentation for each system.

On Linux, you can view the type GUID of a partition by following the command:

❯ lsblk -o NAME,PARTTYPE     
NAME         PARTTYPE
loop0        
loop1        
sda          
├─sda1       0fc63daf-8483-4772-8e79-3d69d8477de4
├─sda2       ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
├─sda3       ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
└─sda4       ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
nvme0n1      
├─nvme0n1p1  0fc63daf-8483-4772-8e79-3d69d8477de4
├─nvme0n1p2  0fc63daf-8483-4772-8e79-3d69d8477de4
├─nvme0n1p3  0fc63daf-8483-4772-8e79-3d69d8477de4
├─nvme0n1p4  de94bba4-06d1-4d40-a16a-bfd50179d6ac
├─nvme0n1p5  ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
├─nvme0n1p6  e3c9e316-0b5c-4db8-817d-f92df00215ae
├─nvme0n1p7  ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
├─nvme0n1p8  de94bba4-06d1-4d40-a16a-bfd50179d6ac
├─nvme0n1p9  c12a7328-f81f-11d2-ba4b-00a0c93ec93b	#As you can see, this is the ESP partition
├─nvme0n1p10 0fc63daf-8483-4772-8e79-3d69d8477de4
└─nvme0n1p11 0657fd6d-a4ab-43c4-84e5-0933c84b4f4f

Of course, you can't remember this bunch of GUID s, but there's a more convenient way to find ESP partitions:

❯ sudo parted -l            
[sudo] password for z: 
Model: ATA WDC WD10EZEX-22M (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name  Flags
 1      1049kB  215GB  215GB  ntfs
 2      215GB   433GB  218GB  ntfs               msftdata
 3      433GB   540GB  107GB  ntfs         job   msftdata
 4      540GB   750GB  210GB  ntfs               msftdata


Model: Colorful CN600 512GB (nvme)
Disk /dev/nvme0n1: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size    File system     Name                          Flags
 1      1049kB  107GB  107GB   ext4
 2      107GB   193GB  85.9GB  ext4
 3      193GB   279GB  85.9GB  ext4
 4      279GB   280GB  555MB   ntfs            Basic data partition          hidden, diag
 5      280GB   280GB  105MB   fat32           Old EFI system partition      msftdata	#### Don't look at this. You think that by name you are an ESP partition
 6      280GB   280GB  16.8MB                  Microsoft reserved partition  msftres
 7      280GB   440GB  160GB   ntfs            Basic data partition          msftdata
 8      440GB   440GB  607MB   ntfs                                          hidden, diag
 9      440GB   441GB  537MB   fat32                                         boot, esp	##### Look here! Can esp see it?
10      441GB   504GB  63.1GB  ext4
11      504GB   512GB  8191MB  linux-swap(v1)                                swap

Determine Startup Order

As mentioned earlier, there can be multiple system startup files in an ESP partition, and the startup files for multiple systems are generally placed under the corresponding directory. Here is an example of an ESP partition:

~ via 🐍 v3.9.7 
❯ sudo ls /boot/efi/EFI/
boot  Manjaro  Microsoft  ubuntu

~ via 🐍 v3.9.7 
❯ sudo ls /boot/efi/EFI/Manjaro
grub.cfg  grubx64.efi

In the ESP partition, there is a / EFI directory where each system has its own subdirectories, such as Manjaro, ubuntu, Microsoft, etc. The subdirectories store the startup files for each system, such as / EFI/Manjaro/grubx64.efi. In addition, there is a boot directory under the / EFI directory, where the default startup files are stored.

So the problem is, so many system startup files are stored in the same ESP partition, which one to start first?

This involves BBS Priority.

Note: BBS = BIOS Boot Specification

When you toggle BIOS, you always see a thing called "BBS priority" in the motherboard configuration. This is a list, which stores the start priority of each system.

Therefore, which system to start first in the same ESP partition is determined by the BBS priority.

Where is the priority file?

No files, BBS priority is stored in the NVRAM of the motherboard.

NVRAM refers to Non-Volatile RAM, which is Non-Volatile RAM. This NVRAM is in the motherboard and not on the disk. It is also normal to think about it carefully because a system can have many hard disks. If the BBS priority is stored on the hard disk, then unplugging the hard disk that stores the boot order is not a mess.

It is important to note that, in general, when a system is installed, a boot entry for the newly installed system is written to NVRAM, but if for some reason it is not added successfully or accidentally deleted by itself, a BBS boot entry can be added through the efibootmgr command on Linux.

Load

Once you have found the ESP partition and determined the boot order, you should load the boot file. That is the boot file, actually the boot file for the OS. This article mainly wants to record the UEFI boot process, not the Bootloader, so the later process will not be written.

summary

UEFI start sequence:

  1. ESP partition found
  2. Read BBS priority from NVRAM to determine which OS to start
  3. Load bootloader for corresponding OS

Reference material

  • https://harddrivetalk.com/what-are-hard-drive-bbs-priorities-and-how-are-they-useful/
  • https://wiki.osdev.org/EFI_System_Partition
  • https://wiki.osdev.org/GPT

Tags: Linux Ubuntu

Posted on Fri, 17 Sep 2021 06:35:46 -0400 by Braclayrab