1 #!/usr/bin/env python 2 # 3 # Copyright (C) 2008 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, produces an image zipfile suitable for 19 use with 'fastboot update'. 20 21 Usage: img_from_target_files [flags] input_target_files output_image_zip 22 23 -z (--bootable_zip) 24 Include only the bootable images (eg 'boot' and 'recovery') in 25 the output. 26 27 """ 28 29 from __future__ import print_function 30 31 import sys 32 33 if sys.hexversion < 0x02070000: 34 print("Python 2.7 or newer is required.", file=sys.stderr) 35 sys.exit(1) 36 37 import os 38 import shutil 39 import zipfile 40 41 import common 42 43 OPTIONS = common.OPTIONS 44 45 46 def CopyInfo(output_zip): 47 """Copy the android-info.txt file from the input to the output.""" 48 common.ZipWrite( 49 output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), 50 "android-info.txt") 51 52 53 def main(argv): 54 bootable_only = [False] 55 56 def option_handler(o, _): 57 if o in ("-z", "--bootable_zip"): 58 bootable_only[0] = True 59 else: 60 return False 61 return True 62 63 args = common.ParseOptions(argv, __doc__, 64 extra_opts="z", 65 extra_long_opts=["bootable_zip"], 66 extra_option_handler=option_handler) 67 68 bootable_only = bootable_only[0] 69 70 if len(args) != 2: 71 common.Usage(__doc__) 72 sys.exit(1) 73 74 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) 75 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) 76 CopyInfo(output_zip) 77 78 try: 79 done = False 80 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES") 81 if os.path.exists(images_path): 82 # If this is a new target-files, it already contains the images, 83 # and all we have to do is copy them to the output zip. 84 images = os.listdir(images_path) 85 if images: 86 for image in images: 87 if bootable_only and image not in ("boot.img", "recovery.img"): 88 continue 89 if not image.endswith(".img"): 90 continue 91 if image == "recovery-two-step.img": 92 continue 93 common.ZipWrite( 94 output_zip, os.path.join(images_path, image), image) 95 done = True 96 97 if not done: 98 # We have an old target-files that doesn't already contain the 99 # images, so build them. 100 import add_img_to_target_files 101 102 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) 103 104 boot_image = common.GetBootableImage( 105 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 106 if boot_image: 107 boot_image.AddToZip(output_zip) 108 109 if OPTIONS.info_dict.get("no_recovery") != "true": 110 recovery_image = common.GetBootableImage( 111 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") 112 if recovery_image: 113 recovery_image.AddToZip(output_zip) 114 115 def banner(s): 116 print("\n\n++++ " + s + " ++++\n\n") 117 118 if not bootable_only: 119 banner("AddSystem") 120 add_img_to_target_files.AddSystem(output_zip, prefix="") 121 try: 122 input_zip.getinfo("VENDOR/") 123 banner("AddVendor") 124 add_img_to_target_files.AddVendor(output_zip, prefix="") 125 except KeyError: 126 pass # no vendor partition for this device 127 banner("AddUserdata") 128 add_img_to_target_files.AddUserdata(output_zip, prefix="") 129 banner("AddCache") 130 add_img_to_target_files.AddCache(output_zip, prefix="") 131 132 finally: 133 print("cleaning up...") 134 common.ZipClose(output_zip) 135 shutil.rmtree(OPTIONS.input_tmp) 136 137 print("done.") 138 139 140 if __name__ == '__main__': 141 try: 142 common.CloseInheritedPipes() 143 main(sys.argv[1:]) 144 except common.ExternalError as e: 145 print("\n ERROR: %s\n" % (e,)) 146 sys.exit(1) 147