blob: 1ce22e7ea32819a564de7aee88de81b09b0f9772 [file] [log] [blame]
Doug Zongker3c84f562014-07-31 11:06:30 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2014 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"""
18Given a target-files zipfile that does not contain images (ie, does
19not have an IMAGES/ top-level subdirectory), produce the images and
20add them to the zipfile.
21
Tianjie Xub48589a2016-08-03 19:21:52 -070022Usage: add_img_to_target_files [flag] target_files
23
24 -a (--add_missing)
25 Build and add missing images to "IMAGES/". If this option is
26 not specified, this script will simply exit when "IMAGES/"
27 directory exists in the target file.
28
29 -r (--rebuild_recovery)
30 Rebuild the recovery patch and write it to the system image. Only
31 meaningful when system image needs to be rebuilt.
32
33 --replace_verity_private_key
34 Replace the private key used for verity signing. (same as the option
35 in sign_target_files_apks)
36
37 --replace_verity_public_key
38 Replace the certificate (public key) used for verity verification. (same
39 as the option in sign_target_files_apks)
40
41 --is_signing
42 Skip building & adding the images for "userdata" and "cache" if we
43 are signing the target files.
Doug Zongker3c84f562014-07-31 11:06:30 -070044"""
45
Tao Bao89fbb0f2017-01-10 10:47:58 -080046from __future__ import print_function
47
Doug Zongker3c84f562014-07-31 11:06:30 -070048import sys
49
50if sys.hexversion < 0x02070000:
Tao Bao89fbb0f2017-01-10 10:47:58 -080051 print("Python 2.7 or newer is required.", file=sys.stderr)
Doug Zongker3c84f562014-07-31 11:06:30 -070052 sys.exit(1)
53
Tao Bao822f5842015-09-30 16:01:14 -070054import datetime
Doug Zongker3c84f562014-07-31 11:06:30 -070055import errno
56import os
David Zeuthend995f4b2016-01-29 16:59:17 -050057import shlex
Ying Wang2a048392015-06-25 13:56:53 -070058import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050059import subprocess
Doug Zongker3c84f562014-07-31 11:06:30 -070060import tempfile
61import zipfile
62
Doug Zongker3c84f562014-07-31 11:06:30 -070063import build_image
64import common
Tianjie Xuf1a13182017-01-19 17:39:30 -080065import rangelib
Tianjie Xucfa86222016-03-07 16:31:19 -080066import sparse_img
Doug Zongker3c84f562014-07-31 11:06:30 -070067
68OPTIONS = common.OPTIONS
69
Michael Runge2e0d8fc2014-11-13 21:41:08 -080070OPTIONS.add_missing = False
71OPTIONS.rebuild_recovery = False
Tianjie Xu9ac4cb02017-06-09 16:58:03 -070072OPTIONS.replace_updated_files_list = []
Baligh Uddin59f4ff12015-09-16 21:20:30 -070073OPTIONS.replace_verity_public_key = False
74OPTIONS.replace_verity_private_key = False
Tianjie Xub48589a2016-08-03 19:21:52 -070075OPTIONS.is_signing = False
Doug Zongker3c84f562014-07-31 11:06:30 -070076
Dan Willemsen2ee00d52017-03-05 19:51:56 -080077
78class OutputFile(object):
79 def __init__(self, output_zip, input_dir, prefix, name):
80 self._output_zip = output_zip
81 self.input_name = os.path.join(input_dir, prefix, name)
82
83 if self._output_zip:
84 self._zip_name = os.path.join(prefix, name)
85
86 root, suffix = os.path.splitext(name)
87 self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix)
88 else:
89 self.name = self.input_name
90
91 def Write(self):
92 if self._output_zip:
93 common.ZipWrite(self._output_zip, self.name, self._zip_name)
94
95
Tianjie Xucfa86222016-03-07 16:31:19 -080096def GetCareMap(which, imgname):
97 """Generate care_map of system (or vendor) partition"""
98
99 assert which in ("system", "vendor")
Tianjie Xucfa86222016-03-07 16:31:19 -0800100
101 simg = sparse_img.SparseImage(imgname)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700102 care_map_list = [which]
Tianjie Xuf1a13182017-01-19 17:39:30 -0800103
104 care_map_ranges = simg.care_map
105 key = which + "_adjusted_partition_size"
106 adjusted_blocks = OPTIONS.info_dict.get(key)
107 if adjusted_blocks:
108 assert adjusted_blocks > 0, "blocks should be positive for " + which
109 care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet(
110 "0-%d" % (adjusted_blocks,)))
111
112 care_map_list.append(care_map_ranges.to_string_raw())
Tianjie Xucfa86222016-03-07 16:31:19 -0800113 return care_map_list
114
115
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800116def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700117 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -0500118 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800119
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800120 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img")
121 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800122 print("system.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800123 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800124
125 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -0700126 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
127 ofile.write(data)
128 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800129
Tianjie Xu38af07f2017-05-25 17:38:53 -0700130 arc_name = "SYSTEM/" + fn
131 if arc_name in output_zip.namelist():
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700132 OPTIONS.replace_updated_files_list.append(arc_name)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700133 else:
134 common.ZipWrite(output_zip, ofile.name, arc_name)
135
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800136 if OPTIONS.rebuild_recovery:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800137 print("Building new recovery patch")
Dan Albert8b72aef2015-03-23 19:13:21 -0700138 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
139 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800140
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800141 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map")
142 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img,
143 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -0500144
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800145 return img.name
Doug Zongkerfc44a512014-08-26 13:10:25 -0700146
147
Alex Light4e358ab2016-06-16 14:47:10 -0700148def AddSystemOther(output_zip, prefix="IMAGES/"):
149 """Turn the contents of SYSTEM_OTHER into a system_other image
150 and store it in output_zip."""
151
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800152 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img")
153 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800154 print("system_other.img already exists in %s, no need to rebuild..." % (
155 prefix,))
Alex Light4e358ab2016-06-16 14:47:10 -0700156 return
157
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800158 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img)
Alex Light4e358ab2016-06-16 14:47:10 -0700159
160
Doug Zongkerfc44a512014-08-26 13:10:25 -0700161def AddVendor(output_zip, prefix="IMAGES/"):
162 """Turn the contents of VENDOR into a vendor image and store in it
163 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800164
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800165 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img")
166 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800167 print("vendor.img already exists in %s, no need to rebuild..." % (prefix,))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800168 return img.input_name
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800169
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800170 block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map")
171 CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img,
172 block_list=block_list)
173 return img.name
Doug Zongker3c84f562014-07-31 11:06:30 -0700174
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700175
Tao Baoc633ed02017-05-30 21:46:33 -0700176def AddDtbo(output_zip, prefix="IMAGES/"):
177 """Adds the DTBO image.
178
179 Uses the image under prefix if it already exists. Otherwise looks for the
180 image under PREBUILT_IMAGES/, signs it as needed, and returns the image name.
181 """
182
183 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img")
184 if os.path.exists(img.input_name):
185 print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,))
186 return img.input_name
187
188 dtbo_prebuilt_path = os.path.join(
189 OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img")
190 assert os.path.exists(dtbo_prebuilt_path)
191 shutil.copy(dtbo_prebuilt_path, img.name)
192
193 # AVB-sign the image as needed.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800194 if OPTIONS.info_dict.get("avb_enable") == "true":
Tao Baoc633ed02017-05-30 21:46:33 -0700195 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Tao Bao3ebfdde2017-05-23 23:06:55 -0700196 part_size = OPTIONS.info_dict["dtbo_size"]
Tao Baoc633ed02017-05-30 21:46:33 -0700197 # The AVB hash footer will be replaced if already present.
198 cmd = [avbtool, "add_hash_footer", "--image", img.name,
199 "--partition_size", str(part_size), "--partition_name", "dtbo"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800200 common.AppendAVBSigningArgs(cmd, "dtbo")
201 args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args")
Tao Baoc633ed02017-05-30 21:46:33 -0700202 if args and args.strip():
203 cmd.extend(shlex.split(args))
204 p = common.Run(cmd, stdout=subprocess.PIPE)
205 p.communicate()
206 assert p.returncode == 0, \
207 "avbtool add_hash_footer of %s failed" % (img.name,)
208
209 img.Write()
210 return img.name
211
Doug Zongker3c84f562014-07-31 11:06:30 -0700212
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800213def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800214 print("creating " + what + ".img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700215
Doug Zongker3c84f562014-07-31 11:06:30 -0700216 # The name of the directory it is making an image out of matters to
217 # mkyaffs2image. It wants "system" but we have a directory named
218 # "SYSTEM", so create a symlink.
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800219 temp_dir = tempfile.mkdtemp()
220 OPTIONS.tempfiles.append(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700221 try:
222 os.symlink(os.path.join(input_dir, what.upper()),
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800223 os.path.join(temp_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700224 except OSError as e:
225 # bogus error on my mac version?
226 # File "./build/tools/releasetools/img_from_target_files"
227 # os.path.join(OPTIONS.input_tmp, "system"))
228 # OSError: [Errno 17] File exists
229 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700230 pass
231
232 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
233 fstab = info_dict["fstab"]
Tianjie Xucfa86222016-03-07 16:31:19 -0800234 mount_point = "/" + what
235 if fstab and mount_point in fstab:
236 image_props["fs_type"] = fstab[mount_point].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700237
Tao Bao822f5842015-09-30 16:01:14 -0700238 # Use a fixed timestamp (01/01/2009) when packaging the image.
239 # Bug: 24377993
240 epoch = datetime.datetime.fromtimestamp(0)
241 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
242 image_props["timestamp"] = int(timestamp)
243
Doug Zongker3c84f562014-07-31 11:06:30 -0700244 if what == "system":
245 fs_config_prefix = ""
246 else:
247 fs_config_prefix = what + "_"
248
249 fs_config = os.path.join(
250 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700251 if not os.path.exists(fs_config):
252 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700253
Ying Wanga2292c92015-03-24 19:07:40 -0700254 # Override values loaded from info_dict.
255 if fs_config:
256 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700257 if block_list:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800258 image_props["block_list"] = block_list.name
Ying Wanga2292c92015-03-24 19:07:40 -0700259
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800260 succ = build_image.BuildImage(os.path.join(temp_dir, what),
261 image_props, output_file.name)
Doug Zongker3c84f562014-07-31 11:06:30 -0700262 assert succ, "build " + what + ".img image failed"
263
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800264 output_file.Write()
265 if block_list:
266 block_list.Write()
267
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700268 # Set the 'adjusted_partition_size' that excludes the verity blocks of the
269 # given image. When avb is enabled, this size is the max image size returned
270 # by the avb tool.
Tianjie Xuf1a13182017-01-19 17:39:30 -0800271 is_verity_partition = "verity_block_device" in image_props
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700272 verity_supported = (image_props.get("verity") == "true" or
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800273 image_props.get("avb_enable") == "true")
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700274 is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
275 if verity_supported and (is_verity_partition or is_avb_enable):
Tianjie Xuf1a13182017-01-19 17:39:30 -0800276 adjusted_blocks_value = image_props.get("partition_size")
277 if adjusted_blocks_value:
278 adjusted_blocks_key = what + "_adjusted_partition_size"
279 info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1
280
Doug Zongker3c84f562014-07-31 11:06:30 -0700281
282def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700283 """Create a userdata image and store it in output_zip.
284
285 In most case we just create and store an empty userdata.img;
286 But the invoker can also request to create userdata.img with real
287 data from the target files, by setting "userdata_img_with_data=true"
288 in OPTIONS.info_dict.
289 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700290
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800291 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img")
292 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800293 print("userdata.img already exists in %s, no need to rebuild..." % (
294 prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800295 return
296
Elliott Hughes305b0882016-06-15 17:04:54 -0700297 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700298 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700299 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700300 return
301
Tao Bao89fbb0f2017-01-10 10:47:58 -0800302 print("creating userdata.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700303
Tao Bao822f5842015-09-30 16:01:14 -0700304 # Use a fixed timestamp (01/01/2009) when packaging the image.
305 # Bug: 24377993
306 epoch = datetime.datetime.fromtimestamp(0)
307 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
308 image_props["timestamp"] = int(timestamp)
309
Doug Zongker3c84f562014-07-31 11:06:30 -0700310 # The name of the directory it is making an image out of matters to
311 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700312 # empty dir named "data", or a symlink to the DATA dir,
313 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700314 temp_dir = tempfile.mkdtemp()
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800315 OPTIONS.tempfiles.append(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700316 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700317 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
318 if empty:
319 # Create an empty dir.
320 os.mkdir(user_dir)
321 else:
322 # Symlink to the DATA dir.
323 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
324 user_dir)
325
Doug Zongker3c84f562014-07-31 11:06:30 -0700326 fstab = OPTIONS.info_dict["fstab"]
327 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700328 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700329 succ = build_image.BuildImage(user_dir, image_props, img.name)
330 assert succ, "build userdata.img image failed"
331
332 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800333 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700334
335
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800336def AppendVBMetaArgsForPartition(cmd, partition, img_path, public_key_dir):
337 if not img_path:
338 return
339
340 # Check if chain partition is used.
341 key_path = OPTIONS.info_dict.get("avb_" + partition + "_key_path")
342 if key_path:
343 # extract public key in AVB format to be included in vbmeta.img
344 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
345 public_key_path = os.path.join(public_key_dir, "%s.avbpubkey" % partition)
346 p = common.Run([avbtool, "extract_public_key", "--key", key_path,
347 "--output", public_key_path],
348 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
349 p.communicate()
350 assert p.returncode == 0, \
351 "avbtool extract_public_key fail for partition: %r" % partition
352
353 rollback_index_location = OPTIONS.info_dict[
354 "avb_" + partition + "_rollback_index_location"]
355 cmd.extend(["--chain_partition", "%s:%s:%s" % (
356 partition, rollback_index_location, public_key_path)])
357 else:
358 cmd.extend(["--include_descriptors_from_image", img_path])
359
360
Bowgo Tsai8ee4a3d2017-03-31 15:21:26 +0800361def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path,
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700362 dtbo_img_path, prefix="IMAGES/"):
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400363 """Create a VBMeta image and store it in output_zip."""
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800364 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img")
Tao Baoc633ed02017-05-30 21:46:33 -0700365 avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800366 cmd = [avbtool, "make_vbmeta_image", "--output", img.name]
367 common.AppendAVBSigningArgs(cmd, "vbmeta")
368
369 public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-")
370 OPTIONS.tempfiles.append(public_key_dir)
371
372 AppendVBMetaArgsForPartition(cmd, "boot", boot_img_path, public_key_dir)
373 AppendVBMetaArgsForPartition(cmd, "system", system_img_path, public_key_dir)
374 AppendVBMetaArgsForPartition(cmd, "vendor", vendor_img_path, public_key_dir)
375 AppendVBMetaArgsForPartition(cmd, "dtbo", dtbo_img_path, public_key_dir)
376
377 args = OPTIONS.info_dict.get("avb_vbmeta_args")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400378 if args and args.strip():
Tao Bao9a5f4192017-07-20 23:51:16 -0700379 split_args = shlex.split(args)
380 for index, arg in enumerate(split_args[:-1]):
381 # Sanity check that the image file exists. Some images might be defined
382 # as a path relative to source tree, which may not be available at the
383 # same location when running this script (we have the input target_files
384 # zip only). For such cases, we additionally scan other locations (e.g.
385 # IMAGES/, RADIO/, etc) before bailing out.
386 if arg == '--include_descriptors_from_image':
387 image_path = split_args[index + 1]
388 if os.path.exists(image_path):
389 continue
390 found = False
391 for dir in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']:
392 alt_path = os.path.join(
393 OPTIONS.input_tmp, dir, os.path.basename(image_path))
394 if os.path.exists(alt_path):
395 split_args[index + 1] = alt_path
396 found = True
397 break
398 assert found, 'failed to find %s' % (image_path,)
399 cmd.extend(split_args)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800400
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400401 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
402 p.communicate()
403 assert p.returncode == 0, "avbtool make_vbmeta_image failed"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800404 img.Write()
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400405
406
David Zeuthen25328622016-04-08 15:08:03 -0400407def AddPartitionTable(output_zip, prefix="IMAGES/"):
408 """Create a partition table image and store it in output_zip."""
409
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800410 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img")
411 bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt")
David Zeuthen25328622016-04-08 15:08:03 -0400412
413 # use BPTTOOL from environ, or "bpttool" if empty or not set.
414 bpttool = os.getenv("BPTTOOL") or "bpttool"
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800415 cmd = [bpttool, "make_table", "--output_json", bpt.name,
416 "--output_gpt", img.name]
David Zeuthen25328622016-04-08 15:08:03 -0400417 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
418 input_files = input_files_str.split(" ")
419 for i in input_files:
420 cmd.extend(["--input", i])
421 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
422 if disk_size:
423 cmd.extend(["--disk_size", disk_size])
424 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
425 if args:
426 cmd.extend(shlex.split(args))
427
428 p = common.Run(cmd, stdout=subprocess.PIPE)
429 p.communicate()
430 assert p.returncode == 0, "bpttool make_table failed"
431
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800432 img.Write()
433 bpt.Write()
David Zeuthen25328622016-04-08 15:08:03 -0400434
435
Doug Zongker3c84f562014-07-31 11:06:30 -0700436def AddCache(output_zip, prefix="IMAGES/"):
437 """Create an empty cache image and store it in output_zip."""
438
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800439 img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img")
440 if os.path.exists(img.input_name):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800441 print("cache.img already exists in %s, no need to rebuild..." % (prefix,))
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800442 return
443
Tao Bao2c15d9e2015-07-09 11:51:16 -0700444 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700445 # The build system has to explicitly request for cache.img.
446 if "fs_type" not in image_props:
447 return
448
Tao Bao89fbb0f2017-01-10 10:47:58 -0800449 print("creating cache.img...")
Doug Zongker3c84f562014-07-31 11:06:30 -0700450
Tao Bao822f5842015-09-30 16:01:14 -0700451 # Use a fixed timestamp (01/01/2009) when packaging the image.
452 # Bug: 24377993
453 epoch = datetime.datetime.fromtimestamp(0)
454 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
455 image_props["timestamp"] = int(timestamp)
456
Doug Zongker3c84f562014-07-31 11:06:30 -0700457 # The name of the directory it is making an image out of matters to
458 # mkyaffs2image. So we create a temp dir, and within it we create an
459 # empty dir named "cache", and build the image from that.
460 temp_dir = tempfile.mkdtemp()
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800461 OPTIONS.tempfiles.append(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700462 user_dir = os.path.join(temp_dir, "cache")
463 os.mkdir(user_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700464
465 fstab = OPTIONS.info_dict["fstab"]
466 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700467 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700468 succ = build_image.BuildImage(user_dir, image_props, img.name)
469 assert succ, "build cache.img image failed"
470
471 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800472 img.Write()
Doug Zongker3c84f562014-07-31 11:06:30 -0700473
474
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700475def ReplaceUpdatedFiles(zip_filename, files_list):
476 """Update all the zip entries listed in the files_list.
Tianjie Xu38af07f2017-05-25 17:38:53 -0700477
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700478 For now the list includes META/care_map.txt, and the related files under
479 SYSTEM/ after rebuilding recovery.
480 """
481
482 cmd = ["zip", "-d", zip_filename] + files_list
Tianjie Xu38af07f2017-05-25 17:38:53 -0700483 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
484 p.communicate()
485
486 output_zip = zipfile.ZipFile(zip_filename, "a",
487 compression=zipfile.ZIP_DEFLATED,
488 allowZip64=True)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700489 for item in files_list:
Tianjie Xu38af07f2017-05-25 17:38:53 -0700490 file_path = os.path.join(OPTIONS.input_tmp, item)
491 assert os.path.exists(file_path)
492 common.ZipWrite(output_zip, file_path, arcname=item)
493 common.ZipClose(output_zip)
494
495
Doug Zongker3c84f562014-07-31 11:06:30 -0700496def AddImagesToTargetFiles(filename):
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800497 if os.path.isdir(filename):
498 OPTIONS.input_tmp = os.path.abspath(filename)
499 input_zip = None
500 else:
501 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700502
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800503 if not OPTIONS.add_missing:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800504 if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")):
505 print("target_files appears to already contain images.")
506 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700507
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800508 has_vendor = os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR"))
509 has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp,
510 "SYSTEM_OTHER"))
Doug Zongker3c84f562014-07-31 11:06:30 -0700511
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800512 if input_zip:
513 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Alex Light4e358ab2016-06-16 14:47:10 -0700514
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800515 common.ZipClose(input_zip)
516 output_zip = zipfile.ZipFile(filename, "a",
517 compression=zipfile.ZIP_DEFLATED,
518 allowZip64=True)
519 else:
520 OPTIONS.info_dict = common.LoadInfoDict(filename, filename)
521 output_zip = None
522 images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")
523 if not os.path.isdir(images_dir):
524 os.makedirs(images_dir)
525 images_dir = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700526
Tao Baodb45efa2015-10-27 19:25:18 -0700527 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
528
Doug Zongkerfc44a512014-08-26 13:10:25 -0700529 def banner(s):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800530 print("\n\n++++ " + s + " ++++\n\n")
Doug Zongker3c84f562014-07-31 11:06:30 -0700531
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800532 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
533 boot_image = None
534 if os.path.exists(prebuilt_path):
David Zeuthend995f4b2016-01-29 16:59:17 -0500535 banner("boot")
Tao Bao89fbb0f2017-01-10 10:47:58 -0800536 print("boot.img already exists in IMAGES/, no need to rebuild...")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800537 if OPTIONS.rebuild_recovery:
538 boot_image = common.GetBootableImage(
539 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
540 else:
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400541 banner("boot")
542 boot_image = common.GetBootableImage(
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800543 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400544 if boot_image:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800545 if output_zip:
546 boot_image.AddToZip(output_zip)
547 else:
548 boot_image.WriteToDir(OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700549
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800550 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700551 if has_recovery:
552 banner("recovery")
553 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
554 if os.path.exists(prebuilt_path):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800555 print("recovery.img already exists in IMAGES/, no need to rebuild...")
Tao Baodb45efa2015-10-27 19:25:18 -0700556 if OPTIONS.rebuild_recovery:
557 recovery_image = common.GetBootableImage(
558 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
559 "RECOVERY")
560 else:
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800561 recovery_image = common.GetBootableImage(
562 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
Tao Baodb45efa2015-10-27 19:25:18 -0700563 if recovery_image:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800564 if output_zip:
565 recovery_image.AddToZip(output_zip)
566 else:
567 recovery_image.WriteToDir(OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700568
Tao Baod42e97e2016-11-30 12:11:57 -0800569 banner("recovery (two-step image)")
570 # The special recovery.img for two-step package use.
571 recovery_two_step_image = common.GetBootableImage(
572 "IMAGES/recovery-two-step.img", "recovery-two-step.img",
573 OPTIONS.input_tmp, "RECOVERY", two_step_image=True)
574 if recovery_two_step_image:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800575 if output_zip:
576 recovery_two_step_image.AddToZip(output_zip)
577 else:
578 recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)
Tao Baod42e97e2016-11-30 12:11:57 -0800579
Doug Zongkerfc44a512014-08-26 13:10:25 -0700580 banner("system")
David Zeuthend995f4b2016-01-29 16:59:17 -0500581 system_img_path = AddSystem(
Tao Baoc633ed02017-05-30 21:46:33 -0700582 output_zip, recovery_img=recovery_image, boot_img=boot_image)
Tianjie Xu737afb92016-07-11 11:42:53 -0700583 vendor_img_path = None
Doug Zongkerfc44a512014-08-26 13:10:25 -0700584 if has_vendor:
585 banner("vendor")
Tianjie Xu737afb92016-07-11 11:42:53 -0700586 vendor_img_path = AddVendor(output_zip)
Alex Light4e358ab2016-06-16 14:47:10 -0700587 if has_system_other:
588 banner("system_other")
589 AddSystemOther(output_zip)
Tianjie Xub48589a2016-08-03 19:21:52 -0700590 if not OPTIONS.is_signing:
591 banner("userdata")
592 AddUserdata(output_zip)
593 banner("cache")
594 AddCache(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700595
596 if OPTIONS.info_dict.get("board_bpt_enable") == "true":
David Zeuthen25328622016-04-08 15:08:03 -0400597 banner("partition-table")
598 AddPartitionTable(output_zip)
Tao Baoc633ed02017-05-30 21:46:33 -0700599
600 dtbo_img_path = None
601 if OPTIONS.info_dict.get("has_dtbo") == "true":
602 banner("dtbo")
603 dtbo_img_path = AddDtbo(output_zip)
604
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800605 if OPTIONS.info_dict.get("avb_enable") == "true":
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400606 banner("vbmeta")
607 boot_contents = boot_image.WriteToTemp()
Yueyao Zhu889ee5e2017-05-12 17:50:46 -0700608 AddVBMeta(output_zip, boot_contents.name, system_img_path,
609 vendor_img_path, dtbo_img_path)
Doug Zongker3c84f562014-07-31 11:06:30 -0700610
Wei Wang2e735ca2016-05-10 22:48:13 -0700611 # For devices using A/B update, copy over images from RADIO/ and/or
612 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
613 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700614 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800615 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
616 if os.path.exists(ab_partitions):
617 with open(ab_partitions, 'r') as f:
618 lines = f.readlines()
Tianjie Xucfa86222016-03-07 16:31:19 -0800619 # For devices using A/B update, generate care_map for system and vendor
620 # partitions (if present), then write this file to target_files package.
621 care_map_list = []
Tao Baoa0421cd2015-11-16 16:32:27 -0800622 for line in lines:
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700623 if line.strip() == "system" and (
624 "system_verity_block_device" in OPTIONS.info_dict or
Tao Bao3f721762017-06-29 15:11:44 -0700625 OPTIONS.info_dict.get("avb_system_hashtree_enable") == "true"):
Tianjie Xu737afb92016-07-11 11:42:53 -0700626 assert os.path.exists(system_img_path)
627 care_map_list += GetCareMap("system", system_img_path)
Tianjie Xu6b2e1552017-06-01 11:32:32 -0700628 if line.strip() == "vendor" and (
629 "vendor_verity_block_device" in OPTIONS.info_dict or
Tao Bao3f721762017-06-29 15:11:44 -0700630 OPTIONS.info_dict.get("avb_vendor_hashtree_enable") == "true"):
Tianjie Xu737afb92016-07-11 11:42:53 -0700631 assert os.path.exists(vendor_img_path)
632 care_map_list += GetCareMap("vendor", vendor_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800633
Tao Baoa0421cd2015-11-16 16:32:27 -0800634 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700635 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
636 if os.path.exists(prebuilt_path):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800637 print("%s already exists, no need to overwrite..." % (img_name,))
Tianjie Xuaaca4212016-06-28 14:34:03 -0700638 continue
639
Tao Baoa0421cd2015-11-16 16:32:27 -0800640 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700641 img_vendor_dir = os.path.join(
642 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800643 if os.path.exists(img_radio_path):
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800644 if output_zip:
645 common.ZipWrite(output_zip, img_radio_path,
646 os.path.join("IMAGES", img_name))
647 else:
648 shutil.copy(img_radio_path, prebuilt_path)
Wei Wang2e735ca2016-05-10 22:48:13 -0700649 else:
650 for root, _, files in os.walk(img_vendor_dir):
651 if img_name in files:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800652 if output_zip:
653 common.ZipWrite(output_zip, os.path.join(root, img_name),
654 os.path.join("IMAGES", img_name))
655 else:
656 shutil.copy(os.path.join(root, img_name), prebuilt_path)
Wei Wang2e735ca2016-05-10 22:48:13 -0700657 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800658
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800659 if output_zip:
660 # Zip spec says: All slashes MUST be forward slashes.
661 img_path = 'IMAGES/' + img_name
662 assert img_path in output_zip.namelist(), "cannot find " + img_name
663 else:
664 img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
665 assert os.path.exists(img_path), "cannot find " + img_name
Tao Baoa0421cd2015-11-16 16:32:27 -0800666
Tianjie Xucfa86222016-03-07 16:31:19 -0800667 if care_map_list:
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700668 care_map_path = "META/care_map.txt"
669 if output_zip and care_map_path not in output_zip.namelist():
670 common.ZipWriteStr(output_zip, care_map_path, '\n'.join(care_map_list))
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800671 else:
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700672 with open(os.path.join(OPTIONS.input_tmp, care_map_path), 'w') as fp:
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800673 fp.write('\n'.join(care_map_list))
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700674 if output_zip:
675 OPTIONS.replace_updated_files_list.append(care_map_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800676
Tao Bao95a95c32017-06-16 15:30:23 -0700677 # Radio images that need to be packed into IMAGES/, and product-img.zip.
678 pack_radioimages = os.path.join(
679 OPTIONS.input_tmp, "META", "pack_radioimages.txt")
680 if os.path.exists(pack_radioimages):
681 with open(pack_radioimages, 'r') as f:
682 lines = f.readlines()
683 for line in lines:
684 img_name = line.strip()
685 _, ext = os.path.splitext(img_name)
686 if not ext:
687 img_name += ".img"
688 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
689 if os.path.exists(prebuilt_path):
690 print("%s already exists, no need to overwrite..." % (img_name,))
691 continue
692
693 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
694 assert os.path.exists(img_radio_path), \
695 "Failed to find %s at %s" % (img_name, img_radio_path)
696 if output_zip:
697 common.ZipWrite(output_zip, img_radio_path,
698 os.path.join("IMAGES", img_name))
699 else:
700 shutil.copy(img_radio_path, prebuilt_path)
701
Dan Willemsen2ee00d52017-03-05 19:51:56 -0800702 if output_zip:
703 common.ZipClose(output_zip)
Tianjie Xu9ac4cb02017-06-09 16:58:03 -0700704 if OPTIONS.replace_updated_files_list:
705 ReplaceUpdatedFiles(output_zip.filename,
706 OPTIONS.replace_updated_files_list)
Tianjie Xu38af07f2017-05-25 17:38:53 -0700707
Doug Zongker3c84f562014-07-31 11:06:30 -0700708
Doug Zongker3c84f562014-07-31 11:06:30 -0700709def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700710 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800711 if o in ("-a", "--add_missing"):
712 OPTIONS.add_missing = True
713 elif o in ("-r", "--rebuild_recovery",):
714 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700715 elif o == "--replace_verity_private_key":
716 OPTIONS.replace_verity_private_key = (True, a)
717 elif o == "--replace_verity_public_key":
718 OPTIONS.replace_verity_public_key = (True, a)
Tianjie Xub48589a2016-08-03 19:21:52 -0700719 elif o == "--is_signing":
720 OPTIONS.is_signing = True
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800721 else:
722 return False
723 return True
724
Dan Albert8b72aef2015-03-23 19:13:21 -0700725 args = common.ParseOptions(
726 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700727 extra_long_opts=["add_missing", "rebuild_recovery",
728 "replace_verity_public_key=",
729 "replace_verity_private_key=",
Tao Bao45810422016-10-17 16:20:12 -0700730 "is_signing"],
Dan Albert8b72aef2015-03-23 19:13:21 -0700731 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800732
Doug Zongker3c84f562014-07-31 11:06:30 -0700733
734 if len(args) != 1:
735 common.Usage(__doc__)
736 sys.exit(1)
737
738 AddImagesToTargetFiles(args[0])
Tao Bao89fbb0f2017-01-10 10:47:58 -0800739 print("done.")
Doug Zongker3c84f562014-07-31 11:06:30 -0700740
741if __name__ == '__main__':
742 try:
743 common.CloseInheritedPipes()
744 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700745 except common.ExternalError as e:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800746 print("\n ERROR: %s\n" % (e,))
Doug Zongker3c84f562014-07-31 11:06:30 -0700747 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700748 finally:
749 common.Cleanup()