Compare commits

...

9 Commits

Author SHA1 Message Date
Daniel Barlow 0a804162d5 turris omnia: switch to regular tftpboot output
now it does zimage and rootfs compression
2023-12-22 23:35:19 +00:00
Daniel Barlow 13288e4eeb tftproot: put command line in dtb 2023-12-22 23:34:08 +00:00
Daniel Barlow 98a075763e tftpboot: support compressed root 2023-12-22 23:20:35 +00:00
Daniel Barlow e7e4b962d2 uninit var 2023-12-22 23:19:28 +00:00
Daniel Barlow 0fe9d95598 tftpboot: move things around in memory
new layout has rootfs followed by kernel and dtb, so that we
know the rootfs start and size to embed them into the dtb instead
of having to use dummy values and fill them in afterwards
2023-12-22 23:03:07 +00:00
Daniel Barlow 30617cdb25 remove unused variable 2023-12-22 22:35:55 +00:00
Daniel Barlow 136d404cd6 tftpboot: add option for kernel image format 2023-12-22 22:15:54 +00:00
Daniel Barlow 44a0cf364b remove boot-scr output, merge into tftpboot
(1) it creates two things (script and dtb); (2) it's a bit pointless
without the tftpboot output it depends on
2023-12-22 21:37:15 +00:00
Daniel Barlow c7b2733bea tftpbootlz: put command line in dtb
this makes boot.scr substantially shorter, in anticipation of using it
for first boot of the omnia and not wanting to embed an essay in
a setenv value
2023-12-22 20:09:44 +00:00
4 changed files with 88 additions and 151 deletions

View File

