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