change uefi packed structs to new integer backed syntax (#13173)

* std.os.uefi: integer backed structs, add tests to catch regressions

device_path_protocol now uses extern structs with align(1) fields because
the transition to integer backed packed struct broke alignment

added comptime asserts that device_path_protocol structs do not violate
alignment and size specifications
This commit is contained in:
Nameless 2022-10-30 19:08:32 +00:00 committed by GitHub
parent 1696434063
commit 40e84a27d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 720 additions and 236 deletions

View File

@ -50,7 +50,9 @@ comptime {
test {
_ = darwin;
_ = linux;
_ = uefi;
if (builtin.os.tag == .uefi) {
_ = uefi;
}
_ = wasi;
_ = windows;
_ = posix_spawn;

View File

@ -35,7 +35,7 @@ pub const Ipv6Address = extern struct {
address: [16]u8,
};
/// GUIDs must be align(8)
/// GUIDs are align(8) unless otherwise specified.
pub const Guid = extern struct {
time_low: u32,
time_mid: u16,
@ -150,3 +150,8 @@ test "GUID formatting" {
try std.testing.expect(std.mem.eql(u8, str, "32cb3c89-8080-427c-ba13-5049873bc287"));
}
test {
_ = tables;
_ = protocols;
}

View File

@ -43,3 +43,8 @@ pub usingnamespace @import("protocols/udp6_protocol.zig");
pub const hii = @import("protocols/hii.zig");
pub usingnamespace @import("protocols/hii_database_protocol.zig");
pub usingnamespace @import("protocols/hii_popup_protocol.zig");
test {
@setEvalBranchQuota(2000);
@import("std").testing.refAllDeclsRecursive(@This());
}

View File

@ -31,6 +31,12 @@ pub const AbsolutePointerProtocol = extern struct {
};
};
pub const AbsolutePointerModeAttributes = packed struct(u32) {
supports_alt_active: bool,
supports_pressure_as_z: bool,
_pad: u30 = 0,
};
pub const AbsolutePointerMode = extern struct {
absolute_min_x: u64,
absolute_min_y: u64,
@ -38,20 +44,18 @@ pub const AbsolutePointerMode = extern struct {
absolute_max_x: u64,
absolute_max_y: u64,
absolute_max_z: u64,
attributes: packed struct {
supports_alt_active: bool,
supports_pressure_as_z: bool,
_pad: u30 = 0,
},
attributes: AbsolutePointerModeAttributes,
};
pub const AbsolutePointerStateActiveButtons = packed struct(u32) {
touch_active: bool,
alt_active: bool,
_pad: u30 = 0,
};
pub const AbsolutePointerState = extern struct {
current_x: u64,
current_y: u64,
current_z: u64,
active_buttons: packed struct {
touch_active: bool,
alt_active: bool,
_pad: u30 = 0,
},
active_buttons: AbsolutePointerStateActiveButtons,
};

File diff suppressed because it is too large Load Diff

View File

@ -6,19 +6,17 @@ const Status = uefi.Status;
/// Override EDID information
pub const EdidOverrideProtocol = extern struct {
_get_edid: std.meta.FnPtr(fn (*const EdidOverrideProtocol, Handle, *u32, *usize, *?[*]u8) callconv(.C) Status),
_get_edid: std.meta.FnPtr(fn (*const EdidOverrideProtocol, Handle, *EdidOverrideProtocolAttributes, *usize, *?[*]u8) callconv(.C) Status),
/// Returns policy information and potentially a replacement EDID for the specified video output device.
pub fn getEdid(
self: *const EdidOverrideProtocol,
handle: Handle,
/// The align(4) here should really be part of the EdidOverrideProtocolAttributes type.
/// TODO remove this workaround when packed(u32) structs are implemented.
attributes: *align(4) EdidOverrideProtocolAttributes,
attributes: *EdidOverrideProtocolAttributes,
edid_size: *usize,
edid: *?[*]u8,
) Status {
return self._get_edid(self, handle, @ptrCast(*u32, attributes), edid_size, edid);
return self._get_edid(self, handle, attributes, edid_size, edid);
}
pub const guid align(8) = Guid{
@ -31,7 +29,7 @@ pub const EdidOverrideProtocol = extern struct {
};
};
pub const EdidOverrideProtocolAttributes = packed struct {
pub const EdidOverrideProtocolAttributes = packed struct(u32) {
dont_override: bool,
enable_hot_plug: bool,
_pad: u30 = 0,

View File

@ -4,7 +4,7 @@ const Guid = uefi.Guid;
pub const HIIHandle = *opaque {};
/// The header found at the start of each package.
pub const HIIPackageHeader = packed struct {
pub const HIIPackageHeader = packed struct(u32) {
length: u24,
type: u8,
@ -43,23 +43,27 @@ pub const HIISimplifiedFontPackage = extern struct {
}
};
pub const NarrowGlyphAttributes = packed struct(u8) {
non_spacing: bool,
wide: bool,
_pad: u6 = 0,
};
pub const NarrowGlyph = extern struct {
unicode_weight: u16,
attributes: packed struct {
non_spacing: bool,
wide: bool,
_pad: u6 = 0,
},
attributes: NarrowGlyphAttributes,
glyph_col_1: [19]u8,
};
pub const WideGlyphAttributes = packed struct(u8) {
non_spacing: bool,
wide: bool,
_pad: u6 = 0,
};
pub const WideGlyph = extern struct {
unicode_weight: u16,
attributes: packed struct {
non_spacing: bool,
wide: bool,
_pad: u6,
},
attributes: WideGlyphAttributes,
glyph_col_1: [19]u8,
glyph_col_2: [19]u8,
_pad: [3]u8 = [_]u8{0} ** 3,

View File

@ -121,7 +121,7 @@ pub const SimpleNetworkMode = extern struct {
media_present: bool,
};
pub const SimpleNetworkReceiveFilter = packed struct {
pub const SimpleNetworkReceiveFilter = packed struct(u32) {
receive_unicast: bool,
receive_multicast: bool,
receive_broadcast: bool,
@ -165,7 +165,7 @@ pub const NetworkStatistics = extern struct {
tx_retry_frames: u64,
};
pub const SimpleNetworkInterruptStatus = packed struct {
pub const SimpleNetworkInterruptStatus = packed struct(u32) {
receive_interrupt: bool,
transmit_interrupt: bool,
command_interrupt: bool,

View File

@ -53,29 +53,33 @@ pub const KeyData = extern struct {
key_state: KeyState = undefined,
};
pub const KeyShiftState = packed struct(u32) {
right_shift_pressed: bool,
left_shift_pressed: bool,
right_control_pressed: bool,
left_control_pressed: bool,
right_alt_pressed: bool,
left_alt_pressed: bool,
right_logo_pressed: bool,
left_logo_pressed: bool,
menu_key_pressed: bool,
sys_req_pressed: bool,
_pad: u21 = 0,
shift_state_valid: bool,
};
pub const KeyToggleState = packed struct(u8) {
scroll_lock_active: bool,
num_lock_active: bool,
caps_lock_active: bool,
_pad: u3 = 0,
key_state_exposed: bool,
toggle_state_valid: bool,
};
pub const KeyState = extern struct {
key_shift_state: packed struct {
right_shift_pressed: bool,
left_shift_pressed: bool,
right_control_pressed: bool,
left_control_pressed: bool,
right_alt_pressed: bool,
left_alt_pressed: bool,
right_logo_pressed: bool,
left_logo_pressed: bool,
menu_key_pressed: bool,
sys_req_pressed: bool,
_pad: u21 = 0,
shift_state_valid: bool,
},
key_toggle_state: packed struct {
scroll_lock_active: bool,
num_lock_active: bool,
caps_lock_active: bool,
_pad: u3 = 0,
key_state_exposed: bool,
toggle_state_valid: bool,
},
key_shift_state: KeyShiftState,
key_toggle_state: KeyToggleState,
};
pub const InputKey = extern struct {

View File

@ -3,3 +3,7 @@ pub usingnamespace @import("tables/runtime_services.zig");
pub usingnamespace @import("tables/configuration_table.zig");
pub usingnamespace @import("tables/system_table.zig");
pub usingnamespace @import("tables/table_header.zig");
test {
@import("std").testing.refAllDeclsRecursive(@This());
}

View File

@ -219,33 +219,32 @@ pub const MemoryType = enum(u32) {
_,
};
pub const MemoryDescriptorAttribute = packed struct(u64) {
uc: bool,
wc: bool,
wt: bool,
wb: bool,
uce: bool,
_pad1: u7 = 0,
wp: bool,
rp: bool,
xp: bool,
nv: bool,
more_reliable: bool,
ro: bool,
sp: bool,
cpu_crypto: bool,
_pad2: u43 = 0,
memory_runtime: bool,
};
pub const MemoryDescriptor = extern struct {
type: MemoryType,
padding: u32,
physical_start: u64,
virtual_start: u64,
number_of_pages: usize,
attribute: packed struct {
uc: bool,
wc: bool,
wt: bool,
wb: bool,
uce: bool,
_pad1: u3,
_pad2: u4,
wp: bool,
rp: bool,
xp: bool,
nv: bool,
more_reliable: bool,
ro: bool,
sp: bool,
cpu_crypto: bool,
_pad3: u4,
_pad4: u32,
_pad5: u7,
memory_runtime: bool,
},
attribute: MemoryDescriptorAttribute,
};
pub const LocateSearchType = enum(u32) {
@ -254,14 +253,14 @@ pub const LocateSearchType = enum(u32) {
ByProtocol,
};
pub const OpenProtocolAttributes = packed struct {
pub const OpenProtocolAttributes = packed struct(u32) {
by_handle_protocol: bool = false,
get_protocol: bool = false,
test_protocol: bool = false,
by_child_controller: bool = false,
by_driver: bool = false,
exclusive: bool = false,
_pad: u26 = 0,
reserved: u26 = 0,
};
pub const ProtocolInformationEntry = extern struct {

View File

@ -78,7 +78,7 @@ pub const CapsuleHeader = extern struct {
pub const UefiCapsuleBlockDescriptor = extern struct {
length: u64,
address: union {
address: extern union {
dataBlock: EfiPhysicalAddress,
continuationPointer: EfiPhysicalAddress,
},