UEFI App Bare Bones: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
Undo revision 27125 by Leapofazzam (talk)
m Bot: Replace deprecated source tag with syntaxhighlight
 
(3 intermediate revisions by 3 users not shown)
Line 7:
 
This tutorial uses the header files and GUID definitions from the [[GNU-EFI]] project, but does '''not''' use the gnu-efi build system, but rather the MinGW-w64 or LLVM/Clang toolchain.
 
If you don't want to learn all that UEFI terminology, take a look at [[POSIX-UEFI]], which is an extremely lightweight (about 32k) static library and build environment that hides everything behind nice and friendly libc API.
 
== Prerequisites ==
Line 16 ⟶ 14:
To build the EFI filesystem image, developers can use [[MTools]] or [https://github.com/jncronin/mkgpt '''mkgpt'''] to create a hard disk image. To build a CD image, '''xorriso''' (in [[mkisofs]] emulation mode) will be needed. To run under an emulator, it is best to use '''qemu-system-x86_64''' coupled with the [http://tianocore.sourceforge.net/wiki/OVMF '''x64 OVMF firmware'''].
 
Under an apt-based system (e.g. Debian/Ubuntu), developers can run: <sourcesyntaxhighlight lang="bash">sudo apt-get install qemu ovmf gnu-efi binutils-mingw-w64 gcc-mingw-w64 xorriso mtools</sourcesyntaxhighlight>
 
To install mkgpt you can run these commands:<sourcesyntaxhighlight lang="bash">git clone https://github.com/jncronin/mkgpt.git
cd mkgpt
automake --add-missing
Line 24 ⟶ 22:
./configure
make
sudo make install</sourcesyntaxhighlight>
 
== Testing the emulator ==
Line 30 ⟶ 28:
Now is a good time to check the emulator is working successfully with the OVMF firmware.
 
<sourcesyntaxhighlight lang="bash">qemu-system-x86_64 -L OVMF_dir/ -pflash OVMF.fd</sourcesyntaxhighlight>should launch qemu and launch a UEFI shell prompt.
 
== Preparing the files ==
Line 37 ⟶ 35:
 
Next, create a file with the following:
<sourcesyntaxhighlight lang="c">#include <efi.h>
#include <efilib.h>
 
Line 68 ⟶ 66:
 
return Status;
}</sourcesyntaxhighlight>
 
=== gnu-efi/lib/data.c ===
Line 81 ⟶ 79:
 
To build, use the cross-compiler:
<sourcesyntaxhighlight lang="bash">
# compile: (flags before -o become CFLAGS in the Makefile)
x86_64-w64-mingw32-gcc -ffreestanding -Ipath/to/gnu-efi/inc -Ipath/to/gnu-efi/inc/x86_64 -Ipath/to/gnu-efi/inc/protocol -c -o hello.o hello.c
Line 87 ⟶ 85:
# link: (flags before -o become LDFLAGS in the Makefile)
x86_64-w64-mingw32-gcc -nostdlib -Wl,-dll -shared -Wl,--subsystem,10 -e efi_main -o BOOTX64.EFI hello.o data.o
</syntaxhighlight>
</source>
Note here that '--subsystem 10' specifies an EFI application for ld.
 
Line 93 ⟶ 91:
The build sequence under LLVM/clang is essentially the same, although there is the advantage of having ''all'' targets installed by default:
 
<sourcesyntaxhighlight lang="bash">
CFLAGS='-target x86_64-unknown-windows
-ffreestanding
Line 107 ⟶ 105:
clang $CFLAGS -c -o data.o path/to/gnu-efi/lib/data.c
clang $LDFLAGS -o BOOTX64.EFI hello.o data.o
</syntaxhighlight>
</source>
 
Passing '--target x86_64-unknown-windows' to clang tells it to compile for x86_64 "Windows". This is quite not the same as 64-bit UEFI PE yet, but as before the "freestanding" part makes it a good kernel image. An example of this toolchain is found in the [https://github.com/c-util/c-efi c-efi] project.
Line 117 ⟶ 115:
 
Next, create a FAT filesystem image.
<sourcesyntaxhighlight lang="bash">
dd if=/dev/zero of=fat.img bs=1k count=1440
mformat -i fat.img -f 1440 ::
Line 123 ⟶ 121:
mmd -i fat.img ::/EFI/BOOT
mcopy -i fat.img BOOTX64.EFI ::/EFI/BOOT
</syntaxhighlight>
</source>
 
=== Running as a USB stick image ===
Line 129 ⟶ 127:
The FAT image can either be written directly to a USB stick and used in in a UEFI machine, or it can be run directly in QEMU:
 
<sourcesyntaxhighlight lang="bash">qemu-system-x86_64 -L OVMF_dir/ -pflash OVMF.fd -usb -usbdevice disk::fat.img</sourcesyntaxhighlight>
 
=== Creating and running the HD image ===
Line 135 ⟶ 133:
The HD image is a disk image in the [[GPT]] format, with the FAT image specially identified as a 'EFI System Partition'.
 
<sourcesyntaxhighlight lang="bash">mkgpt -o hdimage.bin --image-size 4096 --part fat.img --type system
qemu-system-x86_64 -L OVMF_dir/ -pflash OVMF.fd -hda hdimage.bin</sourcesyntaxhighlight>
 
=== Creating and running the CD image ===
The ISO image is a standard ISO9660 image which contains the FAT image as a file. A special El Torito option (-e) then points EFI aware systems to this image to be loaded. The CD image can either be burned to a CD and ran in a UEFI machine, or run directly in QEMU:
<sourcesyntaxhighlight lang="bash">mkdir iso
cp fat.img iso
xorriso -as mkisofs -R -f -e fat.img -no-emul-boot -o cdimage.iso iso
qemu-system-x86_64 -L OVMF_dir/ -pflash OVMF.fd -cdrom cdimage.iso</sourcesyntaxhighlight>
 
== What to do next? ==
Line 160 ⟶ 158:
* [[UEFI ISO Bare Bones]]
* [[GNU-EFI]]
* [[POSIX-UEFI]]
 
[[Category:Bare bones tutorials]]