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