Skip to content
Standard libraryZig 0.17.0Official documentation
Sourcestd/os/uefi.zig

Source line 1

std.os.uefi.assert

alias

Source line 2

Alias of std.debug.assert

std.os.uefi.cc

constant

Source line 73

The calling convention used for all external functions part of the UEFI API.

std.builtin.CallingConvention

std.os.uefi.DevicePath

alias

Source line 6

Alias of std.os.uefi.device_path.DevicePath

std.os.uefi.efi_pool_memory_type

variable

Source line 17

The memory type to allocate when using the pool. Defaults to .loader_data, the default data allocation type used by UEFI applications to allocate pool memory.

tables.MemoryType

std.os.uefi.Error

error-set

Source line 11

Errors

Aborted Error
AccessDenied Error
AlreadyStarted Error
BadBufferSize Error
BufferTooSmall Error
CompromisedData Error
ConnectionFin Error
ConnectionRefused Error
ConnectionReset Error
CrcError Error
DeviceError Error
EndOfFile Error
EndOfMedia Error
HostUnreachable Error
HttpError Error
IcmpError Error
IncompatibleVersion Error
InvalidLanguage Error
InvalidParameter Error
IpAddressConflict Error
LoadError Error
MediaChanged Error
NetworkUnreachable Error
NoMapping Error
NoMedia Error
NoResponse Error
NotFound Error
NotReady Error
NotStarted Error
OutOfResources Error
PortUnreachable Error
ProtocolError Error
ProtocolUnreachable Error
SecurityViolation Error
TftpError Error
Timeout Error
Unexpected UnexpectedError
Unsupported Error
VolumeCorrupted Error
VolumeFull Error
WriteProtected Error

std.os.uefi.Event

type

Source line 31

A handle to an event structure.

std.os.uefi.EventRegistration

type

Source line 33

std.os.uefi.EventType

container

Source line 35

Fields

lo_context: u8 = 0
wait: bool = false

If an event of this type is not already in the signaled state, then the event’s NotificationFunction will be queued at the event’s NotifyTpl whenever the event is being waited on via EFI_BOOT_SERVICES.WaitForEvent() or EFI_BOOT_SERVICES.CheckEvent() .

signal: bool = false

The event’s NotifyFunction is queued whenever the event is signaled.

hi_context: u20 = 0
runtime: bool = false

The event is allocated from runtime memory. If an event is to be signaled after the call to EFI_BOOT_SERVICES.ExitBootServices() the event’s data structure and notification function need to be allocated from runtime memory.

timer: bool = false

std.os.uefi.EventType.signal_exit_boot_services

constant

Source line 55

This event should not be combined with any other event types. This event type is functionally equivalent to the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group.

EventType

std.os.uefi.EventType.signal_virtual_address_change

constant

Source line 64

The event is to be notified by the system when SetVirtualAddressMap() is performed. This event type is a composite of EVT_NOTIFY_SIGNAL, EVT_RUNTIME, and EVT_RUNTIME_CONTEXT and should not be combined with any other event types.

EventType

std.os.uefi.FileHandle

type

Source line 218

File Handle as specified in the EFI Shell Spec

std.os.uefi.Guid

container

Source line 96

GUIDs are align(8) unless otherwise specified.

Fields

time_low: u32 align(8)
time_mid: u16
time_high_and_version: u16
clock_seq_high_and_reserved: u8
clock_seq_low: u8
node: [6]u8

std.os.uefi.Guid.eql

function

Source line 120

pub fn eql(a: Guid, b: Guid) bool

Parameters

a: Guid
b: Guid

std.os.uefi.Guid.format

function

Source line 109

Format GUID into hexadecimal lowercase xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format

pub fn format(self: Guid, writer: *std.Io.Writer) std.Io.Writer.Error!void

Parameters

self: Guid
writer: *std.Io.Writer

Errors

WriteFailed

See the Writer implementation for detailed diagnostics.

std.os.uefi.handle

variable

Source line 22

The EFI image's handle that is passed to its entry point.

Handle

std.os.uefi.Handle

type

Source line 131

An EFI Handle represents a collection of related interfaces.

std.os.uefi.hii

alias

Source line 7

Alias of std.os.uefi.hii

std.os.uefi.IpAddress

container

Source line 90

Fields

std.os.uefi.Ipv4Address

container

Source line 82

Fields

address: [4]u8

std.os.uefi.Ipv6Address

container

Source line 86

Fields

address: [16]u8

std.os.uefi.MacAddress

container

Source line 78

Fields

address: [32]u8

std.os.uefi.Page

type

Source line 28

UEFI's memory interfaces exclusively act on 4096-byte pages.

std.os.uefi.pool_allocator

alias

Source line 18

Alias of std.os.uefi.pool_allocator.pool_allocator

std.os.uefi.protocol

alias

Source line 5

A protocol is an interface identified by a GUID.

Alias of std.os.uefi.protocol

std.os.uefi.raw_pool_allocator

alias

