blob: 28b1b53a84f28cb023b96c46f6987da77b72d496 [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
Doug Zongkereef39442009-04-02 12:14:19 -070030"""
31
Tao Bao89fbb0f2017-01-10 10:47:58 -080032from __future__ import print_function
33
Tao Bao32fcdab2018-10-12 10:30:39 -070034import logging
Tao Bao76def242017-11-21 09:25:31 -080035import os
Doug Zongkereef39442009-04-02 12:14:19 -070036import sys
Tao Bao76def242017-11-21 09:25:31 -080037import zipfile
38
39import common
Yifan Hong0e97dbb2019-04-17 14:28:52 -070040from build_super_image import BuildSuperImage
Doug Zongkereef39442009-04-02 12:14:19 -070041
Doug Zongkercf6d5a92014-02-18 10:57:07 -080042if sys.hexversion < 0x02070000:
Tao Baoac63a9d2019-08-26 20:33:11 -070043 print('Python 2.7 or newer is required.', file=sys.stderr)
Doug Zongkereef39442009-04-02 12:14:19 -070044 sys.exit(1)
45
Tao Bao32fcdab2018-10-12 10:30:39 -070046logger = logging.getLogger(__name__)
Doug Zongkereef39442009-04-02 12:14:19 -070047
48OPTIONS = common.OPTIONS
49
Tao Baoac63a9d2019-08-26 20:33:11 -070050OPTIONS.bootable_only = False
51OPTIONS.put_super = None
52OPTIONS.dynamic_partition_list = None
53OPTIONS.super_device_list = None
54OPTIONS.retrofit_dap = None
55OPTIONS.build_super = None
56OPTIONS.sparse_userimages = None
57
Ying Wanga0febe52013-03-20 11:02:05 -070058
Yifan Hong0e97dbb2019-04-17 14:28:52 -070059def LoadOptions(input_file):
Tao Bao2aac9c92019-08-02 15:50:32 -070060 """Loads information from input_file to OPTIONS.
Yifan Hong0e97dbb2019-04-17 14:28:52 -070061
62 Args:
Tao Bao2aac9c92019-08-02 15:50:32 -070063 input_file: Path to the root dir of an extracted target_files zip.
Yifan Hong0e97dbb2019-04-17 14:28:52 -070064 """
65 info = OPTIONS.info_dict = common.LoadInfoDict(input_file)
66
Tao Baoac63a9d2019-08-26 20:33:11 -070067 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 Hong0e97dbb2019-04-17 14:28:52 -070075
76
77def CopyInfo(input_tmp, output_zip):
Tao Bao2aac9c92019-08-02 15:50:32 -070078 """Copies the android-info.txt file from the input to the output."""
Tao Bao2ed665a2015-04-01 11:21:55 -070079 common.ZipWrite(
Tao Baoac63a9d2019-08-26 20:33:11 -070080 output_zip, os.path.join(input_tmp, 'OTA', 'android-info.txt'),
81 'android-info.txt')
Doug Zongkereef39442009-04-02 12:14:19 -070082
83
Yifan Hong0e97dbb2019-04-17 14:28:52 -070084def CopyUserImages(input_tmp, output_zip):
Tao Bao2aac9c92019-08-02 15:50:32 -070085 """Copies user images from the unzipped input and write to output_zip.
Yifan Hong0e97dbb2019-04-17 14:28:52 -070086
87 Args:
88 input_tmp: path to the unzipped input.
89 output_zip: a ZipFile instance to write images to.
90 """
Tao Baoac63a9d2019-08-26 20:33:11 -070091 dynamic_images = [p + '.img' for p in OPTIONS.dynamic_partition_list]
Yifan Hong0e97dbb2019-04-17 14:28:52 -070092
93 # Filter out system_other for launch DAP devices because it is in super image.
Tao Baoac63a9d2019-08-26 20:33:11 -070094 if not OPTIONS.retrofit_dap and 'system' in OPTIONS.dynamic_partition_list:
95 dynamic_images.append('system_other.img')
Yifan Hong0e97dbb2019-04-17 14:28:52 -070096
Tao Baoac63a9d2019-08-26 20:33:11 -070097 images_path = os.path.join(input_tmp, 'IMAGES')
Yifan Hong0e97dbb2019-04-17 14:28:52 -070098 # 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 Baoac63a9d2019-08-26 20:33:11 -0700101 if OPTIONS.bootable_only and image not in ('boot.img', 'recovery.img'):
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700102 continue
Tao Baoac63a9d2019-08-26 20:33:11 -0700103 if not image.endswith('.img'):
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700104 continue
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700105 if OPTIONS.put_super:
Tao Baoac63a9d2019-08-26 20:33:11 -0700106 if image == 'super_empty.img':
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700107 continue
108 if image in dynamic_images:
109 continue
Tao Baoac63a9d2019-08-26 20:33:11 -0700110 logger.info('writing %s to archive...', os.path.join('IMAGES', image))
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700111 common.ZipWrite(output_zip, os.path.join(images_path, image), image)
112
113
114def WriteSuperImages(input_tmp, output_zip):
Tao Bao2aac9c92019-08-02 15:50:32 -0700115 """Writes super images from the unzipped input into output_zip.
116
Tao Baoac63a9d2019-08-26 20:33:11 -0700117 This is only done if super_image_in_update_package is set to 'true'.
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700118
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 Baoac63a9d2019-08-26 20:33:11 -0700133 images_path = os.path.join(input_tmp, 'OTA')
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700134 for device in OPTIONS.super_device_list:
Tao Baoac63a9d2019-08-26 20:33:11 -0700135 image = 'super_%s.img' % device
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700136 image_path = os.path.join(images_path, image)
137 assert os.path.exists(image_path)
Tao Baoac63a9d2019-08-26 20:33:11 -0700138 logger.info('writing %s to archive...', os.path.join('OTA', image))
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700139 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 Baoac63a9d2019-08-26 20:33:11 -0700143 super_file = common.MakeTempFile('super_', '.img')
144 logger.info('building super image %s...', super_file)
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700145 BuildSuperImage(input_tmp, super_file)
Tao Baoac63a9d2019-08-26 20:33:11 -0700146 logger.info('writing super.img to archive...')
147 common.ZipWrite(output_zip, super_file, 'super.img')
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700148
149
Tao Bao2aac9c92019-08-02 15:50:32 -0700150def 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 Baoac63a9d2019-08-26 20:33:11 -0700161 raise ValueError('%s is not a valid zipfile' % input_file)
Tao Bao2aac9c92019-08-02 15:50:32 -0700162
Tao Baoac63a9d2019-08-26 20:33:11 -0700163 logger.info('Building image zip from target files zip.')
Tao Bao2aac9c92019-08-02 15:50:32 -0700164
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 Baoac63a9d2019-08-26 20:33:11 -0700172 output_file, 'w', compression=zipfile.ZIP_DEFLATED,
Tao Bao2aac9c92019-08-02 15:50:32 -0700173 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 Bao2aac9c92019-08-02 15:50:32 -0700180 common.ZipClose(output_zip)
181
182
Doug Zongkereef39442009-04-02 12:14:19 -0700183def main(argv):
184
Dan Albert8b72aef2015-03-23 19:13:21 -0700185 def option_handler(o, _):
Tao Baoac63a9d2019-08-26 20:33:11 -0700186 if o in ('-z', '--bootable_zip'):
187 OPTIONS.bootable_only = True
Doug Zongkereef39442009-04-02 12:14:19 -0700188 else:
189 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700190 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700191
192 args = common.ParseOptions(argv, __doc__,
Tao Baoac63a9d2019-08-26 20:33:11 -0700193 extra_opts='z',
194 extra_long_opts=['bootable_zip'],
Doug Zongkereef39442009-04-02 12:14:19 -0700195 extra_option_handler=option_handler)
196
197 if len(args) != 2:
198 common.Usage(__doc__)
199 sys.exit(1)
200
Tao Bao32fcdab2018-10-12 10:30:39 -0700201 common.InitLogging()
202
Tao Bao2aac9c92019-08-02 15:50:32 -0700203 ImgFromTargetFiles(args[0], args[1])
Doug Zongkereef39442009-04-02 12:14:19 -0700204
Tao Baoac63a9d2019-08-26 20:33:11 -0700205 logger.info('done.')
Doug Zongkereef39442009-04-02 12:14:19 -0700206
207
208if __name__ == '__main__':
209 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800210 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700211 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700212 except common.ExternalError as e:
Tao Baoac63a9d2019-08-26 20:33:11 -0700213 logger.exception('\n ERROR:\n')
Doug Zongkereef39442009-04-02 12:14:19 -0700214 sys.exit(1)
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700215 finally:
216 common.Cleanup()