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