blob: 8309463cdb1ac19502ab8ef03a7bc8b8657932fa [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
46import sys
47
48if sys.hexversion < 0x02070000:
49 print >> sys.stderr, "Python 2.7 or newer is required."
50 sys.exit(1)
51
Tao Bao822f5842015-09-30 16:01:14 -070052import datetime
Doug Zongker3c84f562014-07-31 11:06:30 -070053import errno
54import os
David Zeuthend995f4b2016-01-29 16:59:17 -050055import shlex
Ying Wang2a048392015-06-25 13:56:53 -070056import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050057import subprocess
Doug Zongker3c84f562014-07-31 11:06:30 -070058import tempfile
59import zipfile
60
Doug Zongker3c84f562014-07-31 11:06:30 -070061import build_image
62import common
Tianjie Xucfa86222016-03-07 16:31:19 -080063import sparse_img
Doug Zongker3c84f562014-07-31 11:06:30 -070064
65OPTIONS = common.OPTIONS
66
Michael Runge2e0d8fc2014-11-13 21:41:08 -080067OPTIONS.add_missing = False
68OPTIONS.rebuild_recovery = False
Baligh Uddin59f4ff12015-09-16 21:20:30 -070069OPTIONS.replace_verity_public_key = False
70OPTIONS.replace_verity_private_key = False
Tianjie Xub48589a2016-08-03 19:21:52 -070071OPTIONS.is_signing = False
Doug Zongker3c84f562014-07-31 11:06:30 -070072
Tianjie Xucfa86222016-03-07 16:31:19 -080073def GetCareMap(which, imgname):
74 """Generate care_map of system (or vendor) partition"""
75
76 assert which in ("system", "vendor")
77 _, blk_device = common.GetTypeAndDevice("/" + which, OPTIONS.info_dict)
78
79 simg = sparse_img.SparseImage(imgname)
80 care_map_list = []
81 care_map_list.append(blk_device)
82 care_map_list.append(simg.care_map.to_string_raw())
83 return care_map_list
84
85
Michael Runge2e0d8fc2014-11-13 21:41:08 -080086def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070087 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -050088 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080089
90 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
91 if os.path.exists(prebuilt_path):
92 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
David Zeuthend995f4b2016-01-29 16:59:17 -050093 return prebuilt_path
Michael Runge2e0d8fc2014-11-13 21:41:08 -080094
95 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -070096 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
97 ofile.write(data)
98 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -080099
100 if OPTIONS.rebuild_recovery:
Dan Albert8b72aef2015-03-23 19:13:21 -0700101 print "Building new recovery patch"
102 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
103 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800104
Doug Zongkerfc44a512014-08-26 13:10:25 -0700105 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
106 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
107 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -0500108
Dan Albert8e0178d2015-01-27 15:53:15 -0800109 common.ZipWrite(output_zip, imgname, prefix + "system.img")
110 common.ZipWrite(output_zip, block_list, prefix + "system.map")
David Zeuthend995f4b2016-01-29 16:59:17 -0500111 return imgname
Doug Zongkerfc44a512014-08-26 13:10:25 -0700112
113
114def BuildSystem(input_dir, info_dict, block_list=None):
115 """Build the (sparse) system image and return the name of a temp
116 file containing it."""
117 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
118
119
Alex Light4e358ab2016-06-16 14:47:10 -0700120def AddSystemOther(output_zip, prefix="IMAGES/"):
121 """Turn the contents of SYSTEM_OTHER into a system_other image
122 and store it in output_zip."""
123
124 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system_other.img")
125 if os.path.exists(prebuilt_path):
126 print "system_other.img already exists in %s, no need to rebuild..." % (prefix,)
127 return
128
129 imgname = BuildSystemOther(OPTIONS.input_tmp, OPTIONS.info_dict)
130 common.ZipWrite(output_zip, imgname, prefix + "system_other.img")
131
132def BuildSystemOther(input_dir, info_dict):
133 """Build the (sparse) system_other image and return the name of a temp
134 file containing it."""
135 return CreateImage(input_dir, info_dict, "system_other", block_list=None)
136
137
Doug Zongkerfc44a512014-08-26 13:10:25 -0700138def AddVendor(output_zip, prefix="IMAGES/"):
139 """Turn the contents of VENDOR into a vendor image and store in it
140 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800141
142 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
143 if os.path.exists(prebuilt_path):
144 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
Tianjie Xucfa86222016-03-07 16:31:19 -0800145 return prebuilt_path
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800146
Doug Zongkerfc44a512014-08-26 13:10:25 -0700147 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
148 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Dan Albert8b72aef2015-03-23 19:13:21 -0700149 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -0800150 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
151 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Tianjie Xucfa86222016-03-07 16:31:19 -0800152 return imgname
Doug Zongker3c84f562014-07-31 11:06:30 -0700153
154
Doug Zongkerfc44a512014-08-26 13:10:25 -0700155def BuildVendor(input_dir, info_dict, block_list=None):
156 """Build the (sparse) vendor image and return the name of a temp
157 file containing it."""
158 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
159
160
161def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700162 print "creating " + what + ".img..."
163
Doug Zongkerfc44a512014-08-26 13:10:25 -0700164 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700165
166 # The name of the directory it is making an image out of matters to
167 # mkyaffs2image. It wants "system" but we have a directory named
168 # "SYSTEM", so create a symlink.
169 try:
170 os.symlink(os.path.join(input_dir, what.upper()),
171 os.path.join(input_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700172 except OSError as e:
173 # bogus error on my mac version?
174 # File "./build/tools/releasetools/img_from_target_files"
175 # os.path.join(OPTIONS.input_tmp, "system"))
176 # OSError: [Errno 17] File exists
177 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700178 pass
179
180 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
181 fstab = info_dict["fstab"]
Tianjie Xucfa86222016-03-07 16:31:19 -0800182 mount_point = "/" + what
183 if fstab and mount_point in fstab:
184 image_props["fs_type"] = fstab[mount_point].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700185
Tao Bao822f5842015-09-30 16:01:14 -0700186 # Use a fixed timestamp (01/01/2009) when packaging the image.
187 # Bug: 24377993
188 epoch = datetime.datetime.fromtimestamp(0)
189 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
190 image_props["timestamp"] = int(timestamp)
191
Doug Zongker3c84f562014-07-31 11:06:30 -0700192 if what == "system":
193 fs_config_prefix = ""
194 else:
195 fs_config_prefix = what + "_"
196
197 fs_config = os.path.join(
198 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700199 if not os.path.exists(fs_config):
200 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700201
Ying Wanga2292c92015-03-24 19:07:40 -0700202 # Override values loaded from info_dict.
203 if fs_config:
204 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700205 if block_list:
206 image_props["block_list"] = block_list
Ying Wanga2292c92015-03-24 19:07:40 -0700207
Doug Zongker3c84f562014-07-31 11:06:30 -0700208 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700209 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700210 assert succ, "build " + what + ".img image failed"
211
Doug Zongkerfc44a512014-08-26 13:10:25 -0700212 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700213
214
215def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700216 """Create a userdata image and store it in output_zip.
217
218 In most case we just create and store an empty userdata.img;
219 But the invoker can also request to create userdata.img with real
220 data from the target files, by setting "userdata_img_with_data=true"
221 in OPTIONS.info_dict.
222 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700223
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800224 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
225 if os.path.exists(prebuilt_path):
226 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
227 return
228
Elliott Hughes305b0882016-06-15 17:04:54 -0700229 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700230 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700231 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700232 return
233
234 print "creating userdata.img..."
235
Tao Bao822f5842015-09-30 16:01:14 -0700236 # Use a fixed timestamp (01/01/2009) when packaging the image.
237 # Bug: 24377993
238 epoch = datetime.datetime.fromtimestamp(0)
239 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
240 image_props["timestamp"] = int(timestamp)
241
Doug Zongker3c84f562014-07-31 11:06:30 -0700242 # The name of the directory it is making an image out of matters to
243 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700244 # empty dir named "data", or a symlink to the DATA dir,
245 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700246 temp_dir = tempfile.mkdtemp()
247 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700248 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
249 if empty:
250 # Create an empty dir.
251 os.mkdir(user_dir)
252 else:
253 # Symlink to the DATA dir.
254 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
255 user_dir)
256
Doug Zongker3c84f562014-07-31 11:06:30 -0700257 img = tempfile.NamedTemporaryFile()
258
259 fstab = OPTIONS.info_dict["fstab"]
260 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700261 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700262 succ = build_image.BuildImage(user_dir, image_props, img.name)
263 assert succ, "build userdata.img image failed"
264
265 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700266 common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700267 img.close()
Ying Wang2a048392015-06-25 13:56:53 -0700268 shutil.rmtree(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700269
270
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400271def AddVBMeta(output_zip, boot_img_path, system_img_path, prefix="IMAGES/"):
272 """Create a VBMeta image and store it in output_zip."""
273 _, img_file_name = tempfile.mkstemp()
274 avbtool = os.getenv('AVBTOOL') or "avbtool"
275 cmd = [avbtool, "make_vbmeta_image",
276 "--output", img_file_name,
277 "--include_descriptors_from_image", boot_img_path,
278 "--include_descriptors_from_image", system_img_path,
279 "--generate_dm_verity_cmdline_from_hashtree", system_img_path]
280 common.AppendAVBSigningArgs(cmd)
281 args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args", None)
282 if args and args.strip():
283 cmd.extend(shlex.split(args))
284 p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
285 p.communicate()
286 assert p.returncode == 0, "avbtool make_vbmeta_image failed"
287 common.ZipWrite(output_zip, img_file_name, prefix + "vbmeta.img")
288
289
David Zeuthen25328622016-04-08 15:08:03 -0400290def AddPartitionTable(output_zip, prefix="IMAGES/"):
291 """Create a partition table image and store it in output_zip."""
292
293 _, img_file_name = tempfile.mkstemp()
294 _, bpt_file_name = tempfile.mkstemp()
295
296 # use BPTTOOL from environ, or "bpttool" if empty or not set.
297 bpttool = os.getenv("BPTTOOL") or "bpttool"
298 cmd = [bpttool, "make_table", "--output_json", bpt_file_name,
299 "--output_gpt", img_file_name]
300 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
301 input_files = input_files_str.split(" ")
302 for i in input_files:
303 cmd.extend(["--input", i])
304 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
305 if disk_size:
306 cmd.extend(["--disk_size", disk_size])
307 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
308 if args:
309 cmd.extend(shlex.split(args))
310
311 p = common.Run(cmd, stdout=subprocess.PIPE)
312 p.communicate()
313 assert p.returncode == 0, "bpttool make_table failed"
314
315 common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img")
316 common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt")
317
318
Doug Zongker3c84f562014-07-31 11:06:30 -0700319def AddCache(output_zip, prefix="IMAGES/"):
320 """Create an empty cache image and store it in output_zip."""
321
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800322 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
323 if os.path.exists(prebuilt_path):
324 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
325 return
326
Tao Bao2c15d9e2015-07-09 11:51:16 -0700327 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700328 # The build system has to explicitly request for cache.img.
329 if "fs_type" not in image_props:
330 return
331
332 print "creating cache.img..."
333
Tao Bao822f5842015-09-30 16:01:14 -0700334 # Use a fixed timestamp (01/01/2009) when packaging the image.
335 # Bug: 24377993
336 epoch = datetime.datetime.fromtimestamp(0)
337 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
338 image_props["timestamp"] = int(timestamp)
339
Doug Zongker3c84f562014-07-31 11:06:30 -0700340 # The name of the directory it is making an image out of matters to
341 # mkyaffs2image. So we create a temp dir, and within it we create an
342 # empty dir named "cache", and build the image from that.
343 temp_dir = tempfile.mkdtemp()
344 user_dir = os.path.join(temp_dir, "cache")
345 os.mkdir(user_dir)
346 img = tempfile.NamedTemporaryFile()
347
348 fstab = OPTIONS.info_dict["fstab"]
349 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700350 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700351 succ = build_image.BuildImage(user_dir, image_props, img.name)
352 assert succ, "build cache.img image failed"
353
354 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700355 common.ZipWrite(output_zip, img.name, prefix + "cache.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700356 img.close()
357 os.rmdir(user_dir)
358 os.rmdir(temp_dir)
359
360
361def AddImagesToTargetFiles(filename):
362 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700363
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800364 if not OPTIONS.add_missing:
365 for n in input_zip.namelist():
366 if n.startswith("IMAGES/"):
367 print "target_files appears to already contain images."
368 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700369
Doug Zongker3c84f562014-07-31 11:06:30 -0700370 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700371 input_zip.getinfo("VENDOR/")
372 has_vendor = True
373 except KeyError:
374 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700375
Alex Light4e358ab2016-06-16 14:47:10 -0700376 has_system_other = "SYSTEM_OTHER/" in input_zip.namelist()
377
Tao Bao2c15d9e2015-07-09 11:51:16 -0700378 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700379
Tao Bao2ed665a2015-04-01 11:21:55 -0700380 common.ZipClose(input_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700381 output_zip = zipfile.ZipFile(filename, "a",
Tao Bao9c84e502016-08-22 10:31:05 -0700382 compression=zipfile.ZIP_DEFLATED,
383 allowZip64=True)
Doug Zongker3c84f562014-07-31 11:06:30 -0700384
Tao Baodb45efa2015-10-27 19:25:18 -0700385 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
David Zeuthend995f4b2016-01-29 16:59:17 -0500386 system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true")
Tao Baodb45efa2015-10-27 19:25:18 -0700387
Doug Zongkerfc44a512014-08-26 13:10:25 -0700388 def banner(s):
389 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700390
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800391 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
392 boot_image = None
393 if os.path.exists(prebuilt_path):
David Zeuthend995f4b2016-01-29 16:59:17 -0500394 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800395 print "boot.img already exists in IMAGES/, no need to rebuild..."
396 if OPTIONS.rebuild_recovery:
397 boot_image = common.GetBootableImage(
398 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
399 else:
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400400 banner("boot")
401 boot_image = common.GetBootableImage(
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800402 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400403 if boot_image:
404 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700405
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800406 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700407 if has_recovery:
408 banner("recovery")
409 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
410 if os.path.exists(prebuilt_path):
411 print "recovery.img already exists in IMAGES/, no need to rebuild..."
412 if OPTIONS.rebuild_recovery:
413 recovery_image = common.GetBootableImage(
414 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
415 "RECOVERY")
416 else:
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800417 recovery_image = common.GetBootableImage(
418 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
Tao Baodb45efa2015-10-27 19:25:18 -0700419 if recovery_image:
420 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700421
Tao Baod42e97e2016-11-30 12:11:57 -0800422 banner("recovery (two-step image)")
423 # The special recovery.img for two-step package use.
424 recovery_two_step_image = common.GetBootableImage(
425 "IMAGES/recovery-two-step.img", "recovery-two-step.img",
426 OPTIONS.input_tmp, "RECOVERY", two_step_image=True)
427 if recovery_two_step_image:
428 recovery_two_step_image.AddToZip(output_zip)
429
Doug Zongkerfc44a512014-08-26 13:10:25 -0700430 banner("system")
David Zeuthend995f4b2016-01-29 16:59:17 -0500431 system_img_path = AddSystem(
432 output_zip, recovery_img=recovery_image, boot_img=boot_image)
Tianjie Xu737afb92016-07-11 11:42:53 -0700433 vendor_img_path = None
Doug Zongkerfc44a512014-08-26 13:10:25 -0700434 if has_vendor:
435 banner("vendor")
Tianjie Xu737afb92016-07-11 11:42:53 -0700436 vendor_img_path = AddVendor(output_zip)
Alex Light4e358ab2016-06-16 14:47:10 -0700437 if has_system_other:
438 banner("system_other")
439 AddSystemOther(output_zip)
Tianjie Xub48589a2016-08-03 19:21:52 -0700440 if not OPTIONS.is_signing:
441 banner("userdata")
442 AddUserdata(output_zip)
443 banner("cache")
444 AddCache(output_zip)
David Zeuthen25328622016-04-08 15:08:03 -0400445 if OPTIONS.info_dict.get("board_bpt_enable", None) == "true":
446 banner("partition-table")
447 AddPartitionTable(output_zip)
David Zeuthen2ce63ed2016-09-15 13:43:54 -0400448 if OPTIONS.info_dict.get("board_avb_enable", None) == "true":
449 banner("vbmeta")
450 boot_contents = boot_image.WriteToTemp()
451 AddVBMeta(output_zip, boot_contents.name, system_img_path)
Doug Zongker3c84f562014-07-31 11:06:30 -0700452
Wei Wang2e735ca2016-05-10 22:48:13 -0700453 # For devices using A/B update, copy over images from RADIO/ and/or
454 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
455 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700456 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800457 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
458 if os.path.exists(ab_partitions):
459 with open(ab_partitions, 'r') as f:
460 lines = f.readlines()
Tianjie Xucfa86222016-03-07 16:31:19 -0800461 # For devices using A/B update, generate care_map for system and vendor
462 # partitions (if present), then write this file to target_files package.
463 care_map_list = []
Tao Baoa0421cd2015-11-16 16:32:27 -0800464 for line in lines:
Tianjie Xucfa86222016-03-07 16:31:19 -0800465 if line.strip() == "system" and OPTIONS.info_dict.get(
466 "system_verity_block_device", None) is not None:
Tianjie Xu737afb92016-07-11 11:42:53 -0700467 assert os.path.exists(system_img_path)
468 care_map_list += GetCareMap("system", system_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800469 if line.strip() == "vendor" and OPTIONS.info_dict.get(
470 "vendor_verity_block_device", None) is not None:
Tianjie Xu737afb92016-07-11 11:42:53 -0700471 assert os.path.exists(vendor_img_path)
472 care_map_list += GetCareMap("vendor", vendor_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800473
Tao Baoa0421cd2015-11-16 16:32:27 -0800474 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700475 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
476 if os.path.exists(prebuilt_path):
477 print "%s already exists, no need to overwrite..." % (img_name,)
478 continue
479
Tao Baoa0421cd2015-11-16 16:32:27 -0800480 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700481 img_vendor_dir = os.path.join(
482 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800483 if os.path.exists(img_radio_path):
484 common.ZipWrite(output_zip, img_radio_path,
485 os.path.join("IMAGES", img_name))
Wei Wang2e735ca2016-05-10 22:48:13 -0700486 else:
487 for root, _, files in os.walk(img_vendor_dir):
488 if img_name in files:
489 common.ZipWrite(output_zip, os.path.join(root, img_name),
490 os.path.join("IMAGES", img_name))
491 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800492
493 # Zip spec says: All slashes MUST be forward slashes.
494 img_path = 'IMAGES/' + img_name
495 assert img_path in output_zip.namelist(), "cannot find " + img_name
496
Tianjie Xucfa86222016-03-07 16:31:19 -0800497 if care_map_list:
498 file_path = "META/care_map.txt"
499 common.ZipWriteStr(output_zip, file_path, '\n'.join(care_map_list))
500
Tao Bao2ed665a2015-04-01 11:21:55 -0700501 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700502
Doug Zongker3c84f562014-07-31 11:06:30 -0700503def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700504 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800505 if o in ("-a", "--add_missing"):
506 OPTIONS.add_missing = True
507 elif o in ("-r", "--rebuild_recovery",):
508 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700509 elif o == "--replace_verity_private_key":
510 OPTIONS.replace_verity_private_key = (True, a)
511 elif o == "--replace_verity_public_key":
512 OPTIONS.replace_verity_public_key = (True, a)
Tianjie Xub48589a2016-08-03 19:21:52 -0700513 elif o == "--is_signing":
514 OPTIONS.is_signing = True
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800515 else:
516 return False
517 return True
518
Dan Albert8b72aef2015-03-23 19:13:21 -0700519 args = common.ParseOptions(
520 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700521 extra_long_opts=["add_missing", "rebuild_recovery",
522 "replace_verity_public_key=",
523 "replace_verity_private_key=",
Tao Bao45810422016-10-17 16:20:12 -0700524 "is_signing"],
Dan Albert8b72aef2015-03-23 19:13:21 -0700525 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800526
Doug Zongker3c84f562014-07-31 11:06:30 -0700527
528 if len(args) != 1:
529 common.Usage(__doc__)
530 sys.exit(1)
531
532 AddImagesToTargetFiles(args[0])
533 print "done."
534
535if __name__ == '__main__':
536 try:
537 common.CloseInheritedPipes()
538 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700539 except common.ExternalError as e:
Doug Zongker3c84f562014-07-31 11:06:30 -0700540 print
541 print " ERROR: %s" % (e,)
542 print
543 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700544 finally:
545 common.Cleanup()