-
Notifications
You must be signed in to change notification settings - Fork 58
/
3_backup_internal_flash.sh
executable file
·93 lines (76 loc) · 2.91 KB
/
3_backup_internal_flash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
set -e
source config.sh $@
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."
exit 1
fi
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
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
rm -f backups/flash_backup_checksummed_${TARGET}.bin
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)"
read -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Aborted."
exit 1
fi
echo "Generating encrypted flash image from backed up data..."
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
echo "Failed to build encrypted flash image."
exit 1
fi
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."
exit 1
fi
echo "Flash successfully programmed. Now do the following procedure:"
echo "- Disconnect power from the device"
echo "- Power it again"
echo "- Press and hold the power button on the device"
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)!"
read -n 1
echo "Dumping internal flash..."
if ! ${OPENOCD} -f "openocd/interface_${ADAPTER}.cfg" \
-c "init;" \
-c "halt;" \
-c "dump_image backups/internal_flash_backup_${TARGET}.bin 0x24000000 131072" \
-c "exit;" >>logs/3_openocd.log 2>&1; then
echo "Dumping internal flash failed."
exit 1
fi
echo "Verifying internal flash backup..."
if ! shasum --check shasums/internal_flash_backup_${TARGET}.bin.sha1 >/dev/null 2>&1; then
echo "The backup of the internal flash failed. Please try again."
exit 1
fi
rm -f new_flash_image.bin
echo "Device backed up successfully"