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