@ -11,14 +11,15 @@
};
module = {pkgs, config, lib, lim, ... }:
let openwrt = pkgs.openwrt; in {
imports = [
../../modules/arch/arm.nix
../../modules/outputs/tftpbootlz.nix
../../modules/outputs/ext4fs.nix
../../modules/outputs/mbrimage.nix
../../modules/outputs/extlinux.nix
];
let openwrt = pkgs.openwrt;
in {
imports = [
../../modules/arch/arm.nix
../../modules/outputs/tftpboot.nix
../../modules/outputs/ext4fs.nix
../../modules/outputs/mbrimage.nix
../../modules/outputs/extlinux.nix
];
kernel = {
src = pkgs.pkgsBuildBuild.fetchurl {
name = "linux.tar.gz";
@ -116,7 +117,11 @@
};
};
boot.tftp.loadAddress = lim.parseInt "0x1000000";
boot.tftp = {
loadAddress = lim.parseInt "0x1000000";
kernelFormat = "zimage";
compressRoot = true;
};
hardware = let
mac80211 = pkgs.mac80211.override {

View File

@ -9,9 +9,19 @@ let
cfg = config.boot.tftp;
in {
imports = [ ../ramdisk.nix ];
options.boot.tftp.freeSpaceBytes = mkOption {
type = types.int;
default = 0;
options.boot.tftp = {
freeSpaceBytes = mkOption {
type = types.int;
default = 0;
};
kernelFormat = mkOption {
type = types.enum [ "zimage" "uimage" ];
default = "uimage";
};
compressRoot = mkOption {
type = types.bool;
default = false;
};
};
options.system.outputs = {
tftpboot = mkOption {
@ -35,73 +45,83 @@ in {
It uses the Linux `phram <https://github.com/torvalds/linux/blob/master/drivers/mtd/devices/phram.c>`_ driver to emulate a flash device using a segment of physical RAM.
'';
};
boot-scr = mkOption {
type = types.package;
internal = true;
description = ''
U-Boot commands to load and boot a kernel and rootfs over TFTP.
Copy-paste into the device boot monitor
'';
};
};
config = {
boot.ramdisk.enable = true;
system.outputs = rec {
tftpboot =
let o = config.system.outputs; in
pkgs.runCommand "tftpboot" {} ''
mkdir $out
cd $out
ln -s ${o.rootfs} rootfs
ln -s ${o.kernel} vmlinux
ln -s ${o.manifest} manifest
ln -s ${o.kernel.headers} build
ln -s ${o.uimage} uimage
ln -s ${o.boot-scr}/dtb dtb
ln -s ${o.boot-scr}/script boot.scr
'';
boot-scr =
let
inherit (pkgs.lib.trivial) toHexString;
o = config.system.outputs;
image = let choices = {
uimage = o.uimage;
zimage = o.zimage;
}; in choices.${cfg.kernelFormat};
bootCommand = let choices = {
uimage = "bootm";
zimage = "bootz";
}; in choices.${cfg.kernelFormat};
cmdline = concatStringsSep " " config.boot.commandLine;
in
pkgs.buildPackages.runCommand "boot-scr" { nativeBuildInputs = [ pkgs.pkgsBuildBuild.dtc ]; } ''
uimageSize=$(($(stat -L -c %s ${o.uimage}) + 0x1000 &(~0xfff)))
rootfsStart=$(printf %x $((${toString cfg.loadAddress} + 0x100000 + $uimageSize &(~0xfffff) )))
rootfsBytes=$(($(stat -L -c %s ${o.rootfs}) + 0x100000 &(~0xfffff)))
rootfsBytes=$(($rootfsBytes + ${toString cfg.freeSpaceBytes} ))
rootfsMb=$(($rootfsBytes >> 20))
cmd="mtdparts=phram0:''${rootfsMb}M(rootfs) phram.phram=phram0,0x''${rootfsStart},''${rootfsBytes},${toString config.hardware.flash.eraseBlockSize} root=/dev/mtdblock0";
dtbStart=$(printf %x $((${toString cfg.loadAddress} + $rootfsBytes + 0x100000 + $uimageSize )))
pkgs.runCommand "tftpboot" { nativeBuildInputs = with pkgs.pkgsBuildBuild; [ lzma dtc ]; } ''
mkdir $out
cat ${o.dtb} > $out/dtb
address_cells=$(fdtget $out/dtb / '#address-cells')
size_cells=$(fdtget $out/dtb / '#size-cells')
cd $out
binsize() { local s=$(stat -L -c %s $1); echo $(($s + 0x1000 &(~0xfff))); }
binsize64k() { local s=$(stat -L -c %s $1); echo $(($s + 0x10000 &(~0xffff))); }
hex() { printf "0x%x" $1; }
rootfsStart=${toString cfg.loadAddress}
rootfsSize=$(binsize64k ${o.rootfs} )
imageStart=$(($rootfsStart + $rootfsSize))
imageSize=$(binsize ${image})
dtbStart=$(($imageStart + $imageSize))
dtbSize=$(binsize ${o.dtb} )
ln -s ${o.manifest} manifest
ln -s ${image} image
${if cfg.compressRoot
then ''
lzma -z9cv ${o.rootfs} > rootfs.lz
rootfsLzStart=$(($dtbStart + $dtbSize))
rootfsLzSize=$(binsize rootfs.lz)
''
else "ln -s ${o.rootfs} rootfs"
}
cat ${o.dtb} > dtb
address_cells=$(fdtget dtb / '#address-cells')
size_cells=$(fdtget dtb / '#size-cells')
if [ $address_cells -gt 1 ]; then ac_prefix=0; fi
if [ $size_cells -gt 1 ]; then sz_prefix=0; fi
fdtput -p $out/dtb /reserved-memory '#address-cells' $address_cells
fdtput -p $out/dtb /reserved-memory '#size-cells' $size_cells
fdtput -p $out/dtb /reserved-memory ranges
fdtput -p -t s $out/dtb /reserved-memory/phram-rootfs@$rootfsStart compatible phram
fdtput -p -t lx $out/dtb /reserved-memory/phram-rootfs@$rootfsStart reg $ac_prefix 0x$rootfsStart $sz_prefix $(printf %x $rootfsBytes)
fdtput -p dtb /reserved-memory '#address-cells' $address_cells
fdtput -p dtb /reserved-memory '#size-cells' $size_cells
fdtput -p dtb /reserved-memory ranges
node=$(printf "phram-rootfs@%x" $rootfsStart)
fdtput -p -t s dtb /reserved-memory/$node compatible phram
fdtput -p -t lx dtb /reserved-memory/$node reg $ac_prefix $(hex $rootfsStart) $sz_prefix $(hex $rootfsSize)
# dtc -I dtb -O dts -o /dev/stdout $out/dtb | grep -A10 reserved-mem ; exit 1
dtbBytes=$(($(stat -L -c %s $out/dtb) + 0x1000 &(~0xfff)))
cmd="liminix ${cmdline} mtdparts=phram0:''${rootfsSize}(rootfs) phram.phram=phram0,''${rootfsStart},''${rootfsSize},${toString config.hardware.flash.eraseBlockSize} root=/dev/mtdblock0";
fdtput -t s dtb /chosen bootargs "$cmd"
cat > $out/script << EOF
# dtc -I dtb -O dts -o /dev/stdout dtb | grep -A10 chosen ; exit 1
cat > boot.scr << EOF
setenv serverip ${cfg.serverip}
setenv ipaddr ${cfg.ipaddr}
setenv bootargs 'liminix ${cmdline} $cmd'
tftpboot 0x${lib.toHexString cfg.loadAddress} result/uimage ; tftpboot 0x$rootfsStart result/rootfs ; tftpboot 0x$dtbStart result/dtb
bootm 0x${lib.toHexString cfg.loadAddress} - 0x$dtbStart
tftpboot $(hex $imageStart) result/image ; ${
if cfg.compressRoot
then "tftpboot $(hex $rootfsLzStart) result/rootfs.lz"
else "tftpboot $(hex $rootfsStart) result/rootfs"
}; tftpboot $(hex $dtbStart) result/dtb
${if cfg.compressRoot
then "lzmadec $(hex $rootfsLzStart) $(hex $rootfsStart)"
else ""
}
${bootCommand} $(hex $imageStart) - $(hex $dtbStart)
EOF
'';
'';
};
};
}

View File

@ -1,90 +0,0 @@
{
config
, pkgs
, lib
, ...
}:
let
inherit (lib) mkOption types concatStringsSep;
inherit (pkgs.lib.trivial) toHexString;
o = config.system.outputs;
cmdline = concatStringsSep " " config.boot.commandLine;
cfg = config.boot.tftp;
tftpbootlz =
pkgs.buildPackages.runCommand "tftpbootlz" {
nativeBuildInputs = with pkgs.pkgsBuildBuild; [ lzma dtc ];
} ''
binsize() { local s=$(stat -L -c %s $1); echo $(($s + 0x1000 &(~0xfff))); }
binsize64k() { local s=$(stat -L -c %s $1); echo $(($s + 0x10000 &(~0xffff))); }
hex() { printf "0x%x" $1; }
kernelStart=${toString cfg.loadAddress}
kernelSize=$(binsize ${o.zimage})
mkdir -p $out
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
# 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
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
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 ipaddr ${cfg.ipaddr}
setenv bootargs "liminix ${cmdline} $cmd"
tftpboot $(hex $kernelStart) result/zImage; tftpboot $(hex $dtbStart) result/dtb ; tftpboot $(hex $rootfsLzStart) result/rootfs.lz
lzmadec $(hex $rootfsLzStart) $(hex $rootfsOrigStart)
bootz $(hex $kernelStart) - $(hex $dtbStart)
EOF
'';
in {
imports = [ ../ramdisk.nix ];
options.boot.tftp.freeSpaceBytes = mkOption {
type = types.int;
default = 0;
};
options.system.outputs = {
tftpbootlz = mkOption {
type = types.package;
description = ''
tftpbootlz
**********
This is a variant of the tftpboot output intended for the
Turris Omnia. It uses a zImage instead of a uimage, as a
workaround for an awkwardly low CONFIG_SYS_BOOTM_LEN setting
in the U-Boot build for the device which means it won't boot
uimages unless they're teeny tiny.
As a bonus, it also uses lzma compression on the rootfs,
which reduces a 20MB ext4 image to around 4MB
'';
};
};
config = {
boot.ramdisk.enable = true;
system.outputs = {
inherit tftpbootlz;
};
};
}

View File

@ -19,13 +19,15 @@ in pkgsBuild.runCommand "check" {
mkdir vm
ln -s ${img} result
touch empty empty2
run-liminix-vm \
--background ./vm \
--u-boot ${uboot}/u-boot.bin \
--arch ${derivation.pkgs.stdenv.hostPlatform.qemuArch} \
--wan "user,tftp=`pwd`" \
--disk-image result/rootfs \
result/uimage result/rootfs
--disk-image empty2 \
empty empty2
expect ${./script.expect} 2>&1 |tee $out
'';