Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # Script to generate a Brillo update for use by the update engine. |
| 8 | # |
| 9 | # usage: brillo_update_payload COMMAND [ARGS] |
| 10 | # The following commands are supported: |
| 11 | # generate generate an unsigned payload |
| 12 | # hash generate a payload or metadata hash |
| 13 | # sign generate a signed payload |
| 14 | # |
| 15 | # Generate command arguments: |
Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 16 | # --payload generated unsigned payload output file |
| 17 | # --source_image if defined, generate a delta payload from the specified |
| 18 | # image to the target_image |
| 19 | # --target_image the target image that should be sent to clients |
| 20 | # --metadata_size_file if defined, generate a file containing the size of the payload |
| 21 | # metadata in bytes to the specified file |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 22 | # |
| 23 | # Hash command arguments: |
| 24 | # --unsigned_payload the input unsigned payload to generate the hash from |
| 25 | # --signature_size signature sizes in bytes in the following format: |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 26 | # "size1:size2[:...]" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 27 | # --payload_hash_file if defined, generate a payload hash and output to the |
| 28 | # specified file |
| 29 | # --metadata_hash_file if defined, generate a metadata hash and output to the |
| 30 | # specified file |
| 31 | # |
| 32 | # Sign command arguments: |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 33 | # --unsigned_payload the input unsigned payload to insert the signatures |
| 34 | # --payload the output signed payload |
| 35 | # --signature_size signature sizes in bytes in the following format: |
| 36 | # "size1:size2[:...]" |
| 37 | # --payload_signature_file the payload signature files in the following |
| 38 | # format: |
| 39 | # "payload_signature1:payload_signature2[:...]" |
| 40 | # --metadata_signature_file the metadata signature files in the following |
| 41 | # format: |
| 42 | # "metadata_signature1:metadata_signature2[:...]" |
Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 43 | # --metadata_size_file if defined, generate a file containing the size of |
| 44 | # the signed payload metadata in bytes to the |
| 45 | # specified file |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 46 | # Note that the number of signature sizes and payload signatures have to match. |
| 47 | |
Gilad Arnold | 957ce12 | 2015-10-14 16:02:55 -0700 | [diff] [blame] | 48 | die() { |
| 49 | echo "brillo_update_payload: error: $*" >&2 |
| 50 | exit 1 |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Gilad Arnold | 957ce12 | 2015-10-14 16:02:55 -0700 | [diff] [blame] | 53 | # Loads shflags. We first look at the default install location; then look for |
| 54 | # crosutils (chroot); finally check our own directory (au-generator zipfile). |
| 55 | load_shflags() { |
| 56 | local my_dir="$(dirname "$(readlink -f "$0")")" |
| 57 | local path |
| 58 | for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do |
| 59 | if [[ -r "${path}/shflags" ]]; then |
| 60 | . "${path}/shflags" || die "Could not load ${path}/shflags." |
| 61 | return |
| 62 | fi |
| 63 | done |
| 64 | die "Could not find shflags." |
| 65 | } |
| 66 | |
| 67 | load_shflags |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 68 | |
Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 69 | HELP_GENERATE="generate: Generate an unsigned update payload." |
| 70 | HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \ |
| 71 | for signing." |
| 72 | HELP_SIGN="sign: Insert the signatures into the unsigned payload." |
| 73 | |
| 74 | usage() { |
| 75 | echo "Supported commands:" |
| 76 | echo |
| 77 | echo "${HELP_GENERATE}" |
| 78 | echo "${HELP_HASH}" |
| 79 | echo "${HELP_SIGN}" |
| 80 | echo |
| 81 | echo "Use: \"$0 <command> --help\" for more options." |
| 82 | } |
| 83 | |
| 84 | # Check that a command is specified. |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 85 | if [[ $# -lt 1 ]]; then |
| 86 | echo "Please specify a command [generate|hash|sign]" |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 90 | # Parse command. |
| 91 | COMMAND="${1:-}" |
| 92 | shift |
| 93 | |
| 94 | case "${COMMAND}" in |
| 95 | generate) |
| 96 | FLAGS_HELP="${HELP_GENERATE}" |
| 97 | ;; |
| 98 | |
| 99 | hash) |
| 100 | FLAGS_HELP="${HELP_HASH}" |
| 101 | ;; |
| 102 | |
| 103 | sign) |
| 104 | FLAGS_HELP="${HELP_SIGN}" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 105 | ;; |
| 106 | *) |
Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 107 | echo "Unrecognized command: \"${COMMAND}\"" >&2 |
| 108 | usage >&2 |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 109 | exit 1 |
| 110 | ;; |
| 111 | esac |
| 112 | |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 113 | # Flags |
Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 114 | FLAGS_HELP="Usage: $0 ${COMMAND} [flags] |
| 115 | ${FLAGS_HELP}" |
| 116 | |
| 117 | if [[ "${COMMAND}" == "generate" ]]; then |
| 118 | DEFINE_string payload "" \ |
| 119 | "Path to output the generated unsigned payload file." |
| 120 | DEFINE_string target_image "" \ |
| 121 | "Path to the target image that should be sent to clients." |
| 122 | DEFINE_string source_image "" \ |
| 123 | "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] | 124 | DEFINE_string metadata_size_file "" \ |
| 125 | "Optional: Path to output metadata size." |
Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 126 | fi |
| 127 | if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then |
| 128 | DEFINE_string unsigned_payload "" "Path to the input unsigned payload." |
| 129 | DEFINE_string signature_size "" \ |
| 130 | "Signature sizes in bytes in the following format: size1:size2[:...]" |
| 131 | fi |
| 132 | if [[ "${COMMAND}" == "hash" ]]; then |
| 133 | DEFINE_string metadata_hash_file "" \ |
| 134 | "Optional: Path to output metadata hash file." |
| 135 | DEFINE_string payload_hash_file "" \ |
| 136 | "Optional: Path to output payload hash file." |
| 137 | fi |
| 138 | if [[ "${COMMAND}" == "sign" ]]; then |
| 139 | DEFINE_string payload "" \ |
| 140 | "Path to output the generated unsigned payload file." |
| 141 | DEFINE_string metadata_signature_file "" \ |
| 142 | "The metatada signatures in the following format: \ |
| 143 | metadata_signature1:metadata_signature2[:...]" |
| 144 | DEFINE_string payload_signature_file "" \ |
| 145 | "The payload signatures in the following format: \ |
| 146 | payload_signature1:payload_signature2[:...]" |
Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 147 | DEFINE_string metadata_size_file "" \ |
| 148 | "Optional: Path to output metadata size." |
Alex Deymo | c64ffd5 | 2015-09-25 18:10:07 -0700 | [diff] [blame] | 149 | fi |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 150 | DEFINE_string work_dir "/tmp" "Where to dump temporary files." |
| 151 | |
| 152 | # Parse command line flag arguments |
| 153 | FLAGS "$@" || exit 1 |
| 154 | eval set -- "${FLAGS_ARGV}" |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 155 | set -e |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 156 | |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 157 | # Associative arrays from partition name to file in the source and target |
| 158 | # images. The size of the updated area must be the size of the file. |
| 159 | declare -A SRC_PARTITIONS |
| 160 | declare -A DST_PARTITIONS |
| 161 | |
| 162 | # A list of temporary files to remove during cleanup. |
| 163 | CLEANUP_FILES=() |
| 164 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 165 | # Global options to force the version of the payload. |
| 166 | FORCE_MAJOR_VERSION="" |
| 167 | FORCE_MINOR_VERSION="" |
| 168 | |
Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 169 | # read_option_int <file.txt> <option_key> [default_value] |
| 170 | # |
| 171 | # Reads the unsigned integer value associated with |option_key| in a key=value |
| 172 | # file |file.txt|. Prints the read value if found and valid, otherwise prints |
| 173 | # the |default_value|. |
| 174 | read_option_uint() { |
| 175 | local file_txt="$1" |
| 176 | local option_key="$2" |
| 177 | local default_value="${3:-}" |
| 178 | local value |
| 179 | if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then |
| 180 | if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then |
| 181 | echo "${value}" |
| 182 | return |
| 183 | fi |
| 184 | fi |
| 185 | echo "${default_value}" |
| 186 | } |
| 187 | |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 188 | # Create a temporary file in the work_dir with an optional pattern name. |
| 189 | # Prints the name of the newly created file. |
| 190 | create_tempfile() { |
| 191 | local pattern="${1:-tempfile.XXXXXX}" |
| 192 | mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}" |
| 193 | } |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 194 | |
| 195 | cleanup() { |
| 196 | local err="" |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 197 | rm -f "${CLEANUP_FILES[@]}" || err=1 |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 198 | |
| 199 | # If we are cleaning up after an error, or if we got an error during |
| 200 | # cleanup (even if we eventually succeeded) return a non-zero exit |
| 201 | # code. This triggers additional logging in most environments that call |
| 202 | # this script. |
| 203 | if [[ -n "${err}" ]]; then |
| 204 | die "Cleanup encountered an error." |
| 205 | fi |
| 206 | } |
| 207 | |
| 208 | cleanup_on_error() { |
| 209 | trap - INT TERM ERR EXIT |
| 210 | cleanup |
| 211 | die "Cleanup success after an error." |
| 212 | } |
| 213 | |
| 214 | cleanup_on_exit() { |
| 215 | trap - INT TERM ERR EXIT |
| 216 | cleanup |
| 217 | } |
| 218 | |
| 219 | trap cleanup_on_error INT TERM ERR |
| 220 | trap cleanup_on_exit EXIT |
| 221 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 222 | |
| 223 | # extract_image <image> <partitions_array> |
| 224 | # |
| 225 | # Detect the format of the |image| file and extract its updatable partitions |
| 226 | # into new temporary files. Add the list of partition names and its files to the |
| 227 | # associative array passed in |partitions_array|. |
| 228 | extract_image() { |
| 229 | local image="$1" |
| 230 | |
| 231 | # Brillo images are zip files. We detect the 4-byte magic header of the zip |
| 232 | # file. |
| 233 | local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"') |
| 234 | if [[ "${magic}" == "504b0304" ]]; then |
| 235 | echo "Detected .zip file, extracting Brillo image." |
| 236 | extract_image_brillo "$@" |
| 237 | return |
| 238 | fi |
| 239 | |
| 240 | # Chrome OS images are GPT partitioned disks. We should have the cgpt binary |
| 241 | # bundled here and we will use it to extract the partitions, so the GPT |
| 242 | # headers must be valid. |
| 243 | if cgpt show -q -n "${image}" >/dev/null; then |
| 244 | echo "Detected GPT image, extracting Chrome OS image." |
| 245 | extract_image_cros "$@" |
| 246 | return |
| 247 | fi |
| 248 | |
| 249 | die "Couldn't detect the image format of ${image}" |
| 250 | } |
| 251 | |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 252 | # extract_image_cros <image.bin> <partitions_array> |
| 253 | # |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 254 | # Extract Chromium OS recovery images into new temporary files. |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 255 | extract_image_cros() { |
| 256 | local image="$1" |
| 257 | local partitions_array="$2" |
| 258 | |
| 259 | local kernel root |
| 260 | kernel=$(create_tempfile "kernel.bin.XXXXXX") |
| 261 | CLEANUP_FILES+=("${kernel}") |
| 262 | root=$(create_tempfile "root.bin.XXXXXX") |
| 263 | CLEANUP_FILES+=("${root}") |
| 264 | |
| 265 | cros_generate_update_payload --extract \ |
| 266 | --image "${image}" \ |
| 267 | --kern_path "${kernel}" --root_path "${root}" \ |
| 268 | --work_dir "${FLAGS_work_dir}" --outside_chroot |
| 269 | |
Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 270 | # Chrome OS uses major_version 1 payloads for all versions, even if the |
| 271 | # updater supports a newer major version. |
| 272 | FORCE_MAJOR_VERSION="1" |
| 273 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 274 | # When generating legacy Chrome OS images, we need to use "boot" and "system" |
| 275 | # for the partition names to be compatible with updating Brillo devices with |
| 276 | # Chrome OS images. |
| 277 | eval ${partitions_array}[boot]=\""${kernel}"\" |
| 278 | eval ${partitions_array}[system]=\""${root}"\" |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 279 | |
| 280 | local part varname |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 281 | for part in boot system; do |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 282 | varname="${partitions_array}[${part}]" |
| 283 | printf "md5sum of %s: " "${varname}" |
| 284 | md5sum "${!varname}" |
| 285 | done |
| 286 | } |
| 287 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 288 | # extract_image_brillo <target_files.zip> <partitions_array> |
| 289 | # |
| 290 | # Extract the A/B updated partitions from a Brillo target_files zip file into |
| 291 | # new temporary files. |
| 292 | extract_image_brillo() { |
| 293 | local image="$1" |
| 294 | local partitions_array="$2" |
| 295 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 296 | local partitions=( "boot" "system" ) |
Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame^] | 297 | local ab_partitions_list |
| 298 | ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX") |
| 299 | CLEANUP_FILES+=("${ab_partitions_list}") |
| 300 | if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then |
| 301 | if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then |
| 302 | die "Invalid partition names found in the partition list." |
| 303 | fi |
| 304 | partitions=($(cat "${ab_partitions_list}")) |
| 305 | if [[ ${#partitions[@]} -eq 0 ]]; then |
| 306 | die "The list of partitions is empty. Can't generate a payload." |
| 307 | fi |
| 308 | else |
| 309 | warn "No ab_partitions.txt found. Using default." |
| 310 | fi |
| 311 | echo "List of A/B partitions: ${partitions[@]}" |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 312 | |
Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 313 | # All Brillo updaters support major version 2. |
| 314 | FORCE_MAJOR_VERSION="2" |
| 315 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 316 | if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then |
Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 317 | ue_config=$(create_tempfile "ue_config.XXXXXX") |
| 318 | CLEANUP_FILES+=("${ue_config}") |
| 319 | if ! unzip -p "${image}" "META/update_engine_config.txt" \ |
| 320 | >"${ue_config}"; then |
| 321 | warn "No update_engine_config.txt found. Assuming pre-release image, \ |
| 322 | using payload minor version 2" |
| 323 | fi |
Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 324 | # For delta payloads, we use the major and minor version supported by the |
| 325 | # old updater. |
Alex Deymo | c97df43 | 2015-09-25 17:23:52 -0700 | [diff] [blame] | 326 | FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \ |
| 327 | "PAYLOAD_MINOR_VERSION" 2) |
Alex Deymo | 83f2f70 | 2015-10-14 14:49:33 -0700 | [diff] [blame] | 328 | FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \ |
| 329 | "PAYLOAD_MAJOR_VERSION" 2) |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 330 | fi |
| 331 | |
| 332 | local part part_file temp_raw filesize |
| 333 | for part in "${partitions[@]}"; do |
| 334 | part_file=$(create_tempfile "${part}.img.XXXXXX") |
| 335 | CLEANUP_FILES+=("${part_file}") |
| 336 | unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}" |
| 337 | |
| 338 | # If the partition is stored as an Android sparse image file, we need to |
| 339 | # convert them to a raw image for the update. |
| 340 | local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"') |
| 341 | if [[ "${magic}" == "3aff26ed" ]]; then |
| 342 | temp_raw=$(create_tempfile "${part}.raw.XXXXXX") |
| 343 | CLEANUP_FILES+=("${temp_raw}") |
| 344 | echo "Converting Android sparse image ${part}.img to RAW." |
| 345 | simg2img "${part_file}" "${temp_raw}" |
| 346 | # At this point, we can drop the contents of the old part_file file, but |
| 347 | # we can't delete the file because it will be deleted in cleanup. |
| 348 | true >"${part_file}" |
| 349 | part_file="${temp_raw}" |
| 350 | fi |
| 351 | |
| 352 | # delta_generator only supports images multiple of 4 KiB, so we pad with |
| 353 | # zeros if needed. |
| 354 | filesize=$(stat -c%s "${part_file}") |
| 355 | if [[ $(( filesize % 4096 )) -ne 0 ]]; then |
| 356 | echo "Rounding up partition ${part}.img to multiple of 4 KiB." |
| 357 | : $(( filesize = (filesize + 4095) & -4096 )) |
| 358 | truncate --size="${filesize}" "${part_file}" |
| 359 | fi |
| 360 | |
| 361 | eval "${partitions_array}[\"${part}\"]=\"${part_file}\"" |
| 362 | echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes" |
| 363 | done |
| 364 | } |
| 365 | |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 366 | validate_generate() { |
| 367 | [[ -n "${FLAGS_payload}" ]] || |
| 368 | die "Error: you must specify an output filename with --payload FILENAME" |
| 369 | |
| 370 | [[ -n "${FLAGS_target_image}" ]] || |
| 371 | die "Error: you must specify a target image with --target_image FILENAME" |
| 372 | } |
| 373 | |
| 374 | cmd_generate() { |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 375 | local payload_type="delta" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 376 | if [[ -z "${FLAGS_source_image}" ]]; then |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 377 | payload_type="full" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 378 | fi |
| 379 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 380 | echo "Extracting images for ${payload_type} update." |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 381 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 382 | extract_image "${FLAGS_target_image}" DST_PARTITIONS |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 383 | if [[ "${payload_type}" == "delta" ]]; then |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 384 | extract_image "${FLAGS_source_image}" SRC_PARTITIONS |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 385 | fi |
| 386 | |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 387 | echo "Generating ${payload_type} update." |
Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame^] | 388 | # Common payload args: |
| 389 | GENERATOR_ARGS=( -out_file="${FLAGS_payload}" ) |
| 390 | |
| 391 | local part old_partitions="" new_partitions="" partition_names="" |
| 392 | for part in "${!DST_PARTITIONS[@]}"; do |
| 393 | if [[ -n "${partition_names}" ]]; then |
| 394 | partition_names+=":" |
| 395 | new_partitions+=":" |
| 396 | old_partitions+=":" |
| 397 | fi |
| 398 | partition_names+="${part}" |
| 399 | new_partitions+="${DST_PARTITIONS[${part}]}" |
| 400 | old_partitions+="${SRC_PARTITIONS[${part}]:-}" |
| 401 | done |
| 402 | |
| 403 | # Target image args: |
| 404 | GENERATOR_ARGS+=( |
| 405 | -partition_names="${partition_names}" |
| 406 | -new_partitions="${new_partitions}" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 407 | ) |
| 408 | |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 409 | if [[ "${payload_type}" == "delta" ]]; then |
Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame^] | 410 | # Source image args: |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 411 | GENERATOR_ARGS+=( |
Alex Deymo | 168b535 | 2015-11-04 13:51:52 -0800 | [diff] [blame^] | 412 | -old_partitions="${old_partitions}" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 413 | ) |
Alex Deymo | 48b502a | 2015-09-17 19:00:18 -0700 | [diff] [blame] | 414 | if [[ -n "${FORCE_MINOR_VERSION}" ]]; then |
| 415 | GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" ) |
| 416 | fi |
| 417 | fi |
| 418 | |
| 419 | if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then |
| 420 | GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" ) |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 421 | fi |
| 422 | |
Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 423 | if [[ -n "${FLAGS_metadata_size_file}" ]]; then |
| 424 | GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" ) |
| 425 | fi |
| 426 | |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 427 | echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}" |
Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 428 | "${GENERATOR}" "${GENERATOR_ARGS[@]}" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 429 | |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 430 | echo "Done generating ${payload_type} update." |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | validate_hash() { |
| 434 | [[ -n "${FLAGS_signature_size}" ]] || |
| 435 | die "Error: you must specify signature size with --signature_size SIZES" |
| 436 | |
| 437 | [[ -n "${FLAGS_unsigned_payload}" ]] || |
| 438 | die "Error: you must specify the input unsigned payload with \ |
| 439 | --unsigned_payload FILENAME" |
| 440 | |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 441 | [[ -n "${FLAGS_payload_hash_file}" ]] || |
Sen Jiang | bf1266f | 2015-10-26 11:29:24 -0700 | [diff] [blame] | 442 | die "Error: you must specify --payload_hash_file FILENAME" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | cmd_hash() { |
Sen Jiang | bf1266f | 2015-10-26 11:29:24 -0700 | [diff] [blame] | 446 | "${GENERATOR}" \ |
| 447 | -in_file="${FLAGS_unsigned_payload}" \ |
| 448 | -signature_size="${FLAGS_signature_size}" \ |
| 449 | -out_hash_file="${FLAGS_payload_hash_file}" \ |
| 450 | -out_metadata_hash_file="${FLAGS_metadata_hash_file}" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 451 | |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 452 | echo "Done generating hash." |
| 453 | } |
| 454 | |
| 455 | validate_sign() { |
| 456 | [[ -n "${FLAGS_signature_size}" ]] || |
| 457 | die "Error: you must specify signature size with --signature_size SIZES" |
| 458 | |
| 459 | [[ -n "${FLAGS_unsigned_payload}" ]] || |
| 460 | die "Error: you must specify the input unsigned payload with \ |
| 461 | --unsigned_payload FILENAME" |
| 462 | |
| 463 | [[ -n "${FLAGS_payload}" ]] || |
| 464 | die "Error: you must specify the output signed payload with \ |
| 465 | --payload FILENAME" |
| 466 | |
| 467 | [[ -n "${FLAGS_payload_signature_file}" ]] || |
| 468 | die "Error: you must specify the payload signature file with \ |
| 469 | --payload_signature_file SIGNATURES" |
Alex Deymo | 89ff9e3 | 2015-09-15 19:29:01 -0700 | [diff] [blame] | 470 | |
| 471 | [[ -n "${FLAGS_metadata_signature_file}" ]] || |
| 472 | die "Error: you must specify the metadata signature file with \ |
| 473 | --metadata_signature_file SIGNATURES" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | cmd_sign() { |
Jason Kusuma | 9a4cae2 | 2015-10-08 18:17:57 -0700 | [diff] [blame] | 477 | GENERATOR_ARGS=( |
| 478 | -in_file="${FLAGS_unsigned_payload}" |
| 479 | -signature_size="${FLAGS_signature_size}" |
| 480 | -signature_file="${FLAGS_payload_signature_file}" |
| 481 | -metadata_signature_file="${FLAGS_metadata_signature_file}" |
| 482 | -out_file="${FLAGS_payload}" |
| 483 | ) |
| 484 | |
| 485 | if [[ -n "${FLAGS_metadata_size_file}" ]]; then |
| 486 | GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" ) |
| 487 | fi |
| 488 | |
| 489 | "${GENERATOR}" "${GENERATOR_ARGS[@]}" |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 490 | echo "Done signing payload." |
| 491 | } |
| 492 | |
Jason Kusuma | be998f4 | 2015-09-03 15:53:13 -0700 | [diff] [blame] | 493 | # Sanity check that the real generator exists: |
| 494 | GENERATOR="$(which delta_generator)" |
| 495 | [[ -x "${GENERATOR}" ]] || die "can't find delta_generator" |
| 496 | |
| 497 | case "$COMMAND" in |
| 498 | generate) validate_generate |
| 499 | cmd_generate |
| 500 | ;; |
| 501 | hash) validate_hash |
| 502 | cmd_hash |
| 503 | ;; |
| 504 | sign) validate_sign |
| 505 | cmd_sign |
| 506 | ;; |
| 507 | esac |