2022-10-07 01:24:35 +02:00
|
|
|
{
|
|
|
|
config
|
2023-02-11 14:10:38 +01:00
|
|
|
, pkgs
|
|
|
|
, lib
|
2022-10-07 01:24:35 +02:00
|
|
|
, ...
|
|
|
|
}:
|
2023-02-11 14:10:38 +01:00
|
|
|
let
|
|
|
|
inherit (lib) mkOption types concatStringsSep;
|
|
|
|
cfg = config.boot.tftp;
|
|
|
|
in {
|
2023-12-10 16:23:12 +01:00
|
|
|
imports = [ ../ramdisk.nix ];
|
2023-04-24 00:29:53 +02:00
|
|
|
options.boot.tftp.freeSpaceBytes = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 0;
|
|
|
|
};
|
2023-07-13 20:24:59 +02:00
|
|
|
options.system.outputs = {
|
|
|
|
tftpboot = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
description = ''
|
2023-11-12 18:15:58 +01:00
|
|
|
tftpboot
|
|
|
|
********
|
|
|
|
|
|
|
|
This output is intended for developing on a new device.
|
|
|
|
It assumes you have a serial connection and a
|
|
|
|
network connection to the device and that your
|
|
|
|
build machine is running a TFTP server.
|
|
|
|
|
|
|
|
The output is a directory containing kernel and
|
|
|
|
root filesystem image, and a script :file:`boot.scr` of U-Boot
|
|
|
|
commands that will load the images into memory and
|
|
|
|
run them directly,
|
|
|
|
instead of first writing them to flash. This saves
|
|
|
|
time and erase cycles.
|
|
|
|
|
|
|
|
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.
|
2023-07-13 20:24:59 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
boot-scr = mkOption {
|
|
|
|
type = types.package;
|
2023-11-12 18:15:58 +01:00
|
|
|
internal = true;
|
2023-07-13 20:24:59 +02:00
|
|
|
description = ''
|
|
|
|
U-Boot commands to load and boot a kernel and rootfs over TFTP.
|
|
|
|
Copy-paste into the device boot monitor
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2022-10-07 01:24:35 +02:00
|
|
|
config = {
|
2023-03-17 13:22:20 +01:00
|
|
|
boot.ramdisk.enable = true;
|
2022-10-07 01:24:35 +02:00
|
|
|
|
2023-07-13 20:24:59 +02:00
|
|
|
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
|
2023-10-02 18:14:47 +02:00
|
|
|
ln -s ${o.boot-scr}/dtb dtb
|
|
|
|
ln -s ${o.boot-scr}/script boot.scr
|
2023-07-13 20:24:59 +02:00
|
|
|
'';
|
2023-02-11 14:10:38 +01:00
|
|
|
|
2023-07-13 20:24:59 +02:00
|
|
|
boot-scr =
|
|
|
|
let
|
|
|
|
inherit (pkgs.lib.trivial) toHexString;
|
|
|
|
o = config.system.outputs;
|
2023-10-02 18:14:47 +02:00
|
|
|
cmdline = concatStringsSep " " config.boot.commandLine;
|
2023-07-13 20:24:59 +02:00
|
|
|
in
|
2023-10-02 18:14:47 +02:00
|
|
|
pkgs.buildPackages.runCommand "boot-scr" { nativeBuildInputs = [ pkgs.pkgsBuildBuild.dtc ]; } ''
|
2023-07-13 20:24:59 +02:00
|
|
|
uimageSize=$(($(stat -L -c %s ${o.uimage}) + 0x1000 &(~0xfff)))
|
2023-12-19 13:12:12 +01:00
|
|
|
rootfsStart=$(printf %x $((${toString cfg.loadAddress} + 0x100000 + $uimageSize &(~0xfffff) )))
|
2023-07-13 20:24:59 +02:00
|
|
|
rootfsBytes=$(($(stat -L -c %s ${o.rootfs}) + 0x100000 &(~0xfffff)))
|
|
|
|
rootfsBytes=$(($rootfsBytes + ${toString cfg.freeSpaceBytes} ))
|
2023-10-10 21:26:27 +02:00
|
|
|
rootfsMb=$(($rootfsBytes >> 20))
|
2023-12-19 13:12:12 +01:00
|
|
|
cmd="mtdparts=phram0:''${rootfsMb}M(rootfs) phram.phram=phram0,0x''${rootfsStart},''${rootfsBytes},${toString config.hardware.flash.eraseBlockSize} root=/dev/mtdblock0";
|
2023-03-17 13:22:20 +01:00
|
|
|
|
2023-11-12 19:47:31 +01:00
|
|
|
dtbStart=$(printf %x $((${toString cfg.loadAddress} + $rootfsBytes + 0x100000 + $uimageSize )))
|
2023-10-02 18:14:47 +02:00
|
|
|
|
|
|
|
mkdir $out
|
|
|
|
cat ${o.dtb} > $out/dtb
|
2023-12-21 23:21:20 +01:00
|
|
|
address_cells=$(fdtget $out/dtb / '#address-cells')
|
|
|
|
size_cells=$(fdtget $out/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
|
2023-12-19 13:12:12 +01:00
|
|
|
fdtput -p $out/dtb /reserved-memory ranges
|
2023-12-21 23:21:20 +01:00
|
|
|
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)
|
2023-10-02 18:14:47 +02:00
|
|
|
|
2023-12-19 13:12:12 +01:00
|
|
|
# dtc -I dtb -O dts -o /dev/stdout $out/dtb; exit 1
|
2023-10-02 18:14:47 +02:00
|
|
|
dtbBytes=$(($(stat -L -c %s $out/dtb) + 0x1000 &(~0xfff)))
|
|
|
|
|
|
|
|
cat > $out/script << EOF
|
2023-07-13 20:24:59 +02:00
|
|
|
setenv serverip ${cfg.serverip}
|
|
|
|
setenv ipaddr ${cfg.ipaddr}
|
2023-10-02 18:14:47 +02:00
|
|
|
setenv bootargs 'liminix ${cmdline} $cmd'
|
2023-12-19 13:12:12 +01:00
|
|
|
tftpboot 0x${lib.toHexString cfg.loadAddress} result/uimage ; tftpboot 0x$rootfsStart result/rootfs ; tftpboot 0x$dtbStart result/dtb
|
2023-11-25 00:29:12 +01:00
|
|
|
bootm 0x${lib.toHexString cfg.loadAddress} - 0x$dtbStart
|
2023-07-13 20:24:59 +02:00
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
};
|
2022-10-07 01:24:35 +02:00
|
|
|
};
|
|
|
|
}
|