blob: aeb73d410e7b0d66b510975981bc19941fd60903 [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
22Usage: add_img_to_target_files target_files
23"""
24
25import sys
26
27if sys.hexversion < 0x02070000:
28 print >> sys.stderr, "Python 2.7 or newer is required."
29 sys.exit(1)
30
Tao Bao822f5842015-09-30 16:01:14 -070031import datetime
Doug Zongker3c84f562014-07-31 11:06:30 -070032import errno
33import os
David Zeuthend995f4b2016-01-29 16:59:17 -050034import shlex
Ying Wang2a048392015-06-25 13:56:53 -070035import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050036import subprocess
Doug Zongker3c84f562014-07-31 11:06:30 -070037import tempfile
38import zipfile
39
Doug Zongker3c84f562014-07-31 11:06:30 -070040import build_image
41import common
Tianjie Xucfa86222016-03-07 16:31:19 -080042import sparse_img
Doug Zongker3c84f562014-07-31 11:06:30 -070043
44OPTIONS = common.OPTIONS
45
Michael Runge2e0d8fc2014-11-13 21:41:08 -080046OPTIONS.add_missing = False
47OPTIONS.rebuild_recovery = False
Baligh Uddin59f4ff12015-09-16 21:20:30 -070048OPTIONS.replace_verity_public_key = False
49OPTIONS.replace_verity_private_key = False
50OPTIONS.verity_signer_path = None
Doug Zongker3c84f562014-07-31 11:06:30 -070051
Tianjie Xucfa86222016-03-07 16:31:19 -080052def GetCareMap(which, imgname):
53 """Generate care_map of system (or vendor) partition"""
54
55 assert which in ("system", "vendor")
56 _, blk_device = common.GetTypeAndDevice("/" + which, OPTIONS.info_dict)
57
58 simg = sparse_img.SparseImage(imgname)
59 care_map_list = []
60 care_map_list.append(blk_device)
61 care_map_list.append(simg.care_map.to_string_raw())
62 return care_map_list
63
64
Michael Runge2e0d8fc2014-11-13 21:41:08 -080065def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070066 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -050067 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080068
69 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
70 if os.path.exists(prebuilt_path):
71 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
David Zeuthend995f4b2016-01-29 16:59:17 -050072 return prebuilt_path
Michael Runge2e0d8fc2014-11-13 21:41:08 -080073
74 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -070075 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
76 ofile.write(data)
77 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -080078
79 if OPTIONS.rebuild_recovery:
Dan Albert8b72aef2015-03-23 19:13:21 -070080 print "Building new recovery patch"
81 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
82 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -080083
Doug Zongkerfc44a512014-08-26 13:10:25 -070084 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
85 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
86 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -050087
88 # If requested, calculate and add dm-verity integrity hashes and
89 # metadata to system.img.
90 if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
91 bvbtool = os.getenv('BVBTOOL') or "bvbtool"
92 cmd = [bvbtool, "add_image_hashes", "--image", imgname]
93 args = OPTIONS.info_dict.get("board_bvb_add_image_hashes_args", None)
94 if args and args.strip():
95 cmd.extend(shlex.split(args))
96 p = common.Run(cmd, stdout=subprocess.PIPE)
97 p.communicate()
98 assert p.returncode == 0, "bvbtool add_image_hashes of %s image failed" % (
99 os.path.basename(OPTIONS.input_tmp),)
100
Dan Albert8e0178d2015-01-27 15:53:15 -0800101 common.ZipWrite(output_zip, imgname, prefix + "system.img")
102 common.ZipWrite(output_zip, block_list, prefix + "system.map")
David Zeuthend995f4b2016-01-29 16:59:17 -0500103 return imgname
Doug Zongkerfc44a512014-08-26 13:10:25 -0700104
105
106def BuildSystem(input_dir, info_dict, block_list=None):
107 """Build the (sparse) system image and return the name of a temp
108 file containing it."""
109 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
110
111
Alex Light4e358ab2016-06-16 14:47:10 -0700112def AddSystemOther(output_zip, prefix="IMAGES/"):
113 """Turn the contents of SYSTEM_OTHER into a system_other image
114 and store it in output_zip."""
115
116 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system_other.img")
117 if os.path.exists(prebuilt_path):
118 print "system_other.img already exists in %s, no need to rebuild..." % (prefix,)
119 return
120
121 imgname = BuildSystemOther(OPTIONS.input_tmp, OPTIONS.info_dict)
122 common.ZipWrite(output_zip, imgname, prefix + "system_other.img")
123
124def BuildSystemOther(input_dir, info_dict):
125 """Build the (sparse) system_other image and return the name of a temp
126 file containing it."""
127 return CreateImage(input_dir, info_dict, "system_other", block_list=None)
128
129
Doug Zongkerfc44a512014-08-26 13:10:25 -0700130def AddVendor(output_zip, prefix="IMAGES/"):
131 """Turn the contents of VENDOR into a vendor image and store in it
132 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800133
134 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
135 if os.path.exists(prebuilt_path):
136 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
Tianjie Xucfa86222016-03-07 16:31:19 -0800137 return prebuilt_path
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800138
Doug Zongkerfc44a512014-08-26 13:10:25 -0700139 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
140 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Dan Albert8b72aef2015-03-23 19:13:21 -0700141 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -0800142 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
143 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Tianjie Xucfa86222016-03-07 16:31:19 -0800144 return imgname
Doug Zongker3c84f562014-07-31 11:06:30 -0700145
146
Doug Zongkerfc44a512014-08-26 13:10:25 -0700147def BuildVendor(input_dir, info_dict, block_list=None):
148 """Build the (sparse) vendor image and return the name of a temp
149 file containing it."""
150 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
151
152
153def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700154 print "creating " + what + ".img..."
155
Doug Zongkerfc44a512014-08-26 13:10:25 -0700156 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700157
158 # The name of the directory it is making an image out of matters to
159 # mkyaffs2image. It wants "system" but we have a directory named
160 # "SYSTEM", so create a symlink.
161 try:
162 os.symlink(os.path.join(input_dir, what.upper()),
163 os.path.join(input_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700164 except OSError as e:
165 # bogus error on my mac version?
166 # File "./build/tools/releasetools/img_from_target_files"
167 # os.path.join(OPTIONS.input_tmp, "system"))
168 # OSError: [Errno 17] File exists
169 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700170 pass
171
172 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
173 fstab = info_dict["fstab"]
Tianjie Xucfa86222016-03-07 16:31:19 -0800174 mount_point = "/" + what
175 if fstab and mount_point in fstab:
176 image_props["fs_type"] = fstab[mount_point].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700177
Tao Bao822f5842015-09-30 16:01:14 -0700178 # Use a fixed timestamp (01/01/2009) when packaging the image.
179 # Bug: 24377993
180 epoch = datetime.datetime.fromtimestamp(0)
181 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
182 image_props["timestamp"] = int(timestamp)
183
Doug Zongker3c84f562014-07-31 11:06:30 -0700184 if what == "system":
185 fs_config_prefix = ""
186 else:
187 fs_config_prefix = what + "_"
188
189 fs_config = os.path.join(
190 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700191 if not os.path.exists(fs_config):
192 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700193
Ying Wanga2292c92015-03-24 19:07:40 -0700194 # Override values loaded from info_dict.
195 if fs_config:
196 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700197 if block_list:
198 image_props["block_list"] = block_list
Ying Wanga2292c92015-03-24 19:07:40 -0700199
Doug Zongker3c84f562014-07-31 11:06:30 -0700200 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700201 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700202 assert succ, "build " + what + ".img image failed"
203
Doug Zongkerfc44a512014-08-26 13:10:25 -0700204 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700205
206
207def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700208 """Create a userdata image and store it in output_zip.
209
210 In most case we just create and store an empty userdata.img;
211 But the invoker can also request to create userdata.img with real
212 data from the target files, by setting "userdata_img_with_data=true"
213 in OPTIONS.info_dict.
214 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700215
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800216 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
217 if os.path.exists(prebuilt_path):
218 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
219 return
220
Elliott Hughes305b0882016-06-15 17:04:54 -0700221 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700222 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700223 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700224 return
225
226 print "creating userdata.img..."
227
Tao Bao822f5842015-09-30 16:01:14 -0700228 # Use a fixed timestamp (01/01/2009) when packaging the image.
229 # Bug: 24377993
230 epoch = datetime.datetime.fromtimestamp(0)
231 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
232 image_props["timestamp"] = int(timestamp)
233
Doug Zongker3c84f562014-07-31 11:06:30 -0700234 # The name of the directory it is making an image out of matters to
235 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700236 # empty dir named "data", or a symlink to the DATA dir,
237 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700238 temp_dir = tempfile.mkdtemp()
239 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700240 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
241 if empty:
242 # Create an empty dir.
243 os.mkdir(user_dir)
244 else:
245 # Symlink to the DATA dir.
246 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
247 user_dir)
248
Doug Zongker3c84f562014-07-31 11:06:30 -0700249 img = tempfile.NamedTemporaryFile()
250
251 fstab = OPTIONS.info_dict["fstab"]
252 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700253 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700254 succ = build_image.BuildImage(user_dir, image_props, img.name)
255 assert succ, "build userdata.img image failed"
256
257 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700258 common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700259 img.close()
Ying Wang2a048392015-06-25 13:56:53 -0700260 shutil.rmtree(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700261
262
David Zeuthen25328622016-04-08 15:08:03 -0400263def AddPartitionTable(output_zip, prefix="IMAGES/"):
264 """Create a partition table image and store it in output_zip."""
265
266 _, img_file_name = tempfile.mkstemp()
267 _, bpt_file_name = tempfile.mkstemp()
268
269 # use BPTTOOL from environ, or "bpttool" if empty or not set.
270 bpttool = os.getenv("BPTTOOL") or "bpttool"
271 cmd = [bpttool, "make_table", "--output_json", bpt_file_name,
272 "--output_gpt", img_file_name]
273 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
274 input_files = input_files_str.split(" ")
275 for i in input_files:
276 cmd.extend(["--input", i])
277 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
278 if disk_size:
279 cmd.extend(["--disk_size", disk_size])
280 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
281 if args:
282 cmd.extend(shlex.split(args))
283
284 p = common.Run(cmd, stdout=subprocess.PIPE)
285 p.communicate()
286 assert p.returncode == 0, "bpttool make_table failed"
287
288 common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img")
289 common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt")
290
291
Doug Zongker3c84f562014-07-31 11:06:30 -0700292def AddCache(output_zip, prefix="IMAGES/"):
293 """Create an empty cache image and store it in output_zip."""
294
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800295 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
296 if os.path.exists(prebuilt_path):
297 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
298 return
299
Tao Bao2c15d9e2015-07-09 11:51:16 -0700300 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700301 # The build system has to explicitly request for cache.img.
302 if "fs_type" not in image_props:
303 return
304
305 print "creating cache.img..."
306
Tao Bao822f5842015-09-30 16:01:14 -0700307 # Use a fixed timestamp (01/01/2009) when packaging the image.
308 # Bug: 24377993
309 epoch = datetime.datetime.fromtimestamp(0)
310 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
311 image_props["timestamp"] = int(timestamp)
312
Doug Zongker3c84f562014-07-31 11:06:30 -0700313 # The name of the directory it is making an image out of matters to
314 # mkyaffs2image. So we create a temp dir, and within it we create an
315 # empty dir named "cache", and build the image from that.
316 temp_dir = tempfile.mkdtemp()
317 user_dir = os.path.join(temp_dir, "cache")
318 os.mkdir(user_dir)
319 img = tempfile.NamedTemporaryFile()
320
321 fstab = OPTIONS.info_dict["fstab"]
322 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700323 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700324 succ = build_image.BuildImage(user_dir, image_props, img.name)
325 assert succ, "build cache.img image failed"
326
327 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700328 common.ZipWrite(output_zip, img.name, prefix + "cache.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700329 img.close()
330 os.rmdir(user_dir)
331 os.rmdir(temp_dir)
332
333
334def AddImagesToTargetFiles(filename):
335 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700336
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800337 if not OPTIONS.add_missing:
338 for n in input_zip.namelist():
339 if n.startswith("IMAGES/"):
340 print "target_files appears to already contain images."
341 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700342
Doug Zongker3c84f562014-07-31 11:06:30 -0700343 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700344 input_zip.getinfo("VENDOR/")
345 has_vendor = True
346 except KeyError:
347 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700348
Alex Light4e358ab2016-06-16 14:47:10 -0700349 has_system_other = "SYSTEM_OTHER/" in input_zip.namelist()
350
Tao Bao2c15d9e2015-07-09 11:51:16 -0700351 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700352
Tao Bao2ed665a2015-04-01 11:21:55 -0700353 common.ZipClose(input_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700354 output_zip = zipfile.ZipFile(filename, "a",
355 compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -0700356
Tao Baodb45efa2015-10-27 19:25:18 -0700357 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
David Zeuthend995f4b2016-01-29 16:59:17 -0500358 system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true")
359 board_bvb_enable = (OPTIONS.info_dict.get("board_bvb_enable", None) == "true")
360
361 # Brillo Verified Boot is incompatible with certain
362 # configurations. Explicitly check for these.
363 if board_bvb_enable:
364 assert not has_recovery, "has_recovery incompatible with bvb"
365 assert not system_root_image, "system_root_image incompatible with bvb"
366 assert not OPTIONS.rebuild_recovery, "rebuild_recovery incompatible with bvb"
367 assert not has_vendor, "VENDOR images currently incompatible with bvb"
Tao Baodb45efa2015-10-27 19:25:18 -0700368
Doug Zongkerfc44a512014-08-26 13:10:25 -0700369 def banner(s):
370 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700371
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800372 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
373 boot_image = None
374 if os.path.exists(prebuilt_path):
David Zeuthend995f4b2016-01-29 16:59:17 -0500375 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800376 print "boot.img already exists in IMAGES/, no need to rebuild..."
377 if OPTIONS.rebuild_recovery:
378 boot_image = common.GetBootableImage(
379 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
380 else:
David Zeuthend995f4b2016-01-29 16:59:17 -0500381 if board_bvb_enable:
382 # With Brillo Verified Boot, we need to build system.img before
383 # boot.img since the latter includes the dm-verity root hash and
384 # salt for the former.
385 pass
386 else:
387 banner("boot")
388 boot_image = common.GetBootableImage(
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800389 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
David Zeuthend995f4b2016-01-29 16:59:17 -0500390 if boot_image:
391 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700392
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800393 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700394 if has_recovery:
395 banner("recovery")
396 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
397 if os.path.exists(prebuilt_path):
398 print "recovery.img already exists in IMAGES/, no need to rebuild..."
399 if OPTIONS.rebuild_recovery:
400 recovery_image = common.GetBootableImage(
401 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
402 "RECOVERY")
403 else:
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800404 recovery_image = common.GetBootableImage(
405 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
Tao Baodb45efa2015-10-27 19:25:18 -0700406 if recovery_image:
407 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700408
Doug Zongkerfc44a512014-08-26 13:10:25 -0700409 banner("system")
David Zeuthend995f4b2016-01-29 16:59:17 -0500410 system_img_path = AddSystem(
411 output_zip, recovery_img=recovery_image, boot_img=boot_image)
412 if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
413 # If we're using Brillo Verified Boot, we can now build boot.img
414 # given that we have system.img.
415 banner("boot")
416 boot_image = common.GetBootableImage(
417 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT",
418 system_img_path=system_img_path)
419 if boot_image:
420 boot_image.AddToZip(output_zip)
Tianjie Xu737afb92016-07-11 11:42:53 -0700421 vendor_img_path = None
Doug Zongkerfc44a512014-08-26 13:10:25 -0700422 if has_vendor:
423 banner("vendor")
Tianjie Xu737afb92016-07-11 11:42:53 -0700424 vendor_img_path = AddVendor(output_zip)
Alex Light4e358ab2016-06-16 14:47:10 -0700425 if has_system_other:
426 banner("system_other")
427 AddSystemOther(output_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700428 banner("userdata")
429 AddUserdata(output_zip)
430 banner("cache")
431 AddCache(output_zip)
David Zeuthen25328622016-04-08 15:08:03 -0400432 if OPTIONS.info_dict.get("board_bpt_enable", None) == "true":
433 banner("partition-table")
434 AddPartitionTable(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700435
Wei Wang2e735ca2016-05-10 22:48:13 -0700436 # For devices using A/B update, copy over images from RADIO/ and/or
437 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
438 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700439 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800440 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
441 if os.path.exists(ab_partitions):
442 with open(ab_partitions, 'r') as f:
443 lines = f.readlines()
Tianjie Xucfa86222016-03-07 16:31:19 -0800444 # For devices using A/B update, generate care_map for system and vendor
445 # partitions (if present), then write this file to target_files package.
446 care_map_list = []
Tao Baoa0421cd2015-11-16 16:32:27 -0800447 for line in lines:
Tianjie Xucfa86222016-03-07 16:31:19 -0800448 if line.strip() == "system" and OPTIONS.info_dict.get(
449 "system_verity_block_device", None) is not None:
Tianjie Xu737afb92016-07-11 11:42:53 -0700450 assert os.path.exists(system_img_path)
451 care_map_list += GetCareMap("system", system_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800452 if line.strip() == "vendor" and OPTIONS.info_dict.get(
453 "vendor_verity_block_device", None) is not None:
Tianjie Xu737afb92016-07-11 11:42:53 -0700454 assert os.path.exists(vendor_img_path)
455 care_map_list += GetCareMap("vendor", vendor_img_path)
Tianjie Xucfa86222016-03-07 16:31:19 -0800456
Tao Baoa0421cd2015-11-16 16:32:27 -0800457 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700458 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
459 if os.path.exists(prebuilt_path):
460 print "%s already exists, no need to overwrite..." % (img_name,)
461 continue
462
Tao Baoa0421cd2015-11-16 16:32:27 -0800463 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700464 img_vendor_dir = os.path.join(
465 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800466 if os.path.exists(img_radio_path):
467 common.ZipWrite(output_zip, img_radio_path,
468 os.path.join("IMAGES", img_name))
Wei Wang2e735ca2016-05-10 22:48:13 -0700469 else:
470 for root, _, files in os.walk(img_vendor_dir):
471 if img_name in files:
472 common.ZipWrite(output_zip, os.path.join(root, img_name),
473 os.path.join("IMAGES", img_name))
474 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800475
476 # Zip spec says: All slashes MUST be forward slashes.
477 img_path = 'IMAGES/' + img_name
478 assert img_path in output_zip.namelist(), "cannot find " + img_name
479
Tianjie Xucfa86222016-03-07 16:31:19 -0800480 if care_map_list:
481 file_path = "META/care_map.txt"
482 common.ZipWriteStr(output_zip, file_path, '\n'.join(care_map_list))
483
Tao Bao2ed665a2015-04-01 11:21:55 -0700484 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700485
Doug Zongker3c84f562014-07-31 11:06:30 -0700486def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700487 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800488 if o in ("-a", "--add_missing"):
489 OPTIONS.add_missing = True
490 elif o in ("-r", "--rebuild_recovery",):
491 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700492 elif o == "--replace_verity_private_key":
493 OPTIONS.replace_verity_private_key = (True, a)
494 elif o == "--replace_verity_public_key":
495 OPTIONS.replace_verity_public_key = (True, a)
496 elif o == "--verity_signer_path":
497 OPTIONS.verity_signer_path = a
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800498 else:
499 return False
500 return True
501
Dan Albert8b72aef2015-03-23 19:13:21 -0700502 args = common.ParseOptions(
503 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700504 extra_long_opts=["add_missing", "rebuild_recovery",
505 "replace_verity_public_key=",
506 "replace_verity_private_key=",
507 "verity_signer_path="],
Dan Albert8b72aef2015-03-23 19:13:21 -0700508 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800509
Doug Zongker3c84f562014-07-31 11:06:30 -0700510
511 if len(args) != 1:
512 common.Usage(__doc__)
513 sys.exit(1)
514
515 AddImagesToTargetFiles(args[0])
516 print "done."
517
518if __name__ == '__main__':
519 try:
520 common.CloseInheritedPipes()
521 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700522 except common.ExternalError as e:
Doug Zongker3c84f562014-07-31 11:06:30 -0700523 print
524 print " ERROR: %s" % (e,)
525 print
526 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700527 finally:
528 common.Cleanup()