CLOSE

Printing a welcome message on the screen using UEFI (Unified Extensible Firmware Interface) involves writing a UEFI application. UEFI provides a set of runtime and boot-time services accessible through a standardized interface. We can use these services to interact with the system's hardware, including the console output.

Prerequisites

Development Environment: You need a development environment set up with a UEFI toolchain. The GNU-EFI or EDK2 (EFI Development Kit) toolchain can be used.

We will be using GNU-EFI

sudo apt-get install build-essential uuid-dev git nasm

// Install the GNU-EFI library:
sudo apt-get install gnu-efi

Compiler: A C compiler like gcc or clang.

UEFI Shell: An environment where you can test the UEFI application, such as QEMU (emulator) with OVMF firmware or a real UEFI-capable machine.

Write the UEFI Application:

boot.c:

#include <efi.h>
#include <efilib.h>

EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
    InitializeLib(ImageHandle, SystemTable);

    Print(L"Welcome to UEFI!\n");

    // Wait for a key press before exiting
    EFI_INPUT_KEY Key;
    SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);
    SystemTable->ConIn->ReadKeyStroke(SystemTable->ConIn, &Key);

    return EFI_SUCCESS;
}

2 Compile the UEFI Application:

# Compile the C source file to an object file
gcc -I /usr/include/efi -I /usr/include/efi/x86_64 -O2 -fpic -fshort-wchar -ffreestanding -c hello.c -o hello.o

# Link the object file to create the EFI executable
ld -nostdlib -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic -L /usr/lib /usr/lib/crt0-efi-x86_64.o hello.o -o hello.efi -lefi -lgnuefi

3 Test the UEFI Application:

3.1 Create a Disk Image:

qemu-img create -f raw disk.img 200M
mkfs.vfat disk.img

3.2 Mount the Disk Image and Copy the Application:

sudo mount -o loop disk.img /mnt
mkdir -p /mnt/EFI/BOOT
cp hello.efi /mnt/EFI/BOOT/BOOTX64.EFI
sudo umount /mnt

3.3 Run the Application in QEMU:

qemu-system-x86_64 -drive file=disk.img,format=raw -bios /usr/share/ovmf/OVMF.fd