forked from LineageOS/android_device_qcom_common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
releasetools.py
executable file
·382 lines (321 loc) · 11.2 KB
/
releasetools.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright (C) 2009 The Android Open Source Project
# Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Emit commands needed for QCOM devices during OTA installation
(installing the radio image)."""
import common
import re
bootImages = {}
binImages = {}
fwImages = {}
imgImages = {}
# Parse filesmap file containing firmware residing places
def LoadFilesMap(zip, name="RADIO/filesmap"):
try:
data = zip.read(name)
except KeyError:
print "Warning: could not find %s in %s." % (name, zip)
data = ""
d = {}
for line in data.split("\n"):
line = line.strip()
if not line or line.startswith("#"):
continue
pieces = line.split()
if not (len(pieces) == 2):
raise ValueError("malformed filesmap line: \"%s\"" % (line,))
d[pieces[0]] = pieces[1]
return d
# Read firmware images from target files zip
def GetRadioFiles(z):
out = {}
for info in z.infolist():
f = info.filename
if f.startswith("RADIO/") and (f.__len__() > len("RADIO/")):
fn = f[6:]
if fn.startswith("filesmap"):
continue
data = z.read(f)
out[fn] = common.File(f, data)
return out
# Get firmware residing place from filesmap
def GetFileDestination(fn, filesmap):
# if file is encoded disregard the .enc extention
if fn.endswith('.enc'):
fn = fn[:-4]
# get backup destination as well if present
backup = None
if fn + ".bak" in filesmap:
backup = filesmap[fn + ".bak"]
# If full filename is not specified in filesmap get only the name part
# and look for this token
if fn not in filesmap:
fn = fn.split(".")[0] + ".*"
if fn not in filesmap:
print "warning radio-update: '%s' not found in filesmap" % (fn)
return None, backup
return filesmap[fn], backup
# Separate image types as each type needs different handling
def SplitFwTypes(files):
boot = {}
bin = {}
fw = {}
img = {}
for f in files:
extIdx = -1
dotSeparated = f.split(".")
while True:
if dotSeparated[extIdx] != 'p' and dotSeparated[extIdx] != 'enc':
break
extIdx -= 1
if dotSeparated[extIdx] == 'mbn' or dotSeparated[extIdx] == 'elf':
boot[f] = files[f]
elif dotSeparated[extIdx] == 'bin':
bin[f] = files[f]
elif dotSeparated[extIdx] == 'img':
img[f] = files[f]
else:
fw[f] = files[f]
return boot, bin, fw, img
# Prepare radio-update files and verify them
def OTA_VerifyEnd(info, api_version, target_zip, source_zip=None):
if api_version < 3:
print "warning radio-update: no support for api_version less than 3"
return False
print "Loading radio filesmap..."
filesmap = LoadFilesMap(target_zip)
if filesmap == {}:
print "warning radio-update: no or invalid filesmap file found"
return False
print "Loading radio target..."
tgt_files = GetRadioFiles(target_zip)
if tgt_files == {}:
print "warning radio-update: no radio images in input target_files"
return False
src_files = None
if source_zip is not None:
print "Loading radio source..."
src_files = GetRadioFiles(source_zip)
update_list = {}
largest_source_size = 0
print "Preparing radio-update files..."
for fn in tgt_files:
dest, destBak = GetFileDestination(fn, filesmap)
if dest is None:
continue
tf = tgt_files[fn]
sf = None
if src_files is not None:
sf = src_files.get(fn, None)
full = sf is None or fn.endswith('.enc')
if not full:
# no difference - skip this file
if tf.sha1 == sf.sha1:
continue
d = common.Difference(tf, sf)
_, _, d = d.ComputePatch()
# no difference - skip this file
if d is None:
continue
# if patch is almost as big as the file - don't bother patching
full = len(d) > tf.size * common.OPTIONS.patch_threshold
if not full:
f = "patch/firmware-update/" + fn + ".p"
common.ZipWriteStr(info.output_zip, f, d)
update_list[f] = (dest, destBak, tf, sf)
largest_source_size = max(largest_source_size, sf.size)
if full:
f = "firmware-update/" + fn
common.ZipWriteStr(info.output_zip, f, tf.data)
update_list[f] = (dest, destBak, None, None)
global bootImages
global binImages
global fwImages
global imgImages
bootImages, binImages, fwImages, imgImages = SplitFwTypes(update_list)
# If there are incremental patches verify them
if largest_source_size != 0:
info.script.Comment("---- radio update verification ----")
info.script.Print("Verifying radio-update...")
for f in bootImages:
dest, destBak, tf, sf = bootImages[f]
# Not incremental
if sf is None:
continue
info.script.PatchCheck("EMMC:%s:%d:%s:%d:%s" %
(dest, sf.size, sf.sha1, tf.size, tf.sha1))
if destBak is not None:
info.script.PatchCheck("EMMC:%s:%d:%s:%d:%s" %
(destBak, sf.size, sf.sha1, tf.size, tf.sha1))
for f in binImages:
dest, destBak, tf, sf = binImages[f]
# Not incremental
if sf is None:
continue
info.script.PatchCheck("EMMC:%s:%d:%s:%d:%s" %
(dest, sf.size, sf.sha1, tf.size, tf.sha1))
last_mounted = ""
for f in fwImages:
dest, destBak, tf, sf = fwImages[f]
# Not incremental
if sf is None:
continue
# Get the filename without the path and the patch (.p) extention
f = f.split("/")[-1][:-2]
# Parse filesmap destination paths for "/dev/" pattern in the beginng.
# This would mean that the file must be written to block device -
# fs mount needed
if dest.startswith("/dev/"):
if last_mounted != dest:
info.script.AppendExtra('unmount("/firmware");')
info.script.AppendExtra('mount("vfat", "EMMC", "%s", "/firmware");' %
(dest))
last_mounted = dest
dest = "/firmware/image/" + f
else:
dest = dest + "/" + f
info.script.PatchCheck(dest, tf.sha1, sf.sha1)
for f in imgImages:
dest, tf, sf = imgImages[f]
# Not incremental
if sf is None:
continue
info.script.PatchCheck("EMMC:%s:%d:%s:%d:%s" %
(dest, sf.size, sf.sha1, tf.size, tf.sha1))
last_mounted = ""
info.script.CacheFreeSpaceCheck(largest_source_size)
return True
def FullOTA_Assertions(info):
#TODO: Implement device specific asserstions.
return
def IncrementalOTA_Assertions(info):
#TODO: Implement device specific asserstions.
return
def IncrementalOTA_VerifyEnd(info):
OTA_VerifyEnd(info, info.target_version, info.target_zip, info.source_zip)
return
# This function handles only non-HLOS whole partition images
def InstallRawImage(script, f, dest, tf, sf):
if f.endswith('.p'):
script.ApplyPatch("EMMC:%s:%d:%s:%d:%s" %
(dest, sf.size, sf.sha1, tf.size, tf.sha1),
"-", tf.size, tf.sha1, sf.sha1, f)
elif f.endswith('.enc'):
# Get the filename without the path
fn = f.split("/")[-1]
script.AppendExtra('package_extract_file("%s", "/tmp/%s");' % (f, fn))
script.AppendExtra('msm.decrypt("/tmp/%s", "%s");' % (fn, dest))
else:
script.AppendExtra('package_extract_file("%s", "%s");' % (f, dest))
return
# This function handles only non-HLOS boot images - files list must contain
# only such images (aboot, tz, etc)
def InstallBootImages(script, files):
bakExists = False
# update main partitions
script.AppendExtra('ifelse(msm.boot_update("main"), (')
for f in files:
dest, destBak, tf, sf = files[f]
if destBak is not None:
bakExists = True
InstallRawImage(script, f, dest, tf, sf)
script.AppendExtra('), "");')
# update backup partitions
if bakExists:
script.AppendExtra('ifelse(msm.boot_update("backup"), (')
for f in files:
dest, destBak, tf, sf = files[f]
if destBak is not None:
InstallRawImage(script, f, destBak, tf, sf)
script.AppendExtra('), "");')
# just finalize primary update stage
else:
script.AppendExtra('msm.boot_update("backup");')
# finalize partitions update
script.AppendExtra('msm.boot_update("finalize");')
return
# This function handles only non-HLOS bin images
def InstallBinImages(script, files):
for f in files:
dest, _, tf, sf = files[f]
InstallRawImage(script, f, dest, tf, sf)
return
# This function handles only non-HLOS firmware files that are not whole
# partition images (modem, dsp, etc)
def InstallFwImages(script, files):
last_mounted = ""
for f in files:
dest, _, tf, sf = files[f]
# Get the filename without the path
fn = f.split("/")[-1]
# Parse filesmap destination paths for "/dev/" pattern in the beginng.
# This would mean that the file must be written to block device -
# fs mount needed
if dest.startswith("/dev/"):
if last_mounted != dest:
script.AppendExtra('unmount("/firmware");')
script.AppendExtra('mount("vfat", "EMMC", "%s", "/firmware");' %
(dest))
last_mounted = dest
dest = "/firmware/image/" + fn
else:
dest = dest + "/" + fn
if f.endswith('.p'):
script.ApplyPatch(dest[:-2], "-", tf.size, tf.sha1, sf.sha1, f)
elif f.endswith('.enc'):
script.AppendExtra('package_extract_file("%s", "/tmp/%s");' % (f, fn))
script.AppendExtra('msm.decrypt("/tmp/%s", "%s");' % (fn, dest[:-4]))
else:
script.AppendExtra('package_extract_file("%s", "%s");' % (f, dest))
if last_mounted != "":
script.AppendExtra('unmount("/firmware");')
return
# This function handles *.img images
def InstallImgImages(script, files):
for f in files:
dest, _, tf, sf = files[f]
InstallRawImage(script, f, dest, tf, sf)
return
def OTA_InstallEnd(info):
print "Applying radio-update script modifications..."
info.script.Comment("---- radio update tasks ----")
info.script.Print("Patching firmware images...")
if bootImages != {}:
InstallBootImages(info.script, bootImages)
if binImages != {}:
InstallBinImages(info.script, binImages)
if fwImages != {}:
InstallFwImages(info.script, fwImages)
if imgImages != {}:
InstallImgImages(info.script, imgImages)
return
def FullOTA_InstallEnd_MMC(info):
if OTA_VerifyEnd(info, info.input_version, info.input_zip):
OTA_InstallEnd(info)
return
def FullOTA_InstallEnd_MTD(info):
print "warning radio-update: radio update for NAND devices not supported"
return
def FullOTA_InstallEnd(info):
FullOTA_InstallEnd_MMC(info)
return
def IncrementalOTA_InstallEnd_MMC(info):
OTA_InstallEnd(info)
return
def IncrementalOTA_InstallEnd_MTD(info):
print "warning radio-update: radio update for NAND devices not supported"
return
def IncrementalOTA_InstallEnd(info):
IncrementalOTA_InstallEnd_MMC(info)
return