forked from ghidraninja/game-and-watch-flashloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash_multi.sh
executable file
·71 lines (55 loc) · 1.55 KB
/
flash_multi.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
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ELF=${DIR}/build/gw_base.elf
if [[ $# -lt 1 ]]; then
echo "Usage: flash_multi.sh <binary to flash>"
echo "Note! This will cut the binary in 1M chunks and flash them to 0x000000 and onwards"
exit
fi
IMAGE=$1
if [[ $# -gt 1 ]]; then
ADDRESS=$2
fi
if [[ $# -gt 2 ]]; then
SIZE=$3
fi
FILESIZE=$(stat -c%s $IMAGE)
CHUNKS=$(( FILESIZE / (1024*1024) ))
SIZE=$((FILESIZE))
BLOCK_SIZE=$(( 64 * 1024 ))
ERASE_BLOCKS=0
echo $CHUNKS
ERASE=1
i=0
while [[ $SIZE -gt 0 ]]; do
ADDRESS_HEX=$(printf "0x%08x" $(( i * 1024 * 1024 )))
if [[ $SIZE -ge $((1024*1024)) ]]; then
echo less
CHUNK_SIZE=$((1024*1024))
else
echo else
CHUNK_SIZE=${SIZE}
fi
SIZE_HEX=$(printf "0x%08x" ${CHUNK_SIZE})
TMPFILE=$(mktemp /tmp/flash_chunk.XXXXXX)
if [[ ! -e $TMPFILE ]]; then
echo "Can't create tempfile!"
exit 1
fi
echo "Preparing chunk $i in file ${TMPFILE}"
dd if=${IMAGE} of=${TMPFILE} bs=1024 count=$(( CHUNK_SIZE / 1024 )) skip=$(( i * 1024 ))
echo "Flashing!"
if [[ $ERASE -eq 1 ]]; then
ERASE_BLOCKS=$(( ( SIZE + BLOCK_SIZE - 1 ) / BLOCK_SIZE ))
echo "erasing $ERASE_BLOCKS"
${DIR}/flash.sh ${TMPFILE} ${ADDRESS_HEX} ${SIZE_HEX} ${ERASE} ${ERASE_BLOCKS}
else
${DIR}/flash.sh ${TMPFILE} ${ADDRESS_HEX} ${SIZE_HEX} 0
fi
# Skip erase the following iterations
ERASE=0
rm -f ${TMPFILE}
SIZE=$(( SIZE - 1024*1024 ))
i=$(( i + 1 ))
done