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