-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpbootimg
executable file
·298 lines (237 loc) · 11.6 KB
/
dumpbootimg
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
import sys
from pkg_resources import parse_version
from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
if parse_version(ks_version) < parse_version('0.7'):
raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))
class AndroidImg(KaitaiStruct):
"""
.. seealso::
Source - https://source.android.com/devices/bootloader/boot-image-header
"""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.magic = self._io.ensure_fixed_contents(b"\x41\x4E\x44\x52\x4F\x49\x44\x21")
self.kernel = self._root.Load(self._io, self, self._root)
self.ramdisk = self._root.Load(self._io, self, self._root)
self.second = self._root.Load(self._io, self, self._root)
self.tags_load = self._io.read_u4le()
self.page_size = self._io.read_u4le()
self.header_version = self._io.read_u4le()
self.os_version = self._root.OsVersion(self._io, self, self._root)
self.name = (KaitaiStream.bytes_terminate(self._io.read_bytes(16), 0, False)).decode(u"ASCII")
self.cmdline = (KaitaiStream.bytes_terminate(self._io.read_bytes(512), 0, False)).decode(u"ASCII")
self.sha = self._io.read_bytes(32)
self.extra_cmdline = (KaitaiStream.bytes_terminate(self._io.read_bytes(1024), 0, False)).decode(u"ASCII")
if self.header_version > 0:
self.recovery_dtbo = self._root.SizeOffset(self._io, self, self._root)
if self.header_version > 0:
self.boot_header_size = self._io.read_u4le()
if self.header_version > 1:
self.dtb = self._root.LoadLong(self._io, self, self._root)
class Load(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.size = self._io.read_u4le()
self.addr = self._io.read_u4le()
class LoadLong(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.size = self._io.read_u4le()
self.addr = self._io.read_u8le()
class SizeOffset(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.size = self._io.read_u4le()
self.offset = self._io.read_u8le()
class OsVersion(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.version = self._io.read_u4le()
@property
def month(self):
if hasattr(self, '_m_month'):
return self._m_month if hasattr(self, '_m_month') else None
self._m_month = (self.version & 15)
return self._m_month if hasattr(self, '_m_month') else None
@property
def patch(self):
if hasattr(self, '_m_patch'):
return self._m_patch if hasattr(self, '_m_patch') else None
self._m_patch = ((self.version >> 11) & 127)
return self._m_patch if hasattr(self, '_m_patch') else None
@property
def year(self):
if hasattr(self, '_m_year'):
return self._m_year if hasattr(self, '_m_year') else None
self._m_year = (((self.version >> 4) & 127) + 2000)
return self._m_year if hasattr(self, '_m_year') else None
@property
def major(self):
if hasattr(self, '_m_major'):
return self._m_major if hasattr(self, '_m_major') else None
self._m_major = ((self.version >> 25) & 127)
return self._m_major if hasattr(self, '_m_major') else None
@property
def minor(self):
if hasattr(self, '_m_minor'):
return self._m_minor if hasattr(self, '_m_minor') else None
self._m_minor = ((self.version >> 18) & 127)
return self._m_minor if hasattr(self, '_m_minor') else None
@property
def kernel_img(self):
if hasattr(self, '_m_kernel_img'):
return self._m_kernel_img if hasattr(self, '_m_kernel_img') else None
_pos = self._io.pos()
self._io.seek(self.page_size)
self._m_kernel_img = self._io.read_bytes(self.kernel.size)
self._io.seek(_pos)
return self._m_kernel_img if hasattr(self, '_m_kernel_img') else None
@property
def tags_offset(self):
"""tags offset from base."""
if hasattr(self, '_m_tags_offset'):
return self._m_tags_offset if hasattr(self, '_m_tags_offset') else None
self._m_tags_offset = (self.tags_load - self.base)
return self._m_tags_offset if hasattr(self, '_m_tags_offset') else None
@property
def ramdisk_offset(self):
"""ramdisk offset from base."""
if hasattr(self, '_m_ramdisk_offset'):
return self._m_ramdisk_offset if hasattr(self, '_m_ramdisk_offset') else None
self._m_ramdisk_offset = ((self.ramdisk.addr - self.base) if self.ramdisk.addr > 0 else 0)
return self._m_ramdisk_offset if hasattr(self, '_m_ramdisk_offset') else None
@property
def second_offset(self):
"""2nd bootloader offset from base."""
if hasattr(self, '_m_second_offset'):
return self._m_second_offset if hasattr(self, '_m_second_offset') else None
self._m_second_offset = ((self.second.addr - self.base) if self.second.addr > 0 else 0)
return self._m_second_offset if hasattr(self, '_m_second_offset') else None
@property
def kernel_offset(self):
"""kernel offset from base."""
if hasattr(self, '_m_kernel_offset'):
return self._m_kernel_offset if hasattr(self, '_m_kernel_offset') else None
self._m_kernel_offset = (self.kernel.addr - self.base)
return self._m_kernel_offset if hasattr(self, '_m_kernel_offset') else None
@property
def dtb_offset(self):
"""dtb offset from base."""
if hasattr(self, '_m_dtb_offset'):
return self._m_dtb_offset if hasattr(self, '_m_dtb_offset') else None
if self.header_version > 1:
self._m_dtb_offset = ((self.dtb.addr - self.base) if self.dtb.addr > 0 else 0)
return self._m_dtb_offset if hasattr(self, '_m_dtb_offset') else None
@property
def dtb_img(self):
if hasattr(self, '_m_dtb_img'):
return self._m_dtb_img if hasattr(self, '_m_dtb_img') else None
if ((self.header_version > 1) and (self.dtb.size > 0)) :
_pos = self._io.pos()
self._io.seek((((((((self.page_size + self.kernel.size) + self.ramdisk.size) + self.second.size) + self.recovery_dtbo.size) + self.page_size) - 1) // self.page_size * self.page_size))
self._m_dtb_img = self._io.read_bytes(self.dtb.size)
self._io.seek(_pos)
return self._m_dtb_img if hasattr(self, '_m_dtb_img') else None
@property
def ramdisk_img(self):
if hasattr(self, '_m_ramdisk_img'):
return self._m_ramdisk_img if hasattr(self, '_m_ramdisk_img') else None
if self.ramdisk.size > 0:
_pos = self._io.pos()
self._io.seek(((((self.page_size + self.kernel.size) + self.page_size) - 1) // self.page_size * self.page_size))
self._m_ramdisk_img = self._io.read_bytes(self.ramdisk.size)
self._io.seek(_pos)
return self._m_ramdisk_img if hasattr(self, '_m_ramdisk_img') else None
@property
def recovery_dtbo_img(self):
if hasattr(self, '_m_recovery_dtbo_img'):
return self._m_recovery_dtbo_img if hasattr(self, '_m_recovery_dtbo_img') else None
if ((self.header_version > 0) and (self.recovery_dtbo.size > 0)) :
_pos = self._io.pos()
self._io.seek(self.recovery_dtbo.offset)
self._m_recovery_dtbo_img = self._io.read_bytes(self.recovery_dtbo.size)
self._io.seek(_pos)
return self._m_recovery_dtbo_img if hasattr(self, '_m_recovery_dtbo_img') else None
@property
def second_img(self):
if hasattr(self, '_m_second_img'):
return self._m_second_img if hasattr(self, '_m_second_img') else None
if self.second.size > 0:
_pos = self._io.pos()
self._io.seek((((((self.page_size + self.kernel.size) + self.ramdisk.size) + self.page_size) - 1) // self.page_size * self.page_size))
self._m_second_img = self._io.read_bytes(self.second.size)
self._io.seek(_pos)
return self._m_second_img if hasattr(self, '_m_second_img') else None
@property
def base(self):
"""base loading address."""
if hasattr(self, '_m_base'):
return self._m_base if hasattr(self, '_m_base') else None
self._m_base = (self.kernel.addr - 32768)
return self._m_base if hasattr(self, '_m_base') else None
if __name__ == '__main__':
if (len(sys.argv) < 2):
sys.stderr.write("Usage: {} <boot.img>\n".format(sys.argv[0]))
sys.exit(1)
try:
with open(sys.argv[1], 'rb') as fh:
img = AndroidImg(KaitaiStream(BytesIO(fh.read())))
print("header_version={}".format(img.header_version))
os_version = img.os_version
print("os_version={}.{}.{}".format(os_version.major, os_version.minor, os_version.patch))
print("os_patch_level={}-{:02d}".format(os_version.year, os_version.month))
if img.name:
print("board={:s}".format(img.name))
print("pagesize={}".format(img.page_size))
if img.cmdline:
print("cmdline={}".format(img.cmdline))
if img.extra_cmdline:
print("extra_cmdline={}".format(img.extra_cmdline))
print("base={:#010x}".format(img.base))
print("kernel_offset={:#010x}".format(img.kernel_offset))
print("ramdisk_offset={:#010x}".format(img.ramdisk_offset))
print("second_offset={:#010x}".format(img.second_offset))
print("tags_offset={:#010x}".format(img.tags_offset))
if img.dtb_offset:
print("dtb_offset={:#010x}".format(img.dtb_offset))
try:
if img.kernel_img:
with open('kernel', 'wb') as fh:
fh.write(img.kernel_img)
if img.ramdisk_img:
with open('ramdisk', 'wb') as fh:
fh.write(img.ramdisk_img)
if img.dtb_img:
with open('dtb', 'wb') as fh:
fh.write(img.dtb_img)
if img.recovery_dtbo_img:
with open('recovery_dtbo', 'wb') as fh:
fh.write(img.recovery_dtbo_img)
if img.second_img:
with open('second_img', 'wb') as fh:
fh.write(img.second_img)
except IOError as e:
sys.stderr.write("Can't open file: {}\n".format(str(e)))
except IOError:
sys.stderr.write("Can't open file {}\n".format(sys.argv[1]))