λ

lumine

UEFI x86_64 bootloader for linux kernel images

settings

src/boot/Chainload.zig

1089 bytes raw

const std = @import("std");
const uefi = std.os.uefi;

const Path = @import("../Config.zig").Path;
const luefi = @import("../luefi.zig");

const Chainload = @This();

img_handle: uefi.Handle,
img_loaded: *uefi.protocol.LoadedImage,

img_buffer: []align(4096) u8,
img_pages: usize,

pub fn load(path: Path) !Chainload {
    const buffer, const pages = blk: {
        const file = try luefi.fileOpen(path);
        defer file.close() catch unreachable;

        break :blk try luefi.fileReadAlloc(file);
    };
    errdefer luefi.freePages(buffer, pages);

    const handle, const loaded = try luefi.loadImage(buffer);

    return .{
        .img_buffer = buffer,
        .img_pages = pages,
        .img_handle = handle,
        .img_loaded = loaded,
    };
}

pub fn deinit(self: Chainload) void {
    luefi.unloadImage(self.img_handle);
    luefi.freePages(self.img_buffer, self.img_pages);
}

pub fn start(self: Chainload) !uefi.Status {
    const boot_svc = uefi.system_table.boot_services.?;
    const exit_data = try boot_svc.startImage(self.img_handle);
    return exit_data.code;
}