Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 1 | #!/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 | """ |
| 18 | Given a target-files zipfile that does not contain images (ie, does |
| 19 | not have an IMAGES/ top-level subdirectory), produce the images and |
| 20 | add them to the zipfile. |
| 21 | |
| 22 | Usage: add_img_to_target_files target_files |
| 23 | """ |
| 24 | |
| 25 | import sys |
| 26 | |
| 27 | if sys.hexversion < 0x02070000: |
| 28 | print >> sys.stderr, "Python 2.7 or newer is required." |
| 29 | sys.exit(1) |
| 30 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 31 | import datetime |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 32 | import errno |
| 33 | import os |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 34 | import shlex |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 35 | import shutil |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 36 | import subprocess |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 37 | import tempfile |
| 38 | import zipfile |
| 39 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 40 | import build_image |
| 41 | import common |
| 42 | |
| 43 | OPTIONS = common.OPTIONS |
| 44 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 45 | OPTIONS.add_missing = False |
| 46 | OPTIONS.rebuild_recovery = False |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 47 | OPTIONS.replace_verity_public_key = False |
| 48 | OPTIONS.replace_verity_private_key = False |
| 49 | OPTIONS.verity_signer_path = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 50 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 51 | def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 52 | """Turn the contents of SYSTEM into a system image and store it in |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 53 | output_zip. Returns the name of the system image file.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 54 | |
| 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 Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 58 | return prebuilt_path |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 59 | |
| 60 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 61 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 62 | ofile.write(data) |
| 63 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 64 | |
| 65 | if OPTIONS.rebuild_recovery: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 66 | print "Building new recovery patch" |
| 67 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 68 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 69 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 70 | block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map") |
| 71 | imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, |
| 72 | block_list=block_list) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 73 | |
| 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 Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 87 | common.ZipWrite(output_zip, imgname, prefix + "system.img") |
| 88 | common.ZipWrite(output_zip, block_list, prefix + "system.map") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 89 | return imgname |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def 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 | |
| 98 | def AddVendor(output_zip, prefix="IMAGES/"): |
| 99 | """Turn the contents of VENDOR into a vendor image and store in it |
| 100 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 101 | |
| 102 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img") |
| 103 | if os.path.exists(prebuilt_path): |
| 104 | print "vendor.img already exists in %s, no need to rebuild..." % (prefix,) |
| 105 | return |
| 106 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 107 | block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map") |
| 108 | imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 109 | block_list=block_list) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 110 | common.ZipWrite(output_zip, imgname, prefix + "vendor.img") |
| 111 | common.ZipWrite(output_zip, block_list, prefix + "vendor.map") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 112 | |
| 113 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 114 | def BuildVendor(input_dir, info_dict, block_list=None): |
| 115 | """Build the (sparse) vendor image and return the name of a temp |
| 116 | file containing it.""" |
| 117 | return CreateImage(input_dir, info_dict, "vendor", block_list=block_list) |
| 118 | |
| 119 | |
| 120 | def CreateImage(input_dir, info_dict, what, block_list=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 121 | print "creating " + what + ".img..." |
| 122 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 123 | img = common.MakeTempFile(prefix=what + "-", suffix=".img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 124 | |
| 125 | # The name of the directory it is making an image out of matters to |
| 126 | # mkyaffs2image. It wants "system" but we have a directory named |
| 127 | # "SYSTEM", so create a symlink. |
| 128 | try: |
| 129 | os.symlink(os.path.join(input_dir, what.upper()), |
| 130 | os.path.join(input_dir, what)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 131 | except OSError as e: |
| 132 | # bogus error on my mac version? |
| 133 | # File "./build/tools/releasetools/img_from_target_files" |
| 134 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 135 | # OSError: [Errno 17] File exists |
| 136 | if e.errno == errno.EEXIST: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 137 | pass |
| 138 | |
| 139 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 140 | fstab = info_dict["fstab"] |
| 141 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 142 | image_props["fs_type"] = fstab["/" + what].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 143 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 144 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 145 | # Bug: 24377993 |
| 146 | epoch = datetime.datetime.fromtimestamp(0) |
| 147 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 148 | image_props["timestamp"] = int(timestamp) |
| 149 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 150 | if what == "system": |
| 151 | fs_config_prefix = "" |
| 152 | else: |
| 153 | fs_config_prefix = what + "_" |
| 154 | |
| 155 | fs_config = os.path.join( |
| 156 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 157 | if not os.path.exists(fs_config): |
| 158 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 159 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 160 | # Override values loaded from info_dict. |
| 161 | if fs_config: |
| 162 | image_props["fs_config"] = fs_config |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 163 | if block_list: |
| 164 | image_props["block_list"] = block_list |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 165 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 166 | succ = build_image.BuildImage(os.path.join(input_dir, what), |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 167 | image_props, img) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 168 | assert succ, "build " + what + ".img image failed" |
| 169 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 170 | return img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 171 | |
| 172 | |
| 173 | def AddUserdata(output_zip, prefix="IMAGES/"): |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 174 | """Create a userdata image and store it in output_zip. |
| 175 | |
| 176 | In most case we just create and store an empty userdata.img; |
| 177 | But the invoker can also request to create userdata.img with real |
| 178 | data from the target files, by setting "userdata_img_with_data=true" |
| 179 | in OPTIONS.info_dict. |
| 180 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 181 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 182 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img") |
| 183 | if os.path.exists(prebuilt_path): |
| 184 | print "userdata.img already exists in %s, no need to rebuild..." % (prefix,) |
| 185 | return |
| 186 | |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame^] | 187 | # Skip userdata.img if no size. |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 188 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame^] | 189 | if not image_props.get("partition_size"): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 190 | return |
| 191 | |
| 192 | print "creating userdata.img..." |
| 193 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 194 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 195 | # Bug: 24377993 |
| 196 | epoch = datetime.datetime.fromtimestamp(0) |
| 197 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 198 | image_props["timestamp"] = int(timestamp) |
| 199 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 200 | # The name of the directory it is making an image out of matters to |
| 201 | # mkyaffs2image. So we create a temp dir, and within it we create an |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 202 | # empty dir named "data", or a symlink to the DATA dir, |
| 203 | # and build the image from that. |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 204 | temp_dir = tempfile.mkdtemp() |
| 205 | user_dir = os.path.join(temp_dir, "data") |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 206 | empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true") |
| 207 | if empty: |
| 208 | # Create an empty dir. |
| 209 | os.mkdir(user_dir) |
| 210 | else: |
| 211 | # Symlink to the DATA dir. |
| 212 | os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"), |
| 213 | user_dir) |
| 214 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 215 | img = tempfile.NamedTemporaryFile() |
| 216 | |
| 217 | fstab = OPTIONS.info_dict["fstab"] |
| 218 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 219 | image_props["fs_type"] = fstab["/data"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 220 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 221 | assert succ, "build userdata.img image failed" |
| 222 | |
| 223 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 224 | common.ZipWrite(output_zip, img.name, prefix + "userdata.img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 225 | img.close() |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 226 | shutil.rmtree(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 227 | |
| 228 | |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 229 | def AddPartitionTable(output_zip, prefix="IMAGES/"): |
| 230 | """Create a partition table image and store it in output_zip.""" |
| 231 | |
| 232 | _, img_file_name = tempfile.mkstemp() |
| 233 | _, bpt_file_name = tempfile.mkstemp() |
| 234 | |
| 235 | # use BPTTOOL from environ, or "bpttool" if empty or not set. |
| 236 | bpttool = os.getenv("BPTTOOL") or "bpttool" |
| 237 | cmd = [bpttool, "make_table", "--output_json", bpt_file_name, |
| 238 | "--output_gpt", img_file_name] |
| 239 | input_files_str = OPTIONS.info_dict["board_bpt_input_files"] |
| 240 | input_files = input_files_str.split(" ") |
| 241 | for i in input_files: |
| 242 | cmd.extend(["--input", i]) |
| 243 | disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") |
| 244 | if disk_size: |
| 245 | cmd.extend(["--disk_size", disk_size]) |
| 246 | args = OPTIONS.info_dict.get("board_bpt_make_table_args") |
| 247 | if args: |
| 248 | cmd.extend(shlex.split(args)) |
| 249 | |
| 250 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 251 | p.communicate() |
| 252 | assert p.returncode == 0, "bpttool make_table failed" |
| 253 | |
| 254 | common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img") |
| 255 | common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt") |
| 256 | |
| 257 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 258 | def AddCache(output_zip, prefix="IMAGES/"): |
| 259 | """Create an empty cache image and store it in output_zip.""" |
| 260 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 261 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img") |
| 262 | if os.path.exists(prebuilt_path): |
| 263 | print "cache.img already exists in %s, no need to rebuild..." % (prefix,) |
| 264 | return |
| 265 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 266 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 267 | # The build system has to explicitly request for cache.img. |
| 268 | if "fs_type" not in image_props: |
| 269 | return |
| 270 | |
| 271 | print "creating cache.img..." |
| 272 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 273 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 274 | # Bug: 24377993 |
| 275 | epoch = datetime.datetime.fromtimestamp(0) |
| 276 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 277 | image_props["timestamp"] = int(timestamp) |
| 278 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 279 | # The name of the directory it is making an image out of matters to |
| 280 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 281 | # empty dir named "cache", and build the image from that. |
| 282 | temp_dir = tempfile.mkdtemp() |
| 283 | user_dir = os.path.join(temp_dir, "cache") |
| 284 | os.mkdir(user_dir) |
| 285 | img = tempfile.NamedTemporaryFile() |
| 286 | |
| 287 | fstab = OPTIONS.info_dict["fstab"] |
| 288 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 289 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 290 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 291 | assert succ, "build cache.img image failed" |
| 292 | |
| 293 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 294 | common.ZipWrite(output_zip, img.name, prefix + "cache.img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 295 | img.close() |
| 296 | os.rmdir(user_dir) |
| 297 | os.rmdir(temp_dir) |
| 298 | |
| 299 | |
| 300 | def AddImagesToTargetFiles(filename): |
| 301 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 302 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 303 | if not OPTIONS.add_missing: |
| 304 | for n in input_zip.namelist(): |
| 305 | if n.startswith("IMAGES/"): |
| 306 | print "target_files appears to already contain images." |
| 307 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 308 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 309 | try: |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 310 | input_zip.getinfo("VENDOR/") |
| 311 | has_vendor = True |
| 312 | except KeyError: |
| 313 | has_vendor = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 314 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 315 | OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 316 | |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 317 | common.ZipClose(input_zip) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 318 | output_zip = zipfile.ZipFile(filename, "a", |
| 319 | compression=zipfile.ZIP_DEFLATED) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 320 | |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 321 | has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 322 | system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true") |
| 323 | board_bvb_enable = (OPTIONS.info_dict.get("board_bvb_enable", None) == "true") |
| 324 | |
| 325 | # Brillo Verified Boot is incompatible with certain |
| 326 | # configurations. Explicitly check for these. |
| 327 | if board_bvb_enable: |
| 328 | assert not has_recovery, "has_recovery incompatible with bvb" |
| 329 | assert not system_root_image, "system_root_image incompatible with bvb" |
| 330 | assert not OPTIONS.rebuild_recovery, "rebuild_recovery incompatible with bvb" |
| 331 | assert not has_vendor, "VENDOR images currently incompatible with bvb" |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 332 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 333 | def banner(s): |
| 334 | print "\n\n++++ " + s + " ++++\n\n" |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 335 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 336 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 337 | boot_image = None |
| 338 | if os.path.exists(prebuilt_path): |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 339 | banner("boot") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 340 | print "boot.img already exists in IMAGES/, no need to rebuild..." |
| 341 | if OPTIONS.rebuild_recovery: |
| 342 | boot_image = common.GetBootableImage( |
| 343 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 344 | else: |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 345 | if board_bvb_enable: |
| 346 | # With Brillo Verified Boot, we need to build system.img before |
| 347 | # boot.img since the latter includes the dm-verity root hash and |
| 348 | # salt for the former. |
| 349 | pass |
| 350 | else: |
| 351 | banner("boot") |
| 352 | boot_image = common.GetBootableImage( |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 353 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 354 | if boot_image: |
| 355 | boot_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 356 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 357 | recovery_image = None |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 358 | if has_recovery: |
| 359 | banner("recovery") |
| 360 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") |
| 361 | if os.path.exists(prebuilt_path): |
| 362 | print "recovery.img already exists in IMAGES/, no need to rebuild..." |
| 363 | if OPTIONS.rebuild_recovery: |
| 364 | recovery_image = common.GetBootableImage( |
| 365 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, |
| 366 | "RECOVERY") |
| 367 | else: |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 368 | recovery_image = common.GetBootableImage( |
| 369 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 370 | if recovery_image: |
| 371 | recovery_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 372 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 373 | banner("system") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 374 | system_img_path = AddSystem( |
| 375 | output_zip, recovery_img=recovery_image, boot_img=boot_image) |
| 376 | if OPTIONS.info_dict.get("board_bvb_enable", None) == "true": |
| 377 | # If we're using Brillo Verified Boot, we can now build boot.img |
| 378 | # given that we have system.img. |
| 379 | banner("boot") |
| 380 | boot_image = common.GetBootableImage( |
| 381 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT", |
| 382 | system_img_path=system_img_path) |
| 383 | if boot_image: |
| 384 | boot_image.AddToZip(output_zip) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 385 | if has_vendor: |
| 386 | banner("vendor") |
| 387 | AddVendor(output_zip) |
| 388 | banner("userdata") |
| 389 | AddUserdata(output_zip) |
| 390 | banner("cache") |
| 391 | AddCache(output_zip) |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 392 | if OPTIONS.info_dict.get("board_bpt_enable", None) == "true": |
| 393 | banner("partition-table") |
| 394 | AddPartitionTable(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 395 | |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 396 | # For devices using A/B update, copy over images from RADIO/ and/or |
| 397 | # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed |
| 398 | # images ready under IMAGES/. All images should have '.img' as extension. |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 399 | ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") |
| 400 | if os.path.exists(ab_partitions): |
| 401 | with open(ab_partitions, 'r') as f: |
| 402 | lines = f.readlines() |
| 403 | for line in lines: |
| 404 | img_name = line.strip() + ".img" |
| 405 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 406 | img_vendor_dir = os.path.join( |
| 407 | OPTIONS.input_tmp, "VENDOR_IMAGES") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 408 | if os.path.exists(img_radio_path): |
| 409 | common.ZipWrite(output_zip, img_radio_path, |
| 410 | os.path.join("IMAGES", img_name)) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 411 | else: |
| 412 | for root, _, files in os.walk(img_vendor_dir): |
| 413 | if img_name in files: |
| 414 | common.ZipWrite(output_zip, os.path.join(root, img_name), |
| 415 | os.path.join("IMAGES", img_name)) |
| 416 | break |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 417 | |
| 418 | # Zip spec says: All slashes MUST be forward slashes. |
| 419 | img_path = 'IMAGES/' + img_name |
| 420 | assert img_path in output_zip.namelist(), "cannot find " + img_name |
| 421 | |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 422 | common.ZipClose(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 423 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 424 | def main(argv): |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 425 | def option_handler(o, a): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 426 | if o in ("-a", "--add_missing"): |
| 427 | OPTIONS.add_missing = True |
| 428 | elif o in ("-r", "--rebuild_recovery",): |
| 429 | OPTIONS.rebuild_recovery = True |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 430 | elif o == "--replace_verity_private_key": |
| 431 | OPTIONS.replace_verity_private_key = (True, a) |
| 432 | elif o == "--replace_verity_public_key": |
| 433 | OPTIONS.replace_verity_public_key = (True, a) |
| 434 | elif o == "--verity_signer_path": |
| 435 | OPTIONS.verity_signer_path = a |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 436 | else: |
| 437 | return False |
| 438 | return True |
| 439 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 440 | args = common.ParseOptions( |
| 441 | argv, __doc__, extra_opts="ar", |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 442 | extra_long_opts=["add_missing", "rebuild_recovery", |
| 443 | "replace_verity_public_key=", |
| 444 | "replace_verity_private_key=", |
| 445 | "verity_signer_path="], |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 446 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 447 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 448 | |
| 449 | if len(args) != 1: |
| 450 | common.Usage(__doc__) |
| 451 | sys.exit(1) |
| 452 | |
| 453 | AddImagesToTargetFiles(args[0]) |
| 454 | print "done." |
| 455 | |
| 456 | if __name__ == '__main__': |
| 457 | try: |
| 458 | common.CloseInheritedPipes() |
| 459 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 460 | except common.ExternalError as e: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 461 | print |
| 462 | print " ERROR: %s" % (e,) |
| 463 | print |
| 464 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 465 | finally: |
| 466 | common.Cleanup() |