blob: 1a08cb6fa7ad9f8d4490d73dd42bc5de854c0b10 [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
Doug Zongker3c84f562014-07-31 11:06:30 -070055import errno
Tao Bao2b6dfd62017-09-27 17:17:43 -070056import hashlib
Doug Zongker3c84f562014-07-31 11:06:30 -070057import os
David Zeuthend995f4b2016-01-29 16:59:17 -050058import shlex
Ying Wang2a048392015-06-25 13:56:53 -070059import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050060import subprocess
Doug Zongker3c84f562014-07-31 11:06:30 -070061import tempfile
Tao Baod86e3112017-09-22 15:45:33 -070062import uuid
Doug Zongker3c84f562014-07-31 11:06:30 -070063import zipfile
64
Doug Zongker3c84f562014-07-31 11:06:30 -070065import build_image
66import common
Tianjie Xuf1a13182017-01-19 17:39:30 -080067import rangelib
Tianjie Xucfa86222016-03-07 16:31:19 -080068import sparse_img
Doug Zongker3c84f562014-07-31 11:06:30 -070069
70OPTIONS = common.OPTIONS
71
Michael Runge2e0d8fc2014-11-13 21:41:08 -080072OPTIONS.add_missing = False
73OPTIONS.rebuild_recovery = False
Tianjie Xu9ac4cb02017-06-09 16:58:03 -070074OPTIONS.replace_updated_files_list = []
Baligh Uddin59f4ff12015-09-16 21:20:30 -070075OPTIONS.replace_verity_public_key = False
76OPTIONS.replace_verity_private_key = False
Tianjie Xub48589a2016-08-03 19:21:52 -070077OPTIONS.is_signing = False
Doug Zongker3c84f562014-07-31 11:06:30 -070078
Dan Willemsen2ee00d52017-03-05 19:51:56 -080079
80class OutputFile(object):
81 def __init__(self, output_zip, input_dir, prefix, name):
82 self._output_zip = output_zip
83 self.input_name = os.path.join(input_dir, prefix, name)
84
85 if self._output_zip:
86 self._zip_name = os.path.join(prefix, name)
87
88 root, suffix = os.path.splitext(name)
89 self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix)
90 else:
91 self.name = self.input_name
92
93 def Write(self):
94 if self._output_zip:
95 common.ZipWrite(self._output_zip, self.name, self._zip_name)
96
97
Tianjie Xucfa86222016-03-07 16:31:19 -080098def GetCareMap(which, imgname):
99 """Generate care_map of system (or vendor) partition"""
100
101 assert which in ("system", "vendor")
Tianjie Xucfa86222016-03-07 16:31:19 -0800102
103 simg = sparse_img.SparseImage(imgname)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700104 care_map_list = [which]
Tianjie Xuf1a13182017-01-19 17:39:30 -0800105
106 care_map_ranges = simg.care_map
107 key = which + "_adjusted_partition_size"
108 adjusted_blocks = OPTIONS.info_dict.get(key)
109 if adjusted_blocks:
110 assert adjusted_blocks > 0, "blocks should be positive for " + which
111 care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet(
112 "0-%d" % (adjusted_blocks,)))
113
114 care_map_list.append(care_map_ranges.to_string_raw())
Tianjie Xucfa86222016-03-07 16:31:19 -0800115 return care_map_list
116
117
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800118def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700119 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -0500120 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800121
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800122 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img")
123 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800124 print("system.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800125 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800126
127 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -0700128 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
129 ofile.write(data)
130 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800131
Tianjie Xu38af07f2017-05-25 17:38:53 -0700132 arc_name = "SYSTEM/" + fn
133 if arc_name in output_zip.namelist():
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700134 OPTIONS.replace_updated_files_list.append(arc_name)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700135 else:
136 common.ZipWrite(output_zip, ofile.name, arc_name)
137
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800138 if OPTIONS.rebuild_recovery:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800139 print("Building new recovery patch")
Dan Albert8b72aef2015-03-23 19:13:21 -0700140 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
141 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800142
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800143 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map")
144 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img,
145 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -0500146
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800147 return img.name
Doug Zongkerfc44a512014-08-26 13:10:25 -0700148
149
Alex Light4e358ab2016-06-16 14:47:10 -0700150def AddSystemOther(output_zip, prefix="IMAGES/"):
151 """Turn the contents of SYSTEM_OTHER into a system_other image
152 and store it in output_zip."""
153
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800154 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img")
155 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800156 print("system_other.img already exists in %s, no need to rebuild..." % (
157 prefix,))
Alex Light4e358ab2016-06-16 14:47:10 -0700158 return
159
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800160 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img)
Alex Light4e358ab2016-06-16 14:47:10 -0700161
162
Doug Zongkerfc44a512014-08-26 13:10:25 -0700163def AddVendor(output_zip, prefix="IMAGES/"):
164 """Turn the contents of VENDOR into a vendor image and store in it
165 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800166
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800167 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img")
168 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800169 print("vendor.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800170 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800171
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800172 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map")
173 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img,
174 block_list=block_list)
175 return img.name
Doug Zongker3c84f562014-07-31 11:06:30 -0700176
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700177
Tao Baoc633ed02017-05-30 21:46:33 -0700178def AddDtbo(output_zip, prefix="IMAGES/"):
179 """Adds the DTBO image.
180
181 Uses the image under prefix if it already exists. Otherwise looks for the
182 image under PREBUILT_IMAGES/, signs it as needed, and returns the image name.
183 """
184
185 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img")
186 if os.path.exists(img.input_name):
187 print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,))
188 return img.input_name
189
190 dtbo_prebuilt_path = os.path.join(
191 OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img")
192 assert os.path.exists(dtbo_prebuilt_path)
193 shutil.copy(dtbo_prebuilt_path, img.name)
194
195 # AVB-sign the image as needed.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800196 if OPTIONS.info_dict.get("avb_enable") == "true":
Tao Baoc633ed02017-05-30 21:46:33 -0700197 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Tao Bao3ebfdde2017-05-23 23:06:55 -0700198 part_size = OPTIONS.info_dict["dtbo_size"]
Tao Baoc633ed02017-05-30 21:46:33 -0700199 # The AVB hash footer will be replaced if already present.
200 cmd = [avbtool, "add_hash_footer", "--image", img.name,
201 "--partition_size", str(part_size), "--partition_name", "dtbo"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800202 common.AppendAVBSigningArgs(cmd, "dtbo")
203 args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args")
Tao Baoc633ed02017-05-30 21:46:33 -0700204 if args and args.strip():
205 cmd.extend(shlex.split(args))
206 p = common.Run(cmd, stdout=subprocess.PIPE)
207 p.communicate()
208 assert p.returncode == 0, \
209 "avbtool add_hash_footer of %s failed" % (img.name,)
210
211 img.Write()
212 return img.name
213
Doug Zongker3c84f562014-07-31 11:06:30 -0700214
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800215def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800216 print("creating " + what + ".img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700217
Doug Zongker3c84f562014-07-31 11:06:30 -0700218 # The name of the directory it is making an image out of matters to
219 # mkyaffs2image. It wants "system" but we have a directory named
220 # "SYSTEM", so create a symlink.
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800221 temp_dir = tempfile.mkdtemp()
222 OPTIONS.tempfiles.append(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700223 try:
224 os.symlink(os.path.join(input_dir, what.upper()),
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800225 os.path.join(temp_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700226 except OSError as e:
227 # bogus error on my mac version?
228 # File "./build/tools/releasetools/img_from_target_files"
229 # os.path.join(OPTIONS.input_tmp, "system"))
230 # OSError: [Errno 17] File exists
231 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700232 pass
233
234 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
235 fstab = info_dict["fstab"]
Tianjie Xucfa86222016-03-07 16:31:19 -0800236 mount_point = "/" + what
237 if fstab and mount_point in fstab:
238 image_props["fs_type"] = fstab[mount_point].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700239
Tao Bao822f5842015-09-30 16:01:14 -0700240 # Use a fixed timestamp (01/01/2009) when packaging the image.
241 # Bug: 24377993
242 epoch = datetime.datetime.fromtimestamp(0)
243 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
244 image_props["timestamp"] = int(timestamp)
245
Doug Zongker3c84f562014-07-31 11:06:30 -0700246 if what == "system":
247 fs_config_prefix = ""
248 else:
249 fs_config_prefix = what + "_"
250
251 fs_config = os.path.join(
252 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700253 if not os.path.exists(fs_config):
254 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700255
Ying Wanga2292c92015-03-24 19:07:40 -0700256 # Override values loaded from info_dict.
257 if fs_config:
258 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700259 if block_list:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800260 image_props["block_list"] = block_list.name
Ying Wanga2292c92015-03-24 19:07:40 -0700261
Tao Baod86e3112017-09-22 15:45:33 -0700262 # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and
263 # build fingerprint).
264 uuid_seed = what + "-"
265 if "build.prop" in info_dict:
266 build_prop = info_dict["build.prop"]
267 if "ro.build.fingerprint" in build_prop:
268 uuid_seed += build_prop["ro.build.fingerprint"]
269 elif "ro.build.thumbprint" in build_prop:
270 uuid_seed += build_prop["ro.build.thumbprint"]
271 image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed))
272 hash_seed = "hash_seed-" + uuid_seed
273 image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))
274
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800275 succ = build_image.BuildImage(os.path.join(temp_dir, what),
276 image_props, output_file.name)
Doug Zongker3c84f562014-07-31 11:06:30 -0700277 assert succ, "build " + what + ".img image failed"
278
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800279 output_file.Write()
280 if block_list:
281 block_list.Write()
282
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700283 # Set the 'adjusted_partition_size' that excludes the verity blocks of the
284 # given image. When avb is enabled, this size is the max image size returned
285 # by the avb tool.
Tianjie Xuf1a13182017-01-19 17:39:30 -0800286 is_verity_partition = "verity_block_device" in image_props
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700287 verity_supported = (image_props.get("verity") == "true" or
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800288 image_props.get("avb_enable") == "true")
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700289 is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
290 if verity_supported and (is_verity_partition or is_avb_enable):
Tianjie Xuf1a13182017-01-19 17:39:30 -0800291 adjusted_blocks_value = image_props.get("partition_size")
292 if adjusted_blocks_value:
293 adjusted_blocks_key = what + "_adjusted_partition_size"
294 info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1
295
Doug Zongker3c84f562014-07-31 11:06:30 -0700296
297def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700298 """Create a userdata image and store it in output_zip.
299
300 In most case we just create and store an empty userdata.img;
301 But the invoker can also request to create userdata.img with real
302 data from the target files, by setting "userdata_img_with_data=true"
303 in OPTIONS.info_dict.
304 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700305
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800306 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img")
307 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800308 print("userdata.img already exists in %s, no need to rebuild..." % (
309 prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800310 return
311
Elliott Hughes305b0882016-06-15 17:04:54 -0700312 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700313 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700314 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700315 return
316
Tao Bao89fbb0f2017-01-10 10:47:58 -0800317 print("creating userdata.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700318
Tao Bao822f5842015-09-30 16:01:14 -0700319 # Use a fixed timestamp (01/01/2009) when packaging the image.
320 # Bug: 24377993
321 epoch = datetime.datetime.fromtimestamp(0)
322 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
323 image_props["timestamp"] = int(timestamp)
324
Doug Zongker3c84f562014-07-31 11:06:30 -0700325 # The name of the directory it is making an image out of matters to
326 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700327 # empty dir named "data", or a symlink to the DATA dir,
328 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700329 temp_dir = tempfile.mkdtemp()
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800330 OPTIONS.tempfiles.append(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700331 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700332 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
333 if empty:
334 # Create an empty dir.
335 os.mkdir(user_dir)
336 else:
337 # Symlink to the DATA dir.
338 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
339 user_dir)
340
Doug Zongker3c84f562014-07-31 11:06:30 -0700341 fstab = OPTIONS.info_dict["fstab"]
342 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700343 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700344 succ = build_image.BuildImage(user_dir, image_props, img.name)
345 assert succ, "build userdata.img image failed"
346
347 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800348 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700349
350
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800351def AppendVBMetaArgsForPartition(cmd, partition, img_path, public_key_dir):
352 if not img_path:
353 return
354
355 # Check if chain partition is used.
356 key_path = OPTIONS.info_dict.get("avb_" + partition + "_key_path")
357 if key_path:
358 # extract public key in AVB format to be included in vbmeta.img
359 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
360 public_key_path = os.path.join(public_key_dir, "%s.avbpubkey" % partition)
361 p = common.Run([avbtool, "extract_public_key", "--key", key_path,
362 "--output", public_key_path],
363 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
364 p.communicate()
365 assert p.returncode == 0, \
366 "avbtool extract_public_key fail for partition: %r" % partition
367
368 rollback_index_location = OPTIONS.info_dict[
369 "avb_" + partition + "_rollback_index_location"]
370 cmd.extend(["--chain_partition", "%s:%s:%s" % (
371 partition, rollback_index_location, public_key_path)])
372 else:
373 cmd.extend(["--include_descriptors_from_image", img_path])
374
375
Bowgo Tsai8ee4a3d2017-03-31 15:21:26 +0800376def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path,
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700377 dtbo_img_path, prefix="IMAGES/"):
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400378 """Create a VBMeta image and store it in output_zip."""
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800379 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img")
Tao Baoc633ed02017-05-30 21:46:33 -0700380 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800381 cmd = [avbtool, "make_vbmeta_image", "--output", img.name]
382 common.AppendAVBSigningArgs(cmd, "vbmeta")
383
384 public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-")
385 OPTIONS.tempfiles.append(public_key_dir)
386
387 AppendVBMetaArgsForPartition(cmd, "boot", boot_img_path, public_key_dir)
388 AppendVBMetaArgsForPartition(cmd, "system", system_img_path, public_key_dir)
389 AppendVBMetaArgsForPartition(cmd, "vendor", vendor_img_path, public_key_dir)
390 AppendVBMetaArgsForPartition(cmd, "dtbo", dtbo_img_path, public_key_dir)
391
392 args = OPTIONS.info_dict.get("avb_vbmeta_args")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400393 if args and args.strip():
Tao Bao9a5f4192017-07-20 23:51:16 -0700394 split_args = shlex.split(args)
395 for index, arg in enumerate(split_args[:-1]):
396 # Sanity check that the image file exists. Some images might be defined
397 # as a path relative to source tree, which may not be available at the
398 # same location when running this script (we have the input target_files
399 # zip only). For such cases, we additionally scan other locations (e.g.
400 # IMAGES/, RADIO/, etc) before bailing out.
401 if arg == '--include_descriptors_from_image':
402 image_path = split_args[index + 1]
403 if os.path.exists(image_path):
404 continue
405 found = False
406 for dir in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']:
407 alt_path = os.path.join(
408 OPTIONS.input_tmp, dir, os.path.basename(image_path))
409 if os.path.exists(alt_path):
410 split_args[index + 1] = alt_path
411 found = True
412 break
413 assert found, 'failed to find %s' % (image_path,)
414 cmd.extend(split_args)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800415
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400416 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
417 p.communicate()
418 assert p.returncode == 0, "avbtool make_vbmeta_image failed"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800419 img.Write()
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400420
421
David Zeuthen25328622016-04-08 15:08:03 -0400422def AddPartitionTable(output_zip, prefix="IMAGES/"):
423 """Create a partition table image and store it in output_zip."""
424
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800425 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img")
426 bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt")
David Zeuthen25328622016-04-08 15:08:03 -0400427
428 # use BPTTOOL from environ, or "bpttool" if empty or not set.
429 bpttool = os.getenv("BPTTOOL") or "bpttool"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800430 cmd = [bpttool, "make_table", "--output_json", bpt.name,
431 "--output_gpt", img.name]
David Zeuthen25328622016-04-08 15:08:03 -0400432 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
433 input_files = input_files_str.split(" ")
434 for i in input_files:
435 cmd.extend(["--input", i])
436 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
437 if disk_size:
438 cmd.extend(["--disk_size", disk_size])
439 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
440 if args:
441 cmd.extend(shlex.split(args))
442
443 p = common.Run(cmd, stdout=subprocess.PIPE)
444 p.communicate()
445 assert p.returncode == 0, "bpttool make_table failed"
446
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800447 img.Write()
448 bpt.Write()
David Zeuthen25328622016-04-08 15:08:03 -0400449
450
Doug Zongker3c84f562014-07-31 11:06:30 -0700451def AddCache(output_zip, prefix="IMAGES/"):
452 """Create an empty cache image and store it in output_zip."""
453
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800454 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img")
455 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800456 print("cache.img already exists in %s, no need to rebuild..." % (prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800457 return
458
Tao Bao2c15d9e2015-07-09 11:51:16 -0700459 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700460 # The build system has to explicitly request for cache.img.
461 if "fs_type" not in image_props:
462 return
463
Tao Bao89fbb0f2017-01-10 10:47:58 -0800464 print("creating cache.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700465
Tao Bao822f5842015-09-30 16:01:14 -0700466 # Use a fixed timestamp (01/01/2009) when packaging the image.
467 # Bug: 24377993
468 epoch = datetime.datetime.fromtimestamp(0)
469 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
470 image_props["timestamp"] = int(timestamp)
471
Doug Zongker3c84f562014-07-31 11:06:30 -0700472 # The name of the directory it is making an image out of matters to
473 # mkyaffs2image. So we create a temp dir, and within it we create an
474 # empty dir named "cache", and build the image from that.
475 temp_dir = tempfile.mkdtemp()
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800476 OPTIONS.tempfiles.append(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700477 user_dir = os.path.join(temp_dir, "cache")
478 os.mkdir(user_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700479
480 fstab = OPTIONS.info_dict["fstab"]
481 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700482 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700483 succ = build_image.BuildImage(user_dir, image_props, img.name)
484 assert succ, "build cache.img image failed"
485
486 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800487 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700488
489
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700490def ReplaceUpdatedFiles(zip_filename, files_list):
491 """Update all the zip entries listed in the files_list.
Tianjie Xu38af07f2017-05-25 17:38:53 -0700492
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700493 For now the list includes META/care_map.txt, and the related files under
494 SYSTEM/ after rebuilding recovery.
495 """
496
497 cmd = ["zip", "-d", zip_filename] + files_list
Tianjie Xu38af07f2017-05-25 17:38:53 -0700498 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
499 p.communicate()
500
501 output_zip = zipfile.ZipFile(zip_filename, "a",
502 compression=zipfile.ZIP_DEFLATED,
503 allowZip64=True)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700504 for item in files_list:
Tianjie Xu38af07f2017-05-25 17:38:53 -0700505 file_path = os.path.join(OPTIONS.input_tmp, item)
506 assert os.path.exists(file_path)
507 common.ZipWrite(output_zip, file_path, arcname=item)
508 common.ZipClose(output_zip)
509
510
Doug Zongker3c84f562014-07-31 11:06:30 -0700511def AddImagesToTargetFiles(filename):
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800512 if os.path.isdir(filename):
513 OPTIONS.input_tmp = os.path.abspath(filename)
514 input_zip = None
515 else:
516 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700517
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800518 if not OPTIONS.add_missing:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800519 if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")):
520 print("target_files appears to already contain images.")
521 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700522
Tao Baob22afea2017-09-12 12:39:09 -0700523 # vendor.img is unlike system.img or system_other.img. Because it could be
524 # built from source, or dropped into target_files.zip as a prebuilt blob. We
525 # consider either of them as vendor.img being available, which could be used
526 # when generating vbmeta.img for AVB.
527 has_vendor = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) or
528 os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES",
529 "vendor.img")))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800530 has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp,
531 "SYSTEM_OTHER"))
Doug Zongker3c84f562014-07-31 11:06:30 -0700532
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800533 if input_zip:
534 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Alex Light4e358ab2016-06-16 14:47:10 -0700535
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800536 common.ZipClose(input_zip)
537 output_zip = zipfile.ZipFile(filename, "a",
538 compression=zipfile.ZIP_DEFLATED,
539 allowZip64=True)
540 else:
541 OPTIONS.info_dict = common.LoadInfoDict(filename, filename)
542 output_zip = None
543 images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")
544 if not os.path.isdir(images_dir):
545 os.makedirs(images_dir)
546 images_dir = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700547
Tao Baodb45efa2015-10-27 19:25:18 -0700548 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
549
Tao Bao2b6dfd62017-09-27 17:17:43 -0700550 if OPTIONS.info_dict.get("avb_enable") == "true":
551 fp = None
552 if "build.prop" in OPTIONS.info_dict:
553 build_prop = OPTIONS.info_dict["build.prop"]
554 if "ro.build.fingerprint" in build_prop:
555 fp = build_prop["ro.build.fingerprint"]
556 elif "ro.build.thumbprint" in build_prop:
557 fp = build_prop["ro.build.thumbprint"]
558 if fp:
559 OPTIONS.info_dict["avb_salt"] = hashlib.sha256(fp).hexdigest()
560
Doug Zongkerfc44a512014-08-26 13:10:25 -0700561 def banner(s):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800562 print("\n\n++++ " + s + " ++++\n\n")
Doug Zongker3c84f562014-07-31 11:06:30 -0700563
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800564 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
565 boot_image = None
566 if os.path.exists(prebuilt_path):
David Zeuthend995f4b2016-01-29 16:59:17 -0500567 banner("boot")
Tao Bao89fbb0f2017-01-10 10:47:58 -0800568 print("boot.img already exists in IMAGES/, no need to rebuild...")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800569 if OPTIONS.rebuild_recovery:
570 boot_image = common.GetBootableImage(
571 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
572 else:
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400573 banner("boot")
574 boot_image = common.GetBootableImage(
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800575 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400576 if boot_image:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800577 if output_zip:
578 boot_image.AddToZip(output_zip)
579 else:
580 boot_image.WriteToDir(OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700581
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800582 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700583 if has_recovery:
584 banner("recovery")
585 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
586 if os.path.exists(prebuilt_path):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800587 print("recovery.img already exists in IMAGES/, no need to rebuild...")
Tao Baodb45efa2015-10-27 19:25:18 -0700588 if OPTIONS.rebuild_recovery:
589 recovery_image = common.GetBootableImage(
590 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
591 "RECOVERY")
592 else:
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800593 recovery_image = common.GetBootableImage(
594 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
Tao Baodb45efa2015-10-27 19:25:18 -0700595 if recovery_image:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800596 if output_zip:
597 recovery_image.AddToZip(output_zip)
598 else:
599 recovery_image.WriteToDir(OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700600
Tao Baod42e97e2016-11-30 12:11:57 -0800601 banner("recovery (two-step image)")
602 # The special recovery.img for two-step package use.
603 recovery_two_step_image = common.GetBootableImage(
604 "IMAGES/recovery-two-step.img", "recovery-two-step.img",
605 OPTIONS.input_tmp, "RECOVERY", two_step_image=True)
606 if recovery_two_step_image:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800607 if output_zip:
608 recovery_two_step_image.AddToZip(output_zip)
609 else:
610 recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)
Tao Baod42e97e2016-11-30 12:11:57 -0800611
Doug Zongkerfc44a512014-08-26 13:10:25 -0700612 banner("system")
David Zeuthend995f4b2016-01-29 16:59:17 -0500613 system_img_path = AddSystem(
Tao Baoc633ed02017-05-30 21:46:33 -0700614 output_zip, recovery_img=recovery_image, boot_img=boot_image)
Tianjie Xu737afb92016-07-11 11:42:53 -0700615 vendor_img_path = None
Doug Zongkerfc44a512014-08-26 13:10:25 -0700616 if has_vendor:
617 banner("vendor")
Tianjie Xu737afb92016-07-11 11:42:53 -0700618 vendor_img_path = AddVendor(output_zip)
Alex Light4e358ab2016-06-16 14:47:10 -0700619 if has_system_other:
620 banner("system_other")
621 AddSystemOther(output_zip)
Tianjie Xub48589a2016-08-03 19:21:52 -0700622 if not OPTIONS.is_signing:
623 banner("userdata")
624 AddUserdata(output_zip)
625 banner("cache")
626 AddCache(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700627
628 if OPTIONS.info_dict.get("board_bpt_enable") == "true":
David Zeuthen25328622016-04-08 15:08:03 -0400629 banner("partition-table")
630 AddPartitionTable(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700631
632 dtbo_img_path = None
633 if OPTIONS.info_dict.get("has_dtbo") == "true":
634 banner("dtbo")
635 dtbo_img_path = AddDtbo(output_zip)
636
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800637 if OPTIONS.info_dict.get("avb_enable") == "true":
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400638 banner("vbmeta")
639 boot_contents = boot_image.WriteToTemp()
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700640 AddVBMeta(output_zip, boot_contents.name, system_img_path,
641 vendor_img_path, dtbo_img_path)
Doug Zongker3c84f562014-07-31 11:06:30 -0700642
Wei Wang2e735ca2016-05-10 22:48:13 -0700643 # For devices using A/B update, copy over images from RADIO/ and/or
644 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
645 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700646 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800647 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
648 if os.path.exists(ab_partitions):
649 with open(ab_partitions, 'r') as f:
650 lines = f.readlines()
Tianjie Xucfa86222016-03-07 16:31:19 -0800651 # For devices using A/B update, generate care_map for system and vendor
652 # partitions (if present), then write this file to target_files package.
653 care_map_list = []
Tao Baoa0421cd2015-11-16 16:32:27 -0800654 for line in lines:
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700655 if line.strip() == "system" and (
656 "system_verity_block_device" in OPTIONS.info_dict or
Tao Bao3f721762017-06-29 15:11:44 -0700657 OPTIONS.info_dict.get("avb_system_hashtree_enable") == "true"):
Tianjie Xu737afb92016-07-11 11:42:53 -0700658 assert os.path.exists(system_img_path)
659 care_map_list += GetCareMap("system", system_img_path)
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700660 if line.strip() == "vendor" and (
661 "vendor_verity_block_device" in OPTIONS.info_dict or
Tao Bao3f721762017-06-29 15:11:44 -0700662 OPTIONS.info_dict.get("avb_vendor_hashtree_enable") == "true"):
Tianjie Xu737afb92016-07-11 11:42:53 -0700663 assert os.path.exists(vendor_img_path)
664 care_map_list += GetCareMap("vendor", vendor_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800665
Tao Baoa0421cd2015-11-16 16:32:27 -0800666 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700667 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
668 if os.path.exists(prebuilt_path):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800669 print("%s already exists, no need to overwrite..." % (img_name,))
Tianjie Xuaaca4212016-06-28 14:34:03 -0700670 continue
671
Tao Baoa0421cd2015-11-16 16:32:27 -0800672 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700673 img_vendor_dir = os.path.join(
674 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800675 if os.path.exists(img_radio_path):
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800676 if output_zip:
677 common.ZipWrite(output_zip, img_radio_path,
678 os.path.join("IMAGES", img_name))
679 else:
680 shutil.copy(img_radio_path, prebuilt_path)
Wei Wang2e735ca2016-05-10 22:48:13 -0700681 else:
682 for root, _, files in os.walk(img_vendor_dir):
683 if img_name in files:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800684 if output_zip:
685 common.ZipWrite(output_zip, os.path.join(root, img_name),
686 os.path.join("IMAGES", img_name))
687 else:
688 shutil.copy(os.path.join(root, img_name), prebuilt_path)
Wei Wang2e735ca2016-05-10 22:48:13 -0700689 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800690
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800691 if output_zip:
692 # Zip spec says: All slashes MUST be forward slashes.
693 img_path = 'IMAGES/' + img_name
694 assert img_path in output_zip.namelist(), "cannot find " + img_name
695 else:
696 img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
697 assert os.path.exists(img_path), "cannot find " + img_name
Tao Baoa0421cd2015-11-16 16:32:27 -0800698
Tianjie Xucfa86222016-03-07 16:31:19 -0800699 if care_map_list:
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700700 care_map_path = "META/care_map.txt"
701 if output_zip and care_map_path not in output_zip.namelist():
702 common.ZipWriteStr(output_zip, care_map_path, '\n'.join(care_map_list))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800703 else:
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700704 with open(os.path.join(OPTIONS.input_tmp, care_map_path), 'w') as fp:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800705 fp.write('\n'.join(care_map_list))
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700706 if output_zip:
707 OPTIONS.replace_updated_files_list.append(care_map_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800708
Tao Bao95a95c32017-06-16 15:30:23 -0700709 # Radio images that need to be packed into IMAGES/, and product-img.zip.
710 pack_radioimages = os.path.join(
711 OPTIONS.input_tmp, "META", "pack_radioimages.txt")
712 if os.path.exists(pack_radioimages):
713 with open(pack_radioimages, 'r') as f:
714 lines = f.readlines()
715 for line in lines:
716 img_name = line.strip()
717 _, ext = os.path.splitext(img_name)
718 if not ext:
719 img_name += ".img"
720 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
721 if os.path.exists(prebuilt_path):
722 print("%s already exists, no need to overwrite..." % (img_name,))
723 continue
724
725 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
726 assert os.path.exists(img_radio_path), \
727 "Failed to find %s at %s" % (img_name, img_radio_path)
728 if output_zip:
729 common.ZipWrite(output_zip, img_radio_path,
730 os.path.join("IMAGES", img_name))
731 else:
732 shutil.copy(img_radio_path, prebuilt_path)
733
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800734 if output_zip:
735 common.ZipClose(output_zip)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700736 if OPTIONS.replace_updated_files_list:
737 ReplaceUpdatedFiles(output_zip.filename,
738 OPTIONS.replace_updated_files_list)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700739
Doug Zongker3c84f562014-07-31 11:06:30 -0700740
Doug Zongker3c84f562014-07-31 11:06:30 -0700741def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700742 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800743 if o in ("-a", "--add_missing"):
744 OPTIONS.add_missing = True
745 elif o in ("-r", "--rebuild_recovery",):
746 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700747 elif o == "--replace_verity_private_key":
748 OPTIONS.replace_verity_private_key = (True, a)
749 elif o == "--replace_verity_public_key":
750 OPTIONS.replace_verity_public_key = (True, a)
Tianjie Xub48589a2016-08-03 19:21:52 -0700751 elif o == "--is_signing":
752 OPTIONS.is_signing = True
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800753 else:
754 return False
755 return True
756
Dan Albert8b72aef2015-03-23 19:13:21 -0700757 args = common.ParseOptions(
758 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700759 extra_long_opts=["add_missing", "rebuild_recovery",
760 "replace_verity_public_key=",
761 "replace_verity_private_key=",
Tao Bao45810422016-10-17 16:20:12 -0700762 "is_signing"],
Dan Albert8b72aef2015-03-23 19:13:21 -0700763 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800764
Doug Zongker3c84f562014-07-31 11:06:30 -0700765
766 if len(args) != 1:
767 common.Usage(__doc__)
768 sys.exit(1)
769
770 AddImagesToTargetFiles(args[0])
Tao Bao89fbb0f2017-01-10 10:47:58 -0800771 print("done.")
Doug Zongker3c84f562014-07-31 11:06:30 -0700772
773if __name__ == '__main__':
774 try:
775 common.CloseInheritedPipes()
776 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700777 except common.ExternalError as e:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800778 print("\n ERROR: %s\n" % (e,))
Doug Zongker3c84f562014-07-31 11:06:30 -0700779 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700780 finally:
781 common.Cleanup()