Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2008 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, produces an OTA package that installs |
| 19 | that build. An incremental OTA is produced if -i is given, otherwise |
| 20 | a full OTA is produced. |
| 21 | |
| 22 | Usage: ota_from_target_files [flags] input_target_files output_ota_package |
| 23 | |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 24 | -k (--package_key) <key> Key to use to sign the package (default is |
| 25 | the value of default_system_dev_certificate from the input |
| 26 | target-files's META/misc_info.txt, or |
| 27 | "build/target/product/security/testkey" if that value is not |
| 28 | specified). |
| 29 | |
| 30 | For incremental OTAs, the default value is based on the source |
| 31 | target-file, not the target build. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 32 | |
| 33 | -i (--incremental_from) <file> |
| 34 | Generate an incremental OTA using the given target-files zip as |
| 35 | the starting build. |
| 36 | |
Tao Bao | 43078aa | 2015-04-21 14:32:35 -0700 | [diff] [blame] | 37 | --full_radio |
| 38 | When generating an incremental OTA, always include a full copy of |
| 39 | radio image. This option is only meaningful when -i is specified, |
| 40 | because a full radio is always included in a full OTA if applicable. |
| 41 | |
leozwang | aa6c1a1 | 2015-08-14 10:57:58 -0700 | [diff] [blame] | 42 | --full_bootloader |
| 43 | Similar to --full_radio. When generating an incremental OTA, always |
| 44 | include a full copy of bootloader image. |
| 45 | |
Tao Bao | edb35b8 | 2017-10-30 16:07:13 -0700 | [diff] [blame] | 46 | --verify |
| 47 | Remount and verify the checksums of the files written to the system and |
| 48 | vendor (if used) partitions. Non-A/B incremental OTAs only. |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 49 | |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame] | 50 | -o (--oem_settings) <main_file[,additional_files...]> |
| 51 | Comma seperated list of files used to specify the expected OEM-specific |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 52 | properties on the OEM partition of the intended device. Multiple expected |
| 53 | values can be used by providing multiple files. Only the first dict will |
| 54 | be used to compute fingerprint, while the rest will be used to assert |
| 55 | OEM-specific properties. |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame] | 56 | |
Tao Bao | 8608cde | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 57 | --oem_no_mount |
| 58 | For devices with OEM-specific properties but without an OEM partition, |
| 59 | do not mount the OEM partition in the updater-script. This should be |
| 60 | very rarely used, since it's expected to have a dedicated OEM partition |
| 61 | for OEM-specific properties. Only meaningful when -o is specified. |
| 62 | |
Tao Bao | 337633f | 2017-12-06 15:20:19 -0800 | [diff] [blame] | 63 | --wipe_user_data |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 64 | Generate an OTA package that will wipe the user data partition |
| 65 | when installed. |
| 66 | |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 67 | --downgrade |
| 68 | Intentionally generate an incremental OTA that updates from a newer |
| 69 | build to an older one (based on timestamp comparison). "post-timestamp" |
| 70 | will be replaced by "ota-downgrade=yes" in the metadata file. A data |
| 71 | wipe will always be enforced, so "ota-wipe=yes" will also be included in |
Tao Bao | 4996cf0 | 2016-03-08 17:53:39 -0800 | [diff] [blame] | 72 | the metadata file. The update-binary in the source build will be used in |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 73 | the OTA package, unless --binary flag is specified. Please also check the |
| 74 | doc for --override_timestamp below. |
| 75 | |
| 76 | --override_timestamp |
| 77 | Intentionally generate an incremental OTA that updates from a newer |
| 78 | build to an older one (based on timestamp comparison), by overriding the |
| 79 | timestamp in package metadata. This differs from --downgrade flag: we |
| 80 | know for sure this is NOT an actual downgrade case, but two builds are |
| 81 | cut in a reverse order. A legit use case is that we cut a new build C |
| 82 | (after having A and B), but want to enfore an update path of A -> C -> B. |
| 83 | Specifying --downgrade may not help since that would enforce a data wipe |
| 84 | for C -> B update. The value of "post-timestamp" will be set to the newer |
| 85 | timestamp plus one, so that the package can be pushed and applied. |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 86 | |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 87 | -e (--extra_script) <file> |
| 88 | Insert the contents of file at the end of the update script. |
| 89 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 90 | -2 (--two_step) |
| 91 | Generate a 'two-step' OTA package, where recovery is updated |
| 92 | first, so that any changes made to the system partition are done |
| 93 | using the new recovery (new kernel, etc.). |
| 94 | |
Doug Zongker | 26e6619 | 2014-02-20 13:22:07 -0800 | [diff] [blame] | 95 | --block |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 96 | Generate a block-based OTA for non-A/B device. We have deprecated the |
| 97 | support for file-based OTA since O. Block-based OTA will be used by |
| 98 | default for all non-A/B devices. Keeping this flag here to not break |
| 99 | existing callers. |
Doug Zongker | 26e6619 | 2014-02-20 13:22:07 -0800 | [diff] [blame] | 100 | |
Doug Zongker | 2556848 | 2014-03-03 10:21:27 -0800 | [diff] [blame] | 101 | -b (--binary) <file> |
| 102 | Use the given binary as the update-binary in the output package, |
| 103 | instead of the binary in the build's target_files. Use for |
| 104 | development only. |
| 105 | |
Martin Blumenstingl | 374e114 | 2014-05-31 20:42:55 +0200 | [diff] [blame] | 106 | -t (--worker_threads) <int> |
| 107 | Specifies the number of worker-threads that will be used when |
| 108 | generating patches for incremental updates (defaults to 3). |
| 109 | |
Tao Bao | 8dcf738 | 2015-05-21 14:09:49 -0700 | [diff] [blame] | 110 | --stash_threshold <float> |
| 111 | Specifies the threshold that will be used to compute the maximum |
| 112 | allowed stash size (defaults to 0.8). |
Tao Bao | 9bc6bb2 | 2015-11-09 16:58:28 -0800 | [diff] [blame] | 113 | |
Tao Bao | d62c603 | 2015-11-30 09:40:20 -0800 | [diff] [blame] | 114 | --log_diff <file> |
| 115 | Generate a log file that shows the differences in the source and target |
| 116 | builds for an incremental package. This option is only meaningful when |
| 117 | -i is specified. |
Tao Bao | dea0f8b | 2016-06-20 17:55:06 -0700 | [diff] [blame] | 118 | |
| 119 | --payload_signer <signer> |
| 120 | Specify the signer when signing the payload and metadata for A/B OTAs. |
| 121 | By default (i.e. without this flag), it calls 'openssl pkeyutl' to sign |
| 122 | with the package private key. If the private key cannot be accessed |
| 123 | directly, a payload signer that knows how to do that should be specified. |
| 124 | The signer will be supplied with "-inkey <path_to_key>", |
| 125 | "-in <input_file>" and "-out <output_file>" parameters. |
Baligh Uddin | 2abbbd0 | 2016-06-22 12:14:16 -0700 | [diff] [blame] | 126 | |
| 127 | --payload_signer_args <args> |
| 128 | Specify the arguments needed for payload signer. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 129 | """ |
| 130 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 131 | from __future__ import print_function |
| 132 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 133 | import multiprocessing |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 134 | import os.path |
Baligh Uddin | 2abbbd0 | 2016-06-22 12:14:16 -0700 | [diff] [blame] | 135 | import shlex |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 136 | import subprocess |
| 137 | import sys |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 138 | import tempfile |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 139 | import zipfile |
| 140 | |
| 141 | import common |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 142 | import edify_generator |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 143 | import sparse_img |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 144 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 145 | if sys.hexversion < 0x02070000: |
| 146 | print("Python 2.7 or newer is required.", file=sys.stderr) |
| 147 | sys.exit(1) |
| 148 | |
| 149 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 150 | OPTIONS = common.OPTIONS |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 151 | OPTIONS.package_key = None |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 152 | OPTIONS.incremental_source = None |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 153 | OPTIONS.verify = False |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 154 | OPTIONS.patch_threshold = 0.95 |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 155 | OPTIONS.wipe_user_data = False |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 156 | OPTIONS.downgrade = False |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 157 | OPTIONS.timestamp = False |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 158 | OPTIONS.extra_script = None |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 159 | OPTIONS.worker_threads = multiprocessing.cpu_count() // 2 |
| 160 | if OPTIONS.worker_threads == 0: |
| 161 | OPTIONS.worker_threads = 1 |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 162 | OPTIONS.two_step = False |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 163 | OPTIONS.no_signing = False |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 164 | OPTIONS.block_based = True |
Doug Zongker | 2556848 | 2014-03-03 10:21:27 -0800 | [diff] [blame] | 165 | OPTIONS.updater_binary = None |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 166 | OPTIONS.oem_source = None |
Tao Bao | 8608cde | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 167 | OPTIONS.oem_no_mount = False |
Tao Bao | 43078aa | 2015-04-21 14:32:35 -0700 | [diff] [blame] | 168 | OPTIONS.full_radio = False |
leozwang | aa6c1a1 | 2015-08-14 10:57:58 -0700 | [diff] [blame] | 169 | OPTIONS.full_bootloader = False |
Tao Bao | d47d8e1 | 2015-05-21 14:09:49 -0700 | [diff] [blame] | 170 | # Stash size cannot exceed cache_size * threshold. |
| 171 | OPTIONS.cache_size = None |
| 172 | OPTIONS.stash_threshold = 0.8 |
Tao Bao | d62c603 | 2015-11-30 09:40:20 -0800 | [diff] [blame] | 173 | OPTIONS.log_diff = None |
Tao Bao | dea0f8b | 2016-06-20 17:55:06 -0700 | [diff] [blame] | 174 | OPTIONS.payload_signer = None |
Baligh Uddin | 2abbbd0 | 2016-06-22 12:14:16 -0700 | [diff] [blame] | 175 | OPTIONS.payload_signer_args = [] |
Tao Bao | 5f8ff93 | 2017-03-21 22:35:00 -0700 | [diff] [blame] | 176 | OPTIONS.extracted_input = None |
Christian Oder | f63e2cd | 2017-05-01 22:30:15 +0200 | [diff] [blame] | 177 | OPTIONS.key_passwords = [] |
Tao Bao | 8dcf738 | 2015-05-21 14:09:49 -0700 | [diff] [blame] | 178 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 179 | METADATA_NAME = 'META-INF/com/android/metadata' |
Tao Bao | 6b0b2f9 | 2017-03-05 11:38:11 -0800 | [diff] [blame] | 180 | UNZIP_PATTERN = ['IMAGES/*', 'META/*'] |
| 181 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 182 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 183 | class BuildInfo(object): |
| 184 | """A class that holds the information for a given build. |
| 185 | |
| 186 | This class wraps up the property querying for a given source or target build. |
| 187 | It abstracts away the logic of handling OEM-specific properties, and caches |
| 188 | the commonly used properties such as fingerprint. |
| 189 | |
| 190 | There are two types of info dicts: a) build-time info dict, which is generated |
| 191 | at build time (i.e. included in a target_files zip); b) OEM info dict that is |
| 192 | specified at package generation time (via command line argument |
| 193 | '--oem_settings'). If a build doesn't use OEM-specific properties (i.e. not |
| 194 | having "oem_fingerprint_properties" in build-time info dict), all the queries |
| 195 | would be answered based on build-time info dict only. Otherwise if using |
| 196 | OEM-specific properties, some of them will be calculated from two info dicts. |
| 197 | |
| 198 | Users can query properties similarly as using a dict() (e.g. info['fstab']), |
| 199 | or to query build properties via GetBuildProp() or GetVendorBuildProp(). |
| 200 | |
| 201 | Attributes: |
| 202 | info_dict: The build-time info dict. |
| 203 | is_ab: Whether it's a build that uses A/B OTA. |
| 204 | oem_dicts: A list of OEM dicts. |
| 205 | oem_props: A list of OEM properties that should be read from OEM dicts; None |
| 206 | if the build doesn't use any OEM-specific property. |
| 207 | fingerprint: The fingerprint of the build, which would be calculated based |
| 208 | on OEM properties if applicable. |
| 209 | device: The device name, which could come from OEM dicts if applicable. |
| 210 | """ |
| 211 | |
| 212 | def __init__(self, info_dict, oem_dicts): |
| 213 | """Initializes a BuildInfo instance with the given dicts. |
| 214 | |
| 215 | Arguments: |
| 216 | info_dict: The build-time info dict. |
| 217 | oem_dicts: A list of OEM dicts (which is parsed from --oem_settings). Note |
| 218 | that it always uses the first dict to calculate the fingerprint or the |
| 219 | device name. The rest would be used for asserting OEM properties only |
| 220 | (e.g. one package can be installed on one of these devices). |
| 221 | """ |
| 222 | self.info_dict = info_dict |
| 223 | self.oem_dicts = oem_dicts |
| 224 | |
| 225 | self._is_ab = info_dict.get("ab_update") == "true" |
| 226 | self._oem_props = info_dict.get("oem_fingerprint_properties") |
| 227 | |
| 228 | if self._oem_props: |
| 229 | assert oem_dicts, "OEM source required for this build" |
| 230 | |
| 231 | # These two should be computed only after setting self._oem_props. |
| 232 | self._device = self.GetOemProperty("ro.product.device") |
| 233 | self._fingerprint = self.CalculateFingerprint() |
| 234 | |
| 235 | @property |
| 236 | def is_ab(self): |
| 237 | return self._is_ab |
| 238 | |
| 239 | @property |
| 240 | def device(self): |
| 241 | return self._device |
| 242 | |
| 243 | @property |
| 244 | def fingerprint(self): |
| 245 | return self._fingerprint |
| 246 | |
| 247 | @property |
| 248 | def oem_props(self): |
| 249 | return self._oem_props |
| 250 | |
| 251 | def __getitem__(self, key): |
| 252 | return self.info_dict[key] |
| 253 | |
| 254 | def get(self, key, default=None): |
| 255 | return self.info_dict.get(key, default) |
| 256 | |
| 257 | def GetBuildProp(self, prop): |
| 258 | """Returns the inquired build property.""" |
| 259 | try: |
| 260 | return self.info_dict.get("build.prop", {})[prop] |
| 261 | except KeyError: |
| 262 | raise common.ExternalError("couldn't find %s in build.prop" % (prop,)) |
| 263 | |
| 264 | def GetVendorBuildProp(self, prop): |
| 265 | """Returns the inquired vendor build property.""" |
| 266 | try: |
| 267 | return self.info_dict.get("vendor.build.prop", {})[prop] |
| 268 | except KeyError: |
| 269 | raise common.ExternalError( |
| 270 | "couldn't find %s in vendor.build.prop" % (prop,)) |
| 271 | |
| 272 | def GetOemProperty(self, key): |
| 273 | if self.oem_props is not None and key in self.oem_props: |
| 274 | return self.oem_dicts[0][key] |
| 275 | return self.GetBuildProp(key) |
| 276 | |
| 277 | def CalculateFingerprint(self): |
| 278 | if self.oem_props is None: |
| 279 | return self.GetBuildProp("ro.build.fingerprint") |
| 280 | return "%s/%s/%s:%s" % ( |
| 281 | self.GetOemProperty("ro.product.brand"), |
| 282 | self.GetOemProperty("ro.product.name"), |
| 283 | self.GetOemProperty("ro.product.device"), |
| 284 | self.GetBuildProp("ro.build.thumbprint")) |
| 285 | |
| 286 | def WriteMountOemScript(self, script): |
| 287 | assert self.oem_props is not None |
| 288 | recovery_mount_options = self.info_dict.get("recovery_mount_options") |
| 289 | script.Mount("/oem", recovery_mount_options) |
| 290 | |
| 291 | def WriteDeviceAssertions(self, script, oem_no_mount): |
| 292 | # Read the property directly if not using OEM properties. |
| 293 | if not self.oem_props: |
| 294 | script.AssertDevice(self.device) |
| 295 | return |
| 296 | |
| 297 | # Otherwise assert OEM properties. |
| 298 | if not self.oem_dicts: |
| 299 | raise common.ExternalError( |
| 300 | "No OEM file provided to answer expected assertions") |
| 301 | |
| 302 | for prop in self.oem_props.split(): |
| 303 | values = [] |
| 304 | for oem_dict in self.oem_dicts: |
| 305 | if prop in oem_dict: |
| 306 | values.append(oem_dict[prop]) |
| 307 | if not values: |
| 308 | raise common.ExternalError( |
| 309 | "The OEM file is missing the property %s" % (prop,)) |
| 310 | script.AssertOemProperty(prop, values, oem_no_mount) |
| 311 | |
| 312 | |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 313 | class PayloadSigner(object): |
| 314 | """A class that wraps the payload signing works. |
| 315 | |
| 316 | When generating a Payload, hashes of the payload and metadata files will be |
| 317 | signed with the device key, either by calling an external payload signer or |
| 318 | by calling openssl with the package key. This class provides a unified |
| 319 | interface, so that callers can just call PayloadSigner.Sign(). |
| 320 | |
| 321 | If an external payload signer has been specified (OPTIONS.payload_signer), it |
| 322 | calls the signer with the provided args (OPTIONS.payload_signer_args). Note |
| 323 | that the signing key should be provided as part of the payload_signer_args. |
| 324 | Otherwise without an external signer, it uses the package key |
| 325 | (OPTIONS.package_key) and calls openssl for the signing works. |
| 326 | """ |
| 327 | |
| 328 | def __init__(self): |
| 329 | if OPTIONS.payload_signer is None: |
| 330 | # Prepare the payload signing key. |
| 331 | private_key = OPTIONS.package_key + OPTIONS.private_key_suffix |
| 332 | pw = OPTIONS.key_passwords[OPTIONS.package_key] |
| 333 | |
| 334 | cmd = ["openssl", "pkcs8", "-in", private_key, "-inform", "DER"] |
| 335 | cmd.extend(["-passin", "pass:" + pw] if pw else ["-nocrypt"]) |
| 336 | signing_key = common.MakeTempFile(prefix="key-", suffix=".key") |
| 337 | cmd.extend(["-out", signing_key]) |
| 338 | |
| 339 | get_signing_key = common.Run(cmd, verbose=False, stdout=subprocess.PIPE, |
| 340 | stderr=subprocess.STDOUT) |
| 341 | stdoutdata, _ = get_signing_key.communicate() |
| 342 | assert get_signing_key.returncode == 0, \ |
| 343 | "Failed to get signing key: {}".format(stdoutdata) |
| 344 | |
| 345 | self.signer = "openssl" |
| 346 | self.signer_args = ["pkeyutl", "-sign", "-inkey", signing_key, |
| 347 | "-pkeyopt", "digest:sha256"] |
| 348 | else: |
| 349 | self.signer = OPTIONS.payload_signer |
| 350 | self.signer_args = OPTIONS.payload_signer_args |
| 351 | |
| 352 | def Sign(self, in_file): |
| 353 | """Signs the given input file. Returns the output filename.""" |
| 354 | out_file = common.MakeTempFile(prefix="signed-", suffix=".bin") |
| 355 | cmd = [self.signer] + self.signer_args + ['-in', in_file, '-out', out_file] |
| 356 | signing = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 357 | stdoutdata, _ = signing.communicate() |
| 358 | assert signing.returncode == 0, \ |
| 359 | "Failed to sign the input file: {}".format(stdoutdata) |
| 360 | return out_file |
| 361 | |
| 362 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 363 | def SignOutput(temp_zip_name, output_zip_name): |
Christian Oder | f63e2cd | 2017-05-01 22:30:15 +0200 | [diff] [blame] | 364 | pw = OPTIONS.key_passwords[OPTIONS.package_key] |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 365 | |
Doug Zongker | 951495f | 2009-08-14 12:44:19 -0700 | [diff] [blame] | 366 | common.SignFile(temp_zip_name, output_zip_name, OPTIONS.package_key, pw, |
| 367 | whole_file=True) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 368 | |
| 369 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 370 | def _LoadOemDicts(oem_source): |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame] | 371 | """Returns the list of loaded OEM properties dict.""" |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 372 | if not oem_source: |
| 373 | return None |
| 374 | |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame] | 375 | oem_dicts = [] |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 376 | for oem_file in oem_source: |
| 377 | with open(oem_file) as fp: |
| 378 | oem_dicts.append(common.LoadDictionaryFromLines(fp.readlines())) |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame] | 379 | return oem_dicts |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 380 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 381 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 382 | def _WriteRecoveryImageToBoot(script, output_zip): |
| 383 | """Find and write recovery image to /boot in two-step OTA. |
| 384 | |
| 385 | In two-step OTAs, we write recovery image to /boot as the first step so that |
| 386 | we can reboot to there and install a new recovery image to /recovery. |
| 387 | A special "recovery-two-step.img" will be preferred, which encodes the correct |
| 388 | path of "/boot". Otherwise the device may show "device is corrupt" message |
| 389 | when booting into /boot. |
| 390 | |
| 391 | Fall back to using the regular recovery.img if the two-step recovery image |
| 392 | doesn't exist. Note that rebuilding the special image at this point may be |
| 393 | infeasible, because we don't have the desired boot signer and keys when |
| 394 | calling ota_from_target_files.py. |
| 395 | """ |
| 396 | |
| 397 | recovery_two_step_img_name = "recovery-two-step.img" |
| 398 | recovery_two_step_img_path = os.path.join( |
| 399 | OPTIONS.input_tmp, "IMAGES", recovery_two_step_img_name) |
| 400 | if os.path.exists(recovery_two_step_img_path): |
| 401 | recovery_two_step_img = common.GetBootableImage( |
| 402 | recovery_two_step_img_name, recovery_two_step_img_name, |
| 403 | OPTIONS.input_tmp, "RECOVERY") |
| 404 | common.ZipWriteStr( |
| 405 | output_zip, recovery_two_step_img_name, recovery_two_step_img.data) |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 406 | print("two-step package: using %s in stage 1/3" % ( |
| 407 | recovery_two_step_img_name,)) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 408 | script.WriteRawImage("/boot", recovery_two_step_img_name) |
| 409 | else: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 410 | print("two-step package: using recovery.img in stage 1/3") |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 411 | # The "recovery.img" entry has been written into package earlier. |
| 412 | script.WriteRawImage("/boot", "recovery.img") |
| 413 | |
| 414 | |
Doug Zongker | c925382 | 2014-02-04 12:17:58 -0800 | [diff] [blame] | 415 | def HasRecoveryPatch(target_files_zip): |
Tao Bao | f2cffbd | 2015-07-22 12:33:18 -0700 | [diff] [blame] | 416 | namelist = [name for name in target_files_zip.namelist()] |
| 417 | return ("SYSTEM/recovery-from-boot.p" in namelist or |
| 418 | "SYSTEM/etc/recovery.img" in namelist) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 419 | |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 420 | |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 421 | def HasVendorPartition(target_files_zip): |
| 422 | try: |
| 423 | target_files_zip.getinfo("VENDOR/") |
| 424 | return True |
| 425 | except KeyError: |
| 426 | return False |
| 427 | |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 428 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 429 | def HasTrebleEnabled(target_files_zip, target_info): |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 430 | return (HasVendorPartition(target_files_zip) and |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 431 | target_info.GetBuildProp("ro.treble.enabled") == "true") |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 432 | |
| 433 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 434 | def WriteFingerprintAssertion(script, target_info, source_info): |
| 435 | source_oem_props = source_info.oem_props |
| 436 | target_oem_props = target_info.oem_props |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 437 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 438 | if source_oem_props is None and target_oem_props is None: |
| 439 | script.AssertSomeFingerprint( |
| 440 | source_info.fingerprint, target_info.fingerprint) |
| 441 | elif source_oem_props is not None and target_oem_props is not None: |
| 442 | script.AssertSomeThumbprint( |
| 443 | target_info.GetBuildProp("ro.build.thumbprint"), |
| 444 | source_info.GetBuildProp("ro.build.thumbprint")) |
| 445 | elif source_oem_props is None and target_oem_props is not None: |
| 446 | script.AssertFingerprintOrThumbprint( |
| 447 | source_info.fingerprint, |
| 448 | target_info.GetBuildProp("ro.build.thumbprint")) |
| 449 | else: |
| 450 | script.AssertFingerprintOrThumbprint( |
| 451 | target_info.fingerprint, |
| 452 | source_info.GetBuildProp("ro.build.thumbprint")) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 453 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 454 | |
Tao Bao | 7e0f160 | 2017-03-06 15:50:08 -0800 | [diff] [blame] | 455 | def GetImage(which, tmpdir): |
| 456 | """Returns an image object suitable for passing to BlockImageDiff. |
| 457 | |
| 458 | 'which' partition must be "system" or "vendor". A prebuilt image and file |
| 459 | map must already exist in tmpdir. |
| 460 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 461 | |
| 462 | assert which in ("system", "vendor") |
| 463 | |
| 464 | path = os.path.join(tmpdir, "IMAGES", which + ".img") |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 465 | mappath = os.path.join(tmpdir, "IMAGES", which + ".map") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 466 | |
Tao Bao | 7e0f160 | 2017-03-06 15:50:08 -0800 | [diff] [blame] | 467 | # The image and map files must have been created prior to calling |
| 468 | # ota_from_target_files.py (since LMP). |
| 469 | assert os.path.exists(path) and os.path.exists(mappath) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 470 | |
Tao Bao | ff77781 | 2015-05-12 11:42:31 -0700 | [diff] [blame] | 471 | # Bug: http://b/20939131 |
| 472 | # In ext4 filesystems, block 0 might be changed even being mounted |
| 473 | # R/O. We add it to clobbered_blocks so that it will be written to the |
| 474 | # target unconditionally. Note that they are still part of care_map. |
| 475 | clobbered_blocks = "0" |
| 476 | |
| 477 | return sparse_img.SparseImage(path, mappath, clobbered_blocks) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 478 | |
| 479 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 480 | def AddCompatibilityArchiveIfTrebleEnabled(target_zip, output_zip, target_info, |
| 481 | source_info=None): |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 482 | """Adds compatibility info into the output zip if it's Treble-enabled target. |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 483 | |
| 484 | Metadata used for on-device compatibility verification is retrieved from |
| 485 | target_zip then added to compatibility.zip which is added to the output_zip |
| 486 | archive. |
| 487 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 488 | Compatibility archive should only be included for devices that have enabled |
| 489 | Treble support. |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 490 | |
| 491 | Args: |
| 492 | target_zip: Zip file containing the source files to be included for OTA. |
| 493 | output_zip: Zip file that will be sent for OTA. |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 494 | target_info: The BuildInfo instance that holds the target build info. |
| 495 | source_info: The BuildInfo instance that holds the source build info, if |
| 496 | generating an incremental OTA; None otherwise. |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 497 | """ |
| 498 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 499 | def AddCompatibilityArchive(system_updated, vendor_updated): |
| 500 | """Adds compatibility info based on system/vendor update status. |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 501 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 502 | Args: |
| 503 | system_updated: If True, the system image will be updated and therefore |
| 504 | its metadata should be included. |
| 505 | vendor_updated: If True, the vendor image will be updated and therefore |
| 506 | its metadata should be included. |
| 507 | """ |
| 508 | # Determine what metadata we need. Files are names relative to META/. |
| 509 | compatibility_files = [] |
| 510 | vendor_metadata = ("vendor_manifest.xml", "vendor_matrix.xml") |
| 511 | system_metadata = ("system_manifest.xml", "system_matrix.xml") |
| 512 | if vendor_updated: |
| 513 | compatibility_files += vendor_metadata |
| 514 | if system_updated: |
| 515 | compatibility_files += system_metadata |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 516 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 517 | # Create new archive. |
| 518 | compatibility_archive = tempfile.NamedTemporaryFile() |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 519 | compatibility_archive_zip = zipfile.ZipFile( |
| 520 | compatibility_archive, "w", compression=zipfile.ZIP_DEFLATED) |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 521 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 522 | # Add metadata. |
| 523 | for file_name in compatibility_files: |
| 524 | target_file_name = "META/" + file_name |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 525 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 526 | if target_file_name in target_zip.namelist(): |
| 527 | data = target_zip.read(target_file_name) |
| 528 | common.ZipWriteStr(compatibility_archive_zip, file_name, data) |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 529 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 530 | # Ensure files are written before we copy into output_zip. |
| 531 | compatibility_archive_zip.close() |
| 532 | |
| 533 | # Only add the archive if we have any compatibility info. |
| 534 | if compatibility_archive_zip.namelist(): |
| 535 | common.ZipWrite(output_zip, compatibility_archive.name, |
| 536 | arcname="compatibility.zip", |
| 537 | compress_type=zipfile.ZIP_STORED) |
| 538 | |
| 539 | # Will only proceed if the target has enabled the Treble support (as well as |
| 540 | # having a /vendor partition). |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 541 | if not HasTrebleEnabled(target_zip, target_info): |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 542 | return |
| 543 | |
| 544 | # We don't support OEM thumbprint in Treble world (which calculates |
| 545 | # fingerprints in a different way as shown in CalculateFingerprint()). |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 546 | assert not target_info.oem_props |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 547 | |
| 548 | # Full OTA carries the info for system/vendor both. |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 549 | if source_info is None: |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 550 | AddCompatibilityArchive(True, True) |
| 551 | return |
| 552 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 553 | assert not source_info.oem_props |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 554 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 555 | source_fp = source_info.fingerprint |
| 556 | target_fp = target_info.fingerprint |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 557 | system_updated = source_fp != target_fp |
| 558 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 559 | source_fp_vendor = source_info.GetVendorBuildProp( |
| 560 | "ro.vendor.build.fingerprint") |
| 561 | target_fp_vendor = target_info.GetVendorBuildProp( |
| 562 | "ro.vendor.build.fingerprint") |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 563 | vendor_updated = source_fp_vendor != target_fp_vendor |
| 564 | |
| 565 | AddCompatibilityArchive(system_updated, vendor_updated) |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 566 | |
| 567 | |
Doug Zongker | c77a9ad | 2010-09-16 11:28:43 -0700 | [diff] [blame] | 568 | def WriteFullOTAPackage(input_zip, output_zip): |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 569 | target_info = BuildInfo(OPTIONS.info_dict, OPTIONS.oem_dicts) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 570 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 571 | # We don't know what version it will be installed on top of. We expect the API |
| 572 | # just won't change very often. Similarly for fstab, it might have changed in |
| 573 | # the target build. |
| 574 | target_api_version = target_info["recovery_api_version"] |
| 575 | script = edify_generator.EdifyGenerator(target_api_version, target_info) |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 576 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 577 | if target_info.oem_props and not OPTIONS.oem_no_mount: |
| 578 | target_info.WriteMountOemScript(script) |
| 579 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 580 | metadata = GetPackageMetadata(target_info) |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 581 | |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 582 | device_specific = common.DeviceSpecificParams( |
| 583 | input_zip=input_zip, |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 584 | input_version=target_api_version, |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 585 | output_zip=output_zip, |
| 586 | script=script, |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 587 | input_tmp=OPTIONS.input_tmp, |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 588 | metadata=metadata, |
| 589 | info_dict=OPTIONS.info_dict) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 590 | |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 591 | assert HasRecoveryPatch(input_zip) |
Doug Zongker | c925382 | 2014-02-04 12:17:58 -0800 | [diff] [blame] | 592 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 593 | # Assertions (e.g. downgrade check, device properties check). |
| 594 | ts = target_info.GetBuildProp("ro.build.date.utc") |
| 595 | ts_text = target_info.GetBuildProp("ro.build.date") |
Elliott Hughes | d8a52f9 | 2016-06-20 14:35:47 -0700 | [diff] [blame] | 596 | script.AssertOlderBuild(ts, ts_text) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 597 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 598 | target_info.WriteDeviceAssertions(script, OPTIONS.oem_no_mount) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 599 | device_specific.FullOTA_Assertions() |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 600 | |
| 601 | # Two-step package strategy (in chronological order, which is *not* |
| 602 | # the order in which the generated script has things): |
| 603 | # |
| 604 | # if stage is not "2/3" or "3/3": |
| 605 | # write recovery image to boot partition |
| 606 | # set stage to "2/3" |
| 607 | # reboot to boot partition and restart recovery |
| 608 | # else if stage is "2/3": |
| 609 | # write recovery image to recovery partition |
| 610 | # set stage to "3/3" |
| 611 | # reboot to recovery partition and restart recovery |
| 612 | # else: |
| 613 | # (stage must be "3/3") |
| 614 | # set stage to "" |
| 615 | # do normal full package installation: |
| 616 | # wipe and install system, boot image, etc. |
| 617 | # set up system to update recovery partition on first boot |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 618 | # complete script normally |
| 619 | # (allow recovery to mark itself finished and reboot) |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 620 | |
| 621 | recovery_img = common.GetBootableImage("recovery.img", "recovery.img", |
| 622 | OPTIONS.input_tmp, "RECOVERY") |
| 623 | if OPTIONS.two_step: |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 624 | if not target_info.get("multistage_support"): |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 625 | assert False, "two-step packages not supported by this build" |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 626 | fs = target_info["fstab"]["/misc"] |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 627 | assert fs.fs_type.upper() == "EMMC", \ |
| 628 | "two-step packages only supported on devices with EMMC /misc partitions" |
| 629 | bcb_dev = {"bcb_dev": fs.device} |
| 630 | common.ZipWriteStr(output_zip, "recovery.img", recovery_img.data) |
| 631 | script.AppendExtra(""" |
Michael Runge | fb8886d | 2014-10-23 13:51:04 -0700 | [diff] [blame] | 632 | if get_stage("%(bcb_dev)s") == "2/3" then |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 633 | """ % bcb_dev) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 634 | |
| 635 | # Stage 2/3: Write recovery image to /recovery (currently running /boot). |
| 636 | script.Comment("Stage 2/3") |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 637 | script.WriteRawImage("/recovery", "recovery.img") |
| 638 | script.AppendExtra(""" |
| 639 | set_stage("%(bcb_dev)s", "3/3"); |
| 640 | reboot_now("%(bcb_dev)s", "recovery"); |
Michael Runge | fb8886d | 2014-10-23 13:51:04 -0700 | [diff] [blame] | 641 | else if get_stage("%(bcb_dev)s") == "3/3" then |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 642 | """ % bcb_dev) |
| 643 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 644 | # Stage 3/3: Make changes. |
| 645 | script.Comment("Stage 3/3") |
| 646 | |
Tao Bao | 6c55a8a | 2015-04-08 15:30:27 -0700 | [diff] [blame] | 647 | # Dump fingerprints |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 648 | script.Print("Target: {}".format(target_info.fingerprint)) |
Tao Bao | 6c55a8a | 2015-04-08 15:30:27 -0700 | [diff] [blame] | 649 | |
Doug Zongker | e5ff590 | 2012-01-17 10:55:37 -0800 | [diff] [blame] | 650 | device_specific.FullOTA_InstallBegin() |
Doug Zongker | 171f1cd | 2009-06-15 22:36:37 -0700 | [diff] [blame] | 651 | |
Doug Zongker | 01ce19c | 2014-02-04 13:48:15 -0800 | [diff] [blame] | 652 | system_progress = 0.75 |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 653 | |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 654 | if OPTIONS.wipe_user_data: |
Doug Zongker | 01ce19c | 2014-02-04 13:48:15 -0800 | [diff] [blame] | 655 | system_progress -= 0.1 |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 656 | if HasVendorPartition(input_zip): |
| 657 | system_progress -= 0.1 |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 658 | |
Doug Zongker | 4b9596f | 2014-06-09 14:15:45 -0700 | [diff] [blame] | 659 | script.ShowProgress(system_progress, 0) |
Jesse Zhao | 75bcea0 | 2015-01-06 10:59:53 -0800 | [diff] [blame] | 660 | |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 661 | # Full OTA is done as an "incremental" against an empty source image. This |
| 662 | # has the effect of writing new data from the package to the entire |
| 663 | # partition, but lets us reuse the updater code that writes incrementals to |
| 664 | # do it. |
| 665 | system_tgt = GetImage("system", OPTIONS.input_tmp) |
| 666 | system_tgt.ResetFileMap() |
| 667 | system_diff = common.BlockDifference("system", system_tgt, src=None) |
| 668 | system_diff.WriteScript(script, output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 669 | |
Tao Bao | 7a5bf8a | 2015-07-21 18:01:20 -0700 | [diff] [blame] | 670 | boot_img = common.GetBootableImage( |
| 671 | "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
Doug Zongker | c925382 | 2014-02-04 12:17:58 -0800 | [diff] [blame] | 672 | |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 673 | if HasVendorPartition(input_zip): |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 674 | script.ShowProgress(0.1, 0) |
| 675 | |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 676 | vendor_tgt = GetImage("vendor", OPTIONS.input_tmp) |
| 677 | vendor_tgt.ResetFileMap() |
| 678 | vendor_diff = common.BlockDifference("vendor", vendor_tgt) |
| 679 | vendor_diff.WriteScript(script, output_zip) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 680 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 681 | AddCompatibilityArchiveIfTrebleEnabled(input_zip, output_zip, target_info) |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 682 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 683 | common.CheckSize(boot_img.data, "boot.img", target_info) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 684 | common.ZipWriteStr(output_zip, "boot.img", boot_img.data) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 685 | |
Doug Zongker | 01ce19c | 2014-02-04 13:48:15 -0800 | [diff] [blame] | 686 | script.ShowProgress(0.05, 5) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 687 | script.WriteRawImage("/boot", "boot.img") |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 688 | |
Doug Zongker | 01ce19c | 2014-02-04 13:48:15 -0800 | [diff] [blame] | 689 | script.ShowProgress(0.2, 10) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 690 | device_specific.FullOTA_InstallEnd() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 691 | |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 692 | if OPTIONS.extra_script is not None: |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 693 | script.AppendExtra(OPTIONS.extra_script) |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 694 | |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 695 | script.UnmountAll() |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 696 | |
Doug Zongker | 922206e | 2014-03-04 13:16:24 -0800 | [diff] [blame] | 697 | if OPTIONS.wipe_user_data: |
| 698 | script.ShowProgress(0.1, 10) |
| 699 | script.FormatPartition("/data") |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 700 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 701 | if OPTIONS.two_step: |
| 702 | script.AppendExtra(""" |
| 703 | set_stage("%(bcb_dev)s", ""); |
| 704 | """ % bcb_dev) |
| 705 | script.AppendExtra("else\n") |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 706 | |
| 707 | # Stage 1/3: Nothing to verify for full OTA. Write recovery image to /boot. |
| 708 | script.Comment("Stage 1/3") |
| 709 | _WriteRecoveryImageToBoot(script, output_zip) |
| 710 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 711 | script.AppendExtra(""" |
| 712 | set_stage("%(bcb_dev)s", "2/3"); |
| 713 | reboot_now("%(bcb_dev)s", ""); |
| 714 | endif; |
| 715 | endif; |
| 716 | """ % bcb_dev) |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 717 | |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 718 | script.SetProgress(1) |
| 719 | script.AddToZip(input_zip, output_zip, input_path=OPTIONS.updater_binary) |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 720 | metadata["ota-required-cache"] = str(script.required_cache) |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 721 | WriteMetadata(metadata, output_zip) |
| 722 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 723 | |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 724 | def WriteMetadata(metadata, output_zip): |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 725 | value = "".join(["%s=%s\n" % kv for kv in sorted(metadata.iteritems())]) |
| 726 | common.ZipWriteStr(output_zip, METADATA_NAME, value, |
| 727 | compress_type=zipfile.ZIP_STORED) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 728 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 729 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 730 | def HandleDowngradeMetadata(metadata, target_info, source_info): |
Tao Bao | b31892e | 2017-02-07 11:21:17 -0800 | [diff] [blame] | 731 | # Only incremental OTAs are allowed to reach here. |
| 732 | assert OPTIONS.incremental_source is not None |
| 733 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 734 | post_timestamp = target_info.GetBuildProp("ro.build.date.utc") |
| 735 | pre_timestamp = source_info.GetBuildProp("ro.build.date.utc") |
Tao Bao | b31892e | 2017-02-07 11:21:17 -0800 | [diff] [blame] | 736 | is_downgrade = long(post_timestamp) < long(pre_timestamp) |
| 737 | |
| 738 | if OPTIONS.downgrade: |
Tao Bao | b31892e | 2017-02-07 11:21:17 -0800 | [diff] [blame] | 739 | if not is_downgrade: |
| 740 | raise RuntimeError("--downgrade specified but no downgrade detected: " |
| 741 | "pre: %s, post: %s" % (pre_timestamp, post_timestamp)) |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 742 | metadata["ota-downgrade"] = "yes" |
| 743 | elif OPTIONS.timestamp: |
| 744 | if not is_downgrade: |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 745 | raise RuntimeError("--override_timestamp specified but no timestamp hack " |
| 746 | "needed: pre: %s, post: %s" % (pre_timestamp, |
| 747 | post_timestamp)) |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 748 | metadata["post-timestamp"] = str(long(pre_timestamp) + 1) |
Tao Bao | b31892e | 2017-02-07 11:21:17 -0800 | [diff] [blame] | 749 | else: |
| 750 | if is_downgrade: |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 751 | raise RuntimeError("Downgrade detected based on timestamp check: " |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 752 | "pre: %s, post: %s. Need to specify " |
| 753 | "--override_timestamp OR --downgrade to allow " |
| 754 | "building the incremental." % (pre_timestamp, |
| 755 | post_timestamp)) |
Tao Bao | b31892e | 2017-02-07 11:21:17 -0800 | [diff] [blame] | 756 | metadata["post-timestamp"] = post_timestamp |
| 757 | |
| 758 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 759 | def GetPackageMetadata(target_info, source_info=None): |
| 760 | """Generates and returns the metadata dict. |
| 761 | |
| 762 | It generates a dict() that contains the info to be written into an OTA |
| 763 | package (META-INF/com/android/metadata). It also handles the detection of |
| 764 | downgrade / timestamp override / data wipe based on the global options. |
| 765 | |
| 766 | Args: |
| 767 | target_info: The BuildInfo instance that holds the target build info. |
| 768 | source_info: The BuildInfo instance that holds the source build info, or |
| 769 | None if generating full OTA. |
| 770 | |
| 771 | Returns: |
| 772 | A dict to be written into package metadata entry. |
| 773 | """ |
| 774 | assert isinstance(target_info, BuildInfo) |
| 775 | assert source_info is None or isinstance(source_info, BuildInfo) |
| 776 | |
| 777 | metadata = { |
| 778 | 'post-build' : target_info.fingerprint, |
| 779 | 'post-build-incremental' : target_info.GetBuildProp( |
| 780 | 'ro.build.version.incremental'), |
| 781 | } |
| 782 | |
| 783 | if target_info.is_ab: |
| 784 | metadata['ota-type'] = 'AB' |
| 785 | metadata['ota-required-cache'] = '0' |
| 786 | else: |
| 787 | metadata['ota-type'] = 'BLOCK' |
| 788 | |
| 789 | if OPTIONS.wipe_user_data: |
| 790 | metadata['ota-wipe'] = 'yes' |
| 791 | |
| 792 | is_incremental = source_info is not None |
| 793 | if is_incremental: |
| 794 | metadata['pre-build'] = source_info.fingerprint |
| 795 | metadata['pre-build-incremental'] = source_info.GetBuildProp( |
| 796 | 'ro.build.version.incremental') |
| 797 | metadata['pre-device'] = source_info.device |
| 798 | else: |
| 799 | metadata['pre-device'] = target_info.device |
| 800 | |
| 801 | # Detect downgrades, or fill in the post-timestamp. |
| 802 | if is_incremental: |
| 803 | HandleDowngradeMetadata(metadata, target_info, source_info) |
| 804 | else: |
| 805 | metadata['post-timestamp'] = target_info.GetBuildProp('ro.build.date.utc') |
| 806 | |
| 807 | return metadata |
| 808 | |
| 809 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 810 | def WriteBlockIncrementalOTAPackage(target_zip, source_zip, output_zip): |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 811 | target_info = BuildInfo(OPTIONS.target_info_dict, OPTIONS.oem_dicts) |
| 812 | source_info = BuildInfo(OPTIONS.source_info_dict, OPTIONS.oem_dicts) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 813 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 814 | target_api_version = target_info["recovery_api_version"] |
| 815 | source_api_version = source_info["recovery_api_version"] |
| 816 | if source_api_version == 0: |
Tao Bao | 3e30d97 | 2016-03-15 13:20:19 -0700 | [diff] [blame] | 817 | print("WARNING: generating edify script for a source that " |
| 818 | "can't install it.") |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 819 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 820 | script = edify_generator.EdifyGenerator( |
| 821 | source_api_version, target_info, fstab=source_info["fstab"]) |
| 822 | |
| 823 | if target_info.oem_props or source_info.oem_props: |
| 824 | if not OPTIONS.oem_no_mount: |
| 825 | source_info.WriteMountOemScript(script) |
Tao Bao | 3806c23 | 2015-07-05 21:08:33 -0700 | [diff] [blame] | 826 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 827 | metadata = GetPackageMetadata(target_info, source_info) |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 828 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 829 | device_specific = common.DeviceSpecificParams( |
| 830 | source_zip=source_zip, |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 831 | source_version=source_api_version, |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 832 | target_zip=target_zip, |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 833 | target_version=target_api_version, |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 834 | output_zip=output_zip, |
| 835 | script=script, |
| 836 | metadata=metadata, |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 837 | info_dict=source_info) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 838 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 839 | source_boot = common.GetBootableImage( |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 840 | "/tmp/boot.img", "boot.img", OPTIONS.source_tmp, "BOOT", source_info) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 841 | target_boot = common.GetBootableImage( |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 842 | "/tmp/boot.img", "boot.img", OPTIONS.target_tmp, "BOOT", target_info) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 843 | updating_boot = (not OPTIONS.two_step and |
| 844 | (source_boot.data != target_boot.data)) |
| 845 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 846 | target_recovery = common.GetBootableImage( |
| 847 | "/tmp/recovery.img", "recovery.img", OPTIONS.target_tmp, "RECOVERY") |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 848 | |
Tao Bao | 7e0f160 | 2017-03-06 15:50:08 -0800 | [diff] [blame] | 849 | system_src = GetImage("system", OPTIONS.source_tmp) |
| 850 | system_tgt = GetImage("system", OPTIONS.target_tmp) |
Tao Bao | dd2a589 | 2015-03-12 12:32:37 -0700 | [diff] [blame] | 851 | |
Tao Bao | 0582cb6 | 2017-12-21 11:47:01 -0800 | [diff] [blame] | 852 | blockimgdiff_version = max( |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 853 | int(i) for i in target_info.get("blockimgdiff_versions", "1").split(",")) |
Tao Bao | 0582cb6 | 2017-12-21 11:47:01 -0800 | [diff] [blame] | 854 | assert blockimgdiff_version >= 3 |
Tao Bao | dd2a589 | 2015-03-12 12:32:37 -0700 | [diff] [blame] | 855 | |
Tao Bao | f8acad1 | 2016-07-07 09:09:58 -0700 | [diff] [blame] | 856 | # Check the first block of the source system partition for remount R/W only |
| 857 | # if the filesystem is ext4. |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 858 | system_src_partition = source_info["fstab"]["/system"] |
Tao Bao | f8acad1 | 2016-07-07 09:09:58 -0700 | [diff] [blame] | 859 | check_first_block = system_src_partition.fs_type == "ext4" |
Tao Bao | 293fd13 | 2016-06-11 12:19:23 -0700 | [diff] [blame] | 860 | # Disable using imgdiff for squashfs. 'imgdiff -z' expects input files to be |
| 861 | # in zip formats. However with squashfs, a) all files are compressed in LZ4; |
| 862 | # b) the blocks listed in block map may not contain all the bytes for a given |
| 863 | # file (because they're rounded to be 4K-aligned). |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 864 | system_tgt_partition = target_info["fstab"]["/system"] |
Tao Bao | f8acad1 | 2016-07-07 09:09:58 -0700 | [diff] [blame] | 865 | disable_imgdiff = (system_src_partition.fs_type == "squashfs" or |
| 866 | system_tgt_partition.fs_type == "squashfs") |
Doug Zongker | b34fcce | 2014-09-11 09:34:56 -0700 | [diff] [blame] | 867 | system_diff = common.BlockDifference("system", system_tgt, system_src, |
Tianjie Xu | fc3422a | 2015-12-15 11:53:59 -0800 | [diff] [blame] | 868 | check_first_block, |
Tao Bao | 293fd13 | 2016-06-11 12:19:23 -0700 | [diff] [blame] | 869 | version=blockimgdiff_version, |
| 870 | disable_imgdiff=disable_imgdiff) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 871 | |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 872 | if HasVendorPartition(target_zip): |
| 873 | if not HasVendorPartition(source_zip): |
| 874 | raise RuntimeError("can't generate incremental that adds /vendor") |
Tao Bao | 7e0f160 | 2017-03-06 15:50:08 -0800 | [diff] [blame] | 875 | vendor_src = GetImage("vendor", OPTIONS.source_tmp) |
| 876 | vendor_tgt = GetImage("vendor", OPTIONS.target_tmp) |
Tianjie Xu | fc3422a | 2015-12-15 11:53:59 -0800 | [diff] [blame] | 877 | |
| 878 | # Check first block of vendor partition for remount R/W only if |
| 879 | # disk type is ext4 |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 880 | vendor_partition = source_info["fstab"]["/vendor"] |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 881 | check_first_block = vendor_partition.fs_type == "ext4" |
Tao Bao | 293fd13 | 2016-06-11 12:19:23 -0700 | [diff] [blame] | 882 | disable_imgdiff = vendor_partition.fs_type == "squashfs" |
Doug Zongker | b34fcce | 2014-09-11 09:34:56 -0700 | [diff] [blame] | 883 | vendor_diff = common.BlockDifference("vendor", vendor_tgt, vendor_src, |
Tianjie Xu | fc3422a | 2015-12-15 11:53:59 -0800 | [diff] [blame] | 884 | check_first_block, |
Tao Bao | 293fd13 | 2016-06-11 12:19:23 -0700 | [diff] [blame] | 885 | version=blockimgdiff_version, |
| 886 | disable_imgdiff=disable_imgdiff) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 887 | else: |
| 888 | vendor_diff = None |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 889 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 890 | AddCompatibilityArchiveIfTrebleEnabled( |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 891 | target_zip, output_zip, target_info, source_info) |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 892 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 893 | # Assertions (e.g. device properties check). |
| 894 | target_info.WriteDeviceAssertions(script, OPTIONS.oem_no_mount) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 895 | device_specific.IncrementalOTA_Assertions() |
| 896 | |
| 897 | # Two-step incremental package strategy (in chronological order, |
| 898 | # which is *not* the order in which the generated script has |
| 899 | # things): |
| 900 | # |
| 901 | # if stage is not "2/3" or "3/3": |
| 902 | # do verification on current system |
| 903 | # write recovery image to boot partition |
| 904 | # set stage to "2/3" |
| 905 | # reboot to boot partition and restart recovery |
| 906 | # else if stage is "2/3": |
| 907 | # write recovery image to recovery partition |
| 908 | # set stage to "3/3" |
| 909 | # reboot to recovery partition and restart recovery |
| 910 | # else: |
| 911 | # (stage must be "3/3") |
| 912 | # perform update: |
| 913 | # patch system files, etc. |
| 914 | # force full install of new boot image |
| 915 | # set up system to update recovery partition on first boot |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 916 | # complete script normally |
| 917 | # (allow recovery to mark itself finished and reboot) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 918 | |
| 919 | if OPTIONS.two_step: |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 920 | if not source_info.get("multistage_support"): |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 921 | assert False, "two-step packages not supported by this build" |
Tao Bao | 24604cc | 2018-02-01 16:25:44 -0800 | [diff] [blame^] | 922 | fs = source_info["fstab"]["/misc"] |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 923 | assert fs.fs_type.upper() == "EMMC", \ |
| 924 | "two-step packages only supported on devices with EMMC /misc partitions" |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 925 | bcb_dev = {"bcb_dev" : fs.device} |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 926 | common.ZipWriteStr(output_zip, "recovery.img", target_recovery.data) |
| 927 | script.AppendExtra(""" |
Michael Runge | fb8886d | 2014-10-23 13:51:04 -0700 | [diff] [blame] | 928 | if get_stage("%(bcb_dev)s") == "2/3" then |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 929 | """ % bcb_dev) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 930 | |
| 931 | # Stage 2/3: Write recovery image to /recovery (currently running /boot). |
| 932 | script.Comment("Stage 2/3") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 933 | script.AppendExtra("sleep(20);\n") |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 934 | script.WriteRawImage("/recovery", "recovery.img") |
| 935 | script.AppendExtra(""" |
| 936 | set_stage("%(bcb_dev)s", "3/3"); |
| 937 | reboot_now("%(bcb_dev)s", "recovery"); |
Michael Runge | fb8886d | 2014-10-23 13:51:04 -0700 | [diff] [blame] | 938 | else if get_stage("%(bcb_dev)s") != "3/3" then |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 939 | """ % bcb_dev) |
| 940 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 941 | # Stage 1/3: (a) Verify the current system. |
| 942 | script.Comment("Stage 1/3") |
| 943 | |
Tao Bao | 6c55a8a | 2015-04-08 15:30:27 -0700 | [diff] [blame] | 944 | # Dump fingerprints |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 945 | script.Print("Source: {}".format(source_info.fingerprint)) |
| 946 | script.Print("Target: {}".format(target_info.fingerprint)) |
Tao Bao | 6c55a8a | 2015-04-08 15:30:27 -0700 | [diff] [blame] | 947 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 948 | script.Print("Verifying current system...") |
| 949 | |
| 950 | device_specific.IncrementalOTA_VerifyBegin() |
| 951 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 952 | WriteFingerprintAssertion(script, target_info, source_info) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 953 | |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 954 | # Check the required cache size (i.e. stashed blocks). |
| 955 | size = [] |
| 956 | if system_diff: |
| 957 | size.append(system_diff.required_cache) |
| 958 | if vendor_diff: |
| 959 | size.append(vendor_diff.required_cache) |
| 960 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 961 | if updating_boot: |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 962 | boot_type, boot_device = common.GetTypeAndDevice("/boot", source_info) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 963 | d = common.Difference(target_boot, source_boot) |
| 964 | _, _, d = d.ComputePatch() |
Doug Zongker | f834008 | 2014-08-05 10:39:37 -0700 | [diff] [blame] | 965 | if d is None: |
| 966 | include_full_boot = True |
| 967 | common.ZipWriteStr(output_zip, "boot.img", target_boot.data) |
| 968 | else: |
| 969 | include_full_boot = False |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 970 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 971 | print("boot target: %d source: %d diff: %d" % ( |
| 972 | target_boot.size, source_boot.size, len(d))) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 973 | |
Doug Zongker | f834008 | 2014-08-05 10:39:37 -0700 | [diff] [blame] | 974 | common.ZipWriteStr(output_zip, "patch/boot.img.p", d) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 975 | |
Doug Zongker | f834008 | 2014-08-05 10:39:37 -0700 | [diff] [blame] | 976 | script.PatchCheck("%s:%s:%d:%s:%d:%s" % |
| 977 | (boot_type, boot_device, |
| 978 | source_boot.size, source_boot.sha1, |
| 979 | target_boot.size, target_boot.sha1)) |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 980 | size.append(target_boot.size) |
| 981 | |
| 982 | if size: |
| 983 | script.CacheFreeSpaceCheck(max(size)) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 984 | |
| 985 | device_specific.IncrementalOTA_VerifyEnd() |
| 986 | |
| 987 | if OPTIONS.two_step: |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 988 | # Stage 1/3: (b) Write recovery image to /boot. |
| 989 | _WriteRecoveryImageToBoot(script, output_zip) |
| 990 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 991 | script.AppendExtra(""" |
| 992 | set_stage("%(bcb_dev)s", "2/3"); |
| 993 | reboot_now("%(bcb_dev)s", ""); |
| 994 | else |
| 995 | """ % bcb_dev) |
| 996 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 997 | # Stage 3/3: Make changes. |
| 998 | script.Comment("Stage 3/3") |
| 999 | |
Jesse Zhao | 75bcea0 | 2015-01-06 10:59:53 -0800 | [diff] [blame] | 1000 | # Verify the existing partitions. |
Tao Bao | d522bdc | 2016-04-12 15:53:16 -0700 | [diff] [blame] | 1001 | system_diff.WriteVerifyScript(script, touched_blocks_only=True) |
Jesse Zhao | 75bcea0 | 2015-01-06 10:59:53 -0800 | [diff] [blame] | 1002 | if vendor_diff: |
Tao Bao | d522bdc | 2016-04-12 15:53:16 -0700 | [diff] [blame] | 1003 | vendor_diff.WriteVerifyScript(script, touched_blocks_only=True) |
Jesse Zhao | 75bcea0 | 2015-01-06 10:59:53 -0800 | [diff] [blame] | 1004 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1005 | script.Comment("---- start making changes here ----") |
| 1006 | |
| 1007 | device_specific.IncrementalOTA_InstallBegin() |
| 1008 | |
Doug Zongker | ab7ca1d | 2014-08-26 10:40:28 -0700 | [diff] [blame] | 1009 | system_diff.WriteScript(script, output_zip, |
| 1010 | progress=0.8 if vendor_diff else 0.9) |
Tao Bao | 68658c0 | 2015-06-01 13:40:49 -0700 | [diff] [blame] | 1011 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 1012 | if vendor_diff: |
Doug Zongker | ab7ca1d | 2014-08-26 10:40:28 -0700 | [diff] [blame] | 1013 | vendor_diff.WriteScript(script, output_zip, progress=0.1) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1014 | |
| 1015 | if OPTIONS.two_step: |
| 1016 | common.ZipWriteStr(output_zip, "boot.img", target_boot.data) |
| 1017 | script.WriteRawImage("/boot", "boot.img") |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1018 | print("writing full boot image (forced by two-step mode)") |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1019 | |
| 1020 | if not OPTIONS.two_step: |
| 1021 | if updating_boot: |
Doug Zongker | f834008 | 2014-08-05 10:39:37 -0700 | [diff] [blame] | 1022 | if include_full_boot: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1023 | print("boot image changed; including full.") |
Doug Zongker | f834008 | 2014-08-05 10:39:37 -0700 | [diff] [blame] | 1024 | script.Print("Installing boot image...") |
| 1025 | script.WriteRawImage("/boot", "boot.img") |
| 1026 | else: |
| 1027 | # Produce the boot image by applying a patch to the current |
| 1028 | # contents of the boot partition, and write it back to the |
| 1029 | # partition. |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1030 | print("boot image changed; including patch.") |
Doug Zongker | f834008 | 2014-08-05 10:39:37 -0700 | [diff] [blame] | 1031 | script.Print("Patching boot image...") |
| 1032 | script.ShowProgress(0.1, 10) |
| 1033 | script.ApplyPatch("%s:%s:%d:%s:%d:%s" |
| 1034 | % (boot_type, boot_device, |
| 1035 | source_boot.size, source_boot.sha1, |
| 1036 | target_boot.size, target_boot.sha1), |
| 1037 | "-", |
| 1038 | target_boot.size, target_boot.sha1, |
| 1039 | source_boot.sha1, "patch/boot.img.p") |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1040 | else: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1041 | print("boot image unchanged; skipping.") |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1042 | |
| 1043 | # Do device-specific installation (eg, write radio image). |
| 1044 | device_specific.IncrementalOTA_InstallEnd() |
| 1045 | |
| 1046 | if OPTIONS.extra_script is not None: |
| 1047 | script.AppendExtra(OPTIONS.extra_script) |
| 1048 | |
Doug Zongker | 922206e | 2014-03-04 13:16:24 -0800 | [diff] [blame] | 1049 | if OPTIONS.wipe_user_data: |
| 1050 | script.Print("Erasing user data...") |
| 1051 | script.FormatPartition("/data") |
| 1052 | |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1053 | if OPTIONS.two_step: |
| 1054 | script.AppendExtra(""" |
| 1055 | set_stage("%(bcb_dev)s", ""); |
| 1056 | endif; |
| 1057 | endif; |
| 1058 | """ % bcb_dev) |
| 1059 | |
| 1060 | script.SetProgress(1) |
Tao Bao | 4996cf0 | 2016-03-08 17:53:39 -0800 | [diff] [blame] | 1061 | # For downgrade OTAs, we prefer to use the update-binary in the source |
| 1062 | # build that is actually newer than the one in the target build. |
| 1063 | if OPTIONS.downgrade: |
| 1064 | script.AddToZip(source_zip, output_zip, input_path=OPTIONS.updater_binary) |
| 1065 | else: |
| 1066 | script.AddToZip(target_zip, output_zip, input_path=OPTIONS.updater_binary) |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 1067 | metadata["ota-required-cache"] = str(script.required_cache) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 1068 | WriteMetadata(metadata, output_zip) |
| 1069 | |
Doug Zongker | 32b527d | 2014-03-04 10:03:02 -0800 | [diff] [blame] | 1070 | |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1071 | def WriteABOTAPackageWithBrilloScript(target_file, output_file, |
| 1072 | source_file=None): |
| 1073 | """Generate an Android OTA package that has A/B update payload.""" |
| 1074 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1075 | def ComputeStreamingMetadata(zip_file, reserve_space=False, |
| 1076 | expected_length=None): |
| 1077 | """Compute the streaming metadata for a given zip. |
| 1078 | |
| 1079 | When 'reserve_space' is True, we reserve extra space for the offset and |
| 1080 | length of the metadata entry itself, although we don't know the final |
| 1081 | values until the package gets signed. This function will be called again |
| 1082 | after signing. We then write the actual values and pad the string to the |
| 1083 | length we set earlier. Note that we can't use the actual length of the |
| 1084 | metadata entry in the second run. Otherwise the offsets for other entries |
| 1085 | will be changing again. |
| 1086 | """ |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1087 | |
| 1088 | def ComputeEntryOffsetSize(name): |
| 1089 | """Compute the zip entry offset and size.""" |
| 1090 | info = zip_file.getinfo(name) |
| 1091 | offset = info.header_offset + len(info.FileHeader()) |
| 1092 | size = info.file_size |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1093 | return '%s:%d:%d' % (os.path.basename(name), offset, size) |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1094 | |
| 1095 | # payload.bin and payload_properties.txt must exist. |
| 1096 | offsets = [ComputeEntryOffsetSize('payload.bin'), |
| 1097 | ComputeEntryOffsetSize('payload_properties.txt')] |
| 1098 | |
| 1099 | # care_map.txt is available only if dm-verity is enabled. |
| 1100 | if 'care_map.txt' in zip_file.namelist(): |
| 1101 | offsets.append(ComputeEntryOffsetSize('care_map.txt')) |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1102 | |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 1103 | if 'compatibility.zip' in zip_file.namelist(): |
| 1104 | offsets.append(ComputeEntryOffsetSize('compatibility.zip')) |
| 1105 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1106 | # 'META-INF/com/android/metadata' is required. We don't know its actual |
| 1107 | # offset and length (as well as the values for other entries). So we |
| 1108 | # reserve 10-byte as a placeholder, which is to cover the space for metadata |
| 1109 | # entry ('xx:xxx', since it's ZIP_STORED which should appear at the |
| 1110 | # beginning of the zip), as well as the possible value changes in other |
| 1111 | # entries. |
| 1112 | if reserve_space: |
| 1113 | offsets.append('metadata:' + ' ' * 10) |
| 1114 | else: |
| 1115 | offsets.append(ComputeEntryOffsetSize(METADATA_NAME)) |
| 1116 | |
| 1117 | value = ','.join(offsets) |
| 1118 | if expected_length is not None: |
| 1119 | assert len(value) <= expected_length, \ |
| 1120 | 'Insufficient reserved space: reserved=%d, actual=%d' % ( |
| 1121 | expected_length, len(value)) |
| 1122 | value += ' ' * (expected_length - len(value)) |
| 1123 | return value |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1124 | |
Alex Deymo | d8d96ec | 2016-06-10 16:38:31 -0700 | [diff] [blame] | 1125 | # The place where the output from the subprocess should go. |
| 1126 | log_file = sys.stdout if OPTIONS.verbose else subprocess.PIPE |
| 1127 | |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 1128 | # Get the PayloadSigner to be used in step 3. |
| 1129 | payload_signer = PayloadSigner() |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1130 | |
Tao Bao | dea0f8b | 2016-06-20 17:55:06 -0700 | [diff] [blame] | 1131 | # Stage the output zip package for package signing. |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1132 | temp_zip_file = tempfile.NamedTemporaryFile() |
| 1133 | output_zip = zipfile.ZipFile(temp_zip_file, "w", |
| 1134 | compression=zipfile.ZIP_DEFLATED) |
| 1135 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1136 | if source_file is not None: |
| 1137 | target_info = BuildInfo(OPTIONS.target_info_dict, OPTIONS.oem_dicts) |
| 1138 | source_info = BuildInfo(OPTIONS.source_info_dict, OPTIONS.oem_dicts) |
| 1139 | else: |
| 1140 | target_info = BuildInfo(OPTIONS.info_dict, OPTIONS.oem_dicts) |
| 1141 | source_info = None |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1142 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1143 | # Metadata to comply with Android OTA package format. |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 1144 | metadata = GetPackageMetadata(target_info, source_info) |
Tao Bao | b31892e | 2017-02-07 11:21:17 -0800 | [diff] [blame] | 1145 | |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1146 | # 1. Generate payload. |
| 1147 | payload_file = common.MakeTempFile(prefix="payload-", suffix=".bin") |
| 1148 | cmd = ["brillo_update_payload", "generate", |
| 1149 | "--payload", payload_file, |
| 1150 | "--target_image", target_file] |
| 1151 | if source_file is not None: |
| 1152 | cmd.extend(["--source_image", source_file]) |
Alex Deymo | d8d96ec | 2016-06-10 16:38:31 -0700 | [diff] [blame] | 1153 | p1 = common.Run(cmd, stdout=log_file, stderr=subprocess.STDOUT) |
| 1154 | p1.communicate() |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1155 | assert p1.returncode == 0, "brillo_update_payload generate failed" |
| 1156 | |
| 1157 | # 2. Generate hashes of the payload and metadata files. |
| 1158 | payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin") |
| 1159 | metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin") |
| 1160 | cmd = ["brillo_update_payload", "hash", |
| 1161 | "--unsigned_payload", payload_file, |
| 1162 | "--signature_size", "256", |
| 1163 | "--metadata_hash_file", metadata_sig_file, |
| 1164 | "--payload_hash_file", payload_sig_file] |
Alex Deymo | d8d96ec | 2016-06-10 16:38:31 -0700 | [diff] [blame] | 1165 | p1 = common.Run(cmd, stdout=log_file, stderr=subprocess.STDOUT) |
| 1166 | p1.communicate() |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1167 | assert p1.returncode == 0, "brillo_update_payload hash failed" |
| 1168 | |
| 1169 | # 3. Sign the hashes and insert them back into the payload file. |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1170 | # 3a. Sign the payload hash. |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 1171 | signed_payload_sig_file = payload_signer.Sign(payload_sig_file) |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1172 | |
| 1173 | # 3b. Sign the metadata hash. |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 1174 | signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file) |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1175 | |
| 1176 | # 3c. Insert the signatures back into the payload file. |
| 1177 | signed_payload_file = common.MakeTempFile(prefix="signed-payload-", |
| 1178 | suffix=".bin") |
| 1179 | cmd = ["brillo_update_payload", "sign", |
| 1180 | "--unsigned_payload", payload_file, |
| 1181 | "--payload", signed_payload_file, |
| 1182 | "--signature_size", "256", |
| 1183 | "--metadata_signature_file", signed_metadata_sig_file, |
| 1184 | "--payload_signature_file", signed_payload_sig_file] |
Alex Deymo | d8d96ec | 2016-06-10 16:38:31 -0700 | [diff] [blame] | 1185 | p1 = common.Run(cmd, stdout=log_file, stderr=subprocess.STDOUT) |
| 1186 | p1.communicate() |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1187 | assert p1.returncode == 0, "brillo_update_payload sign failed" |
| 1188 | |
Alex Deymo | 19241c1 | 2016-02-04 22:29:29 -0800 | [diff] [blame] | 1189 | # 4. Dump the signed payload properties. |
| 1190 | properties_file = common.MakeTempFile(prefix="payload-properties-", |
| 1191 | suffix=".txt") |
| 1192 | cmd = ["brillo_update_payload", "properties", |
| 1193 | "--payload", signed_payload_file, |
| 1194 | "--properties_file", properties_file] |
Alex Deymo | d8d96ec | 2016-06-10 16:38:31 -0700 | [diff] [blame] | 1195 | p1 = common.Run(cmd, stdout=log_file, stderr=subprocess.STDOUT) |
| 1196 | p1.communicate() |
Alex Deymo | 19241c1 | 2016-02-04 22:29:29 -0800 | [diff] [blame] | 1197 | assert p1.returncode == 0, "brillo_update_payload properties failed" |
| 1198 | |
Tao Bao | 7c5dc57 | 2016-06-14 17:48:11 -0700 | [diff] [blame] | 1199 | if OPTIONS.wipe_user_data: |
| 1200 | with open(properties_file, "a") as f: |
| 1201 | f.write("POWERWASH=1\n") |
Tao Bao | 7c5dc57 | 2016-06-14 17:48:11 -0700 | [diff] [blame] | 1202 | |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1203 | # Add the signed payload file and properties into the zip. In order to |
| 1204 | # support streaming, we pack payload.bin, payload_properties.txt and |
| 1205 | # care_map.txt as ZIP_STORED. So these entries can be read directly with |
| 1206 | # the offset and length pairs. |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1207 | common.ZipWrite(output_zip, signed_payload_file, arcname="payload.bin", |
| 1208 | compress_type=zipfile.ZIP_STORED) |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1209 | common.ZipWrite(output_zip, properties_file, |
| 1210 | arcname="payload_properties.txt", |
| 1211 | compress_type=zipfile.ZIP_STORED) |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1212 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 1213 | # If dm-verity is supported for the device, copy contents of care_map |
| 1214 | # into A/B OTA package. |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 1215 | target_zip = zipfile.ZipFile(target_file, "r") |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1216 | if (target_info.get("verity") == "true" or |
| 1217 | target_info.get("avb_enable") == "true"): |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 1218 | care_map_path = "META/care_map.txt" |
| 1219 | namelist = target_zip.namelist() |
| 1220 | if care_map_path in namelist: |
| 1221 | care_map_data = target_zip.read(care_map_path) |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1222 | common.ZipWriteStr(output_zip, "care_map.txt", care_map_data, |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1223 | compress_type=zipfile.ZIP_STORED) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 1224 | else: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1225 | print("Warning: cannot find care map file in target_file package") |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 1226 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1227 | # source_info must be None for full OTAs. |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 1228 | if source_file is None: |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1229 | assert source_info is None |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 1230 | |
Tao Bao | bcd1d16 | 2017-08-26 13:10:26 -0700 | [diff] [blame] | 1231 | AddCompatibilityArchiveIfTrebleEnabled( |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1232 | target_zip, output_zip, target_info, source_info) |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 1233 | |
Tao Bao | 21803d3 | 2017-04-19 10:16:09 -0700 | [diff] [blame] | 1234 | common.ZipClose(target_zip) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 1235 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1236 | # Write the current metadata entry with placeholders. |
Tao Bao | bfdcb12 | 2017-01-31 15:06:05 -0800 | [diff] [blame] | 1237 | metadata['ota-streaming-property-files'] = ComputeStreamingMetadata( |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1238 | output_zip, reserve_space=True) |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1239 | WriteMetadata(metadata, output_zip) |
| 1240 | common.ZipClose(output_zip) |
| 1241 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1242 | # SignOutput(), which in turn calls signapk.jar, will possibly reorder the |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1243 | # ZIP entries, as well as padding the entry headers. We do a preliminary |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1244 | # signing (with an incomplete metadata entry) to allow that to happen. Then |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1245 | # compute the ZIP entry offsets, write back the final metadata and do the |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1246 | # final signing. |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1247 | prelim_signing = common.MakeTempFile(suffix='.zip') |
| 1248 | SignOutput(temp_zip_file.name, prelim_signing) |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1249 | common.ZipClose(temp_zip_file) |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1250 | |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1251 | # Open the signed zip. Compute the final metadata that's needed for streaming. |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1252 | prelim_signing_zip = zipfile.ZipFile(prelim_signing, 'r') |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1253 | expected_length = len(metadata['ota-streaming-property-files']) |
| 1254 | metadata['ota-streaming-property-files'] = ComputeStreamingMetadata( |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1255 | prelim_signing_zip, reserve_space=False, expected_length=expected_length) |
| 1256 | common.ZipClose(prelim_signing_zip) |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1257 | |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1258 | # Replace the METADATA entry. |
| 1259 | common.ZipDelete(prelim_signing, METADATA_NAME) |
| 1260 | output_zip = zipfile.ZipFile(prelim_signing, 'a', |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1261 | compression=zipfile.ZIP_DEFLATED) |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1262 | WriteMetadata(metadata, output_zip) |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1263 | common.ZipClose(output_zip) |
| 1264 | |
| 1265 | # Re-sign the package after updating the metadata entry. |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1266 | SignOutput(prelim_signing, output_file) |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1267 | |
| 1268 | # Reopen the final signed zip to double check the streaming metadata. |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 1269 | output_zip = zipfile.ZipFile(output_file, 'r') |
Tao Bao | 2dd1c48 | 2017-02-03 16:49:39 -0800 | [diff] [blame] | 1270 | actual = metadata['ota-streaming-property-files'].strip() |
| 1271 | expected = ComputeStreamingMetadata(output_zip) |
| 1272 | assert actual == expected, \ |
| 1273 | "Mismatching streaming metadata: %s vs %s." % (actual, expected) |
Tao Bao | c96316c | 2017-01-24 22:10:49 -0800 | [diff] [blame] | 1274 | common.ZipClose(output_zip) |
| 1275 | |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1276 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1277 | def main(argv): |
| 1278 | |
| 1279 | def option_handler(o, a): |
Tao Bao | 4b76a0e | 2017-10-31 12:13:33 -0700 | [diff] [blame] | 1280 | if o in ("-k", "--package_key"): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1281 | OPTIONS.package_key = a |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1282 | elif o in ("-i", "--incremental_from"): |
| 1283 | OPTIONS.incremental_source = a |
Tao Bao | 43078aa | 2015-04-21 14:32:35 -0700 | [diff] [blame] | 1284 | elif o == "--full_radio": |
| 1285 | OPTIONS.full_radio = True |
leozwang | aa6c1a1 | 2015-08-14 10:57:58 -0700 | [diff] [blame] | 1286 | elif o == "--full_bootloader": |
| 1287 | OPTIONS.full_bootloader = True |
Tao Bao | 337633f | 2017-12-06 15:20:19 -0800 | [diff] [blame] | 1288 | elif o == "--wipe_user_data": |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 1289 | OPTIONS.wipe_user_data = True |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 1290 | elif o == "--downgrade": |
| 1291 | OPTIONS.downgrade = True |
| 1292 | OPTIONS.wipe_user_data = True |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 1293 | elif o == "--override_timestamp": |
| 1294 | OPTIONS.timestamp = True |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 1295 | elif o in ("-o", "--oem_settings"): |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame] | 1296 | OPTIONS.oem_source = a.split(',') |
Tao Bao | 8608cde | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 1297 | elif o == "--oem_no_mount": |
| 1298 | OPTIONS.oem_no_mount = True |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 1299 | elif o in ("-e", "--extra_script"): |
| 1300 | OPTIONS.extra_script = a |
Martin Blumenstingl | 374e114 | 2014-05-31 20:42:55 +0200 | [diff] [blame] | 1301 | elif o in ("-t", "--worker_threads"): |
| 1302 | if a.isdigit(): |
| 1303 | OPTIONS.worker_threads = int(a) |
| 1304 | else: |
| 1305 | raise ValueError("Cannot parse value %r for option %r - only " |
| 1306 | "integers are allowed." % (a, o)) |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 1307 | elif o in ("-2", "--two_step"): |
| 1308 | OPTIONS.two_step = True |
Doug Zongker | 26e6619 | 2014-02-20 13:22:07 -0800 | [diff] [blame] | 1309 | elif o == "--no_signing": |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 1310 | OPTIONS.no_signing = True |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1311 | elif o == "--verify": |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 1312 | OPTIONS.verify = True |
Doug Zongker | 26e6619 | 2014-02-20 13:22:07 -0800 | [diff] [blame] | 1313 | elif o == "--block": |
| 1314 | OPTIONS.block_based = True |
Doug Zongker | 2556848 | 2014-03-03 10:21:27 -0800 | [diff] [blame] | 1315 | elif o in ("-b", "--binary"): |
| 1316 | OPTIONS.updater_binary = a |
Tao Bao | 8dcf738 | 2015-05-21 14:09:49 -0700 | [diff] [blame] | 1317 | elif o == "--stash_threshold": |
| 1318 | try: |
| 1319 | OPTIONS.stash_threshold = float(a) |
| 1320 | except ValueError: |
| 1321 | raise ValueError("Cannot parse value %r for option %r - expecting " |
| 1322 | "a float" % (a, o)) |
Tao Bao | d62c603 | 2015-11-30 09:40:20 -0800 | [diff] [blame] | 1323 | elif o == "--log_diff": |
| 1324 | OPTIONS.log_diff = a |
Tao Bao | dea0f8b | 2016-06-20 17:55:06 -0700 | [diff] [blame] | 1325 | elif o == "--payload_signer": |
| 1326 | OPTIONS.payload_signer = a |
Baligh Uddin | 2abbbd0 | 2016-06-22 12:14:16 -0700 | [diff] [blame] | 1327 | elif o == "--payload_signer_args": |
| 1328 | OPTIONS.payload_signer_args = shlex.split(a) |
Dan Willemsen | cea5cd2 | 2017-03-21 14:44:27 -0700 | [diff] [blame] | 1329 | elif o == "--extracted_input_target_files": |
| 1330 | OPTIONS.extracted_input = a |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1331 | else: |
| 1332 | return False |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 1333 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1334 | |
| 1335 | args = common.ParseOptions(argv, __doc__, |
Tao Bao | 337633f | 2017-12-06 15:20:19 -0800 | [diff] [blame] | 1336 | extra_opts="b:k:i:d:e:t:2o:", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1337 | extra_long_opts=[ |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1338 | "package_key=", |
| 1339 | "incremental_from=", |
Tao Bao | 43078aa | 2015-04-21 14:32:35 -0700 | [diff] [blame] | 1340 | "full_radio", |
leozwang | aa6c1a1 | 2015-08-14 10:57:58 -0700 | [diff] [blame] | 1341 | "full_bootloader", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1342 | "wipe_user_data", |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 1343 | "downgrade", |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 1344 | "override_timestamp", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1345 | "extra_script=", |
| 1346 | "worker_threads=", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1347 | "two_step", |
| 1348 | "no_signing", |
| 1349 | "block", |
| 1350 | "binary=", |
| 1351 | "oem_settings=", |
Tao Bao | 8608cde | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 1352 | "oem_no_mount", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1353 | "verify", |
Tao Bao | 8dcf738 | 2015-05-21 14:09:49 -0700 | [diff] [blame] | 1354 | "stash_threshold=", |
Tao Bao | d62c603 | 2015-11-30 09:40:20 -0800 | [diff] [blame] | 1355 | "log_diff=", |
Tao Bao | dea0f8b | 2016-06-20 17:55:06 -0700 | [diff] [blame] | 1356 | "payload_signer=", |
Baligh Uddin | 2abbbd0 | 2016-06-22 12:14:16 -0700 | [diff] [blame] | 1357 | "payload_signer_args=", |
Dan Willemsen | cea5cd2 | 2017-03-21 14:44:27 -0700 | [diff] [blame] | 1358 | "extracted_input_target_files=", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1359 | ], extra_option_handler=option_handler) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1360 | |
| 1361 | if len(args) != 2: |
| 1362 | common.Usage(__doc__) |
| 1363 | sys.exit(1) |
| 1364 | |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 1365 | if OPTIONS.downgrade: |
| 1366 | # Sanity check to enforce a data wipe. |
| 1367 | if not OPTIONS.wipe_user_data: |
| 1368 | raise ValueError("Cannot downgrade without a data wipe") |
| 1369 | |
| 1370 | # We should only allow downgrading incrementals (as opposed to full). |
| 1371 | # Otherwise the device may go back from arbitrary build with this full |
| 1372 | # OTA package. |
| 1373 | if OPTIONS.incremental_source is None: |
Elliott Hughes | d8a52f9 | 2016-06-20 14:35:47 -0700 | [diff] [blame] | 1374 | raise ValueError("Cannot generate downgradable full OTAs") |
Tao Bao | 5d18256 | 2016-02-23 11:38:39 -0800 | [diff] [blame] | 1375 | |
Tao Bao | 3e6161a | 2017-02-28 11:48:48 -0800 | [diff] [blame] | 1376 | assert not (OPTIONS.downgrade and OPTIONS.timestamp), \ |
| 1377 | "Cannot have --downgrade AND --override_timestamp both" |
| 1378 | |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1379 | # Load the build info dicts from the zip directly or the extracted input |
| 1380 | # directory. We don't need to unzip the entire target-files zips, because they |
| 1381 | # won't be needed for A/B OTAs (brillo_update_payload does that on its own). |
| 1382 | # When loading the info dicts, we don't need to provide the second parameter |
| 1383 | # to common.LoadInfoDict(). Specifying the second parameter allows replacing |
| 1384 | # some properties with their actual paths, such as 'selinux_fc', |
| 1385 | # 'ramdisk_dir', which won't be used during OTA generation. |
Dan Willemsen | cea5cd2 | 2017-03-21 14:44:27 -0700 | [diff] [blame] | 1386 | if OPTIONS.extracted_input is not None: |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1387 | OPTIONS.info_dict = common.LoadInfoDict(OPTIONS.extracted_input) |
Dan Willemsen | cea5cd2 | 2017-03-21 14:44:27 -0700 | [diff] [blame] | 1388 | else: |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1389 | with zipfile.ZipFile(args[0], 'r') as input_zip: |
| 1390 | OPTIONS.info_dict = common.LoadInfoDict(input_zip) |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1391 | |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1392 | if OPTIONS.verbose: |
| 1393 | print("--- target info ---") |
| 1394 | common.DumpInfoDict(OPTIONS.info_dict) |
| 1395 | |
| 1396 | # Load the source build dict if applicable. |
| 1397 | if OPTIONS.incremental_source is not None: |
| 1398 | OPTIONS.target_info_dict = OPTIONS.info_dict |
| 1399 | with zipfile.ZipFile(OPTIONS.incremental_source, 'r') as source_zip: |
| 1400 | OPTIONS.source_info_dict = common.LoadInfoDict(source_zip) |
| 1401 | |
| 1402 | if OPTIONS.verbose: |
| 1403 | print("--- source info ---") |
| 1404 | common.DumpInfoDict(OPTIONS.source_info_dict) |
| 1405 | |
| 1406 | # Load OEM dicts if provided. |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1407 | OPTIONS.oem_dicts = _LoadOemDicts(OPTIONS.oem_source) |
| 1408 | |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1409 | ab_update = OPTIONS.info_dict.get("ab_update") == "true" |
| 1410 | |
Christian Oder | f63e2cd | 2017-05-01 22:30:15 +0200 | [diff] [blame] | 1411 | # Use the default key to sign the package if not specified with package_key. |
| 1412 | # package_keys are needed on ab_updates, so always define them if an |
| 1413 | # ab_update is getting created. |
| 1414 | if not OPTIONS.no_signing or ab_update: |
| 1415 | if OPTIONS.package_key is None: |
| 1416 | OPTIONS.package_key = OPTIONS.info_dict.get( |
| 1417 | "default_system_dev_certificate", |
| 1418 | "build/target/product/security/testkey") |
| 1419 | # Get signing keys |
| 1420 | OPTIONS.key_passwords = common.GetKeyPasswords([OPTIONS.package_key]) |
| 1421 | |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1422 | if ab_update: |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1423 | WriteABOTAPackageWithBrilloScript( |
| 1424 | target_file=args[0], |
| 1425 | output_file=args[1], |
| 1426 | source_file=OPTIONS.incremental_source) |
| 1427 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1428 | print("done.") |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1429 | return |
| 1430 | |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1431 | # Sanity check the loaded info dicts first. |
| 1432 | if OPTIONS.info_dict.get("no_recovery") == "true": |
| 1433 | raise common.ExternalError( |
| 1434 | "--- target build has specified no recovery ---") |
| 1435 | |
| 1436 | # Non-A/B OTAs rely on /cache partition to store temporary files. |
| 1437 | cache_size = OPTIONS.info_dict.get("cache_size") |
| 1438 | if cache_size is None: |
| 1439 | print("--- can't determine the cache partition size ---") |
| 1440 | OPTIONS.cache_size = cache_size |
| 1441 | |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 1442 | if OPTIONS.extra_script is not None: |
| 1443 | OPTIONS.extra_script = open(OPTIONS.extra_script).read() |
| 1444 | |
Dan Willemsen | cea5cd2 | 2017-03-21 14:44:27 -0700 | [diff] [blame] | 1445 | if OPTIONS.extracted_input is not None: |
| 1446 | OPTIONS.input_tmp = OPTIONS.extracted_input |
Dan Willemsen | cea5cd2 | 2017-03-21 14:44:27 -0700 | [diff] [blame] | 1447 | input_zip = zipfile.ZipFile(args[0], "r") |
| 1448 | else: |
| 1449 | print("unzipping target target-files...") |
| 1450 | OPTIONS.input_tmp, input_zip = common.UnzipTemp( |
| 1451 | args[0], UNZIP_PATTERN) |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1452 | OPTIONS.target_tmp = OPTIONS.input_tmp |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 1453 | |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1454 | # If the caller explicitly specified the device-specific extensions path via |
| 1455 | # -s / --device_specific, use that. Otherwise, use META/releasetools.py if it |
| 1456 | # is present in the target target_files. Otherwise, take the path of the file |
| 1457 | # from 'tool_extensions' in the info dict and look for that in the local |
| 1458 | # filesystem, relative to the current directory. |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1459 | if OPTIONS.device_specific is None: |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1460 | from_input = os.path.join(OPTIONS.input_tmp, "META", "releasetools.py") |
| 1461 | if os.path.exists(from_input): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1462 | print("(using device-specific extensions from target_files)") |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1463 | OPTIONS.device_specific = from_input |
| 1464 | else: |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1465 | OPTIONS.device_specific = OPTIONS.info_dict.get("tool_extensions") |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1466 | |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1467 | if OPTIONS.device_specific is not None: |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1468 | OPTIONS.device_specific = os.path.abspath(OPTIONS.device_specific) |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1469 | |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1470 | # Set up the output zip. Create a temporary zip file if signing is needed. |
| 1471 | if OPTIONS.no_signing: |
| 1472 | if os.path.exists(args[1]): |
| 1473 | os.unlink(args[1]) |
| 1474 | output_zip = zipfile.ZipFile(args[1], "w", |
| 1475 | compression=zipfile.ZIP_DEFLATED) |
| 1476 | else: |
| 1477 | temp_zip_file = tempfile.NamedTemporaryFile() |
| 1478 | output_zip = zipfile.ZipFile(temp_zip_file, "w", |
| 1479 | compression=zipfile.ZIP_DEFLATED) |
Doug Zongker | 62d4f18 | 2014-08-04 16:06:43 -0700 | [diff] [blame] | 1480 | |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1481 | # Generate a full OTA. |
Tao Bao | 0c6a414 | 2017-12-15 10:11:19 -0800 | [diff] [blame] | 1482 | if OPTIONS.incremental_source is None: |
Tao Bao | c098e9e | 2016-01-07 13:03:56 -0800 | [diff] [blame] | 1483 | WriteFullOTAPackage(input_zip, output_zip) |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1484 | |
Tao Bao | 32b80dc | 2018-01-08 22:50:47 -0800 | [diff] [blame] | 1485 | # Generate an incremental OTA. |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1486 | else: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1487 | print("unzipping source target-files...") |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1488 | OPTIONS.source_tmp, source_zip = common.UnzipTemp( |
Tao Bao | 6b0b2f9 | 2017-03-05 11:38:11 -0800 | [diff] [blame] | 1489 | OPTIONS.incremental_source, |
Tao Bao | 457cbf6 | 2017-03-06 09:56:01 -0800 | [diff] [blame] | 1490 | UNZIP_PATTERN) |
Tao Bao | 32b80dc | 2018-01-08 22:50:47 -0800 | [diff] [blame] | 1491 | |
| 1492 | WriteBlockIncrementalOTAPackage(input_zip, source_zip, output_zip) |
| 1493 | |
| 1494 | if OPTIONS.log_diff: |
| 1495 | with open(OPTIONS.log_diff, 'w') as out_file: |
Tao Bao | d62c603 | 2015-11-30 09:40:20 -0800 | [diff] [blame] | 1496 | import target_files_diff |
Tao Bao | 32b80dc | 2018-01-08 22:50:47 -0800 | [diff] [blame] | 1497 | target_files_diff.recursiveDiff( |
| 1498 | '', OPTIONS.source_tmp, OPTIONS.input_tmp, out_file) |
Doug Zongker | 62d4f18 | 2014-08-04 16:06:43 -0700 | [diff] [blame] | 1499 | |
Tao Bao | 2db1385 | 2018-01-08 22:28:57 -0800 | [diff] [blame] | 1500 | common.ZipClose(input_zip) |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1501 | common.ZipClose(output_zip) |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1502 | |
Tao Bao | 767e3ac | 2015-11-10 12:19:19 -0800 | [diff] [blame] | 1503 | # Sign the generated zip package unless no_signing is specified. |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 1504 | if not OPTIONS.no_signing: |
| 1505 | SignOutput(temp_zip_file.name, args[1]) |
| 1506 | temp_zip_file.close() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1507 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1508 | print("done.") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1509 | |
| 1510 | |
| 1511 | if __name__ == '__main__': |
| 1512 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 1513 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1514 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 1515 | except common.ExternalError as e: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 1516 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1517 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 1518 | finally: |
| 1519 | common.Cleanup() |