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