-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhex2tohex0.py
261 lines (215 loc) · 9.17 KB
/
hex2tohex0.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import pprint
import struct
import sys
def main():
hex2path = sys.argv[1]
hex0path = sys.argv[2]
base_address = int(sys.argv[3], 16)
labels = find_labels(hex2path, base_address)
print_replace(hex2path, hex0path, labels, base_address)
for label in ['addr_packet', 'GDT_start', 'IDT_locator_32', 'IDT_locator_16']:
if label in labels and labels[label] % 16 != 0:
print("ERROR: label %s not aligned to 16 byte boundary (%x)!" % (label, labels[label]))
sys.exit(1)
if 'past_MBR' in labels and labels['past_MBR'] != 0x7e00 and labels['past_MBR'] != 0x8000:
print("ERROR: label past_MBR is not 0x7e00 or 0x8000 (%x)" % labels['past_MBR'])
sys.exit(1)
def find_labels(hex2path, base_address):
cur_address = base_address
labels = {}
with open(hex2path, "r") as h2f:
for line in h2f:
if line[0] == ':':
if line[1:].strip() in labels:
print("ERROR: duplicate label: %s" % line[1:])
sys.exit(1)
labels[line[1:].strip()] = cur_address
else:
cur_address += count_hex(line)
return labels
def count_hex(line):
count = 0
pos = 0
while pos < len(line):
if line[pos] in ['#', ';']:
break
if line[pos].isalnum():
pos += 2
count += 1
continue
if line[pos] == '&' or line[pos] == '%':
count += 4
pos += 1
while line[pos].isalnum() or line[pos] == '_':
pos += 1
continue
if line[pos] == '$' or line[pos] == '@':
count += 2
pos += 1
while line[pos].isalnum() or line[pos] == '_':
pos += 1
continue
if line[pos] == '!':
count += 1
pos += 1
while line[pos].isalnum() or line[pos] == '_':
pos += 1
continue
pos += 1
return count
def print_replace(hex2path, hex0path, labels, base_address):
cur_address = base_address
print_header_comment = False
with open(hex2path, "r") as h2f, open(hex0path, "w") as h0f:
for line in h2f:
for label, address in labels.items():
if len(label) + 1 > len(littleendian(address)):
# the label (with &) is long and we need to pad the replaced address with extra spaces
extra_spaces = ' ' * (len(label) + 1 - len(littleendian(address)))
line = line.replace("&" + label + " ", littleendian(address) + extra_spaces + " ")
else:
# the label (with &) is short and we need to remove extra spaces to match the length of the address
extra_spaces = ' ' * (len(littleendian(address)) - (len(label) + 1))
line = line.replace("&" + label + extra_spaces + " ", littleendian(address) + " ")
# if there were no extra spaces make sure the replacement can work anyway
line = line.replace("&" + label + " ", littleendian(address) + " ")
if len(label) + 1 > len(littleendian16(address)):
# the label (with $) is long and we need to pad the replaced address with extra spaces
extra_spaces = ' ' * (len(label) + 1 - len(littleendian16(address)))
line = line.replace("$" + label + " 00 00", littleendian16(address) + " 00 00" + extra_spaces)
line = line.replace("$" + label + " 08 00", littleendian16(address) + " 08 00" + extra_spaces)
line = line.replace("$" + label + " ", littleendian16(address) + extra_spaces + " ")
else:
# the label (with $) is short and we need to remove extra spaces to match the length of the address
extra_spaces = ' ' * (len(littleendian16(address)) - (len(label) + 1))
line = line.replace("$" + label + extra_spaces + " ", littleendian16(address) + " ")
# if there were no extra spaces make sure the replacement can work anyway
line = line.replace("$" + label + " ", littleendian16(address) + " ")
# XXX these functions could probably be unified!
line = relative8bit_replace(line, cur_address, label, address)
line = relative16bit_replace(line, cur_address, label, address)
line = relative32bit_replace(line, cur_address, label, address)
if line[0] == ":" and print_header_comment:
line = line.replace(":" + label + "\n", "#[" + format(address, "04X") + "]\n#:" + label + "\n")
if line[0] == ":":
line = "#" + line
cur_address += count_hex(line)
h0f.write(line)
if line.startswith("#-"):
print_header_comment = True
else:
print_header_comment = False
def littleendian(address):
big_endian = format(address, "08X")
return big_endian[6:8] + ' ' + big_endian[4:6] + ' ' + big_endian[2:4] + ' ' + big_endian[0:2]
def littleendian16(address):
big_endian = format(address, "04X")
return big_endian[2:4] + ' ' + big_endian[0:2]
def relative8bit_replace(line, cur_address, label, address):
pos = 0
while pos < len(line):
if line[pos] in ['#', ';']:
break
if line[pos].isalnum():
pos += 2
cur_address += 1
continue
if line[pos] == '&':
return line
if line[pos] == '%':
return line
if line[pos] == '$':
return line
if line[pos] == '@':
return line
if line[pos] == '!':
cur_address += 1
pos += 1
end_pos = pos
while line[end_pos].isalnum() or line[end_pos] == '_':
end_pos += 1
if label == line[pos:end_pos]:
extra_spaces = ' ' * (end_pos - pos - 1)
if address - cur_address > 0:
line = line.replace("!" + label, format(address - cur_address, "02X") + extra_spaces)
else:
line = line.replace("!" + label, format(0x100 + address - cur_address, "02X") + extra_spaces)
return line
pos += 1
return line
def relative16bit_replace(line, cur_address, label, address):
pos = 0
while pos < len(line):
if line[pos] in ['#', ';']:
break
if line[pos].isalnum():
pos += 2
cur_address += 1
continue
if line[pos] == '&':
return line
if line[pos] == '%':
return line
if line[pos] == '$':
return line
if line[pos] == '!':
return line
if line[pos] == '@':
cur_address += 2
pos += 1
end_pos = pos
while line[end_pos].isalnum() or line[end_pos] == '_':
end_pos += 1
if label == line[pos:end_pos]:
extra_spaces = ' ' * (end_pos - pos - 4)
if address - cur_address > 0:
line = line.replace("@" + label, littleendian16(address - cur_address) + extra_spaces)
else:
line = line.replace("@" + label, littleendian16(0x10000 + address - cur_address) + extra_spaces)
return line
pos += 1
return line
def relative32bit_replace(line, cur_address, label, address):
pos = 0
while pos < len(line):
if line[pos] in ['#', ';']:
break
if line[pos].isalnum():
pos += 2
cur_address += 1
continue
if line[pos] == '&':
return line
if line[pos] == '!':
return line
if line[pos] == '$':
return line
if line[pos] == '@':
return line
if line[pos] == '%':
cur_address += 4
pos += 1
end_pos = pos
while line[end_pos].isalnum() or line[end_pos] == '_':
end_pos += 1
if label == line[pos:end_pos]:
label_len = end_pos - pos + 1
if address - cur_address > 0:
if label_len > 11:
extra_spaces = ' ' * (label_len - 11)
line = line.replace("%" + label, littleendian(address - cur_address) + extra_spaces)
else:
extra_spaces = ' ' * (11 - label_len)
line = line.replace("%" + label + extra_spaces, littleendian(address - cur_address))
else:
if label_len > 11:
extra_spaces = ' ' * (label_len - 11)
line = line.replace("%" + label, littleendian(0x100000000 + address - cur_address) + extra_spaces)
else:
extra_spaces = ' ' * (11 - label_len)
line = line.replace("%" + label + extra_spaces, littleendian(0x100000000 + address - cur_address))
return line
pos += 1
return line
if __name__ == '__main__':
main()