-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_logimg.py
69 lines (53 loc) · 1.79 KB
/
parse_logimg.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
import struct
class Flags(object):
SEQUENCE = (
"flush", "fua", "read", "write", "sync",
"discard", "has_msg", "is_bio", "error", "has_data",
)
def __init__(self, flags):
self.flags = flags
def __getattr__(self, key):
try:
idx = self.SEQUENCE.index(key)
except ValueError:
raise AttributeError(key)
idxval = 1 << idx
return bool(self.flags & idxval)
def __str__(self):
s = []
for f in self.SEQUENCE:
s.append(f'{f}={getattr(self, f)}')
return ','.join(s)
__unicode__ = __repr__ = __str__
class Header(object):
def __init__(self, bytearr):
assert len(bytearr) == 512
unpacked = struct.unpack(f"<4Q{512-32}s", bytearr)
self.magic, self.sector, self.size, self.flags, self.msg = unpacked
self.flags = Flags(self.flags)
def __str__(self):
return f'sector={self.sector},size={self.size},flags=({self.flags})'
__unicode__ = __repr__ = __str__
class Superblock(object):
def __init__(self, bytearr):
assert len(bytearr) == 512
unpacked = struct.unpack(f"<4Q{512-32}s", bytearr)
self.magic, self.num_bios, self.last_sector, self.num_missing, junk = unpacked
def __str__(self):
return f'num_bios={self.num_bios},last_sector={self.last_sector},num_missing={self.num_missing}'
__unicode__ = __repr__ = __str__
logimg = []
with open("log.img", "rb") as fp:
sb = Superblock(fp.read(512))
i = 0
logimg.append(sb)
print(sb)
while i < sb.num_bios:
i += 1
data = fp.read(512)
header = Header(data)
logimg.append(header)
if header.size != 0:
# add data blocks
data = fp.read(header.size)
logimg.append(data)