Source line 19

Alias of std.os.uefi.pool_allocator.raw_pool_allocator

std.os.uefi.Status

alias

Source line 10

Status codes returned by EFI interfaces

Alias of std.os.uefi.status.Status

std.os.uefi.std

alias

Source line 1

Alias of std

std.os.uefi.system_table

variable

Source line 25

A pointer to the EFI System Table that is passed to the EFI image's entry point.

*tables.SystemTable

std.os.uefi.tables

alias

Source line 12

Alias of std.os.uefi.tables

std.os.uefi.Time

container

Source line 134

This structure represents time information.

Fields

year: u16

1900 - 9999

month: u8

1 - 12

day: u8

1 - 31

hour: u8

0 - 23

minute: u8

0 - 59

second: u8

0 - 59

_pad1: u8
nanosecond: u32

0 - 999999999

timezone: i16

The time's offset in minutes from UTC. Allowed values are -1440 to 1440 or unspecified_timezone

daylight: packed struct(u8) {
    /// If true, the time has been adjusted for daylight savings time.
    in_daylight: bool,

    /// If true, the time is affected by daylight savings time.
    adjust_daylight: bool,

    _: u6,
}
_pad2: u8

std.os.uefi.Time.daysInYear

function

Source line 180

fn daysInYear(year: u16, max_month: u4) u9

Parameters

year: u16
max_month: u4

std.os.uefi.Time.toEpoch

function

Source line 189

pub fn toEpoch(self: std.os.uefi.Time) u64

Parameters

self: std.os.uefi.Time

std.os.uefi.Time.unspecified_timezone

constant

Source line 178

Time is to be interpreted as local time

i16

std.os.uefi.TimeCapabilities

container

Source line 206

Capabilities of the clock device

Fields

resolution: u32

Resolution in Hz

accuracy: u32

Accuracy in an error rate of 1e-6 parts per million.

sets_to_zero: bool

If true, a time set operation clears the device's time below the resolution level.

std.os.uefi.UnexpectedError

error-set

Source line 235

Errors

Unexpected

std.os.uefi.unexpectedStatus

function

Source line 237

pub fn unexpectedStatus(status: Status) UnexpectedError

Parameters

status: Status
Source code

std/os/uefi.zig

const std = @import("../std.zig");
const assert = std.debug.assert;

/// A protocol is an interface identified by a GUID.
pub const protocol = @import("uefi/protocol.zig");
pub const DevicePath = @import("uefi/device_path.zig").DevicePath;
pub const hii = @import("uefi/hii.zig");

/// Status codes returned by EFI interfaces
pub const Status = @import("uefi/status.zig").Status;
pub const Error = UnexpectedError || Status.Error;
pub const tables = @import("uefi/tables.zig");

/// The memory type to allocate when using the pool.
/// Defaults to `.loader_data`, the default data allocation type
/// used by UEFI applications to allocate pool memory.
pub var efi_pool_memory_type: tables.MemoryType = .loader_data;
pub const pool_allocator = @import("uefi/pool_allocator.zig").pool_allocator;
pub const raw_pool_allocator = @import("uefi/pool_allocator.zig").raw_pool_allocator;

/// The EFI image's handle that is passed to its entry point.
pub var handle: Handle = undefined;

/// A pointer to the EFI System Table that is passed to the EFI image's entry point.
pub var system_table: *tables.SystemTable = undefined;

/// UEFI's memory interfaces exclusively act on 4096-byte pages.
pub const Page = [4096]u8;

/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = *const opaque {};

pub const EventType = packed struct(u32) {
    lo_context: u8 = 0,
    /// If an event of this type is not already in the signaled state, then
    /// the event’s NotificationFunction will be queued at the event’s NotifyTpl
    /// whenever the event is being waited on via EFI_BOOT_SERVICES.WaitForEvent()
    /// or EFI_BOOT_SERVICES.CheckEvent() .
    wait: bool = false,
    /// The event’s NotifyFunction is queued whenever the event is signaled.
    signal: bool = false,
    hi_context: u20 = 0,
    /// The event is allocated from runtime memory. If an event is to be signaled
    /// after the call to EFI_BOOT_SERVICES.ExitBootServices() the event’s data
    /// structure and notification function need to be allocated from runtime
    /// memory.
    runtime: bool = false,
    timer: bool = false,

    /// This event should not be combined with any other event types. This event
    /// type is functionally equivalent to the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES
    /// event group.
    pub const signal_exit_boot_services: EventType = .{
        .signal = true,
        .lo_context = 1,
    };

    /// The event is to be notified by the system when SetVirtualAddressMap()
    /// is performed. This event type is a composite of EVT_NOTIFY_SIGNAL,
    /// EVT_RUNTIME, and EVT_RUNTIME_CONTEXT and should not be combined with
    /// any other event types.
    pub const signal_virtual_address_change: EventType = .{
        .runtime = true,
        .hi_context = 0x20000,
        .signal = true,
        .lo_context = 2,
    };
};

