How does UEFI decide whoch efi file to boot into?

Solution 1:

It stores the paths in the motherboard's NVRAM (the same that still gets called "CMOS memory").

The details can be found in the freely-available UEFI Specification (section 3). The EFI NVRAM contains a list of boot option variables named Boot#### (e.g. Boot0001, Boot0002), each of which stores the exact path of a single .efi file – including the partition's GUID.

The boot options aren't used in numerical order, but in the order specified by the BootOrder variable. (The OS can also set BootNext to request EFI to reboot straight into one of the boot options, or OsIndications to request a reboot into the UEFI setup screen.)

You can see the boot-related EFI variables using:

  • On Linux: efibootmgr -v

  • On Windows: bcdedit /enum firmware

    (The bcdedit output uses Windows BCD-specific terminology, and everything looks quite different from the actual EFI variables, but each of the entries still represents one 'Boot####' item – except for {fwbootmgr}, which represents 'BootOrder'.)

And if you're booting from removable media (which the NVRAM of course won't know about) – or if you're booting from an internal disk, but none of the BootOrder options worked – the firmware will then look for one specific path in each EFI System Partition – always \EFI\Boot\BootX64.efi on x86_64 systems. (Section "3.5.1.1 Removable Media Boot Behavior" lists names for each CPU architecture.)

Most OS installers place a copy of their bootloader at \EFI\Boot\BootX64.efi to make sure the system still boots into something even if the NVRAM data is lost.

(Finally... some firmwares will automatically look for \EFI\Microsoft\Boot\Bootmgfw.efi, because that's where the Windows Boot Manager is placed. If you're lucky, the firmware will just use this as a 2nd fallback path. Yet there have been UEFI motherboards which insist on only using this path and refuse to boot anything else but Windows.)