Skip to content

Instantly share code, notes, and snippets.

@ohnx
Last active January 4, 2018 21:31
Show Gist options
  • Save ohnx/f319745e1dcb255f9cb1f0ad6d8f21a6 to your computer and use it in GitHub Desktop.
Save ohnx/f319745e1dcb255f9cb1f0ad6d8f21a6 to your computer and use it in GitHub Desktop.
Giving userspace access to EFI NVRAM variables

Haiku has an EFI bootloader now (I'm a few months late to the party, sorry). Yay!

Now, on to the next step... allowing user applications to access EFI-related things, e.g. EFI NVRAM.

From my understanding, there's three things that need to happen before userspace applications can take advantage of EFI variables:

  1. Pass RuntimeServices to the kernel

This shouldn't be too bad to implement. Add an additional variable to struct kernel_args which will be NULL on non-EFI systems, and will be a pointer to the RuntimeServices on EFI systems.

  1. Make the kernel understand RuntimeServices

The kernel needs to understand the format of the RuntimeServices call table in order to be able to access the functions that read and write EFI variables. I haven't poked around the kernel much, so I'm not sure what would need to be done here.

  1. Give userspace an interface to read/write/list EFI variables

Looking at other systems in the wild, Linux has sysfs and uses a virtual file system to manage NVRAM variables. Tools such as efivar rely on a filesystem being present at /sys/firmware/efi/vars that allows the tool to read, write, and list EFI variables. I think there are also sysctl calls that allow the modification of EFI variables. Windows has a registry, and (from what it looks like), EFI variables are stored in the registry. MacOS has a command-line tool that seems to interface some kernel module... I would assume using sysctl, but I haven't checked in depth. I'm not sure about the BSD's; I couldn't find much information about EFI support on them. What little I did find relates to using sysctl.

Haiku doesn't have a registry, a sysfs, nor sysctl, but it does have a rather verbose devices folder. My proposition right now is to create a folder in /dev such as /dev/nvram to provide an interface for applications running on Haiku to access EFI variables.

I haven't started much work on this beyond initial planning (seen above), but that's my take on this whole thing. Thoughts, criticism, and other comments are very much appreciated! :)

@tqh
Copy link

tqh commented Jan 4, 2018

Looks good, calling conventions for RuntimeServices will be MS Windows format though, not Linux and Haiku.
Another thing to make sure is to make nvram access secure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment