Skip to content

Commit

Permalink
Big refactor of entire PID1
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Jul 3, 2024
1 parent 9458a17 commit 9dff14d
Show file tree
Hide file tree
Showing 30 changed files with 2,479 additions and 2,721 deletions.
11 changes: 0 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ pub fn build(b: *std.Build) !void {
const with_loader = b.option(bool, "loader", "With boot loader") orelse true;
const with_tools = b.option(bool, "tools", "With tools") orelse true;

const loglevel = b.option(
u8,
"loglevel",
"Log level",
) orelse @intFromEnum(std.log.Level.debug);

const clap = b.dependency("clap", .{});

const linux_headers_translated = b.addTranslateC(.{
Expand All @@ -30,9 +24,6 @@ pub fn build(b: *std.Build) !void {
});
const linux_headers_module = linux_headers_translated.addModule("linux_headers");

const tboot_loader_options = b.addOptions();
tboot_loader_options.addOption(u8, "loglevel", loglevel);

if (with_loader) {
const tboot_loader = b.addExecutable(.{
.name = "tboot-loader",
Expand All @@ -41,7 +32,6 @@ pub fn build(b: *std.Build) !void {
.optimize = tboot_loader_optimize,
.strip = optimize != std.builtin.OptimizeMode.Debug,
});
tboot_loader.root_module.addOptions("build_options", tboot_loader_options);
tboot_loader.root_module.addAnonymousImport("test_key", .{
.root_source_file = b.path("test/keys/tboot/key.der"),
});
Expand Down Expand Up @@ -148,7 +138,6 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});

unit_tests.root_module.addOptions("build_options", tboot_loader_options);
unit_tests.root_module.addImport("linux_headers", linux_headers_module);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion kernel-configs/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ in
IMA_DEFAULT_HASH_SHA256 = yes;
IMA_KEXEC = yes;
IMA_MEASURE_ASYMMETRIC_KEYS = yes;
INOTIFY_USER = yes;
INPUT = yes;
INPUT_KEYBOARD = yes;
INPUT_MOUSE = unset;
Expand Down
5 changes: 4 additions & 1 deletion options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ in
linux.kconfig.CMDLINE = lib.kernel.freeform (
toString (
lib.optionals config.video [ "fbcon=logo-count:1" ]
++ [ (if config.debug then "debug" else "quiet") ]
++ [
"printk.devkmsg=on"
"loglevel=${if config.debug then "8" else "5"}"
]
++ map (c: "console=${c}") config.linux.consoles
)
);
Expand Down
2 changes: 0 additions & 2 deletions pkgs/tinyboot/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
debug ? false,
withLoader ? true,
withTools ? true,

Expand Down Expand Up @@ -51,7 +50,6 @@ stdenv.mkDerivation (
zigBuildFlags = [
"-Dtarget=${stdenv.hostPlatform.qemuArch}-${stdenv.hostPlatform.parsed.kernel.name}-${zigLibc}"
"-Ddynamic-linker=${stdenv.cc.bintools.dynamicLinker}"
"-Dloglevel=${toString (if debug then 3 else 2)}" # https://github.com/ziglang/zig/blob/084c2cd90f79d5e7edf76b7ddd390adb95a27f0c/lib/std/log.zig#L78
"-Dloader=${lib.boolToString withLoader}"
"-Dtools=${lib.boolToString withTools}"
"--system"
Expand Down
74 changes: 74 additions & 0 deletions src/autoboot.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const std = @import("std");
const posix = std.posix;

const BootLoader = @import("./boot/bootloader.zig");
const Console = @import("./console.zig");

const Autoboot = @This();

boot_loader: ?*BootLoader = null,

pub fn init() Autoboot {
return .{};
}

pub fn run(
self: *Autoboot,
boot_loaders: *std.ArrayList(*BootLoader),
timerfd: posix.fd_t,
) !?Console.Event {
if (self.boot_loader) |boot_loader| {
defer {
self.boot_loader = null;
}

std.log.info("autobooting {s}", .{boot_loader.device});

const entries = try boot_loader.probe();

for (entries) |entry| {
boot_loader.load(entry) catch |err| {
std.log.err(
"failed to load entry {s}: {}",
.{ entry.linux, err },
);
continue;
};
return .kexec;
}
} else {
if (boot_loaders.items.len == 0) {
return error.NoBootloaders;
}

const head = boot_loaders.orderedRemove(0);
try boot_loaders.append(head);

// If we've already tried this boot loader, this means we've gone full
// circle back to the first bootloader, so we are done.
if (head.boot_attempted) {
return error.NoBootloaders;
}

self.boot_loader = head;

const timeout = try self.boot_loader.?.timeout();
if (timeout == 0) {
return self.run(boot_loaders, timerfd);
} else {
try posix.timerfd_settime(timerfd, .{}, &.{
// oneshot
.it_interval = .{ .tv_sec = 0, .tv_nsec = 0 },
// consider settled after N seconds without any new events
.it_value = .{ .tv_sec = timeout, .tv_nsec = 0 },
}, null);

std.log.info(
"will boot in {} seconds without any user input",
.{timeout},
);
}
}

return null;
}
Loading

0 comments on commit 9dff14d

Please sign in to comment.