Skip to content

Commit

Permalink
Implement xz compression of initrd in zig
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Feb 12, 2025
1 parent 75ef9cf commit cb6a0cc
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 421 deletions.
28 changes: 16 additions & 12 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn build(b: *std.Build) !void {
std.builtin.OptimizeMode.ReleaseSmall;

const with_loader = b.option(bool, "loader", "With boot loader") orelse true;
const with_tools = b.option(bool, "tools", "With tools") orelse true;
const with_tools = b.option(bool, "tools", "With tools") orelse false;
const firmware_directory = b.option(
[]const u8,
"firmware-directory",
Expand Down Expand Up @@ -61,20 +61,14 @@ pub fn build(b: *std.Build) !void {
});
tboot_loader.root_module.addImport("linux_headers", linux_headers_module);

const cpio_tool = b.addExecutable(.{
.name = "cpio",
.target = b.graph.host,
.root_source_file = b.path("src/cpio/main.zig"),
});
cpio_tool.root_module.addImport("clap", clap.module("clap"));
var run_tboot_initrd = b.addSystemCommand(&.{"tboot-initrd"});

const run_cpio_tool = b.addRunArtifact(cpio_tool);
// TODO(jared): Would be nicer to have generic
// --file=tboot_loader:/init CLI interface, but don't know how to
// obtain path and string format it into that form. Further, would
// be nicer to not shell-out to a separate tool at all and just do
// the CPIO generation in here.
run_cpio_tool.addPrefixedFileArg("-i", tboot_loader.getEmittedBin());
run_tboot_initrd.addPrefixedFileArg("-i", tboot_loader.getEmittedBin());

if (firmware_directory) |directory| {
const directory_ = b.addWriteFiles().addCopyDirectory(
Expand All @@ -83,15 +77,15 @@ pub fn build(b: *std.Build) !void {
.{},
);

run_cpio_tool.addPrefixedDirectoryArg("-d", directory_);
run_tboot_initrd.addPrefixedDirectoryArg("-d", directory_);
}

const cpio_output_file = run_cpio_tool.addPrefixedOutputFileArg(
const cpio_output_file = run_tboot_initrd.addPrefixedOutputFileArg(
"-o",
"tboot-loader.cpio",
);

run_cpio_tool.expectExitCode(0);
run_tboot_initrd.expectExitCode(0);

const cpio_archive = b.addInstallFile(
cpio_output_file,
Expand Down Expand Up @@ -127,6 +121,16 @@ pub fn build(b: *std.Build) !void {
}

if (with_tools) {
const tboot_initrd_tool = b.addExecutable(.{
.name = "tboot-initrd",
.target = target,
.root_source_file = b.path("src/tboot-initrd.zig"),
});
tboot_initrd_tool.linkLibC();
tboot_initrd_tool.linkSystemLibrary("liblzma");
tboot_initrd_tool.root_module.addImport("clap", clap.module("clap"));
b.installArtifact(tboot_initrd_tool);

const tboot_bless_boot = b.addExecutable(.{
.name = "tboot-bless-boot",
.root_source_file = b.path("src/tboot-bless-boot.zig"),
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

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

20 changes: 7 additions & 13 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
(
{
zigForTinyboot = inputs.zig-overlay.packages.${final.stdenv.buildPlatform.system}.master;
tinybootLoader = final.pkgsStatic.callPackage ./pkgs/tinyboot {
withLoader = true;
withTools = false;
};
tinybootTools = final.pkgsStatic.callPackage ./pkgs/tinyboot {
withLoader = false;
withTools = true;
};
tinybootLoader = final.pkgsStatic.callPackage ./pkgs/tinyboot {
withLoader = true;
withTools = false;
tinybootTools = final.buildPackages.pkgsStatic.tinybootTools;
};
armTrustedFirmwareMT8183 = final.callPackage ./pkgs/arm-trusted-firmware-cros {
platform = "mt8183";
};
Expand Down Expand Up @@ -63,18 +64,11 @@
);
devShells = inputs.nixpkgs.lib.mapAttrs (_: pkgs: {
default = pkgs.mkShell {
inputsFrom = [
pkgs.tinybootLoader
pkgs.tinybootTools
];
inputsFrom = [ pkgs.tinybootLoader ];
packages = [
pkgs.swtpm
pkgs.qemu
pkgs.swtpm
];
# https://github.com/NixOS/nixpkgs/issues/270415
shellHook = ''
unset ZIG_GLOBAL_CACHE_DIR
'';
env.TINYBOOT_KERNEL = ''${pkgs."tinyboot-qemu-${pkgs.stdenv.hostPlatform.qemuArch}".linux}/kernel'';
};
}) inputs.self.legacyPackages;
Expand Down
10 changes: 5 additions & 5 deletions pkgs/flashrom-cros/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
sphinx,
bash-completion,
}:
stdenv.mkDerivation (finalAttrs: {
stdenv.mkDerivation {
pname = "flashrom-cros";
version = "1.5.0-devel";
src = fetchgit {
url = "https://chromium.googlesource.com/chromiumos/third_party/flashrom";
branchName = "release-R130-16033.B";
rev = "c1ab7468d28d164a30d598eb3e42a5febaf73bbc";
hash = "sha256-0bUEsFOhwWahjkk+m+PmjOVD2dOk1S2dZTfpENRwgzg=";
branchName = "release-R134-16181.B";
rev = "f9e2b906229ec01bd0bb3321e9c424c6796d5408";
hash = "sha256-4Loiu040yyyyo+/deZGp5mmO0kljhXNZMvRbZ0c4mC0=";
};
patches = [ ./power-management.patch ];
outputs = [
Expand All @@ -44,4 +44,4 @@ stdenv.mkDerivation (finalAttrs: {
libjaylink
];
meta.mainProgram = "flashrom";
})
}
31 changes: 15 additions & 16 deletions pkgs/tinyboot/default.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
firmwareDirectory ? null,
tinybootTools,
withLoader ? true,
withTools ? true,
firmwareDirectory ? null,
zigForTinyboot,

callPackage,
lib,
openssl,
pkg-config,
stdenv,
xz,
zigForTinyboot,
}:

let
Expand All @@ -23,8 +24,9 @@ in
# Using zig-overlay (without the patches from nixpkgs) does not work well when
# doing sandboxed builds because of the following issue: https://github.com/ziglang/zig/issues/15898
assert stdenv.hostPlatform.isStatic && stdenv.hostPlatform.libc == "musl";
assert withTools != withLoader;
stdenv.mkDerivation {
pname = "tinyboot";
pname = "tinyboot-${if withTools then "tools" else "loader"}";
version = "0.1.0";

src = lib.fileset.toSource {
Expand All @@ -40,11 +42,14 @@ stdenv.mkDerivation {
strictDeps = true;

nativeBuildInputs = [
pkg-config
zigForTinyboot
] ++ lib.optional (!withTools) tinybootTools;

buildInputs = lib.optionals withTools [
openssl
xz
pkg-config
];
buildInputs = lib.optionals withTools [ openssl ];

zigBuildFlags =
[
Expand All @@ -69,17 +74,11 @@ stdenv.mkDerivation {
runHook postConfigure
'';

buildPhase =
''
runHook preBuild
zig build install --prefix $out $zigBuildFlags
''
+ lib.optionalString withLoader ''
xz --threads=$NIX_BUILD_CORES --check=crc32 --lzma2=dict=512KiB $out/tboot-loader.cpio
''
+ ''
runHook postBuild
'';
buildPhase = ''
runHook preBuild
zig build install --prefix $out $zigBuildFlags
runHook postBuild
'';

checkPhase = ''
runHook preCheck
Expand Down
Loading

0 comments on commit cb6a0cc

Please sign in to comment.