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