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