2022-10-03 23:28:15 +02:00
|
|
|
{
|
|
|
|
lzma
|
|
|
|
, stdenv
|
|
|
|
, ubootTools
|
2022-10-05 22:52:30 +02:00
|
|
|
, dtc
|
2023-09-20 18:57:17 +02:00
|
|
|
, lib
|
2022-10-03 23:28:15 +02:00
|
|
|
} :
|
|
|
|
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
|
|
|
|
'';
|
2022-10-03 23:28:15 +02:00
|
|
|
in {
|
2022-10-19 18:34:22 +02:00
|
|
|
kernel
|
2022-10-05 22:52:30 +02:00
|
|
|
, commandLine
|
2024-02-17 02:16:20 +01:00
|
|
|
, commandLineDtbNode ? "bootargs"
|
2022-10-03 23:28:15 +02:00
|
|
|
, entryPoint
|
2022-10-05 22:52:30 +02:00
|
|
|
, extraName ? "" # e.g. socFamily
|
2022-10-03 23:28:15 +02:00
|
|
|
, 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 {
|
2022-10-03 23:28:15 +02:00
|
|
|
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"
|
|
|
|
];
|
2022-10-03 23:28:15 +02:00
|
|
|
nativeBuildInputs = [
|
|
|
|
lzma
|
2022-10-05 22:52:30 +02:00
|
|
|
dtc
|
2022-10-03 23:28:15 +02:00
|
|
|
stdenv.cc
|
|
|
|
ubootTools
|
|
|
|
];
|
2022-10-05 22:52:30 +02:00
|
|
|
preparePhase = ''
|
2022-10-19 18:34:22 +02:00
|
|
|
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}
|
2024-02-17 02:16:20 +01:00
|
|
|
echo '/{ chosen { ${commandLineDtbNode} = ${builtins.toJSON commandLine}; }; };' >> tmp.dts
|
2022-10-05 22:52:30 +02:00
|
|
|
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}
|
2023-11-12 21:39:06 +01:00
|
|
|
mkimage -A ${arch} -O linux -T kernel -C lzma -a 0x${lib.toHexString loadAddress} -e 0x${lib.toHexString entryPoint} -n '${lib.toUpper arch} Liminix Linux ${extraName}' -d vmlinux.bin.lzma kernel.uimage
|
2023-10-08 23:35:30 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhaseFIT = ''
|
|
|
|
${stripAndZip}
|
|
|
|
cat ${./kernel_fdt.its} > mkimage.its
|
|
|
|
cat << _VARS >> mkimage.its
|
|
|
|
/ {
|
|
|
|
images {
|
|
|
|
kernel {
|
|
|
|
data = /incbin/("./vmlinux.bin.lzma");
|
2023-11-12 19:47:31 +01:00
|
|
|
load = <0x${lib.toHexString loadAddress}>;
|
2023-11-12 19:50:47 +01:00
|
|
|
entry = <0x${lib.toHexString 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
|
|
|
|
'';
|
|
|
|
|
2022-10-03 23:28:15 +02:00
|
|
|
installPhase = ''
|
2023-10-08 23:35:30 +02:00
|
|
|
cp kernel.uimage $out
|
2022-10-03 23:28:15 +02:00
|
|
|
'';
|
|
|
|
}
|