forked from pret/ds_disassembly_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asmdiff.sh
138 lines (125 loc) · 3.65 KB
/
asmdiff.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
DEFAULT_BASEROM=baserom.nds
DEFAULT_ARM9BUILDDIR=build/heartgold.us
DEFAULT_ARM7BUILDDIR=sub/build
# Build ntruncompbw on demand
[[ ntruncompbw -nt ntruncompbw.c ]] || gcc -O3 -g -o ntruncompbw ntruncompbw.c
getword() {
od -j "$2" -N 4 -A n -t u "$1" | awk '{$1=$1};1'
}
[[ -n "$DEVKITARM" ]] && export PATH=${DEVKITARM}/bin:${PATH}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h)
echo "Diff segments of a Nintendo DS ROM"
echo "Usage: $0 [-h] [-7] [-m OVERLAY] [-r BASEROM] [-d BUILDDIR] [START [END]]"
echo ""
echo "Arguments:"
echo " START, END Start and end virtual addresses to diff"
echo ""
echo "Options:"
echo " -7 Diff the ARM7 module (default: ARM9)"
echo " -m OVERLAY Diff the indicated overlay module (default: static module)"
echo " -r BASEROM Use the indicated baserom (default: baserom.nds)"
echo " -d BUILDDIR Look for compiled binaries in this directory (default: build/heartgold.us)"
echo " -t Force THUMB instructions (default: ARM)"
echo " -h Show this message and exit"
exit 0
;;
-7)
proc=armv4t
builddir=${builddir:-$DEFAULT_ARM7BUILDDIR}
basestem=${basestem}.sub
shift
;;
-m)
[[ -n $overlay ]] && { echo can only do one overlay at a time; exit 1; }
mode=overlay
overlay="$2"
basestem=${basestem}.o${overlay}
shift
shift
;;
-r)
baserom="$2"
shift
shift
;;
-t)
thumb=-Mforce-thumb
shift
;;
-d)
builddir="$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
mode=${mode:-static}
proc=${proc:-armv5te}
builddir=${builddir:-$DEFAULT_ARM9BUILDDIR}
baserom=${baserom:-$DEFAULT_BASEROM}
basefile=${baserom}${basestem}.sbin
[[ "$mode" == overlay ]] && {
case $proc in
armv4t)
ovt=88
;;
armv5te)
ovt=80
;;
esac
ovtoff=$(getword "$baserom" "$ovt")
vma=$(getword "$baserom" "$((ovtoff+32*overlay+4))")
size=$(getword "$baserom" "$((ovtoff+32*overlay+8))")
[[ -f $basefile ]] || {
fileid=$(getword "$baserom" "$((ovtoff+32*overlay+24))")
param=$(getword "$baserom" "$((ovtoff+32*overlay+28))")
fatoff=$(getword "$baserom" 72)
fileoff=$(getword "$baserom" "$((fatoff+8*fileid))")
filesize=$(($(getword "$baserom" "$((fatoff+8*fileid+4))")-fileoff))
dd if="$baserom" of="$basefile" bs=1 skip="$fileoff" count="$filesize" 2>/dev/null
(( param & 16777216 )) && {
compsize=$((param & 16777215))
./ntruncompbw $basefile $vma $((vma+compsize)) || { rm -f $basefile; exit 1; }
}
}
buildfile=$builddir/OVY_${overlay}.sbin
} || {
case $proc in
armv4t)
romtab=48
compname=sub
;;
armv5te)
romtab=32
compname=main
;;
esac
fileoff=$(getword "$baserom" "$romtab")
vma=$(getword "$baserom" "$((romtab+8))")
size=$(getword "$baserom" "$((romtab+12))")
[[ -f $basefile ]] || {
dd if="$baserom" of="$basefile" bs=1 skip="$fileoff" count="$size" 2>/dev/null
[[ $proc == armv5te ]] && {
_start_ModuleParams=$(python find_module_params.py ${basefile})
compstatend=$(getword "$basefile" $((_start_ModuleParams+20)))
[[ $compstatend != "0" ]] && { ./ntruncompbw $basefile $vma $compstatend || { rm -f $basefile; exit 1; }; }
}
}
buildfile=${builddir}/${compname}.sbin
}
[[ -n "$1" ]] && start=$(($1)) || start=$vma
[[ -n "$2" ]] && size=$(($2)) || size=$(wc -c <$basefile)
do-objdump () {
arm-none-eabi-objdump -Drz -bbinary -m$proc $thumb --adjust-vma=$vma --start-address=$start --stop-address=$((start+size)) $1
}
diff -u <(do-objdump $basefile) <(do-objdump $buildfile)