2022-05-07 23:02:00 +03:00
# Compile and run GNU-EFI applications under linux
## Requirements :
Download gnu-efi from here : [https://sourceforge.net/projects/gnu-efi/ ](https://sourceforge.net/projects/gnu-efi/ )
- **gcc-3.0 or newer**
```bash
2022-05-08 00:13:30 +03:00
gcc --version
2022-05-07 23:02:00 +03:00
```
- A version of **objcopy** that supports EFI applications :
```bash
2022-05-08 00:13:30 +03:00
objcopy --help
2022-05-07 23:02:00 +03:00
```
Check if supported target contains elf64-x86-64 elf32-i386 elf32-x86-64
- **mtools** : Utilities to access MS-DOS disks from Linux
## Compile and install GNU-EFI Library:
Go to gnu-efi source folder
```bash
2022-05-08 00:13:30 +03:00
make
make install
2022-05-07 23:02:00 +03:00
```
## Compile GNU-EFI application :
Use this makefile to easily compile and run gnu-efi application. It will recursively compile all c files in the sub-directories.
Change the following macros to locate the libraries and headers installed on the previous step.
2022-05-07 23:50:19 +03:00
```makefile
2022-05-08 00:13:30 +03:00
IMAGE = uefi_app
TARGET = main.efi
2022-05-07 23:49:33 +03:00
2022-05-08 00:13:30 +03:00
INCDIR = /usr/local/include
LIBDIR = /usr/local/lib
EFILIB = /usr/local/lib
2022-05-07 23:50:19 +03:00
```
2022-05-07 23:02:00 +03:00
## Run on qemu :
### Locate OVMF in Makefile:
To run the compiled application in qemu we need OVMF for UEFI emulation. OVMF is a port of Intel's tianocore firmware to the qemu virtual machine. Download it from here.
2022-05-07 23:50:19 +03:00
```makefile
2022-05-08 00:13:30 +03:00
OVMF_DIR = ../OVMF
2022-05-07 23:50:19 +03:00
```
2022-05-07 23:02:00 +03:00
Change the OVMF directory in the makefile.
### Create Image :
2022-05-07 23:50:19 +03:00
```bash
2022-05-08 00:13:30 +03:00
make img
2022-05-07 23:50:19 +03:00
```
2022-05-07 23:02:00 +03:00
It will do the following tasks :
```bash
2022-05-08 00:13:30 +03:00
# Create a new image file that will contain the GNU-EFI application.
dd if=/dev/zero of=$(IMAGE).img bs=512 count=93750
# mformat to format it with FAT16.
mformat -i $(IMAGE).img -h 32 -t 32 -n 64 -c 1 ::
# Create directory
mmd -i $(IMAGE).img ::/EFI
mmd -i $(IMAGE).img ::/EFI/BOOT
# Copy image
mcopy -i $(IMAGE).img $(TARGET) ::/EFI/BOOT
# Copy the nsh script to load efi application automatically from fs0
mcopy -i $(IMAGE).img startup.nsh ::
2022-05-07 23:02:00 +03:00
```
### Run :
```bash
2022-05-07 23:47:57 +03:00
make run
2022-05-07 23:02:00 +03:00
```
## Running on real hardware :
- Format a pendrive with FAT32 file system.
- Create /efi/boot/ folders in the pendrive
- Rename main.efi to bootx64.efi for 64 bit and bootia32.efi for 32 bit application.
- For 64 bit application the pendrive should look like this : ** /efi/boot/bootx64.efi**
- For 32 bit application the pendrive should look like this : ** /efi/boot/bootia32.efi**
# Further Reading :
- https://wiki.osdev.org/UEFI
- https://wiki.osdev.org/GNU-EFI
- https://wiki.osdev.org/UEFI_App_Bare_Bones