add_img_to_target_files.py revision 4536e45f66b7ca4b48be2941ae9f402768ecbd7f
10dc076565f772bb1953209fb69ea150b494aaa40robbiew#!/usr/bin/env python 20dc076565f772bb1953209fb69ea150b494aaa40robbiew# 30dc076565f772bb1953209fb69ea150b494aaa40robbiew# Copyright (C) 2014 The Android Open Source Project 40dc076565f772bb1953209fb69ea150b494aaa40robbiew# 50dc076565f772bb1953209fb69ea150b494aaa40robbiew# Licensed under the Apache License, Version 2.0 (the "License"); 60dc076565f772bb1953209fb69ea150b494aaa40robbiew# you may not use this file except in compliance with the License. 70dc076565f772bb1953209fb69ea150b494aaa40robbiew# You may obtain a copy of the License at 80dc076565f772bb1953209fb69ea150b494aaa40robbiew# 90dc076565f772bb1953209fb69ea150b494aaa40robbiew# http://www.apache.org/licenses/LICENSE-2.0 100dc076565f772bb1953209fb69ea150b494aaa40robbiew# 110dc076565f772bb1953209fb69ea150b494aaa40robbiew# Unless required by applicable law or agreed to in writing, software 120dc076565f772bb1953209fb69ea150b494aaa40robbiew# distributed under the License is distributed on an "AS IS" BASIS, 130dc076565f772bb1953209fb69ea150b494aaa40robbiew# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 140dc076565f772bb1953209fb69ea150b494aaa40robbiew# See the License for the specific language governing permissions and 150dc076565f772bb1953209fb69ea150b494aaa40robbiew# limitations under the License. 160dc076565f772bb1953209fb69ea150b494aaa40robbiew 170dc076565f772bb1953209fb69ea150b494aaa40robbiew""" 180dc076565f772bb1953209fb69ea150b494aaa40robbiewGiven a target-files zipfile that does not contain images (ie, does 190dc076565f772bb1953209fb69ea150b494aaa40robbiewnot have an IMAGES/ top-level subdirectory), produce the images and 200dc076565f772bb1953209fb69ea150b494aaa40robbiewadd them to the zipfile. 210dc076565f772bb1953209fb69ea150b494aaa40robbiew 220dc076565f772bb1953209fb69ea150b494aaa40robbiewUsage: add_img_to_target_files [flag] target_files 230dc076565f772bb1953209fb69ea150b494aaa40robbiew 240dc076565f772bb1953209fb69ea150b494aaa40robbiew -a (--add_missing) 250dc076565f772bb1953209fb69ea150b494aaa40robbiew Build and add missing images to "IMAGES/". If this option is 260dc076565f772bb1953209fb69ea150b494aaa40robbiew not specified, this script will simply exit when "IMAGES/" 270dc076565f772bb1953209fb69ea150b494aaa40robbiew directory exists in the target file. 280dc076565f772bb1953209fb69ea150b494aaa40robbiew 290dc076565f772bb1953209fb69ea150b494aaa40robbiew -r (--rebuild_recovery) 300dc076565f772bb1953209fb69ea150b494aaa40robbiew Rebuild the recovery patch and write it to the system image. Only 310dc076565f772bb1953209fb69ea150b494aaa40robbiew meaningful when system image needs to be rebuilt. 320dc076565f772bb1953209fb69ea150b494aaa40robbiew 330dc076565f772bb1953209fb69ea150b494aaa40robbiew --replace_verity_private_key 340dc076565f772bb1953209fb69ea150b494aaa40robbiew Replace the private key used for verity signing. (same as the option 350dc076565f772bb1953209fb69ea150b494aaa40robbiew in sign_target_files_apks) 360dc076565f772bb1953209fb69ea150b494aaa40robbiew 370dc076565f772bb1953209fb69ea150b494aaa40robbiew --replace_verity_public_key 380dc076565f772bb1953209fb69ea150b494aaa40robbiew Replace the certificate (public key) used for verity verification. (same 390dc076565f772bb1953209fb69ea150b494aaa40robbiew as the option in sign_target_files_apks) 400dc076565f772bb1953209fb69ea150b494aaa40robbiew 410dc076565f772bb1953209fb69ea150b494aaa40robbiew --is_signing 420dc076565f772bb1953209fb69ea150b494aaa40robbiew Skip building & adding the images for "userdata" and "cache" if we 430dc076565f772bb1953209fb69ea150b494aaa40robbiew are signing the target files. 440dc076565f772bb1953209fb69ea150b494aaa40robbiew""" 450dc076565f772bb1953209fb69ea150b494aaa40robbiew 460dc076565f772bb1953209fb69ea150b494aaa40robbiewfrom __future__ import print_function 470dc076565f772bb1953209fb69ea150b494aaa40robbiew 480dc076565f772bb1953209fb69ea150b494aaa40robbiewimport sys 490dc076565f772bb1953209fb69ea150b494aaa40robbiew 500dc076565f772bb1953209fb69ea150b494aaa40robbiewif sys.hexversion < 0x02070000: 510dc076565f772bb1953209fb69ea150b494aaa40robbiew print("Python 2.7 or newer is required.", file=sys.stderr) 520dc076565f772bb1953209fb69ea150b494aaa40robbiew sys.exit(1) 530dc076565f772bb1953209fb69ea150b494aaa40robbiew 540dc076565f772bb1953209fb69ea150b494aaa40robbiewimport datetime 550dc076565f772bb1953209fb69ea150b494aaa40robbiewimport errno 560dc076565f772bb1953209fb69ea150b494aaa40robbiewimport os 570dc076565f772bb1953209fb69ea150b494aaa40robbiewimport shlex 580dc076565f772bb1953209fb69ea150b494aaa40robbiewimport shutil 590dc076565f772bb1953209fb69ea150b494aaa40robbiewimport subprocess 600dc076565f772bb1953209fb69ea150b494aaa40robbiewimport tempfile 610dc076565f772bb1953209fb69ea150b494aaa40robbiewimport zipfile 620dc076565f772bb1953209fb69ea150b494aaa40robbiew 630dc076565f772bb1953209fb69ea150b494aaa40robbiewimport build_image 640dc076565f772bb1953209fb69ea150b494aaa40robbiewimport common 650dc076565f772bb1953209fb69ea150b494aaa40robbiewimport rangelib 660dc076565f772bb1953209fb69ea150b494aaa40robbiewimport sparse_img 670dc076565f772bb1953209fb69ea150b494aaa40robbiew 680dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS = common.OPTIONS 690dc076565f772bb1953209fb69ea150b494aaa40robbiew 700dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS.add_missing = False 710dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS.rebuild_recovery = False 720dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS.replace_recovery_patch_files_list = [] 730dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS.replace_verity_public_key = False 740dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS.replace_verity_private_key = False 750dc076565f772bb1953209fb69ea150b494aaa40robbiewOPTIONS.is_signing = False 760dc076565f772bb1953209fb69ea150b494aaa40robbiew 770dc076565f772bb1953209fb69ea150b494aaa40robbiew 780dc076565f772bb1953209fb69ea150b494aaa40robbiewclass OutputFile(object): 790dc076565f772bb1953209fb69ea150b494aaa40robbiew def __init__(self, output_zip, input_dir, prefix, name): 800dc076565f772bb1953209fb69ea150b494aaa40robbiew self._output_zip = output_zip 810dc076565f772bb1953209fb69ea150b494aaa40robbiew self.input_name = os.path.join(input_dir, prefix, name) 820dc076565f772bb1953209fb69ea150b494aaa40robbiew 830dc076565f772bb1953209fb69ea150b494aaa40robbiew if self._output_zip: 840dc076565f772bb1953209fb69ea150b494aaa40robbiew self._zip_name = os.path.join(prefix, name) 850dc076565f772bb1953209fb69ea150b494aaa40robbiew 860dc076565f772bb1953209fb69ea150b494aaa40robbiew root, suffix = os.path.splitext(name) 870dc076565f772bb1953209fb69ea150b494aaa40robbiew self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix) 880dc076565f772bb1953209fb69ea150b494aaa40robbiew else: 890dc076565f772bb1953209fb69ea150b494aaa40robbiew self.name = self.input_name 900dc076565f772bb1953209fb69ea150b494aaa40robbiew 910dc076565f772bb1953209fb69ea150b494aaa40robbiew def Write(self): 920dc076565f772bb1953209fb69ea150b494aaa40robbiew if self._output_zip: 930dc076565f772bb1953209fb69ea150b494aaa40robbiew common.ZipWrite(self._output_zip, self.name, self._zip_name) 940dc076565f772bb1953209fb69ea150b494aaa40robbiew 950dc076565f772bb1953209fb69ea150b494aaa40robbiew 960dc076565f772bb1953209fb69ea150b494aaa40robbiewdef GetCareMap(which, imgname): 970dc076565f772bb1953209fb69ea150b494aaa40robbiew """Generate care_map of system (or vendor) partition""" 980dc076565f772bb1953209fb69ea150b494aaa40robbiew 990dc076565f772bb1953209fb69ea150b494aaa40robbiew assert which in ("system", "vendor") 1000dc076565f772bb1953209fb69ea150b494aaa40robbiew 1010dc076565f772bb1953209fb69ea150b494aaa40robbiew simg = sparse_img.SparseImage(imgname) 1020dc076565f772bb1953209fb69ea150b494aaa40robbiew care_map_list = [] 1030dc076565f772bb1953209fb69ea150b494aaa40robbiew care_map_list.append(which) 1040dc076565f772bb1953209fb69ea150b494aaa40robbiew 1050dc076565f772bb1953209fb69ea150b494aaa40robbiew care_map_ranges = simg.care_map 1060dc076565f772bb1953209fb69ea150b494aaa40robbiew key = which + "_adjusted_partition_size" 1070dc076565f772bb1953209fb69ea150b494aaa40robbiew adjusted_blocks = OPTIONS.info_dict.get(key) 108 if adjusted_blocks: 109 assert adjusted_blocks > 0, "blocks should be positive for " + which 110 care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet( 111 "0-%d" % (adjusted_blocks,))) 112 113 care_map_list.append(care_map_ranges.to_string_raw()) 114 return care_map_list 115 116 117def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): 118 """Turn the contents of SYSTEM into a system image and store it in 119 output_zip. Returns the name of the system image file.""" 120 121 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img") 122 if os.path.exists(img.input_name): 123 print("system.img already exists in %s, no need to rebuild..." % (prefix,)) 124 return img.input_name 125 126 def output_sink(fn, data): 127 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") 128 ofile.write(data) 129 ofile.close() 130 131 arc_name = "SYSTEM/" + fn 132 if arc_name in output_zip.namelist(): 133 OPTIONS.replace_recovery_patch_files_list.append(arc_name) 134 else: 135 common.ZipWrite(output_zip, ofile.name, arc_name) 136 137 if OPTIONS.rebuild_recovery: 138 print("Building new recovery patch") 139 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, 140 boot_img, info_dict=OPTIONS.info_dict) 141 142 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map") 143 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img, 144 block_list=block_list) 145 146 return img.name 147 148 149def AddSystemOther(output_zip, prefix="IMAGES/"): 150 """Turn the contents of SYSTEM_OTHER into a system_other image 151 and store it in output_zip.""" 152 153 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img") 154 if os.path.exists(img.input_name): 155 print("system_other.img already exists in %s, no need to rebuild..." % ( 156 prefix,)) 157 return 158 159 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img) 160 161 162def AddVendor(output_zip, prefix="IMAGES/"): 163 """Turn the contents of VENDOR into a vendor image and store in it 164 output_zip.""" 165 166 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img") 167 if os.path.exists(img.input_name): 168 print("vendor.img already exists in %s, no need to rebuild..." % (prefix,)) 169 return img.input_name 170 171 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map") 172 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img, 173 block_list=block_list) 174 return img.name 175 176 177def AddDtbo(output_zip, prefix="IMAGES/"): 178 """Adds the DTBO image. 179 180 Uses the image under prefix if it already exists. Otherwise looks for the 181 image under PREBUILT_IMAGES/, signs it as needed, and returns the image name. 182 """ 183 184 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img") 185 if os.path.exists(img.input_name): 186 print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,)) 187 return img.input_name 188 189 dtbo_prebuilt_path = os.path.join( 190 OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img") 191 assert os.path.exists(dtbo_prebuilt_path) 192 shutil.copy(dtbo_prebuilt_path, img.name) 193 194 # AVB-sign the image as needed. 195 if OPTIONS.info_dict.get("board_avb_enable") == "true": 196 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] 197 part_size = OPTIONS.info_dict.get("dtbo_size") 198 # The AVB hash footer will be replaced if already present. 199 cmd = [avbtool, "add_hash_footer", "--image", img.name, 200 "--partition_size", str(part_size), "--partition_name", "dtbo"] 201 common.AppendAVBSigningArgs(cmd) 202 args = OPTIONS.info_dict.get("board_avb_dtbo_add_hash_footer_args") 203 if args and args.strip(): 204 cmd.extend(shlex.split(args)) 205 p = common.Run(cmd, stdout=subprocess.PIPE) 206 p.communicate() 207 assert p.returncode == 0, \ 208 "avbtool add_hash_footer of %s failed" % (img.name,) 209 210 img.Write() 211 return img.name 212 213 214def CreateImage(input_dir, info_dict, what, output_file, block_list=None): 215 print("creating " + what + ".img...") 216 217 # The name of the directory it is making an image out of matters to 218 # mkyaffs2image. It wants "system" but we have a directory named 219 # "SYSTEM", so create a symlink. 220 temp_dir = tempfile.mkdtemp() 221 OPTIONS.tempfiles.append(temp_dir) 222 try: 223 os.symlink(os.path.join(input_dir, what.upper()), 224 os.path.join(temp_dir, what)) 225 except OSError as e: 226 # bogus error on my mac version? 227 # File "./build/tools/releasetools/img_from_target_files" 228 # os.path.join(OPTIONS.input_tmp, "system")) 229 # OSError: [Errno 17] File exists 230 if e.errno == errno.EEXIST: 231 pass 232 233 image_props = build_image.ImagePropFromGlobalDict(info_dict, what) 234 fstab = info_dict["fstab"] 235 mount_point = "/" + what 236 if fstab and mount_point in fstab: 237 image_props["fs_type"] = fstab[mount_point].fs_type 238 239 # Use a fixed timestamp (01/01/2009) when packaging the image. 240 # Bug: 24377993 241 epoch = datetime.datetime.fromtimestamp(0) 242 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() 243 image_props["timestamp"] = int(timestamp) 244 245 if what == "system": 246 fs_config_prefix = "" 247 else: 248 fs_config_prefix = what + "_" 249 250 fs_config = os.path.join( 251 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") 252 if not os.path.exists(fs_config): 253 fs_config = None 254 255 # Override values loaded from info_dict. 256 if fs_config: 257 image_props["fs_config"] = fs_config 258 if block_list: 259 image_props["block_list"] = block_list.name 260 261 succ = build_image.BuildImage(os.path.join(temp_dir, what), 262 image_props, output_file.name) 263 assert succ, "build " + what + ".img image failed" 264 265 output_file.Write() 266 if block_list: 267 block_list.Write() 268 269 # Set the 'adjusted_partition_size' that excludes the verity blocks of the 270 # given image. When avb is enabled, this size is the max image size returned 271 # by the avb tool. 272 is_verity_partition = "verity_block_device" in image_props 273 verity_supported = (image_props.get("verity") == "true" or 274 image_props.get("board_avb_enable") == "true") 275 is_avb_enable = image_props.get("avb_hashtree_enable") == "true" 276 if verity_supported and (is_verity_partition or is_avb_enable): 277 adjusted_blocks_value = image_props.get("partition_size") 278 if adjusted_blocks_value: 279 adjusted_blocks_key = what + "_adjusted_partition_size" 280 info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1 281 282 283def AddUserdata(output_zip, prefix="IMAGES/"): 284 """Create a userdata image and store it in output_zip. 285 286 In most case we just create and store an empty userdata.img; 287 But the invoker can also request to create userdata.img with real 288 data from the target files, by setting "userdata_img_with_data=true" 289 in OPTIONS.info_dict. 290 """ 291 292 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img") 293 if os.path.exists(img.input_name): 294 print("userdata.img already exists in %s, no need to rebuild..." % ( 295 prefix,)) 296 return 297 298 # Skip userdata.img if no size. 299 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") 300 if not image_props.get("partition_size"): 301 return 302 303 print("creating userdata.img...") 304 305 # Use a fixed timestamp (01/01/2009) when packaging the image. 306 # Bug: 24377993 307 epoch = datetime.datetime.fromtimestamp(0) 308 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() 309 image_props["timestamp"] = int(timestamp) 310 311 # The name of the directory it is making an image out of matters to 312 # mkyaffs2image. So we create a temp dir, and within it we create an 313 # empty dir named "data", or a symlink to the DATA dir, 314 # and build the image from that. 315 temp_dir = tempfile.mkdtemp() 316 OPTIONS.tempfiles.append(temp_dir) 317 user_dir = os.path.join(temp_dir, "data") 318 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true") 319 if empty: 320 # Create an empty dir. 321 os.mkdir(user_dir) 322 else: 323 # Symlink to the DATA dir. 324 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"), 325 user_dir) 326 327 fstab = OPTIONS.info_dict["fstab"] 328 if fstab: 329 image_props["fs_type"] = fstab["/data"].fs_type 330 succ = build_image.BuildImage(user_dir, image_props, img.name) 331 assert succ, "build userdata.img image failed" 332 333 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) 334 img.Write() 335 336 337def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, 338 dtbo_img_path, prefix="IMAGES/"): 339 """Create a VBMeta image and store it in output_zip.""" 340 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") 341 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] 342 cmd = [avbtool, "make_vbmeta_image", 343 "--output", img.name, 344 "--include_descriptors_from_image", boot_img_path, 345 "--include_descriptors_from_image", system_img_path] 346 if vendor_img_path is not None: 347 cmd.extend(["--include_descriptors_from_image", vendor_img_path]) 348 if dtbo_img_path is not None: 349 cmd.extend(["--include_descriptors_from_image", dtbo_img_path]) 350 if OPTIONS.info_dict.get("system_root_image") == "true": 351 cmd.extend(["--setup_rootfs_from_kernel", system_img_path]) 352 common.AppendAVBSigningArgs(cmd) 353 args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args") 354 if args and args.strip(): 355 cmd.extend(shlex.split(args)) 356 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 357 p.communicate() 358 assert p.returncode == 0, "avbtool make_vbmeta_image failed" 359 img.Write() 360 361 362def AddPartitionTable(output_zip, prefix="IMAGES/"): 363 """Create a partition table image and store it in output_zip.""" 364 365 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img") 366 bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt") 367 368 # use BPTTOOL from environ, or "bpttool" if empty or not set. 369 bpttool = os.getenv("BPTTOOL") or "bpttool" 370 cmd = [bpttool, "make_table", "--output_json", bpt.name, 371 "--output_gpt", img.name] 372 input_files_str = OPTIONS.info_dict["board_bpt_input_files"] 373 input_files = input_files_str.split(" ") 374 for i in input_files: 375 cmd.extend(["--input", i]) 376 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") 377 if disk_size: 378 cmd.extend(["--disk_size", disk_size]) 379 args = OPTIONS.info_dict.get("board_bpt_make_table_args") 380 if args: 381 cmd.extend(shlex.split(args)) 382 383 p = common.Run(cmd, stdout=subprocess.PIPE) 384 p.communicate() 385 assert p.returncode == 0, "bpttool make_table failed" 386 387 img.Write() 388 bpt.Write() 389 390 391def AddCache(output_zip, prefix="IMAGES/"): 392 """Create an empty cache image and store it in output_zip.""" 393 394 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img") 395 if os.path.exists(img.input_name): 396 print("cache.img already exists in %s, no need to rebuild..." % (prefix,)) 397 return 398 399 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") 400 # The build system has to explicitly request for cache.img. 401 if "fs_type" not in image_props: 402 return 403 404 print("creating cache.img...") 405 406 # Use a fixed timestamp (01/01/2009) when packaging the image. 407 # Bug: 24377993 408 epoch = datetime.datetime.fromtimestamp(0) 409 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() 410 image_props["timestamp"] = int(timestamp) 411 412 # The name of the directory it is making an image out of matters to 413 # mkyaffs2image. So we create a temp dir, and within it we create an 414 # empty dir named "cache", and build the image from that. 415 temp_dir = tempfile.mkdtemp() 416 OPTIONS.tempfiles.append(temp_dir) 417 user_dir = os.path.join(temp_dir, "cache") 418 os.mkdir(user_dir) 419 420 fstab = OPTIONS.info_dict["fstab"] 421 if fstab: 422 image_props["fs_type"] = fstab["/cache"].fs_type 423 succ = build_image.BuildImage(user_dir, image_props, img.name) 424 assert succ, "build cache.img image failed" 425 426 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) 427 img.Write() 428 429 430def ReplaceRecoveryPatchFiles(zip_filename): 431 """Update the related files under SYSTEM/ after rebuilding recovery.""" 432 433 cmd = ["zip", "-d", zip_filename] + OPTIONS.replace_recovery_patch_files_list 434 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 435 p.communicate() 436 437 output_zip = zipfile.ZipFile(zip_filename, "a", 438 compression=zipfile.ZIP_DEFLATED, 439 allowZip64=True) 440 for item in OPTIONS.replace_recovery_patch_files_list: 441 file_path = os.path.join(OPTIONS.input_tmp, item) 442 assert os.path.exists(file_path) 443 common.ZipWrite(output_zip, file_path, arcname=item) 444 common.ZipClose(output_zip) 445 446 447def AddImagesToTargetFiles(filename): 448 if os.path.isdir(filename): 449 OPTIONS.input_tmp = os.path.abspath(filename) 450 input_zip = None 451 else: 452 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) 453 454 if not OPTIONS.add_missing: 455 if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")): 456 print("target_files appears to already contain images.") 457 sys.exit(1) 458 459 has_vendor = os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) 460 has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp, 461 "SYSTEM_OTHER")) 462 463 if input_zip: 464 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) 465 466 common.ZipClose(input_zip) 467 output_zip = zipfile.ZipFile(filename, "a", 468 compression=zipfile.ZIP_DEFLATED, 469 allowZip64=True) 470 else: 471 OPTIONS.info_dict = common.LoadInfoDict(filename, filename) 472 output_zip = None 473 images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES") 474 if not os.path.isdir(images_dir): 475 os.makedirs(images_dir) 476 images_dir = None 477 478 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") 479 480 def banner(s): 481 print("\n\n++++ " + s + " ++++\n\n") 482 483 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") 484 boot_image = None 485 if os.path.exists(prebuilt_path): 486 banner("boot") 487 print("boot.img already exists in IMAGES/, no need to rebuild...") 488 if OPTIONS.rebuild_recovery: 489 boot_image = common.GetBootableImage( 490 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 491 else: 492 banner("boot") 493 boot_image = common.GetBootableImage( 494 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 495 if boot_image: 496 if output_zip: 497 boot_image.AddToZip(output_zip) 498 else: 499 boot_image.WriteToDir(OPTIONS.input_tmp) 500 501 recovery_image = None 502 if has_recovery: 503 banner("recovery") 504 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") 505 if os.path.exists(prebuilt_path): 506 print("recovery.img already exists in IMAGES/, no need to rebuild...") 507 if OPTIONS.rebuild_recovery: 508 recovery_image = common.GetBootableImage( 509 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, 510 "RECOVERY") 511 else: 512 recovery_image = common.GetBootableImage( 513 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") 514 if recovery_image: 515 if output_zip: 516 recovery_image.AddToZip(output_zip) 517 else: 518 recovery_image.WriteToDir(OPTIONS.input_tmp) 519 520 banner("recovery (two-step image)") 521 # The special recovery.img for two-step package use. 522 recovery_two_step_image = common.GetBootableImage( 523 "IMAGES/recovery-two-step.img", "recovery-two-step.img", 524 OPTIONS.input_tmp, "RECOVERY", two_step_image=True) 525 if recovery_two_step_image: 526 if output_zip: 527 recovery_two_step_image.AddToZip(output_zip) 528 else: 529 recovery_two_step_image.WriteToDir(OPTIONS.input_tmp) 530 531 banner("system") 532 system_img_path = AddSystem( 533 output_zip, recovery_img=recovery_image, boot_img=boot_image) 534 vendor_img_path = None 535 if has_vendor: 536 banner("vendor") 537 vendor_img_path = AddVendor(output_zip) 538 if has_system_other: 539 banner("system_other") 540 AddSystemOther(output_zip) 541 if not OPTIONS.is_signing: 542 banner("userdata") 543 AddUserdata(output_zip) 544 banner("cache") 545 AddCache(output_zip) 546 547 if OPTIONS.info_dict.get("board_bpt_enable") == "true": 548 banner("partition-table") 549 AddPartitionTable(output_zip) 550 551 dtbo_img_path = None 552 if OPTIONS.info_dict.get("has_dtbo") == "true": 553 banner("dtbo") 554 dtbo_img_path = AddDtbo(output_zip) 555 556 if OPTIONS.info_dict.get("board_avb_enable") == "true": 557 banner("vbmeta") 558 boot_contents = boot_image.WriteToTemp() 559 AddVBMeta(output_zip, boot_contents.name, system_img_path, 560 vendor_img_path, dtbo_img_path) 561 562 # For devices using A/B update, copy over images from RADIO/ and/or 563 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed 564 # images ready under IMAGES/. All images should have '.img' as extension. 565 banner("radio") 566 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") 567 if os.path.exists(ab_partitions): 568 with open(ab_partitions, 'r') as f: 569 lines = f.readlines() 570 # For devices using A/B update, generate care_map for system and vendor 571 # partitions (if present), then write this file to target_files package. 572 care_map_list = [] 573 for line in lines: 574 if line.strip() == "system" and ( 575 "system_verity_block_device" in OPTIONS.info_dict or 576 OPTIONS.info_dict.get("system_avb_hashtree_enable") == "true"): 577 assert os.path.exists(system_img_path) 578 care_map_list += GetCareMap("system", system_img_path) 579 if line.strip() == "vendor" and ( 580 "vendor_verity_block_device" in OPTIONS.info_dict or 581 OPTIONS.info_dict.get("vendor_avb_hashtree_enable") == "true"): 582 assert os.path.exists(vendor_img_path) 583 care_map_list += GetCareMap("vendor", vendor_img_path) 584 585 img_name = line.strip() + ".img" 586 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) 587 if os.path.exists(prebuilt_path): 588 print("%s already exists, no need to overwrite..." % (img_name,)) 589 continue 590 591 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) 592 img_vendor_dir = os.path.join( 593 OPTIONS.input_tmp, "VENDOR_IMAGES") 594 if os.path.exists(img_radio_path): 595 if output_zip: 596 common.ZipWrite(output_zip, img_radio_path, 597 os.path.join("IMAGES", img_name)) 598 else: 599 shutil.copy(img_radio_path, prebuilt_path) 600 else: 601 for root, _, files in os.walk(img_vendor_dir): 602 if img_name in files: 603 if output_zip: 604 common.ZipWrite(output_zip, os.path.join(root, img_name), 605 os.path.join("IMAGES", img_name)) 606 else: 607 shutil.copy(os.path.join(root, img_name), prebuilt_path) 608 break 609 610 if output_zip: 611 # Zip spec says: All slashes MUST be forward slashes. 612 img_path = 'IMAGES/' + img_name 613 assert img_path in output_zip.namelist(), "cannot find " + img_name 614 else: 615 img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) 616 assert os.path.exists(img_path), "cannot find " + img_name 617 618 if care_map_list: 619 file_path = "META/care_map.txt" 620 if output_zip: 621 common.ZipWriteStr(output_zip, file_path, '\n'.join(care_map_list)) 622 else: 623 with open(os.path.join(OPTIONS.input_tmp, file_path), 'w') as fp: 624 fp.write('\n'.join(care_map_list)) 625 626 if output_zip: 627 common.ZipClose(output_zip) 628 if OPTIONS.replace_recovery_patch_files_list: 629 ReplaceRecoveryPatchFiles(output_zip.filename) 630 631 632def main(argv): 633 def option_handler(o, a): 634 if o in ("-a", "--add_missing"): 635 OPTIONS.add_missing = True 636 elif o in ("-r", "--rebuild_recovery",): 637 OPTIONS.rebuild_recovery = True 638 elif o == "--replace_verity_private_key": 639 OPTIONS.replace_verity_private_key = (True, a) 640 elif o == "--replace_verity_public_key": 641 OPTIONS.replace_verity_public_key = (True, a) 642 elif o == "--is_signing": 643 OPTIONS.is_signing = True 644 else: 645 return False 646 return True 647 648 args = common.ParseOptions( 649 argv, __doc__, extra_opts="ar", 650 extra_long_opts=["add_missing", "rebuild_recovery", 651 "replace_verity_public_key=", 652 "replace_verity_private_key=", 653 "is_signing"], 654 extra_option_handler=option_handler) 655 656 657 if len(args) != 1: 658 common.Usage(__doc__) 659 sys.exit(1) 660 661 AddImagesToTargetFiles(args[0]) 662 print("done.") 663 664if __name__ == '__main__': 665 try: 666 common.CloseInheritedPipes() 667 main(sys.argv[1:]) 668 except common.ExternalError as e: 669 print("\n ERROR: %s\n" % (e,)) 670 sys.exit(1) 671 finally: 672 common.Cleanup() 673