Skip to content

Proposal: Rework UEFI API #9382

@Exonorid

Description

@Exonorid

At the moment, std.os.uefi is a very barebones wrapper around UEFI. I'm proposing to rework it in the following ways:

  • Make functions that can fail return error unions
  • Pass slices to functions instead of separate lengths and pointers
  • Make reading and writing functions return the number of bytes read or written respectively
  • Use packed struct bitfields instead of ORed integers

As an example, here is the current implementation of std.os.uefi.protocols.FileProtocol.read:

pub fn read(self: *const FileProtocol, buffer_size: *usize, buffer: [*]u8) Status {
return self._read(self, buffer_size, buffer);
}

There are two main issues with this as I see it:

  1. The function returns an enum instead of an error union. This allows the error to be fairly easily discarded using _ = . On top of that, without looking at the UEFI specification itself, it's very unclear what errors the functions could return.
  2. When passing the output buffer to the function, it takes a pointer to the buffer length as a parameter. The actual UEFI function then reads the file into the buffer and writes the number of bytes read to said pointer. On top of passing the pointer and length separately being completely unnecessary while slices exist, having the length of the buffer passed and then changed can lead to all kinds of confusion.

Under this proposal, that function would look like this:

pub fn read(self: *const FileProtocol, buffer: []u8) !usize { 
    var buffer_size = buffer.len;
    return switch(self._read(self, buffer_size, buffer.ptr)) {
        .Success => buffer_size,
        .NoMedia => UEFIError.NoMedia,
        .DeviceError => UEFIError.DeviceError,
        .VolumeCorrupted => UEFIError.VolumeCorrupted,
        .BufferTooSmall => buffer_size, //Didn't read to the end of the file, which Reader.read doesn't consider an error
        else => unreachable,
    };
} 

This implementation, while a bit more complex, is much nicer to work with as an end user, as well as being more idiomatic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions