blob: 5412b2a488e9cbc8925721774b49d8d8713d44fc [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001#!/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 Bao2aac9c92019-08-02 15:50:32 -070018Given an input target-files, produces an image zipfile suitable for use with
19'fastboot update'.
Doug Zongkereef39442009-04-02 12:14:19 -070020
21Usage: img_from_target_files [flags] input_target_files output_image_zip
22
Tao Bao2aac9c92019-08-02 15:50:32 -070023input_target_files: Path to the input target_files zip.
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070024
25Flags:
Doug Zongker55d93282011-01-25 17:03:34 -080026 -z (--bootable_zip)
27 Include only the bootable images (eg 'boot' and 'recovery') in
28 the output.
29
Tao Bao57f8ed62019-08-14 23:08:18 -070030 --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 Zongkereef39442009-04-02 12:14:19 -070038"""
39
Tao Bao89fbb0f2017-01-10 10:47:58 -080040from __future__ import print_function
41
Tao Bao32fcdab2018-10-12 10:30:39 -070042import logging
Tao Bao76def242017-11-21 09:25:31 -080043import os
Doug Zongkereef39442009-04-02 12:14:19 -070044import sys
Tao Bao76def242017-11-21 09:25:31 -080045import zipfile
46
47import common
Yifan Hong0e97dbb2019-04-17 14:28:52 -070048from build_super_image import BuildSuperImage
Doug Zongkereef39442009-04-02 12:14:19 -070049
Doug Zongkercf6d5a92014-02-18 10:57:07 -080050if sys.hexversion < 0x02070000:
Tao Baoac63a9d2019-08-26 20:33:11 -070051 print('Python 2.7 or newer is required.', file=sys.stderr)
Doug Zongkereef39442009-04-02 12:14:19 -070052 sys.exit(1)
53
Tao Bao32fcdab2018-10-12 10:30:39 -070054logger = logging.getLogger(__name__)
Doug Zongkereef39442009-04-02 12:14:19 -070055
56OPTIONS = common.OPTIONS
57
Tao Bao57f8ed62019-08-14 23:08:18 -070058OPTIONS.additional_entries = []
Tao Baoac63a9d2019-08-26 20:33:11 -070059OPTIONS.bootable_only = False
60OPTIONS.put_super = None
Ram Muthiah0c4a3522020-08-27 00:54:13 +000061OPTIONS.put_bootloader = None
Tao Baoac63a9d2019-08-26 20:33:11 -070062OPTIONS.dynamic_partition_list = None
63OPTIONS.super_device_list = None
64OPTIONS.retrofit_dap = None
65OPTIONS.build_super = None
66OPTIONS.sparse_userimages = None
Daniel Zhengee10d072023-06-01 14:18:18 -070067OPTIONS.use_fastboot_info = False
David Iserovichbabdafe2023-08-10 17:58:31 +000068OPTIONS.build_super_image = None
Ying Wanga0febe52013-03-20 11:02:05 -070069
Yifan Hong0e97dbb2019-04-17 14:28:52 -070070def LoadOptions(input_file):
Tao Bao2aac9c92019-08-02 15:50:32 -070071 """Loads information from input_file to OPTIONS.
Yifan Hong0e97dbb2019-04-17 14:28:52 -070072
73 Args:
Tao Bao57f8ed62019-08-14 23:08:18 -070074 input_file: Path to the input target_files zip file.
Yifan Hong0e97dbb2019-04-17 14:28:52 -070075 """
Tao Bao57f8ed62019-08-14 23:08:18 -070076 with zipfile.ZipFile(input_file) as input_zip:
77 info = OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Yifan Hong0e97dbb2019-04-17 14:28:52 -070078
Tao Baoac63a9d2019-08-26 20:33:11 -070079 OPTIONS.put_super = info.get('super_image_in_update_package') == 'true'
Ram Muthiah0c4a3522020-08-27 00:54:13 +000080 OPTIONS.put_bootloader = info.get('bootloader_in_update_package') == 'true'
Tao Baoac63a9d2019-08-26 20:33:11 -070081 OPTIONS.dynamic_partition_list = info.get('dynamic_partition_list',
82 '').strip().split()
83 OPTIONS.super_device_list = info.get('super_block_devices',
84 '').strip().split()
85 OPTIONS.retrofit_dap = info.get('dynamic_partition_retrofit') == 'true'
86 OPTIONS.build_super = info.get('build_super_partition') == 'true'
87 OPTIONS.sparse_userimages = bool(info.get('extfs_sparse_flag'))
Yifan Hong0e97dbb2019-04-17 14:28:52 -070088
89
Tao Bao57f8ed62019-08-14 23:08:18 -070090def CopyZipEntries(input_file, output_file, entries):
91 """Copies ZIP entries between input and output files.
Yifan Hong0e97dbb2019-04-17 14:28:52 -070092
93 Args:
Tao Bao57f8ed62019-08-14 23:08:18 -070094 input_file: Path to the input target_files zip.
95 output_file: Output filename.
96 entries: A list of entries to copy, in a format that's accepted by zip2zip
97 (e.g. 'OTA/android-info.txt:android-info.txt', which copies
98 `OTA/android-info.txt` from input_file into output_file as
99 `android-info.txt`. Refer to the `filespec` arg in zip2zip's help
100 message).
101 """
102 logger.info('Writing %d entries to archive...', len(entries))
103 cmd = ['zip2zip', '-i', input_file, '-o', output_file]
104 cmd.extend(entries)
105 common.RunAndCheckOutput(cmd)
106
107
108def EntriesForUserImages(input_file):
109 """Returns the user images entries to be copied.
110
111 Args:
112 input_file: Path to the input target_files zip file.
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700113 """
Tao Baoac63a9d2019-08-26 20:33:11 -0700114 dynamic_images = [p + '.img' for p in OPTIONS.dynamic_partition_list]
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700115
116 # Filter out system_other for launch DAP devices because it is in super image.
Tao Baoac63a9d2019-08-26 20:33:11 -0700117 if not OPTIONS.retrofit_dap and 'system' in OPTIONS.dynamic_partition_list:
118 dynamic_images.append('system_other.img')
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700119
Tao Bao57f8ed62019-08-14 23:08:18 -0700120 entries = [
121 'OTA/android-info.txt:android-info.txt',
122 ]
Daniel Zhengee10d072023-06-01 14:18:18 -0700123 if OPTIONS.use_fastboot_info:
124 entries.append('META/fastboot-info.txt:fastboot-info.txt')
Tao Bao57f8ed62019-08-14 23:08:18 -0700125 with zipfile.ZipFile(input_file) as input_zip:
126 namelist = input_zip.namelist()
Kelvin Zhangdecee4a2023-05-19 12:33:06 -0700127 if 'PREBUILT_IMAGES/kernel_16k' in namelist:
128 entries.append('PREBUILT_IMAGES/kernel_16k:kernel_16k')
129 if 'PREBUILT_IMAGES/ramdisk_16k.img' in namelist:
130 entries.append('PREBUILT_IMAGES/ramdisk_16k.img:ramdisk_16k.img')
Tao Bao57f8ed62019-08-14 23:08:18 -0700131
132 for image_path in [name for name in namelist if name.startswith('IMAGES/')]:
133 image = os.path.basename(image_path)
Kelvin Zhangdecee4a2023-05-19 12:33:06 -0700134 if OPTIONS.bootable_only and image not in ('boot.img', 'recovery.img', 'bootloader', 'init_boot.img'):
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700135 continue
Ram Muthiah0c4a3522020-08-27 00:54:13 +0000136 if not image.endswith('.img') and image != 'bootloader':
137 continue
138 if image == 'bootloader' and not OPTIONS.put_bootloader:
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700139 continue
Tao Bao57f8ed62019-08-14 23:08:18 -0700140 # Filter out super_empty and the images that are already in super partition.
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700141 if OPTIONS.put_super:
Tao Baoac63a9d2019-08-26 20:33:11 -0700142 if image == 'super_empty.img':
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700143 continue
144 if image in dynamic_images:
145 continue
Tao Bao57f8ed62019-08-14 23:08:18 -0700146 entries.append('{}:{}'.format(image_path, image))
147 return entries
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700148
149
Tao Bao57f8ed62019-08-14 23:08:18 -0700150def EntriesForSplitSuperImages(input_file):
151 """Returns the entries for split super images.
Tao Bao2aac9c92019-08-02 15:50:32 -0700152
Tao Bao57f8ed62019-08-14 23:08:18 -0700153 This is only done for retrofit dynamic partition devices.
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700154
155 Args:
Tao Bao57f8ed62019-08-14 23:08:18 -0700156 input_file: Path to the input target_files zip file.
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700157 """
Tao Bao57f8ed62019-08-14 23:08:18 -0700158 with zipfile.ZipFile(input_file) as input_zip:
159 namelist = input_zip.namelist()
160 entries = []
161 for device in OPTIONS.super_device_list:
162 image = 'OTA/super_{}.img'.format(device)
163 assert image in namelist, 'Failed to find {}'.format(image)
164 entries.append('{}:{}'.format(image, os.path.basename(image)))
165 return entries
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700166
Tao Bao57f8ed62019-08-14 23:08:18 -0700167
168def RebuildAndWriteSuperImages(input_file, output_file):
169 """Builds and writes super images to the output file."""
170 logger.info('Building super image...')
171
172 # We need files under IMAGES/, OTA/, META/ for img_from_target_files.py.
173 # However, common.LoadInfoDict() may read additional files under BOOT/,
174 # RECOVERY/ and ROOT/. So unzip everything from the target_files.zip.
175 input_tmp = common.UnzipTemp(input_file)
176
177 super_file = common.MakeTempFile('super_', '.img')
David Iserovichbabdafe2023-08-10 17:58:31 +0000178
179 # Allow overriding the BUILD_SUPER_IMAGE binary
180 if OPTIONS.build_super_image:
181 command = [OPTIONS.build_super_image, input_tmp, super_file]
182 common.RunAndCheckOutput(command)
183 else:
184 BuildSuperImage(input_tmp, super_file)
Tao Bao57f8ed62019-08-14 23:08:18 -0700185
186 logger.info('Writing super.img to archive...')
187 with zipfile.ZipFile(
Kelvin Zhangdecee4a2023-05-19 12:33:06 -0700188 output_file, 'a', compression=zipfile.ZIP_DEFLATED,
189 allowZip64=True) as output_zip:
Tao Baoac63a9d2019-08-26 20:33:11 -0700190 common.ZipWrite(output_zip, super_file, 'super.img')
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700191
192
Tao Bao2aac9c92019-08-02 15:50:32 -0700193def ImgFromTargetFiles(input_file, output_file):
194 """Creates an image archive from the input target_files zip.
195
196 Args:
197 input_file: Path to the input target_files zip.
198 output_file: Output filename.
199
200 Raises:
201 ValueError: On invalid input.
202 """
jiajia tang92be6ee2021-04-21 15:49:51 +0800203 if not os.path.exists(input_file):
204 raise ValueError('%s is not exist' % input_file)
205
Tao Bao2aac9c92019-08-02 15:50:32 -0700206 if not zipfile.is_zipfile(input_file):
Tao Baoac63a9d2019-08-26 20:33:11 -0700207 raise ValueError('%s is not a valid zipfile' % input_file)
Tao Bao2aac9c92019-08-02 15:50:32 -0700208
Tao Baoac63a9d2019-08-26 20:33:11 -0700209 logger.info('Building image zip from target files zip.')
Tao Bao2aac9c92019-08-02 15:50:32 -0700210
Tao Bao57f8ed62019-08-14 23:08:18 -0700211 LoadOptions(input_file)
Tao Bao2aac9c92019-08-02 15:50:32 -0700212
Tao Bao57f8ed62019-08-14 23:08:18 -0700213 # Entries to be copied into the output file.
214 entries = EntriesForUserImages(input_file)
Tao Bao2aac9c92019-08-02 15:50:32 -0700215
Tao Bao57f8ed62019-08-14 23:08:18 -0700216 # Only for devices that retrofit dynamic partitions there're split super
217 # images available in the target_files.zip.
218 rebuild_super = False
219 if OPTIONS.build_super and OPTIONS.put_super:
220 if OPTIONS.retrofit_dap:
221 entries += EntriesForSplitSuperImages(input_file)
222 else:
223 rebuild_super = True
224
225 # Any additional entries provided by caller.
226 entries += OPTIONS.additional_entries
227
228 CopyZipEntries(input_file, output_file, entries)
229
230 if rebuild_super:
231 RebuildAndWriteSuperImages(input_file, output_file)
Tao Bao2aac9c92019-08-02 15:50:32 -0700232
233
Doug Zongkereef39442009-04-02 12:14:19 -0700234def main(argv):
235
Tao Bao57f8ed62019-08-14 23:08:18 -0700236 def option_handler(o, a):
Tao Baoac63a9d2019-08-26 20:33:11 -0700237 if o in ('-z', '--bootable_zip'):
238 OPTIONS.bootable_only = True
Tao Bao57f8ed62019-08-14 23:08:18 -0700239 elif o == '--additional':
240 OPTIONS.additional_entries.append(a)
David Iserovichbabdafe2023-08-10 17:58:31 +0000241 elif o == '--build_super_image':
242 OPTIONS.build_super_image = a
Doug Zongkereef39442009-04-02 12:14:19 -0700243 else:
244 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700245 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700246
247 args = common.ParseOptions(argv, __doc__,
Tao Baoac63a9d2019-08-26 20:33:11 -0700248 extra_opts='z',
Tao Bao57f8ed62019-08-14 23:08:18 -0700249 extra_long_opts=[
250 'additional=',
251 'bootable_zip',
David Iserovichbabdafe2023-08-10 17:58:31 +0000252 'build_super_image=',
Tao Bao57f8ed62019-08-14 23:08:18 -0700253 ],
Doug Zongkereef39442009-04-02 12:14:19 -0700254 extra_option_handler=option_handler)
Doug Zongkereef39442009-04-02 12:14:19 -0700255 if len(args) != 2:
256 common.Usage(__doc__)
257 sys.exit(1)
258
Tao Bao32fcdab2018-10-12 10:30:39 -0700259 common.InitLogging()
260
Tao Bao2aac9c92019-08-02 15:50:32 -0700261 ImgFromTargetFiles(args[0], args[1])
Doug Zongkereef39442009-04-02 12:14:19 -0700262
Tao Baoac63a9d2019-08-26 20:33:11 -0700263 logger.info('done.')
Doug Zongkereef39442009-04-02 12:14:19 -0700264
265
266if __name__ == '__main__':
267 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800268 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700269 main(sys.argv[1:])
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700270 finally:
271 common.Cleanup()