Add nix run visionFive2_* commands

This commit is contained in:
Robert Hensing 2023-02-16 13:21:01 +01:00
parent f85cae4a48
commit ee4b3c9e33
3 changed files with 169 additions and 1 deletions

View File

@ -131,7 +131,7 @@
type = "app";
program = "${program}";
};
};
} // import ./visionfive2/commands.nix { inherit inputs pkgs; };
packages.${system} = {
jh7100-recover = pkgs.writeCBin "jh7100-recover" (builtins.readFile "${jh71xx-tools}/jh7100-recover.c");
};

61
visionfive2/README.md Normal file
View File

@ -0,0 +1,61 @@
# VisionFive 2
As of writing, VisionFive 2 support is quite experimental.
## Flash the bootloader via serial connection
This step may be optional.
Make the serial connection according to the section "Recovering the Bootloader" in <https://doc-en.rvspace.org/VisionFive2/PDF/VisionFive2_QSG.pdf>.
Flip the tiny switches towards the H (as opposed to L) marking on the PCB (towards edge of the board) as described that section (Step 2).
Power up, and assuming your serial device is `/dev/ttyUSB0`, run:
```shellSession
nix run .#visionFive2_bootloader_recover /dev/ttyUSB0
```
## Write a bootable SD card
```shellSession
$ nix build .#images.visionfive2-cross
```
Insert an SD card of which all contents will be replaced.
These instructions assume your SD card reader is `/dev/mmcblk0`.
Make sure no partitions are mounted.
```shellSession
$ echo /dev/mmcblk0p*
$ sudo umount /dev/mmcblk0p1
$ sudo umount /dev/mmcblk0p2
... repeat for all partitions
```
`pv` provides a rough progress indicator based on the compressed size.
If you don't have `pv`, run in `nix shell nixpkgs#pv` or use `cat` instead.
```shellSession
$ sudo sh -c 'pv <result/sd-image/nixos-sd-image-*.img.zst | zstd -d | dd of=/dev/mmcblk0'
$ sync
```
## Other commands
### Enter the firmware recovery via serial
Prepare as you would for flashing the bootloader, and then:
```shellSession
nix run .#visionFive2_recovery_start /dev/ttyUSB0
```
### Establish a serial connection
Compatible with the recovery mode, but also suitable for the Linux terminal
```shellSession
nix run .#visionFive2_recovery_resume /dev/ttyUSB0
```

107
visionfive2/commands.nix Normal file
View File

@ -0,0 +1,107 @@
{ inputs, pkgs }:
let
wrapSudo = command:
pkgs.writeShellScript "wrapped.sh" ''
if $(groups | grep --quiet --word-regexp "dialout"); then
echo "User is in dialout group, avoiding sudo"
${command} "$@"
else
echo "User is not in dialout group, using sudo"
sudo ${command} "$@"
fi
'';
visionFive2_recovery_start =
let
expectScript = pkgs.writeScript "expect-visionfive-recoverBootLoader" ''
#!${pkgs.expect}/bin/expect -f
set timeout -1
spawn ${pkgs.picocom}/bin/picocom [lindex $argv 0] -b 115200 -s "${pkgs.lrzsz}/bin/sz -X"
expect "CC"
send "\x01\x13"
expect "*** file:"
send "${inputs.jh7110_recovery_binary}"
send "\r"
expect "Transfer complete"
'';
program = pkgs.writeShellScript "flash-visionfive.sh" ''
echo >&2 NOTE: If your board appears to hang, RX/TX may be flipped,
echo >&2 _depending on boot setting_!!!
echo "$0"
${expectScript} "$@"
echo >&2 "Launching new session. Hint enter to display help."
${visionFive2_recovery_resume.program} "$@"
'';
in { type = "app"; program = "${wrapSudo program}"; };
visionFive2_recovery_resume =
let
program = pkgs.writeScript "recoverBootloader_resume" ''
#!${pkgs.runtimeShell}
set -eu
${pkgs.picocom}/bin/picocom $1 -b 115200 -s "${pkgs.lrzsz}/bin/sz -X"
'';
in { type = "app"; program = "${wrapSudo program}"; };
visionFive2_bootloader_recover =
let
expectScript = pkgs.writeScript "expect-visionfive-recover-bootLoader" ''
#!${pkgs.expect}/bin/expect -f
set timeout -1
spawn ${pkgs.picocom}/bin/picocom [lindex $argv 0] -b 115200 -s "${pkgs.lrzsz}/bin/sz -X"
expect "CC"
send "\x01\x13"
expect "*** file:"
send "${inputs.jh7110_recovery_binary}"
send "\r"
expect "Transfer complete"
# Wait for menu and install SPL
expect "0: update 2ndboot/SPL in flash"
send "0\r"
expect "CC"
send "\x01\x13"
expect "*** file:"
send "${inputs.jh7110_u-boot-spl-bin}"
send "\r"
expect "Transfer complete"
# Wait for menu and install u-boot
expect "2: update fw_verif/uboot in flash"
send "2\r"
expect "CC"
send "\x01\x13"
expect "*** file:"
send "${inputs.jh7110_u-boot-bin}"
send "\r"
expect "Transfer complete"
'';
program = pkgs.writeShellScript "flash-visionfive.sh" ''
cat >&2 <<EOF
NOTE: If you haven't already switched the boot mode
- power off
- flip the tiny switches towards the H (as opposed to L)
marking on the PCB (towards edge of the board)
EOF
${expectScript} "$@"
cat >&2 <<EOF
NOTE: If all went well, flip the switches back to the L (as opposed
to H) marking on the PCB (away from edge of board).
'';
in
{
type = "app";
program = "${wrapSudo program}";
};
in
{
inherit
visionFive2_recovery_start
visionFive2_recovery_resume
visionFive2_bootloader_recover
;
}