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