liminix/pkgs/kernel/uimage.nix

80 lines
2.1 KiB
Nix
Raw Normal View History

{
lzma
, stdenv
, ubootTools
2022-10-05 22:52:30 +02:00
, dtc
, lib
} :
let
objcopy = "${stdenv.cc.bintools.targetPrefix}objcopy";
2023-10-08 23:35:30 +02:00
arch = stdenv.hostPlatform.linuxArch;
stripAndZip = ''
${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
'';
in {
kernel
2022-10-05 22:52:30 +02:00
, commandLine
, entryPoint
2022-10-05 22:52:30 +02:00
, extraName ? "" # e.g. socFamily
, loadAddress
2023-10-08 23:35:30 +02:00
, imageFormat
2022-10-05 22:52:30 +02:00
, dtb ? null
2023-10-08 23:35:30 +02:00
} : stdenv.mkDerivation {
name = "kernel.image";
2022-10-05 22:52:30 +02:00
phases = [
"preparePhase"
2023-10-08 23:35:30 +02:00
(if commandLine != null then assert dtb != null; "mungeDtbPhase" else ":")
(if imageFormat == "fit" then "buildPhaseFIT" else "buildPhaseUImage")
"installPhase"
];
nativeBuildInputs = [
lzma
2022-10-05 22:52:30 +02:00
dtc
stdenv.cc
ubootTools
];
2022-10-05 22:52:30 +02:00
preparePhase = ''
cp ${kernel} vmlinux.elf; chmod +w vmlinux.elf
2022-10-05 22:52:30 +02:00
'';
2023-10-08 23:35:30 +02:00
mungeDtbPhase = ''
2022-10-05 22:52:30 +02:00
dtc -I dtb -O dts -o tmp.dts ${dtb}
echo '/{ chosen { bootargs = ${builtins.toJSON commandLine}; }; };' >> tmp.dts
dtc -I dts -O dtb -o tmp.dtb tmp.dts
'';
2023-10-08 23:35:30 +02:00
buildPhaseUImage = ''
test -f tmp.dtb && ${objcopy} --update-section .appended_dtb=tmp.dtb vmlinux.elf || ${objcopy} --add-section .appended_dtb=tmp.dtb vmlinux.elf
${stripAndZip}
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
'';
buildPhaseFIT = ''
${stripAndZip}
cat ${./kernel_fdt.its} > mkimage.its
cat << _VARS >> mkimage.its
/ {
images {
kernel {
data = /incbin/("./vmlinux.bin.lzma");
load = <${loadAddress}>;
entry = <${entryPoint}>;
2023-10-29 12:50:23 +01:00
arch = "${arch}";
2023-10-08 23:35:30 +02:00
compression = "lzma";
};
2023-10-29 12:50:23 +01:00
fdt-1 {
data = /incbin/("./tmp.dtb");
arch = "${arch}";
};
2023-10-08 23:35:30 +02:00
};
};
_VARS
mkimage -f mkimage.its kernel.uimage
mkimage -l kernel.uimage
'';
installPhase = ''
2023-10-08 23:35:30 +02:00
cp kernel.uimage $out
'';
}