blob: 6165d96d6e49f540a75ac6bac45d9023581a63bf [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"""
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070018Given target-files, produces an image zipfile suitable for use
19with 'fastboot update'.
Doug Zongkereef39442009-04-02 12:14:19 -070020
21Usage: img_from_target_files [flags] input_target_files output_image_zip
22
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070023input_target_files: one of the following:
24 - directory containing extracted target files. It will load info from
Yifan Hong0e97dbb2019-04-17 14:28:52 -070025 OTA/android-info.txt, META/misc_info.txt and build the image zipfile using
26 images from IMAGES/.
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070027 - target files package. Same as above, but extracts the archive before
28 building the image zipfile.
29
30Flags:
Doug Zongker55d93282011-01-25 17:03:34 -080031 -z (--bootable_zip)
32 Include only the bootable images (eg 'boot' and 'recovery') in
33 the output.
34
Doug Zongkereef39442009-04-02 12:14:19 -070035"""
36
Tao Bao89fbb0f2017-01-10 10:47:58 -080037from __future__ import print_function
38
Tao Bao32fcdab2018-10-12 10:30:39 -070039import logging
Tao Bao76def242017-11-21 09:25:31 -080040import os
41import shutil
Doug Zongkereef39442009-04-02 12:14:19 -070042import sys
Tao Bao76def242017-11-21 09:25:31 -080043import zipfile
44
45import common
Yifan Hong0e97dbb2019-04-17 14:28:52 -070046from build_super_image import BuildSuperImage
Doug Zongkereef39442009-04-02 12:14:19 -070047
Doug Zongkercf6d5a92014-02-18 10:57:07 -080048if sys.hexversion < 0x02070000:
Tao Bao89fbb0f2017-01-10 10:47:58 -080049 print("Python 2.7 or newer is required.", file=sys.stderr)
Doug Zongkereef39442009-04-02 12:14:19 -070050 sys.exit(1)
51
Tao Bao32fcdab2018-10-12 10:30:39 -070052logger = logging.getLogger(__name__)
Doug Zongkereef39442009-04-02 12:14:19 -070053
54OPTIONS = common.OPTIONS
55
Ying Wanga0febe52013-03-20 11:02:05 -070056
Yifan Hong0e97dbb2019-04-17 14:28:52 -070057def 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
77def CopyInfo(input_tmp, output_zip):
Doug Zongkereef39442009-04-02 12:14:19 -070078 """Copy the android-info.txt file from the input to the output."""
Tao Bao2ed665a2015-04-01 11:21:55 -070079 common.ZipWrite(
Yifan Hong0e97dbb2019-04-17 14:28:52 -070080 output_zip, os.path.join(input_tmp, "OTA", "android-info.txt"),
Tao Bao2ed665a2015-04-01 11:21:55 -070081 "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):
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
117def 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 Zongkereef39442009-04-02 12:14:19 -0700153def main(argv):
Tao Bao76def242017-11-21 09:25:31 -0800154 # This allows modifying the value from inner function.
155 bootable_only_array = [False]
Doug Zongkereef39442009-04-02 12:14:19 -0700156
Dan Albert8b72aef2015-03-23 19:13:21 -0700157 def option_handler(o, _):
Doug Zongker55d93282011-01-25 17:03:34 -0800158 if o in ("-z", "--bootable_zip"):
Tao Bao76def242017-11-21 09:25:31 -0800159 bootable_only_array[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -0700160 else:
161 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700162 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700163
164 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -0700165 extra_opts="z",
166 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -0700167 extra_option_handler=option_handler)
168
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700169 OPTIONS.bootable_only = bootable_only_array[0]
Doug Zongker55d93282011-01-25 17:03:34 -0800170
Doug Zongkereef39442009-04-02 12:14:19 -0700171 if len(args) != 2:
172 common.Usage(__doc__)
173 sys.exit(1)
174
Tao Bao32fcdab2018-10-12 10:30:39 -0700175 common.InitLogging()
176
Daniel Normanb8a2f9d2019-04-24 12:55:51 -0700177 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 Hong0e97dbb2019-04-17 14:28:52 -0700183 OPTIONS.input_tmp = common.UnzipTemp(target_files,
184 ["IMAGES/*", "OTA/*", "META/*"])
Daniel Normanb8a2f9d2019-04-24 12:55:51 -0700185 else:
186 raise ValueError("%s is not a valid path." % target_files)
187
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700188 LoadOptions(OPTIONS.input_tmp)
189 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED,
190 allowZip64=not OPTIONS.sparse_userimages)
Doug Zongkereef39442009-04-02 12:14:19 -0700191
Doug Zongker3c84f562014-07-31 11:06:30 -0700192 try:
Yifan Hong0e97dbb2019-04-17 14:28:52 -0700193 CopyInfo(OPTIONS.input_tmp, output_zip)
194 CopyUserImages(OPTIONS.input_tmp, output_zip)
195 WriteSuperImages(OPTIONS.input_tmp, output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700196 finally:
Tao Bao32fcdab2018-10-12 10:30:39 -0700197 logger.info("cleaning up...")
Tao Bao2ed665a2015-04-01 11:21:55 -0700198 common.ZipClose(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700199
Tao Bao32fcdab2018-10-12 10:30:39 -0700200 logger.info("done.")
Doug Zongkereef39442009-04-02 12:14:19 -0700201
202
203if __name__ == '__main__':
204 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800205 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700206 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700207 except common.ExternalError as e:
Tao Bao32fcdab2018-10-12 10:30:39 -0700208 logger.exception("\n ERROR:\n")
Doug Zongkereef39442009-04-02 12:14:19 -0700209 sys.exit(1)
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700210 finally:
211 common.Cleanup()