Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 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 | """ |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 18 | Given target-files, produces an image zipfile suitable for use |
| 19 | with 'fastboot update'. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 20 | |
| 21 | Usage: img_from_target_files [flags] input_target_files output_image_zip |
| 22 | |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 23 | input_target_files: one of the following: |
| 24 | - directory containing extracted target files. It will load info from |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 25 | OTA/android-info.txt, META/misc_info.txt and build the image zipfile using |
| 26 | images from IMAGES/. |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 27 | - target files package. Same as above, but extracts the archive before |
| 28 | building the image zipfile. |
| 29 | |
| 30 | Flags: |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 31 | -z (--bootable_zip) |
| 32 | Include only the bootable images (eg 'boot' and 'recovery') in |
| 33 | the output. |
| 34 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 35 | """ |
| 36 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 37 | from __future__ import print_function |
| 38 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 39 | import logging |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 40 | import os |
| 41 | import shutil |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 42 | import sys |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 43 | import zipfile |
| 44 | |
| 45 | import common |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 46 | from build_super_image import BuildSuperImage |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 47 | |
Doug Zongker | cf6d5a9 | 2014-02-18 10:57:07 -0800 | [diff] [blame] | 48 | if sys.hexversion < 0x02070000: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 49 | print("Python 2.7 or newer is required.", file=sys.stderr) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 50 | sys.exit(1) |
| 51 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 52 | logger = logging.getLogger(__name__) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 53 | |
| 54 | OPTIONS = common.OPTIONS |
| 55 | |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 56 | |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 57 | def LoadOptions(input_file): |
| 58 | """ |
| 59 | Load information from input_file to OPTIONS. |
| 60 | |
| 61 | Args: |
| 62 | input_file: A Zipfile instance of input zip file, or path to the directory |
| 63 | of extracted zip. |
| 64 | """ |
| 65 | info = OPTIONS.info_dict = common.LoadInfoDict(input_file) |
| 66 | |
| 67 | OPTIONS.put_super = info.get("super_image_in_update_package") == "true" |
| 68 | OPTIONS.dynamic_partition_list = info.get("dynamic_partition_list", |
| 69 | "").strip().split() |
| 70 | OPTIONS.super_device_list = info.get("super_block_devices", |
| 71 | "").strip().split() |
| 72 | OPTIONS.retrofit_dap = info.get("dynamic_partition_retrofit") == "true" |
| 73 | OPTIONS.build_super = info.get("build_super_partition") == "true" |
| 74 | OPTIONS.sparse_userimages = bool(info.get("extfs_sparse_flag")) |
| 75 | |
| 76 | |
| 77 | def CopyInfo(input_tmp, output_zip): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 78 | """Copy the android-info.txt file from the input to the output.""" |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 79 | common.ZipWrite( |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 80 | output_zip, os.path.join(input_tmp, "OTA", "android-info.txt"), |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 81 | "android-info.txt") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 82 | |
| 83 | |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 84 | def CopyUserImages(input_tmp, output_zip): |
| 85 | """ |
| 86 | Copy user images from the unzipped input and write to output_zip. |
| 87 | |
| 88 | Args: |
| 89 | input_tmp: path to the unzipped input. |
| 90 | output_zip: a ZipFile instance to write images to. |
| 91 | """ |
| 92 | dynamic_images = [p + ".img" for p in OPTIONS.dynamic_partition_list] |
| 93 | |
| 94 | # Filter out system_other for launch DAP devices because it is in super image. |
| 95 | if not OPTIONS.retrofit_dap and "system" in OPTIONS.dynamic_partition_list: |
| 96 | dynamic_images.append("system_other.img") |
| 97 | |
| 98 | images_path = os.path.join(input_tmp, "IMAGES") |
| 99 | # A target-files zip must contain the images since Lollipop. |
| 100 | assert os.path.exists(images_path) |
| 101 | for image in sorted(os.listdir(images_path)): |
| 102 | if OPTIONS.bootable_only and image not in ("boot.img", "recovery.img"): |
| 103 | continue |
| 104 | if not image.endswith(".img"): |
| 105 | continue |
| 106 | if image == "recovery-two-step.img": |
| 107 | continue |
| 108 | if OPTIONS.put_super: |
| 109 | if image == "super_empty.img": |
| 110 | continue |
| 111 | if image in dynamic_images: |
| 112 | continue |
| 113 | logger.info("writing %s to archive...", os.path.join("IMAGES", image)) |
| 114 | common.ZipWrite(output_zip, os.path.join(images_path, image), image) |
| 115 | |
| 116 | |
| 117 | def WriteSuperImages(input_tmp, output_zip): |
| 118 | """ |
| 119 | Write super images from the unzipped input and write to output_zip. This is |
| 120 | only done if super_image_in_update_package is set to "true". |
| 121 | |
| 122 | - For retrofit dynamic partition devices, copy split super images from target |
| 123 | files package. |
| 124 | - For devices launched with dynamic partitions, build super image from target |
| 125 | files package. |
| 126 | |
| 127 | Args: |
| 128 | input_tmp: path to the unzipped input. |
| 129 | output_zip: a ZipFile instance to write images to. |
| 130 | """ |
| 131 | if not OPTIONS.build_super or not OPTIONS.put_super: |
| 132 | return |
| 133 | |
| 134 | if OPTIONS.retrofit_dap: |
| 135 | # retrofit devices already have split super images under OTA/ |
| 136 | images_path = os.path.join(input_tmp, "OTA") |
| 137 | for device in OPTIONS.super_device_list: |
| 138 | image = "super_%s.img" % device |
| 139 | image_path = os.path.join(images_path, image) |
| 140 | assert os.path.exists(image_path) |
| 141 | logger.info("writing %s to archive...", os.path.join("OTA", image)) |
| 142 | common.ZipWrite(output_zip, image_path, image) |
| 143 | else: |
| 144 | # super image for non-retrofit devices aren't in target files package, |
| 145 | # so build it. |
| 146 | super_file = common.MakeTempFile("super_", ".img") |
| 147 | logger.info("building super image %s...", super_file) |
| 148 | BuildSuperImage(input_tmp, super_file) |
| 149 | logger.info("writing super.img to archive...") |
| 150 | common.ZipWrite(output_zip, super_file, "super.img") |
| 151 | |
| 152 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 153 | def main(argv): |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 154 | # This allows modifying the value from inner function. |
| 155 | bootable_only_array = [False] |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 156 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 157 | def option_handler(o, _): |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 158 | if o in ("-z", "--bootable_zip"): |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 159 | bootable_only_array[0] = True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 160 | else: |
| 161 | return False |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 162 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 163 | |
| 164 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 165 | extra_opts="z", |
| 166 | extra_long_opts=["bootable_zip"], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 167 | extra_option_handler=option_handler) |
| 168 | |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 169 | OPTIONS.bootable_only = bootable_only_array[0] |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 170 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 171 | if len(args) != 2: |
| 172 | common.Usage(__doc__) |
| 173 | sys.exit(1) |
| 174 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 175 | common.InitLogging() |
| 176 | |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 177 | target_files = args[0] |
| 178 | if os.path.isdir(target_files): |
| 179 | logger.info("Building image zip from extracted target files.") |
| 180 | OPTIONS.input_tmp = target_files |
| 181 | elif zipfile.is_zipfile(target_files): |
| 182 | logger.info("Building image zip from target files zip.") |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 183 | OPTIONS.input_tmp = common.UnzipTemp(target_files, |
| 184 | ["IMAGES/*", "OTA/*", "META/*"]) |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 185 | else: |
| 186 | raise ValueError("%s is not a valid path." % target_files) |
| 187 | |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 188 | LoadOptions(OPTIONS.input_tmp) |
| 189 | output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED, |
| 190 | allowZip64=not OPTIONS.sparse_userimages) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 191 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 192 | try: |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame^] | 193 | CopyInfo(OPTIONS.input_tmp, output_zip) |
| 194 | CopyUserImages(OPTIONS.input_tmp, output_zip) |
| 195 | WriteSuperImages(OPTIONS.input_tmp, output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 196 | finally: |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 197 | logger.info("cleaning up...") |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 198 | common.ZipClose(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 199 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 200 | logger.info("done.") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 201 | |
| 202 | |
| 203 | if __name__ == '__main__': |
| 204 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 205 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 206 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 207 | except common.ExternalError as e: |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 208 | logger.exception("\n ERROR:\n") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 209 | sys.exit(1) |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 210 | finally: |
| 211 | common.Cleanup() |