跳转到内容
标准库Zig 0.17.0官方原文
源码std/os/uefi.zig

第 1 行

std.os.uefi.assert

别名

第 2 行

别名目标 std.debug.assert

std.os.uefi.cc

常量

第 73 行

UEFI API 中所有外部函数使用的调用约定。

std.builtin.CallingConvention

std.os.uefi.DevicePath

别名

第 6 行

别名目标 std.os.uefi.device_path.DevicePath

std.os.uefi.efi_pool_memory_type

变量

第 17 行

使用池时要分配的内存类型。 默认为 .loader_data,这是 UEFI 应用程序分配池内存所用的默认数据分配类型。

tables.MemoryType

std.os.uefi.Error

错误集合

第 11 行

错误

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

类型

第 31 行

事件结构的句柄。

std.os.uefi.EventRegistration

类型

第 33 行

std.os.uefi.EventType

容器

第 35 行

字段

lo_context: u8 = 0
wait: bool = false

若此类事件尚未处于已触发状态,则每当通过 EFI_BOOT_SERVICES.WaitForEvent() 或 EFI_BOOT_SERVICES.CheckEvent() 等待此事件时,都会以事件的 NotifyTpl 将其 NotificationFunction 加入队列。

signal: bool = false

每当事件被通知时,将其 NotifyFunction 加入队列。

hi_context: u20 = 0
runtime: bool = false

事件分配在运行时内存中。若要在调用 EFI_BOOT_SERVICES.ExitBootServices() 后 发送事件信号,则事件的数据 结构和通知函数都必须分配在运行时 内存中。

timer: bool = false

std.os.uefi.EventType.signal_exit_boot_services

常量

第 55 行

此事件不应与其他事件类型组合。其功能等同于 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES 事件组。

EventType

std.os.uefi.EventType.signal_virtual_address_change

常量

第 64 行

执行 SetVirtualAddressMap() 时,系统会通知 此事件。该事件类型是 EVT_NOTIFY_SIGNAL、 EVT_RUNTIME 和 EVT_RUNTIME_CONTEXT 的组合,不应与 任何其他事件类型组合。

EventType

std.os.uefi.FileHandle

类型

第 218 行

EFI Shell 规范中规定的文件句柄

std.os.uefi.Guid

容器

第 96 行

