Build and run Hello World on OVMF Qemu

This post records how I build and run an EDK2 HelloWorld on Qemu, based on OVMF UEFI shell.

Prepare environment

Clone edk2:

1
git clone --recursive https://github.com/tianocore/edk2.git

Enter edk2 build environment:

1
. edksetup.sh BaseTools

Build base tools:

1
make -C <edk2-dir>/BaseTools/Source/C

Create and build HelloWorld

Build HelloWorld package for x64 architecture and using GCC:

1
build -a X64 -p HelloWorldPkg/HelloWorldPkg.dsc -t GCC5

The built HelloWorld.efi is under Build/HelloWorldPkg/DEBUG_GCC5/X64, we can run it under UEFI shell on an x64 PC or an emulator.

Build edk2 for OVMF

Modify the following contents in Conf/target.txt:

1
2
3
ACTIVE_PLATFORM       = OvmfPkg/OvmfPkgX64.dsc
TARGET_ARCH = X64
TOOL_CHAIN_TAG = GCC5

Then, build it:

1
build

Boot and run Hello World

Make an image to store efi files:

1
2
3
4
dd if=/dev/null of=example.img bs=1M seek=512
mount -t ext4 -o loop example.img /mnt/example
cp Build/HelloWorldPkg/DEBUG_GCC5/X64/HelloWorld.efi /mnt/example
umount /mnt/example

Run OVMF as the firmware and mount the images:

1
qemu-system-x86_64 -L . --bios ./ovmf.fd -hda ./example.img

In the UEFI shell, list disk mappings:

1
2
3
4
5
6
map -r
Mapping table
FS0: ...
...
BLK1: ...
...

The files are under fs0, list them:

1
2
ls fs0:\
... HelloWorld.efi

Run our Hello Wolrd:

1
2
fs0:\HelloWorld.efi
Hello World!

Conclusion

In this post, I noted all the steps to build an edk2 HelloWorld. I will write some interesting UEFI applications sooner.

Readings