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