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