blob: 03bb0b106abe673958a66d36ba561134c5d953dd [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
42
43OPTIONS = common.OPTIONS
44
Michael Runge2e0d8fc2014-11-13 21:41:08 -080045OPTIONS.add_missing = False
46OPTIONS.rebuild_recovery = False
Baligh Uddin59f4ff12015-09-16 21:20:30 -070047OPTIONS.replace_verity_public_key = False
48OPTIONS.replace_verity_private_key = False
49OPTIONS.verity_signer_path = None
Doug Zongker3c84f562014-07-31 11:06:30 -070050
Michael Runge2e0d8fc2014-11-13 21:41:08 -080051def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070052 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -050053 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080054
55 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
56 if os.path.exists(prebuilt_path):
57 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
David Zeuthend995f4b2016-01-29 16:59:17 -050058 return prebuilt_path
Michael Runge2e0d8fc2014-11-13 21:41:08 -080059
60 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -070061 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
62 ofile.write(data)
63 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -080064
65 if OPTIONS.rebuild_recovery:
Dan Albert8b72aef2015-03-23 19:13:21 -070066 print "Building new recovery patch"
67 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
68 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -080069
Doug Zongkerfc44a512014-08-26 13:10:25 -070070 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
71 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
72 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -050073
74 # If requested, calculate and add dm-verity integrity hashes and
75 # metadata to system.img.
76 if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
77 bvbtool = os.getenv('BVBTOOL') or "bvbtool"
78 cmd = [bvbtool, "add_image_hashes", "--image", imgname]
79 args = OPTIONS.info_dict.get("board_bvb_add_image_hashes_args", None)
80 if args and args.strip():
81 cmd.extend(shlex.split(args))
82 p = common.Run(cmd, stdout=subprocess.PIPE)
83 p.communicate()
84 assert p.returncode == 0, "bvbtool add_image_hashes of %s image failed" % (
85 os.path.basename(OPTIONS.input_tmp),)
86
Dan Albert8e0178d2015-01-27 15:53:15 -080087 common.ZipWrite(output_zip, imgname, prefix + "system.img")
88 common.ZipWrite(output_zip, block_list, prefix + "system.map")
David Zeuthend995f4b2016-01-29 16:59:17 -050089 return imgname
Doug Zongkerfc44a512014-08-26 13:10:25 -070090
91
92def BuildSystem(input_dir, info_dict, block_list=None):
93 """Build the (sparse) system image and return the name of a temp
94 file containing it."""
95 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
96
97
Alex Light4e358ab2016-06-16 14:47:10 -070098def AddSystemOther(output_zip, prefix="IMAGES/"):
99 """Turn the contents of SYSTEM_OTHER into a system_other image
100 and store it in output_zip."""
101
102 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system_other.img")
103 if os.path.exists(prebuilt_path):
104 print "system_other.img already exists in %s, no need to rebuild..." % (prefix,)
105 return
106
107 imgname = BuildSystemOther(OPTIONS.input_tmp, OPTIONS.info_dict)
108 common.ZipWrite(output_zip, imgname, prefix + "system_other.img")
109
110def BuildSystemOther(input_dir, info_dict):
111 """Build the (sparse) system_other image and return the name of a temp
112 file containing it."""
113 return CreateImage(input_dir, info_dict, "system_other", block_list=None)
114
115
Doug Zongkerfc44a512014-08-26 13:10:25 -0700116def AddVendor(output_zip, prefix="IMAGES/"):
117 """Turn the contents of VENDOR into a vendor image and store in it
118 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800119
120 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
121 if os.path.exists(prebuilt_path):
122 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
123 return
124
Doug Zongkerfc44a512014-08-26 13:10:25 -0700125 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
126 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Dan Albert8b72aef2015-03-23 19:13:21 -0700127 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -0800128 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
129 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Doug Zongker3c84f562014-07-31 11:06:30 -0700130
131
Doug Zongkerfc44a512014-08-26 13:10:25 -0700132def BuildVendor(input_dir, info_dict, block_list=None):
133 """Build the (sparse) vendor image and return the name of a temp
134 file containing it."""
135 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
136
137
138def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700139 print "creating " + what + ".img..."
140
Doug Zongkerfc44a512014-08-26 13:10:25 -0700141 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700142
143 # The name of the directory it is making an image out of matters to
144 # mkyaffs2image. It wants "system" but we have a directory named
145 # "SYSTEM", so create a symlink.
146 try:
147 os.symlink(os.path.join(input_dir, what.upper()),
148 os.path.join(input_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700149 except OSError as e:
150 # bogus error on my mac version?
151 # File "./build/tools/releasetools/img_from_target_files"
152 # os.path.join(OPTIONS.input_tmp, "system"))
153 # OSError: [Errno 17] File exists
154 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700155 pass
156
157 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
158 fstab = info_dict["fstab"]
159 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700160 image_props["fs_type"] = fstab["/" + what].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700161
Tao Bao822f5842015-09-30 16:01:14 -0700162 # Use a fixed timestamp (01/01/2009) when packaging the image.
163 # Bug: 24377993
164 epoch = datetime.datetime.fromtimestamp(0)
165 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
166 image_props["timestamp"] = int(timestamp)
167
Doug Zongker3c84f562014-07-31 11:06:30 -0700168 if what == "system":
169 fs_config_prefix = ""
170 else:
171 fs_config_prefix = what + "_"
172
173 fs_config = os.path.join(
174 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700175 if not os.path.exists(fs_config):
176 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700177
Ying Wanga2292c92015-03-24 19:07:40 -0700178 # Override values loaded from info_dict.
179 if fs_config:
180 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700181 if block_list:
182 image_props["block_list"] = block_list
Ying Wanga2292c92015-03-24 19:07:40 -0700183
Doug Zongker3c84f562014-07-31 11:06:30 -0700184 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700185 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700186 assert succ, "build " + what + ".img image failed"
187
Doug Zongkerfc44a512014-08-26 13:10:25 -0700188 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700189
190
191def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700192 """Create a userdata image and store it in output_zip.
193
194 In most case we just create and store an empty userdata.img;
195 But the invoker can also request to create userdata.img with real
196 data from the target files, by setting "userdata_img_with_data=true"
197 in OPTIONS.info_dict.
198 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700199
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800200 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
201 if os.path.exists(prebuilt_path):
202 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
203 return
204
Elliott Hughes305b0882016-06-15 17:04:54 -0700205 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700206 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700207 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700208 return
209
210 print "creating userdata.img..."
211
Tao Bao822f5842015-09-30 16:01:14 -0700212 # Use a fixed timestamp (01/01/2009) when packaging the image.
213 # Bug: 24377993
214 epoch = datetime.datetime.fromtimestamp(0)
215 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
216 image_props["timestamp"] = int(timestamp)
217
Doug Zongker3c84f562014-07-31 11:06:30 -0700218 # The name of the directory it is making an image out of matters to
219 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700220 # empty dir named "data", or a symlink to the DATA dir,
221 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700222 temp_dir = tempfile.mkdtemp()
223 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700224 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
225 if empty:
226 # Create an empty dir.
227 os.mkdir(user_dir)
228 else:
229 # Symlink to the DATA dir.
230 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
231 user_dir)
232
Doug Zongker3c84f562014-07-31 11:06:30 -0700233 img = tempfile.NamedTemporaryFile()
234
235 fstab = OPTIONS.info_dict["fstab"]
236 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700237 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700238 succ = build_image.BuildImage(user_dir, image_props, img.name)
239 assert succ, "build userdata.img image failed"
240
241 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700242 common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700243 img.close()
Ying Wang2a048392015-06-25 13:56:53 -0700244 shutil.rmtree(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700245
246
David Zeuthen25328622016-04-08 15:08:03 -0400247def AddPartitionTable(output_zip, prefix="IMAGES/"):
248 """Create a partition table image and store it in output_zip."""
249
250 _, img_file_name = tempfile.mkstemp()
251 _, bpt_file_name = tempfile.mkstemp()
252
253 # use BPTTOOL from environ, or "bpttool" if empty or not set.
254 bpttool = os.getenv("BPTTOOL") or "bpttool"
255 cmd = [bpttool, "make_table", "--output_json", bpt_file_name,
256 "--output_gpt", img_file_name]
257 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
258 input_files = input_files_str.split(" ")
259 for i in input_files:
260 cmd.extend(["--input", i])
261 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
262 if disk_size:
263 cmd.extend(["--disk_size", disk_size])
264 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
265 if args:
266 cmd.extend(shlex.split(args))
267
268 p = common.Run(cmd, stdout=subprocess.PIPE)
269 p.communicate()
270 assert p.returncode == 0, "bpttool make_table failed"
271
272 common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img")
273 common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt")
274
275
Doug Zongker3c84f562014-07-31 11:06:30 -0700276def AddCache(output_zip, prefix="IMAGES/"):
277 """Create an empty cache image and store it in output_zip."""
278
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800279 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
280 if os.path.exists(prebuilt_path):
281 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
282 return
283
Tao Bao2c15d9e2015-07-09 11:51:16 -0700284 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700285 # The build system has to explicitly request for cache.img.
286 if "fs_type" not in image_props:
287 return
288
289 print "creating cache.img..."
290
Tao Bao822f5842015-09-30 16:01:14 -0700291 # Use a fixed timestamp (01/01/2009) when packaging the image.
292 # Bug: 24377993
293 epoch = datetime.datetime.fromtimestamp(0)
294 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
295 image_props["timestamp"] = int(timestamp)
296
Doug Zongker3c84f562014-07-31 11:06:30 -0700297 # The name of the directory it is making an image out of matters to
298 # mkyaffs2image. So we create a temp dir, and within it we create an
299 # empty dir named "cache", and build the image from that.
300 temp_dir = tempfile.mkdtemp()
301 user_dir = os.path.join(temp_dir, "cache")
302 os.mkdir(user_dir)
303 img = tempfile.NamedTemporaryFile()
304
305 fstab = OPTIONS.info_dict["fstab"]
306 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700307 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700308 succ = build_image.BuildImage(user_dir, image_props, img.name)
309 assert succ, "build cache.img image failed"
310
311 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700312 common.ZipWrite(output_zip, img.name, prefix + "cache.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700313 img.close()
314 os.rmdir(user_dir)
315 os.rmdir(temp_dir)
316
317
318def AddImagesToTargetFiles(filename):
319 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700320
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800321 if not OPTIONS.add_missing:
322 for n in input_zip.namelist():
323 if n.startswith("IMAGES/"):
324 print "target_files appears to already contain images."
325 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700326
Doug Zongker3c84f562014-07-31 11:06:30 -0700327 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700328 input_zip.getinfo("VENDOR/")
329 has_vendor = True
330 except KeyError:
331 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700332
Alex Light4e358ab2016-06-16 14:47:10 -0700333 has_system_other = "SYSTEM_OTHER/" in input_zip.namelist()
334
Tao Bao2c15d9e2015-07-09 11:51:16 -0700335 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700336
Tao Bao2ed665a2015-04-01 11:21:55 -0700337 common.ZipClose(input_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700338 output_zip = zipfile.ZipFile(filename, "a",
339 compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -0700340
Tao Baodb45efa2015-10-27 19:25:18 -0700341 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
David Zeuthend995f4b2016-01-29 16:59:17 -0500342 system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true")
343 board_bvb_enable = (OPTIONS.info_dict.get("board_bvb_enable", None) == "true")
344
345 # Brillo Verified Boot is incompatible with certain
346 # configurations. Explicitly check for these.
347 if board_bvb_enable:
348 assert not has_recovery, "has_recovery incompatible with bvb"
349 assert not system_root_image, "system_root_image incompatible with bvb"
350 assert not OPTIONS.rebuild_recovery, "rebuild_recovery incompatible with bvb"
351 assert not has_vendor, "VENDOR images currently incompatible with bvb"
Tao Baodb45efa2015-10-27 19:25:18 -0700352
Doug Zongkerfc44a512014-08-26 13:10:25 -0700353 def banner(s):
354 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700355
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800356 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
357 boot_image = None
358 if os.path.exists(prebuilt_path):
David Zeuthend995f4b2016-01-29 16:59:17 -0500359 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800360 print "boot.img already exists in IMAGES/, no need to rebuild..."
361 if OPTIONS.rebuild_recovery:
362 boot_image = common.GetBootableImage(
363 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
364 else:
David Zeuthend995f4b2016-01-29 16:59:17 -0500365 if board_bvb_enable:
366 # With Brillo Verified Boot, we need to build system.img before
367 # boot.img since the latter includes the dm-verity root hash and
368 # salt for the former.
369 pass
370 else:
371 banner("boot")
372 boot_image = common.GetBootableImage(
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800373 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
David Zeuthend995f4b2016-01-29 16:59:17 -0500374 if boot_image:
375 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700376
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800377 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700378 if has_recovery:
379 banner("recovery")
380 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
381 if os.path.exists(prebuilt_path):
382 print "recovery.img already exists in IMAGES/, no need to rebuild..."
383 if OPTIONS.rebuild_recovery:
384 recovery_image = common.GetBootableImage(
385 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
386 "RECOVERY")
387 else:
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800388 recovery_image = common.GetBootableImage(
389 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
Tao Baodb45efa2015-10-27 19:25:18 -0700390 if recovery_image:
391 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700392
Doug Zongkerfc44a512014-08-26 13:10:25 -0700393 banner("system")
David Zeuthend995f4b2016-01-29 16:59:17 -0500394 system_img_path = AddSystem(
395 output_zip, recovery_img=recovery_image, boot_img=boot_image)
396 if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
397 # If we're using Brillo Verified Boot, we can now build boot.img
398 # given that we have system.img.
399 banner("boot")
400 boot_image = common.GetBootableImage(
401 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT",
402 system_img_path=system_img_path)
403 if boot_image:
404 boot_image.AddToZip(output_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700405 if has_vendor:
406 banner("vendor")
407 AddVendor(output_zip)
Alex Light4e358ab2016-06-16 14:47:10 -0700408 if has_system_other:
409 banner("system_other")
410 AddSystemOther(output_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700411 banner("userdata")
412 AddUserdata(output_zip)
413 banner("cache")
414 AddCache(output_zip)
David Zeuthen25328622016-04-08 15:08:03 -0400415 if OPTIONS.info_dict.get("board_bpt_enable", None) == "true":
416 banner("partition-table")
417 AddPartitionTable(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700418
Wei Wang2e735ca2016-05-10 22:48:13 -0700419 # For devices using A/B update, copy over images from RADIO/ and/or
420 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
421 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700422 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800423 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
424 if os.path.exists(ab_partitions):
425 with open(ab_partitions, 'r') as f:
426 lines = f.readlines()
427 for line in lines:
428 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700429 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
430 if os.path.exists(prebuilt_path):
431 print "%s already exists, no need to overwrite..." % (img_name,)
432 continue
433
Tao Baoa0421cd2015-11-16 16:32:27 -0800434 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700435 img_vendor_dir = os.path.join(
436 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800437 if os.path.exists(img_radio_path):
438 common.ZipWrite(output_zip, img_radio_path,
439 os.path.join("IMAGES", img_name))
Wei Wang2e735ca2016-05-10 22:48:13 -0700440 else:
441 for root, _, files in os.walk(img_vendor_dir):
442 if img_name in files:
443 common.ZipWrite(output_zip, os.path.join(root, img_name),
444 os.path.join("IMAGES", img_name))
445 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800446
447 # Zip spec says: All slashes MUST be forward slashes.
448 img_path = 'IMAGES/' + img_name
449 assert img_path in output_zip.namelist(), "cannot find " + img_name
450
Tao Bao2ed665a2015-04-01 11:21:55 -0700451 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700452
Doug Zongker3c84f562014-07-31 11:06:30 -0700453def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700454 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800455 if o in ("-a", "--add_missing"):
456 OPTIONS.add_missing = True
457 elif o in ("-r", "--rebuild_recovery",):
458 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700459 elif o == "--replace_verity_private_key":
460 OPTIONS.replace_verity_private_key = (True, a)
461 elif o == "--replace_verity_public_key":
462 OPTIONS.replace_verity_public_key = (True, a)
463 elif o == "--verity_signer_path":
464 OPTIONS.verity_signer_path = a
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800465 else:
466 return False
467 return True
468
Dan Albert8b72aef2015-03-23 19:13:21 -0700469 args = common.ParseOptions(
470 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700471 extra_long_opts=["add_missing", "rebuild_recovery",
472 "replace_verity_public_key=",
473 "replace_verity_private_key=",
474 "verity_signer_path="],
Dan Albert8b72aef2015-03-23 19:13:21 -0700475 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800476
Doug Zongker3c84f562014-07-31 11:06:30 -0700477
478 if len(args) != 1:
479 common.Usage(__doc__)
480 sys.exit(1)
481
482 AddImagesToTargetFiles(args[0])
483 print "done."
484
485if __name__ == '__main__':
486 try:
487 common.CloseInheritedPipes()
488 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700489 except common.ExternalError as e:
Doug Zongker3c84f562014-07-31 11:06:30 -0700490 print
491 print " ERROR: %s" % (e,)
492 print
493 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700494 finally:
495 common.Cleanup()