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 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 46 | from __future__ import print_function |
| 47 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 48 | import sys |
| 49 | |
| 50 | if sys.hexversion < 0x02070000: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 51 | print("Python 2.7 or newer is required.", file=sys.stderr) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 52 | sys.exit(1) |
| 53 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 54 | import datetime |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 55 | import hashlib |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 56 | import os |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 57 | import shlex |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 58 | import shutil |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 59 | import subprocess |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 60 | import tempfile |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 61 | import uuid |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 62 | import zipfile |
| 63 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 64 | import build_image |
| 65 | import common |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 66 | import rangelib |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 67 | import sparse_img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 68 | |
| 69 | OPTIONS = common.OPTIONS |
| 70 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 71 | OPTIONS.add_missing = False |
| 72 | OPTIONS.rebuild_recovery = False |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 73 | OPTIONS.replace_updated_files_list = [] |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 74 | OPTIONS.replace_verity_public_key = False |
| 75 | OPTIONS.replace_verity_private_key = False |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 76 | OPTIONS.is_signing = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 77 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 78 | |
| 79 | class OutputFile(object): |
| 80 | def __init__(self, output_zip, input_dir, prefix, name): |
| 81 | self._output_zip = output_zip |
| 82 | self.input_name = os.path.join(input_dir, prefix, name) |
| 83 | |
| 84 | if self._output_zip: |
| 85 | self._zip_name = os.path.join(prefix, name) |
| 86 | |
| 87 | root, suffix = os.path.splitext(name) |
| 88 | self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix) |
| 89 | else: |
| 90 | self.name = self.input_name |
| 91 | |
| 92 | def Write(self): |
| 93 | if self._output_zip: |
| 94 | common.ZipWrite(self._output_zip, self.name, self._zip_name) |
| 95 | |
| 96 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 97 | def GetCareMap(which, imgname): |
| 98 | """Generate care_map of system (or vendor) partition""" |
| 99 | |
| 100 | assert which in ("system", "vendor") |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 101 | |
| 102 | simg = sparse_img.SparseImage(imgname) |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 103 | care_map_list = [which] |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 104 | |
| 105 | care_map_ranges = simg.care_map |
| 106 | key = which + "_adjusted_partition_size" |
| 107 | adjusted_blocks = OPTIONS.info_dict.get(key) |
| 108 | if adjusted_blocks: |
| 109 | assert adjusted_blocks > 0, "blocks should be positive for " + which |
| 110 | care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet( |
| 111 | "0-%d" % (adjusted_blocks,))) |
| 112 | |
| 113 | care_map_list.append(care_map_ranges.to_string_raw()) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 114 | return care_map_list |
| 115 | |
| 116 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 117 | def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 118 | """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] | 119 | output_zip. Returns the name of the system image file.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 120 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 121 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img") |
| 122 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 123 | print("system.img already exists in %s, no need to rebuild..." % (prefix,)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 124 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 125 | |
| 126 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 127 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 128 | ofile.write(data) |
| 129 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 130 | |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 131 | arc_name = "SYSTEM/" + fn |
| 132 | if arc_name in output_zip.namelist(): |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 133 | OPTIONS.replace_updated_files_list.append(arc_name) |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 134 | else: |
| 135 | common.ZipWrite(output_zip, ofile.name, arc_name) |
| 136 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 137 | if OPTIONS.rebuild_recovery: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 138 | print("Building new recovery patch") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 139 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 140 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 141 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 142 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map") |
| 143 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img, |
| 144 | block_list=block_list) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 145 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 146 | return img.name |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 147 | |
| 148 | |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 149 | def AddSystemOther(output_zip, prefix="IMAGES/"): |
| 150 | """Turn the contents of SYSTEM_OTHER into a system_other image |
| 151 | and store it in output_zip.""" |
| 152 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 153 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img") |
| 154 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 155 | print("system_other.img already exists in %s, no need to rebuild..." % ( |
| 156 | prefix,)) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 157 | return |
| 158 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 159 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 160 | |
| 161 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 162 | def AddVendor(output_zip, prefix="IMAGES/"): |
| 163 | """Turn the contents of VENDOR into a vendor image and store in it |
| 164 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 165 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 166 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img") |
| 167 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 168 | print("vendor.img already exists in %s, no need to rebuild..." % (prefix,)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 169 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 170 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 171 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map") |
| 172 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img, |
| 173 | block_list=block_list) |
| 174 | return img.name |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 175 | |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 176 | |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 177 | def AddDtbo(output_zip, prefix="IMAGES/"): |
| 178 | """Adds the DTBO image. |
| 179 | |
| 180 | Uses the image under prefix if it already exists. Otherwise looks for the |
| 181 | image under PREBUILT_IMAGES/, signs it as needed, and returns the image name. |
| 182 | """ |
| 183 | |
| 184 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img") |
| 185 | if os.path.exists(img.input_name): |
| 186 | print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,)) |
| 187 | return img.input_name |
| 188 | |
| 189 | dtbo_prebuilt_path = os.path.join( |
| 190 | OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img") |
| 191 | assert os.path.exists(dtbo_prebuilt_path) |
| 192 | shutil.copy(dtbo_prebuilt_path, img.name) |
| 193 | |
| 194 | # AVB-sign the image as needed. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 195 | if OPTIONS.info_dict.get("avb_enable") == "true": |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 196 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
Tao Bao | 3ebfdde | 2017-05-23 23:06:55 -0700 | [diff] [blame] | 197 | part_size = OPTIONS.info_dict["dtbo_size"] |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 198 | # The AVB hash footer will be replaced if already present. |
| 199 | cmd = [avbtool, "add_hash_footer", "--image", img.name, |
| 200 | "--partition_size", str(part_size), "--partition_name", "dtbo"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 201 | common.AppendAVBSigningArgs(cmd, "dtbo") |
| 202 | args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args") |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 203 | if args and args.strip(): |
| 204 | cmd.extend(shlex.split(args)) |
| 205 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 206 | p.communicate() |
| 207 | assert p.returncode == 0, \ |
| 208 | "avbtool add_hash_footer of %s failed" % (img.name,) |
| 209 | |
| 210 | img.Write() |
| 211 | return img.name |
| 212 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 213 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 214 | def CreateImage(input_dir, info_dict, what, output_file, block_list=None): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 215 | print("creating " + what + ".img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 216 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 217 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 218 | fstab = info_dict["fstab"] |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 219 | mount_point = "/" + what |
| 220 | if fstab and mount_point in fstab: |
| 221 | image_props["fs_type"] = fstab[mount_point].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 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 | if what == "system": |
| 230 | fs_config_prefix = "" |
| 231 | else: |
| 232 | fs_config_prefix = what + "_" |
| 233 | |
| 234 | fs_config = os.path.join( |
| 235 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 236 | if not os.path.exists(fs_config): |
| 237 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 238 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 239 | # Override values loaded from info_dict. |
| 240 | if fs_config: |
| 241 | image_props["fs_config"] = fs_config |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 242 | if block_list: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 243 | image_props["block_list"] = block_list.name |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 244 | |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 245 | # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and |
| 246 | # build fingerprint). |
| 247 | uuid_seed = what + "-" |
| 248 | if "build.prop" in info_dict: |
| 249 | build_prop = info_dict["build.prop"] |
| 250 | if "ro.build.fingerprint" in build_prop: |
| 251 | uuid_seed += build_prop["ro.build.fingerprint"] |
| 252 | elif "ro.build.thumbprint" in build_prop: |
| 253 | uuid_seed += build_prop["ro.build.thumbprint"] |
| 254 | image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed)) |
| 255 | hash_seed = "hash_seed-" + uuid_seed |
| 256 | image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed)) |
| 257 | |
Tao Bao | fa863c8 | 2017-05-23 23:49:03 -0700 | [diff] [blame] | 258 | succ = build_image.BuildImage(os.path.join(input_dir, what.upper()), |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 259 | image_props, output_file.name) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 260 | assert succ, "build " + what + ".img image failed" |
| 261 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 262 | output_file.Write() |
| 263 | if block_list: |
| 264 | block_list.Write() |
| 265 | |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 266 | # Set the 'adjusted_partition_size' that excludes the verity blocks of the |
| 267 | # given image. When avb is enabled, this size is the max image size returned |
| 268 | # by the avb tool. |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 269 | is_verity_partition = "verity_block_device" in image_props |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 270 | verity_supported = (image_props.get("verity") == "true" or |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 271 | image_props.get("avb_enable") == "true") |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 272 | is_avb_enable = image_props.get("avb_hashtree_enable") == "true" |
| 273 | if verity_supported and (is_verity_partition or is_avb_enable): |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 274 | adjusted_blocks_value = image_props.get("partition_size") |
| 275 | if adjusted_blocks_value: |
| 276 | adjusted_blocks_key = what + "_adjusted_partition_size" |
| 277 | info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1 |
| 278 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 279 | |
| 280 | def AddUserdata(output_zip, prefix="IMAGES/"): |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 281 | """Create a userdata image and store it in output_zip. |
| 282 | |
| 283 | In most case we just create and store an empty userdata.img; |
| 284 | But the invoker can also request to create userdata.img with real |
| 285 | data from the target files, by setting "userdata_img_with_data=true" |
| 286 | in OPTIONS.info_dict. |
| 287 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 288 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 289 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img") |
| 290 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 291 | print("userdata.img already exists in %s, no need to rebuild..." % ( |
| 292 | prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 293 | return |
| 294 | |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 295 | # Skip userdata.img if no size. |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 296 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 297 | if not image_props.get("partition_size"): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 298 | return |
| 299 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 300 | print("creating userdata.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 301 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 302 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 303 | # Bug: 24377993 |
| 304 | epoch = datetime.datetime.fromtimestamp(0) |
| 305 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 306 | image_props["timestamp"] = int(timestamp) |
| 307 | |
Tao Bao | fa863c8 | 2017-05-23 23:49:03 -0700 | [diff] [blame] | 308 | if OPTIONS.info_dict.get("userdata_img_with_data") == "true": |
| 309 | user_dir = os.path.join(OPTIONS.input_tmp, "DATA") |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 310 | else: |
Tao Bao | fa863c8 | 2017-05-23 23:49:03 -0700 | [diff] [blame] | 311 | user_dir = tempfile.mkdtemp() |
| 312 | OPTIONS.tempfiles.append(user_dir) |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 313 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 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["/data"].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 userdata.img image failed" |
| 319 | |
| 320 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 321 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 322 | |
| 323 | |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 324 | def AppendVBMetaArgsForPartition(cmd, partition, img_path, public_key_dir): |
| 325 | if not img_path: |
| 326 | return |
| 327 | |
| 328 | # Check if chain partition is used. |
| 329 | key_path = OPTIONS.info_dict.get("avb_" + partition + "_key_path") |
| 330 | if key_path: |
| 331 | # extract public key in AVB format to be included in vbmeta.img |
| 332 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
| 333 | public_key_path = os.path.join(public_key_dir, "%s.avbpubkey" % partition) |
| 334 | p = common.Run([avbtool, "extract_public_key", "--key", key_path, |
| 335 | "--output", public_key_path], |
| 336 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 337 | p.communicate() |
| 338 | assert p.returncode == 0, \ |
| 339 | "avbtool extract_public_key fail for partition: %r" % partition |
| 340 | |
| 341 | rollback_index_location = OPTIONS.info_dict[ |
| 342 | "avb_" + partition + "_rollback_index_location"] |
| 343 | cmd.extend(["--chain_partition", "%s:%s:%s" % ( |
| 344 | partition, rollback_index_location, public_key_path)]) |
| 345 | else: |
| 346 | cmd.extend(["--include_descriptors_from_image", img_path]) |
| 347 | |
| 348 | |
Bowgo Tsai | 8ee4a3d | 2017-03-31 15:21:26 +0800 | [diff] [blame] | 349 | def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 350 | dtbo_img_path, prefix="IMAGES/"): |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 351 | """Create a VBMeta image and store it in output_zip.""" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 352 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 353 | if os.path.exists(img.input_name): |
| 354 | print("vbmeta.img already exists in %s; not rebuilding..." % (prefix,)) |
| 355 | return img.input_name |
| 356 | |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 357 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 358 | cmd = [avbtool, "make_vbmeta_image", "--output", img.name] |
| 359 | common.AppendAVBSigningArgs(cmd, "vbmeta") |
| 360 | |
| 361 | public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-") |
| 362 | OPTIONS.tempfiles.append(public_key_dir) |
| 363 | |
| 364 | AppendVBMetaArgsForPartition(cmd, "boot", boot_img_path, public_key_dir) |
| 365 | AppendVBMetaArgsForPartition(cmd, "system", system_img_path, public_key_dir) |
| 366 | AppendVBMetaArgsForPartition(cmd, "vendor", vendor_img_path, public_key_dir) |
| 367 | AppendVBMetaArgsForPartition(cmd, "dtbo", dtbo_img_path, public_key_dir) |
| 368 | |
| 369 | args = OPTIONS.info_dict.get("avb_vbmeta_args") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 370 | if args and args.strip(): |
Tao Bao | 9a5f419 | 2017-07-20 23:51:16 -0700 | [diff] [blame] | 371 | split_args = shlex.split(args) |
| 372 | for index, arg in enumerate(split_args[:-1]): |
| 373 | # Sanity check that the image file exists. Some images might be defined |
| 374 | # as a path relative to source tree, which may not be available at the |
| 375 | # same location when running this script (we have the input target_files |
| 376 | # zip only). For such cases, we additionally scan other locations (e.g. |
| 377 | # IMAGES/, RADIO/, etc) before bailing out. |
| 378 | if arg == '--include_descriptors_from_image': |
| 379 | image_path = split_args[index + 1] |
| 380 | if os.path.exists(image_path): |
| 381 | continue |
| 382 | found = False |
| 383 | for dir in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']: |
| 384 | alt_path = os.path.join( |
| 385 | OPTIONS.input_tmp, dir, os.path.basename(image_path)) |
| 386 | if os.path.exists(alt_path): |
| 387 | split_args[index + 1] = alt_path |
| 388 | found = True |
| 389 | break |
| 390 | assert found, 'failed to find %s' % (image_path,) |
| 391 | cmd.extend(split_args) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 392 | |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 393 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 394 | p.communicate() |
| 395 | assert p.returncode == 0, "avbtool make_vbmeta_image failed" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 396 | img.Write() |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 397 | |
| 398 | |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 399 | def AddPartitionTable(output_zip, prefix="IMAGES/"): |
| 400 | """Create a partition table image and store it in output_zip.""" |
| 401 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 402 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img") |
| 403 | bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt") |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 404 | |
| 405 | # use BPTTOOL from environ, or "bpttool" if empty or not set. |
| 406 | bpttool = os.getenv("BPTTOOL") or "bpttool" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 407 | cmd = [bpttool, "make_table", "--output_json", bpt.name, |
| 408 | "--output_gpt", img.name] |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 409 | input_files_str = OPTIONS.info_dict["board_bpt_input_files"] |
| 410 | input_files = input_files_str.split(" ") |
| 411 | for i in input_files: |
| 412 | cmd.extend(["--input", i]) |
| 413 | disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") |
| 414 | if disk_size: |
| 415 | cmd.extend(["--disk_size", disk_size]) |
| 416 | args = OPTIONS.info_dict.get("board_bpt_make_table_args") |
| 417 | if args: |
| 418 | cmd.extend(shlex.split(args)) |
| 419 | |
| 420 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 421 | p.communicate() |
| 422 | assert p.returncode == 0, "bpttool make_table failed" |
| 423 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 424 | img.Write() |
| 425 | bpt.Write() |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 426 | |
| 427 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 428 | def AddCache(output_zip, prefix="IMAGES/"): |
| 429 | """Create an empty cache image and store it in output_zip.""" |
| 430 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 431 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img") |
| 432 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 433 | print("cache.img already exists in %s, no need to rebuild..." % (prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 434 | return |
| 435 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 436 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 437 | # The build system has to explicitly request for cache.img. |
| 438 | if "fs_type" not in image_props: |
| 439 | return |
| 440 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 441 | print("creating cache.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 442 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 443 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 444 | # Bug: 24377993 |
| 445 | epoch = datetime.datetime.fromtimestamp(0) |
| 446 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 447 | image_props["timestamp"] = int(timestamp) |
| 448 | |
Tao Bao | fa863c8 | 2017-05-23 23:49:03 -0700 | [diff] [blame] | 449 | user_dir = tempfile.mkdtemp() |
| 450 | OPTIONS.tempfiles.append(user_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 451 | |
| 452 | fstab = OPTIONS.info_dict["fstab"] |
| 453 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 454 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 455 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 456 | assert succ, "build cache.img image failed" |
| 457 | |
| 458 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 459 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 460 | |
| 461 | |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 462 | def ReplaceUpdatedFiles(zip_filename, files_list): |
| 463 | """Update all the zip entries listed in the files_list. |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 464 | |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 465 | For now the list includes META/care_map.txt, and the related files under |
| 466 | SYSTEM/ after rebuilding recovery. |
| 467 | """ |
| 468 | |
| 469 | cmd = ["zip", "-d", zip_filename] + files_list |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 470 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 471 | p.communicate() |
| 472 | |
| 473 | output_zip = zipfile.ZipFile(zip_filename, "a", |
| 474 | compression=zipfile.ZIP_DEFLATED, |
| 475 | allowZip64=True) |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 476 | for item in files_list: |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 477 | file_path = os.path.join(OPTIONS.input_tmp, item) |
| 478 | assert os.path.exists(file_path) |
| 479 | common.ZipWrite(output_zip, file_path, arcname=item) |
| 480 | common.ZipClose(output_zip) |
| 481 | |
| 482 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 483 | def AddImagesToTargetFiles(filename): |
Tao Bao | ae396d9 | 2017-11-20 11:56:43 -0800 | [diff] [blame^] | 484 | """Creates and adds images (boot/recovery/system/...) to a target_files.zip. |
| 485 | |
| 486 | It works with either a zip file (zip mode), or a directory that contains the |
| 487 | files to be packed into a target_files.zip (dir mode). The latter is used when |
| 488 | being called from build/make/core/Makefile. |
| 489 | |
| 490 | The images will be created under IMAGES/ in the input target_files.zip. |
| 491 | |
| 492 | Args: |
| 493 | filename: the target_files.zip, or the zip root directory. |
| 494 | """ |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 495 | if os.path.isdir(filename): |
| 496 | OPTIONS.input_tmp = os.path.abspath(filename) |
| 497 | input_zip = None |
| 498 | else: |
| 499 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 500 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 501 | if not OPTIONS.add_missing: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 502 | if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")): |
| 503 | print("target_files appears to already contain images.") |
| 504 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 505 | |
Tao Bao | b22afea | 2017-09-12 12:39:09 -0700 | [diff] [blame] | 506 | # vendor.img is unlike system.img or system_other.img. Because it could be |
| 507 | # built from source, or dropped into target_files.zip as a prebuilt blob. We |
| 508 | # consider either of them as vendor.img being available, which could be used |
| 509 | # when generating vbmeta.img for AVB. |
| 510 | has_vendor = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) or |
| 511 | os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES", |
| 512 | "vendor.img"))) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 513 | has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp, |
| 514 | "SYSTEM_OTHER")) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 515 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 516 | if input_zip: |
| 517 | OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 518 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 519 | common.ZipClose(input_zip) |
| 520 | output_zip = zipfile.ZipFile(filename, "a", |
| 521 | compression=zipfile.ZIP_DEFLATED, |
| 522 | allowZip64=True) |
| 523 | else: |
| 524 | OPTIONS.info_dict = common.LoadInfoDict(filename, filename) |
| 525 | output_zip = None |
Tao Bao | ae396d9 | 2017-11-20 11:56:43 -0800 | [diff] [blame^] | 526 | |
| 527 | # Always make input_tmp/IMAGES available, since we may stage boot / recovery |
| 528 | # images there even under zip mode. The directory will be cleaned up as part |
| 529 | # of OPTIONS.input_tmp. |
| 530 | images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES") |
| 531 | if not os.path.isdir(images_dir): |
| 532 | os.makedirs(images_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 533 | |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 534 | has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") |
| 535 | |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 536 | if OPTIONS.info_dict.get("avb_enable") == "true": |
| 537 | fp = None |
| 538 | if "build.prop" in OPTIONS.info_dict: |
| 539 | build_prop = OPTIONS.info_dict["build.prop"] |
| 540 | if "ro.build.fingerprint" in build_prop: |
| 541 | fp = build_prop["ro.build.fingerprint"] |
| 542 | elif "ro.build.thumbprint" in build_prop: |
| 543 | fp = build_prop["ro.build.thumbprint"] |
| 544 | if fp: |
| 545 | OPTIONS.info_dict["avb_salt"] = hashlib.sha256(fp).hexdigest() |
| 546 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 547 | def banner(s): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 548 | print("\n\n++++ " + s + " ++++\n\n") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 549 | |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 550 | banner("boot") |
| 551 | # common.GetBootableImage() returns the image directly if present. |
| 552 | boot_image = common.GetBootableImage( |
| 553 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 554 | # boot.img may be unavailable in some targets (e.g. aosp_arm64). |
| 555 | if boot_image: |
| 556 | boot_img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 557 | if not os.path.exists(boot_img_path): |
| 558 | boot_image.WriteToDir(OPTIONS.input_tmp) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 559 | if output_zip: |
| 560 | boot_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 561 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 562 | recovery_image = None |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 563 | if has_recovery: |
| 564 | banner("recovery") |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 565 | recovery_image = common.GetBootableImage( |
| 566 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
| 567 | assert recovery_image, "Failed to create recovery.img." |
| 568 | recovery_img_path = os.path.join( |
| 569 | OPTIONS.input_tmp, "IMAGES", "recovery.img") |
| 570 | if not os.path.exists(recovery_img_path): |
| 571 | recovery_image.WriteToDir(OPTIONS.input_tmp) |
| 572 | if output_zip: |
| 573 | recovery_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 574 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 575 | banner("recovery (two-step image)") |
| 576 | # The special recovery.img for two-step package use. |
| 577 | recovery_two_step_image = common.GetBootableImage( |
| 578 | "IMAGES/recovery-two-step.img", "recovery-two-step.img", |
| 579 | OPTIONS.input_tmp, "RECOVERY", two_step_image=True) |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 580 | assert recovery_two_step_image, "Failed to create recovery-two-step.img." |
| 581 | recovery_two_step_image_path = os.path.join( |
| 582 | OPTIONS.input_tmp, "IMAGES", "recovery-two-step.img") |
| 583 | if not os.path.exists(recovery_two_step_image_path): |
| 584 | recovery_two_step_image.WriteToDir(OPTIONS.input_tmp) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 585 | if output_zip: |
| 586 | recovery_two_step_image.AddToZip(output_zip) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 587 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 588 | banner("system") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 589 | system_img_path = AddSystem( |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 590 | output_zip, recovery_img=recovery_image, boot_img=boot_image) |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 591 | vendor_img_path = None |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 592 | if has_vendor: |
| 593 | banner("vendor") |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 594 | vendor_img_path = AddVendor(output_zip) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 595 | if has_system_other: |
| 596 | banner("system_other") |
| 597 | AddSystemOther(output_zip) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 598 | if not OPTIONS.is_signing: |
| 599 | banner("userdata") |
| 600 | AddUserdata(output_zip) |
| 601 | banner("cache") |
| 602 | AddCache(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 603 | |
| 604 | if OPTIONS.info_dict.get("board_bpt_enable") == "true": |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 605 | banner("partition-table") |
| 606 | AddPartitionTable(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 607 | |
| 608 | dtbo_img_path = None |
| 609 | if OPTIONS.info_dict.get("has_dtbo") == "true": |
| 610 | banner("dtbo") |
| 611 | dtbo_img_path = AddDtbo(output_zip) |
| 612 | |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 613 | if OPTIONS.info_dict.get("avb_enable") == "true": |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 614 | banner("vbmeta") |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 615 | AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, |
| 616 | dtbo_img_path) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 617 | |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 618 | # For devices using A/B update, copy over images from RADIO/ and/or |
| 619 | # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed |
| 620 | # images ready under IMAGES/. All images should have '.img' as extension. |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 621 | banner("radio") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 622 | ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") |
| 623 | if os.path.exists(ab_partitions): |
| 624 | with open(ab_partitions, 'r') as f: |
| 625 | lines = f.readlines() |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 626 | # For devices using A/B update, generate care_map for system and vendor |
| 627 | # partitions (if present), then write this file to target_files package. |
| 628 | care_map_list = [] |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 629 | for line in lines: |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 630 | if line.strip() == "system" and ( |
| 631 | "system_verity_block_device" in OPTIONS.info_dict or |
Tao Bao | 3f72176 | 2017-06-29 15:11:44 -0700 | [diff] [blame] | 632 | OPTIONS.info_dict.get("avb_system_hashtree_enable") == "true"): |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 633 | assert os.path.exists(system_img_path) |
| 634 | care_map_list += GetCareMap("system", system_img_path) |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 635 | if line.strip() == "vendor" and ( |
| 636 | "vendor_verity_block_device" in OPTIONS.info_dict or |
Tao Bao | 3f72176 | 2017-06-29 15:11:44 -0700 | [diff] [blame] | 637 | OPTIONS.info_dict.get("avb_vendor_hashtree_enable") == "true"): |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 638 | assert os.path.exists(vendor_img_path) |
| 639 | care_map_list += GetCareMap("vendor", vendor_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 640 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 641 | img_name = line.strip() + ".img" |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 642 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 643 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 644 | print("%s already exists, no need to overwrite..." % (img_name,)) |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 645 | continue |
| 646 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 647 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 648 | img_vendor_dir = os.path.join( |
| 649 | OPTIONS.input_tmp, "VENDOR_IMAGES") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 650 | if os.path.exists(img_radio_path): |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 651 | if output_zip: |
| 652 | common.ZipWrite(output_zip, img_radio_path, |
| 653 | os.path.join("IMAGES", img_name)) |
| 654 | else: |
| 655 | shutil.copy(img_radio_path, prebuilt_path) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 656 | else: |
| 657 | for root, _, files in os.walk(img_vendor_dir): |
| 658 | if img_name in files: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 659 | if output_zip: |
| 660 | common.ZipWrite(output_zip, os.path.join(root, img_name), |
| 661 | os.path.join("IMAGES", img_name)) |
| 662 | else: |
| 663 | shutil.copy(os.path.join(root, img_name), prebuilt_path) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 664 | break |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 665 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 666 | if output_zip: |
| 667 | # Zip spec says: All slashes MUST be forward slashes. |
| 668 | img_path = 'IMAGES/' + img_name |
| 669 | assert img_path in output_zip.namelist(), "cannot find " + img_name |
| 670 | else: |
| 671 | img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 672 | assert os.path.exists(img_path), "cannot find " + img_name |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 673 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 674 | if care_map_list: |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 675 | care_map_path = "META/care_map.txt" |
| 676 | if output_zip and care_map_path not in output_zip.namelist(): |
| 677 | common.ZipWriteStr(output_zip, care_map_path, '\n'.join(care_map_list)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 678 | else: |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 679 | with open(os.path.join(OPTIONS.input_tmp, care_map_path), 'w') as fp: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 680 | fp.write('\n'.join(care_map_list)) |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 681 | if output_zip: |
| 682 | OPTIONS.replace_updated_files_list.append(care_map_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 683 | |
Tao Bao | 95a95c3 | 2017-06-16 15:30:23 -0700 | [diff] [blame] | 684 | # Radio images that need to be packed into IMAGES/, and product-img.zip. |
| 685 | pack_radioimages = os.path.join( |
| 686 | OPTIONS.input_tmp, "META", "pack_radioimages.txt") |
| 687 | if os.path.exists(pack_radioimages): |
| 688 | with open(pack_radioimages, 'r') as f: |
| 689 | lines = f.readlines() |
| 690 | for line in lines: |
| 691 | img_name = line.strip() |
| 692 | _, ext = os.path.splitext(img_name) |
| 693 | if not ext: |
| 694 | img_name += ".img" |
| 695 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 696 | if os.path.exists(prebuilt_path): |
| 697 | print("%s already exists, no need to overwrite..." % (img_name,)) |
| 698 | continue |
| 699 | |
| 700 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
| 701 | assert os.path.exists(img_radio_path), \ |
| 702 | "Failed to find %s at %s" % (img_name, img_radio_path) |
| 703 | if output_zip: |
| 704 | common.ZipWrite(output_zip, img_radio_path, |
| 705 | os.path.join("IMAGES", img_name)) |
| 706 | else: |
| 707 | shutil.copy(img_radio_path, prebuilt_path) |
| 708 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 709 | if output_zip: |
| 710 | common.ZipClose(output_zip) |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 711 | if OPTIONS.replace_updated_files_list: |
| 712 | ReplaceUpdatedFiles(output_zip.filename, |
| 713 | OPTIONS.replace_updated_files_list) |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 714 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 715 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 716 | def main(argv): |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 717 | def option_handler(o, a): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 718 | if o in ("-a", "--add_missing"): |
| 719 | OPTIONS.add_missing = True |
| 720 | elif o in ("-r", "--rebuild_recovery",): |
| 721 | OPTIONS.rebuild_recovery = True |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 722 | elif o == "--replace_verity_private_key": |
| 723 | OPTIONS.replace_verity_private_key = (True, a) |
| 724 | elif o == "--replace_verity_public_key": |
| 725 | OPTIONS.replace_verity_public_key = (True, a) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 726 | elif o == "--is_signing": |
| 727 | OPTIONS.is_signing = True |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 728 | else: |
| 729 | return False |
| 730 | return True |
| 731 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 732 | args = common.ParseOptions( |
| 733 | argv, __doc__, extra_opts="ar", |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 734 | extra_long_opts=["add_missing", "rebuild_recovery", |
| 735 | "replace_verity_public_key=", |
| 736 | "replace_verity_private_key=", |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 737 | "is_signing"], |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 738 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 739 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 740 | |
| 741 | if len(args) != 1: |
| 742 | common.Usage(__doc__) |
| 743 | sys.exit(1) |
| 744 | |
| 745 | AddImagesToTargetFiles(args[0]) |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 746 | print("done.") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 747 | |
| 748 | if __name__ == '__main__': |
| 749 | try: |
| 750 | common.CloseInheritedPipes() |
| 751 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 752 | except common.ExternalError as e: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 753 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 754 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 755 | finally: |
| 756 | common.Cleanup() |