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"
|
2021-03-27 16:22:10 +00:00
|
|
|
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
|
2021-03-27 16:22:10 +00:00
|
|
|
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
|
|
|
|
|
2022-05-17 16:16:53 +01:00
|
|
|
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
|
2021-03-27 16:22:10 +00:00
|
|
|
|
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."
|
2021-01-01 22:21:28 +00:00
|
|
|
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"
|
2022-05-17 16:17:49 +01:00
|
|
|
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"
|
2022-05-17 16:17:49 +01:00
|
|
|
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"
|