Skip to content

Commit

Permalink
Fix cross-compilation for tboot-sign
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Jun 5, 2024
1 parent 43e77e0 commit c3eab38
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn build(b: *std.Build) !void {
const tboot_sign = b.addExecutable(.{
.name = "tboot-sign",
.root_source_file = .{ .path = "src/tboot-sign.zig" },
.target = b.host,
.target = target,
.optimize = optimize,
.strip = optimize != std.builtin.OptimizeMode.Debug,
});
Expand Down
6 changes: 5 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
corebootSrc = inputs.coreboot.outPath;
version = "24.05";
};
tinyboot = prev.callPackage ./pkgs/tinyboot.nix { zigSrc = inputs.zig.outPath; };
# TODO(jared): use pkgsStatic for now since zig's cross-compilation dynamic linking support seems to be broken
tinyboot = prev.pkgsStatic.callPackage ./pkgs/tinyboot.nix { zigSrc = inputs.zig.outPath; };
armTrustedFirmwareMT8183 = prev.callPackage ./pkgs/arm-trusted-firmware-cros.nix {
platform = "mt8183";
};
Expand Down Expand Up @@ -65,6 +66,9 @@
qemu
zon2nix
];
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 module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ in
boot.loader.external.enable = true;
boot.loader.external.installHook = toString [
(lib.getExe' pkgs.tinyboot "tboot-nixos-install")
"efi-sys-mount-point=${config.boot.loader.efi.efiSysMountPoint}"
"private-key=${cfg.verifiedBoot.tbootPrivateKey}"
"public-key=${cfg.verifiedBoot.tbootPublicCertificate}"
"timeout=${toString config.boot.loader.timeout}"
"max-tries=${toString cfg.maxFailedBootAttempts}"
"--esp-mnt=${config.boot.loader.efi.efiSysMountPoint}"
"--private-key=${cfg.verifiedBoot.tbootPrivateKey}"
"--public-key=${cfg.verifiedBoot.tbootPublicCertificate}"
"--timeout=${toString config.boot.loader.timeout}"
"--max-tries=${toString cfg.maxFailedBootAttempts}"
];
systemd.additionalUpstreamSystemUnits = [ "boot-complete.target" ];
systemd.generators.tboot-bless-boot-generator = lib.getExe' pkgs.tinyboot "tboot-bless-boot-generator";
Expand Down
7 changes: 4 additions & 3 deletions pkgs/tinyboot.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ stdenv.mkDerivation (
strictDeps = true;

nativeBuildInputs = [
(pkgsBuildBuild.zig_0_12.overrideAttrs (_: {
(pkgsBuildBuild.zig_0_12.overrideAttrs (old: {
src = zigSrc;
patches = (old.patches or [ ]) ++ [ ./zig-pkg-config-cross.patch ];
})).hook
xz
pkg-config
Expand All @@ -56,13 +57,13 @@ stdenv.mkDerivation (
"${finalAttrs.deps}"
];

# TODO(jared): this is a bug in nixpkgs in the zig hook
# TODO(jared): The checkPhase should already include the zigBuildFlags,
# probably a nixpkgs bug.
zigCheckFlags = finalAttrs.zigBuildFlags;

# TODO(jared): make embedFile work better with the test key
preConfigure = ''
ln -sf ${../test/keys/tboot/key.der} src/test_key
export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
'';

postInstall = ''
Expand Down
25 changes: 25 additions & 0 deletions pkgs/zig-pkg-config-cross.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
index 314db765cb..c4b4dbacc4 100644
--- a/lib/std/Build/Step/Compile.zig
+++ b/lib/std/Build/Step/Compile.zig
@@ -703,8 +703,9 @@ fn runPkgConfig(self: *Compile, lib_name: []const u8) !PkgConfigResult {
};

var code: u8 = undefined;
+ const pkg_config_exe = b.graph.env_map.get("PKG_CONFIG") orelse "pkg-config";
const stdout = if (b.runAllowFail(&[_][]const u8{
- "pkg-config",
+ pkg_config_exe,
pkg_name,
"--cflags",
"--libs",
@@ -1824,7 +1825,8 @@ pub fn doAtomicSymLinks(
}

fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || RunError)![]const PkgConfigPkg {
- const stdout = try self.runAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
+ const pkg_config_exe = self.graph.env_map.get("PKG_CONFIG") orelse "pkg-config";
+ const stdout = try self.runAllowFail(&[_][]const u8{ pkg_config_exe, "--list-all" }, out_code, .Ignore);
var list = ArrayList(PkgConfigPkg).init(self.allocator);
errdefer list.deinit();
var line_it = mem.tokenizeAny(u8, stdout, "\r\n");

0 comments on commit c3eab38

Please sign in to comment.