1
0
Fork 0

uimage: make fit optional

This commit is contained in:
Daniel Barlow 2023-10-08 22:35:30 +01:00
parent f62ad0e1d7
commit bd20f3e419
4 changed files with 49 additions and 32 deletions

View File

@ -131,9 +131,11 @@
SERIAL_OF_PLATFORM="y"; SERIAL_OF_PLATFORM="y";
}; };
}; };
boot.commandLine = [ boot = {
"console=ttyS0,115200" commandLine = [ "console=ttyS0,115200" ];
]; tftp.loadAddress = "0x4007ff28";
imageFormat = "fit";
};
filesystem = filesystem =
let inherit (pkgs.pseudofile) dir symlink; let inherit (pkgs.pseudofile) dir symlink;
in in

View File

@ -47,6 +47,10 @@ in {
default = []; default = [];
description = "Kernel command line"; description = "Kernel command line";
}; };
imageFormat = mkOption {
type = types.enum ["fit" "uimage"];
default = "uimage";
};
tftp = { tftp = {
loadAddress = mkOption { loadAddress = mkOption {
type = types.str; type = types.str;

View File

@ -78,6 +78,7 @@ in
uimage = liminix.builders.uimage { uimage = liminix.builders.uimage {
commandLine = concatStringsSep " " config.boot.commandLine; commandLine = concatStringsSep " " config.boot.commandLine;
inherit (config.hardware) loadAddress entryPoint; inherit (config.hardware) loadAddress entryPoint;
inherit (config.boot) imageFormat;
inherit kernel; inherit kernel;
inherit dtb; inherit dtb;
}; };

View File

@ -7,21 +7,27 @@
} : } :
let let
objcopy = "${stdenv.cc.bintools.targetPrefix}objcopy"; objcopy = "${stdenv.cc.bintools.targetPrefix}objcopy";
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 { in {
kernel kernel
, commandLine , commandLine
, entryPoint , entryPoint
, extraName ? "" # e.g. socFamily , extraName ? "" # e.g. socFamily
, loadAddress , loadAddress
, imageFormat
, dtb ? null , dtb ? null
} : } : stdenv.mkDerivation {
stdenv.mkDerivation {
name = "kernel.image"; name = "kernel.image";
phases = [ phases = [
"preparePhase" "preparePhase"
(if dtb != null then "dtbPhase" else ":") (if commandLine != null then assert dtb != null; "mungeDtbPhase" else ":")
"buildPhase" (if imageFormat == "fit" then "buildPhaseFIT" else "buildPhaseUImage")
"installPhase" ]; "installPhase"
];
nativeBuildInputs = [ nativeBuildInputs = [
lzma lzma
dtc dtc
@ -31,35 +37,39 @@ stdenv.mkDerivation {
preparePhase = '' preparePhase = ''
cp ${kernel} vmlinux.elf; chmod +w vmlinux.elf cp ${kernel} vmlinux.elf; chmod +w vmlinux.elf
''; '';
dtbPhase = '' mungeDtbPhase = ''
dtc -I dtb -O dts -o tmp.dts ${dtb} dtc -I dtb -O dts -o tmp.dts ${dtb}
echo '/{ chosen { bootargs = ${builtins.toJSON commandLine}; }; };' >> tmp.dts echo '/{ chosen { bootargs = ${builtins.toJSON commandLine}; }; };' >> tmp.dts
dtc -I dts -O dtb -o tmp.dtb tmp.dts dtc -I dts -O dtb -o tmp.dtb tmp.dts
''; '';
buildPhase = buildPhaseUImage = ''
let arch = test -f tmp.dtb && ${objcopy} --update-section .appended_dtb=tmp.dtb vmlinux.elf || ${objcopy} --add-section .appended_dtb=tmp.dtb vmlinux.elf
# per output of "mkimage -A list". I *think* these ${stripAndZip}
# are the same as the kernel arch convention, but 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
# maybe that's coincidence '';
if stdenv.isMips
then "mips" buildPhaseFIT = ''
else if stdenv.isAarch64 ${stripAndZip}
then "arm64" cat ${./kernel_fdt.its} > mkimage.its
else throw "unknown arch"; cat << _VARS >> mkimage.its
in '' / {
${objcopy} -O binary -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id -S vmlinux.elf vmlinux.bin images {
rm -f vmlinux.bin.lzma ; lzma -k -z vmlinux.bin kernel {
cat ${./kernel_fdt.its} > mkimage.its data = /incbin/("./vmlinux.bin.lzma");
echo '/ { images { kernel { data = /incbin/("./vmlinux.bin.lzma"); }; }; };' >> mkimage.its load = <${loadAddress}>;
echo '/ { images { kernel { load = <${loadAddress}>; }; }; };' >> mkimage.its entry = <${entryPoint}>;
echo '/ { images { kernel { entry = <${entryPoint}>; }; }; };' >> mkimage.its compression = "lzma";
echo '/ { images { kernel { compression = "lzma"; }; }; };' >> mkimage.its };
echo '/ { images { fdt-1 { data = /incbin/("./tmp.dtb"); }; }; }; ' >> mkimage.its fdt-1 { data = /incbin/("./tmp.dtb"); };
mkimage -f mkimage.its mkimage.itb };
mkimage -l mkimage.itb };
''; _VARS
mkimage -f mkimage.its kernel.uimage
mkimage -l kernel.uimage
'';
installPhase = '' installPhase = ''
cp mkimage.itb $out cp kernel.uimage $out
''; '';
} }