Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the GRUB2 UEFI loader know where to look for the configuration file (or where the 2nd stage's files are located)?

If I use GRUB2 on a GPT-enabled partition how does loader "know" where to find its configuration file and other 2nd stage's files?

Note: I found some mentions about a config file which is located in the same folder as GRUB's EFI loader and contains chained load of "primary" configuration file from the specified partition, but that definitely is not true - there is only one "something.efi" file.

like image 573
Sap Avatar asked Feb 08 '16 12:02

Sap


2 Answers

There are actual several ways this can happen:

  • Load an embedded config file.
  • Load a config file in the same directory as the GRUB binary.
  • Load a config file from a path decided at grub-mkimage (called by grub-install) execution time.

The latter is probably the functionality you are really asking for - and it's a combination of the default config file name (grub.cfg), the prefix (default /boot/grub, but can be explicitly specified to grub-mkimage) and the grub partition name for the partition where the prefix is located.

If I run strings /boot/efi/EFI/debian/grubx64.efi | tail -1 on my current workstation, it prints out the stored value: (,gpt2)/boot/grub, telling grubx64.efi to look for its configuration file in /boot/grub on GPT partition 2. The bit before the comma (the GRUB disk device name) gets filled in at runtime based on which disk the grubx64.efi image itself was loaded from.

Dynamically loaded modules will also be searched for under this location, but in an architecture/platform-specific directory - in this case /boot/grub/x86_64-efi.

like image 173
unixsmurf Avatar answered Sep 28 '22 01:09

unixsmurf


for EFI image, I found that grub-install or grub-mkimage will always embed an early config into the result EFI binary, regardless of whether or not you have specified the --config FILE option. If you do not specify the --config FILE option, it will try to embed /boot/grub/x86-64_efi/load.cfg, This early config file looks like this:

    search.fs_uuid 8ef704aa-041d-443c-8ce6-71ac7e7f30da root hd0,gpt1
    set prefix=($root)'/boot/grub'
    configfile $prefix/grub.cfg  # this line seems can be omitted, because it seems to be the default next action
  • The uuid means uuid of file system, not of partition, you can use blkid to list it.
  • The hd0,gpt1 is just a hint.
  • You can change the first line into set root=hd0,gpt1

This default behavior of auto embedding is different as in BIOS mode, the latter by default only embed a prefix string like (,gpt3)/boot without bothering search.uuid.

I also found that Ubuntu bionic EFI image embedded a early config like this https://source.puri.sm/pureos/core/grub2/blob/master/debian/build-efi-images#L64

if [ -z "\$prefix" -o ! -e "\$prefix" ]; then
    if ! search --file --set=root /.disk/info; then
        search --file --set=root /.disk/mini-info
    fi
    set prefix=(\$root)/boot/grub
fi
if [ -e \$prefix/$platform/grub.cfg ]; then
    source \$prefix/$platform/grub.cfg
elif [ -e \$prefix/grub.cfg ]; then
    source \$prefix/grub.cfg
else
    source \$cmdpath/grub.cfg
fi

The cmdpath is the DIR of efi binary, so it will fallback to the grub.cfg in the same dir of the efi binary, as you found.

like image 36
osexp2003 Avatar answered Sep 28 '22 02:09

osexp2003