Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 1 | #!/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 | """ |
| 18 | Given a target-files zipfile that does not contain images (ie, does |
| 19 | not have an IMAGES/ top-level subdirectory), produce the images and |
| 20 | add them to the zipfile. |
| 21 | |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 22 | Usage: 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 Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 44 | """ |
| 45 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 46 | from __future__ import print_function |
| 47 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 48 | import sys |
| 49 | |
| 50 | if sys.hexversion < 0x02070000: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 51 | print("Python 2.7 or newer is required.", file=sys.stderr) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 52 | sys.exit(1) |
| 53 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 54 | import datetime |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 55 | import errno |
| 56 | import os |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 57 | import shlex |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 58 | import shutil |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 59 | import subprocess |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 60 | import tempfile |
| 61 | import zipfile |
| 62 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 63 | import build_image |
| 64 | import common |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 65 | import rangelib |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 66 | import sparse_img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 67 | |
| 68 | OPTIONS = common.OPTIONS |
| 69 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 70 | OPTIONS.add_missing = False |
| 71 | OPTIONS.rebuild_recovery = False |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 72 | OPTIONS.replace_verity_public_key = False |
| 73 | OPTIONS.replace_verity_private_key = False |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 74 | OPTIONS.is_signing = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 75 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 76 | |
| 77 | class OutputFile(object): |
| 78 | def __init__(self, output_zip, input_dir, prefix, name): |
| 79 | self._output_zip = output_zip |
| 80 | self.input_name = os.path.join(input_dir, prefix, name) |
| 81 | |
| 82 | if self._output_zip: |
| 83 | self._zip_name = os.path.join(prefix, name) |
| 84 | |
| 85 | root, suffix = os.path.splitext(name) |
| 86 | self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix) |
| 87 | else: |
| 88 | self.name = self.input_name |
| 89 | |
| 90 | def Write(self): |
| 91 | if self._output_zip: |
| 92 | common.ZipWrite(self._output_zip, self.name, self._zip_name) |
| 93 | |
| 94 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 95 | def GetCareMap(which, imgname): |
| 96 | """Generate care_map of system (or vendor) partition""" |
| 97 | |
| 98 | assert which in ("system", "vendor") |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 99 | |
| 100 | simg = sparse_img.SparseImage(imgname) |
| 101 | care_map_list = [] |
Tianjie Xu | 955629b | 2017-03-01 11:48:25 -0800 | [diff] [blame] | 102 | care_map_list.append(which) |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 103 | |
| 104 | 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 | |
| 112 | care_map_list.append(care_map_ranges.to_string_raw()) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 113 | return care_map_list |
| 114 | |
| 115 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 116 | def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 117 | """Turn the contents of SYSTEM into a system image and store it in |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 118 | output_zip. Returns the name of the system image file.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 119 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 120 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img") |
| 121 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 122 | print("system.img already exists in %s, no need to rebuild..." % (prefix,)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 123 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 124 | |
| 125 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 126 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 127 | ofile.write(data) |
| 128 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 129 | |
| 130 | if OPTIONS.rebuild_recovery: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 131 | print("Building new recovery patch") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 132 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 133 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 134 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 135 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map") |
| 136 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img, |
| 137 | block_list=block_list) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 138 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 139 | return img.name |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 140 | |
| 141 | |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 142 | def AddSystemOther(output_zip, prefix="IMAGES/"): |
| 143 | """Turn the contents of SYSTEM_OTHER into a system_other image |
| 144 | and store it in output_zip.""" |
| 145 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 146 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img") |
| 147 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 148 | print("system_other.img already exists in %s, no need to rebuild..." % ( |
| 149 | prefix,)) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 150 | return |
| 151 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 152 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 153 | |
| 154 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 155 | def AddVendor(output_zip, prefix="IMAGES/"): |
| 156 | """Turn the contents of VENDOR into a vendor image and store in it |
| 157 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 158 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 159 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img") |
| 160 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 161 | print("vendor.img already exists in %s, no need to rebuild..." % (prefix,)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 162 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 163 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 164 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map") |
| 165 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img, |
| 166 | block_list=block_list) |
| 167 | return img.name |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 168 | |
| 169 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 170 | def CreateImage(input_dir, info_dict, what, output_file, block_list=None): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 171 | print("creating " + what + ".img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 172 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 173 | # The name of the directory it is making an image out of matters to |
| 174 | # mkyaffs2image. It wants "system" but we have a directory named |
| 175 | # "SYSTEM", so create a symlink. |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 176 | temp_dir = tempfile.mkdtemp() |
| 177 | OPTIONS.tempfiles.append(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 178 | try: |
| 179 | os.symlink(os.path.join(input_dir, what.upper()), |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 180 | os.path.join(temp_dir, what)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 181 | except OSError as e: |
| 182 | # bogus error on my mac version? |
| 183 | # File "./build/tools/releasetools/img_from_target_files" |
| 184 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 185 | # OSError: [Errno 17] File exists |
| 186 | if e.errno == errno.EEXIST: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 187 | pass |
| 188 | |
| 189 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 190 | fstab = info_dict["fstab"] |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 191 | mount_point = "/" + what |
| 192 | if fstab and mount_point in fstab: |
| 193 | image_props["fs_type"] = fstab[mount_point].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 194 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 195 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 196 | # Bug: 24377993 |
| 197 | epoch = datetime.datetime.fromtimestamp(0) |
| 198 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 199 | image_props["timestamp"] = int(timestamp) |
| 200 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 201 | if what == "system": |
| 202 | fs_config_prefix = "" |
| 203 | else: |
| 204 | fs_config_prefix = what + "_" |
| 205 | |
| 206 | fs_config = os.path.join( |
| 207 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 208 | if not os.path.exists(fs_config): |
| 209 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 210 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 211 | # Override values loaded from info_dict. |
| 212 | if fs_config: |
| 213 | image_props["fs_config"] = fs_config |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 214 | if block_list: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 215 | image_props["block_list"] = block_list.name |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 216 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 217 | succ = build_image.BuildImage(os.path.join(temp_dir, what), |
| 218 | image_props, output_file.name) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 219 | assert succ, "build " + what + ".img image failed" |
| 220 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 221 | output_file.Write() |
| 222 | if block_list: |
| 223 | block_list.Write() |
| 224 | |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 225 | is_verity_partition = "verity_block_device" in image_props |
| 226 | verity_supported = image_props.get("verity") == "true" |
| 227 | if is_verity_partition and verity_supported: |
| 228 | adjusted_blocks_value = image_props.get("partition_size") |
| 229 | if adjusted_blocks_value: |
| 230 | adjusted_blocks_key = what + "_adjusted_partition_size" |
| 231 | info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1 |
| 232 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 233 | |
| 234 | def AddUserdata(output_zip, prefix="IMAGES/"): |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 235 | """Create a userdata image and store it in output_zip. |
| 236 | |
| 237 | In most case we just create and store an empty userdata.img; |
| 238 | But the invoker can also request to create userdata.img with real |
| 239 | data from the target files, by setting "userdata_img_with_data=true" |
| 240 | in OPTIONS.info_dict. |
| 241 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 242 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 243 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img") |
| 244 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 245 | print("userdata.img already exists in %s, no need to rebuild..." % ( |
| 246 | prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 247 | return |
| 248 | |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 249 | # Skip userdata.img if no size. |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 250 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 251 | if not image_props.get("partition_size"): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 252 | return |
| 253 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 254 | print("creating userdata.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 255 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 256 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 257 | # Bug: 24377993 |
| 258 | epoch = datetime.datetime.fromtimestamp(0) |
| 259 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 260 | image_props["timestamp"] = int(timestamp) |
| 261 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 262 | # The name of the directory it is making an image out of matters to |
| 263 | # mkyaffs2image. So we create a temp dir, and within it we create an |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 264 | # empty dir named "data", or a symlink to the DATA dir, |
| 265 | # and build the image from that. |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 266 | temp_dir = tempfile.mkdtemp() |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 267 | OPTIONS.tempfiles.append(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 268 | user_dir = os.path.join(temp_dir, "data") |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 269 | empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true") |
| 270 | if empty: |
| 271 | # Create an empty dir. |
| 272 | os.mkdir(user_dir) |
| 273 | else: |
| 274 | # Symlink to the DATA dir. |
| 275 | os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"), |
| 276 | user_dir) |
| 277 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 278 | fstab = OPTIONS.info_dict["fstab"] |
| 279 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 280 | image_props["fs_type"] = fstab["/data"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 281 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 282 | assert succ, "build userdata.img image failed" |
| 283 | |
| 284 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 285 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 286 | |
| 287 | |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 288 | def AddVBMeta(output_zip, boot_img_path, system_img_path, prefix="IMAGES/"): |
| 289 | """Create a VBMeta image and store it in output_zip.""" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 290 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 291 | avbtool = os.getenv('AVBTOOL') or "avbtool" |
| 292 | cmd = [avbtool, "make_vbmeta_image", |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 293 | "--output", img.name, |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 294 | "--include_descriptors_from_image", boot_img_path, |
| 295 | "--include_descriptors_from_image", system_img_path, |
| 296 | "--generate_dm_verity_cmdline_from_hashtree", system_img_path] |
| 297 | common.AppendAVBSigningArgs(cmd) |
| 298 | args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args", None) |
| 299 | if args and args.strip(): |
| 300 | cmd.extend(shlex.split(args)) |
| 301 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 302 | p.communicate() |
| 303 | assert p.returncode == 0, "avbtool make_vbmeta_image failed" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 304 | img.Write() |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 305 | |
| 306 | |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 307 | def AddPartitionTable(output_zip, prefix="IMAGES/"): |
| 308 | """Create a partition table image and store it in output_zip.""" |
| 309 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 310 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img") |
| 311 | bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt") |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 312 | |
| 313 | # use BPTTOOL from environ, or "bpttool" if empty or not set. |
| 314 | bpttool = os.getenv("BPTTOOL") or "bpttool" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 315 | cmd = [bpttool, "make_table", "--output_json", bpt.name, |
| 316 | "--output_gpt", img.name] |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 317 | input_files_str = OPTIONS.info_dict["board_bpt_input_files"] |
| 318 | input_files = input_files_str.split(" ") |
| 319 | for i in input_files: |
| 320 | cmd.extend(["--input", i]) |
| 321 | disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") |
| 322 | if disk_size: |
| 323 | cmd.extend(["--disk_size", disk_size]) |
| 324 | args = OPTIONS.info_dict.get("board_bpt_make_table_args") |
| 325 | if args: |
| 326 | cmd.extend(shlex.split(args)) |
| 327 | |
| 328 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 329 | p.communicate() |
| 330 | assert p.returncode == 0, "bpttool make_table failed" |
| 331 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 332 | img.Write() |
| 333 | bpt.Write() |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 334 | |
| 335 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 336 | def AddCache(output_zip, prefix="IMAGES/"): |
| 337 | """Create an empty cache image and store it in output_zip.""" |
| 338 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 339 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img") |
| 340 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 341 | print("cache.img already exists in %s, no need to rebuild..." % (prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 342 | return |
| 343 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 344 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 345 | # The build system has to explicitly request for cache.img. |
| 346 | if "fs_type" not in image_props: |
| 347 | return |
| 348 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 349 | print("creating cache.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 350 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 351 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 352 | # Bug: 24377993 |
| 353 | epoch = datetime.datetime.fromtimestamp(0) |
| 354 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 355 | image_props["timestamp"] = int(timestamp) |
| 356 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 357 | # The name of the directory it is making an image out of matters to |
| 358 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 359 | # empty dir named "cache", and build the image from that. |
| 360 | temp_dir = tempfile.mkdtemp() |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 361 | OPTIONS.tempfiles.append(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 362 | user_dir = os.path.join(temp_dir, "cache") |
| 363 | os.mkdir(user_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 364 | |
| 365 | fstab = OPTIONS.info_dict["fstab"] |
| 366 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 367 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 368 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 369 | assert succ, "build cache.img image failed" |
| 370 | |
| 371 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 372 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 373 | |
| 374 | |
| 375 | def AddImagesToTargetFiles(filename): |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 376 | if os.path.isdir(filename): |
| 377 | OPTIONS.input_tmp = os.path.abspath(filename) |
| 378 | input_zip = None |
| 379 | else: |
| 380 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 381 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 382 | if not OPTIONS.add_missing: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 383 | if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")): |
| 384 | print("target_files appears to already contain images.") |
| 385 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 386 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 387 | has_vendor = os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) |
| 388 | has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp, |
| 389 | "SYSTEM_OTHER")) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 390 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 391 | if input_zip: |
| 392 | OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 393 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 394 | common.ZipClose(input_zip) |
| 395 | output_zip = zipfile.ZipFile(filename, "a", |
| 396 | compression=zipfile.ZIP_DEFLATED, |
| 397 | allowZip64=True) |
| 398 | else: |
| 399 | OPTIONS.info_dict = common.LoadInfoDict(filename, filename) |
| 400 | output_zip = None |
| 401 | images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES") |
| 402 | if not os.path.isdir(images_dir): |
| 403 | os.makedirs(images_dir) |
| 404 | images_dir = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 405 | |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 406 | has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 407 | system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 408 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 409 | def banner(s): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 410 | print("\n\n++++ " + s + " ++++\n\n") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 411 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 412 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 413 | boot_image = None |
| 414 | if os.path.exists(prebuilt_path): |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 415 | banner("boot") |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 416 | print("boot.img already exists in IMAGES/, no need to rebuild...") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 417 | if OPTIONS.rebuild_recovery: |
| 418 | boot_image = common.GetBootableImage( |
| 419 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 420 | else: |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 421 | banner("boot") |
| 422 | boot_image = common.GetBootableImage( |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 423 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 424 | if boot_image: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 425 | if output_zip: |
| 426 | boot_image.AddToZip(output_zip) |
| 427 | else: |
| 428 | boot_image.WriteToDir(OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 429 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 430 | recovery_image = None |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 431 | if has_recovery: |
| 432 | banner("recovery") |
| 433 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") |
| 434 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 435 | print("recovery.img already exists in IMAGES/, no need to rebuild...") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 436 | if OPTIONS.rebuild_recovery: |
| 437 | recovery_image = common.GetBootableImage( |
| 438 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, |
| 439 | "RECOVERY") |
| 440 | else: |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 441 | recovery_image = common.GetBootableImage( |
| 442 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 443 | if recovery_image: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 444 | if output_zip: |
| 445 | recovery_image.AddToZip(output_zip) |
| 446 | else: |
| 447 | recovery_image.WriteToDir(OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 448 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 449 | banner("recovery (two-step image)") |
| 450 | # The special recovery.img for two-step package use. |
| 451 | recovery_two_step_image = common.GetBootableImage( |
| 452 | "IMAGES/recovery-two-step.img", "recovery-two-step.img", |
| 453 | OPTIONS.input_tmp, "RECOVERY", two_step_image=True) |
| 454 | if recovery_two_step_image: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 455 | if output_zip: |
| 456 | recovery_two_step_image.AddToZip(output_zip) |
| 457 | else: |
| 458 | recovery_two_step_image.WriteToDir(OPTIONS.input_tmp) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 459 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 460 | banner("system") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 461 | system_img_path = AddSystem( |
| 462 | output_zip, recovery_img=recovery_image, boot_img=boot_image) |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 463 | vendor_img_path = None |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 464 | if has_vendor: |
| 465 | banner("vendor") |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 466 | vendor_img_path = AddVendor(output_zip) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 467 | if has_system_other: |
| 468 | banner("system_other") |
| 469 | AddSystemOther(output_zip) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 470 | if not OPTIONS.is_signing: |
| 471 | banner("userdata") |
| 472 | AddUserdata(output_zip) |
| 473 | banner("cache") |
| 474 | AddCache(output_zip) |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 475 | if OPTIONS.info_dict.get("board_bpt_enable", None) == "true": |
| 476 | banner("partition-table") |
| 477 | AddPartitionTable(output_zip) |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 478 | if OPTIONS.info_dict.get("board_avb_enable", None) == "true": |
| 479 | banner("vbmeta") |
| 480 | boot_contents = boot_image.WriteToTemp() |
| 481 | AddVBMeta(output_zip, boot_contents.name, system_img_path) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 482 | |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 483 | # For devices using A/B update, copy over images from RADIO/ and/or |
| 484 | # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed |
| 485 | # images ready under IMAGES/. All images should have '.img' as extension. |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 486 | banner("radio") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 487 | ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") |
| 488 | if os.path.exists(ab_partitions): |
| 489 | with open(ab_partitions, 'r') as f: |
| 490 | lines = f.readlines() |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 491 | # For devices using A/B update, generate care_map for system and vendor |
| 492 | # partitions (if present), then write this file to target_files package. |
| 493 | care_map_list = [] |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 494 | for line in lines: |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 495 | if line.strip() == "system" and OPTIONS.info_dict.get( |
| 496 | "system_verity_block_device", None) is not None: |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 497 | assert os.path.exists(system_img_path) |
| 498 | care_map_list += GetCareMap("system", system_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 499 | if line.strip() == "vendor" and OPTIONS.info_dict.get( |
| 500 | "vendor_verity_block_device", None) is not None: |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 501 | assert os.path.exists(vendor_img_path) |
| 502 | care_map_list += GetCareMap("vendor", vendor_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 503 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 504 | img_name = line.strip() + ".img" |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 505 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 506 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 507 | print("%s already exists, no need to overwrite..." % (img_name,)) |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 508 | continue |
| 509 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 510 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 511 | img_vendor_dir = os.path.join( |
| 512 | OPTIONS.input_tmp, "VENDOR_IMAGES") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 513 | if os.path.exists(img_radio_path): |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 514 | if output_zip: |
| 515 | common.ZipWrite(output_zip, img_radio_path, |
| 516 | os.path.join("IMAGES", img_name)) |
| 517 | else: |
| 518 | shutil.copy(img_radio_path, prebuilt_path) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 519 | else: |
| 520 | for root, _, files in os.walk(img_vendor_dir): |
| 521 | if img_name in files: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 522 | if output_zip: |
| 523 | common.ZipWrite(output_zip, os.path.join(root, img_name), |
| 524 | os.path.join("IMAGES", img_name)) |
| 525 | else: |
| 526 | shutil.copy(os.path.join(root, img_name), prebuilt_path) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 527 | break |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 528 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 529 | if output_zip: |
| 530 | # Zip spec says: All slashes MUST be forward slashes. |
| 531 | img_path = 'IMAGES/' + img_name |
| 532 | assert img_path in output_zip.namelist(), "cannot find " + img_name |
| 533 | else: |
| 534 | img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 535 | assert os.path.exists(img_path), "cannot find " + img_name |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 536 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 537 | if care_map_list: |
| 538 | file_path = "META/care_map.txt" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 539 | if output_zip: |
| 540 | common.ZipWriteStr(output_zip, file_path, '\n'.join(care_map_list)) |
| 541 | else: |
| 542 | with open(os.path.join(OPTIONS.input_tmp, file_path), 'w') as fp: |
| 543 | fp.write('\n'.join(care_map_list)) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 544 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame^] | 545 | if output_zip: |
| 546 | common.ZipClose(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 547 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 548 | def main(argv): |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 549 | def option_handler(o, a): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 550 | if o in ("-a", "--add_missing"): |
| 551 | OPTIONS.add_missing = True |
| 552 | elif o in ("-r", "--rebuild_recovery",): |
| 553 | OPTIONS.rebuild_recovery = True |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 554 | elif o == "--replace_verity_private_key": |
| 555 | OPTIONS.replace_verity_private_key = (True, a) |
| 556 | elif o == "--replace_verity_public_key": |
| 557 | OPTIONS.replace_verity_public_key = (True, a) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 558 | elif o == "--is_signing": |
| 559 | OPTIONS.is_signing = True |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 560 | else: |
| 561 | return False |
| 562 | return True |
| 563 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 564 | args = common.ParseOptions( |
| 565 | argv, __doc__, extra_opts="ar", |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 566 | extra_long_opts=["add_missing", "rebuild_recovery", |
| 567 | "replace_verity_public_key=", |
| 568 | "replace_verity_private_key=", |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 569 | "is_signing"], |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 570 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 571 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 572 | |
| 573 | if len(args) != 1: |
| 574 | common.Usage(__doc__) |
| 575 | sys.exit(1) |
| 576 | |
| 577 | AddImagesToTargetFiles(args[0]) |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 578 | print("done.") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 579 | |
| 580 | if __name__ == '__main__': |
| 581 | try: |
| 582 | common.CloseInheritedPipes() |
| 583 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 584 | except common.ExternalError as e: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 585 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 586 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 587 | finally: |
| 588 | common.Cleanup() |