跳转到内容

std.os.uefi.protocol.device_path

标准库Zig 0.17.0官方原文
源码std/os/uefi/protocol/device_path.zig

第 1 行

std.os.uefi.protocol.device_path.Allocator

别名

第 4 行

别名目标 std.mem.Allocator

std.os.uefi.protocol.device_path.assert

别名

第 6 行

别名目标 std.debug.assert

std.os.uefi.protocol.device_path.DevicePath

容器

第 11 行

字段

type: uefi.DevicePath.Type
subtype: u8
length: u16 align(1)

std.os.uefi.protocol.device_path.DevicePath.createFileDevicePath

函数

第 47 行

从已有设备路径和文件路径创建文件设备路径。

pub fn createFileDevicePath( self: *const DevicePath, allocator: Allocator, path: []const u16, ) CreateFileDevicePathError!*const DevicePath

参数

self: *const DevicePath
allocator: Allocator
path: []const u16

错误

OutOfMemory Error

std.os.uefi.protocol.device_path.DevicePath.CreateFileDevicePathError

别名

第 16 行

别名目标 std.mem.Allocator.Error

std.os.uefi.protocol.device_path.DevicePath.getDevicePath

函数

第 84 行

pub fn getDevicePath(self: *const DevicePath) ?uefi.DevicePath

参数

self: *const DevicePath

std.os.uefi.protocol.device_path.DevicePath.guid

常量

第 18 行

std.os.uefi.protocol.device_path.DevicePath.initSubtype

函数

第 103 行

pub fn initSubtype(self: *const DevicePath, comptime TUnion: type) ?TUnion

参数

self: *const DevicePath
TUnion: type

std.os.uefi.protocol.device_path.DevicePath.next

函数

第 28 行

返回序列中的下一个 DevicePath 节点(如果存在)。

pub fn next(self: *const DevicePath) ?*const DevicePath

参数

self: *const DevicePath

std.os.uefi.protocol.device_path.DevicePath.size

函数

第 36 行

计算设备路径结构的总字节长度,包括设备路径结束节点。

pub fn size(self: *const DevicePath) usize

参数

self: *const DevicePath

std.os.uefi.protocol.device_path.Guid

别名

第 5 行

别名目标 std.os.uefi.Guid

std.os.uefi.protocol.device_path.mem

别名

第 2 行

别名目标 std.mem

std.os.uefi.protocol.device_path.std

别名

第 1 行

别名目标 std

std.os.uefi.protocol.device_path.uefi

别名

第 3 行

别名目标 std.os.uefi

源码

std/os/uefi/protocol/device_path.zig

const std = @import("../../../std.zig");
const mem = std.mem;
const uefi = std.os.uefi;
const Allocator = mem.Allocator;
const Guid = uefi.Guid;
const assert = std.debug.assert;

// 所有设备路径节点均按字节紧凑排列,可出现在任意字节边界。
// 所有引用设备路径节点的代码都必须假定其全部字段未对齐。

