blob: fea335e73eb0f94731f1e6128623021c3cca6278 [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Doug Zongkerea5d7a92010-09-12 15:26:16 -070015import copy
Doug Zongker8ce7c252009-05-22 13:34:54 -070016import errno
Doug Zongkereef39442009-04-02 12:14:19 -070017import getopt
18import getpass
Doug Zongker05d3dea2009-06-22 11:32:31 -070019import imp
Doug Zongkereef39442009-04-02 12:14:19 -070020import os
Ying Wang7e6d4e42010-12-13 16:25:36 -080021import platform
Doug Zongkereef39442009-04-02 12:14:19 -070022import re
T.R. Fullhart37e10522013-03-18 10:31:26 -070023import shlex
Doug Zongkereef39442009-04-02 12:14:19 -070024import shutil
25import subprocess
26import sys
27import tempfile
Doug Zongkerea5d7a92010-09-12 15:26:16 -070028import threading
29import time
Doug Zongker048e7ca2009-06-15 14:31:53 -070030import zipfile
Doug Zongkereef39442009-04-02 12:14:19 -070031
Doug Zongker55d93282011-01-25 17:03:34 -080032try:
davidcad0bb92011-03-15 14:21:38 +000033 from hashlib import sha1 as sha1
Doug Zongker55d93282011-01-25 17:03:34 -080034except ImportError:
davidcad0bb92011-03-15 14:21:38 +000035 from sha import sha as sha1
Doug Zongker55d93282011-01-25 17:03:34 -080036
Doug Zongkereef39442009-04-02 12:14:19 -070037# missing in Python 2.4 and before
38if not hasattr(os, "SEEK_SET"):
39 os.SEEK_SET = 0
40
41class Options(object): pass
42OPTIONS = Options()
Doug Zongker602a84e2009-06-18 08:35:12 -070043OPTIONS.search_path = "out/host/linux-x86"
T.R. Fullhart37e10522013-03-18 10:31:26 -070044OPTIONS.signapk_path = "framework/signapk.jar" # Relative to search_path
45OPTIONS.extra_signapk_args = []
46OPTIONS.java_path = "java" # Use the one on the path by default.
47OPTIONS.public_key_suffix = ".x509.pem"
48OPTIONS.private_key_suffix = ".pk8"
Doug Zongkereef39442009-04-02 12:14:19 -070049OPTIONS.verbose = False
50OPTIONS.tempfiles = []
Doug Zongker05d3dea2009-06-22 11:32:31 -070051OPTIONS.device_specific = None
Doug Zongker8bec09e2009-11-30 15:37:14 -080052OPTIONS.extras = {}
Doug Zongkerc77a9ad2010-09-16 11:28:43 -070053OPTIONS.info_dict = None
Doug Zongkereef39442009-04-02 12:14:19 -070054
Doug Zongkerf6a53aa2009-12-15 15:06:55 -080055
56# Values for "certificate" in apkcerts that mean special things.
57SPECIAL_CERT_STRINGS = ("PRESIGNED", "EXTERNAL")
58
59
Doug Zongkereef39442009-04-02 12:14:19 -070060class ExternalError(RuntimeError): pass
61
62
63def Run(args, **kwargs):
64 """Create and return a subprocess.Popen object, printing the command
65 line on the terminal if -v was specified."""
66 if OPTIONS.verbose:
67 print " running: ", " ".join(args)
68 return subprocess.Popen(args, **kwargs)
69
70
Ying Wang7e6d4e42010-12-13 16:25:36 -080071def CloseInheritedPipes():
72 """ Gmake in MAC OS has file descriptor (PIPE) leak. We close those fds
73 before doing other work."""
74 if platform.system() != "Darwin":
75 return
76 for d in range(3, 1025):
77 try:
78 stat = os.fstat(d)
79 if stat is not None:
80 pipebit = stat[0] & 0x1000
81 if pipebit != 0:
82 os.close(d)
83 except OSError:
84 pass
85
86
Doug Zongkerc9253822014-02-04 12:17:58 -080087def LoadInfoDict(input):
Doug Zongkerc19a8d52010-07-01 15:30:11 -070088 """Read and parse the META/misc_info.txt key/value pairs from the
89 input target files and return a dict."""
90
Doug Zongkerc9253822014-02-04 12:17:58 -080091 def read_helper(fn):
92 if isinstance(input, zipfile.ZipFile):
93 return input.read(fn)
94 else:
95 path = os.path.join(input, *fn.split("/"))
96 try:
97 with open(path) as f:
98 return f.read()
99 except IOError, e:
100 if e.errno == errno.ENOENT:
101 raise KeyError(fn)
Doug Zongkerc19a8d52010-07-01 15:30:11 -0700102 d = {}
103 try:
Michael Runge6e836112014-04-15 17:40:21 -0700104 d = LoadDictionaryFromLines(read_helper("META/misc_info.txt").split("\n"))
Doug Zongker37974732010-09-16 17:44:38 -0700105 except KeyError:
106 # ok if misc_info.txt doesn't exist
107 pass
Doug Zongkerc19a8d52010-07-01 15:30:11 -0700108
Doug Zongker37974732010-09-16 17:44:38 -0700109 # backwards compatibility: These values used to be in their own
110 # files. Look for them, in case we're processing an old
111 # target_files zip.
112
113 if "mkyaffs2_extra_flags" not in d:
114 try:
Doug Zongkerc9253822014-02-04 12:17:58 -0800115 d["mkyaffs2_extra_flags"] = read_helper("META/mkyaffs2-extra-flags.txt").strip()
Doug Zongker37974732010-09-16 17:44:38 -0700116 except KeyError:
117 # ok if flags don't exist
118 pass
119
120 if "recovery_api_version" not in d:
121 try:
Doug Zongkerc9253822014-02-04 12:17:58 -0800122 d["recovery_api_version"] = read_helper("META/recovery-api-version.txt").strip()
Doug Zongker37974732010-09-16 17:44:38 -0700123 except KeyError:
124 raise ValueError("can't find recovery API version in input target-files")
125
126 if "tool_extensions" not in d:
127 try:
Doug Zongkerc9253822014-02-04 12:17:58 -0800128 d["tool_extensions"] = read_helper("META/tool-extensions.txt").strip()
Doug Zongker37974732010-09-16 17:44:38 -0700129 except KeyError:
130 # ok if extensions don't exist
131 pass
132
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800133 if "fstab_version" not in d:
134 d["fstab_version"] = "1"
135
Doug Zongker37974732010-09-16 17:44:38 -0700136 try:
Doug Zongkerc9253822014-02-04 12:17:58 -0800137 data = read_helper("META/imagesizes.txt")
Doug Zongker37974732010-09-16 17:44:38 -0700138 for line in data.split("\n"):
139 if not line: continue
Doug Zongker1684d9c2010-09-17 07:44:38 -0700140 name, value = line.split(" ", 1)
141 if not value: continue
Doug Zongker37974732010-09-16 17:44:38 -0700142 if name == "blocksize":
143 d[name] = value
144 else:
145 d[name + "_size"] = value
146 except KeyError:
147 pass
148
149 def makeint(key):
150 if key in d:
151 d[key] = int(d[key], 0)
152
153 makeint("recovery_api_version")
154 makeint("blocksize")
155 makeint("system_size")
156 makeint("userdata_size")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700157 makeint("cache_size")
Doug Zongker37974732010-09-16 17:44:38 -0700158 makeint("recovery_size")
159 makeint("boot_size")
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800160 makeint("fstab_version")
Doug Zongkerc19a8d52010-07-01 15:30:11 -0700161
Doug Zongkerc9253822014-02-04 12:17:58 -0800162 d["fstab"] = LoadRecoveryFSTab(read_helper, d["fstab_version"])
163 d["build.prop"] = LoadBuildProp(read_helper)
Doug Zongker1eb74dd2012-08-16 16:19:00 -0700164 return d
165
Doug Zongkerc9253822014-02-04 12:17:58 -0800166def LoadBuildProp(read_helper):
Doug Zongker1eb74dd2012-08-16 16:19:00 -0700167 try:
Doug Zongkerc9253822014-02-04 12:17:58 -0800168 data = read_helper("SYSTEM/build.prop")
Doug Zongker1eb74dd2012-08-16 16:19:00 -0700169 except KeyError:
170 print "Warning: could not find SYSTEM/build.prop in %s" % zip
171 data = ""
Michael Runge6e836112014-04-15 17:40:21 -0700172 return LoadDictionaryFromLines(data.split("\n"))
Doug Zongker1eb74dd2012-08-16 16:19:00 -0700173
Michael Runge6e836112014-04-15 17:40:21 -0700174def LoadDictionaryFromLines(lines):
Doug Zongker1eb74dd2012-08-16 16:19:00 -0700175 d = {}
Michael Runge6e836112014-04-15 17:40:21 -0700176 for line in lines:
Doug Zongker1eb74dd2012-08-16 16:19:00 -0700177 line = line.strip()
178 if not line or line.startswith("#"): continue
Ying Wang114b46f2014-04-15 11:24:00 -0700179 if "=" in line:
180 name, value = line.split("=", 1)
181 d[name] = value
Doug Zongkerc19a8d52010-07-01 15:30:11 -0700182 return d
183
Doug Zongkerc9253822014-02-04 12:17:58 -0800184def LoadRecoveryFSTab(read_helper, fstab_version):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700185 class Partition(object):
186 pass
187
188 try:
Doug Zongkerc9253822014-02-04 12:17:58 -0800189 data = read_helper("RECOVERY/RAMDISK/etc/recovery.fstab")
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700190 except KeyError:
Doug Zongkerc9253822014-02-04 12:17:58 -0800191 print "Warning: could not find RECOVERY/RAMDISK/etc/recovery.fstab"
Jeff Davidson033fbe22011-10-26 18:08:09 -0700192 data = ""
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700193
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800194 if fstab_version == 1:
195 d = {}
196 for line in data.split("\n"):
197 line = line.strip()
198 if not line or line.startswith("#"): continue
199 pieces = line.split()
200 if not (3 <= len(pieces) <= 4):
201 raise ValueError("malformed recovery.fstab line: \"%s\"" % (line,))
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700202
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800203 p = Partition()
204 p.mount_point = pieces[0]
205 p.fs_type = pieces[1]
206 p.device = pieces[2]
207 p.length = 0
208 options = None
209 if len(pieces) >= 4:
210 if pieces[3].startswith("/"):
211 p.device2 = pieces[3]
212 if len(pieces) >= 5:
213 options = pieces[4]
214 else:
215 p.device2 = None
216 options = pieces[3]
Doug Zongker086cbb02011-02-17 15:54:20 -0800217 else:
218 p.device2 = None
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700219
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800220 if options:
221 options = options.split(",")
222 for i in options:
223 if i.startswith("length="):
224 p.length = int(i[7:])
225 else:
226 print "%s: unknown option \"%s\"" % (p.mount_point, i)
227
228 d[p.mount_point] = p
229
230 elif fstab_version == 2:
231 d = {}
232 for line in data.split("\n"):
233 line = line.strip()
234 if not line or line.startswith("#"): continue
235 pieces = line.split()
236 if len(pieces) != 5:
237 raise ValueError("malformed recovery.fstab line: \"%s\"" % (line,))
238
239 # Ignore entries that are managed by vold
240 options = pieces[4]
241 if "voldmanaged=" in options: continue
242
243 # It's a good line, parse it
244 p = Partition()
245 p.device = pieces[0]
246 p.mount_point = pieces[1]
247 p.fs_type = pieces[2]
248 p.device2 = None
249 p.length = 0
250
Doug Zongker086cbb02011-02-17 15:54:20 -0800251 options = options.split(",")
252 for i in options:
253 if i.startswith("length="):
254 p.length = int(i[7:])
255 else:
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800256 # Ignore all unknown options in the unified fstab
257 continue
Doug Zongker086cbb02011-02-17 15:54:20 -0800258
Ken Sumrall3b07cf12013-02-19 17:35:29 -0800259 d[p.mount_point] = p
260
261 else:
262 raise ValueError("Unknown fstab_version: \"%d\"" % (fstab_version,))
263
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700264 return d
265
266
Doug Zongker37974732010-09-16 17:44:38 -0700267def DumpInfoDict(d):
268 for k, v in sorted(d.items()):
269 print "%-25s = (%s) %s" % (k, type(v).__name__, v)
Doug Zongkerc19a8d52010-07-01 15:30:11 -0700270
Doug Zongkerd5131602012-08-02 14:46:42 -0700271def BuildBootableImage(sourcedir, fs_config_file, info_dict=None):
Doug Zongkereef39442009-04-02 12:14:19 -0700272 """Take a kernel, cmdline, and ramdisk directory from the input (in
Doug Zongkere1c31ba2009-06-23 17:40:35 -0700273 'sourcedir'), and turn them into a boot image. Return the image
274 data, or None if sourcedir does not appear to contains files for
275 building the requested image."""
276
277 if (not os.access(os.path.join(sourcedir, "RAMDISK"), os.F_OK) or
278 not os.access(os.path.join(sourcedir, "kernel"), os.F_OK)):
279 return None
Doug Zongkereef39442009-04-02 12:14:19 -0700280
Doug Zongkerd5131602012-08-02 14:46:42 -0700281 if info_dict is None:
282 info_dict = OPTIONS.info_dict
283
Doug Zongkereef39442009-04-02 12:14:19 -0700284 ramdisk_img = tempfile.NamedTemporaryFile()
285 img = tempfile.NamedTemporaryFile()
286
Doug Zongkerfffe1d52012-05-03 16:15:29 -0700287 if os.access(fs_config_file, os.F_OK):
288 cmd = ["mkbootfs", "-f", fs_config_file, os.path.join(sourcedir, "RAMDISK")]
289 else:
290 cmd = ["mkbootfs", os.path.join(sourcedir, "RAMDISK")]
291 p1 = Run(cmd, stdout=subprocess.PIPE)
Doug Zongker32da27a2009-05-29 09:35:56 -0700292 p2 = Run(["minigzip"],
293 stdin=p1.stdout, stdout=ramdisk_img.file.fileno())
Doug Zongkereef39442009-04-02 12:14:19 -0700294
295 p2.wait()
296 p1.wait()
297 assert p1.returncode == 0, "mkbootfs of %s ramdisk failed" % (targetname,)
Doug Zongker32da27a2009-05-29 09:35:56 -0700298 assert p2.returncode == 0, "minigzip of %s ramdisk failed" % (targetname,)
Doug Zongkereef39442009-04-02 12:14:19 -0700299
Bjorn Andersson612e2cd2012-11-25 16:53:44 -0800300 # use MKBOOTIMG from environ, or "mkbootimg" if empty or not set
301 mkbootimg = os.getenv('MKBOOTIMG') or "mkbootimg"
302
303 cmd = [mkbootimg, "--kernel", os.path.join(sourcedir, "kernel")]
Doug Zongker38a649f2009-06-17 09:07:09 -0700304
Doug Zongker171f1cd2009-06-15 22:36:37 -0700305 fn = os.path.join(sourcedir, "cmdline")
306 if os.access(fn, os.F_OK):
Doug Zongker38a649f2009-06-17 09:07:09 -0700307 cmd.append("--cmdline")
308 cmd.append(open(fn).read().rstrip("\n"))
309
310 fn = os.path.join(sourcedir, "base")
311 if os.access(fn, os.F_OK):
312 cmd.append("--base")
313 cmd.append(open(fn).read().rstrip("\n"))
314
Ying Wang4de6b5b2010-08-25 14:29:34 -0700315 fn = os.path.join(sourcedir, "pagesize")
316 if os.access(fn, os.F_OK):
317 cmd.append("--pagesize")
318 cmd.append(open(fn).read().rstrip("\n"))
319
Doug Zongkerd5131602012-08-02 14:46:42 -0700320 args = info_dict.get("mkbootimg_args", None)
321 if args and args.strip():
Jianxun Zhang09849492013-04-17 15:19:19 -0700322 cmd.extend(shlex.split(args))
Doug Zongkerd5131602012-08-02 14:46:42 -0700323
Doug Zongker38a649f2009-06-17 09:07:09 -0700324 cmd.extend(["--ramdisk", ramdisk_img.name,
325 "--output", img.name])
326
327 p = Run(cmd, stdout=subprocess.PIPE)
Doug Zongkereef39442009-04-02 12:14:19 -0700328 p.communicate()
Doug Zongkere1c31ba2009-06-23 17:40:35 -0700329 assert p.returncode == 0, "mkbootimg of %s image failed" % (
330 os.path.basename(sourcedir),)
Doug Zongkereef39442009-04-02 12:14:19 -0700331
332 img.seek(os.SEEK_SET, 0)
333 data = img.read()
334
335 ramdisk_img.close()
336 img.close()
337
338 return data
339
340
Doug Zongkerd5131602012-08-02 14:46:42 -0700341def GetBootableImage(name, prebuilt_name, unpack_dir, tree_subdir,
342 info_dict=None):
Doug Zongker55d93282011-01-25 17:03:34 -0800343 """Return a File object (with name 'name') with the desired bootable
344 image. Look for it in 'unpack_dir'/BOOTABLE_IMAGES under the name
345 'prebuilt_name', otherwise construct it from the source files in
346 'unpack_dir'/'tree_subdir'."""
Doug Zongkereef39442009-04-02 12:14:19 -0700347
Doug Zongker55d93282011-01-25 17:03:34 -0800348 prebuilt_path = os.path.join(unpack_dir, "BOOTABLE_IMAGES", prebuilt_name)
349 if os.path.exists(prebuilt_path):
350 print "using prebuilt %s..." % (prebuilt_name,)
351 return File.FromLocalFile(name, prebuilt_path)
352 else:
353 print "building image from target_files %s..." % (tree_subdir,)
Doug Zongkerfffe1d52012-05-03 16:15:29 -0700354 fs_config = "META/" + tree_subdir.lower() + "_filesystem_config.txt"
Ying Wangd89ffa82014-02-05 11:28:51 -0800355 data = BuildBootableImage(os.path.join(unpack_dir, tree_subdir),
356 os.path.join(unpack_dir, fs_config),
Ying Wang64a55ba2014-02-05 12:15:06 -0800357 info_dict)
Ying Wangd89ffa82014-02-05 11:28:51 -0800358 if data:
359 return File(name, data)
360 return None
Doug Zongker55d93282011-01-25 17:03:34 -0800361
Doug Zongkereef39442009-04-02 12:14:19 -0700362
Doug Zongker75f17362009-12-08 13:46:44 -0800363def UnzipTemp(filename, pattern=None):
Doug Zongker55d93282011-01-25 17:03:34 -0800364 """Unzip the given archive into a temporary directory and return the name.
365
366 If filename is of the form "foo.zip+bar.zip", unzip foo.zip into a
367 temp dir, then unzip bar.zip into that_dir/BOOTABLE_IMAGES.
368
369 Returns (tempdir, zipobj) where zipobj is a zipfile.ZipFile (of the
370 main file), open for reading.
371 """
Doug Zongkereef39442009-04-02 12:14:19 -0700372
373 tmp = tempfile.mkdtemp(prefix="targetfiles-")
374 OPTIONS.tempfiles.append(tmp)
Doug Zongker55d93282011-01-25 17:03:34 -0800375
376 def unzip_to_dir(filename, dirname):
377 cmd = ["unzip", "-o", "-q", filename, "-d", dirname]
378 if pattern is not None:
379 cmd.append(pattern)
380 p = Run(cmd, stdout=subprocess.PIPE)
381 p.communicate()
382 if p.returncode != 0:
383 raise ExternalError("failed to unzip input target-files \"%s\"" %
384 (filename,))
385
386 m = re.match(r"^(.*[.]zip)\+(.*[.]zip)$", filename, re.IGNORECASE)
387 if m:
388 unzip_to_dir(m.group(1), tmp)
389 unzip_to_dir(m.group(2), os.path.join(tmp, "BOOTABLE_IMAGES"))
390 filename = m.group(1)
391 else:
392 unzip_to_dir(filename, tmp)
393
394 return tmp, zipfile.ZipFile(filename, "r")
Doug Zongkereef39442009-04-02 12:14:19 -0700395
396
397def GetKeyPasswords(keylist):
398 """Given a list of keys, prompt the user to enter passwords for
399 those which require them. Return a {key: password} dict. password
400 will be None if the key has no password."""
401
Doug Zongker8ce7c252009-05-22 13:34:54 -0700402 no_passwords = []
403 need_passwords = []
T.R. Fullhart37e10522013-03-18 10:31:26 -0700404 key_passwords = {}
Doug Zongkereef39442009-04-02 12:14:19 -0700405 devnull = open("/dev/null", "w+b")
406 for k in sorted(keylist):
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800407 # We don't need a password for things that aren't really keys.
408 if k in SPECIAL_CERT_STRINGS:
Doug Zongker8ce7c252009-05-22 13:34:54 -0700409 no_passwords.append(k)
Doug Zongker43874f82009-04-14 14:05:15 -0700410 continue
411
T.R. Fullhart37e10522013-03-18 10:31:26 -0700412 p = Run(["openssl", "pkcs8", "-in", k+OPTIONS.private_key_suffix,
Doug Zongker602a84e2009-06-18 08:35:12 -0700413 "-inform", "DER", "-nocrypt"],
414 stdin=devnull.fileno(),
415 stdout=devnull.fileno(),
416 stderr=subprocess.STDOUT)
Doug Zongkereef39442009-04-02 12:14:19 -0700417 p.communicate()
418 if p.returncode == 0:
T.R. Fullhart37e10522013-03-18 10:31:26 -0700419 # Definitely an unencrypted key.
Doug Zongker8ce7c252009-05-22 13:34:54 -0700420 no_passwords.append(k)
Doug Zongkereef39442009-04-02 12:14:19 -0700421 else:
T.R. Fullhart37e10522013-03-18 10:31:26 -0700422 p = Run(["openssl", "pkcs8", "-in", k+OPTIONS.private_key_suffix,
423 "-inform", "DER", "-passin", "pass:"],
424 stdin=devnull.fileno(),
425 stdout=devnull.fileno(),
426 stderr=subprocess.PIPE)
427 stdout, stderr = p.communicate()
428 if p.returncode == 0:
429 # Encrypted key with empty string as password.
430 key_passwords[k] = ''
431 elif stderr.startswith('Error decrypting key'):
432 # Definitely encrypted key.
433 # It would have said "Error reading key" if it didn't parse correctly.
434 need_passwords.append(k)
435 else:
436 # Potentially, a type of key that openssl doesn't understand.
437 # We'll let the routines in signapk.jar handle it.
438 no_passwords.append(k)
Doug Zongkereef39442009-04-02 12:14:19 -0700439 devnull.close()
Doug Zongker8ce7c252009-05-22 13:34:54 -0700440
T.R. Fullhart37e10522013-03-18 10:31:26 -0700441 key_passwords.update(PasswordManager().GetPasswords(need_passwords))
Doug Zongker8ce7c252009-05-22 13:34:54 -0700442 key_passwords.update(dict.fromkeys(no_passwords, None))
Doug Zongkereef39442009-04-02 12:14:19 -0700443 return key_passwords
444
445
Doug Zongker951495f2009-08-14 12:44:19 -0700446def SignFile(input_name, output_name, key, password, align=None,
447 whole_file=False):
Doug Zongkereef39442009-04-02 12:14:19 -0700448 """Sign the input_name zip/jar/apk, producing output_name. Use the
449 given key and password (the latter may be None if the key does not
450 have a password.
451
452 If align is an integer > 1, zipalign is run to align stored files in
453 the output zip on 'align'-byte boundaries.
Doug Zongker951495f2009-08-14 12:44:19 -0700454
455 If whole_file is true, use the "-w" option to SignApk to embed a
456 signature that covers the whole file in the archive comment of the
457 zip file.
Doug Zongkereef39442009-04-02 12:14:19 -0700458 """
Doug Zongker951495f2009-08-14 12:44:19 -0700459
Doug Zongkereef39442009-04-02 12:14:19 -0700460 if align == 0 or align == 1:
461 align = None
462
463 if align:
464 temp = tempfile.NamedTemporaryFile()
465 sign_name = temp.name
466 else:
467 sign_name = output_name
468
T.R. Fullhart37e10522013-03-18 10:31:26 -0700469 cmd = [OPTIONS.java_path, "-Xmx2048m", "-jar",
470 os.path.join(OPTIONS.search_path, OPTIONS.signapk_path)]
471 cmd.extend(OPTIONS.extra_signapk_args)
Doug Zongker951495f2009-08-14 12:44:19 -0700472 if whole_file:
473 cmd.append("-w")
T.R. Fullhart37e10522013-03-18 10:31:26 -0700474 cmd.extend([key + OPTIONS.public_key_suffix,
475 key + OPTIONS.private_key_suffix,
Doug Zongker951495f2009-08-14 12:44:19 -0700476 input_name, sign_name])
477
478 p = Run(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Doug Zongkereef39442009-04-02 12:14:19 -0700479 if password is not None:
480 password += "\n"
481 p.communicate(password)
482 if p.returncode != 0:
483 raise ExternalError("signapk.jar failed: return code %s" % (p.returncode,))
484
485 if align:
Doug Zongker602a84e2009-06-18 08:35:12 -0700486 p = Run(["zipalign", "-f", str(align), sign_name, output_name])
Doug Zongkereef39442009-04-02 12:14:19 -0700487 p.communicate()
488 if p.returncode != 0:
489 raise ExternalError("zipalign failed: return code %s" % (p.returncode,))
490 temp.close()
491
492
Doug Zongker37974732010-09-16 17:44:38 -0700493def CheckSize(data, target, info_dict):
Doug Zongkereef39442009-04-02 12:14:19 -0700494 """Check the data string passed against the max size limit, if
495 any, for the given target. Raise exception if the data is too big.
496 Print a warning if the data is nearing the maximum size."""
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700497
Doug Zongker1684d9c2010-09-17 07:44:38 -0700498 if target.endswith(".img"): target = target[:-4]
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700499 mount_point = "/" + target
500
501 if info_dict["fstab"]:
502 if mount_point == "/userdata": mount_point = "/data"
503 p = info_dict["fstab"][mount_point]
504 fs_type = p.fs_type
Andrew Boie0f9aec82012-02-14 09:32:52 -0800505 device = p.device
506 if "/" in device:
507 device = device[device.rfind("/")+1:]
508 limit = info_dict.get(device + "_size", None)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700509 if not fs_type or not limit: return
Doug Zongkereef39442009-04-02 12:14:19 -0700510
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700511 if fs_type == "yaffs2":
512 # image size should be increased by 1/64th to account for the
513 # spare area (64 bytes per 2k page)
514 limit = limit / 2048 * (2048+64)
Andrew Boie0f9aec82012-02-14 09:32:52 -0800515 size = len(data)
516 pct = float(size) * 100.0 / limit
517 msg = "%s size (%d) is %.2f%% of limit (%d)" % (target, size, pct, limit)
518 if pct >= 99.0:
519 raise ExternalError(msg)
520 elif pct >= 95.0:
521 print
522 print " WARNING: ", msg
523 print
524 elif OPTIONS.verbose:
525 print " ", msg
Doug Zongkereef39442009-04-02 12:14:19 -0700526
527
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800528def ReadApkCerts(tf_zip):
529 """Given a target_files ZipFile, parse the META/apkcerts.txt file
530 and return a {package: cert} dict."""
531 certmap = {}
532 for line in tf_zip.read("META/apkcerts.txt").split("\n"):
533 line = line.strip()
534 if not line: continue
535 m = re.match(r'^name="(.*)"\s+certificate="(.*)"\s+'
536 r'private_key="(.*)"$', line)
537 if m:
538 name, cert, privkey = m.groups()
T.R. Fullhart37e10522013-03-18 10:31:26 -0700539 public_key_suffix_len = len(OPTIONS.public_key_suffix)
540 private_key_suffix_len = len(OPTIONS.private_key_suffix)
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800541 if cert in SPECIAL_CERT_STRINGS and not privkey:
542 certmap[name] = cert
T.R. Fullhart37e10522013-03-18 10:31:26 -0700543 elif (cert.endswith(OPTIONS.public_key_suffix) and
544 privkey.endswith(OPTIONS.private_key_suffix) and
545 cert[:-public_key_suffix_len] == privkey[:-private_key_suffix_len]):
546 certmap[name] = cert[:-public_key_suffix_len]
Doug Zongkerf6a53aa2009-12-15 15:06:55 -0800547 else:
548 raise ValueError("failed to parse line from apkcerts.txt:\n" + line)
549 return certmap
550
551
Doug Zongkereef39442009-04-02 12:14:19 -0700552COMMON_DOCSTRING = """
553 -p (--path) <dir>
Doug Zongker602a84e2009-06-18 08:35:12 -0700554 Prepend <dir>/bin to the list of places to search for binaries
555 run by this script, and expect to find jars in <dir>/framework.
Doug Zongkereef39442009-04-02 12:14:19 -0700556
Doug Zongker05d3dea2009-06-22 11:32:31 -0700557 -s (--device_specific) <file>
558 Path to the python module containing device-specific
559 releasetools code.
560
Doug Zongker8bec09e2009-11-30 15:37:14 -0800561 -x (--extra) <key=value>
562 Add a key/value pair to the 'extras' dict, which device-specific
563 extension code may look at.
564
Doug Zongkereef39442009-04-02 12:14:19 -0700565 -v (--verbose)
566 Show command lines being executed.
567
568 -h (--help)
569 Display this usage message and exit.
570"""
571
572def Usage(docstring):
573 print docstring.rstrip("\n")
574 print COMMON_DOCSTRING
575
576
577def ParseOptions(argv,
578 docstring,
579 extra_opts="", extra_long_opts=(),
580 extra_option_handler=None):
581 """Parse the options in argv and return any arguments that aren't
582 flags. docstring is the calling module's docstring, to be displayed
583 for errors and -h. extra_opts and extra_long_opts are for flags
584 defined by the caller, which are processed by passing them to
585 extra_option_handler."""
586
587 try:
588 opts, args = getopt.getopt(
Doug Zongker8bec09e2009-11-30 15:37:14 -0800589 argv, "hvp:s:x:" + extra_opts,
T.R. Fullhart37e10522013-03-18 10:31:26 -0700590 ["help", "verbose", "path=", "signapk_path=", "extra_signapk_args=",
591 "java_path=", "public_key_suffix=", "private_key_suffix=",
592 "device_specific=", "extra="] +
593 list(extra_long_opts))
Doug Zongkereef39442009-04-02 12:14:19 -0700594 except getopt.GetoptError, err:
595 Usage(docstring)
596 print "**", str(err), "**"
597 sys.exit(2)
598
599 path_specified = False
600
601 for o, a in opts:
602 if o in ("-h", "--help"):
603 Usage(docstring)
604 sys.exit()
605 elif o in ("-v", "--verbose"):
606 OPTIONS.verbose = True
607 elif o in ("-p", "--path"):
Doug Zongker602a84e2009-06-18 08:35:12 -0700608 OPTIONS.search_path = a
T.R. Fullhart37e10522013-03-18 10:31:26 -0700609 elif o in ("--signapk_path",):
610 OPTIONS.signapk_path = a
611 elif o in ("--extra_signapk_args",):
612 OPTIONS.extra_signapk_args = shlex.split(a)
613 elif o in ("--java_path",):
614 OPTIONS.java_path = a
615 elif o in ("--public_key_suffix",):
616 OPTIONS.public_key_suffix = a
617 elif o in ("--private_key_suffix",):
618 OPTIONS.private_key_suffix = a
Doug Zongker05d3dea2009-06-22 11:32:31 -0700619 elif o in ("-s", "--device_specific"):
620 OPTIONS.device_specific = a
Doug Zongker5ecba702009-12-03 16:36:20 -0800621 elif o in ("-x", "--extra"):
Doug Zongker8bec09e2009-11-30 15:37:14 -0800622 key, value = a.split("=", 1)
623 OPTIONS.extras[key] = value
Doug Zongkereef39442009-04-02 12:14:19 -0700624 else:
625 if extra_option_handler is None or not extra_option_handler(o, a):
626 assert False, "unknown option \"%s\"" % (o,)
627
Doug Zongker602a84e2009-06-18 08:35:12 -0700628 os.environ["PATH"] = (os.path.join(OPTIONS.search_path, "bin") +
629 os.pathsep + os.environ["PATH"])
Doug Zongkereef39442009-04-02 12:14:19 -0700630
631 return args
632
633
634def Cleanup():
635 for i in OPTIONS.tempfiles:
636 if os.path.isdir(i):
637 shutil.rmtree(i)
638 else:
639 os.remove(i)
Doug Zongker8ce7c252009-05-22 13:34:54 -0700640
641
642class PasswordManager(object):
643 def __init__(self):
644 self.editor = os.getenv("EDITOR", None)
645 self.pwfile = os.getenv("ANDROID_PW_FILE", None)
646
647 def GetPasswords(self, items):
648 """Get passwords corresponding to each string in 'items',
649 returning a dict. (The dict may have keys in addition to the
650 values in 'items'.)
651
652 Uses the passwords in $ANDROID_PW_FILE if available, letting the
653 user edit that file to add more needed passwords. If no editor is
654 available, or $ANDROID_PW_FILE isn't define, prompts the user
655 interactively in the ordinary way.
656 """
657
658 current = self.ReadFile()
659
660 first = True
661 while True:
662 missing = []
663 for i in items:
664 if i not in current or not current[i]:
665 missing.append(i)
666 # Are all the passwords already in the file?
667 if not missing: return current
668
669 for i in missing:
670 current[i] = ""
671
672 if not first:
673 print "key file %s still missing some passwords." % (self.pwfile,)
674 answer = raw_input("try to edit again? [y]> ").strip()
675 if answer and answer[0] not in 'yY':
676 raise RuntimeError("key passwords unavailable")
677 first = False
678
679 current = self.UpdateAndReadFile(current)
680
681 def PromptResult(self, current):
682 """Prompt the user to enter a value (password) for each key in
683 'current' whose value is fales. Returns a new dict with all the
684 values.
685 """
686 result = {}
687 for k, v in sorted(current.iteritems()):
688 if v:
689 result[k] = v
690 else:
691 while True:
692 result[k] = getpass.getpass("Enter password for %s key> "
693 % (k,)).strip()
694 if result[k]: break
695 return result
696
697 def UpdateAndReadFile(self, current):
698 if not self.editor or not self.pwfile:
699 return self.PromptResult(current)
700
701 f = open(self.pwfile, "w")
702 os.chmod(self.pwfile, 0600)
703 f.write("# Enter key passwords between the [[[ ]]] brackets.\n")
704 f.write("# (Additional spaces are harmless.)\n\n")
705
706 first_line = None
707 sorted = [(not v, k, v) for (k, v) in current.iteritems()]
708 sorted.sort()
709 for i, (_, k, v) in enumerate(sorted):
710 f.write("[[[ %s ]]] %s\n" % (v, k))
711 if not v and first_line is None:
712 # position cursor on first line with no password.
713 first_line = i + 4
714 f.close()
715
716 p = Run([self.editor, "+%d" % (first_line,), self.pwfile])
717 _, _ = p.communicate()
718
719 return self.ReadFile()
720
721 def ReadFile(self):
722 result = {}
723 if self.pwfile is None: return result
724 try:
725 f = open(self.pwfile, "r")
726 for line in f:
727 line = line.strip()
728 if not line or line[0] == '#': continue
729 m = re.match(r"^\[\[\[\s*(.*?)\s*\]\]\]\s*(\S+)$", line)
730 if not m:
731 print "failed to parse password file: ", line
732 else:
733 result[m.group(2)] = m.group(1)
734 f.close()
735 except IOError, e:
736 if e.errno != errno.ENOENT:
737 print "error reading password file: ", str(e)
738 return result
Doug Zongker048e7ca2009-06-15 14:31:53 -0700739
740
Geremy Condra36bd3652014-02-06 19:45:10 -0800741def ZipWriteStr(zip, filename, data, perms=0644, compression=None):
Doug Zongker048e7ca2009-06-15 14:31:53 -0700742 # use a fixed timestamp so the output is repeatable.
743 zinfo = zipfile.ZipInfo(filename=filename,
744 date_time=(2009, 1, 1, 0, 0, 0))
Geremy Condra36bd3652014-02-06 19:45:10 -0800745 if compression is None:
746 zinfo.compress_type = zip.compression
747 else:
748 zinfo.compress_type = compression
Doug Zongker048e7ca2009-06-15 14:31:53 -0700749 zinfo.external_attr = perms << 16
750 zip.writestr(zinfo, data)
Doug Zongker05d3dea2009-06-22 11:32:31 -0700751
752
753class DeviceSpecificParams(object):
754 module = None
755 def __init__(self, **kwargs):
756 """Keyword arguments to the constructor become attributes of this
757 object, which is passed to all functions in the device-specific
758 module."""
759 for k, v in kwargs.iteritems():
760 setattr(self, k, v)
Doug Zongker8bec09e2009-11-30 15:37:14 -0800761 self.extras = OPTIONS.extras
Doug Zongker05d3dea2009-06-22 11:32:31 -0700762
763 if self.module is None:
764 path = OPTIONS.device_specific
Doug Zongkerc18736b2009-09-30 09:20:32 -0700765 if not path: return
Doug Zongker8e2f2b92009-06-24 14:34:57 -0700766 try:
767 if os.path.isdir(path):
768 info = imp.find_module("releasetools", [path])
769 else:
770 d, f = os.path.split(path)
771 b, x = os.path.splitext(f)
772 if x == ".py":
773 f = b
774 info = imp.find_module(f, [d])
Doug Zongkereb0a78a2014-01-27 10:01:06 -0800775 print "loaded device-specific extensions from", path
Doug Zongker8e2f2b92009-06-24 14:34:57 -0700776 self.module = imp.load_module("device_specific", *info)
777 except ImportError:
778 print "unable to load device-specific module; assuming none"
Doug Zongker05d3dea2009-06-22 11:32:31 -0700779
780 def _DoCall(self, function_name, *args, **kwargs):
781 """Call the named function in the device-specific module, passing
782 the given args and kwargs. The first argument to the call will be
783 the DeviceSpecific object itself. If there is no module, or the
784 module does not define the function, return the value of the
785 'default' kwarg (which itself defaults to None)."""
786 if self.module is None or not hasattr(self.module, function_name):
787 return kwargs.get("default", None)
788 return getattr(self.module, function_name)(*((self,) + args), **kwargs)
789
790 def FullOTA_Assertions(self):
791 """Called after emitting the block of assertions at the top of a
792 full OTA package. Implementations can add whatever additional
793 assertions they like."""
794 return self._DoCall("FullOTA_Assertions")
795
Doug Zongkere5ff5902012-01-17 10:55:37 -0800796 def FullOTA_InstallBegin(self):
797 """Called at the start of full OTA installation."""
798 return self._DoCall("FullOTA_InstallBegin")
799
Doug Zongker05d3dea2009-06-22 11:32:31 -0700800 def FullOTA_InstallEnd(self):
801 """Called at the end of full OTA installation; typically this is
802 used to install the image for the device's baseband processor."""
803 return self._DoCall("FullOTA_InstallEnd")
804
805 def IncrementalOTA_Assertions(self):
806 """Called after emitting the block of assertions at the top of an
807 incremental OTA package. Implementations can add whatever
808 additional assertions they like."""
809 return self._DoCall("IncrementalOTA_Assertions")
810
Doug Zongkere5ff5902012-01-17 10:55:37 -0800811 def IncrementalOTA_VerifyBegin(self):
812 """Called at the start of the verification phase of incremental
813 OTA installation; additional checks can be placed here to abort
814 the script before any changes are made."""
815 return self._DoCall("IncrementalOTA_VerifyBegin")
816
Doug Zongker05d3dea2009-06-22 11:32:31 -0700817 def IncrementalOTA_VerifyEnd(self):
818 """Called at the end of the verification phase of incremental OTA
819 installation; additional checks can be placed here to abort the
820 script before any changes are made."""
821 return self._DoCall("IncrementalOTA_VerifyEnd")
822
Doug Zongkere5ff5902012-01-17 10:55:37 -0800823 def IncrementalOTA_InstallBegin(self):
824 """Called at the start of incremental OTA installation (after
825 verification is complete)."""
826 return self._DoCall("IncrementalOTA_InstallBegin")
827
Doug Zongker05d3dea2009-06-22 11:32:31 -0700828 def IncrementalOTA_InstallEnd(self):
829 """Called at the end of incremental OTA installation; typically
830 this is used to install the image for the device's baseband
831 processor."""
832 return self._DoCall("IncrementalOTA_InstallEnd")
Doug Zongkerea5d7a92010-09-12 15:26:16 -0700833
834class File(object):
835 def __init__(self, name, data):
836 self.name = name
837 self.data = data
838 self.size = len(data)
Doug Zongker55d93282011-01-25 17:03:34 -0800839 self.sha1 = sha1(data).hexdigest()
840
841 @classmethod
842 def FromLocalFile(cls, name, diskname):
843 f = open(diskname, "rb")
844 data = f.read()
845 f.close()
846 return File(name, data)
Doug Zongkerea5d7a92010-09-12 15:26:16 -0700847
848 def WriteToTemp(self):
849 t = tempfile.NamedTemporaryFile()
850 t.write(self.data)
851 t.flush()
852 return t
853
Geremy Condra36bd3652014-02-06 19:45:10 -0800854 def AddToZip(self, z, compression=None):
855 ZipWriteStr(z, self.name, self.data, compression=compression)
Doug Zongkerea5d7a92010-09-12 15:26:16 -0700856
857DIFF_PROGRAM_BY_EXT = {
858 ".gz" : "imgdiff",
859 ".zip" : ["imgdiff", "-z"],
860 ".jar" : ["imgdiff", "-z"],
861 ".apk" : ["imgdiff", "-z"],
862 ".img" : "imgdiff",
863 }
864
865class Difference(object):
Doug Zongker24cd2802012-08-14 16:36:15 -0700866 def __init__(self, tf, sf, diff_program=None):
Doug Zongkerea5d7a92010-09-12 15:26:16 -0700867 self.tf = tf
868 self.sf = sf
869 self.patch = None
Doug Zongker24cd2802012-08-14 16:36:15 -0700870 self.diff_program = diff_program
Doug Zongkerea5d7a92010-09-12 15:26:16 -0700871
872 def ComputePatch(self):
873 """Compute the patch (as a string of data) needed to turn sf into
874 tf. Returns the same tuple as GetPatch()."""
875
876 tf = self.tf
877 sf = self.sf
878
Doug Zongker24cd2802012-08-14 16:36:15 -0700879 if self.diff_program:
880 diff_program = self.diff_program
881 else:
882 ext = os.path.splitext(tf.name)[1]
883 diff_program = DIFF_PROGRAM_BY_EXT.get(ext, "bsdiff")
Doug Zongkerea5d7a92010-09-12 15:26:16 -0700884
885 ttemp = tf.WriteToTemp()
886 stemp = sf.WriteToTemp()
887
888 ext = os.path.splitext(tf.name)[1]
889
890 try:
891 ptemp = tempfile.NamedTemporaryFile()
892 if isinstance(diff_program, list):
893 cmd = copy.copy(diff_program)
894 else:
895 cmd = [diff_program]
896 cmd.append(stemp.name)
897 cmd.append(ttemp.name)
898 cmd.append(ptemp.name)
899 p = Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
900 _, err = p.communicate()
901 if err or p.returncode != 0:
902 print "WARNING: failure running %s:\n%s\n" % (diff_program, err)
903 return None
904 diff = ptemp.read()
905 finally:
906 ptemp.close()
907 stemp.close()
908 ttemp.close()
909
910 self.patch = diff
911 return self.tf, self.sf, self.patch
912
913
914 def GetPatch(self):
915 """Return a tuple (target_file, source_file, patch_data).
916 patch_data may be None if ComputePatch hasn't been called, or if
917 computing the patch failed."""
918 return self.tf, self.sf, self.patch
919
920
921def ComputeDifferences(diffs):
922 """Call ComputePatch on all the Difference objects in 'diffs'."""
923 print len(diffs), "diffs to compute"
924
925 # Do the largest files first, to try and reduce the long-pole effect.
926 by_size = [(i.tf.size, i) for i in diffs]
927 by_size.sort(reverse=True)
928 by_size = [i[1] for i in by_size]
929
930 lock = threading.Lock()
931 diff_iter = iter(by_size) # accessed under lock
932
933 def worker():
934 try:
935 lock.acquire()
936 for d in diff_iter:
937 lock.release()
938 start = time.time()
939 d.ComputePatch()
940 dur = time.time() - start
941 lock.acquire()
942
943 tf, sf, patch = d.GetPatch()
944 if sf.name == tf.name:
945 name = tf.name
946 else:
947 name = "%s (%s)" % (tf.name, sf.name)
948 if patch is None:
949 print "patching failed! %s" % (name,)
950 else:
951 print "%8.2f sec %8d / %8d bytes (%6.2f%%) %s" % (
952 dur, len(patch), tf.size, 100.0 * len(patch) / tf.size, name)
953 lock.release()
954 except Exception, e:
955 print e
956 raise
957
958 # start worker threads; wait for them all to finish.
959 threads = [threading.Thread(target=worker)
960 for i in range(OPTIONS.worker_threads)]
961 for th in threads:
962 th.start()
963 while threads:
964 threads.pop().join()
Doug Zongker96a57e72010-09-26 14:57:41 -0700965
966
967# map recovery.fstab's fs_types to mount/format "partition types"
968PARTITION_TYPES = { "yaffs2": "MTD", "mtd": "MTD",
969 "ext4": "EMMC", "emmc": "EMMC" }
970
971def GetTypeAndDevice(mount_point, info):
972 fstab = info["fstab"]
973 if fstab:
974 return PARTITION_TYPES[fstab[mount_point].fs_type], fstab[mount_point].device
975 else:
Ying Wanga73b6562011-03-03 21:52:08 -0800976 return None
Baligh Uddinbeb6afd2013-11-13 00:22:34 +0000977
978
979def ParseCertificate(data):
980 """Parse a PEM-format certificate."""
981 cert = []
982 save = False
983 for line in data.split("\n"):
984 if "--END CERTIFICATE--" in line:
985 break
986 if save:
987 cert.append(line)
988 if "--BEGIN CERTIFICATE--" in line:
989 save = True
990 cert = "".join(cert).decode('base64')
991 return cert
Doug Zongkerc9253822014-02-04 12:17:58 -0800992
Geremy Condra36bd3652014-02-06 19:45:10 -0800993def XDelta3(source_path, target_path, output_path):
994 diff_program = ["xdelta3", "-0", "-B", str(64<<20), "-e", "-f", "-s"]
995 diff_program.append(source_path)
996 diff_program.append(target_path)
997 diff_program.append(output_path)
998 p = Run(diff_program, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
999 p.communicate()
1000 assert p.returncode == 0, "Couldn't produce patch"
1001
1002def XZ(path):
1003 compress_program = ["xz", "-zk", "-9", "--check=crc32"]
1004 compress_program.append(path)
1005 p = Run(compress_program, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
1006 p.communicate()
1007 assert p.returncode == 0, "Couldn't compress patch"
1008
1009def MakeSystemPatch(source_file, target_file):
1010 with tempfile.NamedTemporaryFile() as output_file:
1011 XDelta3(source_file.name, target_file.name, output_file.name)
1012 XZ(output_file.name)
1013 with open(output_file.name + ".xz") as patch_file:
1014 patch_data = patch_file.read()
1015 os.unlink(patch_file.name)
Doug Zongker5fad2032014-02-24 08:13:45 -08001016 return File("system.muimg.p", patch_data)
Doug Zongkerc9253822014-02-04 12:17:58 -08001017
Doug Zongker412c02f2014-02-13 10:58:24 -08001018def MakeRecoveryPatch(input_dir, output_sink, recovery_img, boot_img,
1019 info_dict=None):
Doug Zongkerc9253822014-02-04 12:17:58 -08001020 """Generate a binary patch that creates the recovery image starting
1021 with the boot image. (Most of the space in these images is just the
1022 kernel, which is identical for the two, so the resulting patch
1023 should be efficient.) Add it to the output zip, along with a shell
1024 script that is run from init.rc on first boot to actually do the
1025 patching and install the new recovery image.
1026
1027 recovery_img and boot_img should be File objects for the
1028 corresponding images. info should be the dictionary returned by
1029 common.LoadInfoDict() on the input target_files.
1030 """
1031
Doug Zongker412c02f2014-02-13 10:58:24 -08001032 if info_dict is None:
1033 info_dict = OPTIONS.info_dict
1034
Doug Zongkerc9253822014-02-04 12:17:58 -08001035 diff_program = ["imgdiff"]
1036 path = os.path.join(input_dir, "SYSTEM", "etc", "recovery-resource.dat")
1037 if os.path.exists(path):
1038 diff_program.append("-b")
1039 diff_program.append(path)
1040 bonus_args = "-b /system/etc/recovery-resource.dat"
1041 else:
1042 bonus_args = ""
1043
1044 d = Difference(recovery_img, boot_img, diff_program=diff_program)
1045 _, _, patch = d.ComputePatch()
1046 output_sink("recovery-from-boot.p", patch)
1047
Doug Zongker412c02f2014-02-13 10:58:24 -08001048 boot_type, boot_device = GetTypeAndDevice("/boot", info_dict)
1049 recovery_type, recovery_device = GetTypeAndDevice("/recovery", info_dict)
Doug Zongkerc9253822014-02-04 12:17:58 -08001050
1051 sh = """#!/system/bin/sh
1052if ! applypatch -c %(recovery_type)s:%(recovery_device)s:%(recovery_size)d:%(recovery_sha1)s; then
1053 applypatch %(bonus_args)s %(boot_type)s:%(boot_device)s:%(boot_size)d:%(boot_sha1)s %(recovery_type)s:%(recovery_device)s %(recovery_sha1)s %(recovery_size)d %(boot_sha1)s:/system/recovery-from-boot.p && log -t recovery "Installing new recovery image: succeeded" || log -t recovery "Installing new recovery image: failed"
1054else
1055 log -t recovery "Recovery image already installed"
1056fi
1057""" % { 'boot_size': boot_img.size,
1058 'boot_sha1': boot_img.sha1,
1059 'recovery_size': recovery_img.size,
1060 'recovery_sha1': recovery_img.sha1,
1061 'boot_type': boot_type,
1062 'boot_device': boot_device,
1063 'recovery_type': recovery_type,
1064 'recovery_device': recovery_device,
1065 'bonus_args': bonus_args,
1066 }
1067
1068 # The install script location moved from /system/etc to /system/bin
1069 # in the L release. Parse the init.rc file to find out where the
1070 # target-files expects it to be, and put it there.
1071 sh_location = "etc/install-recovery.sh"
1072 try:
1073 with open(os.path.join(input_dir, "BOOT", "RAMDISK", "init.rc")) as f:
1074 for line in f:
1075 m = re.match("^service flash_recovery /system/(\S+)\s*$", line)
1076 if m:
1077 sh_location = m.group(1)
1078 print "putting script in", sh_location
1079 break
1080 except (OSError, IOError), e:
1081 print "failed to read init.rc: %s" % (e,)
1082
1083 output_sink(sh_location, sh)