diff --git a/mnt/reform/pkgs/default.nix b/mnt/reform/pkgs/default.nix new file mode 100644 index 00000000..0c1a7b4e --- /dev/null +++ b/mnt/reform/pkgs/default.nix @@ -0,0 +1,23 @@ +{ + lib, + callPackage, +}: +rec { + reform-flash-uboot = + lib.mapAttrs (name: config: callPackage ./reform-flash-uboot.nix { inherit config; }) + { + reform2-rk3588-dsi = { + warn = true; + mmc = "mmcblk0"; + mmcBoot0 = false; + ubootOffset = 32768; + flashbinOffset = 0; + image = "${ubootImage.reform2-rk3588-dsi}/rk3588-mnt-reform2-dsi-flash.bin"; + }; + }; + + uboot.reform2-rk3588-dsi = callPackage ../rk3588/uboot.nix { }; + ubootImage.reform2-rk3588-dsi = callPackage ../rk3588/uboot-image.nix { + uboot = uboot.reform2-rk3588-dsi; + }; +} diff --git a/mnt/reform/pkgs/reform-flash-uboot.nix b/mnt/reform/pkgs/reform-flash-uboot.nix new file mode 100644 index 00000000..0320d091 --- /dev/null +++ b/mnt/reform/pkgs/reform-flash-uboot.nix @@ -0,0 +1,44 @@ +{ + lib, + writeShellApplication, + config, +}: +let + dev = "/dev/" + config.mmc + lib.optionalString config.mmcBoot0 "boot0"; +in +writeShellApplication { + name = "reform-flash-uboot"; + text = '' + if [ "$(id -u)" -ne 0 ]; then + echo "reform-flash-uboot has to be run as root / using sudo." + exit 1 + fi + '' + + lib.optionalString config.warn '' + echo "W: Flashing u-boot to eMMC on $(cat /proc/device-tree/model) is not without risk." >&2 + echo "W: If you flash the wrong u-boot or if the flashing process goes wrong, it is" >&2 + echo "W: possible to soft-brick your board. Restoring it might need some extra hardware." >&2 + echo "W: Please only proceed if you are sure that the benefits outweigh the risks for you." >&2 + printf "Are you sure you want to proceed? [y/N] " + read -r response + + if [ "$response" != "y" ]; then + echo "Exiting." + exit + fi + '' + + '' + echo "Writing ${config.image} to ${dev}" >&2 + '' + + lib.optionalString config.mmcBoot0 '' + echo 0 >"/sys/class/block/${config.mmc}boot0/force_ro" + '' + + '' + dd if='${config.image}' of='${dev}' bs=512 seek='${ + builtins.toString (config.ubootOffset / 512) + }' skip='${builtins.toString (config.flashbinOffset / 512)}' + '' + + lib.optionalString config.mmcBoot0 '' + echo 1 >"/sys/class/block/${config.mmc}boot0/force_ro" + ''; +} diff --git a/mnt/reform/rk3588/installer.nix b/mnt/reform/rk3588/installer.nix index dca4e3ec..73a3647c 100644 --- a/mnt/reform/rk3588/installer.nix +++ b/mnt/reform/rk3588/installer.nix @@ -44,8 +44,8 @@ storeContents = config.installerImage.storeContents; comp = config.installerImage.squashfsCompression; }; - uboot = pkgs.callPackage ./uboot.nix { }; - firmware = pkgs.callPackage ./firmware.nix { inherit uboot; }; + packages = pkgs.callPackage ../pkgs { }; + ubootImage = packages.ubootImage.reform2-rk3588-dsi; content = pkgs.callPackage ( { stdenv, @@ -215,7 +215,7 @@ eval $(partx $img -o START,SECTORS --nr 1 --pairs) dd conv=notrunc if=${content} of=$img seek=$START count=$SECTORS - dd conv=notrunc if=${firmware}/rk3588-mnt-reform2-dsi-flash.bin of=$img seek=64 + dd conv=notrunc if=${ubootImage}/rk3588-mnt-reform2-dsi-flash.bin of=$img seek=64 if test -n "$compressImage"; then zstd -T$NIX_BUILD_CORES --rm $img diff --git a/mnt/reform/rk3588/firmware.nix b/mnt/reform/rk3588/uboot-image.nix similarity index 84% rename from mnt/reform/rk3588/firmware.nix rename to mnt/reform/rk3588/uboot-image.nix index 37784721..c6003ea5 100644 --- a/mnt/reform/rk3588/firmware.nix +++ b/mnt/reform/rk3588/uboot-image.nix @@ -4,7 +4,7 @@ fetchFromGitHub, }: # script from https://source.mnt.re/reform/reform-rk3588-uboot/-/blob/28289e36cd1cb90b302780e83b014250c880c4ec/build.sh -runCommand "mnt-reform-firmware-rk3855${uboot.variant}" { } '' +runCommand "mnt-reform-uboot-image-rk3855${uboot.variant}" { } '' mkdir $out cp ${uboot}/idbloader.img $out/rk3588-mnt-reform2${uboot.variant}-flash.bin dd if=${uboot}/u-boot.itb of=$out/rk3588-mnt-reform${uboot.variant}-flash.bin seek=16320