blob: 5369c5b6b4e7554d226c782f1a49417979d6cabd [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.
44
45 --verity_signer_path
46 Specify the signer path to build verity metadata.
Doug Zongker3c84f562014-07-31 11:06:30 -070047"""
48
49import sys
50
51if sys.hexversion < 0x02070000:
52 print >> sys.stderr, "Python 2.7 or newer is required."
53 sys.exit(1)
54
Tao Bao822f5842015-09-30 16:01:14 -070055import datetime
Doug Zongker3c84f562014-07-31 11:06:30 -070056import errno
57import os
David Zeuthend995f4b2016-01-29 16:59:17 -050058import shlex
Ying Wang2a048392015-06-25 13:56:53 -070059import shutil
David Zeuthend995f4b2016-01-29 16:59:17 -050060import subprocess
Doug Zongker3c84f562014-07-31 11:06:30 -070061import tempfile
62import zipfile
63
Doug Zongker3c84f562014-07-31 11:06:30 -070064import build_image
65import common
66
67OPTIONS = common.OPTIONS
68
Michael Runge2e0d8fc2014-11-13 21:41:08 -080069OPTIONS.add_missing = False
70OPTIONS.rebuild_recovery = False
Baligh Uddin59f4ff12015-09-16 21:20:30 -070071OPTIONS.replace_verity_public_key = False
72OPTIONS.replace_verity_private_key = False
Tianjie Xub48589a2016-08-03 19:21:52 -070073OPTIONS.is_signing = False
Baligh Uddin59f4ff12015-09-16 21:20:30 -070074OPTIONS.verity_signer_path = None
Doug Zongker3c84f562014-07-31 11:06:30 -070075
Michael Runge2e0d8fc2014-11-13 21:41:08 -080076def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070077 """Turn the contents of SYSTEM into a system image and store it in
David Zeuthend995f4b2016-01-29 16:59:17 -050078 output_zip. Returns the name of the system image file."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080079
80 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
81 if os.path.exists(prebuilt_path):
82 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
David Zeuthend995f4b2016-01-29 16:59:17 -050083 return prebuilt_path
Michael Runge2e0d8fc2014-11-13 21:41:08 -080084
85 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -070086 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
87 ofile.write(data)
88 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -080089
90 if OPTIONS.rebuild_recovery:
Dan Albert8b72aef2015-03-23 19:13:21 -070091 print "Building new recovery patch"
92 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
93 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -080094
Doug Zongkerfc44a512014-08-26 13:10:25 -070095 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
96 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
97 block_list=block_list)
David Zeuthend995f4b2016-01-29 16:59:17 -050098
99 # If requested, calculate and add dm-verity integrity hashes and
100 # metadata to system.img.
101 if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
102 bvbtool = os.getenv('BVBTOOL') or "bvbtool"
103 cmd = [bvbtool, "add_image_hashes", "--image", imgname]
104 args = OPTIONS.info_dict.get("board_bvb_add_image_hashes_args", None)
105 if args and args.strip():
106 cmd.extend(shlex.split(args))
107 p = common.Run(cmd, stdout=subprocess.PIPE)
108 p.communicate()
109 assert p.returncode == 0, "bvbtool add_image_hashes of %s image failed" % (
110 os.path.basename(OPTIONS.input_tmp),)
111
Dan Albert8e0178d2015-01-27 15:53:15 -0800112 common.ZipWrite(output_zip, imgname, prefix + "system.img")
113 common.ZipWrite(output_zip, block_list, prefix + "system.map")
David Zeuthend995f4b2016-01-29 16:59:17 -0500114 return imgname
Doug Zongkerfc44a512014-08-26 13:10:25 -0700115
116
117def BuildSystem(input_dir, info_dict, block_list=None):
118 """Build the (sparse) system image and return the name of a temp
119 file containing it."""
120 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
121
122
123def AddVendor(output_zip, prefix="IMAGES/"):
124 """Turn the contents of VENDOR into a vendor image and store in it
125 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800126
127 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
128 if os.path.exists(prebuilt_path):
129 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
130 return
131
Doug Zongkerfc44a512014-08-26 13:10:25 -0700132 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
133 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Dan Albert8b72aef2015-03-23 19:13:21 -0700134 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -0800135 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
136 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Doug Zongker3c84f562014-07-31 11:06:30 -0700137
138
Doug Zongkerfc44a512014-08-26 13:10:25 -0700139def BuildVendor(input_dir, info_dict, block_list=None):
140 """Build the (sparse) vendor image and return the name of a temp
141 file containing it."""
142 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
143
144
145def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700146 print "creating " + what + ".img..."
147
Doug Zongkerfc44a512014-08-26 13:10:25 -0700148 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700149
150 # The name of the directory it is making an image out of matters to
151 # mkyaffs2image. It wants "system" but we have a directory named
152 # "SYSTEM", so create a symlink.
153 try:
154 os.symlink(os.path.join(input_dir, what.upper()),
155 os.path.join(input_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700156 except OSError as e:
157 # bogus error on my mac version?
158 # File "./build/tools/releasetools/img_from_target_files"
159 # os.path.join(OPTIONS.input_tmp, "system"))
160 # OSError: [Errno 17] File exists
161 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700162 pass
163
164 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
165 fstab = info_dict["fstab"]
166 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700167 image_props["fs_type"] = fstab["/" + what].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700168
Tao Bao822f5842015-09-30 16:01:14 -0700169 # Use a fixed timestamp (01/01/2009) when packaging the image.
170 # Bug: 24377993
171 epoch = datetime.datetime.fromtimestamp(0)
172 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
173 image_props["timestamp"] = int(timestamp)
174
Doug Zongker3c84f562014-07-31 11:06:30 -0700175 if what == "system":
176 fs_config_prefix = ""
177 else:
178 fs_config_prefix = what + "_"
179
180 fs_config = os.path.join(
181 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700182 if not os.path.exists(fs_config):
183 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700184
Ying Wanga2292c92015-03-24 19:07:40 -0700185 # Override values loaded from info_dict.
186 if fs_config:
187 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700188 if block_list:
189 image_props["block_list"] = block_list
Ying Wanga2292c92015-03-24 19:07:40 -0700190
Doug Zongker3c84f562014-07-31 11:06:30 -0700191 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700192 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700193 assert succ, "build " + what + ".img image failed"
194
Doug Zongkerfc44a512014-08-26 13:10:25 -0700195 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700196
197
198def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700199 """Create a userdata image and store it in output_zip.
200
201 In most case we just create and store an empty userdata.img;
202 But the invoker can also request to create userdata.img with real
203 data from the target files, by setting "userdata_img_with_data=true"
204 in OPTIONS.info_dict.
205 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700206
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800207 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
208 if os.path.exists(prebuilt_path):
209 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
210 return
211
Elliott Hughes305b0882016-06-15 17:04:54 -0700212 # Skip userdata.img if no size.
Tao Bao2c15d9e2015-07-09 11:51:16 -0700213 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Elliott Hughes305b0882016-06-15 17:04:54 -0700214 if not image_props.get("partition_size"):
Doug Zongker3c84f562014-07-31 11:06:30 -0700215 return
216
217 print "creating userdata.img..."
218
Tao Bao822f5842015-09-30 16:01:14 -0700219 # Use a fixed timestamp (01/01/2009) when packaging the image.
220 # Bug: 24377993
221 epoch = datetime.datetime.fromtimestamp(0)
222 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
223 image_props["timestamp"] = int(timestamp)
224
Doug Zongker3c84f562014-07-31 11:06:30 -0700225 # The name of the directory it is making an image out of matters to
226 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700227 # empty dir named "data", or a symlink to the DATA dir,
228 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700229 temp_dir = tempfile.mkdtemp()
230 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700231 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
232 if empty:
233 # Create an empty dir.
234 os.mkdir(user_dir)
235 else:
236 # Symlink to the DATA dir.
237 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
238 user_dir)
239
Doug Zongker3c84f562014-07-31 11:06:30 -0700240 img = tempfile.NamedTemporaryFile()
241
242 fstab = OPTIONS.info_dict["fstab"]
243 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700244 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700245 succ = build_image.BuildImage(user_dir, image_props, img.name)
246 assert succ, "build userdata.img image failed"
247
248 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700249 common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700250 img.close()
Ying Wang2a048392015-06-25 13:56:53 -0700251 shutil.rmtree(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700252
253
David Zeuthen25328622016-04-08 15:08:03 -0400254def AddPartitionTable(output_zip, prefix="IMAGES/"):
255 """Create a partition table image and store it in output_zip."""
256
257 _, img_file_name = tempfile.mkstemp()
258 _, bpt_file_name = tempfile.mkstemp()
259
260 # use BPTTOOL from environ, or "bpttool" if empty or not set.
261 bpttool = os.getenv("BPTTOOL") or "bpttool"
262 cmd = [bpttool, "make_table", "--output_json", bpt_file_name,
263 "--output_gpt", img_file_name]
264 input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
265 input_files = input_files_str.split(" ")
266 for i in input_files:
267 cmd.extend(["--input", i])
268 disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
269 if disk_size:
270 cmd.extend(["--disk_size", disk_size])
271 args = OPTIONS.info_dict.get("board_bpt_make_table_args")
272 if args:
273 cmd.extend(shlex.split(args))
274
275 p = common.Run(cmd, stdout=subprocess.PIPE)
276 p.communicate()
277 assert p.returncode == 0, "bpttool make_table failed"
278
279 common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img")
280 common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt")
281
282
Doug Zongker3c84f562014-07-31 11:06:30 -0700283def AddCache(output_zip, prefix="IMAGES/"):
284 """Create an empty cache image and store it in output_zip."""
285
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800286 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
287 if os.path.exists(prebuilt_path):
288 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
289 return
290
Tao Bao2c15d9e2015-07-09 11:51:16 -0700291 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700292 # The build system has to explicitly request for cache.img.
293 if "fs_type" not in image_props:
294 return
295
296 print "creating cache.img..."
297
Tao Bao822f5842015-09-30 16:01:14 -0700298 # Use a fixed timestamp (01/01/2009) when packaging the image.
299 # Bug: 24377993
300 epoch = datetime.datetime.fromtimestamp(0)
301 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
302 image_props["timestamp"] = int(timestamp)
303
Doug Zongker3c84f562014-07-31 11:06:30 -0700304 # The name of the directory it is making an image out of matters to
305 # mkyaffs2image. So we create a temp dir, and within it we create an
306 # empty dir named "cache", and build the image from that.
307 temp_dir = tempfile.mkdtemp()
308 user_dir = os.path.join(temp_dir, "cache")
309 os.mkdir(user_dir)
310 img = tempfile.NamedTemporaryFile()
311
312 fstab = OPTIONS.info_dict["fstab"]
313 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700314 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700315 succ = build_image.BuildImage(user_dir, image_props, img.name)
316 assert succ, "build cache.img image failed"
317
318 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700319 common.ZipWrite(output_zip, img.name, prefix + "cache.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700320 img.close()
321 os.rmdir(user_dir)
322 os.rmdir(temp_dir)
323
324
325def AddImagesToTargetFiles(filename):
326 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700327
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800328 if not OPTIONS.add_missing:
329 for n in input_zip.namelist():
330 if n.startswith("IMAGES/"):
331 print "target_files appears to already contain images."
332 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700333
Doug Zongker3c84f562014-07-31 11:06:30 -0700334 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700335 input_zip.getinfo("VENDOR/")
336 has_vendor = True
337 except KeyError:
338 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700339
Tao Bao2c15d9e2015-07-09 11:51:16 -0700340 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700341
Tao Bao2ed665a2015-04-01 11:21:55 -0700342 common.ZipClose(input_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700343 output_zip = zipfile.ZipFile(filename, "a",
Tao Bao9c84e502016-08-22 10:31:05 -0700344 compression=zipfile.ZIP_DEFLATED,
345 allowZip64=True)
Doug Zongker3c84f562014-07-31 11:06:30 -0700346
Tao Baodb45efa2015-10-27 19:25:18 -0700347 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
David Zeuthend995f4b2016-01-29 16:59:17 -0500348 system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true")
349 board_bvb_enable = (OPTIONS.info_dict.get("board_bvb_enable", None) == "true")
350
351 # Brillo Verified Boot is incompatible with certain
352 # configurations. Explicitly check for these.
353 if board_bvb_enable:
354 assert not has_recovery, "has_recovery incompatible with bvb"
355 assert not system_root_image, "system_root_image incompatible with bvb"
356 assert not OPTIONS.rebuild_recovery, "rebuild_recovery incompatible with bvb"
357 assert not has_vendor, "VENDOR images currently incompatible with bvb"
Tao Baodb45efa2015-10-27 19:25:18 -0700358
Doug Zongkerfc44a512014-08-26 13:10:25 -0700359 def banner(s):
360 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700361
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800362 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
363 boot_image = None
364 if os.path.exists(prebuilt_path):
David Zeuthend995f4b2016-01-29 16:59:17 -0500365 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800366 print "boot.img already exists in IMAGES/, no need to rebuild..."
367 if OPTIONS.rebuild_recovery:
368 boot_image = common.GetBootableImage(
369 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
370 else:
David Zeuthend995f4b2016-01-29 16:59:17 -0500371 if board_bvb_enable:
372 # With Brillo Verified Boot, we need to build system.img before
373 # boot.img since the latter includes the dm-verity root hash and
374 # salt for the former.
375 pass
376 else:
377 banner("boot")
378 boot_image = common.GetBootableImage(
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800379 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
David Zeuthend995f4b2016-01-29 16:59:17 -0500380 if boot_image:
381 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700382
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800383 recovery_image = None
Tao Baodb45efa2015-10-27 19:25:18 -0700384 if has_recovery:
385 banner("recovery")
386 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
387 if os.path.exists(prebuilt_path):
388 print "recovery.img already exists in IMAGES/, no need to rebuild..."
389 if OPTIONS.rebuild_recovery:
390 recovery_image = common.GetBootableImage(
391 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
392 "RECOVERY")
393 else:
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800394 recovery_image = common.GetBootableImage(
395 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
Tao Baodb45efa2015-10-27 19:25:18 -0700396 if recovery_image:
397 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700398
Doug Zongkerfc44a512014-08-26 13:10:25 -0700399 banner("system")
David Zeuthend995f4b2016-01-29 16:59:17 -0500400 system_img_path = AddSystem(
401 output_zip, recovery_img=recovery_image, boot_img=boot_image)
402 if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
403 # If we're using Brillo Verified Boot, we can now build boot.img
404 # given that we have system.img.
405 banner("boot")
406 boot_image = common.GetBootableImage(
407 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT",
408 system_img_path=system_img_path)
409 if boot_image:
410 boot_image.AddToZip(output_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700411 if has_vendor:
412 banner("vendor")
413 AddVendor(output_zip)
Tianjie Xub48589a2016-08-03 19:21:52 -0700414 if not OPTIONS.is_signing:
415 banner("userdata")
416 AddUserdata(output_zip)
417 banner("cache")
418 AddCache(output_zip)
David Zeuthen25328622016-04-08 15:08:03 -0400419 if OPTIONS.info_dict.get("board_bpt_enable", None) == "true":
420 banner("partition-table")
421 AddPartitionTable(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700422
Wei Wang2e735ca2016-05-10 22:48:13 -0700423 # For devices using A/B update, copy over images from RADIO/ and/or
424 # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
425 # images ready under IMAGES/. All images should have '.img' as extension.
Tianjie Xuaaca4212016-06-28 14:34:03 -0700426 banner("radio")
Tao Baoa0421cd2015-11-16 16:32:27 -0800427 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
428 if os.path.exists(ab_partitions):
429 with open(ab_partitions, 'r') as f:
430 lines = f.readlines()
431 for line in lines:
432 img_name = line.strip() + ".img"
Tianjie Xuaaca4212016-06-28 14:34:03 -0700433 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
434 if os.path.exists(prebuilt_path):
435 print "%s already exists, no need to overwrite..." % (img_name,)
436 continue
437
Tao Baoa0421cd2015-11-16 16:32:27 -0800438 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
Wei Wang2e735ca2016-05-10 22:48:13 -0700439 img_vendor_dir = os.path.join(
440 OPTIONS.input_tmp, "VENDOR_IMAGES")
Tao Baoa0421cd2015-11-16 16:32:27 -0800441 if os.path.exists(img_radio_path):
442 common.ZipWrite(output_zip, img_radio_path,
443 os.path.join("IMAGES", img_name))
Wei Wang2e735ca2016-05-10 22:48:13 -0700444 else:
445 for root, _, files in os.walk(img_vendor_dir):
446 if img_name in files:
447 common.ZipWrite(output_zip, os.path.join(root, img_name),
448 os.path.join("IMAGES", img_name))
449 break
Tao Baoa0421cd2015-11-16 16:32:27 -0800450
451 # Zip spec says: All slashes MUST be forward slashes.
452 img_path = 'IMAGES/' + img_name
453 assert img_path in output_zip.namelist(), "cannot find " + img_name
454
Tao Bao2ed665a2015-04-01 11:21:55 -0700455 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700456
Doug Zongker3c84f562014-07-31 11:06:30 -0700457def main(argv):
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700458 def option_handler(o, a):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800459 if o in ("-a", "--add_missing"):
460 OPTIONS.add_missing = True
461 elif o in ("-r", "--rebuild_recovery",):
462 OPTIONS.rebuild_recovery = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700463 elif o == "--replace_verity_private_key":
464 OPTIONS.replace_verity_private_key = (True, a)
465 elif o == "--replace_verity_public_key":
466 OPTIONS.replace_verity_public_key = (True, a)
Tianjie Xub48589a2016-08-03 19:21:52 -0700467 elif o == "--is_signing":
468 OPTIONS.is_signing = True
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700469 elif o == "--verity_signer_path":
470 OPTIONS.verity_signer_path = a
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800471 else:
472 return False
473 return True
474
Dan Albert8b72aef2015-03-23 19:13:21 -0700475 args = common.ParseOptions(
476 argv, __doc__, extra_opts="ar",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700477 extra_long_opts=["add_missing", "rebuild_recovery",
478 "replace_verity_public_key=",
479 "replace_verity_private_key=",
Tianjie Xub48589a2016-08-03 19:21:52 -0700480 "is_signing",
Baligh Uddin59f4ff12015-09-16 21:20:30 -0700481 "verity_signer_path="],
Dan Albert8b72aef2015-03-23 19:13:21 -0700482 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800483
Doug Zongker3c84f562014-07-31 11:06:30 -0700484
485 if len(args) != 1:
486 common.Usage(__doc__)
487 sys.exit(1)
488
489 AddImagesToTargetFiles(args[0])
490 print "done."
491
492if __name__ == '__main__':
493 try:
494 common.CloseInheritedPipes()
495 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700496 except common.ExternalError as e:
Doug Zongker3c84f562014-07-31 11:06:30 -0700497 print
498 print " ERROR: %s" % (e,)
499 print
500 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700501 finally:
502 common.Cleanup()