| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 |  | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 3 | # | 
|  | 4 | # Copyright (C) 2015 The Android Open Source Project | 
|  | 5 | # | 
|  | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 7 | # you may not use this file except in compliance with the License. | 
|  | 8 | # You may obtain a copy of the License at | 
|  | 9 | # | 
|  | 10 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | # | 
|  | 12 | # Unless required by applicable law or agreed to in writing, software | 
|  | 13 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 15 | # See the License for the specific language governing permissions and | 
|  | 16 | # limitations under the License. | 
|  | 17 | # | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | # Script to generate a Brillo update for use by the update engine. | 
|  | 20 | # | 
|  | 21 | # usage: brillo_update_payload COMMAND [ARGS] | 
|  | 22 | # The following commands are supported: | 
|  | 23 | #  generate    generate an unsigned payload | 
|  | 24 | #  hash        generate a payload or metadata hash | 
|  | 25 | #  sign        generate a signed payload | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 26 | #  properties  generate a properties file from a payload | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 27 | #  verify      verify a payload by recreating a target image. | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 28 | #  check       verify a payload using paycheck (static testing) | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 29 | # | 
|  | 30 | #  Generate command arguments: | 
| Tianjie Xu | 72d512c | 2019-08-21 15:20:35 -0700 | [diff] [blame] | 31 | #  --payload                  generated unsigned payload output file | 
|  | 32 | #  --source_image             if defined, generate a delta payload from the | 
|  | 33 | #                             specified image to the target_image | 
|  | 34 | #  --target_image             the target image that should be sent to clients | 
|  | 35 | #  --metadata_size_file       if defined, generate a file containing the size | 
|  | 36 | #                             of the ayload metadata in bytes to the specified | 
|  | 37 | #                             file | 
|  | 38 | #  --disable_fec_computation  Disable the on device fec data computation for | 
|  | 39 | #                             incremental update. This feature is enabled by | 
|  | 40 | #                             default | 
| Tianjie | f4502bb | 2021-09-08 19:08:53 -0700 | [diff] [blame] | 41 | #  --force_minor_version      Override the minor version used for delta | 
|  | 42 | #                             generation. | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 43 | # | 
|  | 44 | #  Hash command arguments: | 
|  | 45 | #  --unsigned_payload    the input unsigned payload to generate the hash from | 
|  | 46 | #  --signature_size      signature sizes in bytes in the following format: | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 47 | #                        "size1:size2[:...]" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 48 | #  --payload_hash_file   if defined, generate a payload hash and output to the | 
|  | 49 | #                        specified file | 
|  | 50 | #  --metadata_hash_file  if defined, generate a metadata hash and output to the | 
|  | 51 | #                        specified file | 
|  | 52 | # | 
|  | 53 | #  Sign command arguments: | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 54 | #  --unsigned_payload        the input unsigned payload to insert the signatures | 
|  | 55 | #  --payload                 the output signed payload | 
|  | 56 | #  --signature_size          signature sizes in bytes in the following format: | 
|  | 57 | #                            "size1:size2[:...]" | 
|  | 58 | #  --payload_signature_file  the payload signature files in the following | 
|  | 59 | #                            format: | 
|  | 60 | #                            "payload_signature1:payload_signature2[:...]" | 
|  | 61 | #  --metadata_signature_file the metadata signature files in the following | 
|  | 62 | #                            format: | 
|  | 63 | #                            "metadata_signature1:metadata_signature2[:...]" | 
| Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 64 | #  --metadata_size_file      if defined, generate a file containing the size of | 
|  | 65 | #                            the signed payload metadata in bytes to the | 
|  | 66 | #                            specified file | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 67 | #  Note that the number of signature sizes and payload signatures have to match. | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 68 | # | 
|  | 69 | #  Properties command arguments: | 
|  | 70 | #  --payload                 the input signed or unsigned payload | 
|  | 71 | #  --properties_file         the output path where to write the properties, or | 
|  | 72 | #                            '-' for stdout. | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 73 | #  Verify command arguments: | 
|  | 74 | #  --payload             payload input file | 
|  | 75 | #  --source_image        verify payload to the specified source image. | 
|  | 76 | #  --target_image        the target image to verify upon. | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 77 | # | 
|  | 78 | #  Check command arguments: | 
|  | 79 | #     Symmetrical with the verify command. | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 80 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 81 |  | 
| Alex Deymo | 61e1fa8 | 2016-01-19 15:16:34 -0800 | [diff] [blame] | 82 | # Exit codes: | 
|  | 83 | EX_UNSUPPORTED_DELTA=100 | 
|  | 84 |  | 
| Jason Kusuma | f514c54 | 2015-11-05 18:43:45 -0800 | [diff] [blame] | 85 | warn() { | 
|  | 86 | echo "brillo_update_payload: warning: $*" >&2 | 
|  | 87 | } | 
|  | 88 |  | 
| Gilad Arnold | 957ce12 | 2015-10-14 16:02:55 -0700 | [diff] [blame] | 89 | die() { | 
|  | 90 | echo "brillo_update_payload: error: $*" >&2 | 
|  | 91 | exit 1 | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Parveen Kumar | b31e1ac | 2020-10-16 15:30:09 -0700 | [diff] [blame] | 94 | # Loads shflags. We first look at the default install location; then our own | 
|  | 95 | # directory; finally the parent directory. | 
| Gilad Arnold | 957ce12 | 2015-10-14 16:02:55 -0700 | [diff] [blame] | 96 | load_shflags() { | 
|  | 97 | local my_dir="$(dirname "$(readlink -f "$0")")" | 
|  | 98 | local path | 
| Parveen Kumar | b31e1ac | 2020-10-16 15:30:09 -0700 | [diff] [blame] | 99 | for path in /usr/share/misc \ | 
|  | 100 | "${my_dir}"/lib/shflags \ | 
|  | 101 | "${my_dir}"/../lib/shflags; do | 
| Gilad Arnold | 957ce12 | 2015-10-14 16:02:55 -0700 | [diff] [blame] | 102 | if [[ -r "${path}/shflags" ]]; then | 
|  | 103 | . "${path}/shflags" || die "Could not load ${path}/shflags." | 
|  | 104 | return | 
|  | 105 | fi | 
|  | 106 | done | 
|  | 107 | die "Could not find shflags." | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | load_shflags | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 111 |  | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 112 | HELP_GENERATE="generate: Generate an unsigned update payload." | 
|  | 113 | HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \ | 
|  | 114 | for signing." | 
|  | 115 | HELP_SIGN="sign: Insert the signatures into the unsigned payload." | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 116 | HELP_PROPERTIES="properties: Extract payload properties to a file." | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 117 | HELP_VERIFY="verify: Verify a (signed) update payload using delta_generator." | 
|  | 118 | HELP_CHECK="check: Check a (signed) update payload using paycheck (static \ | 
|  | 119 | testing)." | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | usage() { | 
|  | 122 | echo "Supported commands:" | 
|  | 123 | echo | 
|  | 124 | echo "${HELP_GENERATE}" | 
|  | 125 | echo "${HELP_HASH}" | 
|  | 126 | echo "${HELP_SIGN}" | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 127 | echo "${HELP_PROPERTIES}" | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 128 | echo "${HELP_VERIFY}" | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 129 | echo "${HELP_CHECK}" | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 130 | echo | 
|  | 131 | echo "Use: \"$0 <command> --help\" for more options." | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | # Check that a command is specified. | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 135 | if [[ $# -lt 1 ]]; then | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 136 | echo "Please specify a command [generate|hash|sign|properties|verify|check]" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 137 | exit 1 | 
|  | 138 | fi | 
|  | 139 |  | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 140 | # Parse command. | 
|  | 141 | COMMAND="${1:-}" | 
|  | 142 | shift | 
|  | 143 |  | 
|  | 144 | case "${COMMAND}" in | 
|  | 145 | generate) | 
|  | 146 | FLAGS_HELP="${HELP_GENERATE}" | 
|  | 147 | ;; | 
|  | 148 |  | 
|  | 149 | hash) | 
|  | 150 | FLAGS_HELP="${HELP_HASH}" | 
|  | 151 | ;; | 
|  | 152 |  | 
|  | 153 | sign) | 
|  | 154 | FLAGS_HELP="${HELP_SIGN}" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 155 | ;; | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 156 |  | 
|  | 157 | properties) | 
|  | 158 | FLAGS_HELP="${HELP_PROPERTIES}" | 
|  | 159 | ;; | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | verify) | 
|  | 162 | FLAGS_HELP="${HELP_VERIFY}" | 
|  | 163 | ;; | 
|  | 164 |  | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 165 | check) | 
|  | 166 | FLAGS_HELP="${HELP_CHECK}" | 
|  | 167 | ;; | 
|  | 168 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 169 | *) | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 170 | echo "Unrecognized command: \"${COMMAND}\"" >&2 | 
|  | 171 | usage >&2 | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 172 | exit 1 | 
|  | 173 | ;; | 
|  | 174 | esac | 
|  | 175 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 176 | # Flags | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 177 | FLAGS_HELP="Usage: $0 ${COMMAND} [flags] | 
|  | 178 | ${FLAGS_HELP}" | 
|  | 179 |  | 
|  | 180 | if [[ "${COMMAND}" == "generate" ]]; then | 
|  | 181 | DEFINE_string payload "" \ | 
|  | 182 | "Path to output the generated unsigned payload file." | 
|  | 183 | DEFINE_string target_image "" \ | 
|  | 184 | "Path to the target image that should be sent to clients." | 
|  | 185 | DEFINE_string source_image "" \ | 
|  | 186 | "Optional: Path to a source image. If specified, this makes a delta update." | 
| Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 187 | DEFINE_string metadata_size_file "" \ | 
|  | 188 | "Optional: Path to output metadata size." | 
| Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 189 | DEFINE_string max_timestamp "" \ | 
|  | 190 | "Optional: The maximum unix timestamp of the OS allowed to apply this \ | 
|  | 191 | payload, should be set to a number higher than the build timestamp of the \ | 
|  | 192 | system running on the device, 0 if not specified." | 
| Kelvin Zhang | 1f49642 | 2020-08-11 17:18:23 -0400 | [diff] [blame] | 193 | DEFINE_string partition_timestamps "" \ | 
|  | 194 | "Optional: Per-partition maximum unix timestamp of the OS allowed to \ | 
|  | 195 | apply this payload. Should be a comma separated key value pairs. e.x.\ | 
|  | 196 | system:1234,vendor:456" | 
| Tianjie Xu | 72d512c | 2019-08-21 15:20:35 -0700 | [diff] [blame] | 197 | DEFINE_string disable_fec_computation "" \ | 
|  | 198 | "Optional: Disables the on device fec data computation for incremental \ | 
|  | 199 | update. This feature is enabled by default." | 
| Kelvin Zhang | 098e79a | 2020-11-19 17:40:56 -0500 | [diff] [blame] | 200 | DEFINE_string disable_verity_computation "" \ | 
|  | 201 | "Optional: Disables the on device verity computation for incremental \ | 
|  | 202 | update. This feature is enabled by default." | 
| Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 203 | DEFINE_string is_partial_update "" \ | 
|  | 204 | "Optional: True if the payload is for partial update. i.e. it only updates \ | 
|  | 205 | a subset of partitions on device." | 
| Kelvin Zhang | dde2ef4 | 2020-11-20 12:26:19 -0500 | [diff] [blame] | 206 | DEFINE_string full_boot "" "Will include full boot image" | 
| Kelvin Zhang | 9101ff3 | 2021-01-19 15:48:53 -0500 | [diff] [blame] | 207 | DEFINE_string disable_vabc "" \ | 
|  | 208 | "Optional: Disables Virtual AB Compression when installing the OTA" | 
| Kelvin Zhang | 02df21b | 2021-01-07 14:55:18 -0500 | [diff] [blame] | 209 | DEFINE_string enable_vabc_xor "" \ | 
|  | 210 | "Optional: Enable the use of Virtual AB Compression XOR feature" | 
| Tianjie | f4502bb | 2021-09-08 19:08:53 -0700 | [diff] [blame] | 211 | DEFINE_string force_minor_version "" \ | 
|  | 212 | "Optional: Override the minor version for the delta generation." | 
| Kelvin Zhang | 0aa4fae | 2021-10-28 09:15:27 -0700 | [diff] [blame] | 213 | DEFINE_string compressor_types "" \ | 
|  | 214 | "Optional: allowed compressor types. Colon separated, allowe values are bz2 and brotli" | 
| Kelvin Zhang | 2298798 | 2022-01-04 14:37:28 -0800 | [diff] [blame] | 215 | DEFINE_string enable_zucchini "" \ | 
|  | 216 | "Optional: Whether to enable zucchini diffing" | 
| Kelvin Zhang | 907b0e6 | 2022-01-10 06:23:10 -0800 | [diff] [blame] | 217 | DEFINE_string enable_lz4diff "" \ | 
|  | 218 | "Optional: Whether to enable lz4 diffing for EROFS" | 
|  | 219 | DEFINE_string liblz4_path "" \ | 
|  | 220 | "Required if --enabled_lz4diff true is passed. Path to liblz4.so. delta_generator will use this copy of liblz4.so for compression. It is important that this copy of liblz4.so is the same as the one on source build." | 
| Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 221 | DEFINE_string erofs_compression_param "" \ | 
|  | 222 | "Compression parameter passed to mkfs.erofs's -z option." | 
| Kelvin Zhang | cfc531f | 2022-08-24 17:58:53 +0000 | [diff] [blame] | 223 | DEFINE_string security_patch_level "" \ | 
|  | 224 | "Optional: security patch level of this OTA" | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 225 | fi | 
|  | 226 | if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then | 
|  | 227 | DEFINE_string unsigned_payload "" "Path to the input unsigned payload." | 
|  | 228 | DEFINE_string signature_size "" \ | 
|  | 229 | "Signature sizes in bytes in the following format: size1:size2[:...]" | 
|  | 230 | fi | 
|  | 231 | if [[ "${COMMAND}" == "hash" ]]; then | 
|  | 232 | DEFINE_string metadata_hash_file "" \ | 
|  | 233 | "Optional: Path to output metadata hash file." | 
|  | 234 | DEFINE_string payload_hash_file "" \ | 
|  | 235 | "Optional: Path to output payload hash file." | 
|  | 236 | fi | 
|  | 237 | if [[ "${COMMAND}" == "sign" ]]; then | 
|  | 238 | DEFINE_string payload "" \ | 
|  | 239 | "Path to output the generated unsigned payload file." | 
|  | 240 | DEFINE_string metadata_signature_file "" \ | 
|  | 241 | "The metatada signatures in the following format: \ | 
|  | 242 | metadata_signature1:metadata_signature2[:...]" | 
|  | 243 | DEFINE_string payload_signature_file "" \ | 
|  | 244 | "The payload signatures in the following format: \ | 
|  | 245 | payload_signature1:payload_signature2[:...]" | 
| Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 246 | DEFINE_string metadata_size_file "" \ | 
|  | 247 | "Optional: Path to output metadata size." | 
| Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 248 | fi | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 249 | if [[ "${COMMAND}" == "properties" ]]; then | 
|  | 250 | DEFINE_string payload "" \ | 
|  | 251 | "Path to the input signed or unsigned payload file." | 
|  | 252 | DEFINE_string properties_file "-" \ | 
|  | 253 | "Path to output the extracted property files. If '-' is passed stdout will \ | 
|  | 254 | be used." | 
|  | 255 | fi | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 256 | if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 257 | DEFINE_string payload "" \ | 
|  | 258 | "Path to the input payload file." | 
|  | 259 | DEFINE_string target_image "" \ | 
|  | 260 | "Path to the target image to verify upon." | 
|  | 261 | DEFINE_string source_image "" \ | 
|  | 262 | "Optional: Path to a source image. If specified, the delta update is \ | 
|  | 263 | applied to this." | 
|  | 264 | fi | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 265 |  | 
| Alex Deymo | 5fbb110 | 2017-01-12 13:55:52 -0800 | [diff] [blame] | 266 | DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files." | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 267 |  | 
|  | 268 | # Parse command line flag arguments | 
|  | 269 | FLAGS "$@" || exit 1 | 
|  | 270 | eval set -- "${FLAGS_ARGV}" | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 271 | set -e | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 272 |  | 
| Alex Deymo | 5fbb110 | 2017-01-12 13:55:52 -0800 | [diff] [blame] | 273 | # Override the TMPDIR with the passed work_dir flags, which anyway defaults to | 
|  | 274 | # ${TMPDIR}. | 
|  | 275 | TMPDIR="${FLAGS_work_dir}" | 
|  | 276 | export TMPDIR | 
|  | 277 |  | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 278 | # Associative arrays from partition name to file in the source and target | 
|  | 279 | # images. The size of the updated area must be the size of the file. | 
|  | 280 | declare -A SRC_PARTITIONS | 
|  | 281 | declare -A DST_PARTITIONS | 
|  | 282 |  | 
| Alex Deymo | 20bdc70 | 2016-12-07 21:07:11 -0800 | [diff] [blame] | 283 | # Associative arrays for the .map files associated with each src/dst partition | 
|  | 284 | # file in SRC_PARTITIONS and DST_PARTITIONS. | 
|  | 285 | declare -A SRC_PARTITIONS_MAP | 
|  | 286 | declare -A DST_PARTITIONS_MAP | 
|  | 287 |  | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 288 | # List of partition names in order. | 
|  | 289 | declare -a PARTITIONS_ORDER | 
|  | 290 |  | 
| Tao Bao | 9648990 | 2019-04-02 16:25:03 -0700 | [diff] [blame] | 291 | # A list of PIDs of the extract_image workers. | 
|  | 292 | EXTRACT_IMAGE_PIDS=() | 
|  | 293 |  | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 294 | # A list of temporary files to remove during cleanup. | 
|  | 295 | CLEANUP_FILES=() | 
|  | 296 |  | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 297 | # Global options to force the version of the payload. | 
|  | 298 | FORCE_MAJOR_VERSION="" | 
|  | 299 | FORCE_MINOR_VERSION="" | 
|  | 300 |  | 
| Sen Jiang | 6f7b22c | 2015-11-12 15:50:39 -0800 | [diff] [blame] | 301 | # Path to the postinstall config file in target image if exists. | 
|  | 302 | POSTINSTALL_CONFIG_FILE="" | 
|  | 303 |  | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 304 | # Path to the dynamic partition info file in target image if exists. | 
|  | 305 | DYNAMIC_PARTITION_INFO_FILE="" | 
|  | 306 |  | 
| Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 307 | # Path to the META/apex_info.pb found in target build | 
|  | 308 | APEX_INFO_FILE="" | 
|  | 309 |  | 
| Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 310 | # read_option_int <file.txt> <option_key> [default_value] | 
|  | 311 | # | 
|  | 312 | # Reads the unsigned integer value associated with |option_key| in a key=value | 
|  | 313 | # file |file.txt|. Prints the read value if found and valid, otherwise prints | 
|  | 314 | # the |default_value|. | 
|  | 315 | read_option_uint() { | 
|  | 316 | local file_txt="$1" | 
|  | 317 | local option_key="$2" | 
|  | 318 | local default_value="${3:-}" | 
|  | 319 | local value | 
| Tao Bao | c288d5b | 2019-10-03 13:47:06 -0700 | [diff] [blame] | 320 | if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then | 
| Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 321 | if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then | 
|  | 322 | echo "${value}" | 
|  | 323 | return | 
|  | 324 | fi | 
|  | 325 | fi | 
|  | 326 | echo "${default_value}" | 
|  | 327 | } | 
|  | 328 |  | 
| Sen Jiang | d0e9a89 | 2016-07-22 16:28:07 -0700 | [diff] [blame] | 329 | # truncate_file <file_path> <file_size> | 
|  | 330 | # | 
| Dan Willemsen | 3271186 | 2018-10-04 21:25:50 -0700 | [diff] [blame] | 331 | # Truncate the given |file_path| to |file_size| using python. | 
| Sen Jiang | d0e9a89 | 2016-07-22 16:28:07 -0700 | [diff] [blame] | 332 | # The truncate binary might not be available. | 
|  | 333 | truncate_file() { | 
|  | 334 | local file_path="$1" | 
|  | 335 | local file_size="$2" | 
| Dan Willemsen | 3271186 | 2018-10-04 21:25:50 -0700 | [diff] [blame] | 336 | python -c "open(\"${file_path}\", 'a').truncate(${file_size})" | 
| Sen Jiang | d0e9a89 | 2016-07-22 16:28:07 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 339 | # Create a temporary file in the work_dir with an optional pattern name. | 
|  | 340 | # Prints the name of the newly created file. | 
|  | 341 | create_tempfile() { | 
|  | 342 | local pattern="${1:-tempfile.XXXXXX}" | 
|  | 343 | mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}" | 
|  | 344 | } | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 345 |  | 
|  | 346 | cleanup() { | 
|  | 347 | local err="" | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 348 | rm -f "${CLEANUP_FILES[@]}" || err=1 | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 349 |  | 
|  | 350 | # If we are cleaning up after an error, or if we got an error during | 
|  | 351 | # cleanup (even if we eventually succeeded) return a non-zero exit | 
|  | 352 | # code. This triggers additional logging in most environments that call | 
|  | 353 | # this script. | 
|  | 354 | if [[ -n "${err}" ]]; then | 
|  | 355 | die "Cleanup encountered an error." | 
|  | 356 | fi | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | cleanup_on_error() { | 
|  | 360 | trap - INT TERM ERR EXIT | 
|  | 361 | cleanup | 
|  | 362 | die "Cleanup success after an error." | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | cleanup_on_exit() { | 
|  | 366 | trap - INT TERM ERR EXIT | 
|  | 367 | cleanup | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | trap cleanup_on_error INT TERM ERR | 
|  | 371 | trap cleanup_on_exit EXIT | 
|  | 372 |  | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 373 | # extract_file <zip_file> <entry_name> <destination> | 
|  | 374 | # | 
|  | 375 | # Extracts |entry_name| from |zip_file| to |destination|. | 
|  | 376 | extract_file() { | 
|  | 377 | local zip_file="$1" | 
|  | 378 | local entry_name="$2" | 
|  | 379 | local destination="$3" | 
|  | 380 |  | 
|  | 381 | # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory | 
|  | 382 | # as the destination of the unzip, and move the file to the intended | 
|  | 383 | # destination. | 
|  | 384 | local output_directory=$( | 
|  | 385 | mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX") | 
|  | 386 | unzip "${zip_file}" "${entry_name}" -d "${output_directory}" || | 
|  | 387 | { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; } | 
|  | 388 |  | 
|  | 389 | mv "${output_directory}/${entry_name}" "${destination}" | 
|  | 390 | rm -rf "${output_directory}" | 
|  | 391 | } | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 392 |  | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 393 | # extract_image <image> <partitions_array> [partitions_order] | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 394 | # | 
|  | 395 | # Detect the format of the |image| file and extract its updatable partitions | 
|  | 396 | # into new temporary files. Add the list of partition names and its files to the | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 397 | # associative array passed in |partitions_array|. If |partitions_order| is | 
|  | 398 | # passed, set it to list of partition names in order. | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 399 | extract_image() { | 
|  | 400 | local image="$1" | 
|  | 401 |  | 
|  | 402 | # Brillo images are zip files. We detect the 4-byte magic header of the zip | 
|  | 403 | # file. | 
| Elliott Hughes | 5df503c | 2018-11-27 16:57:34 -0800 | [diff] [blame] | 404 | local magic=$(xxd -p -l4 "${image}") | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 405 | if [[ "${magic}" == "504b0304" ]]; then | 
|  | 406 | echo "Detected .zip file, extracting Brillo image." | 
|  | 407 | extract_image_brillo "$@" | 
|  | 408 | return | 
|  | 409 | fi | 
|  | 410 |  | 
|  | 411 | # Chrome OS images are GPT partitioned disks. We should have the cgpt binary | 
|  | 412 | # bundled here and we will use it to extract the partitions, so the GPT | 
|  | 413 | # headers must be valid. | 
|  | 414 | if cgpt show -q -n "${image}" >/dev/null; then | 
|  | 415 | echo "Detected GPT image, extracting Chrome OS image." | 
|  | 416 | extract_image_cros "$@" | 
|  | 417 | return | 
|  | 418 | fi | 
|  | 419 |  | 
|  | 420 | die "Couldn't detect the image format of ${image}" | 
|  | 421 | } | 
|  | 422 |  | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 423 | # extract_image_cros <image.bin> <partitions_array> [partitions_order] | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 424 | # | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 425 | # Extract Chromium OS recovery images into new temporary files. | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 426 | extract_image_cros() { | 
|  | 427 | local image="$1" | 
|  | 428 | local partitions_array="$2" | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 429 | local partitions_order="${3:-}" | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 430 |  | 
|  | 431 | local kernel root | 
|  | 432 | kernel=$(create_tempfile "kernel.bin.XXXXXX") | 
|  | 433 | CLEANUP_FILES+=("${kernel}") | 
|  | 434 | root=$(create_tempfile "root.bin.XXXXXX") | 
|  | 435 | CLEANUP_FILES+=("${root}") | 
|  | 436 |  | 
|  | 437 | cros_generate_update_payload --extract \ | 
|  | 438 | --image "${image}" \ | 
| Amin Hassani | 58e01d6 | 2018-09-19 14:56:15 -0700 | [diff] [blame] | 439 | --kern_path "${kernel}" --root_path "${root}" | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 440 |  | 
| Amin Hassani | 58e01d6 | 2018-09-19 14:56:15 -0700 | [diff] [blame] | 441 | # Chrome OS now uses major_version 2 payloads for all boards. | 
|  | 442 | # See crbug.com/794404 for more information. | 
|  | 443 | FORCE_MAJOR_VERSION="2" | 
| Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 444 |  | 
| Tudor Brindus | dda79e2 | 2018-06-28 18:03:21 -0700 | [diff] [blame] | 445 | eval ${partitions_array}[kernel]=\""${kernel}"\" | 
|  | 446 | eval ${partitions_array}[root]=\""${root}"\" | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 447 |  | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 448 | if [[ -n "${partitions_order}" ]]; then | 
| Tudor Brindus | dda79e2 | 2018-06-28 18:03:21 -0700 | [diff] [blame] | 449 | eval "${partitions_order}=( \"root\" \"kernel\" )" | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 450 | fi | 
|  | 451 |  | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 452 | local part varname | 
| Tudor Brindus | dda79e2 | 2018-06-28 18:03:21 -0700 | [diff] [blame] | 453 | for part in kernel root; do | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 454 | varname="${partitions_array}[${part}]" | 
|  | 455 | printf "md5sum of %s: " "${varname}" | 
|  | 456 | md5sum "${!varname}" | 
|  | 457 | done | 
|  | 458 | } | 
|  | 459 |  | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 460 | # extract_partition_brillo <target_files.zip> <partitions_array> <partition> | 
|  | 461 | #     <part_file> <part_map_file> | 
|  | 462 | # | 
|  | 463 | # Extract the <partition> from target_files zip file into <part_file> and its | 
|  | 464 | # map file into <part_map_file>. | 
|  | 465 | extract_partition_brillo() { | 
|  | 466 | local image="$1" | 
|  | 467 | local partitions_array="$2" | 
|  | 468 | local part="$3" | 
|  | 469 | local part_file="$4" | 
|  | 470 | local part_map_file="$5" | 
|  | 471 |  | 
|  | 472 | # For each partition, we in turn look for its image file under IMAGES/ and | 
|  | 473 | # RADIO/ in the given target_files zip file. | 
|  | 474 | local path path_in_zip | 
|  | 475 | for path in IMAGES RADIO; do | 
|  | 476 | if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then | 
|  | 477 | path_in_zip="${path}" | 
|  | 478 | break | 
|  | 479 | fi | 
|  | 480 | done | 
|  | 481 | [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img" | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 482 | extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}" | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 483 |  | 
|  | 484 | # If the partition is stored as an Android sparse image file, we need to | 
|  | 485 | # convert them to a raw image for the update. | 
| Yifan Hong | 4b821d7 | 2018-12-07 17:26:04 -0800 | [diff] [blame] | 486 | local magic=$(xxd -p -l4 "${part_file}") | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 487 | if [[ "${magic}" == "3aff26ed" ]]; then | 
|  | 488 | local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX") | 
|  | 489 | echo "Converting Android sparse image ${part}.img to RAW." | 
|  | 490 | mv "${part_file}" "${temp_sparse}" | 
|  | 491 | simg2img "${temp_sparse}" "${part_file}" | 
|  | 492 | rm -f "${temp_sparse}" | 
|  | 493 | fi | 
|  | 494 |  | 
|  | 495 | # Extract the .map file (if one is available). | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 496 | if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then | 
|  | 497 | extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}" | 
|  | 498 | fi | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 499 |  | 
|  | 500 | # delta_generator only supports images multiple of 4 KiB. For target images | 
|  | 501 | # we pad the data with zeros if needed, but for source images we truncate | 
|  | 502 | # down the data since the last block of the old image could be padded on | 
|  | 503 | # disk with unknown data. | 
|  | 504 | local filesize=$(stat -c%s "${part_file}") | 
|  | 505 | if [[ $(( filesize % 4096 )) -ne 0 ]]; then | 
|  | 506 | if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then | 
|  | 507 | echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB." | 
|  | 508 | : $(( filesize = filesize & -4096 )) | 
|  | 509 | else | 
|  | 510 | echo "Rounding UP partition ${part}.img to a multiple of 4 KiB." | 
|  | 511 | : $(( filesize = (filesize + 4095) & -4096 )) | 
|  | 512 | fi | 
|  | 513 | truncate_file "${part_file}" "${filesize}" | 
|  | 514 | fi | 
|  | 515 |  | 
|  | 516 | echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes" | 
|  | 517 | } | 
|  | 518 |  | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 519 | # extract_image_brillo <target_files.zip> <partitions_array> [partitions_order] | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 520 | # | 
|  | 521 | # Extract the A/B updated partitions from a Brillo target_files zip file into | 
|  | 522 | # new temporary files. | 
|  | 523 | extract_image_brillo() { | 
|  | 524 | local image="$1" | 
|  | 525 | local partitions_array="$2" | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 526 | local partitions_order="${3:-}" | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 527 |  | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 528 | local partitions=( "boot" "system" ) | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 529 | local ab_partitions_list | 
|  | 530 | ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX") | 
|  | 531 | CLEANUP_FILES+=("${ab_partitions_list}") | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 532 | if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then | 
|  | 533 | extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}" | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 534 | if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then | 
|  | 535 | die "Invalid partition names found in the partition list." | 
|  | 536 | fi | 
| Sen Jiang | 34c711a | 2017-10-25 17:25:21 -0700 | [diff] [blame] | 537 | # Get partition list without duplicates. | 
|  | 538 | partitions=($(awk '!seen[$0]++' "${ab_partitions_list}")) | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 539 | if [[ ${#partitions[@]} -eq 0 ]]; then | 
|  | 540 | die "The list of partitions is empty. Can't generate a payload." | 
|  | 541 | fi | 
|  | 542 | else | 
|  | 543 | warn "No ab_partitions.txt found. Using default." | 
|  | 544 | fi | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 545 | echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}" | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 546 |  | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 547 | if [[ -n "${partitions_order}" ]]; then | 
|  | 548 | eval "${partitions_order}=(${partitions[@]})" | 
|  | 549 | fi | 
|  | 550 |  | 
| Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 551 | # All Brillo updaters support major version 2. | 
|  | 552 | FORCE_MAJOR_VERSION="2" | 
|  | 553 |  | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 554 | if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then | 
| Sen Jiang | 6f7b22c | 2015-11-12 15:50:39 -0800 | [diff] [blame] | 555 | # Source image | 
|  | 556 | local ue_config=$(create_tempfile "ue_config.XXXXXX") | 
| Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 557 | CLEANUP_FILES+=("${ue_config}") | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 558 | if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then | 
|  | 559 | extract_file "${image}" "META/update_engine_config.txt" "${ue_config}" | 
|  | 560 | else | 
| Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 561 | warn "No update_engine_config.txt found. Assuming pre-release image, \ | 
|  | 562 | using payload minor version 2" | 
|  | 563 | fi | 
| Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 564 | # For delta payloads, we use the major and minor version supported by the | 
|  | 565 | # old updater. | 
| Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 566 | FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \ | 
|  | 567 | "PAYLOAD_MINOR_VERSION" 2) | 
| Tianjie | f4502bb | 2021-09-08 19:08:53 -0700 | [diff] [blame] | 568 | if [[ -n "${FLAGS_force_minor_version}" ]]; then | 
|  | 569 | FORCE_MINOR_VERSION="${FLAGS_force_minor_version}" | 
|  | 570 | fi | 
| Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 571 | FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \ | 
|  | 572 | "PAYLOAD_MAJOR_VERSION" 2) | 
| Alex Deymo | 61e1fa8 | 2016-01-19 15:16:34 -0800 | [diff] [blame] | 573 |  | 
|  | 574 | # Brillo support for deltas started with minor version 3. | 
|  | 575 | if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then | 
|  | 576 | warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \ | 
|  | 577 | Disabling deltas for this source version." | 
|  | 578 | exit ${EX_UNSUPPORTED_DELTA} | 
|  | 579 | fi | 
| Sen Jiang | 6f7b22c | 2015-11-12 15:50:39 -0800 | [diff] [blame] | 580 | else | 
|  | 581 | # Target image | 
|  | 582 | local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX") | 
|  | 583 | CLEANUP_FILES+=("${postinstall_config}") | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 584 | if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then | 
|  | 585 | extract_file "${image}" "META/postinstall_config.txt" \ | 
|  | 586 | "${postinstall_config}" | 
| Sen Jiang | 6f7b22c | 2015-11-12 15:50:39 -0800 | [diff] [blame] | 587 | POSTINSTALL_CONFIG_FILE="${postinstall_config}" | 
|  | 588 | fi | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 589 | local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX") | 
|  | 590 | CLEANUP_FILES+=("${dynamic_partitions_info}") | 
| Tianjie Xu | 14715ce | 2019-08-06 17:24:43 -0700 | [diff] [blame] | 591 | if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then | 
|  | 592 | extract_file "${image}" "META/dynamic_partitions_info.txt" \ | 
|  | 593 | "${dynamic_partitions_info}" | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 594 | DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}" | 
|  | 595 | fi | 
| Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 596 | local apex_info=$(create_tempfile "apex_info.XXXXXX") | 
|  | 597 | CLEANUP_FILES+=("${apex_info}") | 
|  | 598 | if unzip -l "${image}" "META/apex_info.pb" > /dev/null; then | 
|  | 599 | extract_file "${image}" "META/apex_info.pb" \ | 
|  | 600 | "${apex_info}" | 
|  | 601 | APEX_INFO_FILE="${apex_info}" | 
|  | 602 | fi | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 603 | fi | 
|  | 604 |  | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 605 | local part | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 606 | for part in "${partitions[@]}"; do | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 607 | local part_file=$(create_tempfile "${part}.img.XXXXXX") | 
|  | 608 | local part_map_file=$(create_tempfile "${part}.map.XXXXXX") | 
|  | 609 | CLEANUP_FILES+=("${part_file}" "${part_map_file}") | 
|  | 610 | # Extract partitions in background. | 
|  | 611 | extract_partition_brillo "${image}" "${partitions_array}" "${part}" \ | 
|  | 612 | "${part_file}" "${part_map_file}" & | 
| Tao Bao | 9648990 | 2019-04-02 16:25:03 -0700 | [diff] [blame] | 613 | EXTRACT_IMAGE_PIDS+=("$!") | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 614 | eval "${partitions_array}[\"${part}\"]=\"${part_file}\"" | 
| Alex Deymo | 20bdc70 | 2016-12-07 21:07:11 -0800 | [diff] [blame] | 615 | eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\"" | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 616 | done | 
|  | 617 | } | 
|  | 618 |  | 
|  | 619 | # cleanup_partition_array <partitions_array> | 
|  | 620 | # | 
|  | 621 | # Remove all empty files in <partitions_array>. | 
|  | 622 | cleanup_partition_array() { | 
|  | 623 | local partitions_array="$1" | 
|  | 624 | # Have to use eval to iterate over associative array keys with variable array | 
|  | 625 | # names, we should change it to use nameref once bash 4.3 is available | 
|  | 626 | # everywhere. | 
|  | 627 | for part in $(eval "echo \${!${partitions_array}[@]}"); do | 
|  | 628 | local path="${partitions_array}[$part]" | 
|  | 629 | if [[ ! -s "${!path}" ]]; then | 
|  | 630 | eval "unset ${partitions_array}[${part}]" | 
|  | 631 | fi | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 632 | done | 
|  | 633 | } | 
|  | 634 |  | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 635 | extract_payload_images() { | 
|  | 636 | local payload_type=$1 | 
|  | 637 | echo "Extracting images for ${payload_type} update." | 
|  | 638 |  | 
|  | 639 | if [[ "${payload_type}" == "delta" ]]; then | 
|  | 640 | extract_image "${FLAGS_source_image}" SRC_PARTITIONS | 
|  | 641 | fi | 
|  | 642 | extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER | 
| Tao Bao | 9648990 | 2019-04-02 16:25:03 -0700 | [diff] [blame] | 643 | # Wait for all subprocesses to finish. Not using `wait` since it doesn't die | 
|  | 644 | # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}` | 
|  | 645 | # as it gives the status of the last process it has waited for. | 
|  | 646 | for pid in ${EXTRACT_IMAGE_PIDS[@]}; do | 
|  | 647 | wait ${pid} | 
|  | 648 | done | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 649 | cleanup_partition_array SRC_PARTITIONS | 
|  | 650 | cleanup_partition_array SRC_PARTITIONS_MAP | 
|  | 651 | cleanup_partition_array DST_PARTITIONS | 
|  | 652 | cleanup_partition_array DST_PARTITIONS_MAP | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 653 | } | 
|  | 654 |  | 
|  | 655 | get_payload_type() { | 
|  | 656 | if [[ -z "${FLAGS_source_image}" ]]; then | 
|  | 657 | echo "full" | 
|  | 658 | else | 
|  | 659 | echo "delta" | 
|  | 660 | fi | 
|  | 661 | } | 
|  | 662 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 663 | validate_generate() { | 
|  | 664 | [[ -n "${FLAGS_payload}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 665 | die "You must specify an output filename with --payload FILENAME" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 666 |  | 
|  | 667 | [[ -n "${FLAGS_target_image}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 668 | die "You must specify a target image with --target_image FILENAME" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 669 | } | 
|  | 670 |  | 
|  | 671 | cmd_generate() { | 
| Sen Jiang | 3e5804d | 2018-09-06 15:53:00 -0700 | [diff] [blame] | 672 | local payload_type=$(get_payload_type) | 
|  | 673 | extract_payload_images ${payload_type} | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 674 |  | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 675 | echo "Generating ${payload_type} update." | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 676 | # Common payload args: | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 677 | GENERATOR_ARGS=( --out_file="${FLAGS_payload}" ) | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 678 |  | 
|  | 679 | local part old_partitions="" new_partitions="" partition_names="" | 
| Alex Deymo | 20bdc70 | 2016-12-07 21:07:11 -0800 | [diff] [blame] | 680 | local old_mapfiles="" new_mapfiles="" | 
| Sen Jiang | 788c2d9 | 2016-03-09 12:48:40 -0800 | [diff] [blame] | 681 | for part in "${PARTITIONS_ORDER[@]}"; do | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 682 | if [[ -n "${partition_names}" ]]; then | 
|  | 683 | partition_names+=":" | 
|  | 684 | new_partitions+=":" | 
|  | 685 | old_partitions+=":" | 
| Alex Deymo | 20bdc70 | 2016-12-07 21:07:11 -0800 | [diff] [blame] | 686 | new_mapfiles+=":" | 
|  | 687 | old_mapfiles+=":" | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 688 | fi | 
|  | 689 | partition_names+="${part}" | 
|  | 690 | new_partitions+="${DST_PARTITIONS[${part}]}" | 
| Kelvin Zhang | 999705e | 2020-11-03 10:07:09 -0500 | [diff] [blame] | 691 | if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then | 
|  | 692 | # Skip boot partition. | 
|  | 693 | old_partitions+="" | 
|  | 694 | else | 
|  | 695 | old_partitions+="${SRC_PARTITIONS[${part}]:-}" | 
|  | 696 | fi | 
| Alex Deymo | 20bdc70 | 2016-12-07 21:07:11 -0800 | [diff] [blame] | 697 | new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}" | 
|  | 698 | old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}" | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 699 | done | 
|  | 700 |  | 
|  | 701 | # Target image args: | 
|  | 702 | GENERATOR_ARGS+=( | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 703 | --partition_names="${partition_names}" | 
|  | 704 | --new_partitions="${new_partitions}" | 
|  | 705 | --new_mapfiles="${new_mapfiles}" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 706 | ) | 
|  | 707 |  | 
| Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 708 | if [[ "${FLAGS_is_partial_update}" == "true" ]]; then | 
|  | 709 | GENERATOR_ARGS+=( --is_partial_update="true" ) | 
|  | 710 | # Need at least minor version 7 for partial update, so generate with minor | 
|  | 711 | # version 7 if we don't have a source image. Let the delta_generator to fail | 
|  | 712 | # the other incompatiable minor versions. | 
|  | 713 | if [[ -z "${FORCE_MINOR_VERSION}" ]]; then | 
|  | 714 | FORCE_MINOR_VERSION="7" | 
|  | 715 | fi | 
|  | 716 | fi | 
|  | 717 |  | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 718 | if [[ "${payload_type}" == "delta" ]]; then | 
| Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame] | 719 | # Source image args: | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 720 | GENERATOR_ARGS+=( | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 721 | --old_partitions="${old_partitions}" | 
|  | 722 | --old_mapfiles="${old_mapfiles}" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 723 | ) | 
| Tianjie Xu | 72d512c | 2019-08-21 15:20:35 -0700 | [diff] [blame] | 724 | if [[ -n "${FLAGS_disable_fec_computation}" ]]; then | 
|  | 725 | GENERATOR_ARGS+=( | 
|  | 726 | --disable_fec_computation="${FLAGS_disable_fec_computation}" ) | 
|  | 727 | fi | 
| Kelvin Zhang | 098e79a | 2020-11-19 17:40:56 -0500 | [diff] [blame] | 728 | if [[ -n "${FLAGS_disable_verity_computation}" ]]; then | 
|  | 729 | GENERATOR_ARGS+=( | 
|  | 730 | --disable_verity_computation="${FLAGS_disable_verity_computation}" ) | 
|  | 731 | fi | 
| Kelvin Zhang | 0aa4fae | 2021-10-28 09:15:27 -0700 | [diff] [blame] | 732 | if [[ -n "${FLAGS_compressor_types}" ]]; then | 
|  | 733 | GENERATOR_ARGS+=( | 
|  | 734 | --compressor_types="${FLAGS_compressor_types}" ) | 
|  | 735 | fi | 
| Kelvin Zhang | 2298798 | 2022-01-04 14:37:28 -0800 | [diff] [blame] | 736 | if [[ -n "${FLAGS_enable_zucchini}" ]]; then | 
|  | 737 | GENERATOR_ARGS+=( | 
|  | 738 | --enable_zucchini="${FLAGS_enable_zucchini}" ) | 
|  | 739 | fi | 
| Kelvin Zhang | 907b0e6 | 2022-01-10 06:23:10 -0800 | [diff] [blame] | 740 | if [[ -n "${FLAGS_enable_lz4diff}" ]]; then | 
|  | 741 | if [[ "${FLAGS_enable_lz4diff}" == "true" && -z "${FLAGS_liblz4_path}" ]]; then | 
|  | 742 | echo "--liblz4_path is required when enable_lz4diff is set to true." | 
|  | 743 | exit 1 | 
|  | 744 | fi | 
|  | 745 | GENERATOR_ARGS+=( | 
|  | 746 | --enable_lz4diff="${FLAGS_enable_lz4diff}" ) | 
|  | 747 | fi | 
| Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 748 | if [[ -n "${FLAGS_erofs_compression_param}" ]]; then | 
|  | 749 | GENERATOR_ARGS+=( | 
|  | 750 | --erofs_compression_param="${FLAGS_erofs_compression_param}" ) | 
|  | 751 | fi | 
| Kelvin Zhang | 413982e | 2021-03-02 15:34:50 -0500 | [diff] [blame] | 752 | fi | 
|  | 753 |  | 
| Kelvin Zhang | 02df21b | 2021-01-07 14:55:18 -0500 | [diff] [blame] | 754 | if [[ -n "${FLAGS_enable_vabc_xor}" ]]; then | 
|  | 755 | GENERATOR_ARGS+=( | 
|  | 756 | --enable_vabc_xor="${FLAGS_enable_vabc_xor}" ) | 
|  | 757 | fi | 
|  | 758 |  | 
| Kelvin Zhang | 413982e | 2021-03-02 15:34:50 -0500 | [diff] [blame] | 759 | if [[ -n "${FLAGS_disable_vabc}" ]]; then | 
|  | 760 | GENERATOR_ARGS+=( | 
|  | 761 | --disable_vabc="${FLAGS_disable_vabc}" ) | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 762 | fi | 
|  | 763 |  | 
| Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 764 | # minor version is set only for delta or partial payload. | 
|  | 765 | if [[ -n "${FORCE_MINOR_VERSION}" ]]; then | 
|  | 766 | GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" ) | 
|  | 767 | fi | 
|  | 768 |  | 
| Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 769 | if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then | 
|  | 770 | GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" ) | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 771 | fi | 
|  | 772 |  | 
| Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 773 | if [[ -n "${FLAGS_metadata_size_file}" ]]; then | 
|  | 774 | GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" ) | 
|  | 775 | fi | 
|  | 776 |  | 
| Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 777 | if [[ -n "${FLAGS_max_timestamp}" ]]; then | 
|  | 778 | GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" ) | 
|  | 779 | fi | 
|  | 780 |  | 
| Kelvin Zhang | cfc531f | 2022-08-24 17:58:53 +0000 | [diff] [blame] | 781 | if [[ -n "${FLAGS_security_patch_level}" ]]; then | 
|  | 782 | GENERATOR_ARGS+=( --security_patch_level="${FLAGS_security_patch_level}" ) | 
|  | 783 | fi | 
|  | 784 |  | 
| Kelvin Zhang | 1f49642 | 2020-08-11 17:18:23 -0400 | [diff] [blame] | 785 | if [[ -n "${FLAGS_partition_timestamps}" ]]; then | 
|  | 786 | GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" ) | 
|  | 787 | fi | 
|  | 788 |  | 
| Sen Jiang | 6f7b22c | 2015-11-12 15:50:39 -0800 | [diff] [blame] | 789 | if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then | 
|  | 790 | GENERATOR_ARGS+=( | 
|  | 791 | --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}" | 
|  | 792 | ) | 
|  | 793 | fi | 
|  | 794 |  | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 795 | if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then | 
|  | 796 | GENERATOR_ARGS+=( | 
|  | 797 | --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}" | 
|  | 798 | ) | 
|  | 799 | fi | 
| Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 800 | if [[ -n "{APEX_INFO_FILE}" ]]; then | 
|  | 801 | GENERATOR_ARGS+=( | 
|  | 802 | --apex_info_file="${APEX_INFO_FILE}" | 
|  | 803 | ) | 
|  | 804 | fi | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 805 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 806 | echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}" | 
| Kelvin Zhang | 907b0e6 | 2022-01-10 06:23:10 -0800 | [diff] [blame] | 807 | if [[ -n "${FLAGS_enable_lz4diff}" ]]; then | 
|  | 808 | LD_PRELOAD=${LD_PRELOAD}:${FLAGS_liblz4_path} "${GENERATOR}" "${GENERATOR_ARGS[@]}" | 
|  | 809 | else | 
|  | 810 | "${GENERATOR}" "${GENERATOR_ARGS[@]}" | 
|  | 811 | fi | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 812 |  | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 813 | echo "Done generating ${payload_type} update." | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 814 | } | 
|  | 815 |  | 
|  | 816 | validate_hash() { | 
|  | 817 | [[ -n "${FLAGS_signature_size}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 818 | die "You must specify signature size with --signature_size SIZES" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 819 |  | 
|  | 820 | [[ -n "${FLAGS_unsigned_payload}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 821 | die "You must specify the input unsigned payload with \ | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 822 | --unsigned_payload FILENAME" | 
|  | 823 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 824 | [[ -n "${FLAGS_payload_hash_file}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 825 | die "You must specify --payload_hash_file FILENAME" | 
| Jason Kusuma | f514c54 | 2015-11-05 18:43:45 -0800 | [diff] [blame] | 826 |  | 
|  | 827 | [[ -n "${FLAGS_metadata_hash_file}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 828 | die "You must specify --metadata_hash_file FILENAME" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 829 | } | 
|  | 830 |  | 
|  | 831 | cmd_hash() { | 
| Sen Jiang | bf1266f | 2015-10-26 11:29:24 -0700 | [diff] [blame] | 832 | "${GENERATOR}" \ | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 833 | --in_file="${FLAGS_unsigned_payload}" \ | 
|  | 834 | --signature_size="${FLAGS_signature_size}" \ | 
|  | 835 | --out_hash_file="${FLAGS_payload_hash_file}" \ | 
|  | 836 | --out_metadata_hash_file="${FLAGS_metadata_hash_file}" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 837 |  | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 838 | echo "Done generating hash." | 
|  | 839 | } | 
|  | 840 |  | 
|  | 841 | validate_sign() { | 
|  | 842 | [[ -n "${FLAGS_signature_size}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 843 | die "You must specify signature size with --signature_size SIZES" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 844 |  | 
|  | 845 | [[ -n "${FLAGS_unsigned_payload}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 846 | die "You must specify the input unsigned payload with \ | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 847 | --unsigned_payload FILENAME" | 
|  | 848 |  | 
|  | 849 | [[ -n "${FLAGS_payload}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 850 | die "You must specify the output signed payload with --payload FILENAME" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 851 |  | 
|  | 852 | [[ -n "${FLAGS_payload_signature_file}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 853 | die "You must specify the payload signature file with \ | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 854 | --payload_signature_file SIGNATURES" | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 855 |  | 
|  | 856 | [[ -n "${FLAGS_metadata_signature_file}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 857 | die "You must specify the metadata signature file with \ | 
| Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 858 | --metadata_signature_file SIGNATURES" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 859 | } | 
|  | 860 |  | 
|  | 861 | cmd_sign() { | 
| Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 862 | GENERATOR_ARGS=( | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 863 | --in_file="${FLAGS_unsigned_payload}" | 
|  | 864 | --signature_size="${FLAGS_signature_size}" | 
|  | 865 | --payload_signature_file="${FLAGS_payload_signature_file}" | 
|  | 866 | --metadata_signature_file="${FLAGS_metadata_signature_file}" | 
|  | 867 | --out_file="${FLAGS_payload}" | 
| Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 868 | ) | 
|  | 869 |  | 
|  | 870 | if [[ -n "${FLAGS_metadata_size_file}" ]]; then | 
|  | 871 | GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" ) | 
|  | 872 | fi | 
|  | 873 |  | 
|  | 874 | "${GENERATOR}" "${GENERATOR_ARGS[@]}" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 875 | echo "Done signing payload." | 
|  | 876 | } | 
|  | 877 |  | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 878 | validate_properties() { | 
|  | 879 | [[ -n "${FLAGS_payload}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 880 | die "You must specify the payload file with --payload FILENAME" | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 881 |  | 
|  | 882 | [[ -n "${FLAGS_properties_file}" ]] || | 
| Sen Jiang | 53f04d7 | 2016-07-13 16:43:39 -0700 | [diff] [blame] | 883 | die "You must specify a non empty --properties_file FILENAME" | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 884 | } | 
|  | 885 |  | 
|  | 886 | cmd_properties() { | 
|  | 887 | "${GENERATOR}" \ | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 888 | --in_file="${FLAGS_payload}" \ | 
|  | 889 | --properties_file="${FLAGS_properties_file}" | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 890 | } | 
|  | 891 |  | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 892 | validate_verify_and_check() { | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 893 | [[ -n "${FLAGS_payload}" ]] || | 
|  | 894 | die "Error: you must specify an input filename with --payload FILENAME" | 
|  | 895 |  | 
|  | 896 | [[ -n "${FLAGS_target_image}" ]] || | 
|  | 897 | die "Error: you must specify a target image with --target_image FILENAME" | 
|  | 898 | } | 
|  | 899 |  | 
|  | 900 | cmd_verify() { | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 901 | local payload_type=$(get_payload_type) | 
|  | 902 | extract_payload_images ${payload_type} | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 903 |  | 
|  | 904 | declare -A TMP_PARTITIONS | 
|  | 905 | for part in "${PARTITIONS_ORDER[@]}"; do | 
|  | 906 | local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX") | 
|  | 907 | echo "Creating temporary target partition ${tmp_part} for ${part}" | 
|  | 908 | CLEANUP_FILES+=("${tmp_part}") | 
|  | 909 | TMP_PARTITIONS[${part}]=${tmp_part} | 
|  | 910 | local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}") | 
|  | 911 | echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}" | 
|  | 912 | truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}" | 
|  | 913 | done | 
|  | 914 |  | 
|  | 915 | echo "Verifying ${payload_type} update." | 
|  | 916 | # Common payload args: | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 917 | GENERATOR_ARGS=( --in_file="${FLAGS_payload}" ) | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 918 |  | 
|  | 919 | local part old_partitions="" new_partitions="" partition_names="" | 
|  | 920 | for part in "${PARTITIONS_ORDER[@]}"; do | 
|  | 921 | if [[ -n "${partition_names}" ]]; then | 
|  | 922 | partition_names+=":" | 
|  | 923 | new_partitions+=":" | 
|  | 924 | old_partitions+=":" | 
|  | 925 | fi | 
|  | 926 | partition_names+="${part}" | 
|  | 927 | new_partitions+="${TMP_PARTITIONS[${part}]}" | 
|  | 928 | old_partitions+="${SRC_PARTITIONS[${part}]:-}" | 
|  | 929 | done | 
|  | 930 |  | 
|  | 931 | # Target image args: | 
|  | 932 | GENERATOR_ARGS+=( | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 933 | --partition_names="${partition_names}" | 
|  | 934 | --new_partitions="${new_partitions}" | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 935 | ) | 
|  | 936 |  | 
|  | 937 | if [[ "${payload_type}" == "delta" ]]; then | 
|  | 938 | # Source image args: | 
|  | 939 | GENERATOR_ARGS+=( | 
| Tudor Brindus | 5ec5bd1 | 2018-07-11 11:02:44 -0700 | [diff] [blame] | 940 | --old_partitions="${old_partitions}" | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 941 | ) | 
|  | 942 | fi | 
|  | 943 |  | 
| Amin Hassani | a566cb6 | 2017-08-23 12:36:55 -0700 | [diff] [blame] | 944 | if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then | 
|  | 945 | GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" ) | 
|  | 946 | fi | 
|  | 947 |  | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 948 | echo "Running delta_generator to verify ${payload_type} payload with args: \ | 
|  | 949 | ${GENERATOR_ARGS[@]}" | 
| Sen Jiang | 6feb15c | 2018-08-31 15:45:17 -0700 | [diff] [blame] | 950 | "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 951 |  | 
| Sen Jiang | 6feb15c | 2018-08-31 15:45:17 -0700 | [diff] [blame] | 952 | echo "Done applying ${payload_type} update." | 
|  | 953 | echo "Checking the newly generated partitions against the target partitions" | 
|  | 954 | local need_pause=false | 
|  | 955 | for part in "${PARTITIONS_ORDER[@]}"; do | 
|  | 956 | local not_str="" | 
|  | 957 | if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then | 
|  | 958 | not_str="in" | 
|  | 959 | need_pause=true | 
|  | 960 | fi | 
|  | 961 | echo "The new partition (${part}) is ${not_str}valid." | 
|  | 962 | done | 
|  | 963 | # All images will be cleaned up when script exits, pause here to give a chance | 
|  | 964 | # to inspect the images. | 
|  | 965 | if [[ "$need_pause" == true ]]; then | 
|  | 966 | read -n1 -r -s -p "Paused to investigate invalid partitions, \ | 
|  | 967 | press any key to exit." | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 968 | fi | 
|  | 969 | } | 
|  | 970 |  | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 971 | cmd_check() { | 
|  | 972 | local payload_type=$(get_payload_type) | 
|  | 973 | extract_payload_images ${payload_type} | 
|  | 974 |  | 
|  | 975 | local part dst_partitions="" src_partitions="" | 
|  | 976 | for part in "${PARTITIONS_ORDER[@]}"; do | 
|  | 977 | if [[ -n "${dst_partitions}" ]]; then | 
|  | 978 | dst_partitions+=" " | 
|  | 979 | src_partitions+=" " | 
|  | 980 | fi | 
|  | 981 | dst_partitions+="${DST_PARTITIONS[${part}]}" | 
|  | 982 | src_partitions+="${SRC_PARTITIONS[${part}]:-}" | 
|  | 983 | done | 
|  | 984 |  | 
|  | 985 | # Common payload args: | 
|  | 986 | PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \ | 
|  | 987 | --part_names ${PARTITIONS_ORDER[@]} \ | 
|  | 988 | --dst_part_paths ${dst_partitions} ) | 
|  | 989 |  | 
|  | 990 | if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then | 
|  | 991 | PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} ) | 
|  | 992 | fi | 
|  | 993 |  | 
|  | 994 | echo "Checking ${payload_type} update." | 
|  | 995 | check_update_payload ${PAYCHECK_ARGS[@]} --check | 
|  | 996 | } | 
|  | 997 |  | 
| Tianjie | e283ce4 | 2020-07-29 11:37:51 -0700 | [diff] [blame] | 998 | # Check that the real generator exists: | 
| Yifan Hong | 3756c3e | 2020-07-24 20:25:51 -0700 | [diff] [blame] | 999 | [[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)" | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 1000 | [[ -x "${GENERATOR}" ]] || die "can't find delta_generator" | 
|  | 1001 |  | 
|  | 1002 | case "$COMMAND" in | 
|  | 1003 | generate) validate_generate | 
|  | 1004 | cmd_generate | 
|  | 1005 | ;; | 
|  | 1006 | hash) validate_hash | 
|  | 1007 | cmd_hash | 
|  | 1008 | ;; | 
|  | 1009 | sign) validate_sign | 
|  | 1010 | cmd_sign | 
|  | 1011 | ;; | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 1012 | properties) validate_properties | 
|  | 1013 | cmd_properties | 
|  | 1014 | ;; | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 1015 | verify) validate_verify_and_check | 
| Amin Hassani | 1352093 | 2017-07-26 11:26:05 -0700 | [diff] [blame] | 1016 | cmd_verify | 
|  | 1017 | ;; | 
| Tudor Brindus | b432db8 | 2018-06-29 13:13:27 -0700 | [diff] [blame] | 1018 | check) validate_verify_and_check | 
|  | 1019 | cmd_check | 
|  | 1020 | ;; | 
| Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 1021 | esac |