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