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 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 30 | --additional <filespec> |
| 31 | Include an additional entry into the generated zip file. The filespec is |
| 32 | in a format that's accepted by zip2zip (e.g. |
| 33 | 'OTA/android-info.txt:android-info.txt', to copy `OTA/android-info.txt` |
| 34 | from input_file into output_file as `android-info.txt`. Refer to the |
| 35 | `filespec` arg in zip2zip's help message). The option can be repeated to |
| 36 | include multiple entries. |
| 37 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 38 | """ |
| 39 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 40 | from __future__ import print_function |
| 41 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 42 | import logging |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 43 | import os |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 44 | import sys |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 45 | import zipfile |
| 46 | |
| 47 | import common |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 48 | from build_super_image import BuildSuperImage |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 49 | |
Doug Zongker | cf6d5a9 | 2014-02-18 10:57:07 -0800 | [diff] [blame] | 50 | if sys.hexversion < 0x02070000: |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 51 | print('Python 2.7 or newer is required.', file=sys.stderr) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 52 | sys.exit(1) |
| 53 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 54 | logger = logging.getLogger(__name__) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 55 | |
| 56 | OPTIONS = common.OPTIONS |
| 57 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 58 | OPTIONS.additional_entries = [] |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 59 | OPTIONS.bootable_only = False |
| 60 | OPTIONS.put_super = None |
| 61 | OPTIONS.dynamic_partition_list = None |
| 62 | OPTIONS.super_device_list = None |
| 63 | OPTIONS.retrofit_dap = None |
| 64 | OPTIONS.build_super = None |
| 65 | OPTIONS.sparse_userimages = None |
| 66 | |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 67 | |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 68 | def LoadOptions(input_file): |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 69 | """Loads information from input_file to OPTIONS. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 70 | |
| 71 | Args: |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 72 | input_file: Path to the input target_files zip file. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 73 | """ |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 74 | with zipfile.ZipFile(input_file) as input_zip: |
| 75 | info = OPTIONS.info_dict = common.LoadInfoDict(input_zip) |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 76 | |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 77 | OPTIONS.put_super = info.get('super_image_in_update_package') == 'true' |
| 78 | OPTIONS.dynamic_partition_list = info.get('dynamic_partition_list', |
| 79 | '').strip().split() |
| 80 | OPTIONS.super_device_list = info.get('super_block_devices', |
| 81 | '').strip().split() |
| 82 | OPTIONS.retrofit_dap = info.get('dynamic_partition_retrofit') == 'true' |
| 83 | OPTIONS.build_super = info.get('build_super_partition') == 'true' |
| 84 | OPTIONS.sparse_userimages = bool(info.get('extfs_sparse_flag')) |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 85 | |
| 86 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 87 | def CopyZipEntries(input_file, output_file, entries): |
| 88 | """Copies ZIP entries between input and output files. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 89 | |
| 90 | Args: |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 91 | input_file: Path to the input target_files zip. |
| 92 | output_file: Output filename. |
| 93 | entries: A list of entries to copy, in a format that's accepted by zip2zip |
| 94 | (e.g. 'OTA/android-info.txt:android-info.txt', which copies |
| 95 | `OTA/android-info.txt` from input_file into output_file as |
| 96 | `android-info.txt`. Refer to the `filespec` arg in zip2zip's help |
| 97 | message). |
| 98 | """ |
| 99 | logger.info('Writing %d entries to archive...', len(entries)) |
| 100 | cmd = ['zip2zip', '-i', input_file, '-o', output_file] |
| 101 | cmd.extend(entries) |
| 102 | common.RunAndCheckOutput(cmd) |
| 103 | |
| 104 | |
| 105 | def EntriesForUserImages(input_file): |
| 106 | """Returns the user images entries to be copied. |
| 107 | |
| 108 | Args: |
| 109 | input_file: Path to the input target_files zip file. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 110 | """ |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 111 | dynamic_images = [p + '.img' for p in OPTIONS.dynamic_partition_list] |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 112 | |
| 113 | # 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] | 114 | if not OPTIONS.retrofit_dap and 'system' in OPTIONS.dynamic_partition_list: |
| 115 | dynamic_images.append('system_other.img') |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 116 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 117 | entries = [ |
| 118 | 'OTA/android-info.txt:android-info.txt', |
| 119 | ] |
| 120 | with zipfile.ZipFile(input_file) as input_zip: |
| 121 | namelist = input_zip.namelist() |
| 122 | |
| 123 | for image_path in [name for name in namelist if name.startswith('IMAGES/')]: |
| 124 | image = os.path.basename(image_path) |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 125 | if OPTIONS.bootable_only and image not in ('boot.img', 'recovery.img'): |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 126 | continue |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 127 | if not image.endswith('.img'): |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 128 | continue |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 129 | # Filter out super_empty and the images that are already in super partition. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 130 | if OPTIONS.put_super: |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 131 | if image == 'super_empty.img': |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 132 | continue |
| 133 | if image in dynamic_images: |
| 134 | continue |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 135 | entries.append('{}:{}'.format(image_path, image)) |
| 136 | return entries |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 137 | |
| 138 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 139 | def EntriesForSplitSuperImages(input_file): |
| 140 | """Returns the entries for split super images. |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 141 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 142 | This is only done for retrofit dynamic partition devices. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 143 | |
| 144 | Args: |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 145 | input_file: Path to the input target_files zip file. |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 146 | """ |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 147 | with zipfile.ZipFile(input_file) as input_zip: |
| 148 | namelist = input_zip.namelist() |
| 149 | entries = [] |
| 150 | for device in OPTIONS.super_device_list: |
| 151 | image = 'OTA/super_{}.img'.format(device) |
| 152 | assert image in namelist, 'Failed to find {}'.format(image) |
| 153 | entries.append('{}:{}'.format(image, os.path.basename(image))) |
| 154 | return entries |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 155 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 156 | |
| 157 | def RebuildAndWriteSuperImages(input_file, output_file): |
| 158 | """Builds and writes super images to the output file.""" |
| 159 | logger.info('Building super image...') |
| 160 | |
| 161 | # We need files under IMAGES/, OTA/, META/ for img_from_target_files.py. |
| 162 | # However, common.LoadInfoDict() may read additional files under BOOT/, |
| 163 | # RECOVERY/ and ROOT/. So unzip everything from the target_files.zip. |
| 164 | input_tmp = common.UnzipTemp(input_file) |
| 165 | |
| 166 | super_file = common.MakeTempFile('super_', '.img') |
| 167 | BuildSuperImage(input_tmp, super_file) |
| 168 | |
| 169 | logger.info('Writing super.img to archive...') |
| 170 | with zipfile.ZipFile( |
| 171 | output_file, 'a', compression=zipfile.ZIP_DEFLATED, |
| 172 | allowZip64=not OPTIONS.sparse_userimages) as output_zip: |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 173 | common.ZipWrite(output_zip, super_file, 'super.img') |
Yifan Hong | 0e97dbb | 2019-04-17 14:28:52 -0700 | [diff] [blame] | 174 | |
| 175 | |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 176 | def ImgFromTargetFiles(input_file, output_file): |
| 177 | """Creates an image archive from the input target_files zip. |
| 178 | |
| 179 | Args: |
| 180 | input_file: Path to the input target_files zip. |
| 181 | output_file: Output filename. |
| 182 | |
| 183 | Raises: |
| 184 | ValueError: On invalid input. |
| 185 | """ |
| 186 | if not zipfile.is_zipfile(input_file): |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 187 | raise ValueError('%s is not a valid zipfile' % input_file) |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 188 | |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 189 | logger.info('Building image zip from target files zip.') |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 190 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 191 | LoadOptions(input_file) |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 192 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 193 | # Entries to be copied into the output file. |
| 194 | entries = EntriesForUserImages(input_file) |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 195 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 196 | # Only for devices that retrofit dynamic partitions there're split super |
| 197 | # images available in the target_files.zip. |
| 198 | rebuild_super = False |
| 199 | if OPTIONS.build_super and OPTIONS.put_super: |
| 200 | if OPTIONS.retrofit_dap: |
| 201 | entries += EntriesForSplitSuperImages(input_file) |
| 202 | else: |
| 203 | rebuild_super = True |
| 204 | |
| 205 | # Any additional entries provided by caller. |
| 206 | entries += OPTIONS.additional_entries |
| 207 | |
| 208 | CopyZipEntries(input_file, output_file, entries) |
| 209 | |
| 210 | if rebuild_super: |
| 211 | RebuildAndWriteSuperImages(input_file, output_file) |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 212 | |
| 213 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 214 | def main(argv): |
| 215 | |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 216 | def option_handler(o, a): |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 217 | if o in ('-z', '--bootable_zip'): |
| 218 | OPTIONS.bootable_only = True |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 219 | elif o == '--additional': |
| 220 | OPTIONS.additional_entries.append(a) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 221 | else: |
| 222 | return False |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 223 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 224 | |
| 225 | args = common.ParseOptions(argv, __doc__, |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 226 | extra_opts='z', |
Tao Bao | 57f8ed6 | 2019-08-14 23:08:18 -0700 | [diff] [blame] | 227 | extra_long_opts=[ |
| 228 | 'additional=', |
| 229 | 'bootable_zip', |
| 230 | ], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 231 | extra_option_handler=option_handler) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 232 | if len(args) != 2: |
| 233 | common.Usage(__doc__) |
| 234 | sys.exit(1) |
| 235 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 236 | common.InitLogging() |
| 237 | |
Tao Bao | 2aac9c9 | 2019-08-02 15:50:32 -0700 | [diff] [blame] | 238 | ImgFromTargetFiles(args[0], args[1]) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 239 | |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 240 | logger.info('done.') |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 241 | |
| 242 | |
| 243 | if __name__ == '__main__': |
| 244 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 245 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 246 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 247 | except common.ExternalError as e: |
Tao Bao | ac63a9d | 2019-08-26 20:33:11 -0700 | [diff] [blame] | 248 | logger.exception('\n ERROR:\n') |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 249 | sys.exit(1) |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 250 | finally: |
| 251 | common.Cleanup() |