-
Notifications
You must be signed in to change notification settings - Fork 1
/
gxpsplit.py
211 lines (184 loc) · 5.76 KB
/
gxpsplit.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import io
import os
import struct
import yaml
from bitarray import bitarray
from bitarray.util import int2ba
from shutil import copyfile
import click
def decode_program(f: io.BytesIO):
p_insts = []
s_insts = []
f.seek(60)
p_inst_count = struct.unpack('I', f.read(4))[0]
p_inst_offset = struct.unpack('I', f.read(4))[0]
f.read(p_inst_offset - 4)
for i in range(p_inst_count):
inst = struct.unpack('Q', f.read(8))[0]
p_insts.append(inst)
f.seek(68)
s_inst_count = struct.unpack('I', f.read(4))[0]
s_inst_offset = struct.unpack('I', f.read(4))[0]
f.read(s_inst_offset - 4)
for i in range(s_inst_count):
inst = struct.unpack('Q', f.read(8))[0]
s_insts.append(inst)
return {
'p_insts': p_insts,
's_insts': s_insts
}
db = yaml.load(open("grammar.yaml"))
def destruct_member(member):
name = list(member.keys())[0]
fields = member[name]
offset = None
match = None
if isinstance(fields, str):
size = len(fields)
match = fields
elif isinstance(fields, int):
size = fields
elif 'size' in fields:
size = fields['size']
if 'offset' in fields:
offset = 64 - fields['offset'] - size
if 'match' in fields:
match = fields['match']
return (name, size, offset, match)
def find_decoder(inst):
for key, item in db.items():
reject = False
pointer = 0
for member in item['members']:
name, size, offset, match = destruct_member(member)
if offset is not None:
if offset != pointer:
pointer = offset
if match is not None:
if inst[pointer:pointer+size].to01() != match:
reject = True
pointer += size
if not reject:
return (key, item['members'])
return None
def generate_fields(insts):
out = []
for inst in insts:
inst_ba = int2ba(inst, 64)
inst_name, members = find_decoder(inst_ba)
desc = []
values = []
pointer = 0
for member in members:
name, size, offset, match = destruct_member(member)
if offset is not None:
if offset != pointer:
desc.append('-' * (offset - pointer))
values.append(inst_ba[pointer:offset].to01())
pointer = offset
if match is not None:
if match != inst_ba[pointer:pointer+size].to01():
print(inst_name + " not matching")
desc.append(name)
values.append(inst_ba[pointer:pointer+size].to01())
pointer += size
if pointer != 64:
desc.append('-' * (64 - pointer))
values.append(inst_ba[pointer:64].to01())
pad_desc = []
pad_values = []
for i in range(len(desc)):
d = desc[i]
v = values[i]
if len(d) > len(v):
v += ' ' * (len(d) - len(v))
else:
d += ' ' * (len(v) - len(d))
pad_desc.append(d)
pad_values.append(v)
out.append(inst_name)
out.append('|'.join(pad_desc))
out.append('|'.join(pad_values))
return '\n'.join(out)
def generate_split(program):
out = ''
out += 'Primary program start:\n'
out += generate_fields(program['p_insts']) + '\n'
out += 'Primary program end:\n'
out += '\n\n\n'
out += 'Secondary program start:\n'
out += generate_fields(program['s_insts']) + '\n'
out += 'Secondary program end:\n'
return out
def patch_program(ff, split):
lines = split.split('\n')
p_start = 0
p_end = 0
s_start = 0
s_end = 0
for i in range(len(lines)):
if lines[i] == 'Primary program start:':
p_start = i + 1
elif lines[i] == 'Primary program end:':
p_end = i
elif lines[i] == 'Secondary program start:':
s_start = i + 1
elif lines[i] == 'Secondary program end:':
s_end = i
ff.seek(64)
p_inst_offset = struct.unpack('I', ff.read(4))[0]
ff.read(p_inst_offset - 4)
i = p_start
while i < p_end:
inst = lines[i+2].replace(' ','').replace('|', '')
ff.write(struct.pack('Q', int(inst, 2)))
i += 3
ff.seek(72)
s_inst_offset = struct.unpack('I', ff.read(4))[0]
ff.read(s_inst_offset - 4)
i = s_start
while i < s_end:
inst = lines[i+2].replace(' ','').replace('|', '')
ff.write(struct.pack('Q', int(inst, 2)))
i += 3
@click.group()
def gxpsplit():
pass
@gxpsplit.command()
@click.argument("gxp")
def split(gxp):
with open(gxp, 'rb') as f:
program = decode_program(f)
out = generate_split(program)
with open(gxp + '.split', 'w') as ff:
ff.write(out)
@gxpsplit.command()
@click.argument("gxp")
def patch(gxp):
with open(gxp + '.split', 'r') as f:
split = f.read()
copyfile(gxp, 'patched_' + gxp)
with open('patched_'+gxp, 'r+b') as ff:
patch_program(ff, split)
import random
TEST_N = 50
FLOAT_MIN = 0.0
FLOAT_MAX = 1.0
INPUT_N = 144
@gxpsplit.command()
@click.argument("gxp")
def gent(gxp):
os.mkdir('test')
os.mkdir('test/gxp')
os.mkdir('test/input')
os.mkdir('test/res')
for i in range(50):
copyfile(gxp, 'test/gxp/' + gxp + str(i))
with open('test/input/' + gxp + str(i) + '.txt', 'w') as f:
out = []
for j in range(INPUT_N):
ip = random.uniform(FLOAT_MIN, FLOAT_MAX)
out.append(str(ip))
f.write('\n'.join(out) + '\n')
if __name__ == '__main__':
gxpsplit()