-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.py
275 lines (227 loc) · 9.25 KB
/
image.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
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
#!/usr/bin/env python3
from os import path
import logging
from lib.bootloader import *
from lib.common import *
from lib.nand import NANDHeader, ImageType, OutputPath
logging.basicConfig()
LOGGER = logging.getLogger('image')
class Image(object):
"""
Stores structures from NAND data
Provides methods to identify structures from RAW data reads
"""
outputpath = None
imagetype = None
nandheader = None
kv = None
smc = None
smcconfig = None
sb = None
cb = None
sc = None
sd = None
cd = None
se = None
ce = None
cf = None
cg = None
cf1 = None
cg1 = None
kernel = None
hv = None
def getAvailableStructures(self):
# All variable members of this class that are not None
return {k: v for k, v in vars(Image).items() if
not callable(getattr(Image, k)) and not k.startswith('__') and v is not None}
# return vars(Image)['_data']
def identifyBL2(image):
"""
Identifies bootloaders given previous is available and proper seek
"""
try:
bl2header = None
offset = image.tell()
bl2header = BootloaderHeader(image.read(BootloaderHeader.HEADER_SIZE), currentoffset=offset)
except ValueError as e:
LOGGER.debug('Failed BL2 header check: ' + str(e))
except Exception as e:
raise
if bl2header:
try:
Image.cb = CB(image.read(bl2header.length), bl2header)
LOGGER.debug(bl2header)
except ValueError as e:
LOGGER.debug('Failed CB header check: ' + str(e))
except Exception as e:
raise
try:
image.seek(bl2header.offset, 0)
Image.sb = SB(image.read(bl2header.length), bl2header)
LOGGER.debug(bl2header)
except ValueError as e:
LOGGER.debug('Failed SB check: ' + str(e))
except Exception as e:
raise
def identifyCD(image):
# assert(Image.cb != None)
try:
bl4header = None
offset = image.tell()
bl4header = BootloaderHeader(image.read(BootloaderHeader.HEADER_SIZE), currentoffset=offset)
LOGGER.debug(bl4header)
except ValueError as e:
LOGGER.debug('Failed CD header check: ' + str(e))
except Exception as e:
raise
if bl4header:
try:
image.seek(bl4header.offset, 0)
Image.cd = CD(image.read(bl4header.length), bl4header)
except ValueError as e:
LOGGER.debug('Failed CD check: ' + str(e))
except Exception as e:
raise
def identifyCE(image):
# assert(Image.cd != None)
try:
bl5header = None
offset = image.tell()
bl5header = BootloaderHeader(image.read(BootloaderHeader.HEADER_SIZE), currentoffset=offset)
LOGGER.debug(bl5header)
except ValueError as e:
LOGGER.debug('Failed CE header check: ' + str(e))
except Exception as e:
raise
if bl5header:
try:
image.seek(bl5header.offset, 0)
Image.ce = CE(image.read(bl5header.length), bl5header)
except ValueError as e:
LOGGER.debug('Failed CE check: ' + str(e))
except Exception as e:
raise
def identifySC(image):
# assert(Image.sb != None)
try:
bl3header = None
offset = image.tell()
bl3header = BootloaderHeader(image.read(BootloaderHeader.HEADER_SIZE), currentoffset=offset)
LOGGER.debug(bl3header)
except ValueError as e:
LOGGER.debug('Failed SC header check: ' + str(e))
except Exception as e:
raise
if bl3header:
try:
image.seek(bl3header.offset, 0)
Image.sc = SC(image.read(bl3header.length), bl3header)
except ValueError as e:
LOGGER.debug('Failed SC check: ' + str(e))
except Exception as e:
raise
def identifySD(image):
# assert(Image.sc != None)
try:
bl4header = None
offset = image.tell()
bl4header = BootloaderHeader(image.read(BootloaderHeader.HEADER_SIZE), currentoffset=offset)
LOGGER.debug(bl4header)
except ValueError as e:
LOGGER.debug('Failed SD header check: ' + str(e))
except Exception as e:
raise
if bl4header:
try:
image.seek(bl4header.offset, 0)
Image.sd = SD(image.read(bl4header.length), bl4header)
except ValueError as e:
LOGGER.debug('Failed SD check: ' + str(e))
except Exception as e:
raise
def identifySE(image):
# assert(Image.sd != None)
try:
bl5header = None
offset = image.tell()
bl5header = BootloaderHeader(image.read(BootloaderHeader.HEADER_SIZE), currentoffset=offset)
LOGGER.debug(bl5header)
except ValueError as e:
LOGGER.debug('Failed SE header check: ' + str(e))
except Exception as e:
raise
if bl5header:
try:
image.seek(bl5header.offset, 0)
Image.se = SE(image.read(bl5header.length), bl5header)
except ValueError as e:
LOGGER.debug('Failed SE check: ' + str(e))
except Exception as e:
raise
def identifyAvailableStructures(target):
# Assume that the path is valid. There will be an unhandled exception if we try to write to a bad path
Image.outputpath = OutputPath(target)
# Is the target an existing file to analyze and manipulate or an output path?
if not path.isfile(target):
return
# If real file, begin populating available
with open(target, 'rb') as image:
# First check for the NAND header, if that exists, check if it is a full NAND / Shadowboot image
# If this fails, fall back to individual parts
# Check for NAND/Shadowboot header
try:
offset = image.tell()
Image.nandheader = NANDHeader(image.read(NANDHeader.HEADER_SIZE), currentoffset=offset)
except ValueError as e:
LOGGER.debug('Failed NAND header check: ' + str(e))
except Exception as e:
raise
# If valid header, check for subsequent parts
if Image.nandheader:
image.seek(Image.nandheader.bl2offset, 0)
# Identifies either SB or CB given an image at the proper seek
Image.identifyBL2(image)
# Identify retail NAND structures
if Image.cb:
Image.cb.updateKey()
Image.cb.decrypt()
image.seek(Image.cb.header.offset + Image.cb.header.length, 0)
Image.identifyCD(image)
if Image.cd:
Image.cd.updateKey(Image.cb.key)
Image.cd.decrypt()
image.seek(Image.cd.header.offset + Image.cd.header.length, 0)
Image.identifyCE(image)
if Image.ce:
Image.ce.updateKey(Image.cd.key)
Image.ce.decrypt()
# Set image type
# TODO This AND is redundent
if Image.nandheader and Image.cb and Image.cd and Image.ce:
Image.imagetype = ImageType.RETAIL
# Identify devkit NAND structures
elif Image.sb:
Image.sb.updateKey()
Image.sb.decrypt()
image.seek(Image.sb.header.offset + Image.sb.header.length, 0)
Image.identifySC(image)
if Image.sc:
Image.sc.updateKey(Constants.SECRET_ZERO)
Image.sc.decrypt()
image.seek(Image.sc.header.offset + Image.sc.header.length, 0)
Image.identifySD(image)
if Image.sd:
Image.sd.updateKey(Image.sc.key)
Image.sd.decrypt()
image.seek(Image.sd.header.offset + Image.sd.header.length, 0)
Image.identifySE(image)
if Image.se:
Image.se.updateKey(Image.sd.key)
Image.se.decrypt()
# TODO This AND is redundent
if Image.nandheader and Image.sb and Image.sc and Image.sd and Image.se:
# Determine devkit vs shadowboot by file size
Image.imagetype = ImageType.SHADOWBOOT if path.getsize(
target) <= Constants.SHADOWBOOT_SIZE else ImageType.DEVKIT
# TODO Fall back to individual structures
# image.seek(0, 0)