lumine
UEFI x86_64 bootloader for linux kernel images
src/luefi.zig
4730 bytes raw
1//! lumine uefi abstractions
2const std = @import("std");
3const uefi = std.os.uefi;
4
5const Path = @import("./Config.zig").Path;
6
7pub fn posixPathToUefi(path: []u16) void {
8 for (0..path.len) |i|
9 switch (path[i]) {
10 '/' => path[i] = '\\',
11 else => {},
12 };
13}
14
15pub fn fileOpenBoot(path: []const u8) !*uefi.protocol.File {
16 const boot_svc = uefi.system_table.boot_services.?;
17 const allocator = uefi.pool_allocator;
18
19 const loaded_img = try boot_svc.openProtocol(uefi.protocol.LoadedImage, uefi.handle, .{
20 .get_protocol = .{ .agent = uefi.handle },
21 }) orelse unreachable;
22
23 const device_handle = loaded_img.device_handle orelse unreachable;
24 const simple_fs = try boot_svc.openProtocol(uefi.protocol.SimpleFileSystem, device_handle, .{
25 .get_protocol = .{ .agent = uefi.handle },
26 }) orelse unreachable;
27
28 const root_fs = try simple_fs.openVolume();
29 defer root_fs.close() catch unreachable;
30
31 // is allocating a good idea?
32 const utf16_path = try std.unicode.utf8ToUtf16LeAllocZ(allocator, path);
33 defer allocator.free(utf16_path);
34
35 posixPathToUefi(utf16_path);
36
37 return root_fs.open(utf16_path, .read, .{});
38}
39
40pub fn fileOpen(path: Path) !*uefi.protocol.File {
41 return switch (path) {
42 .boot => |boot_path| fileOpenBoot(boot_path),
43 };
44}
45
46pub fn fileSize(file: *uefi.protocol.File) !usize {
47 const allocator = uefi.pool_allocator;
48
49 const info_size = try file.getInfoSize(.file);
50 const info_buf = try allocator.alignedAlloc(u8, .@"8", info_size);
51 defer allocator.free(info_buf);
52
53 const info = try file.getInfo(.file, info_buf);
54 return info.file_size;
55}
56
57pub fn fileReadSlice(file: *uefi.protocol.File, slice: []u8) !void {
58 var read: usize = 0;
59
60 while (read < slice.len) {
61 const chunk = try file.read(slice[read..]);
62 if (chunk == 0) return error.ReadShort;
63 read += chunk;
64 }
65}
66
67pub fn fileReadAlloc(file: *uefi.protocol.File) !struct { []align(4096) u8, usize } {
68 const boot_svc = uefi.system_table.boot_services.?;
69
70 const size = try fileSize(file);
71 const page_count = try std.math.divCeil(usize, size, 4096);
72
73 const pages = try boot_svc.allocatePages(.any, .loader_data, page_count);
74 errdefer boot_svc.freePages(pages) catch unreachable;
75
76 const slice_ptr: [*]align(4096) u8 = @ptrCast(pages);
77 const slice = slice_ptr[0..size];
78 try fileReadSlice(file, slice);
79
80 return .{ slice, page_count };
81}
82
83pub fn freePages(buf: []align(4096) u8, page_count: usize) void {
84 const boot_svc = uefi.system_table.boot_services.?;
85
86 const slice_ptr: [*]align(4096) [4096]u8 = @ptrCast(buf);
87 boot_svc.freePages(slice_ptr[0..page_count]) catch unreachable;
88}
89
90pub fn loadImage(buf: []u8) !struct { uefi.Handle, *uefi.protocol.LoadedImage } {
91 const boot_svc = uefi.system_table.boot_services.?;
92
93 const handle = try boot_svc.loadImage(false, uefi.handle, .{ .buffer = buf });
94 errdefer unloadImage(handle);
95
96 const loaded_img = try boot_svc.openProtocol(uefi.protocol.LoadedImage, handle, .{
97 .get_protocol = .{ .agent = uefi.handle },
98 }) orelse unreachable;
99
100 return .{ handle, loaded_img };
101}
102
103pub fn unloadImage(handle: uefi.Handle) void {
104 const boot_svc = uefi.system_table.boot_services.?;
105 _ = boot_svc.unloadImage(handle) catch unreachable;
106}
107
108// https://codeberg.org/ziglang/zig/pulls/31934
109pub fn installProtocols(interfaces: anytype) !void {
110 const fields = @typeInfo(@TypeOf(interfaces)).@"struct".fields;
111 comptime var tuple_types: [fields.len * 2 + 1]type = undefined;
112 inline for (0..fields.len) |i| {
113 tuple_types[i * 2 + 1] = *const uefi.Guid;
114 tuple_types[i * 2 + 2] = *const anyopaque;
115 }
116 tuple_types[0] = *?uefi.Handle;
117
118 var args_tuple: @Tuple(tuple_types[0..]) = undefined;
119 inline for (interfaces, 0..) |iface, i| {
120 const child = @typeInfo(@TypeOf(iface)).pointer.child;
121 args_tuple[i * 2 + 1] = &@field(child, "guid");
122 args_tuple[i * 2 + 2] = @ptrCast(iface);
123 }
124 var handle: ?uefi.Handle = null;
125 args_tuple[0] = &handle;
126
127 const boot_svc = uefi.system_table.boot_services.?;
128 const InstallFn = *const fn (handle: *?uefi.Handle, ...) callconv(uefi.cc) uefi.Status;
129 const installFn: InstallFn = @ptrCast(boot_svc._installMultipleProtocolInterfaces);
130
131 const status = @call(.auto, installFn, args_tuple);
132 return switch (status) {
133 .success => return,
134 .already_started => return error.AlreadyStarted,
135 .out_of_resources => return error.OutOfResources,
136 .invalid_parameter => return error.InvalidParameter,
137 else => return uefi.unexpectedStatus(status),
138 };
139}