-
Notifications
You must be signed in to change notification settings - Fork 0
/
releasetools.py
279 lines (241 loc) · 8.57 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
# Copyright (C) 2009 The Android Open Source Project
# Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
# Copyright (C) 2019 The LineageOS Project
#
# 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.
import hashlib
import common
import re
bootImages = {}
binImages = {}
fwImages = {}
# 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):
# 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 = {}
for f in files:
extIdx = -1
dotSeparated = f.split(".")
if dotSeparated[extIdx] == 'mbn' or dotSeparated[extIdx] == 'elf' or dotSeparated[extIdx] == 'img':
boot[f] = files[f]
elif dotSeparated[extIdx] == 'bin':
bin[f] = files[f]
else:
fw[f] = files[f]
return boot, bin, fw
# 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
bootImages, binImages, fwImages = 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)
info.script.CacheFreeSpaceCheck(largest_source_size)
return True
# This function handles only non-HLOS whole partition images
def InstallRawImage(script, f, dest, tf, sf):
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):
for f in files:
dest, _, tf, sf = files[f]
InstallRawImage(script, f, dest, tf, sf)
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
def FullOTA_InstallEnd(info):
OTA_InstallEnd(info)
return
def IncrementalOTA_InstallEnd(info):
OTA_InstallEnd(info)
return
def FullOTA_Assertions(info):
AddTrustZoneAssertion(info, info.input_zip)
return
def IncrementalOTA_Assertions(info):
AddTrustZoneAssertion(info, info.target_zip)
return
def IncrementalOTA_VerifyEnd(info):
OTA_VerifyEnd(info, info.target_version, info.target_zip, info.source_zip)
return
def AddImage(info, basename, dest):
path = "IMAGES/" + basename
if path not in info.input_zip.namelist():
return
data = info.input_zip.read(path)
common.ZipWriteStr(info.output_zip, basename, data)
info.script.AppendExtra('package_extract_file("%s", "%s");' % (basename, dest))
def OTA_InstallEnd(info):
info.script.Print("Patching device-tree and verity images...")
AddImage(info, "dtbo.img", "/dev/block/bootdevice/by-name/dtbo")
AddImage(info, "vbmeta.img", "/dev/block/bootdevice/by-name/vbmeta")
if OTA_VerifyEnd(info, info.input_version, info.input_zip):
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)
return
def AddTrustZoneAssertion(info, input_zip):
android_info = info.input_zip.read("OTA/android-info.txt")
m = re.search(r'require\s+version-trustzone\s*=\s*(\S+)', android_info)
if m:
versions = m.group(1).split('|')
if len(versions) and '*' not in versions:
cmd = 'assert(lavender.verify_trustzone(' + ','.join(['"%s"' % tz for tz in versions]) + ') == "1");'
info.script.AppendExtra(cmd)
return