除非另有规定,GUID 采用 align(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

std.os.uefi.Guid.eql

函数

第 120 行

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

参数

a: Guid
b: Guid

std.os.uefi.Guid.format

函数

第 109 行

将 GUID 格式化为小写十六进制 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 格式

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

参数

self: Guid
writer: *std.Io.Writer

错误

WriteFailed

详细诊断信息见 Writer 实现。

std.os.uefi.handle

变量

第 22 行

传递给 EFI 映像入口点的映像句柄。

Handle

std.os.uefi.Handle

类型

第 131 行

EFI 句柄表示一组相关接口。

std.os.uefi.hii

别名

第 7 行

别名目标 std.os.uefi.hii

std.os.uefi.IpAddress

容器

第 90 行

字段

std.os.uefi.Ipv4Address

容器

第 82 行

字段

address: [4]u8

std.os.uefi.Ipv6Address

容器

第 86 行

字段

address: [16]u8

std.os.uefi.MacAddress

容器

第 78 行

字段

address: [32]u8

std.os.uefi.Page

类型

第 28 行

UEFI 的内存接口仅操作 4096 字节页。

std.os.uefi.pool_allocator

别名

第 18 行

别名目标 std.os.uefi.pool_allocator.pool_allocator

std.os.uefi.protocol

别名

第 5 行

协议是由 GUID 标识的接口。

别名目标 std.os.uefi.protocol

std.os.uefi.raw_pool_allocator

别名

第 19 行

别名目标 std.os.uefi.pool_allocator.raw_pool_allocator

std.os.uefi.Status

别名

第 10 行

EFI 接口返回的状态码

别名目标 std.os.uefi.status.Status

std.os.uefi.std

别名

第 1 行

别名目标 std

std.os.uefi.system_table

变量

第 25 行

传给 EFI 映像入口点的 EFI 系统表指针。

*tables.SystemTable

std.os.uefi.tables

别名

第 12 行

别名目标 std.os.uefi.tables

std.os.uefi.Time

容器

第 134 行

此结构体表示时间信息。

字段

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

此时间相对于 UTC 的偏移量,单位为分钟。 允许值为 -1440 至 1440,或 unspecified_timezone。

daylight: packed struct(u8) {
    /// 若为 true,则时间已根据夏令时进行调整。
    in_daylight: bool,

    /// 若为 true,则该时间受夏令时影响。
    adjust_daylight: bool,

    _: u6,
}
_pad2: u8

std.os.uefi.Time.daysInYear

函数

第 180 行

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

参数

year: u16
max_month: u4

std.os.uefi.Time.toEpoch

函数

第 189 行

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

参数

self: std.os.uefi.Time

std.os.uefi.Time.unspecified_timezone

常量

第 178 行

时间应解释为本地时间

i16

std.os.uefi.TimeCapabilities

容器

第 206 行

时钟设备的能力

字段

resolution: u32

分辨率,单位为 Hz

accuracy: u32

精度误差率为百万分之 1e-6。

sets_to_zero: bool

若为 true,设置时间时会将设备时间清除到分辨率级别以下。

std.os.uefi.UnexpectedError

错误集合

第 235 行

错误

Unexpected

std.os.uefi.unexpectedStatus

函数

第 237 行

pub fn unexpectedStatus(status: Status) UnexpectedError

参数

status: Status
源码

std/os/uefi.zig

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

/// 协议是由 GUID 标识的接口。
pub const protocol = @import("uefi/protocol.zig");
pub const DevicePath = @import("uefi/device_path.zig").DevicePath;
pub const hii = @import("uefi/hii.zig");

/// EFI 接口返回的状态码
pub const Status = @import("uefi/status.zig").Status;
pub const Error = UnexpectedError || Status.Error;
pub const tables = @import("uefi/tables.zig");

/// 使用池时要分配的内存类型。
/// 默认为 `.loader_data`,这是 UEFI 应用程序分配池内存所用的默认数据分配类型。
///
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;

/// 传递给 EFI 映像入口点的映像句柄。
pub var handle: Handle = undefined;

/// 传给 EFI 映像入口点的 EFI 系统表指针。
pub var system_table: *tables.SystemTable = undefined;

/// UEFI 的内存接口仅操作 4096 字节页。
pub const Page = [4096]u8;

/// 事件结构的句柄。
pub const Event = *opaque {};

pub const EventRegistration = *const opaque {};

pub const EventType = packed struct(u32) {
    lo_context: u8 = 0,
    /// 若此类事件尚未处于已触发状态,则每当通过
    /// EFI_BOOT_SERVICES.WaitForEvent() 或 EFI_BOOT_SERVICES.CheckEvent()
    /// 等待此事件时,都会以事件的 NotifyTpl
    /// 将其 NotificationFunction 加入队列。
    wait: bool = false,
    /// 每当事件被通知时,将其 NotifyFunction 加入队列。
    signal: bool = false,
    hi_context: u20 = 0,
    /// 事件分配在运行时内存中。若要在调用 EFI_BOOT_SERVICES.ExitBootServices() 后
    /// 发送事件信号,则事件的数据
    /// 结构和通知函数都必须分配在运行时
    /// 内存中。
    runtime: bool = false,
    timer: bool = false,

    /// 此事件不应与其他事件类型组合。其功能等同于 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES 事件组。
    ///
    ///
    pub const signal_exit_boot_services: EventType = .{
        .signal = true,
        .lo_context = 1,
    };

    /// 执行 SetVirtualAddressMap() 时,系统会通知
    /// 此事件。该事件类型是 EVT_NOTIFY_SIGNAL、
    /// EVT_RUNTIME 和 EVT_RUNTIME_CONTEXT 的组合,不应与
    /// 任何其他事件类型组合。
    pub const signal_virtual_address_change: EventType = .{
        .runtime = true,
        .hi_context = 0x20000,
        .signal = true,
        .lo_context = 2,
    };
};

/// 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,
};

/// 除非另有规定,GUID 采用 align(8)。
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,

    /// 将 GUID 格式化为小写十六进制 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 格式
    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);
    }
};

/// EFI 句柄表示一组相关接口。
pub const Handle = *opaque {};

/// 此结构体表示时间信息。
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,

    /// 此时间相对于 UTC 的偏移量,单位为分钟。
    /// 允许值为 -1440 至 1440,或 unspecified_timezone。
    timezone: i16,
    daylight: packed struct(u8) {
        /// 若为 true,则时间已根据夏令时进行调整。
        in_daylight: bool,

        /// 若为 true,则该时间受夏令时影响。
        adjust_daylight: bool,

        _: u6,
    },

    _pad2: u8,

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

    /// 时间应解释为本地时间
    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);
    }
};

/// 时钟设备的能力
pub const TimeCapabilities = extern struct {
    /// 分辨率,单位为 Hz
    resolution: u32,

    /// 精度误差率为百万分之 1e-6。
    accuracy: u32,

    /// 若为 true,设置时间时会将设备时间清除到分辨率级别以下。
    sets_to_zero: bool,
};

/// EFI Shell 规范中规定的文件句柄
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: 是否调试打印遇到的错误?是否处理警告?
    _ = status;
    return error.Unexpected;
}