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