liminix-fork/kernel/uimage.nix

49 lines
1.2 KiB
Nix
Raw Normal View History

{
lzma
, stdenv
, ubootTools
2022-10-05 22:52:30 +02:00
, dtc
} :
let
objcopy = "${stdenv.cc.bintools.targetPrefix}objcopy";
in {
vmlinux
2022-10-05 22:52:30 +02:00
, commandLine
, entryPoint
2022-10-05 22:52:30 +02:00
, extraName ? "" # e.g. socFamily
, loadAddress
2022-10-05 22:52:30 +02:00
, dtb ? null
} :
stdenv.mkDerivation {
name = "kernel.image";
2022-10-05 22:52:30 +02:00
phases = [
"preparePhase"
(if dtb != null then "dtbPhase" else ":")
"buildPhase"
"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 ${vmlinux} vmlinux.elf; chmod +w vmlinux.elf
'';
dtbPhase = ''
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
${objcopy} --update-section .appended_dtb=tmp.dtb vmlinux.elf || ${objcopy} --add-section .appended_dtb=${dtb} vmlinux.elf
'';
buildPhase = ''
2022-10-05 22:52:30 +02:00
${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
'';
installPhase = ''
2022-10-05 22:52:30 +02:00
cp kernel.uimage $out
'';
}