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;
|
2024-02-15 21:12:03 +01:00
|
|
|
hw = config.hardware;
|
|
|
|
arch = pkgs.stdenv.hostPlatform.linuxArch;
|
2023-02-11 14:10:38 +01:00
|
|
|
in {
|
2023-12-10 16:23:12 +01:00
|
|
|
imports = [ ../ramdisk.nix ];
|
2023-12-22 23:15:54 +01:00
|
|
|
options.boot.tftp = {
|
|
|
|
freeSpaceBytes = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 0;
|
|
|
|
};
|
|
|
|
kernelFormat = mkOption {
|
|
|
|
type = types.enum [ "zimage" "uimage" ];
|
|
|
|
default = "uimage";
|
|
|
|
};
|
2023-12-23 00:20:35 +01:00
|
|
|
compressRoot = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2024-02-15 21:12:03 +01:00
|
|
|
appendDTB = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2023-04-24 00:29:53 +02:00
|
|
|
};
|
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
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
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
|
|
|
|
inherit (pkgs.lib.trivial) toHexString;
|
|
|
|
o = config.system.outputs;
|
2023-12-22 23:15:54 +01:00
|
|
|
image = let choices = {
|
|
|
|
uimage = o.uimage;
|
|
|
|
zimage = o.zimage;
|
|
|
|
}; in choices.${cfg.kernelFormat};
|
|
|
|
bootCommand = let choices = {
|
|
|
|
uimage = "bootm";
|
|
|
|
zimage = "bootz";
|
|
|
|
}; in choices.${cfg.kernelFormat};
|
2024-02-15 21:12:03 +01:00
|
|
|
|
2023-10-02 18:14:47 +02:00
|
|
|
cmdline = concatStringsSep " " config.boot.commandLine;
|
2024-02-15 21:12:03 +01:00
|
|
|
objcopy = "${pkgs.stdenv.cc.bintools.targetPrefix}objcopy";
|
|
|
|
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
|
|
|
|
'';
|
2023-07-13 20:24:59 +02:00
|
|
|
in
|
2024-02-15 21:12:03 +01:00
|
|
|
pkgs.runCommand "tftpboot" { nativeBuildInputs = with pkgs.pkgsBuildBuild; [ lzma dtc pkgs.stdenv.cc ubootTools ]; } ''
|
2023-12-22 22:37:15 +01:00
|
|
|
mkdir $out
|
|
|
|
cd $out
|
2023-12-23 00:03:07 +01:00
|
|
|
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; }
|
2024-02-15 21:12:03 +01:00
|
|
|
|
2023-12-23 00:03:07 +01:00
|
|
|
rootfsStart=${toString cfg.loadAddress}
|
|
|
|
rootfsSize=$(binsize64k ${o.rootfs} )
|
2024-01-26 23:45:59 +01:00
|
|
|
rootfsSize=$(($rootfsSize + ${toString cfg.freeSpaceBytes} ))
|
2023-12-23 00:03:07 +01:00
|
|
|
|
2023-12-22 22:37:15 +01:00
|
|
|
ln -s ${o.manifest} manifest
|
2023-12-23 16:34:01 +01:00
|
|
|
ln -s ${o.kernel} vmlinux # handy for gdb
|
2023-03-17 13:22:20 +01:00
|
|
|
|
2024-02-15 21:12:03 +01:00
|
|
|
# if we are transferring kernel and dtb separately, the
|
|
|
|
# dtb has to precede the kernel in ram, because zimage
|
|
|
|
# decompression code will assume that any memory after the
|
|
|
|
# end of the kernel is free
|
|
|
|
|
|
|
|
dtbStart=$(($rootfsStart + $rootfsSize))
|
|
|
|
${if cfg.compressRoot
|
|
|
|
then ''
|
|
|
|
lzma -z9cv ${o.rootfs} > rootfs.lz
|
|
|
|
rootfsLzStart=$dtbStart
|
|
|
|
rootfsLzSize=$(binsize rootfs.lz)
|
|
|
|
dtbStart=$(($dtbStart + $rootfsLzSize))
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
ln -s ${o.rootfs} rootfs
|
|
|
|
''
|
|
|
|
}
|
|
|
|
|
2023-12-23 00:03:07 +01:00
|
|
|
cat ${o.dtb} > dtb
|
|
|
|
address_cells=$(fdtget dtb / '#address-cells')
|
|
|
|
size_cells=$(fdtget dtb / '#size-cells')
|
2023-12-22 18:37:53 +01:00
|
|
|
if [ $address_cells -gt 1 ]; then ac_prefix=0; fi
|
|
|
|
if [ $size_cells -gt 1 ]; then sz_prefix=0; fi
|
|
|
|
|
2023-12-23 00:03:07 +01:00
|
|
|
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
|
2023-12-23 00:19:28 +01:00
|
|
|
fdtput -p -t lx dtb /reserved-memory/$node reg $ac_prefix $(hex $rootfsStart) $sz_prefix $(hex $rootfsSize)
|
2023-12-23 00:03:07 +01:00
|
|
|
|
2023-12-23 00:34:08 +01:00
|
|
|
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"
|
2023-10-02 18:14:47 +02:00
|
|
|
|
2024-02-15 21:12:03 +01:00
|
|
|
dtbSize=$(binsize ./dtb )
|
|
|
|
|
|
|
|
${if cfg.appendDTB then ''
|
|
|
|
imageStart=$dtbStart
|
|
|
|
# re-package image with updated dtb
|
|
|
|
cat ${o.kernel} > vmlinux.elf
|
|
|
|
${objcopy} --update-section .appended_dtb=dtb vmlinux.elf
|
|
|
|
${stripAndZip}
|
|
|
|
mkimage -A ${arch} -O linux -T kernel -C lzma -a $(hex ${toString hw.loadAddress}) -e $(hex ${toString hw.entryPoint}) -n '${lib.toUpper arch} Liminix Linux tftpboot' -d vmlinux.bin.lzma image
|
|
|
|
# dtc -I dtb -O dts -o /dev/stdout dtb | grep -A10 chosen ; exit 1
|
|
|
|
tftpcmd="tftpboot $(hex $imageStart) result/image "
|
|
|
|
bootcmd="bootm $(hex $imageStart)"
|
|
|
|
'' else ''
|
|
|
|
imageStart=$(($dtbStart + $dtbSize))
|
|
|
|
tftpcmd="tftpboot $(hex $imageStart) result/image; tftpboot $(hex $dtbStart) result/dtb "
|
|
|
|
ln -s ${image} image
|
|
|
|
bootcmd="${bootCommand} $(hex $imageStart) - $(hex $dtbStart)"
|
|
|
|
''}
|
2023-10-02 18:14:47 +02:00
|
|
|
|
2023-12-23 00:03:07 +01:00
|
|
|
cat > boot.scr << EOF
|
2023-07-13 20:24:59 +02:00
|
|
|
setenv serverip ${cfg.serverip}
|
|
|
|
setenv ipaddr ${cfg.ipaddr}
|
2024-02-15 21:12:03 +01:00
|
|
|
${
|
2023-12-23 00:20:35 +01:00
|
|
|
if cfg.compressRoot
|
|
|
|
then "tftpboot $(hex $rootfsLzStart) result/rootfs.lz"
|
|
|
|
else "tftpboot $(hex $rootfsStart) result/rootfs"
|
2024-02-15 21:12:03 +01:00
|
|
|
}; $tftpcmd
|
2023-12-23 00:20:35 +01:00
|
|
|
${if cfg.compressRoot
|
2024-01-01 21:21:42 +01:00
|
|
|
then "lzmadec $(hex $rootfsLzStart) $(hex $rootfsStart); "
|
2023-12-23 00:20:35 +01:00
|
|
|
else ""
|
2024-02-15 21:12:03 +01:00
|
|
|
} $bootcmd
|
2023-07-13 20:24:59 +02:00
|
|
|
EOF
|
2023-12-22 22:37:15 +01:00
|
|
|
'';
|
|
|
|
|
2023-07-13 20:24:59 +02:00
|
|
|
};
|
2022-10-07 01:24:35 +02:00
|
|
|
};
|
|
|
|
}
|