game-and-watch-backup/3_backup_internal_flash.sh

94 lines
2.9 KiB
Bash
Raw Normal View History

2020-11-29 10:58:55 +00:00
#!/bin/bash
set -e
2021-11-14 14:06:48 +00:00
source config.sh $@
2020-11-29 10:58:55 +00:00
2021-11-14 14:06:48 +00:00
if test -f backups/internal_flash_backup_${TARGET}.bin; then
echo "Already have a backup in backups/internal_flash_backup_${TARGET}.bin, refusing to overwrite."
2020-11-29 10:58:55 +00:00
exit 1
fi
2021-11-14 14:06:48 +00:00
if ! dd if=backups/flash_backup_${TARGET}.bin of=backups/flash_backup_checksummed_${TARGET}.bin bs=16 skip=${SPIFLASH_SKIP_16} count=${SPIFLASH_COUNT_16} >/dev/null 2>&1; then
echo "Failed to access flash_backup_${TARGET}.bin"
echo "Please run ./2_backup_flash.sh again"
exit 1
fi
2021-11-14 14:06:48 +00:00
if ! shasum --check shasums/flash_backup_checksummed_${TARGET}.bin.sha1 >/dev/null 2>&1; then
echo "*** External flash backup does not verify correctly ***"
echo "Please run ./2_backup_flash.sh again"
rm backups/flash_backup_checksummed.bin
exit 1
fi
echo "Validating ITCM dump..."
if ! shasum --check shasums/itcm_backup_${TARGET}.bin.sha1 >/dev/null 2>&1; then
echo "*** ITCM dump does not verify correctly ***"
echo "Please run ./2_backup_flash.sh again"
exit 1
fi
2021-11-14 14:06:48 +00:00
rm -f backups/flash_backup_checksummed_${TARGET}.bin
2020-11-29 18:30:27 +00:00
echo "This step will overwrite the contents of the SPI flash chip that we backed up in step 2."
echo "It will be restored in step 5. Continue? (y/N)"
2020-11-29 12:23:05 +00:00
read -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Aborted."
exit 1
fi
2020-11-29 10:58:55 +00:00
echo "Generating encrypted flash image from backed up data..."
2021-11-14 14:06:48 +00:00
if ! python3 python/tcm_encrypt.py \
backups/flash_backup_${TARGET}.bin \
${FLASH_OFFSET} \
backups/itcm_backup_${TARGET}.bin \
payload/payload.bin \
new_flash_image.bin; then
2020-11-29 10:58:55 +00:00
echo "Failed to build encrypted flash image."
exit 1
fi
2021-11-14 14:06:48 +00:00
echo "Programming payload to SPI flash..."
if ! ${OPENOCD} -f "openocd/target_${TARGET}.cfg" -f "openocd/interface_${ADAPTER}.cfg" \
-c "init;" \
-c "halt;" \
-c "program new_flash_image.bin 0x90000000 verify;" \
-c "exit;" >>logs/3_openocd.log 2>&1; then
echo "Writing payload to SPI flash failed. Check debug connection and try again."
2020-11-29 11:54:59 +00:00
exit 1
2020-11-29 10:58:55 +00:00
fi
2021-11-14 14:06:48 +00:00
echo "Flash successfully programmed. Now do the following procedure:"
2020-11-29 10:58:55 +00:00
echo "- Disconnect power from the device"
echo "- Power it again"
echo "- Press and hold the power button on the device"
2021-11-14 14:06:48 +00:00
echo "- The LCD should show a blue screen"
echo "- If it's not blue, you can try pressing the Time button on the device"
echo "- Press return (while still holding the power button)!"
2020-11-29 10:58:55 +00:00
read -n 1
echo "Dumping internal flash..."
2021-11-14 14:06:48 +00:00
if ! ${OPENOCD} -f "openocd/interface_${ADAPTER}.cfg" \
2020-11-29 10:58:55 +00:00
-c "init;" \
-c "halt;" \
2021-11-14 14:06:48 +00:00
-c "dump_image backups/internal_flash_backup_${TARGET}.bin 0x24000000 131072" \
2020-11-29 11:31:12 +00:00
-c "exit;" >>logs/3_openocd.log 2>&1; then
2020-11-29 10:58:55 +00:00
echo "Dumping internal flash failed."
exit 1
fi
echo "Verifying internal flash backup..."
2021-11-14 14:06:48 +00:00
if ! shasum --check shasums/internal_flash_backup_${TARGET}.bin.sha1 >/dev/null 2>&1; then
2020-11-29 10:58:55 +00:00
echo "The backup of the internal flash failed. Please try again."
exit 1
fi
2021-11-14 14:06:48 +00:00
rm -f new_flash_image.bin
2020-11-29 10:58:55 +00:00
2020-12-02 04:14:00 +00:00
echo "Device backed up successfully"