FreeBSD with uefi on qemu amd64

All the information on using uefi with qemu seems to aged out as projects have been abandoned.

On FreeBSD (around 15-beta2) we need to get the uefi edk2 firmware, which is packaged:

# pkg install qemu edk2-qemu-x64

This will provide three images, one of which we need to give to qemu with the bios option.

$ pkg info -l    edk2-qemu-x64
edk2-qemu-x64-g202308_5:
        /usr/local/share/edk2-qemu/QEMU_UEFI-x86_64.fd
        /usr/local/share/edk2-qemu/QEMU_UEFI_CODE-x86_64.fd
        /usr/local/share/edk2-qemu/QEMU_UEFI_VARS-x86_64.fd
        /usr/local/share/licenses/edk2-qemu-x64-g202308_5/BSD3CLAUSE
        /usr/local/share/licenses/edk2-qemu-x64-g202308_5/LICENSE
        /usr/local/share/licenses/edk2-qemu-x64-g202308_5/catalog.mk

I picked QEMU-UEFI-x86_64 which worked.

qemu-system-x86_64 will take a bios to use with the --bios flag (or if you are bored and want to write 16 bit assembly you can give it your own bios )

I spent quite a while debugging a minimal boot flow, I think qemu is stashing nvram somewhere, but I can't for the life of me figure out if it really is or where it might be. In the end I used the efi shell to reconfigure the boot order and I started getting complaints about there not being enough memory with the default qemu flags. I added -m 2048M to get around this.

qemu-system-x86_64 -display none -serial mon:stdio \
    -m 2048M --bios /usr/local/share/edk2-qemu/QEMU_UEFI-x86_64.fd \
    -drive file=fbsd.raw,format=raw,if=virtio