zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

blob 9674e704 (7561B) - Raw


      1 const std = @import("../std.zig");
      2 
      3 /// A protocol is an interface identified by a GUID.
      4 pub const protocol = @import("uefi/protocol.zig");
      5 pub const DevicePath = @import("uefi/device_path.zig").DevicePath;
      6 pub const hii = @import("uefi/hii.zig");
      7 
      8 /// Status codes returned by EFI interfaces
      9 pub const Status = @import("uefi/status.zig").Status;
     10 pub const tables = @import("uefi/tables.zig");
     11 
     12 /// The memory type to allocate when using the pool.
     13 /// Defaults to `.loader_data`, the default data allocation type
     14 /// used by UEFI applications to allocate pool memory.
     15 pub var efi_pool_memory_type: tables.MemoryType = .loader_data;
     16 pub const pool_allocator = @import("uefi/pool_allocator.zig").pool_allocator;
     17 pub const raw_pool_allocator = @import("uefi/pool_allocator.zig").raw_pool_allocator;
     18 
     19 /// The EFI image's handle that is passed to its entry point.
     20 pub var handle: Handle = undefined;
     21 
     22 /// A pointer to the EFI System Table that is passed to the EFI image's entry point.
     23 pub var system_table: *tables.SystemTable = undefined;
     24 
     25 /// A handle to an event structure.
     26 pub const Event = *opaque {};
     27 
     28 /// The calling convention used for all external functions part of the UEFI API.
     29 pub const cc: std.builtin.CallingConvention = switch (@import("builtin").target.cpu.arch) {
     30     .x86_64 => .{ .x86_64_win = .{} },
     31     else => .c,
     32 };
     33 
     34 pub const MacAddress = extern struct {
     35     address: [32]u8,
     36 };
     37 
     38 pub const Ipv4Address = extern struct {
     39     address: [4]u8,
     40 };
     41 
     42 pub const Ipv6Address = extern struct {
     43     address: [16]u8,
     44 };
     45 
     46 /// GUIDs are align(8) unless otherwise specified.
     47 pub const Guid = extern struct {
     48     time_low: u32,
     49     time_mid: u16,
     50     time_high_and_version: u16,
     51     clock_seq_high_and_reserved: u8,
     52     clock_seq_low: u8,
     53     node: [6]u8,
     54 
     55     /// Format GUID into hexadecimal lowercase xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format
     56     pub fn format(
     57         self: @This(),
     58         comptime f: []const u8,
     59         options: std.fmt.FormatOptions,
     60         writer: anytype,
     61     ) !void {
     62         _ = options;
     63         if (f.len == 0) {
     64             const fmt = std.fmt.fmtSliceHexLower;
     65 
     66             const time_low = @byteSwap(self.time_low);
     67             const time_mid = @byteSwap(self.time_mid);
     68             const time_high_and_version = @byteSwap(self.time_high_and_version);
     69 
     70             return std.fmt.format(writer, "{:0>8}-{:0>4}-{:0>4}-{:0>2}{:0>2}-{:0>12}", .{
     71                 fmt(std.mem.asBytes(&time_low)),
     72                 fmt(std.mem.asBytes(&time_mid)),
     73                 fmt(std.mem.asBytes(&time_high_and_version)),
     74                 fmt(std.mem.asBytes(&self.clock_seq_high_and_reserved)),
     75                 fmt(std.mem.asBytes(&self.clock_seq_low)),
     76                 fmt(std.mem.asBytes(&self.node)),
     77             });
     78         } else {
     79             std.fmt.invalidFmtError(f, self);
     80         }
     81     }
     82 
     83     pub fn eql(a: std.os.uefi.Guid, b: std.os.uefi.Guid) bool {
     84         return a.time_low == b.time_low and
     85             a.time_mid == b.time_mid and
     86             a.time_high_and_version == b.time_high_and_version and
     87             a.clock_seq_high_and_reserved == b.clock_seq_high_and_reserved and
     88             a.clock_seq_low == b.clock_seq_low and
     89             std.mem.eql(u8, &a.node, &b.node);
     90     }
     91 };
     92 
     93 /// An EFI Handle represents a collection of related interfaces.
     94 pub const Handle = *opaque {};
     95 
     96 /// This structure represents time information.
     97 pub const Time = extern struct {
     98     /// 1900 - 9999
     99     year: u16,
    100 
    101     /// 1 - 12
    102     month: u8,
    103 
    104     /// 1 - 31
    105     day: u8,
    106 
    107     /// 0 - 23
    108     hour: u8,
    109 
    110     /// 0 - 59
    111     minute: u8,
    112 
    113     /// 0 - 59
    114     second: u8,
    115 
    116     _pad1: u8,
    117 
    118     /// 0 - 999999999
    119     nanosecond: u32,
    120 
    121     /// The time's offset in minutes from UTC.
    122     /// Allowed values are -1440 to 1440 or unspecified_timezone
    123     timezone: i16,
    124     daylight: packed struct(u8) {
    125         /// If true, the time has been adjusted for daylight savings time.
    126         in_daylight: bool,
    127 
    128         /// If true, the time is affected by daylight savings time.
    129         adjust_daylight: bool,
    130 
    131         _: u6,
    132     },
    133 
    134     _pad2: u8,
    135 
    136     comptime {
    137         std.debug.assert(@sizeOf(Time) == 16);
    138     }
    139 
    140     /// Time is to be interpreted as local time
    141     pub const unspecified_timezone: i16 = 0x7ff;
    142 
    143     fn daysInYear(year: u16, max_month: u4) u9 {
    144         var days: u9 = 0;
    145         var month: u4 = 0;
    146         while (month < max_month) : (month += 1) {
    147             days += std.time.epoch.getDaysInMonth(year, @enumFromInt(month + 1));
    148         }
    149         return days;
    150     }
    151 
    152     pub fn toEpoch(self: std.os.uefi.Time) u64 {
    153         var year: u16 = 0;
    154         var days: u32 = 0;
    155 
    156         while (year < (self.year - 1971)) : (year += 1) {
    157             days += daysInYear(year + 1970, 12);
    158         }
    159 
    160         days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;
    161         const hours: u64 = self.hour + (days * 24);
    162         const minutes: u64 = self.minute + (hours * 60);
    163         const seconds: u64 = self.second + (minutes * std.time.s_per_min);
    164         return self.nanosecond + (seconds * std.time.ns_per_s);
    165     }
    166 };
    167 
    168 /// Capabilities of the clock device
    169 pub const TimeCapabilities = extern struct {
    170     /// Resolution in Hz
    171     resolution: u32,
    172 
    173     /// Accuracy in an error rate of 1e-6 parts per million.
    174     accuracy: u32,
    175 
    176     /// If true, a time set operation clears the device's time below the resolution level.
    177     sets_to_zero: bool,
    178 };
    179 
    180 /// File Handle as specified in the EFI Shell Spec
    181 pub const FileHandle = *opaque {};
    182 
    183 test "GUID formatting" {
    184     const bytes = [_]u8{ 137, 60, 203, 50, 128, 128, 124, 66, 186, 19, 80, 73, 135, 59, 194, 135 };
    185     const guid: Guid = @bitCast(bytes);
    186 
    187     const str = try std.fmt.allocPrint(std.testing.allocator, "{}", .{guid});
    188     defer std.testing.allocator.free(str);
    189 
    190     try std.testing.expect(std.mem.eql(u8, str, "32cb3c89-8080-427c-ba13-5049873bc287"));
    191 }
    192 
    193 pub const FileInfo = extern struct {
    194     size: u64,
    195     file_size: u64,
    196     physical_size: u64,
    197     create_time: Time,
    198     last_access_time: Time,
    199     modification_time: Time,
    200     attribute: u64,
    201 
    202     pub fn getFileName(self: *const FileInfo) [*:0]const u16 {
    203         return @ptrCast(@alignCast(@as([*]const u8, @ptrCast(self)) + @sizeOf(FileInfo)));
    204     }
    205 
    206     pub const efi_file_read_only: u64 = 0x0000000000000001;
    207     pub const efi_file_hidden: u64 = 0x0000000000000002;
    208     pub const efi_file_system: u64 = 0x0000000000000004;
    209     pub const efi_file_reserved: u64 = 0x0000000000000008;
    210     pub const efi_file_directory: u64 = 0x0000000000000010;
    211     pub const efi_file_archive: u64 = 0x0000000000000020;
    212     pub const efi_file_valid_attr: u64 = 0x0000000000000037;
    213 
    214     pub const guid align(8) = Guid{
    215         .time_low = 0x09576e92,
    216         .time_mid = 0x6d3f,
    217         .time_high_and_version = 0x11d2,
    218         .clock_seq_high_and_reserved = 0x8e,
    219         .clock_seq_low = 0x39,
    220         .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
    221     };
    222 };
    223 
    224 pub const FileSystemInfo = extern struct {
    225     size: u64,
    226     read_only: bool,
    227     volume_size: u64,
    228     free_space: u64,
    229     block_size: u32,
    230     _volume_label: u16,
    231 
    232     pub fn getVolumeLabel(self: *const FileSystemInfo) [*:0]const u16 {
    233         return @as([*:0]const u16, @ptrCast(&self._volume_label));
    234     }
    235 
    236     pub const guid align(8) = Guid{
    237         .time_low = 0x09576e93,
    238         .time_mid = 0x6d3f,
    239         .time_high_and_version = 0x11d2,
    240         .clock_seq_high_and_reserved = 0x8e,
    241         .clock_seq_low = 0x39,
    242         .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
    243     };
    244 };
    245 
    246 test {
    247     _ = tables;
    248     _ = protocol;
    249 }