How can I define a boot sequence in UEFI mode and perform pre-boot operations?

The EFI-Extensible Firmware Interface system partition is a partition on a data storage device (usually a hard disk driver or a solid state drive) that is used by computers adhering to the UEFI-Unified Extensible Firmware Interface. The purpose of this guide is to explain:

  • how to load a UEFI Shell on a target hard drive;

  • how to use this shell to perform automatic operations during the system boot (as UEFI driver loading) before the OS.

INDEX


Preliminary operations

On a system with an installed operating system, the boot manager that exists as part of the motherboard UEFI firmware will have an EFI file location stored in the BootOrder variable. This might actually be another boot manager if you have an installed multi-boot tool but is usually just the EFI boot loader for your operating system.
Most of the time, this EFI file is stored on a special EFI system partition. Usually it is this one:

EFI\Boot\Bootx64.efi

The easiset way to perform modifications in the boot sequence is to use a bootable UEFI USB device.
If you need to create a device from zero, please follow the guide https://secogroup.atlassian.net/l/c/Kvry6Eos


What about Linux?

The first step is creating a script that will be called during the EFI Shell boot from the hard disk.

Create a file named startup.nsh and modify it to add custom commands that will be executed automatically during the boot. These command may be default EFI Shell commands or callback to custom EFI application.
The only mandatory commands are: the first line that will be used to open hard drive file system (by default should be fs0) and the last line that must be the called to grub bootloader:

fs0: customoperations EFI\ubuntu\grubx64_org.efi

For instance, it is possible to load the EFI driver before booting the OS.
As first step we must copy the EFI driver to our target hard disk:

  • cp fs1:\drivername.efi fs0:\drivername.efi

    In this case the custom operations in our startup.nsh script will be load drivername.efi.

Once this file is ready save it and copy to the root directory of the USB EFI device that will be used for the modification. Turn on the system and boot from EFI USB device. The EFI shell will be launched and Device mapping table will be displayed (if not, use the command map to show it).

In the picture, for example, we have two filesystems: one referring to the SATA hard disk where we want to apply the modification (fs0), and another one referring to the USB drive with the EFI Shell that we have booted (fs1).

Now open the target hard disk filesystem and move to the folder where EFI bootloader is placed:

fs0: cd EFI\Boot

Rename bootx64.efi as bootx64_org.efi:

mv bootx64.efi bootx64_org.efi

Copy both the EFI Shell from the USB drive and the startup script to the hard drive boot folder:

cp fs1:\EFI\Boot\bootx64.efi fs0:\EFI\Boot\bootx64.efi cp fs1:\startup.nsh fs0:\EFI\Boot\startup.nsh

Move to the EFI\ubuntu folder and rename grubx64.efi as grubx64_org.efi:

mv grubx64.efi grubx64_org.efi

Now poweroff the system. On the next boot the EFI Shell will be launched before loading Ubuntu OS and the requested operations will be performed.


What about Windows?

The first step is creating a script that will be called during the EFI Shell boot from the hard disk. Create a file named startup.nsh and modify it to add custom commands that will be executed automatically during the boot. These command may be default EFI Shell commands or callback to custom EFI application.
The only mandatory commands are: the first line that will be used to open hard drive file system (by default should be fs0), and the last line that must be the called to Windows bootloader.

fs0: customoperations EFI\ubuntu\bootmgfw_org.efi

For instance, it is possible to load the EFI driver before booting to OS.
As first step we must copy the EFI driver to our target hard disk:

  • cp fs1:\drivername.efi fs0:\drivername.efi

In this case the custom operations in the startup.nsh script will be load drivername.efi.

Once this file is ready save it and copy to the root directory of the USB EFI device that will be used for the modification. Turn on the system and boot from EFI USB device. The EFI shell will be launched and Device mapping table will be displayed (if not, use the command map to show it).

In the picture, for example, we have two filesystems: one referring to the SATA hard disk where we want to apply the modification (fs0), and another one referring to the USB drive with the EFI Shell that we have booted (fs1).

Open the target hard disk filesystem and move to the folder where EFI bootloader is placed:

fs0: cd EFI\Boot

Copy both the EFI Shell from the USB drive and the startup script to the hard drive boot folder:

cp fs1:\EFI\Boot\bootx64.efi fs0:\EFI\Boot\bootx64.efi cp fs1:\startup.nsh fs0:\EFI\Boot\startup.nsh

Move to the Microsoft\Boot folder and rename bootmgr.efi as bootmgr_org.efi and bootmgfw.efi as bootmgfw_org.efi:

mv grubx64.efi grubx64_org.efi

Now power off the system. On the next boot the EFI Shell will be launched before loading Ubuntu OS and the requested operations will be performed.