Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.os.uefi.protocol: ziggify function signatures #23214

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/std/os/uefi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,11 @@ test {
_ = tables;
_ = protocol;
}

pub const UnexpectedError = error{Unexpected};

pub fn unexpectedStatus(err: Status) UnexpectedError {
// TODO: debug printing the encountered error? maybe handle warnings?
_ = err;
return error.Unexpected;
}
24 changes: 19 additions & 5 deletions lib/std/os/uefi/protocol/absolute_pointer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@ const Event = uefi.Event;
const Guid = uefi.Guid;
const Status = uefi.Status;
const cc = uefi.cc;
const Error = Status.Error;

/// Protocol for touchscreens.
pub const AbsolutePointer = extern struct {
_reset: *const fn (*const AbsolutePointer, bool) callconv(cc) Status,
_reset: *const fn (*AbsolutePointer, bool) callconv(cc) Status,
_get_state: *const fn (*const AbsolutePointer, *State) callconv(cc) Status,
wait_for_input: Event,
mode: *Mode,

pub const ResetError = uefi.UnexpectedError || error{DeviceError};
pub const GetStateError = uefi.UnexpectedError || error{ NotReady, DeviceError };

/// Resets the pointer device hardware.
pub fn reset(self: *const AbsolutePointer, verify: bool) Status {
return self._reset(self, verify);
pub fn reset(self: *AbsolutePointer, verify: bool) ResetError!void {
switch (self._reset(self, verify)) {
.success => {},
.device_error => return Error.DeviceError,
else => |err| uefi.unexpectedError(err),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else => |err| uefi.unexpectedError(err),
else => |status| return uefi.unexpectedStatus(status),

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same for a bunch more below)

}
}

/// Retrieves the current state of a pointer device.
pub fn getState(self: *const AbsolutePointer, state: *State) Status {
return self._get_state(self, state);
pub fn getState(self: *const AbsolutePointer) GetStateError!State {
var state: State = undefined;
switch (self._get_state(self, &state)) {
.success => return state,
.not_ready => return Error.NotReady,
.device_error => return Error.DeviceError,
else => |err| uefi.unexpectedStatus(err),
}
}

pub const guid align(8) = Guid{
Expand Down
62 changes: 54 additions & 8 deletions lib/std/os/uefi/protocol/block_io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const uefi = std.os.uefi;
const Status = uefi.Status;
const cc = uefi.cc;
const Error = Status.Error;

pub const BlockIo = extern struct {
const Self = @This();
Expand All @@ -14,24 +15,69 @@ pub const BlockIo = extern struct {
_write_blocks: *const fn (*BlockIo, media_id: u32, lba: u64, buffer_size: usize, buf: [*]u8) callconv(cc) Status,
_flush_blocks: *const fn (*BlockIo) callconv(cc) Status,

pub const ResetError = uefi.UnexpectedError || error{DeviceError};
pub const ReadBlocksError = uefi.UnexpectedError || error{
DeviceError,
NoMedia,
BadBufferSize,
InvalidParameter,
};
pub const WriteBlocksError = uefi.UnexpectedError || error{
WriteProtected,
NoMedia,
MediaChanged,
DeviceError,
BadBufferSize,
InvalidParameter,
};
pub const FlushBlocksError = uefi.UnexpectedError || error{
DeviceError,
NoMedia,
};

/// Resets the block device hardware.
pub fn reset(self: *Self, extended_verification: bool) Status {
return self._reset(self, extended_verification);
pub fn reset(self: *Self, extended_verification: bool) ResetError!void {
switch (self._reset(self, extended_verification)) {
.success => {},
.device_error => return Error.DeviceError,
else => |err| uefi.unexpectedStatus(err),
}
}

/// Reads the number of requested blocks from the device.
pub fn readBlocks(self: *Self, media_id: u32, lba: u64, buffer_size: usize, buf: [*]u8) Status {
return self._read_blocks(self, media_id, lba, buffer_size, buf);
pub fn readBlocks(self: *Self, media_id: u32, lba: u64, buf: []u8) ReadBlocksError!void {
switch (self._read_blocks(self, media_id, lba, buf.len, buf.ptr)) {
.success => {},
.device_error => Error.DeviceError,
.no_media => Error.NoMedia,
.bad_buffer_size => Error.BadBufferSize,
.invalid_parameter => Error.InvalidParameter,
else => |err| uefi.unexpectedStatus(err),
}
}

/// Writes a specified number of blocks to the device.
pub fn writeBlocks(self: *Self, media_id: u32, lba: u64, buffer_size: usize, buf: [*]u8) Status {
return self._write_blocks(self, media_id, lba, buffer_size, buf);
pub fn writeBlocks(self: *Self, media_id: u32, lba: u64, buf: []const u8) WriteBlocksError!void {
switch (self._write_blocks(self, media_id, lba, buf.len, buf.ptr)) {
.success => {},
.write_protected => Error.WriteProtected,
.no_media => Error.NoMedia,
.media_changed => Error.MediaChanged,
.device_error => Error.DeviceError,
.bad_buffer_size => Error.BadBufferSize,
.invalid_parameter => Error.InvalidParameter,
else => |err| uefi.unexpectedStatus(err),
}
}

/// Flushes all modified data to a physical block device.
pub fn flushBlocks(self: *Self) Status {
return self._flush_blocks(self);
pub fn flushBlocks(self: *Self) FlushBlocksError!void {
switch (self._flush_blocks(self)) {
.success => {},
.device_error => Error.DeviceError,
.no_media => Error.NoMedia,
else => |err| uefi.unexpectedStatus(err),
}
}

pub const guid align(8) = uefi.Guid{
Expand Down
8 changes: 5 additions & 3 deletions lib/std/os/uefi/protocol/device_path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub const DevicePath = extern struct {
subtype: u8,
length: u16 align(1),

pub const createFileDevicePathError = Allocator.Error;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const createFileDevicePathError = Allocator.Error;
pub const CreateFileDevicePathError = Allocator.Error;


pub const guid align(8) = Guid{
.time_low = 0x09576e91,
.time_mid = 0x6d3f,
Expand All @@ -23,15 +25,15 @@ pub const DevicePath = extern struct {
};

/// Returns the next DevicePath node in the sequence, if any.
pub fn next(self: *DevicePath) ?*DevicePath {
pub fn next(self: *const DevicePath) ?*DevicePath {
if (self.type == .end and @as(uefi.DevicePath.End.Subtype, @enumFromInt(self.subtype)) == .end_entire)
return null;

return @as(*DevicePath, @ptrCast(@as([*]u8, @ptrCast(self)) + self.length));
}

/// Calculates the total length of the device path structure in bytes, including the end of device path node.
pub fn size(self: *DevicePath) usize {
pub fn size(self: *const DevicePath) usize {
var node = self;

while (node.next()) |next_node| {
Expand All @@ -42,7 +44,7 @@ pub const DevicePath = extern struct {
}

/// Creates a file device path from the existing device path and a file path.
pub fn create_file_device_path(self: *DevicePath, allocator: Allocator, path: [:0]align(1) const u16) !*DevicePath {
pub fn createFileDevicePath(self: *const DevicePath, allocator: Allocator, path: []const u16) !*DevicePath {
const path_size = self.size();

// 2 * (path.len + 1) for the path and its null terminator, which are u16s
Expand Down
18 changes: 14 additions & 4 deletions lib/std/os/uefi/protocol/edid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Guid = uefi.Guid;
const Handle = uefi.Handle;
const Status = uefi.Status;
const cc = uefi.cc;
const Error = Status.Error;

/// EDID information for an active video output device
pub const Active = extern struct {
Expand Down Expand Up @@ -44,10 +45,19 @@ pub const Override = extern struct {
self: *const Override,
handle: Handle,
attributes: *Attributes,
edid_size: *usize,
edid: *?[*]u8,
) Status {
return self._get_edid(self, handle, attributes, edid_size, edid);
) !?[]u8 {
var size: usize = 0;
var ptr: ?[*]u8 = null;
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use = undefined for these

switch (self._get_edid(self, handle, attributes, &size, &ptr)) {
.success => {},
.unsupported => return Error.Unsupported,
else => |err| return uefi.unexpectedStatus(err),
}

if (size == 0 or ptr == null)
return null;

return ptr[0..size];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd assume this doesn't work as ptr needs to be null-unwrapped for slicing

}

pub const guid align(8) = Guid{
Expand Down
Loading