lumine
UEFI x86_64 bootloader for linux kernel images
src/boot/Chainload.zig
1086 bytes raw
1const std = @import("std");
2const uefi = std.os.uefi;
3
4const Path = @import("../Config.zig").Path;
5const luefi = @import("../luefi.zig");
6
7const Chainload = @This();
8
9img_handle: uefi.Handle,
10img_loaded: *uefi.protocol.LoadedImage,
11
12img_buffer: []align(4096) u8,
13img_pages: usize,
14
15pub fn load(path: Path) !Chainload {
16 const buffer, const pages = blk: {
17 const file = try luefi.fileOpen(path);
18 defer file.close() catch unreachable;
19
20 break :blk try luefi.fileReadAlloc(file);
21 };
22 defer luefi.freePages(buffer, pages);
23
24 const handle, const loaded = try luefi.loadImage(buffer);
25
26 return .{
27 .img_buffer = buffer,
28 .img_pages = pages,
29 .img_handle = handle,
30 .img_loaded = loaded,
31 };
32}
33
34pub fn deinit(self: Chainload) void {
35 luefi.unloadImage(self.img_handle);
36 luefi.freePages(self.img_buffer, self.img_pages);
37}
38
39pub fn start(self: Chainload) !uefi.Status {
40 const boot_svc = uefi.system_table.boot_services.?;
41 const exit_data = try boot_svc.startImage(self.img_handle);
42 return exit_data.code;
43}