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 { test {
_ = darwin; _ = darwin;
_ = linux; _ = linux;
if (builtin.os.tag == .uefi) {
_ = uefi; _ = uefi;
}
_ = wasi; _ = wasi;
_ = windows; _ = windows;
_ = posix_spawn; _ = posix_spawn;

View File

@ -35,7 +35,7 @@ pub const Ipv6Address = extern struct {
address: [16]u8, address: [16]u8,
}; };
/// GUIDs must be align(8) /// GUIDs are align(8) unless otherwise specified.
pub const Guid = extern struct { pub const Guid = extern struct {
time_low: u32, time_low: u32,
time_mid: u16, time_mid: u16,
@ -150,3 +150,8 @@ test "GUID formatting" {
try std.testing.expect(std.mem.eql(u8, str, "32cb3c89-8080-427c-ba13-5049873bc287")); 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 const hii = @import("protocols/hii.zig");
pub usingnamespace @import("protocols/hii_database_protocol.zig"); pub usingnamespace @import("protocols/hii_database_protocol.zig");
pub usingnamespace @import("protocols/hii_popup_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 { pub const AbsolutePointerMode = extern struct {
absolute_min_x: u64, absolute_min_x: u64,
absolute_min_y: u64, absolute_min_y: u64,
@ -38,20 +44,18 @@ pub const AbsolutePointerMode = extern struct {
absolute_max_x: u64, absolute_max_x: u64,
absolute_max_y: u64, absolute_max_y: u64,
absolute_max_z: u64, absolute_max_z: u64,
attributes: packed struct { attributes: AbsolutePointerModeAttributes,
supports_alt_active: bool, };
supports_pressure_as_z: bool,
pub const AbsolutePointerStateActiveButtons = packed struct(u32) {
touch_active: bool,
alt_active: bool,
_pad: u30 = 0, _pad: u30 = 0,
},
}; };
pub const AbsolutePointerState = extern struct { pub const AbsolutePointerState = extern struct {
current_x: u64, current_x: u64,
current_y: u64, current_y: u64,
current_z: u64, current_z: u64,
active_buttons: packed struct { active_buttons: AbsolutePointerStateActiveButtons,
touch_active: bool,
alt_active: bool,
_pad: u30 = 0,
},
}; };

File diff suppressed because it is too large Load Diff

View File

@ -6,19 +6,17 @@ const Status = uefi.Status;
/// Override EDID information /// Override EDID information
pub const EdidOverrideProtocol = extern struct { 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. /// Returns policy information and potentially a replacement EDID for the specified video output device.
pub fn getEdid( pub fn getEdid(
self: *const EdidOverrideProtocol, self: *const EdidOverrideProtocol,
handle: Handle, handle: Handle,
/// The align(4) here should really be part of the EdidOverrideProtocolAttributes type. attributes: *EdidOverrideProtocolAttributes,
/// TODO remove this workaround when packed(u32) structs are implemented.
attributes: *align(4) EdidOverrideProtocolAttributes,
edid_size: *usize, edid_size: *usize,
edid: *?[*]u8, edid: *?[*]u8,
) Status { ) 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{ 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, dont_override: bool,
enable_hot_plug: bool, enable_hot_plug: bool,
_pad: u30 = 0, _pad: u30 = 0,

View File

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

View File

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

View File

@ -53,8 +53,7 @@ pub const KeyData = extern struct {
key_state: KeyState = undefined, key_state: KeyState = undefined,
}; };
pub const KeyState = extern struct { pub const KeyShiftState = packed struct(u32) {
key_shift_state: packed struct {
right_shift_pressed: bool, right_shift_pressed: bool,
left_shift_pressed: bool, left_shift_pressed: bool,
right_control_pressed: bool, right_control_pressed: bool,
@ -67,15 +66,20 @@ pub const KeyState = extern struct {
sys_req_pressed: bool, sys_req_pressed: bool,
_pad: u21 = 0, _pad: u21 = 0,
shift_state_valid: bool, shift_state_valid: bool,
}, };
key_toggle_state: packed struct {
pub const KeyToggleState = packed struct(u8) {
scroll_lock_active: bool, scroll_lock_active: bool,
num_lock_active: bool, num_lock_active: bool,
caps_lock_active: bool, caps_lock_active: bool,
_pad: u3 = 0, _pad: u3 = 0,
key_state_exposed: bool, key_state_exposed: bool,
toggle_state_valid: bool, toggle_state_valid: bool,
}, };
pub const KeyState = extern struct {
key_shift_state: KeyShiftState,
key_toggle_state: KeyToggleState,
}; };
pub const InputKey = extern struct { 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/configuration_table.zig");
pub usingnamespace @import("tables/system_table.zig"); pub usingnamespace @import("tables/system_table.zig");
pub usingnamespace @import("tables/table_header.zig"); pub usingnamespace @import("tables/table_header.zig");
test {
@import("std").testing.refAllDeclsRecursive(@This());
}

View File

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

View File

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