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