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