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