lumine
UEFI x86_64 bootloader for linux kernel images
src/main.zig
2802 bytes raw
1const std = @import("std");
2const uefi = std.os.uefi;
3const log = std.log;
4const Io = std.Io;
5
6const Chainload = @import("./boot/Chainload.zig");
7const linux = @import("./boot/linux.zig");
8const Config = @import("./Config.zig");
9const Console = @import("./Console.zig");
10const luefi = @import("./luefi.zig");
11
12// TODO: relative to EFI
13const config_paths: []const Config.Path = &.{
14 .{ .boot = "/lumine/config.zon" },
15 .{ .boot = "/lumine/lumine.zon" },
16 .{ .boot = "/lumine.zon" },
17};
18
19pub fn bmain() !uefi.Status {
20 const allocator = uefi.pool_allocator;
21
22 const config: Config = try config: {
23 var file = reader: {
24 for (config_paths) |path| {
25 break :reader luefi.fileOpen(path) catch continue;
26 }
27
28 log.err("failed to detect config, tried paths:", .{});
29 for (config_paths) |path| log.err("\t{f}", .{path});
30 return error.NoConfig;
31 };
32 defer file.close() catch unreachable;
33
34 const source = try allocator.allocSentinel(u8, try luefi.fileSize(file), 0);
35 defer allocator.free(source);
36
37 try luefi.fileReadSlice(file, source);
38
39 break :config std.zon.parse.fromSliceAlloc(Config, allocator, source, null, .{});
40 };
41 defer std.zon.parse.free(allocator, config);
42
43 switch (config.boot) {
44 .chainload => |efi_path| {
45 log.info("loading efi {f}", .{efi_path});
46 const chainload: Chainload = try .load(efi_path);
47 defer chainload.deinit();
48
49 return chainload.start();
50 },
51 .linux => |linux_conf| {
52 const kernel: linux.Kernel = try .load(linux_conf.kernel_path);
53 defer kernel.deinit();
54
55 const initrd: *linux.Initrd = try .load(linux_conf.initrd_paths);
56 defer initrd.deinit();
57
58 return kernel.start(linux_conf.kernel_cmdline);
59 },
60 }
61}
62
63pub fn main() uefi.Status {
64 return bmain() catch |err| {
65 log.err("unrecoverable error in bmain: {}", .{err});
66 return .aborted;
67 };
68}
69
70pub fn logFn(
71 comptime level: log.Level,
72 comptime scope: @EnumLiteral(),
73 comptime format: []const u8,
74 args: anytype,
75) void {
76 // stderr should be equivalent to stdout?
77 // but i dont see logs on my hardware if i use stderr
78 var stderr_buf: [512]u8 = undefined;
79 var stderr: Console = .stdout(&stderr_buf);
80 stderr.interface.print("{t}({}): ", .{ level, scope }) catch return;
81 stderr.interface.print(format ++ "\r\n", args) catch return;
82 stderr.interface.flush() catch return;
83}
84
85pub fn panicFn(msg: []const u8, _: ?usize) noreturn {
86 logFn(.err, .panic, "{s}", .{msg});
87 @trap();
88}
89
90pub const panic = std.debug.FullPanic(panicFn);
91pub const std_options: std.Options = .{ .logFn = logFn };