/// The calling convention used for all external functions part of the UEFI API.
pub const cc: std.builtin.CallingConvention = switch (@import("builtin").target.cpu.arch) {
    .x86_64 => .{ .x86_64_win = .{} },
    else => .c,
};

pub const MacAddress = extern struct {
    address: [32]u8,
};

pub const Ipv4Address = extern struct {
    address: [4]u8,
};

pub const Ipv6Address = extern struct {
    address: [16]u8,
};

pub const IpAddress = extern union {
    v4: Ipv4Address,
    v6: Ipv6Address,
};

/// GUIDs are align(8) unless otherwise specified.
pub const Guid = extern struct {
    comptime {
        std.debug.assert(std.mem.Alignment.of(Guid) == .@"8");
    }

    time_low: u32 align(8),
    time_mid: u16,
    time_high_and_version: u16,
    clock_seq_high_and_reserved: u8,
    clock_seq_low: u8,
    node: [6]u8,

    /// Format GUID into hexadecimal lowercase xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format
    pub fn format(self: Guid, writer: *std.Io.Writer) std.Io.Writer.Error!void {
        return writer.print("{x:0>8}-{x:0>4}-{x:0>4}-{x:0>2}{x:0>2}-{x}", .{
            self.time_low,
            self.time_mid,
            self.time_high_and_version,
            self.clock_seq_high_and_reserved,
            self.clock_seq_low,
            self.node,
        });
    }

    pub fn eql(a: Guid, b: Guid) bool {
        return a.time_low == b.time_low and
            a.time_mid == b.time_mid and
            a.time_high_and_version == b.time_high_and_version and
            a.clock_seq_high_and_reserved == b.clock_seq_high_and_reserved and
            a.clock_seq_low == b.clock_seq_low and
            std.mem.eql(u8, &a.node, &b.node);
    }
};

/// An EFI Handle represents a collection of related interfaces.
pub const Handle = *opaque {};

/// This structure represents time information.
pub const Time = extern struct {
    /// 1900 - 9999
    year: u16,

    /// 1 - 12
    month: u8,

    /// 1 - 31
    day: u8,

    /// 0 - 23
    hour: u8,

    /// 0 - 59
    minute: u8,

    /// 0 - 59
    second: u8,

    _pad1: u8,

    /// 0 - 999999999
    nanosecond: u32,

    /// The time's offset in minutes from UTC.
    /// Allowed values are -1440 to 1440 or unspecified_timezone
    timezone: i16,
    daylight: packed struct(u8) {
        /// If true, the time has been adjusted for daylight savings time.
        in_daylight: bool,

        /// If true, the time is affected by daylight savings time.
        adjust_daylight: bool,

        _: u6,
    },

    _pad2: u8,

    comptime {
        std.debug.assert(@sizeOf(Time) == 16);
    }

    /// Time is to be interpreted as local time
    pub const unspecified_timezone: i16 = 0x7ff;

    fn daysInYear(year: u16, max_month: u4) u9 {
        var days: u9 = 0;
        var month: u4 = 0;
        while (month < max_month) : (month += 1) {
            days += std.time.epoch.getDaysInMonth(year, @fromBackingInt(@intCast(month + 1)));
        }
        return days;
    }

    pub fn toEpoch(self: std.os.uefi.Time) u64 {
        var year: u16 = 0;
        var days: u32 = 0;

        while (year < (self.year - 1971)) : (year += 1) {
            days += daysInYear(year + 1970, 12);
        }

        days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;
        const hours: u64 = self.hour + (days * 24);
        const minutes: u64 = self.minute + (hours * 60);
        const seconds: u64 = self.second + (minutes * std.time.s_per_min);
        return self.nanosecond + (seconds * std.time.ns_per_s);
    }
};

/// Capabilities of the clock device
pub const TimeCapabilities = extern struct {
    /// Resolution in Hz
    resolution: u32,

    /// Accuracy in an error rate of 1e-6 parts per million.
    accuracy: u32,

    /// If true, a time set operation clears the device's time below the resolution level.
    sets_to_zero: bool,
};

/// File Handle as specified in the EFI Shell Spec
pub const FileHandle = *opaque {};

test "GUID formatting" {
    const bytes: [16]u8 = .{ 137, 60, 203, 50, 128, 128, 124, 66, 186, 19, 80, 73, 135, 59, 194, 135 };
    const guid: Guid = @bitCast(bytes);

    const str = try std.fmt.allocPrint(std.testing.allocator, "{f}", .{guid});
    defer std.testing.allocator.free(str);

    try std.testing.expectEqualStrings(str, "32cb3c89-8080-427c-ba13-5049873bc287");
}

test {
    _ = tables;
    _ = protocol;
}

pub const UnexpectedError = error{Unexpected};

pub fn unexpectedStatus(status: Status) UnexpectedError {
    // TODO: debug printing the encountered error? maybe handle warnings?
    _ = status;
    return error.Unexpected;
}