-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoeminfo.py
106 lines (87 loc) · 3.63 KB
/
oeminfo.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
from enum import Enum
from pathlib import Path
from typing import Union, List
from struct import unpack, pack
from argparse import ArgumentParser
MAGIC = b'OEM_INFO'
class Operation(Enum):
OINV_OP_MIN = 0
OEMINFO_WRITE = 1
OEMINFO_READ = 2
OEMINFO_GETAGE = 3
OEMINFO_GETINFO = 4
OEMINFO_ERASE = 5
class OemInfoEntry:
def __init__(self, header: bytes, version: int, id: int,
type: int, length: int, age: int, data: bytes, start: int):
self.header = header
self.version = version
self.id = id
self.type = type
self.length = length
self.age = age
self.data = data
self.start = start
assert len(data) == length, "Data length mismatch."
def __str__(self):
return "%d-%d-%d-%d-0x%x" % (self.version, self.id,
self.type, self.age, self.start)
@classmethod
def from_bytes(cls, data: bytes, offset: int = 0):
(header, version, id, type,
length, age) = unpack('<8sIIIII', data[offset:offset+28])
return cls(header, version, id, type, length, age,
data[offset+0x200:offset+0x200+length], offset)
class OemInfoImage:
def __init__(self, image : Union[bytes, Path]):
if isinstance(image, Path):
with open(image, 'rb') as fp:
self.image = bytearray(fp.read())
else:
self.image = image
self.entries: List[OemInfoEntry] = []
self.parse_entries()
def parse_entries(self):
offset = self.image.find(MAGIC)
while offset != -1:
self.entries.append(
OemInfoEntry.from_bytes(self.image, offset))
offset = self.image.find(MAGIC, offset + 1)
def extract_entries(self, output: Path):
Path(output).mkdir(parents=True, exist_ok=True)
for entry in self.entries:
with open("%s/%s.bin" %
(output, str(entry)), 'wb') as fp:
fp.write(entry.data)
print("Extracted %d entries to '%s'." %
(len(self.entries), output))
def repack_entries(self, input: Path, output: Path):
for entry in self.entries:
with open("%s/%s.bin" %
(input, str(entry)), 'rb') as fp:
self.image[entry.start+0x200:
entry.start+0x200+entry.length] = fp.read()
with open(output, 'wb') as fp:
fp.write(self.image)
print("Repacked %d entries to '%s'." %
(len(self.entries), output))
def main():
parser = ArgumentParser()
subparsers = parser.add_subparsers(dest='action')
parser_extract = subparsers.add_parser('extract', help='Extract entries.')
parser_extract.add_argument('image', help='Path to the oeminfo image.', type=Path)
parser_extract.add_argument('-o', '--output', help='Output path.', default='output', type=Path)
parser_extract.add_argument('-p', '--print', help='Print entries.', action='store_true')
parser_repack = subparsers.add_parser('repack', help='Repack entries.')
parser_repack.add_argument('image', help='Path to the oeminfo image.', type=Path)
parser_repack.add_argument('input', help='Path to the extracted folder.', type=Path)
parser_repack.add_argument('-o', '--output', help='Output file.', default='oeminfo.pack', type=Path)
args = parser.parse_args()
image = OemInfoImage(args.image)
if args.action == 'extract':
image.extract_entries(args.output)
elif args.action == 'repack':
image.repack_entries(args.input,
args.output)
if __name__ == '__main__':
main()