-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmicroparse.py
executable file
·198 lines (153 loc) · 6.43 KB
/
microparse.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
#! /usr/bin/env python3
import argparse
import datetime
import os
import re
import binascii
import struct
import amd
import intel
import via
# Used to parse processor signature
class signature():
fmt_string = "-- %-27s: %s\n"
def __init__(self, signature):
if signature != 0:
self.stepping = signature & 0xF
signature = signature >> 4
self.model = signature & 0xF
signature = signature >> 4
self.family = signature & 0xF
signature = signature >> 4
self.type = signature & 0x3
signature = signature >> 2
self.unknown1 = signature & 0x3
signature = signature >> 2
self.extended_model = signature & 0xF
signature = signature >> 4
self.extended_family = signature & 0xFF
signature = signature >> 8
self.unknown2 = signature & 0xF
else:
raise Exception("Invalid processor signature!")
def __str__(self):
return \
signature.fmt_string % ("Stepping", static.hex8(self.stepping)) + \
signature.fmt_string % ("Model", static.hex8(self.model)) + \
signature.fmt_string % ("Family", static.hex8(self.family)) + \
signature.fmt_string % ("Type", static.hex8(self.type)) + \
signature.fmt_string % ("Unknown 1", static.hex8(self.unknown1)) + \
signature.fmt_string % ("Extended Model", static.hex8(self.extended_model)) + \
signature.fmt_string % ("Extended Family", static.hex8(self.extended_family)) + \
signature.fmt_string % ("Unknown 2", static.hex8(self.unknown2))
class static():
fmt_string = "%-30s: %s\n"
def data(swap_endian):
if not swap_endian:
return struct.Struct("<I")
else:
return struct.Struct(">I")
def int2date(date):
hex_date = static.hex8(date)[2 : ]
return hex_date[4 : 8] + "/" + hex_date[0 : 2] + "/" + hex_date[2 : 4]
def ymd2date(y, m, d):
return str(y).zfill(4) + "/" + str(m).zfill(2) + "/" + str(d).zfill(2)
def hex8(num):
return "0x%08x" % num
def tprint(string):
print(str(datetime.datetime.now()) + ": " + string)
def ascii2bin(data):
code = b""
values = re.sub(b"[ \t\r]", b"", data) # remove spaces, tabs, and newlines (windows)
values = re.split(b"[,\n]", values) # split on newlines (linux) and commas
for v in values:
if v.startswith(b"0x"):
code += binascii.unhexlify(bytes(v[2 : ]))
return code
def detect_ascii(path):
with open(path, "rb") as f:
for block in f:
if b'\0' in block: # check for null
return False
return True
def open_path(path):
if os.path.isdir(path):
if result.recursive == True:
listing = os.listdir(path)
for obj in listing:
open_path(path + "/" + obj)
else:
raise Exception("Cannot open directory without recursion")
else:
data = b""
static.tprint("Parsing " + path)
if path.endswith((".dat", ".bin", ".txt", ".pdb", ".PDB", ".cfg", ".h", ".c")):
with open(path, "rb") as f:
data = f.read()
if detect_ascii(path):
data = ascii2bin(data)
parse(data)
else:
static.tprint("Error: File extension not recognized")
def parse(data):
offset = 0
while offset < len(data):
if result.type == "amd":
if (result.amd_individual):
m = amd.microcode(data[offset : ], dict(), 0, result.swap_endian)
else:
m = amd.container(data[offset : ], result.swap_endian)
elif result.type == "intel":
m = intel.microcode(data[offset: ], result.swap_endian)
elif result.type == "via":
m = via.microcode(data[offset: ], result.swap_endian)
else:
raise Exception("Microcode format not specified")
if result.verbose:
print(m)
if result.report:
report(m)
if result.output:
output(m)
offset += m.size()
def output(m):
if result.type != "amd" or result.amd_individual:
filename = result.output + "/" + m.filename() + ".bin"
if not os.path.exists(result.output):
os.makedirs(result.output)
if not os.path.exists(filename):
with open(filename, "wb") as f:
static.tprint("Writing " + f.name)
f.write(m.raw)
else:
static.tprint("File " + filename + " already exists!")
else:
for microcode in m.microcodes:
filename = result.output + "/" + microcode.filename() + ".bin"
if not os.path.exists(result.output):
os.makedirs(result.output)
if not os.path.exists(filename):
with open(filename, "wb") as f:
static.tprint("Writing " + f.name)
f.write(microcode.raw)
else:
static.tprint("File " + filename + " already exists!")
def report(m):
with open("report.csv", "a") as f:
f.write(m.csv())
static.tprint("Updating report file")
def main():
global result
parser = argparse.ArgumentParser(description = "Microparse: AMD/Intel/VIA CPU microcode update parser")
parser.add_argument("-c", action = "store_true", dest = "amd_individual", default = False, help = "amd microcode is not in container (rare)")
parser.add_argument("-e", action = "store_true", dest = "swap_endian", default = False, help = "swap parsing endianess")
parser.add_argument("-o", action = "store", dest = "output", help = "output directory for segmented microcode")
parser.add_argument("-p", action = "store_true", dest = "report", default = False, help = "generate CSV report of all parsed microcode")
parser.add_argument("-r", action = "store_true", dest = "recursive", default = False, help = "recurse into directory")
parser.add_argument("-t", action = "store", dest = "type", choices = ["amd", "intel", "via"], help = "specify input format as amd, intel, or via microcode")
parser.add_argument("-v", action = "store_true", dest = "verbose", default = False, help = "verbose output")
parser.add_argument("target", action = "store", help = "input file or folder")
result = parser.parse_args()
open_path(result.target)
if __name__ == "__main__":
main()