blob: 4902c0cdc99ff165e675a43d275faf411aedf373 [file] [log] [blame]
Jason Kusumabe998f42015-09-03 15:53:13 -07001#!/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 Kusuma9a4cae22015-10-08 18:17:57 -070016# --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 Kusumabe998f42015-09-03 15:53:13 -070022#
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 Deymo89ff9e32015-09-15 19:29:01 -070026# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070027# --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 Deymo89ff9e32015-09-15 19:29:01 -070033# --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 Kusuma9a4cae22015-10-08 18:17:57 -070043# --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 Kusumabe998f42015-09-03 15:53:13 -070046# Note that the number of signature sizes and payload signatures have to match.
47
Alex Deymo61e1fa82016-01-19 15:16:34 -080048# Exit codes:
49EX_UNSUPPORTED_DELTA=100
50
Jason Kusumaf514c542015-11-05 18:43:45 -080051warn() {
52 echo "brillo_update_payload: warning: $*" >&2
53}
54
Gilad Arnold957ce122015-10-14 16:02:55 -070055die() {
56 echo "brillo_update_payload: error: $*" >&2
57 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070058}
59
Gilad Arnold957ce122015-10-14 16:02:55 -070060# Loads shflags. We first look at the default install location; then look for
61# crosutils (chroot); finally check our own directory (au-generator zipfile).
62load_shflags() {
63 local my_dir="$(dirname "$(readlink -f "$0")")"
64 local path
65 for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do
66 if [[ -r "${path}/shflags" ]]; then
67 . "${path}/shflags" || die "Could not load ${path}/shflags."
68 return
69 fi
70 done
71 die "Could not find shflags."
72}
73
74load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -070075
Alex Deymoc64ffd52015-09-25 18:10:07 -070076HELP_GENERATE="generate: Generate an unsigned update payload."
77HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
78for signing."
79HELP_SIGN="sign: Insert the signatures into the unsigned payload."
80
81usage() {
82 echo "Supported commands:"
83 echo
84 echo "${HELP_GENERATE}"
85 echo "${HELP_HASH}"
86 echo "${HELP_SIGN}"
87 echo
88 echo "Use: \"$0 <command> --help\" for more options."
89}
90
91# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -070092if [[ $# -lt 1 ]]; then
93 echo "Please specify a command [generate|hash|sign]"
94 exit 1
95fi
96
Alex Deymoc64ffd52015-09-25 18:10:07 -070097# Parse command.
98COMMAND="${1:-}"
99shift
100
101case "${COMMAND}" in
102 generate)
103 FLAGS_HELP="${HELP_GENERATE}"
104 ;;
105
106 hash)
107 FLAGS_HELP="${HELP_HASH}"
108 ;;
109
110 sign)
111 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700112 ;;
113 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700114 echo "Unrecognized command: \"${COMMAND}\"" >&2
115 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700116 exit 1
117 ;;
118esac
119
Jason Kusumabe998f42015-09-03 15:53:13 -0700120# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700121FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
122${FLAGS_HELP}"
123
124if [[ "${COMMAND}" == "generate" ]]; then
125 DEFINE_string payload "" \
126 "Path to output the generated unsigned payload file."
127 DEFINE_string target_image "" \
128 "Path to the target image that should be sent to clients."
129 DEFINE_string source_image "" \
130 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700131 DEFINE_string metadata_size_file "" \
132 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700133fi
134if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
135 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
136 DEFINE_string signature_size "" \
137 "Signature sizes in bytes in the following format: size1:size2[:...]"
138fi
139if [[ "${COMMAND}" == "hash" ]]; then
140 DEFINE_string metadata_hash_file "" \
141 "Optional: Path to output metadata hash file."
142 DEFINE_string payload_hash_file "" \
143 "Optional: Path to output payload hash file."
144fi
145if [[ "${COMMAND}" == "sign" ]]; then
146 DEFINE_string payload "" \
147 "Path to output the generated unsigned payload file."
148 DEFINE_string metadata_signature_file "" \
149 "The metatada signatures in the following format: \
150metadata_signature1:metadata_signature2[:...]"
151 DEFINE_string payload_signature_file "" \
152 "The payload signatures in the following format: \
153payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700154 DEFINE_string metadata_size_file "" \
155 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700156fi
Jason Kusumabe998f42015-09-03 15:53:13 -0700157DEFINE_string work_dir "/tmp" "Where to dump temporary files."
158
159# Parse command line flag arguments
160FLAGS "$@" || exit 1
161eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700162set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700163
Alex Deymo89ff9e32015-09-15 19:29:01 -0700164# Associative arrays from partition name to file in the source and target
165# images. The size of the updated area must be the size of the file.
166declare -A SRC_PARTITIONS
167declare -A DST_PARTITIONS
168
169# A list of temporary files to remove during cleanup.
170CLEANUP_FILES=()
171
Alex Deymo48b502a2015-09-17 19:00:18 -0700172# Global options to force the version of the payload.
173FORCE_MAJOR_VERSION=""
174FORCE_MINOR_VERSION=""
175
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800176# Path to the postinstall config file in target image if exists.
177POSTINSTALL_CONFIG_FILE=""
178
Alex Deymoc97df432015-09-25 17:23:52 -0700179# read_option_int <file.txt> <option_key> [default_value]
180#
181# Reads the unsigned integer value associated with |option_key| in a key=value
182# file |file.txt|. Prints the read value if found and valid, otherwise prints
183# the |default_value|.
184read_option_uint() {
185 local file_txt="$1"
186 local option_key="$2"
187 local default_value="${3:-}"
188 local value
189 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
190 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
191 echo "${value}"
192 return
193 fi
194 fi
195 echo "${default_value}"
196}
197
Alex Deymo89ff9e32015-09-15 19:29:01 -0700198# Create a temporary file in the work_dir with an optional pattern name.
199# Prints the name of the newly created file.
200create_tempfile() {
201 local pattern="${1:-tempfile.XXXXXX}"
202 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
203}
Jason Kusumabe998f42015-09-03 15:53:13 -0700204
205cleanup() {
206 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700207 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700208
209 # If we are cleaning up after an error, or if we got an error during
210 # cleanup (even if we eventually succeeded) return a non-zero exit
211 # code. This triggers additional logging in most environments that call
212 # this script.
213 if [[ -n "${err}" ]]; then
214 die "Cleanup encountered an error."
215 fi
216}
217
218cleanup_on_error() {
219 trap - INT TERM ERR EXIT
220 cleanup
221 die "Cleanup success after an error."
222}
223
224cleanup_on_exit() {
225 trap - INT TERM ERR EXIT
226 cleanup
227}
228
229trap cleanup_on_error INT TERM ERR
230trap cleanup_on_exit EXIT
231
Alex Deymo48b502a2015-09-17 19:00:18 -0700232
233# extract_image <image> <partitions_array>
234#
235# Detect the format of the |image| file and extract its updatable partitions
236# into new temporary files. Add the list of partition names and its files to the
237# associative array passed in |partitions_array|.
238extract_image() {
239 local image="$1"
240
241 # Brillo images are zip files. We detect the 4-byte magic header of the zip
242 # file.
243 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
244 if [[ "${magic}" == "504b0304" ]]; then
245 echo "Detected .zip file, extracting Brillo image."
246 extract_image_brillo "$@"
247 return
248 fi
249
250 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
251 # bundled here and we will use it to extract the partitions, so the GPT
252 # headers must be valid.
253 if cgpt show -q -n "${image}" >/dev/null; then
254 echo "Detected GPT image, extracting Chrome OS image."
255 extract_image_cros "$@"
256 return
257 fi
258
259 die "Couldn't detect the image format of ${image}"
260}
261
Alex Deymo89ff9e32015-09-15 19:29:01 -0700262# extract_image_cros <image.bin> <partitions_array>
263#
Alex Deymo48b502a2015-09-17 19:00:18 -0700264# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700265extract_image_cros() {
266 local image="$1"
267 local partitions_array="$2"
268
269 local kernel root
270 kernel=$(create_tempfile "kernel.bin.XXXXXX")
271 CLEANUP_FILES+=("${kernel}")
272 root=$(create_tempfile "root.bin.XXXXXX")
273 CLEANUP_FILES+=("${root}")
274
275 cros_generate_update_payload --extract \
276 --image "${image}" \
277 --kern_path "${kernel}" --root_path "${root}" \
278 --work_dir "${FLAGS_work_dir}" --outside_chroot
279
Alex Deymo83f2f702015-10-14 14:49:33 -0700280 # Chrome OS uses major_version 1 payloads for all versions, even if the
281 # updater supports a newer major version.
282 FORCE_MAJOR_VERSION="1"
283
Alex Deymo48b502a2015-09-17 19:00:18 -0700284 # When generating legacy Chrome OS images, we need to use "boot" and "system"
285 # for the partition names to be compatible with updating Brillo devices with
286 # Chrome OS images.
287 eval ${partitions_array}[boot]=\""${kernel}"\"
288 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700289
290 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700291 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700292 varname="${partitions_array}[${part}]"
293 printf "md5sum of %s: " "${varname}"
294 md5sum "${!varname}"
295 done
296}
297
Alex Deymo48b502a2015-09-17 19:00:18 -0700298# extract_image_brillo <target_files.zip> <partitions_array>
299#
300# Extract the A/B updated partitions from a Brillo target_files zip file into
301# new temporary files.
302extract_image_brillo() {
303 local image="$1"
304 local partitions_array="$2"
305
Alex Deymo48b502a2015-09-17 19:00:18 -0700306 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800307 local ab_partitions_list
308 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
309 CLEANUP_FILES+=("${ab_partitions_list}")
310 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
311 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
312 die "Invalid partition names found in the partition list."
313 fi
314 partitions=($(cat "${ab_partitions_list}"))
315 if [[ ${#partitions[@]} -eq 0 ]]; then
316 die "The list of partitions is empty. Can't generate a payload."
317 fi
318 else
319 warn "No ab_partitions.txt found. Using default."
320 fi
321 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700322
Alex Deymo83f2f702015-10-14 14:49:33 -0700323 # All Brillo updaters support major version 2.
324 FORCE_MAJOR_VERSION="2"
325
Alex Deymo48b502a2015-09-17 19:00:18 -0700326 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800327 # Source image
328 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700329 CLEANUP_FILES+=("${ue_config}")
330 if ! unzip -p "${image}" "META/update_engine_config.txt" \
331 >"${ue_config}"; then
332 warn "No update_engine_config.txt found. Assuming pre-release image, \
333using payload minor version 2"
334 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700335 # For delta payloads, we use the major and minor version supported by the
336 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700337 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
338 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700339 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
340 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800341
342 # Brillo support for deltas started with minor version 3.
343 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
344 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
345Disabling deltas for this source version."
346 exit ${EX_UNSUPPORTED_DELTA}
347 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800348 else
349 # Target image
350 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
351 CLEANUP_FILES+=("${postinstall_config}")
352 if unzip -p "${image}" "META/postinstall_config.txt" \
353 >"${postinstall_config}"; then
354 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
355 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700356 fi
357
358 local part part_file temp_raw filesize
359 for part in "${partitions[@]}"; do
360 part_file=$(create_tempfile "${part}.img.XXXXXX")
361 CLEANUP_FILES+=("${part_file}")
362 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
363
364 # If the partition is stored as an Android sparse image file, we need to
365 # convert them to a raw image for the update.
366 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
367 if [[ "${magic}" == "3aff26ed" ]]; then
368 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
369 CLEANUP_FILES+=("${temp_raw}")
370 echo "Converting Android sparse image ${part}.img to RAW."
371 simg2img "${part_file}" "${temp_raw}"
372 # At this point, we can drop the contents of the old part_file file, but
373 # we can't delete the file because it will be deleted in cleanup.
374 true >"${part_file}"
375 part_file="${temp_raw}"
376 fi
377
378 # delta_generator only supports images multiple of 4 KiB, so we pad with
379 # zeros if needed.
380 filesize=$(stat -c%s "${part_file}")
381 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
382 echo "Rounding up partition ${part}.img to multiple of 4 KiB."
383 : $(( filesize = (filesize + 4095) & -4096 ))
384 truncate --size="${filesize}" "${part_file}"
385 fi
386
387 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
388 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
389 done
390}
391
Jason Kusumabe998f42015-09-03 15:53:13 -0700392validate_generate() {
393 [[ -n "${FLAGS_payload}" ]] ||
394 die "Error: you must specify an output filename with --payload FILENAME"
395
396 [[ -n "${FLAGS_target_image}" ]] ||
397 die "Error: you must specify a target image with --target_image FILENAME"
398}
399
400cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700401 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700402 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700403 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700404 fi
405
Alex Deymo48b502a2015-09-17 19:00:18 -0700406 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700407
Alex Deymo48b502a2015-09-17 19:00:18 -0700408 extract_image "${FLAGS_target_image}" DST_PARTITIONS
Alex Deymo89ff9e32015-09-15 19:29:01 -0700409 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700410 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700411 fi
412
Alex Deymo48b502a2015-09-17 19:00:18 -0700413 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800414 # Common payload args:
415 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
416
417 local part old_partitions="" new_partitions="" partition_names=""
418 for part in "${!DST_PARTITIONS[@]}"; do
419 if [[ -n "${partition_names}" ]]; then
420 partition_names+=":"
421 new_partitions+=":"
422 old_partitions+=":"
423 fi
424 partition_names+="${part}"
425 new_partitions+="${DST_PARTITIONS[${part}]}"
426 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
427 done
428
429 # Target image args:
430 GENERATOR_ARGS+=(
431 -partition_names="${partition_names}"
432 -new_partitions="${new_partitions}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700433 )
434
Alex Deymo89ff9e32015-09-15 19:29:01 -0700435 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800436 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700437 GENERATOR_ARGS+=(
Alex Deymo168b5352015-11-04 13:51:52 -0800438 -old_partitions="${old_partitions}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700439 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700440 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
441 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
442 fi
443 fi
444
445 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
446 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700447 fi
448
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700449 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
450 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
451 fi
452
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800453 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
454 GENERATOR_ARGS+=(
455 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
456 )
457 fi
458
Jason Kusumabe998f42015-09-03 15:53:13 -0700459 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700460 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700461
Alex Deymo89ff9e32015-09-15 19:29:01 -0700462 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700463}
464
465validate_hash() {
466 [[ -n "${FLAGS_signature_size}" ]] ||
467 die "Error: you must specify signature size with --signature_size SIZES"
468
469 [[ -n "${FLAGS_unsigned_payload}" ]] ||
470 die "Error: you must specify the input unsigned payload with \
471--unsigned_payload FILENAME"
472
Jason Kusumabe998f42015-09-03 15:53:13 -0700473 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiangbf1266f2015-10-26 11:29:24 -0700474 die "Error: you must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800475
476 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
477 die "Error: you must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700478}
479
480cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700481 "${GENERATOR}" \
482 -in_file="${FLAGS_unsigned_payload}" \
483 -signature_size="${FLAGS_signature_size}" \
484 -out_hash_file="${FLAGS_payload_hash_file}" \
485 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700486
Jason Kusumabe998f42015-09-03 15:53:13 -0700487 echo "Done generating hash."
488}
489
490validate_sign() {
491 [[ -n "${FLAGS_signature_size}" ]] ||
492 die "Error: you must specify signature size with --signature_size SIZES"
493
494 [[ -n "${FLAGS_unsigned_payload}" ]] ||
495 die "Error: you must specify the input unsigned payload with \
496--unsigned_payload FILENAME"
497
498 [[ -n "${FLAGS_payload}" ]] ||
499 die "Error: you must specify the output signed payload with \
500--payload FILENAME"
501
502 [[ -n "${FLAGS_payload_signature_file}" ]] ||
503 die "Error: you must specify the payload signature file with \
504--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700505
506 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
507 die "Error: you must specify the metadata signature file with \
508--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700509}
510
511cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700512 GENERATOR_ARGS=(
513 -in_file="${FLAGS_unsigned_payload}"
514 -signature_size="${FLAGS_signature_size}"
515 -signature_file="${FLAGS_payload_signature_file}"
516 -metadata_signature_file="${FLAGS_metadata_signature_file}"
517 -out_file="${FLAGS_payload}"
518 )
519
520 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
521 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
522 fi
523
524 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700525 echo "Done signing payload."
526}
527
Jason Kusumabe998f42015-09-03 15:53:13 -0700528# Sanity check that the real generator exists:
529GENERATOR="$(which delta_generator)"
530[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
531
532case "$COMMAND" in
533 generate) validate_generate
534 cmd_generate
535 ;;
536 hash) validate_hash
537 cmd_hash
538 ;;
539 sign) validate_sign
540 cmd_sign
541 ;;
542esac