From 4f29bdd3ed9be1b16627522d8b247b9baf185566 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Wed, 20 Sep 2023 17:57:17 +0100 Subject: [PATCH] detect arch in kernel and uimage also move kernel builder to pkgs/ FIXME we need to straighten out the mess in calling dtb.nix/uimage.nix --- modules/kernel.nix | 2 +- modules/outputs.nix | 4 ++-- pkgs/default.nix | 2 ++ pkgs/kernel/default.nix | 8 +++++++- {kernel => pkgs/kernel}/dtb.nix | 0 {kernel => pkgs/kernel}/uimage.nix | 21 ++++++++++++++++----- 6 files changed, 28 insertions(+), 9 deletions(-) rename {kernel => pkgs/kernel}/dtb.nix (100%) rename {kernel => pkgs/kernel}/uimage.nix (55%) diff --git a/modules/kernel.nix b/modules/kernel.nix index d73665e..ff2d9de 100644 --- a/modules/kernel.nix +++ b/modules/kernel.nix @@ -24,7 +24,7 @@ in { extraPatchPhase = mkOption { default = "true"; type = types.lines; - } ; + }; config = mkOption { description = '' Kernel config options, as listed in Kconfig* files in the diff --git a/modules/outputs.nix b/modules/outputs.nix index b0edf17..02c3cd1 100644 --- a/modules/outputs.nix +++ b/modules/outputs.nix @@ -62,14 +62,14 @@ in kernel = liminix.builders.kernel.override { inherit (config.kernel) config src extraPatchPhase; }; - dtb = (callPackage ../kernel/dtb.nix {}) { + dtb = liminix.builders.dtb { inherit (config.boot) commandLine; dts = config.hardware.dts.src; includes = config.hardware.dts.includes ++ [ "${kernel.headers}/include" ]; }; - uimage = (callPackage ../kernel/uimage.nix {}) { + uimage = liminix.builders.uimage { commandLine = concatStringsSep " " config.boot.commandLine; inherit (config.hardware) loadAddress entryPoint; inherit kernel; diff --git a/pkgs/default.nix b/pkgs/default.nix index e1fa46c..a7233c3 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -16,6 +16,8 @@ in { networking = callPackage ./liminix-tools/networking {}; builders = { squashfs = callPackage ./liminix-tools/builders/squashfs.nix {}; + dtb = callPackage ./kernel/dtb.nix {}; + uimage = callPackage ./kernel/uimage.nix {}; kernel = callPackage ./kernel {}; }; callService = path : parameters : diff --git a/pkgs/kernel/default.nix b/pkgs/kernel/default.nix index b2b8b9f..d6b8df4 100644 --- a/pkgs/kernel/default.nix +++ b/pkgs/kernel/default.nix @@ -11,6 +11,12 @@ let writeConfig = import ./write-kconfig.nix { inherit lib writeText; }; kconfigFile = writeConfig "kconfig" config; + arch = if stdenv.isMips + then "mips" + else if stdenv.isAarch64 + then "arm64" + else throw "unknown arch"; + inherit lib; in stdenv.mkDerivation rec { name = "kernel"; @@ -28,7 +34,7 @@ stdenv.mkDerivation rec { "-I${openssl.dev}/include -L${openssl.out}/lib -L${ncurses.out}/lib"; PKG_CONFIG_PATH = "./pkgconfig"; CROSS_COMPILE = stdenv.cc.bintools.targetPrefix; - ARCH = "mips"; # kernel uses "mips" here for both mips and mipsel + ARCH = arch; KBUILD_BUILD_HOST = "liminix.builder"; dontStrip = true; diff --git a/kernel/dtb.nix b/pkgs/kernel/dtb.nix similarity index 100% rename from kernel/dtb.nix rename to pkgs/kernel/dtb.nix diff --git a/kernel/uimage.nix b/pkgs/kernel/uimage.nix similarity index 55% rename from kernel/uimage.nix rename to pkgs/kernel/uimage.nix index c92faa8..d0882f8 100644 --- a/kernel/uimage.nix +++ b/pkgs/kernel/uimage.nix @@ -3,6 +3,7 @@ , stdenv , ubootTools , dtc +, lib } : let objcopy = "${stdenv.cc.bintools.targetPrefix}objcopy"; @@ -37,11 +38,21 @@ stdenv.mkDerivation { ${objcopy} --update-section .appended_dtb=tmp.dtb vmlinux.elf || ${objcopy} --add-section .appended_dtb=${dtb} vmlinux.elf ''; - buildPhase = '' - ${objcopy} -O binary -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id -S vmlinux.elf vmlinux.bin - rm -f vmlinux.bin.lzma ; lzma -k -z vmlinux.bin - mkimage -A mips -O linux -T kernel -C lzma -a ${loadAddress} -e ${entryPoint} -n 'MIPS Liminix Linux ${extraName}' -d vmlinux.bin.lzma kernel.uimage - ''; + buildPhase = + let arch = + # per output of "mkimage -A list". I *think* these + # are the same as the kernel arch convention, but + # maybe that's coincidence + if stdenv.isMips + then "mips" + else if stdenv.isAarch64 + then "arm64" + else throw "unknown arch"; + in '' + ${objcopy} -O binary -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id -S vmlinux.elf vmlinux.bin + rm -f vmlinux.bin.lzma ; lzma -k -z vmlinux.bin + mkimage -A ${arch} -O linux -T kernel -C lzma -a ${loadAddress} -e ${entryPoint} -n '${lib.toUpper arch} Liminix Linux ${extraName}' -d vmlinux.bin.lzma kernel.uimage + ''; installPhase = '' cp kernel.uimage $out '';