-
Notifications
You must be signed in to change notification settings - Fork 7
/
fd.py
67 lines (59 loc) · 1.99 KB
/
fd.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
"""
FD parser
"""
import os
from fv import FV
from raw import RAW
class FD(object):
def __init__(self, data, start, prefix='', full_dump=True):
self.start = start
self.prefix = prefix
self.data = data
self.size = len(data)
self.full_dump = full_dump
self.blocks = []
start_ncb = 0
cur_pos = 0
index = 0
if prefix.startswith('bios_'):
prefix = prefix[len('bios_'):]
while cur_pos < len(data):
cur_prefix = '%s%02d_' % (prefix, index)
if FV.check_sig(data, cur_pos):
if start_ncb < cur_pos:
ncb = RAW(data[start_ncb:cur_pos], start + start_ncb, cur_prefix)
self.blocks.append(ncb)
index += 1
start_ncb = cur_pos
continue
fv = FV(data[cur_pos:], start + cur_pos, cur_prefix)
self.blocks.append(fv)
index += 1
cur_pos += fv.size
start_ncb = cur_pos
else:
cur_pos += 8
if start_ncb < len(data):
cur_prefix = '%s%02d_' % (prefix, index)
ncb = RAW(data[start_ncb:], start + start_ncb, cur_prefix)
self.blocks.append(ncb)
index += 1
def __str__(self):
return '0x%08x+0x%08x: FD' % (self.start, self.size)
def showinfo(self, ts=' '):
for block in self.blocks:
print ts + str(block)
block.showinfo(ts + ' ')
def dump(self):
if self.full_dump:
fnprefix = '%s%08x' % (self.prefix, self.start)
fn = '%s.fd' % fnprefix
fn = os.path.normpath(fn)
print 'Dumping FD to %s' % fn
dn = os.path.dirname(fn)
if dn and not os.path.isdir(dn):
os.makedirs(dn)
with open(fn, 'wb') as fout:
fout.write(self.data)
for block in self.blocks:
block.dump()