blob: a9863bc91b4625a3e53aca45de192db3bf0e6dba [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
Tao Bao822f5842015-09-30 16:01:14 -070048import datetime
Tao Bao2b6dfd62017-09-27 17:17:43 -070049import hashlib
Doug Zongker3c84f562014-07-31 11:06:30 -070050import os
David Zeuthend995f4b2016-01-29 16:59:17 -050051import shlex
Ying Wang2a048392015-06-25 13:56:53 -070052import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050053import subprocess
Tao Bao6b9fef52017-12-01 16:13:22 -080054import sys
Tao Baod86e3112017-09-22 15:45:33 -070055import uuid
Doug Zongker3c84f562014-07-31 11:06:30 -070056import zipfile
57
Doug Zongker3c84f562014-07-31 11:06:30 -070058import build_image
59import common
Tianjie Xuf1a13182017-01-19 17:39:30 -080060import rangelib
Tianjie Xucfa86222016-03-07 16:31:19 -080061import sparse_img
Doug Zongker3c84f562014-07-31 11:06:30 -070062
Tao Bao6b9fef52017-12-01 16:13:22 -080063if sys.hexversion < 0x02070000:
64 print("Python 2.7 or newer is required.", file=sys.stderr)
65 sys.exit(1)
66
Doug Zongker3c84f562014-07-31 11:06:30 -070067OPTIONS = common.OPTIONS
68
Michael Runge2e0d8fc2014-11-13 21:41:08 -080069OPTIONS.add_missing = False
70OPTIONS.rebuild_recovery = False
Tianjie Xu9ac4cb02017-06-09 16:58:03 -070071OPTIONS.replace_updated_files_list = []
Baligh Uddin59f4ff12015-09-16 21:20:30 -070072OPTIONS.replace_verity_public_key = False
73OPTIONS.replace_verity_private_key = False
Tianjie Xub48589a2016-08-03 19:21:52 -070074OPTIONS.is_signing = False
Doug Zongker3c84f562014-07-31 11:06:30 -070075
Dan Willemsen2ee00d52017-03-05 19:51:56 -080076
Tao Baoa2ff4c92018-01-17 12:14:43 -080077# Partitions that should have their care_map added to META/care_map.txt.
78PARTITIONS_WITH_CARE_MAP = ('system', 'vendor')
79
80
Dan Willemsen2ee00d52017-03-05 19:51:56 -080081class OutputFile(object):
82 def __init__(self, output_zip, input_dir, prefix, name):
83 self._output_zip = output_zip
84 self.input_name = os.path.join(input_dir, prefix, name)
85
86 if self._output_zip:
87 self._zip_name = os.path.join(prefix, name)
88
89 root, suffix = os.path.splitext(name)
90 self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix)
91 else:
92 self.name = self.input_name
93
94 def Write(self):
95 if self._output_zip:
96 common.ZipWrite(self._output_zip, self.name, self._zip_name)
97
98
Tianjie Xucfa86222016-03-07 16:31:19 -080099def GetCareMap(which, imgname):
Tao Baoa2ff4c92018-01-17 12:14:43 -0800100 """Generates the care_map for the given partition."""
101 assert which in PARTITIONS_WITH_CARE_MAP
Tianjie Xucfa86222016-03-07 16:31:19 -0800102
103 simg = sparse_img.SparseImage(imgname)
Tianjie Xuf1a13182017-01-19 17:39:30 -0800104 care_map_ranges = simg.care_map
105 key = which + "_adjusted_partition_size"
106 adjusted_blocks = OPTIONS.info_dict.get(key)
107 if adjusted_blocks:
108 assert adjusted_blocks > 0, "blocks should be positive for " + which
109 care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet(
110 "0-%d" % (adjusted_blocks,)))
111
Tao Baoa2ff4c92018-01-17 12:14:43 -0800112 return [which, care_map_ranges.to_string_raw()]
Tianjie Xucfa86222016-03-07 16:31:19 -0800113
114
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800115def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700116 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -0500117 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800118
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800119 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img")
120 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800121 print("system.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800122 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800123
124 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -0700125 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
126 ofile.write(data)
127 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800128
Tianjie Xu38af07f2017-05-25 17:38:53 -0700129 arc_name = "SYSTEM/" + fn
130 if arc_name in output_zip.namelist():
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700131 OPTIONS.replace_updated_files_list.append(arc_name)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700132 else:
133 common.ZipWrite(output_zip, ofile.name, arc_name)
134
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800135 if OPTIONS.rebuild_recovery:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800136 print("Building new recovery patch")
Dan Albert8b72aef2015-03-23 19:13:21 -0700137 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
138 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800139
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800140 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map")
141 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img,
142 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -0500143
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800144 return img.name
Doug Zongkerfc44a512014-08-26 13:10:25 -0700145
146
Alex Light4e358ab2016-06-16 14:47:10 -0700147def AddSystemOther(output_zip, prefix="IMAGES/"):
148 """Turn the contents of SYSTEM_OTHER into a system_other image
149 and store it in output_zip."""
150
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800151 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img")
152 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800153 print("system_other.img already exists in %s, no need to rebuild..." % (
154 prefix,))
Alex Light4e358ab2016-06-16 14:47:10 -0700155 return
156
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800157 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img)
Alex Light4e358ab2016-06-16 14:47:10 -0700158
159
Doug Zongkerfc44a512014-08-26 13:10:25 -0700160def AddVendor(output_zip, prefix="IMAGES/"):
161 """Turn the contents of VENDOR into a vendor image and store in it
162 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800163
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800164 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img")
165 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800166 print("vendor.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800167 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800168
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800169 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map")
170 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img,
171 block_list=block_list)
172 return img.name
Doug Zongker3c84f562014-07-31 11:06:30 -0700173
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700174
Tao Baoc633ed02017-05-30 21:46:33 -0700175def AddDtbo(output_zip, prefix="IMAGES/"):
176 """Adds the DTBO image.
177
178 Uses the image under prefix if it already exists. Otherwise looks for the
179 image under PREBUILT_IMAGES/, signs it as needed, and returns the image name.
180 """
181
182 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img")
183 if os.path.exists(img.input_name):
184 print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,))
185 return img.input_name
186
187 dtbo_prebuilt_path = os.path.join(
188 OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img")
189 assert os.path.exists(dtbo_prebuilt_path)
190 shutil.copy(dtbo_prebuilt_path, img.name)
191
192 # AVB-sign the image as needed.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800193 if OPTIONS.info_dict.get("avb_enable") == "true":
Tao Baoc633ed02017-05-30 21:46:33 -0700194 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Tao Bao3ebfdde2017-05-23 23:06:55 -0700195 part_size = OPTIONS.info_dict["dtbo_size"]
Tao Baoc633ed02017-05-30 21:46:33 -0700196 # The AVB hash footer will be replaced if already present.
197 cmd = [avbtool, "add_hash_footer", "--image", img.name,
198 "--partition_size", str(part_size), "--partition_name", "dtbo"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800199 common.AppendAVBSigningArgs(cmd, "dtbo")
200 args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args")
Tao Baoc633ed02017-05-30 21:46:33 -0700201 if args and args.strip():
202 cmd.extend(shlex.split(args))
203 p = common.Run(cmd, stdout=subprocess.PIPE)
204 p.communicate()
205 assert p.returncode == 0, \
206 "avbtool add_hash_footer of %s failed" % (img.name,)
207
208 img.Write()
209 return img.name
210
Doug Zongker3c84f562014-07-31 11:06:30 -0700211
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800212def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800213 print("creating " + what + ".img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700214
Doug Zongker3c84f562014-07-31 11:06:30 -0700215 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
216 fstab = info_dict["fstab"]
Tianjie Xucfa86222016-03-07 16:31:19 -0800217 mount_point = "/" + what
218 if fstab and mount_point in fstab:
219 image_props["fs_type"] = fstab[mount_point].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700220
Tao Bao822f5842015-09-30 16:01:14 -0700221 # Use a fixed timestamp (01/01/2009) when packaging the image.
222 # Bug: 24377993
223 epoch = datetime.datetime.fromtimestamp(0)
224 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
225 image_props["timestamp"] = int(timestamp)
226
Doug Zongker3c84f562014-07-31 11:06:30 -0700227 if what == "system":
228 fs_config_prefix = ""
229 else:
230 fs_config_prefix = what + "_"
231
232 fs_config = os.path.join(
233 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700234 if not os.path.exists(fs_config):
235 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700236
Ying Wanga2292c92015-03-24 19:07:40 -0700237 # Override values loaded from info_dict.
238 if fs_config:
239 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700240 if block_list:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800241 image_props["block_list"] = block_list.name
Ying Wanga2292c92015-03-24 19:07:40 -0700242
Tao Baod86e3112017-09-22 15:45:33 -0700243 # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and
244 # build fingerprint).
245 uuid_seed = what + "-"
246 if "build.prop" in info_dict:
247 build_prop = info_dict["build.prop"]
248 if "ro.build.fingerprint" in build_prop:
249 uuid_seed += build_prop["ro.build.fingerprint"]
250 elif "ro.build.thumbprint" in build_prop:
251 uuid_seed += build_prop["ro.build.thumbprint"]
252 image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed))
253 hash_seed = "hash_seed-" + uuid_seed
254 image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))
255
Tao Baofa863c82017-05-23 23:49:03 -0700256 succ = build_image.BuildImage(os.path.join(input_dir, what.upper()),
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800257 image_props, output_file.name)
Doug Zongker3c84f562014-07-31 11:06:30 -0700258 assert succ, "build " + what + ".img image failed"
259
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800260 output_file.Write()
261 if block_list:
262 block_list.Write()
263
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700264 # Set the 'adjusted_partition_size' that excludes the verity blocks of the
265 # given image. When avb is enabled, this size is the max image size returned
266 # by the avb tool.
Tianjie Xuf1a13182017-01-19 17:39:30 -0800267 is_verity_partition = "verity_block_device" in image_props
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700268 verity_supported = (image_props.get("verity") == "true" or
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800269 image_props.get("avb_enable") == "true")
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700270 is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
271 if verity_supported and (is_verity_partition or is_avb_enable):
Tianjie Xuf1a13182017-01-19 17:39:30 -0800272 adjusted_blocks_value = image_props.get("partition_size")
273 if adjusted_blocks_value:
274 adjusted_blocks_key = what + "_adjusted_partition_size"
275 info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1
276
Doug Zongker3c84f562014-07-31 11:06:30 -0700277
278def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700279 """Create a userdata image and store it in output_zip.
280
281 In most case we just create and store an empty userdata.img;
282 But the invoker can also request to create userdata.img with real
283 data from the target files, by setting "userdata_img_with_data=true"
284 in OPTIONS.info_dict.
285 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700286
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800287 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img")
288 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800289 print("userdata.img already exists in %s, no need to rebuild..." % (
290 prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800291 return
292
Elliott Hughes305b0882016-06-15 17:04:54 -0700293 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700294 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700295 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700296 return
297
Tao Bao89fbb0f2017-01-10 10:47:58 -0800298 print("creating userdata.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700299
Tao Bao822f5842015-09-30 16:01:14 -0700300 # Use a fixed timestamp (01/01/2009) when packaging the image.
301 # Bug: 24377993
302 epoch = datetime.datetime.fromtimestamp(0)
303 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
304 image_props["timestamp"] = int(timestamp)
305
Tao Baofa863c82017-05-23 23:49:03 -0700306 if OPTIONS.info_dict.get("userdata_img_with_data") == "true":
307 user_dir = os.path.join(OPTIONS.input_tmp, "DATA")
Ying Wang2a048392015-06-25 13:56:53 -0700308 else:
Tao Bao1c830bf2017-12-25 10:43:47 -0800309 user_dir = common.MakeTempDir()
Ying Wang2a048392015-06-25 13:56:53 -0700310
Doug Zongker3c84f562014-07-31 11:06:30 -0700311 fstab = OPTIONS.info_dict["fstab"]
312 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700313 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700314 succ = build_image.BuildImage(user_dir, image_props, img.name)
315 assert succ, "build userdata.img image failed"
316
317 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800318 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700319
320
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800321def AppendVBMetaArgsForPartition(cmd, partition, img_path, public_key_dir):
322 if not img_path:
323 return
324
325 # Check if chain partition is used.
326 key_path = OPTIONS.info_dict.get("avb_" + partition + "_key_path")
327 if key_path:
328 # extract public key in AVB format to be included in vbmeta.img
329 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
330 public_key_path = os.path.join(public_key_dir, "%s.avbpubkey" % partition)
331 p = common.Run([avbtool, "extract_public_key", "--key", key_path,
332 "--output", public_key_path],
333 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
334 p.communicate()
335 assert p.returncode == 0, \
336 "avbtool extract_public_key fail for partition: %r" % partition
337
338 rollback_index_location = OPTIONS.info_dict[
339 "avb_" + partition + "_rollback_index_location"]
340 cmd.extend(["--chain_partition", "%s:%s:%s" % (
341 partition, rollback_index_location, public_key_path)])
342 else:
343 cmd.extend(["--include_descriptors_from_image", img_path])
344
345
Tao Baobf70c312017-07-11 17:27:55 -0700346def AddVBMeta(output_zip, partitions, prefix="IMAGES/"):
347 """Creates a VBMeta image and store it in output_zip.
348
349 Args:
350 output_zip: The output zip file, which needs to be already open.
351 partitions: A dict that's keyed by partition names with image paths as
352 values. Only valid partition names are accepted, which include 'boot',
353 'recovery', 'system', 'vendor', 'dtbo'.
354 """
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800355 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img")
Tao Bao262bf3f2017-07-11 17:27:55 -0700356 if os.path.exists(img.input_name):
357 print("vbmeta.img already exists in %s; not rebuilding..." % (prefix,))
358 return img.input_name
359
Tao Baoc633ed02017-05-30 21:46:33 -0700360 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800361 cmd = [avbtool, "make_vbmeta_image", "--output", img.name]
362 common.AppendAVBSigningArgs(cmd, "vbmeta")
363
Tao Bao1c830bf2017-12-25 10:43:47 -0800364 public_key_dir = common.MakeTempDir(prefix="avbpubkey-")
Tao Baobf70c312017-07-11 17:27:55 -0700365 for partition, path in partitions.items():
366 assert partition in common.AVB_PARTITIONS, 'Unknown partition: %s' % (
367 partition,)
368 assert os.path.exists(path), 'Failed to find %s for partition %s' % (
369 path, partition)
370 AppendVBMetaArgsForPartition(cmd, partition, path, public_key_dir)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800371
372 args = OPTIONS.info_dict.get("avb_vbmeta_args")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400373 if args and args.strip():
Tao Bao9a5f4192017-07-20 23:51:16 -0700374 split_args = shlex.split(args)
375 for index, arg in enumerate(split_args[:-1]):
376 # Sanity check that the image file exists. Some images might be defined
377 # as a path relative to source tree, which may not be available at the
378 # same location when running this script (we have the input target_files
379 # zip only). For such cases, we additionally scan other locations (e.g.
380 # IMAGES/, RADIO/, etc) before bailing out.
381 if arg == '--include_descriptors_from_image':
382 image_path = split_args[index + 1]
383 if os.path.exists(image_path):
384 continue
385 found = False
Tao Bao6b9fef52017-12-01 16:13:22 -0800386 for dir_name in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']:
Tao Bao9a5f4192017-07-20 23:51:16 -0700387 alt_path = os.path.join(
Tao Bao6b9fef52017-12-01 16:13:22 -0800388 OPTIONS.input_tmp, dir_name, os.path.basename(image_path))
Tao Bao9a5f4192017-07-20 23:51:16 -0700389 if os.path.exists(alt_path):
390 split_args[index + 1] = alt_path
391 found = True
392 break
393 assert found, 'failed to find %s' % (image_path,)
394 cmd.extend(split_args)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800395
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400396 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
397 p.communicate()
398 assert p.returncode == 0, "avbtool make_vbmeta_image failed"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800399 img.Write()
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400400
401
David Zeuthen25328622016-04-08 15:08:03 -0400402def AddPartitionTable(output_zip, prefix="IMAGES/"):
403 """Create a partition table image and store it in output_zip."""
404
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800405 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img")
406 bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt")
David Zeuthen25328622016-04-08 15:08:03 -0400407
408 # use BPTTOOL from environ, or "bpttool" if empty or not set.
409 bpttool = os.getenv("BPTTOOL") or "bpttool"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800410 cmd = [bpttool, "make_table", "--output_json", bpt.name,
411 "--output_gpt", img.name]
David Zeuthen25328622016-04-08 15:08:03 -0400412 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
413 input_files = input_files_str.split(" ")
414 for i in input_files:
415 cmd.extend(["--input", i])
416 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
417 if disk_size:
418 cmd.extend(["--disk_size", disk_size])
419 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
420 if args:
421 cmd.extend(shlex.split(args))
422
423 p = common.Run(cmd, stdout=subprocess.PIPE)
424 p.communicate()
425 assert p.returncode == 0, "bpttool make_table failed"
426
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800427 img.Write()
428 bpt.Write()
David Zeuthen25328622016-04-08 15:08:03 -0400429
430
Doug Zongker3c84f562014-07-31 11:06:30 -0700431def AddCache(output_zip, prefix="IMAGES/"):
432 """Create an empty cache image and store it in output_zip."""
433
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800434 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img")
435 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800436 print("cache.img already exists in %s, no need to rebuild..." % (prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800437 return
438
Tao Bao2c15d9e2015-07-09 11:51:16 -0700439 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700440 # The build system has to explicitly request for cache.img.
441 if "fs_type" not in image_props:
442 return
443
Tao Bao89fbb0f2017-01-10 10:47:58 -0800444 print("creating cache.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700445
Tao Bao822f5842015-09-30 16:01:14 -0700446 # Use a fixed timestamp (01/01/2009) when packaging the image.
447 # Bug: 24377993
448 epoch = datetime.datetime.fromtimestamp(0)
449 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
450 image_props["timestamp"] = int(timestamp)
451
Tao Bao1c830bf2017-12-25 10:43:47 -0800452 user_dir = common.MakeTempDir()
Doug Zongker3c84f562014-07-31 11:06:30 -0700453
454 fstab = OPTIONS.info_dict["fstab"]
455 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700456 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700457 succ = build_image.BuildImage(user_dir, image_props, img.name)
458 assert succ, "build cache.img image failed"
459
460 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800461 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700462
463
Tao Baobea20ac2018-01-17 17:57:49 -0800464def AddRadioImagesForAbOta(output_zip, ab_partitions):
465 """Adds the radio images needed for A/B OTA to the output file.
466
467 It parses the list of A/B partitions, looks for the missing ones from RADIO/
468 or VENDOR_IMAGES/ dirs, and copies them to IMAGES/ of the output file (or
469 dir).
470
471 It also ensures that on returning from the function all the listed A/B
472 partitions must have their images available under IMAGES/.
473
474 Args:
475 output_zip: The output zip file (needs to be already open), or None to
476 write images to OPTIONS.input_tmp/.
477 ab_partitions: The list of A/B partitions.
478
479 Raises:
480 AssertionError: If it can't find an image.
481 """
482 for partition in ab_partitions:
483 img_name = partition.strip() + ".img"
484 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
485 if os.path.exists(prebuilt_path):
486 print("%s already exists, no need to overwrite..." % (img_name,))
487 continue
488
489 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
490 if os.path.exists(img_radio_path):
491 if output_zip:
Tao Baoa2ff4c92018-01-17 12:14:43 -0800492 common.ZipWrite(output_zip, img_radio_path, "IMAGES/" + img_name)
Tao Baobea20ac2018-01-17 17:57:49 -0800493 else:
494 shutil.copy(img_radio_path, prebuilt_path)
Tao Baoa2ff4c92018-01-17 12:14:43 -0800495 continue
Tao Baobea20ac2018-01-17 17:57:49 -0800496
Tao Baoa2ff4c92018-01-17 12:14:43 -0800497 # Walk through VENDOR_IMAGES/ since files could be under subdirs.
498 img_vendor_dir = os.path.join(OPTIONS.input_tmp, "VENDOR_IMAGES")
499 for root, _, files in os.walk(img_vendor_dir):
500 if img_name in files:
501 if output_zip:
502 common.ZipWrite(output_zip, os.path.join(root, img_name),
503 "IMAGES/" + img_name)
504 else:
505 shutil.copy(os.path.join(root, img_name), prebuilt_path)
506 break
507
508 # Assert that the image is present under IMAGES/ now.
Tao Baobea20ac2018-01-17 17:57:49 -0800509 if output_zip:
510 # Zip spec says: All slashes MUST be forward slashes.
511 img_path = 'IMAGES/' + img_name
512 assert img_path in output_zip.namelist(), "cannot find " + img_name
513 else:
514 img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
515 assert os.path.exists(img_path), "cannot find " + img_name
516
517
518def AddCareMapTxtForAbOta(output_zip, ab_partitions, image_paths):
519 """Generates and adds care_map.txt for system and vendor partitions.
520
521 Args:
522 output_zip: The output zip file (needs to be already open), or None to
523 write images to OPTIONS.input_tmp/.
524 ab_partitions: The list of A/B partitions.
525 image_paths: A map from the partition name to the image path.
526 """
527 care_map_list = []
528 for partition in ab_partitions:
529 partition = partition.strip()
Tao Baoa2ff4c92018-01-17 12:14:43 -0800530 if partition not in PARTITIONS_WITH_CARE_MAP:
531 continue
532
533 verity_block_device = "{}_verity_block_device".format(partition)
534 avb_hashtree_enable = "avb_{}_hashtree_enable".format(partition)
535 if (verity_block_device in OPTIONS.info_dict or
536 OPTIONS.info_dict.get(avb_hashtree_enable) == "true"):
537 image_path = image_paths[partition]
538 assert os.path.exists(image_path)
539 care_map_list += GetCareMap(partition, image_path)
Tao Baobea20ac2018-01-17 17:57:49 -0800540
541 if care_map_list:
542 care_map_path = "META/care_map.txt"
543 if output_zip and care_map_path not in output_zip.namelist():
544 common.ZipWriteStr(output_zip, care_map_path, '\n'.join(care_map_list))
545 else:
546 with open(os.path.join(OPTIONS.input_tmp, care_map_path), 'w') as fp:
547 fp.write('\n'.join(care_map_list))
548 if output_zip:
549 OPTIONS.replace_updated_files_list.append(care_map_path)
550
551
552def AddPackRadioImages(output_zip, images):
553 """Copies images listed in META/pack_radioimages.txt from RADIO/ to IMAGES/.
554
555 Args:
556 output_zip: The output zip file (needs to be already open), or None to
557 write images to OPTIONS.input_tmp/.
558 images: A list of image names.
559
560 Raises:
561 AssertionError: If a listed image can't be found.
562 """
563 for image in images:
564 img_name = image.strip()
565 _, ext = os.path.splitext(img_name)
566 if not ext:
567 img_name += ".img"
Tao Baoa2ff4c92018-01-17 12:14:43 -0800568
Tao Baobea20ac2018-01-17 17:57:49 -0800569 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
570 if os.path.exists(prebuilt_path):
571 print("%s already exists, no need to overwrite..." % (img_name,))
572 continue
573
574 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
575 assert os.path.exists(img_radio_path), \
576 "Failed to find %s at %s" % (img_name, img_radio_path)
Tao Baoa2ff4c92018-01-17 12:14:43 -0800577
Tao Baobea20ac2018-01-17 17:57:49 -0800578 if output_zip:
Tao Baoa2ff4c92018-01-17 12:14:43 -0800579 common.ZipWrite(output_zip, img_radio_path, "IMAGES/" + img_name)
Tao Baobea20ac2018-01-17 17:57:49 -0800580 else:
581 shutil.copy(img_radio_path, prebuilt_path)
582
583
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700584def ReplaceUpdatedFiles(zip_filename, files_list):
Tao Bao89d7ab22017-12-14 17:05:33 -0800585 """Updates all the ZIP entries listed in files_list.
Tianjie Xu38af07f2017-05-25 17:38:53 -0700586
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700587 For now the list includes META/care_map.txt, and the related files under
588 SYSTEM/ after rebuilding recovery.
589 """
Tao Bao89d7ab22017-12-14 17:05:33 -0800590 common.ZipDelete(zip_filename, files_list)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700591 output_zip = zipfile.ZipFile(zip_filename, "a",
592 compression=zipfile.ZIP_DEFLATED,
593 allowZip64=True)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700594 for item in files_list:
Tianjie Xu38af07f2017-05-25 17:38:53 -0700595 file_path = os.path.join(OPTIONS.input_tmp, item)
596 assert os.path.exists(file_path)
597 common.ZipWrite(output_zip, file_path, arcname=item)
598 common.ZipClose(output_zip)
599
600
Doug Zongker3c84f562014-07-31 11:06:30 -0700601def AddImagesToTargetFiles(filename):
Tao Baoae396d92017-11-20 11:56:43 -0800602 """Creates and adds images (boot/recovery/system/...) to a target_files.zip.
603
604 It works with either a zip file (zip mode), or a directory that contains the
605 files to be packed into a target_files.zip (dir mode). The latter is used when
606 being called from build/make/core/Makefile.
607
608 The images will be created under IMAGES/ in the input target_files.zip.
609
610 Args:
611 filename: the target_files.zip, or the zip root directory.
612 """
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800613 if os.path.isdir(filename):
614 OPTIONS.input_tmp = os.path.abspath(filename)
615 input_zip = None
616 else:
617 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700618
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800619 if not OPTIONS.add_missing:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800620 if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")):
621 print("target_files appears to already contain images.")
622 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700623
Tao Baob22afea2017-09-12 12:39:09 -0700624 # vendor.img is unlike system.img or system_other.img. Because it could be
625 # built from source, or dropped into target_files.zip as a prebuilt blob. We
626 # consider either of them as vendor.img being available, which could be used
627 # when generating vbmeta.img for AVB.
628 has_vendor = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) or
629 os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES",
630 "vendor.img")))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800631 has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp,
632 "SYSTEM_OTHER"))
Doug Zongker3c84f562014-07-31 11:06:30 -0700633
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800634 if input_zip:
635 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Alex Light4e358ab2016-06-16 14:47:10 -0700636
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800637 common.ZipClose(input_zip)
638 output_zip = zipfile.ZipFile(filename, "a",
639 compression=zipfile.ZIP_DEFLATED,
640 allowZip64=True)
641 else:
642 OPTIONS.info_dict = common.LoadInfoDict(filename, filename)
643 output_zip = None
Tao Baoae396d92017-11-20 11:56:43 -0800644
645 # Always make input_tmp/IMAGES available, since we may stage boot / recovery
646 # images there even under zip mode. The directory will be cleaned up as part
647 # of OPTIONS.input_tmp.
648 images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")
649 if not os.path.isdir(images_dir):
650 os.makedirs(images_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700651
Tao Baodb45efa2015-10-27 19:25:18 -0700652 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
653
Tao Bao2b6dfd62017-09-27 17:17:43 -0700654 if OPTIONS.info_dict.get("avb_enable") == "true":
655 fp = None
656 if "build.prop" in OPTIONS.info_dict:
657 build_prop = OPTIONS.info_dict["build.prop"]
658 if "ro.build.fingerprint" in build_prop:
659 fp = build_prop["ro.build.fingerprint"]
660 elif "ro.build.thumbprint" in build_prop:
661 fp = build_prop["ro.build.thumbprint"]
662 if fp:
663 OPTIONS.info_dict["avb_salt"] = hashlib.sha256(fp).hexdigest()
664
Tao Baobf70c312017-07-11 17:27:55 -0700665 # A map between partition names and their paths, which could be used when
666 # generating AVB vbmeta image.
667 partitions = dict()
668
Doug Zongkerfc44a512014-08-26 13:10:25 -0700669 def banner(s):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800670 print("\n\n++++ " + s + " ++++\n\n")
Doug Zongker3c84f562014-07-31 11:06:30 -0700671
Tao Bao262bf3f2017-07-11 17:27:55 -0700672 banner("boot")
673 # common.GetBootableImage() returns the image directly if present.
674 boot_image = common.GetBootableImage(
675 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
676 # boot.img may be unavailable in some targets (e.g. aosp_arm64).
677 if boot_image:
Tao Baobf70c312017-07-11 17:27:55 -0700678 partitions['boot'] = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
679 if not os.path.exists(partitions['boot']):
Tao Bao262bf3f2017-07-11 17:27:55 -0700680 boot_image.WriteToDir(OPTIONS.input_tmp)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800681 if output_zip:
682 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700683
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800684 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700685 if has_recovery:
686 banner("recovery")
Tao Bao262bf3f2017-07-11 17:27:55 -0700687 recovery_image = common.GetBootableImage(
688 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
689 assert recovery_image, "Failed to create recovery.img."
Tao Baobf70c312017-07-11 17:27:55 -0700690 partitions['recovery'] = os.path.join(
Tao Bao262bf3f2017-07-11 17:27:55 -0700691 OPTIONS.input_tmp, "IMAGES", "recovery.img")
Tao Baobf70c312017-07-11 17:27:55 -0700692 if not os.path.exists(partitions['recovery']):
Tao Bao262bf3f2017-07-11 17:27:55 -0700693 recovery_image.WriteToDir(OPTIONS.input_tmp)
694 if output_zip:
695 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700696
Tao Baod42e97e2016-11-30 12:11:57 -0800697 banner("recovery (two-step image)")
698 # The special recovery.img for two-step package use.
699 recovery_two_step_image = common.GetBootableImage(
700 "IMAGES/recovery-two-step.img", "recovery-two-step.img",
701 OPTIONS.input_tmp, "RECOVERY", two_step_image=True)
Tao Bao262bf3f2017-07-11 17:27:55 -0700702 assert recovery_two_step_image, "Failed to create recovery-two-step.img."
703 recovery_two_step_image_path = os.path.join(
704 OPTIONS.input_tmp, "IMAGES", "recovery-two-step.img")
705 if not os.path.exists(recovery_two_step_image_path):
706 recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800707 if output_zip:
708 recovery_two_step_image.AddToZip(output_zip)
Tao Baod42e97e2016-11-30 12:11:57 -0800709
Doug Zongkerfc44a512014-08-26 13:10:25 -0700710 banner("system")
Tao Baobea20ac2018-01-17 17:57:49 -0800711 partitions['system'] = AddSystem(
Tao Baoc633ed02017-05-30 21:46:33 -0700712 output_zip, recovery_img=recovery_image, boot_img=boot_image)
Tao Baobf70c312017-07-11 17:27:55 -0700713
Doug Zongkerfc44a512014-08-26 13:10:25 -0700714 if has_vendor:
715 banner("vendor")
Tao Baobea20ac2018-01-17 17:57:49 -0800716 partitions['vendor'] = AddVendor(output_zip)
Tao Baobf70c312017-07-11 17:27:55 -0700717
Alex Light4e358ab2016-06-16 14:47:10 -0700718 if has_system_other:
719 banner("system_other")
720 AddSystemOther(output_zip)
Tao Baobf70c312017-07-11 17:27:55 -0700721
Tianjie Xub48589a2016-08-03 19:21:52 -0700722 if not OPTIONS.is_signing:
723 banner("userdata")
724 AddUserdata(output_zip)
725 banner("cache")
726 AddCache(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700727
728 if OPTIONS.info_dict.get("board_bpt_enable") == "true":
David Zeuthen25328622016-04-08 15:08:03 -0400729 banner("partition-table")
730 AddPartitionTable(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700731
Tao Baoc633ed02017-05-30 21:46:33 -0700732 if OPTIONS.info_dict.get("has_dtbo") == "true":
733 banner("dtbo")
Tao Baobf70c312017-07-11 17:27:55 -0700734 partitions['dtbo'] = AddDtbo(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700735
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800736 if OPTIONS.info_dict.get("avb_enable") == "true":
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400737 banner("vbmeta")
Tao Baobf70c312017-07-11 17:27:55 -0700738 AddVBMeta(output_zip, partitions)
Doug Zongker3c84f562014-07-31 11:06:30 -0700739
Tianjie Xuaaca4212016-06-28 14:34:03 -0700740 banner("radio")
Tao Baobea20ac2018-01-17 17:57:49 -0800741 ab_partitions_txt = os.path.join(OPTIONS.input_tmp, "META",
742 "ab_partitions.txt")
743 if os.path.exists(ab_partitions_txt):
744 with open(ab_partitions_txt, 'r') as f:
745 ab_partitions = f.readlines()
Tianjie Xucfa86222016-03-07 16:31:19 -0800746
Tao Baobea20ac2018-01-17 17:57:49 -0800747 # For devices using A/B update, copy over images from RADIO/ and/or
748 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
749 # images ready under IMAGES/. All images should have '.img' as extension.
750 AddRadioImagesForAbOta(output_zip, ab_partitions)
Tianjie Xuaaca4212016-06-28 14:34:03 -0700751
Tao Baobea20ac2018-01-17 17:57:49 -0800752 # Generate care_map.txt for system and vendor partitions (if present), then
753 # write this file to target_files package.
754 AddCareMapTxtForAbOta(output_zip, ab_partitions, partitions)
Tianjie Xucfa86222016-03-07 16:31:19 -0800755
Tao Bao95a95c32017-06-16 15:30:23 -0700756 # Radio images that need to be packed into IMAGES/, and product-img.zip.
Tao Baobea20ac2018-01-17 17:57:49 -0800757 pack_radioimages_txt = os.path.join(
Tao Bao95a95c32017-06-16 15:30:23 -0700758 OPTIONS.input_tmp, "META", "pack_radioimages.txt")
Tao Baobea20ac2018-01-17 17:57:49 -0800759 if os.path.exists(pack_radioimages_txt):
760 with open(pack_radioimages_txt, 'r') as f:
761 AddPackRadioImages(output_zip, f.readlines())
Tao Bao95a95c32017-06-16 15:30:23 -0700762
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800763 if output_zip:
764 common.ZipClose(output_zip)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700765 if OPTIONS.replace_updated_files_list:
766 ReplaceUpdatedFiles(output_zip.filename,
767 OPTIONS.replace_updated_files_list)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700768
Doug Zongker3c84f562014-07-31 11:06:30 -0700769
Doug Zongker3c84f562014-07-31 11:06:30 -0700770def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700771 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800772 if o in ("-a", "--add_missing"):
773 OPTIONS.add_missing = True
774 elif o in ("-r", "--rebuild_recovery",):
775 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700776 elif o == "--replace_verity_private_key":
777 OPTIONS.replace_verity_private_key = (True, a)
778 elif o == "--replace_verity_public_key":
779 OPTIONS.replace_verity_public_key = (True, a)
Tianjie Xub48589a2016-08-03 19:21:52 -0700780 elif o == "--is_signing":
781 OPTIONS.is_signing = True
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800782 else:
783 return False
784 return True
785
Dan Albert8b72aef2015-03-23 19:13:21 -0700786 args = common.ParseOptions(
787 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700788 extra_long_opts=["add_missing", "rebuild_recovery",
789 "replace_verity_public_key=",
790 "replace_verity_private_key=",
Tao Bao45810422016-10-17 16:20:12 -0700791 "is_signing"],
Dan Albert8b72aef2015-03-23 19:13:21 -0700792 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800793
Doug Zongker3c84f562014-07-31 11:06:30 -0700794
795 if len(args) != 1:
796 common.Usage(__doc__)
797 sys.exit(1)
798
799 AddImagesToTargetFiles(args[0])
Tao Bao89fbb0f2017-01-10 10:47:58 -0800800 print("done.")
Doug Zongker3c84f562014-07-31 11:06:30 -0700801
802if __name__ == '__main__':
803 try:
804 common.CloseInheritedPipes()
805 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700806 except common.ExternalError as e:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800807 print("\n ERROR: %s\n" % (e,))
Doug Zongker3c84f562014-07-31 11:06:30 -0700808 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700809 finally:
810 common.Cleanup()