flashimage.nix (2479B)
1 { 2 config 3 , pkgs 4 , lib 5 , ... 6 }: 7 let 8 inherit (lib) mkOption types concatStringsSep; 9 inherit (config.boot) tftp; 10 in { 11 options.system.outputs = { 12 firmware = mkOption { 13 type = types.package; 14 internal = true; # component of flashimage 15 description = '' 16 Binary image (combining kernel, FDT, rootfs, initramfs 17 if needed, etc) for the target device. 18 ''; 19 }; 20 flash-scr = mkOption { 21 type = types.package; 22 internal = true; # component of flashimage 23 description = '' 24 Copy-pastable U-Boot commands to TFTP download the 25 image and write it to flash 26 ''; 27 }; 28 flashimage = mkOption { 29 type = types.package; 30 description = '' 31 Flashable image for the target device, and the script to 32 install it 33 ''; 34 }; 35 }; 36 37 config = { 38 kernel = { 39 config = { 40 MTD_SPLIT_UIMAGE_FW = "y"; 41 }; 42 }; 43 44 programs.busybox.applets = [ 45 "flashcp" 46 ]; 47 48 system.outputs = { 49 firmware = 50 let o = config.system.outputs; in 51 pkgs.runCommand "firmware" {} '' 52 dd if=${o.uimage} of=$out bs=128k conv=sync 53 dd if=${o.rootfs} of=$out bs=128k conv=sync,nocreat,notrunc oflag=append 54 ''; 55 flashimage = 56 let o = config.system.outputs; in 57 pkgs.runCommand "flashimage" {} '' 58 mkdir $out 59 cd $out 60 ln -s ${o.firmware} firmware.bin 61 ln -s ${o.rootfs} rootfs 62 ln -s ${o.kernel} vmlinux 63 ln -s ${o.manifest} manifest 64 ln -s ${o.kernel.headers} build 65 ln -s ${o.uimage} uimage 66 ln -s ${o.dtb} dtb 67 ln -s ${o.flash-scr} flash.scr 68 ''; 69 70 flash-scr = 71 let 72 inherit (pkgs.lib.trivial) toHexString; 73 inherit (config.hardware) flash; 74 in 75 pkgs.buildPackages.runCommand "" {} '' 76 imageSize=$(stat -L -c %s ${config.system.outputs.firmware}) 77 cat > $out << EOF 78 setenv serverip ${tftp.serverip} 79 setenv ipaddr ${tftp.ipaddr} 80 tftp 0x$(printf %x ${tftp.loadAddress}) result/firmware.bin 81 erase 0x$(printf %x ${flash.address}) +${flash.size} 82 cp.b 0x$(printf %x ${tftp.loadAddress}) 0x$(printf %x ${flash.address}) \''${filesize} 83 echo command line was ${builtins.toJSON (concatStringsSep " " config.boot.commandLine)} 84 EOF 85 ''; 86 }; 87 }; 88 }