-
Notifications
You must be signed in to change notification settings - Fork 3
/
diag_common.py
112 lines (86 loc) · 3.12 KB
/
diag_common.py
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
import struct
from common.annotations import apply_comments
from common.memory import entry_points, memory_addr_info
def labelname(name):
name = name.replace(" ", "_")
return "Entry_" + "".join([c for c in name if c.isalnum() or c in "_"])
def parse_header(memory, base_address, address):
test_id = 0
while True:
offset = struct.unpack_from(">H", memory[address:address+2])[0]
memory_addr_info[address].visited = True
memory_addr_info[address].type = ">H"
memory_addr_info[address].label = f"TEST_{test_id}"
test_id += 1
memory_addr_info[address+1].visited = True
address += 2
if offset == 0:
return address
name = ""
memory_addr_info[address].type = "cstring"
while True:
memory_addr_info[address].visited = True
if memory[address] == 0:
break
name += chr(memory[address] & 0x7f)
address += 1
address += 1
entry_points.append(base_address + offset)
memory_addr_info[base_address + offset].label = labelname(name)
def verify_string(memory, address):
for i in range(64):
if memory[address + i] == 0:
return True
if memory[address + i] & 0x80 == 0:
return False
return False
def add_string(mem, address):
memory_addr_info[address].visited = True
memory_addr_info[address].type = "cstring"
while True:
if mem[address] == 0:
break
address += 1
# resume execution after string
address += 1
entry_points.append(address)
def add_word(mem, address):
memory_addr_info[address].visited = True
memory_addr_info[address].type = ">H"
# resume execution after the word
address += 2
entry_points.append(address)
def scan_strings(mem, address):
while True:
if mem[address] == 0x7a and mem[address+1] == 0x01 and mem[address+2] == 0x12:
address += 3
add_string(mem, address)
address += 1
if address >= 0xfd00:
break
def scan_calls (mem, base_address, addr):
# F3/F4 use this pattern setting up calls, and we can just detect them all
while True:
if mem[addr] == 0x90 and mem[addr+3] == 0x50 and mem[addr+4] == 0x80:
dest = struct.unpack_from(">H", mem[addr+1:addr+3])[0]
if dest < 0x800:
entry_points.append(dest + base_address)
memory_addr_info[dest + base_address].label = f"Fn_{dest:x}"
addr += 1
if addr >= 0x10000:
break
def scan_call_args(mem, address):
while True:
# call @(0x0104) - FinishTest
# A word is a relative address of a restart point
if mem[address] == 0x7a and mem[address+1] == 0x01 and mem[address+2] == 0x04:
address += 3
add_word(mem, address)
# call (A + 0x00) - Init
# A word is a hardware base address to use
elif mem[address] == 0x7d and mem[address+1] == 0x00:
address += 2
add_word(mem, address)
address += 1
if address >= 0xfd00:
break