EFI stub kernel
The (U)EFI firmware present in many computers can function as bootloader, allowing systems to boot without needing an additional software bootloader. This article shows how to configure and install an unsigned kernel in the EFI System Partition (ESP) of a computer running in EFI mode with secureboot turned off.
In order to boot directly from UEFI, the kernel needs to know where to find the root (/) partition of the system to be booted. Enable EFI runtime service support (CONFIG_EFI), EFI stub support (CONFIG_EFI_STUB) and Built-in kernel command line (CONFIG_CMDLINE_BOOL) and add the root partition path (example: /dev/sda2) or its PARTUUID to (CONFIG_CMDLINE).
Processor type and features ---> [*] EFI runtime service support [*] EFI stub support [ ] EFI mixed-mode support ... ... [*] Built-in kernel command line (root=/dev/sda2)
root=PARTUUID= might be preferable. To find out use blkid:
The partition's PARTUUID is distinct from the filesystem's UUID.
blkid | grep sda2
/dev/sda2: UUID="d1e0c1e0-3a40-42c5-8931-cfa2c7deae32" TYPE="ext4" PARTUUID="adf55784-15d9-4ca3-bb3f-56de0b35d88d"
Processor type and features ---> [*] Built-in kernel command line (root=PARTUUID=adf55784-15d9-4ca3-bb3f-56de0b35d88d)
It is also a good idea to enable EFI Variable Support via sysfs (CONFIG_EFI_VARS) so that the efivars can be mounted. CONFIG_EFI_MIXED as proposed by Gentoo Handbook is not needed for the EFI boot stub.
Firmware Drivers ---> EFI (Extensible Firmware Interface) Support ---> <*> EFI Variable Support via sysfs
If an ESP does not exist, one needs to be created. See EFI System Partition. Still in the kernel directory, build the kernel and install the modules:
make && make modules_install
Have the ESP (in this example /dev/sda1) mounted at /boot:
mount /dev/sda1 /boot
Copy or move the kernel image to the right place adding version number and the .efi suffix:
mkdir -p /boot/EFI/Gentoo
cp arch/x86/boot/bzImage /boot/EFI/Gentoo/bzImage-4.9.16.efi
In the ESP it should then be listed like:
tree -L 3 /boot
/boot └── EFI └── Gentoo └── bzImage-4.9.16.efi
Alternatively the fallback directory /boot/EFI/Boot could be used additionally to or instead of /boot/EFI/Gentoo.
If for some reason an initramfs is needed, it can either be embedded into the kernel or be used as a separate file.
In case it's desired as a separate file, it should also be copied or moved into the ESP and the NVRAM be updated accordingly.
mv /boot/initramfs-4.9.16-gentoo.img /boot/EFI/Gentoo/initramfs.img
mount /sys/firmware/efi/efivars -o rw,remount
efibootmgr --create --part 1 --label "Gentoo" --loader '\efi\gentoo\bzImage-4.9.16.efi' -u 'initrd=\efi\gentoo\initramfs.img'
mount /sys/firmware/efi/efivars -o ro,remount
Some UEFI implementations however seem to not support passing parameters from the NVRAM to the EFI stub kernel.
For embedding the initramfs directly into the kernel image, the Initramfs source file(s) (CONFIG_INITRAMFS_SOURCE) must be coded in the kernel (directly under the Initial RAM filesystem and RAM disk (initramfs/initrd) support (CONFIG_BLK_DEV_INITRD) option) as shown here for a custom initramfs created in /usr/src/initramfs:
General setup ---> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support (/usr/src/initramfs) Initramfs source file(s)
Embedding takes place on compilation, so the kernel must then be recompiled and be moved (or copied) again into the ESP.
This guide was created with the x64 architecture in mind. According the specification, UEFI is rich enough to cover a range of contemporary processor designs.
UEFI image's name is composed by the machine type defined in the PE32+ format  . For example, a UEFI OS loader for the x86 architecture is bootIA32.efi
- efibootmgr — a tool for managing UEFI boot entries
- efibootmgr in Handbook:AMD64/Installation/Bootloader#Alternative 2: efibootmgr
- Architecture specific kernel configuration (AMD64 Handbook) - should widely equal the #Kernel configuration section above
- rEFInd — a boot manager for EFI and UEFI platforms and not needed for EFI stub kernels
- EFI Stub - booting without a bootloader Blog posting which this article is partially based on.
- EFI bootloaders listing alternative ways to boot an (U)EFI system.
- EFISTUB on wiki.archlinux.org
- Gentoo Forums: Suspend and Hibernate with UEFI
- UEFI Specification Version 2.6, Section 2.1.1 UEFI Images, p.18, January 2016. UEFI Forum. Retrieved on October 10, 2016.
- UEFI Specification Version 2.6, Section 18.104.22.168 Removable Media Boot Behavior, p.90, January 2016. UEFI Forum. Retrieved on October 10, 2016.