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