Morris Hafner (@mmha)
emBO++ 8th March 2018
0xEF00EFI/Boot/bootx64.efi<algorithm>, <array>,<tuple>,<type_traits>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
OVMF is an OSS implementation that can be used with QEMU
Signature is usually
EFI_STATUS(in..., inout *..., out *...)
In C++, we want this:
template <size_t in_count, size_t inout_count,
size_t out_count = SIZE_MAX, typename Callable>
auto wrap(Callable c);
auto locate_device_path =
wrap<1, 1, 1>(LocateDevicePath);
expected<tuple<EFI_HANDLE>, EFI_STATUS> result =
locate_device_path(protocol, device_path);→ Boost Callable Traits
template <size_t offset, typename T, size_t... I>
auto constexpr st_impl(T tuple, index_sequence<I...>) {
return tuple{get<offset + I>(tuple)...};
}
template <size_t N, typename T>
auto constexpr split_tuple(T tuple) {
auto constexpr sz = tuple_size_v<T>;
return tuple{
st_impl<0>(tuple, make_index_sequence<N>{}),
st_impl<N>(tuple, make_index_sequence<sz - N>{}),
};
}using in_split =
decltype(split_tuple<in_count>(declval<args>()));
using In = // <- first
tuple_element_t<0, in_split>;
using InOut_Out =
tuple_element_t<1, in_split>;
using inout_out_split =
decltype(split_tuple<inout_count>(declval<InOut_Out>()));
using InOut = // <- second
tuple_element_t<0, inout_out_split>;
using Out = // <- third
tuple_element_t<1, inout_out_split>;template <typename Func,
typename... Is, typename... IOs, typename... Os>
auto constexpr make_out_param_adapter(
Func func, tuple<Is...>, tuple<IOs...>, tuple<Os...>) {
return [func](Is... is, IOs... ios) {
tuple<remove_pointer_t<Os>...> res;
auto const ptr = transform_tuple(res, [](auto &val) {
return addressof(val);
});
apply(c, tuple_cat(
tuple{get_ptr(is)...}, tuple{get_ptr(ios)...}, ptr));
return res;
};
}aligned_storage!std::tuple causes value initialization
expected<T, E> contains either a value or an errortl::expected<T, E> (thanks, Simon!)if constexpr(
is_same_v<invoke_result_t<Func, Is..., IOs..., Os...>,
EFI_STATUS>) {
using result_t =
tl::expected<tuple<remove_pointer_t<Os>...>, EFI_STATUS>;
if(auto result = apply(...) != EFI_SUCCESS)
return result_t(tl::unexpect, result);
return result_t{transform_tuple(...)};
} else {
apply(...);
return res;
}We can modify the signature to accept references
Create a struct constructible with a reference and a pointer
std::overloadExitBootServices()First, some wrappers
uint64_t handle_count = 0;
auto maybe_gop =
locate_handle_buffer(ByProtocol, graphicsOutProtocolGUID,
nullptr, handle_count)
.and_then([&](auto proto_impl) {
return handle_protocol(get<0>(proto_impl)[0],
graphicsOutProtocolGUID);
})
.map([](auto opened_gop) {
return reinterpret_cast<EFI_GRAPHICS_OUTPUT_PROTOCOL *>(
get<0>(opened_gop));
});
if(!maybe_gop)
conOut->OutputString(conOut, (CHAR16*) u"Fail\r\n");FACT: This slide contains error handling
Structured bindings with polymorphic lambdas
To be discussed in Jacksonville
gop = maybe_gop.value();
auto query_mode = wrap<2, 0, 2>(gop->QueryMode);
for(int i = 0;; ++i) {
auto mode_info = query_mode(gop, i)
.map([](auto r)->decltype(auto){return *std::get<1>(r);});
if(!mode_info) break;
if(mode_info->HorizontalResolution == width &&
mode_info->VerticalResolution == height &&
mode_info->PixelFormat ==
PixelBlueGreenRedReserved8BitPerColor) {
gop->SetMode(gop, i);
break;
}
}With emulated double buffering
void swap_to_screen() const {
auto const pixel_ptr =
reinterpret_cast<uint32_t *>(gop->Mode->FrameBufferBase);
for(auto const& row : rows) {
std::copy(row.begin(), row.end(), pixel_ptr);
pixel_ptr += gop->Mode->Info->PixelsPerScanLine;
}
}
void clear() {
for(auto &row : rows) {
std::fill(row.begin(), row.end(), T{});
}
}Remember: Avoided heap
Implicit surfaces using signed distance fields offer a functional representation of a scene
Unfortunately no time for details