UEFI Applications with C++

Morris Hafner (@mmha)

emBO++ 8th March 2018

What is (U)EFI?

  • “New” firmware interface for x86, ARM, RISC-V
  • Many features compared to BIOS
    • Network, Secure Boot, NVRAM, EFI Bytecode, …
  • No assembly required for many applications (e. g. bootloaders)

EFI executables

  • COFF file format, subsystems 10-13
  • MS calling convention
  • UTF-16 strings
  • On a FAT32 partition with partition type 0xEF00
  • Implementations usually default to executing EFI/Boot/bootx64.efi

First Executable

Toolchain

  • TianoCore EDK II, gnu efilib
  • We will use gnu efilib, it is easier to use
  • Available compilers: MSVC, MinGW gcc
  • No clang
    • Doesn’t support freestanding COFF executables

Standard Library

  • no C or C++ standard library
    • A C95 stdlib implementation is part of EDK II
  • Heap not easily accessible
  • In practice, many headers can still be used
    • most of <algorithm>, <array>,<tuple>,<type_traits>
  • See Ben Craig’s talk and proposal (p0829r1) to fix this

Compiler Invocation

x86_64-w64-mingw32-g++ \
  -mno-red-zone -DEFI_FUNCTION_WRAPPER \
  -ffreestanding -fshort-wchar \ 
  -nostdlib -e efi_main \
  -Wl,-dll -shared -Wl,--subsystem,10 \
  -c main.cpp

This can be wrapped in a CMake Toolchain

The code

Running it

OVMF is an OSS implementation that can be used with QEMU

Accessing EFI Services

The EFI Protocol Interface

  • Firmware services can be queried using GUIDs
  • Everything is loaded into structs
  • OO style interface, but in C with function pointers

Hmmm…

Signature is usually

EFI_STATUS(in..., inout *..., out *...)

In C++, we want this:

What to do?

  1. Partition the argument list
  2. Create a new function wrapping the EFI function
  3. Wrap the error codes

Getting the Argument List

→ Boost Callable Traits

Partition the Argument List

3-way split

Wrap the Function

Overhead?

aligned_storage!

std::tuple causes value initialization

Wrap the error codes

  • expected<T, E> contains either a value or an error
  • We use tl::expected<T, E> (thanks, Simon!)

Wrap the pointers

We can modify the signature to accept references

Create a struct constructible with a reference and a pointer

Overhead?

We made simplifications

  • Assumptions that there are only builtins and PODs
    • All argument types need trivial destructors
  • Cannot be overloaded
  • Not too bad, C doesn’t have overloads or destructors
  • But may be wrapped by a future std::overload
  • Future high level wrappers may implement overloads

Example application

Rendering to the Framebuffer

  • We could write our own kernel in C++ now…
    • ExitBootServices()
  • Let’s render a couple of spheres onto the framebuffer instead

Creating a Graphics Out Protocol Instance

First, some wrappers

Yay, monads!

FACT: This slide contains error handling

A. Sagar: p0931r0

Structured bindings with polymorphic lambdas

To be discussed in Jacksonville

Create the Framebuffer

Use the Framebuffer

With emulated double buffering

Implicit Surfaces

Remember: Avoided heap

Implicit surfaces using signed distance fields offer a functional representation of a scene

Unfortunately no time for details

References

Live Demo