blob: a8826857965093fddc7bcc6a18e4043aa8b6d75f [file] [log] [blame]
Doug Zongker3c84f562014-07-31 11:06:30 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Given a target-files zipfile that does not contain images (ie, does
19not have an IMAGES/ top-level subdirectory), produce the images and
20add them to the zipfile.
21
Tianjie Xub48589a2016-08-03 19:21:52 -070022Usage: add_img_to_target_files [flag] target_files
23
24 -a (--add_missing)
25 Build and add missing images to "IMAGES/". If this option is
26 not specified, this script will simply exit when "IMAGES/"
27 directory exists in the target file.
28
29 -r (--rebuild_recovery)
30 Rebuild the recovery patch and write it to the system image. Only
31 meaningful when system image needs to be rebuilt.
32
33 --replace_verity_private_key
34 Replace the private key used for verity signing. (same as the option
35 in sign_target_files_apks)
36
37 --replace_verity_public_key
38 Replace the certificate (public key) used for verity verification. (same
39 as the option in sign_target_files_apks)
40
41 --is_signing
42 Skip building & adding the images for "userdata" and "cache" if we
43 are signing the target files.
Doug Zongker3c84f562014-07-31 11:06:30 -070044"""
45
Tao Bao89fbb0f2017-01-10 10:47:58 -080046from __future__ import print_function
47
Doug Zongker3c84f562014-07-31 11:06:30 -070048import sys
49
50if sys.hexversion < 0x02070000:
Tao Bao89fbb0f2017-01-10 10:47:58 -080051 print("Python 2.7 or newer is required.", file=sys.stderr)
Doug Zongker3c84f562014-07-31 11:06:30 -070052 sys.exit(1)
53
Tao Bao822f5842015-09-30 16:01:14 -070054import datetime
Tao Bao2b6dfd62017-09-27 17:17:43 -070055import hashlib
Doug Zongker3c84f562014-07-31 11:06:30 -070056import os
David Zeuthend995f4b2016-01-29 16:59:17 -050057import shlex
Ying Wang2a048392015-06-25 13:56:53 -070058import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050059import subprocess
Doug Zongker3c84f562014-07-31 11:06:30 -070060import tempfile
Tao Baod86e3112017-09-22 15:45:33 -070061import uuid
Doug Zongker3c84f562014-07-31 11:06:30 -070062import zipfile
63
Doug Zongker3c84f562014-07-31 11:06:30 -070064import build_image
65import common
Tianjie Xuf1a13182017-01-19 17:39:30 -080066import rangelib
Tianjie Xucfa86222016-03-07 16:31:19 -080067import sparse_img
Doug Zongker3c84f562014-07-31 11:06:30 -070068
69OPTIONS = common.OPTIONS
70
Michael Runge2e0d8fc2014-11-13 21:41:08 -080071OPTIONS.add_missing = False
72OPTIONS.rebuild_recovery = False
Tianjie Xu9ac4cb02017-06-09 16:58:03 -070073OPTIONS.replace_updated_files_list = []
Baligh Uddin59f4ff12015-09-16 21:20:30 -070074OPTIONS.replace_verity_public_key = False
75OPTIONS.replace_verity_private_key = False
Tianjie Xub48589a2016-08-03 19:21:52 -070076OPTIONS.is_signing = False
Doug Zongker3c84f562014-07-31 11:06:30 -070077
Dan Willemsen2ee00d52017-03-05 19:51:56 -080078
79class OutputFile(object):
80 def __init__(self, output_zip, input_dir, prefix, name):
81 self._output_zip = output_zip
82 self.input_name = os.path.join(input_dir, prefix, name)
83
84 if self._output_zip:
85 self._zip_name = os.path.join(prefix, name)
86
87 root, suffix = os.path.splitext(name)
88 self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix)
89 else:
90 self.name = self.input_name
91
92 def Write(self):
93 if self._output_zip:
94 common.ZipWrite(self._output_zip, self.name, self._zip_name)
95
96
Tianjie Xucfa86222016-03-07 16:31:19 -080097def GetCareMap(which, imgname):
98 """Generate care_map of system (or vendor) partition"""
99
100 assert which in ("system", "vendor")
Tianjie Xucfa86222016-03-07 16:31:19 -0800101
102 simg = sparse_img.SparseImage(imgname)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700103 care_map_list = [which]
Tianjie Xuf1a13182017-01-19 17:39:30 -0800104
105 care_map_ranges = simg.care_map
106 key = which + "_adjusted_partition_size"
107 adjusted_blocks = OPTIONS.info_dict.get(key)
108 if adjusted_blocks:
109 assert adjusted_blocks > 0, "blocks should be positive for " + which
110 care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet(
111 "0-%d" % (adjusted_blocks,)))
112
113 care_map_list.append(care_map_ranges.to_string_raw())
Tianjie Xucfa86222016-03-07 16:31:19 -0800114 return care_map_list
115
116
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800117def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700118 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -0500119 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800120
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800121 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img")
122 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800123 print("system.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800124 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800125
126 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -0700127 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
128 ofile.write(data)
129 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800130
Tianjie Xu38af07f2017-05-25 17:38:53 -0700131 arc_name = "SYSTEM/" + fn
132 if arc_name in output_zip.namelist():
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700133 OPTIONS.replace_updated_files_list.append(arc_name)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700134 else:
135 common.ZipWrite(output_zip, ofile.name, arc_name)
136
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800137 if OPTIONS.rebuild_recovery:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800138 print("Building new recovery patch")
Dan Albert8b72aef2015-03-23 19:13:21 -0700139 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
140 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800141
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800142 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map")
143 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img,
144 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -0500145
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800146 return img.name
Doug Zongkerfc44a512014-08-26 13:10:25 -0700147
148
Alex Light4e358ab2016-06-16 14:47:10 -0700149def AddSystemOther(output_zip, prefix="IMAGES/"):
150 """Turn the contents of SYSTEM_OTHER into a system_other image
151 and store it in output_zip."""
152
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800153 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img")
154 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800155 print("system_other.img already exists in %s, no need to rebuild..." % (
156 prefix,))
Alex Light4e358ab2016-06-16 14:47:10 -0700157 return
158
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800159 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img)
Alex Light4e358ab2016-06-16 14:47:10 -0700160
161
Doug Zongkerfc44a512014-08-26 13:10:25 -0700162def AddVendor(output_zip, prefix="IMAGES/"):
163 """Turn the contents of VENDOR into a vendor image and store in it
164 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800165
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800166 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img")
167 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800168 print("vendor.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800169 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800170
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800171 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map")
172 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img,
173 block_list=block_list)
174 return img.name
Doug Zongker3c84f562014-07-31 11:06:30 -0700175
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700176
Tao Baoc633ed02017-05-30 21:46:33 -0700177def AddDtbo(output_zip, prefix="IMAGES/"):
178 """Adds the DTBO image.
179
180 Uses the image under prefix if it already exists. Otherwise looks for the
181 image under PREBUILT_IMAGES/, signs it as needed, and returns the image name.
182 """
183
184 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img")
185 if os.path.exists(img.input_name):
186 print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,))
187 return img.input_name
188
189 dtbo_prebuilt_path = os.path.join(
190 OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img")
191 assert os.path.exists(dtbo_prebuilt_path)
192 shutil.copy(dtbo_prebuilt_path, img.name)
193
194 # AVB-sign the image as needed.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800195 if OPTIONS.info_dict.get("avb_enable") == "true":
Tao Baoc633ed02017-05-30 21:46:33 -0700196 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Tao Bao3ebfdde2017-05-23 23:06:55 -0700197 part_size = OPTIONS.info_dict["dtbo_size"]
Tao Baoc633ed02017-05-30 21:46:33 -0700198 # The AVB hash footer will be replaced if already present.
199 cmd = [avbtool, "add_hash_footer", "--image", img.name,
200 "--partition_size", str(part_size), "--partition_name", "dtbo"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800201 common.AppendAVBSigningArgs(cmd, "dtbo")
202 args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args")
Tao Baoc633ed02017-05-30 21:46:33 -0700203 if args and args.strip():
204 cmd.extend(shlex.split(args))
205 p = common.Run(cmd, stdout=subprocess.PIPE)
206 p.communicate()
207 assert p.returncode == 0, \
208 "avbtool add_hash_footer of %s failed" % (img.name,)
209
210 img.Write()
211 return img.name
212
Doug Zongker3c84f562014-07-31 11:06:30 -0700213
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800214def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800215 print("creating " + what + ".img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700216
Doug Zongker3c84f562014-07-31 11:06:30 -0700217 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
218 fstab = info_dict["fstab"]
Tianjie Xucfa86222016-03-07 16:31:19 -0800219 mount_point = "/" + what
220 if fstab and mount_point in fstab:
221 image_props["fs_type"] = fstab[mount_point].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700222
Tao Bao822f5842015-09-30 16:01:14 -0700223 # Use a fixed timestamp (01/01/2009) when packaging the image.
224 # Bug: 24377993
225 epoch = datetime.datetime.fromtimestamp(0)
226 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
227 image_props["timestamp"] = int(timestamp)
228
Doug Zongker3c84f562014-07-31 11:06:30 -0700229 if what == "system":
230 fs_config_prefix = ""
231 else:
232 fs_config_prefix = what + "_"
233
234 fs_config = os.path.join(
235 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700236 if not os.path.exists(fs_config):
237 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700238
Ying Wanga2292c92015-03-24 19:07:40 -0700239 # Override values loaded from info_dict.
240 if fs_config:
241 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700242 if block_list:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800243 image_props["block_list"] = block_list.name
Ying Wanga2292c92015-03-24 19:07:40 -0700244
Tao Baod86e3112017-09-22 15:45:33 -0700245 # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and
246 # build fingerprint).
247 uuid_seed = what + "-"
248 if "build.prop" in info_dict:
249 build_prop = info_dict["build.prop"]
250 if "ro.build.fingerprint" in build_prop:
251 uuid_seed += build_prop["ro.build.fingerprint"]
252 elif "ro.build.thumbprint" in build_prop:
253 uuid_seed += build_prop["ro.build.thumbprint"]
254 image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed))
255 hash_seed = "hash_seed-" + uuid_seed
256 image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))
257
Tao Baofa863c82017-05-23 23:49:03 -0700258 succ = build_image.BuildImage(os.path.join(input_dir, what.upper()),
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800259 image_props, output_file.name)
Doug Zongker3c84f562014-07-31 11:06:30 -0700260 assert succ, "build " + what + ".img image failed"
261
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800262 output_file.Write()
263 if block_list:
264 block_list.Write()
265
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700266 # Set the 'adjusted_partition_size' that excludes the verity blocks of the
267 # given image. When avb is enabled, this size is the max image size returned
268 # by the avb tool.
Tianjie Xuf1a13182017-01-19 17:39:30 -0800269 is_verity_partition = "verity_block_device" in image_props
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700270 verity_supported = (image_props.get("verity") == "true" or
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800271 image_props.get("avb_enable") == "true")
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700272 is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
273 if verity_supported and (is_verity_partition or is_avb_enable):
Tianjie Xuf1a13182017-01-19 17:39:30 -0800274 adjusted_blocks_value = image_props.get("partition_size")
275 if adjusted_blocks_value:
276 adjusted_blocks_key = what + "_adjusted_partition_size"
277 info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1
278
Doug Zongker3c84f562014-07-31 11:06:30 -0700279
280def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700281 """Create a userdata image and store it in output_zip.
282
283 In most case we just create and store an empty userdata.img;
284 But the invoker can also request to create userdata.img with real
285 data from the target files, by setting "userdata_img_with_data=true"
286 in OPTIONS.info_dict.
287 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700288
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800289 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img")
290 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800291 print("userdata.img already exists in %s, no need to rebuild..." % (
292 prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800293 return
294
Elliott Hughes305b0882016-06-15 17:04:54 -0700295 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700296 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700297 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700298 return
299
Tao Bao89fbb0f2017-01-10 10:47:58 -0800300 print("creating userdata.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700301
Tao Bao822f5842015-09-30 16:01:14 -0700302 # Use a fixed timestamp (01/01/2009) when packaging the image.
303 # Bug: 24377993
304 epoch = datetime.datetime.fromtimestamp(0)
305 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
306 image_props["timestamp"] = int(timestamp)
307
Tao Baofa863c82017-05-23 23:49:03 -0700308 if OPTIONS.info_dict.get("userdata_img_with_data") == "true":
309 user_dir = os.path.join(OPTIONS.input_tmp, "DATA")
Ying Wang2a048392015-06-25 13:56:53 -0700310 else:
Tao Baofa863c82017-05-23 23:49:03 -0700311 user_dir = tempfile.mkdtemp()
312 OPTIONS.tempfiles.append(user_dir)
Ying Wang2a048392015-06-25 13:56:53 -0700313
Doug Zongker3c84f562014-07-31 11:06:30 -0700314 fstab = OPTIONS.info_dict["fstab"]
315 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700316 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700317 succ = build_image.BuildImage(user_dir, image_props, img.name)
318 assert succ, "build userdata.img image failed"
319
320 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800321 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700322
323
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800324def AppendVBMetaArgsForPartition(cmd, partition, img_path, public_key_dir):
325 if not img_path:
326 return
327
328 # Check if chain partition is used.
329 key_path = OPTIONS.info_dict.get("avb_" + partition + "_key_path")
330 if key_path:
331 # extract public key in AVB format to be included in vbmeta.img
332 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
333 public_key_path = os.path.join(public_key_dir, "%s.avbpubkey" % partition)
334 p = common.Run([avbtool, "extract_public_key", "--key", key_path,
335 "--output", public_key_path],
336 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
337 p.communicate()
338 assert p.returncode == 0, \
339 "avbtool extract_public_key fail for partition: %r" % partition
340
341 rollback_index_location = OPTIONS.info_dict[
342 "avb_" + partition + "_rollback_index_location"]
343 cmd.extend(["--chain_partition", "%s:%s:%s" % (
344 partition, rollback_index_location, public_key_path)])
345 else:
346 cmd.extend(["--include_descriptors_from_image", img_path])
347
348
Tao Baobf70c312017-07-11 17:27:55 -0700349def AddVBMeta(output_zip, partitions, prefix="IMAGES/"):
350 """Creates a VBMeta image and store it in output_zip.
351
352 Args:
353 output_zip: The output zip file, which needs to be already open.
354 partitions: A dict that's keyed by partition names with image paths as
355 values. Only valid partition names are accepted, which include 'boot',
356 'recovery', 'system', 'vendor', 'dtbo'.
357 """
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800358 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img")
Tao Bao262bf3f2017-07-11 17:27:55 -0700359 if os.path.exists(img.input_name):
360 print("vbmeta.img already exists in %s; not rebuilding..." % (prefix,))
361 return img.input_name
362
Tao Baoc633ed02017-05-30 21:46:33 -0700363 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800364 cmd = [avbtool, "make_vbmeta_image", "--output", img.name]
365 common.AppendAVBSigningArgs(cmd, "vbmeta")
366
367 public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-")
368 OPTIONS.tempfiles.append(public_key_dir)
369
Tao Baobf70c312017-07-11 17:27:55 -0700370 for partition, path in partitions.items():
371 assert partition in common.AVB_PARTITIONS, 'Unknown partition: %s' % (
372 partition,)
373 assert os.path.exists(path), 'Failed to find %s for partition %s' % (
374 path, partition)
375 AppendVBMetaArgsForPartition(cmd, partition, path, public_key_dir)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800376
377 args = OPTIONS.info_dict.get("avb_vbmeta_args")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400378 if args and args.strip():
Tao Bao9a5f4192017-07-20 23:51:16 -0700379 split_args = shlex.split(args)
380 for index, arg in enumerate(split_args[:-1]):
381 # Sanity check that the image file exists. Some images might be defined
382 # as a path relative to source tree, which may not be available at the
383 # same location when running this script (we have the input target_files
384 # zip only). For such cases, we additionally scan other locations (e.g.
385 # IMAGES/, RADIO/, etc) before bailing out.
386 if arg == '--include_descriptors_from_image':
387 image_path = split_args[index + 1]
388 if os.path.exists(image_path):
389 continue
390 found = False
391 for dir in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']:
392 alt_path = os.path.join(
393 OPTIONS.input_tmp, dir, os.path.basename(image_path))
394 if os.path.exists(alt_path):
395 split_args[index + 1] = alt_path
396 found = True
397 break
398 assert found, 'failed to find %s' % (image_path,)
399 cmd.extend(split_args)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800400
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400401 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
402 p.communicate()
403 assert p.returncode == 0, "avbtool make_vbmeta_image failed"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800404 img.Write()
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400405
406
David Zeuthen25328622016-04-08 15:08:03 -0400407def AddPartitionTable(output_zip, prefix="IMAGES/"):
408 """Create a partition table image and store it in output_zip."""
409
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800410 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img")
411 bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt")
David Zeuthen25328622016-04-08 15:08:03 -0400412
413 # use BPTTOOL from environ, or "bpttool" if empty or not set.
414 bpttool = os.getenv("BPTTOOL") or "bpttool"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800415 cmd = [bpttool, "make_table", "--output_json", bpt.name,
416 "--output_gpt", img.name]
David Zeuthen25328622016-04-08 15:08:03 -0400417 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
418 input_files = input_files_str.split(" ")
419 for i in input_files:
420 cmd.extend(["--input", i])
421 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
422 if disk_size:
423 cmd.extend(["--disk_size", disk_size])
424 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
425 if args:
426 cmd.extend(shlex.split(args))
427
428 p = common.Run(cmd, stdout=subprocess.PIPE)
429 p.communicate()
430 assert p.returncode == 0, "bpttool make_table failed"
431
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800432 img.Write()
433 bpt.Write()
David Zeuthen25328622016-04-08 15:08:03 -0400434
435
Doug Zongker3c84f562014-07-31 11:06:30 -0700436def AddCache(output_zip, prefix="IMAGES/"):
437 """Create an empty cache image and store it in output_zip."""
438
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800439 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img")
440 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800441 print("cache.img already exists in %s, no need to rebuild..." % (prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800442 return
443
Tao Bao2c15d9e2015-07-09 11:51:16 -0700444 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700445 # The build system has to explicitly request for cache.img.
446 if "fs_type" not in image_props:
447 return
448
Tao Bao89fbb0f2017-01-10 10:47:58 -0800449 print("creating cache.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700450
Tao Bao822f5842015-09-30 16:01:14 -0700451 # Use a fixed timestamp (01/01/2009) when packaging the image.
452 # Bug: 24377993
453 epoch = datetime.datetime.fromtimestamp(0)
454 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
455 image_props["timestamp"] = int(timestamp)
456
Tao Baofa863c82017-05-23 23:49:03 -0700457 user_dir = tempfile.mkdtemp()
458 OPTIONS.tempfiles.append(user_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700459
460 fstab = OPTIONS.info_dict["fstab"]
461 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700462 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700463 succ = build_image.BuildImage(user_dir, image_props, img.name)
464 assert succ, "build cache.img image failed"
465
466 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800467 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700468
469
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700470def ReplaceUpdatedFiles(zip_filename, files_list):
471 """Update all the zip entries listed in the files_list.
Tianjie Xu38af07f2017-05-25 17:38:53 -0700472
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700473 For now the list includes META/care_map.txt, and the related files under
474 SYSTEM/ after rebuilding recovery.
475 """
476
477 cmd = ["zip", "-d", zip_filename] + files_list
Tianjie Xu38af07f2017-05-25 17:38:53 -0700478 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
479 p.communicate()
480
481 output_zip = zipfile.ZipFile(zip_filename, "a",
482 compression=zipfile.ZIP_DEFLATED,
483 allowZip64=True)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700484 for item in files_list:
Tianjie Xu38af07f2017-05-25 17:38:53 -0700485 file_path = os.path.join(OPTIONS.input_tmp, item)
486 assert os.path.exists(file_path)
487 common.ZipWrite(output_zip, file_path, arcname=item)
488 common.ZipClose(output_zip)
489
490
Doug Zongker3c84f562014-07-31 11:06:30 -0700491def AddImagesToTargetFiles(filename):
Tao Baoae396d92017-11-20 11:56:43 -0800492 """Creates and adds images (boot/recovery/system/...) to a target_files.zip.
493
494 It works with either a zip file (zip mode), or a directory that contains the
495 files to be packed into a target_files.zip (dir mode). The latter is used when
496 being called from build/make/core/Makefile.
497
498 The images will be created under IMAGES/ in the input target_files.zip.
499
500 Args:
501 filename: the target_files.zip, or the zip root directory.
502 """
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800503 if os.path.isdir(filename):
504 OPTIONS.input_tmp = os.path.abspath(filename)
505 input_zip = None
506 else:
507 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700508
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800509 if not OPTIONS.add_missing:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800510 if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")):
511 print("target_files appears to already contain images.")
512 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700513
Tao Baob22afea2017-09-12 12:39:09 -0700514 # vendor.img is unlike system.img or system_other.img. Because it could be
515 # built from source, or dropped into target_files.zip as a prebuilt blob. We
516 # consider either of them as vendor.img being available, which could be used
517 # when generating vbmeta.img for AVB.
518 has_vendor = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) or
519 os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES",
520 "vendor.img")))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800521 has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp,
522 "SYSTEM_OTHER"))
Doug Zongker3c84f562014-07-31 11:06:30 -0700523
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800524 if input_zip:
525 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Alex Light4e358ab2016-06-16 14:47:10 -0700526
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800527 common.ZipClose(input_zip)
528 output_zip = zipfile.ZipFile(filename, "a",
529 compression=zipfile.ZIP_DEFLATED,
530 allowZip64=True)
531 else:
532 OPTIONS.info_dict = common.LoadInfoDict(filename, filename)
533 output_zip = None
Tao Baoae396d92017-11-20 11:56:43 -0800534
535 # Always make input_tmp/IMAGES available, since we may stage boot / recovery
536 # images there even under zip mode. The directory will be cleaned up as part
537 # of OPTIONS.input_tmp.
538 images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")
539 if not os.path.isdir(images_dir):
540 os.makedirs(images_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700541
Tao Baodb45efa2015-10-27 19:25:18 -0700542 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
543
Tao Bao2b6dfd62017-09-27 17:17:43 -0700544 if OPTIONS.info_dict.get("avb_enable") == "true":
545 fp = None
546 if "build.prop" in OPTIONS.info_dict:
547 build_prop = OPTIONS.info_dict["build.prop"]
548 if "ro.build.fingerprint" in build_prop:
549 fp = build_prop["ro.build.fingerprint"]
550 elif "ro.build.thumbprint" in build_prop:
551 fp = build_prop["ro.build.thumbprint"]
552 if fp:
553 OPTIONS.info_dict["avb_salt"] = hashlib.sha256(fp).hexdigest()
554
Tao Baobf70c312017-07-11 17:27:55 -0700555 # A map between partition names and their paths, which could be used when
556 # generating AVB vbmeta image.
557 partitions = dict()
558
Doug Zongkerfc44a512014-08-26 13:10:25 -0700559 def banner(s):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800560 print("\n\n++++ " + s + " ++++\n\n")
Doug Zongker3c84f562014-07-31 11:06:30 -0700561
Tao Bao262bf3f2017-07-11 17:27:55 -0700562 banner("boot")
563 # common.GetBootableImage() returns the image directly if present.
564 boot_image = common.GetBootableImage(
565 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
566 # boot.img may be unavailable in some targets (e.g. aosp_arm64).
567 if boot_image:
Tao Baobf70c312017-07-11 17:27:55 -0700568 partitions['boot'] = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
569 if not os.path.exists(partitions['boot']):
Tao Bao262bf3f2017-07-11 17:27:55 -0700570 boot_image.WriteToDir(OPTIONS.input_tmp)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800571 if output_zip:
572 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700573
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800574 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700575 if has_recovery:
576 banner("recovery")
Tao Bao262bf3f2017-07-11 17:27:55 -0700577 recovery_image = common.GetBootableImage(
578 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
579 assert recovery_image, "Failed to create recovery.img."
Tao Baobf70c312017-07-11 17:27:55 -0700580 partitions['recovery'] = os.path.join(
Tao Bao262bf3f2017-07-11 17:27:55 -0700581 OPTIONS.input_tmp, "IMAGES", "recovery.img")
Tao Baobf70c312017-07-11 17:27:55 -0700582 if not os.path.exists(partitions['recovery']):
Tao Bao262bf3f2017-07-11 17:27:55 -0700583 recovery_image.WriteToDir(OPTIONS.input_tmp)
584 if output_zip:
585 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700586
Tao Baod42e97e2016-11-30 12:11:57 -0800587 banner("recovery (two-step image)")
588 # The special recovery.img for two-step package use.
589 recovery_two_step_image = common.GetBootableImage(
590 "IMAGES/recovery-two-step.img", "recovery-two-step.img",
591 OPTIONS.input_tmp, "RECOVERY", two_step_image=True)
Tao Bao262bf3f2017-07-11 17:27:55 -0700592 assert recovery_two_step_image, "Failed to create recovery-two-step.img."
593 recovery_two_step_image_path = os.path.join(
594 OPTIONS.input_tmp, "IMAGES", "recovery-two-step.img")
595 if not os.path.exists(recovery_two_step_image_path):
596 recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800597 if output_zip:
598 recovery_two_step_image.AddToZip(output_zip)
Tao Baod42e97e2016-11-30 12:11:57 -0800599
Doug Zongkerfc44a512014-08-26 13:10:25 -0700600 banner("system")
Tao Baobf70c312017-07-11 17:27:55 -0700601 partitions['system'] = system_img_path = AddSystem(
Tao Baoc633ed02017-05-30 21:46:33 -0700602 output_zip, recovery_img=recovery_image, boot_img=boot_image)
Tao Baobf70c312017-07-11 17:27:55 -0700603
Doug Zongkerfc44a512014-08-26 13:10:25 -0700604 if has_vendor:
605 banner("vendor")
Tao Baobf70c312017-07-11 17:27:55 -0700606 partitions['vendor'] = vendor_img_path = AddVendor(output_zip)
607
Alex Light4e358ab2016-06-16 14:47:10 -0700608 if has_system_other:
609 banner("system_other")
610 AddSystemOther(output_zip)
Tao Baobf70c312017-07-11 17:27:55 -0700611
Tianjie Xub48589a2016-08-03 19:21:52 -0700612 if not OPTIONS.is_signing:
613 banner("userdata")
614 AddUserdata(output_zip)
615 banner("cache")
616 AddCache(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700617
618 if OPTIONS.info_dict.get("board_bpt_enable") == "true":
David Zeuthen25328622016-04-08 15:08:03 -0400619 banner("partition-table")
620 AddPartitionTable(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700621
Tao Baoc633ed02017-05-30 21:46:33 -0700622 if OPTIONS.info_dict.get("has_dtbo") == "true":
623 banner("dtbo")
Tao Baobf70c312017-07-11 17:27:55 -0700624 partitions['dtbo'] = AddDtbo(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700625
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800626 if OPTIONS.info_dict.get("avb_enable") == "true":
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400627 banner("vbmeta")
Tao Baobf70c312017-07-11 17:27:55 -0700628 AddVBMeta(output_zip, partitions)
Doug Zongker3c84f562014-07-31 11:06:30 -0700629
Wei Wang2e735ca2016-05-10 22:48:13 -0700630 # For devices using A/B update, copy over images from RADIO/ and/or
631 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
632 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700633 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800634 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
635 if os.path.exists(ab_partitions):
636 with open(ab_partitions, 'r') as f:
637 lines = f.readlines()
Tianjie Xucfa86222016-03-07 16:31:19 -0800638 # For devices using A/B update, generate care_map for system and vendor
639 # partitions (if present), then write this file to target_files package.
640 care_map_list = []
Tao Baoa0421cd2015-11-16 16:32:27 -0800641 for line in lines:
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700642 if line.strip() == "system" and (
643 "system_verity_block_device" in OPTIONS.info_dict or
Tao Bao3f721762017-06-29 15:11:44 -0700644 OPTIONS.info_dict.get("avb_system_hashtree_enable") == "true"):
Tianjie Xu737afb92016-07-11 11:42:53 -0700645 assert os.path.exists(system_img_path)
646 care_map_list += GetCareMap("system", system_img_path)
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700647 if line.strip() == "vendor" and (
648 "vendor_verity_block_device" in OPTIONS.info_dict or
Tao Bao3f721762017-06-29 15:11:44 -0700649 OPTIONS.info_dict.get("avb_vendor_hashtree_enable") == "true"):
Tianjie Xu737afb92016-07-11 11:42:53 -0700650 assert os.path.exists(vendor_img_path)
651 care_map_list += GetCareMap("vendor", vendor_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800652
Tao Baoa0421cd2015-11-16 16:32:27 -0800653 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700654 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
655 if os.path.exists(prebuilt_path):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800656 print("%s already exists, no need to overwrite..." % (img_name,))
Tianjie Xuaaca4212016-06-28 14:34:03 -0700657 continue
658
Tao Baoa0421cd2015-11-16 16:32:27 -0800659 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700660 img_vendor_dir = os.path.join(
661 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800662 if os.path.exists(img_radio_path):
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800663 if output_zip:
664 common.ZipWrite(output_zip, img_radio_path,
665 os.path.join("IMAGES", img_name))
666 else:
667 shutil.copy(img_radio_path, prebuilt_path)
Wei Wang2e735ca2016-05-10 22:48:13 -0700668 else:
669 for root, _, files in os.walk(img_vendor_dir):
670 if img_name in files:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800671 if output_zip:
672 common.ZipWrite(output_zip, os.path.join(root, img_name),
673 os.path.join("IMAGES", img_name))
674 else:
675 shutil.copy(os.path.join(root, img_name), prebuilt_path)
Wei Wang2e735ca2016-05-10 22:48:13 -0700676 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800677
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800678 if output_zip:
679 # Zip spec says: All slashes MUST be forward slashes.
680 img_path = 'IMAGES/' + img_name
681 assert img_path in output_zip.namelist(), "cannot find " + img_name
682 else:
683 img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
684 assert os.path.exists(img_path), "cannot find " + img_name
Tao Baoa0421cd2015-11-16 16:32:27 -0800685
Tianjie Xucfa86222016-03-07 16:31:19 -0800686 if care_map_list:
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700687 care_map_path = "META/care_map.txt"
688 if output_zip and care_map_path not in output_zip.namelist():
689 common.ZipWriteStr(output_zip, care_map_path, '\n'.join(care_map_list))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800690 else:
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700691 with open(os.path.join(OPTIONS.input_tmp, care_map_path), 'w') as fp:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800692 fp.write('\n'.join(care_map_list))
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700693 if output_zip:
694 OPTIONS.replace_updated_files_list.append(care_map_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800695
Tao Bao95a95c32017-06-16 15:30:23 -0700696 # Radio images that need to be packed into IMAGES/, and product-img.zip.
697 pack_radioimages = os.path.join(
698 OPTIONS.input_tmp, "META", "pack_radioimages.txt")
699 if os.path.exists(pack_radioimages):
700 with open(pack_radioimages, 'r') as f:
701 lines = f.readlines()
702 for line in lines:
703 img_name = line.strip()
704 _, ext = os.path.splitext(img_name)
705 if not ext:
706 img_name += ".img"
707 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
708 if os.path.exists(prebuilt_path):
709 print("%s already exists, no need to overwrite..." % (img_name,))
710 continue
711
712 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
713 assert os.path.exists(img_radio_path), \
714 "Failed to find %s at %s" % (img_name, img_radio_path)
715 if output_zip:
716 common.ZipWrite(output_zip, img_radio_path,
717 os.path.join("IMAGES", img_name))
718 else:
719 shutil.copy(img_radio_path, prebuilt_path)
720
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800721 if output_zip:
722 common.ZipClose(output_zip)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700723 if OPTIONS.replace_updated_files_list:
724 ReplaceUpdatedFiles(output_zip.filename,
725 OPTIONS.replace_updated_files_list)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700726
Doug Zongker3c84f562014-07-31 11:06:30 -0700727
Doug Zongker3c84f562014-07-31 11:06:30 -0700728def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700729 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800730 if o in ("-a", "--add_missing"):
731 OPTIONS.add_missing = True
732 elif o in ("-r", "--rebuild_recovery",):
733 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700734 elif o == "--replace_verity_private_key":
735 OPTIONS.replace_verity_private_key = (True, a)
736 elif o == "--replace_verity_public_key":
737 OPTIONS.replace_verity_public_key = (True, a)
Tianjie Xub48589a2016-08-03 19:21:52 -0700738 elif o == "--is_signing":
739 OPTIONS.is_signing = True
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800740 else:
741 return False
742 return True
743
Dan Albert8b72aef2015-03-23 19:13:21 -0700744 args = common.ParseOptions(
745 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700746 extra_long_opts=["add_missing", "rebuild_recovery",
747 "replace_verity_public_key=",
748 "replace_verity_private_key=",
Tao Bao45810422016-10-17 16:20:12 -0700749 "is_signing"],
Dan Albert8b72aef2015-03-23 19:13:21 -0700750 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800751
Doug Zongker3c84f562014-07-31 11:06:30 -0700752
753 if len(args) != 1:
754 common.Usage(__doc__)
755 sys.exit(1)
756
757 AddImagesToTargetFiles(args[0])
Tao Bao89fbb0f2017-01-10 10:47:58 -0800758 print("done.")
Doug Zongker3c84f562014-07-31 11:06:30 -0700759
760if __name__ == '__main__':
761 try:
762 common.CloseInheritedPipes()
763 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700764 except common.ExternalError as e:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800765 print("\n ERROR: %s\n" % (e,))
Doug Zongker3c84f562014-07-31 11:06:30 -0700766 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700767 finally:
768 common.Cleanup()