forked from bzhxx/game-and-watch-retro-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_saves.sh
executable file
·75 lines (61 loc) · 2.11 KB
/
dump_saves.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
#!/bin/bash
if [[ "$VERBOSE" == "1" ]]; then
set -ex
else
set -e
fi
if [[ "${GCC_PATH}" != "" ]]; then
DEFAULT_OBJDUMP=${GCC_PATH}/arm-none-eabi-objdump
DEFAULT_GDB=${GCC_PATH}/arm-none-eabi-gdb
else
DEFAULT_OBJDUMP=arm-none-eabi-objdump
DEFAULT_GDB=arm-none-eabi-gdb
fi
FLSHLD_DIR=${OBJDUMP:-../game-and-watch-flashloader}
OBJDUMP=${OBJDUMP:-$DEFAULT_OBJDUMP}
GDB=${GDB:-$DEFAULT_GDB}
ADAPTER=${ADAPTER:-stlink}
OPENOCD=${OPENOCD:-$(which openocd || true)}
if [[ -z ${OPENOCD} ]]; then
echo "Cannot find 'openocd' in the PATH. You can set the environment variable 'OPENOCD' to manually specify the location"
exit 2
fi
if [[ $# -lt 1 ]]; then
echo "Usage: $(basename $0) <currently_running_binary.elf> [backup directory]"
echo "This will dump all save states from the device to the backup directory"
exit 1
fi
ELF=$1
OUTDIR=save_states
if [[ $# -gt 1 ]]; then
OUTDIR="$2"
fi
function get_number_of_saves {
prefix=$1
objdump_cmd="${OBJDUMP} -t ${ELF}"
echo $(${objdump_cmd} | grep " $prefix" | wc -l)
}
# Start processing
mkdir -p "$OUTDIR"
for emu in gb gg nes pce sms gw; do
mkdir -p "${OUTDIR}/${emu}"
COUNT=$(get_number_of_saves SAVE_$(echo ${emu} | awk '{print toupper($0)}')_)
for i in $(seq 0 $(( COUNT - 1 ))); do
name=$(${GDB} "${ELF}" --batch -q -ex "printf \"%s\n\", ${emu}_roms[${i}].name")
address=$(${GDB} "${ELF}" --batch -q -ex "printf \"0x%08x\n\", ${emu}_roms[${i}].save_address")
size=$(${GDB} "${ELF}" --batch -q -ex "printf \"0x%08x\n\", ${emu}_roms[${i}].save_size")
echo ""
echo ""
echo "Dumping save data for:"
echo " rom_name=\"$name\""
echo " save_address=$address"
echo " save_size=$size"
echo ""
echo ""
image="${OUTDIR}/${emu}/${name}.save"
# openocd does not handle [ and ] well in filenames.
image_quoted=${image//\[/\\[}
image_quoted=${image_quoted//\]/\\]}
${OPENOCD} -f ${FLSHLD_DIR}/interface_${ADAPTER}.cfg -c "init; halt; dump_image \"${image_quoted}\" ${address} ${size}; resume; exit;"
done
done