pub const DevicePath = extern struct {
    type: uefi.DevicePath.Type,
    subtype: u8,
    length: u16 align(1),

    pub const CreateFileDevicePathError = Allocator.Error;

    pub const guid align(8) = Guid{
        .time_low = 0x09576e91,
        .time_mid = 0x6d3f,
        .time_high_and_version = 0x11d2,
        .clock_seq_high_and_reserved = 0x8e,
        .clock_seq_low = 0x39,
        .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
    };

    /// 返回序列中的下一个 DevicePath 节点(如果存在)。
    pub fn next(self: *const DevicePath) ?*const DevicePath {
        const subtype: uefi.DevicePath.End.Subtype = @fromBackingInt(@intCast(self.subtype));
        if (self.type == .end and subtype == .end_entire) return null;
        const bytes: [*]const u8 = @ptrCast(self);
        return @ptrCast(bytes + self.length);
    }

    /// 计算设备路径结构的总字节长度,包括设备路径结束节点。
    pub fn size(self: *const DevicePath) usize {
        var node = self;

        while (node.next()) |next_node| {
            node = next_node;
        }

        return (@intFromPtr(node) + node.length) - @intFromPtr(self);
    }

    /// 从已有设备路径和文件路径创建文件设备路径。
    pub fn createFileDevicePath(
        self: *const DevicePath,
        allocator: Allocator,
        path: []const u16,
    ) CreateFileDevicePathError!*const DevicePath {
        const path_size = self.size();

        // 2 * (path.len + 1) 用于路径及其空终止符,它们采用 u16
        // DevicePath 用于结束节点之前的额外节点
        var buf = try allocator.alloc(u8, path_size + 2 * (path.len + 1) + @sizeOf(DevicePath));

        @memcpy(buf[0..path_size], @as([*]const u8, @ptrCast(self))[0..path_size]);

        // 指向当前链末尾节点副本的指针,即缓冲区末尾减 4,因为末尾节点自身占 4 字节(type: u8 + subtype: u8 + length: u16)。
        //
        var new = @as(*uefi.DevicePath.Media.FilePathDevicePath, @ptrCast(buf.ptr + path_size - 4));

        new.type = .media;
        new.subtype = .file_path;
        new.length = @sizeOf(uefi.DevicePath.Media.FilePathDevicePath) + 2 * (@as(u16, @intCast(path.len)) + 1);

        // 与 new.getPath() 相同,但因正在填充它而不是 const。
        var ptr = @as([*:0]align(1) u16, @ptrCast(@as([*]u8, @ptrCast(new)) + @sizeOf(uefi.DevicePath.Media.FilePathDevicePath)));

        for (path, 0..) |s, i|
            ptr[i] = s;

        ptr[path.len] = 0;

        var end = @as(*uefi.DevicePath.End.EndEntireDevicePath, @ptrCast(@constCast(@as(*DevicePath, @ptrCast(new)).next().?)));
        end.type = .end;
        end.subtype = .end_entire;
        end.length = @sizeOf(uefi.DevicePath.End.EndEntireDevicePath);

        return @as(*DevicePath, @ptrCast(buf.ptr));
    }

    pub fn getDevicePath(self: *const DevicePath) ?uefi.DevicePath {
        const u_info = @typeInfo(uefi.DevicePath).@"union";
        inline for (u_info.field_names, u_info.field_types) |ufield_name, ufield_type| {
            const enum_value = std.meta.stringToEnum(uefi.DevicePath.Type, ufield_name);

            // 已获取与 self.type 关联的联合体类型,现在
            // 需要初始化它及其子类型
            if (self.type == enum_value) {
                const subtype = self.initSubtype(ufield_type);
                if (subtype) |sb| {
                    // 例如 return .{ .hardware = .{ .pci = @ptrCast(...) } }
                    return @unionInit(uefi.DevicePath, ufield_name, sb);
                }
            }
        }

        return null;
    }

    pub fn initSubtype(self: *const DevicePath, comptime TUnion: type) ?TUnion {
        const type_info = @typeInfo(TUnion).@"union";
        const TTag = type_info.tag_type.?;

        inline for (type_info.field_names, type_info.field_types) |subtype_name, subtype_type| {
            // 标签名与联合体名称一致,因此直接从枚举获取
            const tag_val: u8 = @backingInt(@field(TTag, subtype_name));

            if (self.subtype == tag_val) {
                // 例如 expr = .{ .pci = @ptrCast(...) }
                return @unionInit(TUnion, subtype_name, @as(subtype_type, @ptrCast(self)));
            }
        }

        return null;
    }
};

comptime {
    assert(4 == @sizeOf(DevicePath));
    assert(1 == @alignOf(DevicePath));

    assert(0 == @offsetOf(DevicePath, "type"));
    assert(1 == @offsetOf(DevicePath, "subtype"));
    assert(2 == @offsetOf(DevicePath, "length"));
}