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