make rootfs work with tftpbootlz

This commit is contained in:
Daniel Barlow 2023-12-17 19:39:26 +00:00
parent d1e2d525a4
commit 9ca9723c9d

View file

@ -10,34 +10,52 @@ let
o = config.system.outputs; o = config.system.outputs;
cmdline = concatStringsSep " " config.boot.commandLine; cmdline = concatStringsSep " " config.boot.commandLine;
cfg = config.boot.tftp; cfg = config.boot.tftp;
boot-scr = tftpbootlz =
pkgs.buildPackages.runCommand "boot-scr" { nativeBuildInputs = [ pkgs.pkgsBuildBuild.dtc ]; } '' pkgs.buildPackages.runCommand "tftpbootlz" {
uimageSize=$(($(stat -L -c %s ${o.zimage}) + 0x1000 &(~0xfff))) nativeBuildInputs = with pkgs.pkgsBuildBuild; [ lzma dtc ];
rootfsStart=0x$(printf %x $((${toString cfg.loadAddress} + 0x100000 + $uimageSize &(~0xfffff) ))) } ''
rootfsBytes=$(($(stat -L -c %s ${o.rootfs}) + 0x100000 &(~0xfffff))) binsize() { local s=$(stat -L -c %s $1); echo $(($s + 0x1000 &(~0xfff))); }
rootfsBytes=$(($rootfsBytes + ${toString cfg.freeSpaceBytes} )) binsize64k() { local s=$(stat -L -c %s $1); echo $(($s + 0x10000 &(~0xffff))); }
rootfsMb=$(($rootfsBytes >> 20)) hex() { printf "0x%x" $1; }
cmd="mtdparts=phram0:''${rootfsMb}M(rootfs) phram.phram=phram0,''${rootfsStart},''${rootfsBytes},${toString config.hardware.flash.eraseBlockSize} root=/dev/mtdblock0";
dtbStart=$(printf %x $((${toString cfg.loadAddress} + $rootfsBytes + 0x100000 + $uimageSize ))) kernelStart=${toString cfg.loadAddress}
kernelSize=$(binsize ${o.zimage})
mkdir $out mkdir -p $out
cat ${o.dtb} > $out/dtb cat ${o.dtb} > $out/dtb
fdtput -p -t lx $out/dtb /reserved-memory '#address-cells' 1
fdtput -t lx $out/dtb /reserved-memory '#size-cells' 1
fdtput $out/dtb /reserved-memory ranges
fdtput -p -t s $out/dtb /reserved-memory/phram-rootfs compatible phram fdtput -p -t s $out/dtb /reserved-memory/phram-rootfs compatible phram
fdtput -p -t lx $out/dtb /reserved-memory/phram-rootfs reg 0 $rootfsStart 0 $(printf %x $rootfsBytes) # can't calculate the actual address here until we know how
# big the dtb will be
fdtput -t lx $out/dtb /reserved-memory/phram-rootfs reg 0xdead 0xcafe
dtbStart=$(($kernelStart + $kernelSize))
dtbSize=$(binsize $out/dtb)
rootfsOrigSize=$(binsize64k ${o.rootfs})
lzma -z9cv ${o.rootfs} > $out/rootfs.lz
dtbBytes=$(($(stat -L -c %s $out/dtb) + 0x1000 &(~0xfff))) rootfsLzSize=$(binsize $out/rootfs.lz)
rootfsLzStart=$(($dtbStart + $dtbSize))
rootfsOrigStart=$(($rootfsLzStart + $rootfsLzSize))
fdtput -t lx $out/dtb /reserved-memory/phram-rootfs reg $(printf "%x" $rootfsOrigStart) $(printf "%x" $rootfsOrigSize)
# dtc -I dtb -O dts -o /dev/stdout $out/dtb ; exit 1
cat > $out/script << EOF cmd="mtdparts=phram0:''${rootfsOrigSize}(rootfs) phram.phram=phram0,''${rootfsOrigStart},''${rootfsOrigSize},${toString config.hardware.flash.eraseBlockSize} root=/dev/mtdblock0";
(cd $out;
ln -s ${o.zimage} zImage
ln -s ${o.manifest} manifest
ln -s ${o.kernel.headers} build)
cat > $out/boot.scr << EOF
setenv serverip ${cfg.serverip} setenv serverip ${cfg.serverip}
setenv ipaddr ${cfg.ipaddr} setenv ipaddr ${cfg.ipaddr}
setenv bootargs 'liminix ${cmdline} $cmd' setenv bootargs "liminix ${cmdline} $cmd"
tftpboot 0x${lib.toHexString cfg.loadAddress} result/zImage; tftpboot 0x$(printf %x $rootfsStart) result/rootfs ; tftpboot 0x$dtbStart result/dtb tftpboot $(hex $kernelStart) result/zImage; tftpboot $(hex $dtbStart) result/dtb ; tftpboot $(hex $rootfsLzStart) result/rootfs.lz
setexpr bootaddr 0x$dtbStart + \$filesize lzmadec $(hex $rootfsLzStart) $(hex $rootfsOrigStart)
# lzmadec 0x${lib.toHexString cfg.loadAddress} \$bootaddr bootz $(hex $kernelStart) - $(hex $dtbStart)
# bootz \$bootaddr - 0x$dtbStart
bootz 0x${lib.toHexString cfg.loadAddress} - 0x$dtbStart
EOF EOF
''; '';
in { in {
imports = [ ../ramdisk.nix ]; imports = [ ../ramdisk.nix ];
@ -53,35 +71,20 @@ in {
********** **********
This is a variant of the tftpboot output intended for the This is a variant of the tftpboot output intended for the
Turris Omnia. It builds a uimage containing an uncompressed Turris Omnia. It uses a zImage instead of a uimage, as a
kernel, then compresses the resulting image to be decompressed workaround for an awkwardly low CONFIG_SYS_BOOTM_LEN setting
using the u-boot lzmadec command. This is a workaround for an in the U-Boot build for the device which means it won't boot
awkwardly low CONFIG_SYS_BOOTM_LEN setting in the U-Boot build uimages unless they're teeny tiny.
for the device, which means that the regular tftpboot output
would only work for very small kernels. As a bonus, it also uses lzma compression on the rootfs,
which reduces a 20MB ext4 image to around 4MB
''; '';
}; };
}; };
config = { config = {
boot.ramdisk.enable = true; boot.ramdisk.enable = true;
system.outputs = { system.outputs = {
tftpbootlz = inherit tftpbootlz;
let
o = config.system.outputs; in
pkgs.runCommand "tftpboot" {
depsBuildBuild = [ pkgs.pkgsBuildBuild.lzma ];
} ''
mkdir $out
cd $out
ln -s ${o.rootfs} rootfs
ln -s ${o.zimage} zImage
ln -s ${o.manifest} manifest
ln -s ${o.kernel.headers} build
# lzma -c -z -9 < {uimage} > uimage.lz
ln -s ${boot-scr}/dtb dtb
ln -s ${boot-scr}/script boot.scr
'';
}; };
}; };
} }