blob: 22f07dcba0158baa21c66449acd08bcd674a92b4 [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
Jason Kusumaf514c542015-11-05 18:43:45 -080048warn() {
49 echo "brillo_update_payload: warning: $*" >&2
50}
51
Gilad Arnold957ce122015-10-14 16:02:55 -070052die() {
53 echo "brillo_update_payload: error: $*" >&2
54 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070055}
56
Gilad Arnold957ce122015-10-14 16:02:55 -070057# Loads shflags. We first look at the default install location; then look for
58# crosutils (chroot); finally check our own directory (au-generator zipfile).
59load_shflags() {
60 local my_dir="$(dirname "$(readlink -f "$0")")"
61 local path
62 for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do
63 if [[ -r "${path}/shflags" ]]; then
64 . "${path}/shflags" || die "Could not load ${path}/shflags."
65 return
66 fi
67 done
68 die "Could not find shflags."
69}
70
71load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -070072
Alex Deymoc64ffd52015-09-25 18:10:07 -070073HELP_GENERATE="generate: Generate an unsigned update payload."
74HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
75for signing."
76HELP_SIGN="sign: Insert the signatures into the unsigned payload."
77
78usage() {
79 echo "Supported commands:"
80 echo
81 echo "${HELP_GENERATE}"
82 echo "${HELP_HASH}"
83 echo "${HELP_SIGN}"
84 echo
85 echo "Use: \"$0 <command> --help\" for more options."
86}
87
88# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -070089if [[ $# -lt 1 ]]; then
90 echo "Please specify a command [generate|hash|sign]"
91 exit 1
92fi
93
Alex Deymoc64ffd52015-09-25 18:10:07 -070094# Parse command.
95COMMAND="${1:-}"
96shift
97
98case "${COMMAND}" in
99 generate)
100 FLAGS_HELP="${HELP_GENERATE}"
101 ;;
102
103 hash)
104 FLAGS_HELP="${HELP_HASH}"
105 ;;
106
107 sign)
108 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700109 ;;
110 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700111 echo "Unrecognized command: \"${COMMAND}\"" >&2
112 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700113 exit 1
114 ;;
115esac
116
Jason Kusumabe998f42015-09-03 15:53:13 -0700117# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700118FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
119${FLAGS_HELP}"
120
121if [[ "${COMMAND}" == "generate" ]]; then
122 DEFINE_string payload "" \
123 "Path to output the generated unsigned payload file."
124 DEFINE_string target_image "" \
125 "Path to the target image that should be sent to clients."
126 DEFINE_string source_image "" \
127 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700128 DEFINE_string metadata_size_file "" \
129 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700130fi
131if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
132 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
133 DEFINE_string signature_size "" \
134 "Signature sizes in bytes in the following format: size1:size2[:...]"
135fi
136if [[ "${COMMAND}" == "hash" ]]; then
137 DEFINE_string metadata_hash_file "" \
138 "Optional: Path to output metadata hash file."
139 DEFINE_string payload_hash_file "" \
140 "Optional: Path to output payload hash file."
141fi
142if [[ "${COMMAND}" == "sign" ]]; then
143 DEFINE_string payload "" \
144 "Path to output the generated unsigned payload file."
145 DEFINE_string metadata_signature_file "" \
146 "The metatada signatures in the following format: \
147metadata_signature1:metadata_signature2[:...]"
148 DEFINE_string payload_signature_file "" \
149 "The payload signatures in the following format: \
150payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700151 DEFINE_string metadata_size_file "" \
152 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700153fi
Jason Kusumabe998f42015-09-03 15:53:13 -0700154DEFINE_string work_dir "/tmp" "Where to dump temporary files."
155
156# Parse command line flag arguments
157FLAGS "$@" || exit 1
158eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700159set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700160
Alex Deymo89ff9e32015-09-15 19:29:01 -0700161# Associative arrays from partition name to file in the source and target
162# images. The size of the updated area must be the size of the file.
163declare -A SRC_PARTITIONS
164declare -A DST_PARTITIONS
165
166# A list of temporary files to remove during cleanup.
167CLEANUP_FILES=()
168
Alex Deymo48b502a2015-09-17 19:00:18 -0700169# Global options to force the version of the payload.
170FORCE_MAJOR_VERSION=""
171FORCE_MINOR_VERSION=""
172
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800173# Path to the postinstall config file in target image if exists.
174POSTINSTALL_CONFIG_FILE=""
175
Alex Deymoc97df432015-09-25 17:23:52 -0700176# read_option_int <file.txt> <option_key> [default_value]
177#
178# Reads the unsigned integer value associated with |option_key| in a key=value
179# file |file.txt|. Prints the read value if found and valid, otherwise prints
180# the |default_value|.
181read_option_uint() {
182 local file_txt="$1"
183 local option_key="$2"
184 local default_value="${3:-}"
185 local value
186 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
187 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
188 echo "${value}"
189 return
190 fi
191 fi
192 echo "${default_value}"
193}
194
Alex Deymo89ff9e32015-09-15 19:29:01 -0700195# Create a temporary file in the work_dir with an optional pattern name.
196# Prints the name of the newly created file.
197create_tempfile() {
198 local pattern="${1:-tempfile.XXXXXX}"
199 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
200}
Jason Kusumabe998f42015-09-03 15:53:13 -0700201
202cleanup() {
203 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700204 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700205
206 # If we are cleaning up after an error, or if we got an error during
207 # cleanup (even if we eventually succeeded) return a non-zero exit
208 # code. This triggers additional logging in most environments that call
209 # this script.
210 if [[ -n "${err}" ]]; then
211 die "Cleanup encountered an error."
212 fi
213}
214
215cleanup_on_error() {
216 trap - INT TERM ERR EXIT
217 cleanup
218 die "Cleanup success after an error."
219}
220
221cleanup_on_exit() {
222 trap - INT TERM ERR EXIT
223 cleanup
224}
225
226trap cleanup_on_error INT TERM ERR
227trap cleanup_on_exit EXIT
228
Alex Deymo48b502a2015-09-17 19:00:18 -0700229
230# extract_image <image> <partitions_array>
231#
232# Detect the format of the |image| file and extract its updatable partitions
233# into new temporary files. Add the list of partition names and its files to the
234# associative array passed in |partitions_array|.
235extract_image() {
236 local image="$1"
237
238 # Brillo images are zip files. We detect the 4-byte magic header of the zip
239 # file.
240 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
241 if [[ "${magic}" == "504b0304" ]]; then
242 echo "Detected .zip file, extracting Brillo image."
243 extract_image_brillo "$@"
244 return
245 fi
246
247 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
248 # bundled here and we will use it to extract the partitions, so the GPT
249 # headers must be valid.
250 if cgpt show -q -n "${image}" >/dev/null; then
251 echo "Detected GPT image, extracting Chrome OS image."
252 extract_image_cros "$@"
253 return
254 fi
255
256 die "Couldn't detect the image format of ${image}"
257}
258
Alex Deymo89ff9e32015-09-15 19:29:01 -0700259# extract_image_cros <image.bin> <partitions_array>
260#
Alex Deymo48b502a2015-09-17 19:00:18 -0700261# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700262extract_image_cros() {
263 local image="$1"
264 local partitions_array="$2"
265
266 local kernel root
267 kernel=$(create_tempfile "kernel.bin.XXXXXX")
268 CLEANUP_FILES+=("${kernel}")
269 root=$(create_tempfile "root.bin.XXXXXX")
270 CLEANUP_FILES+=("${root}")
271
272 cros_generate_update_payload --extract \
273 --image "${image}" \
274 --kern_path "${kernel}" --root_path "${root}" \
275 --work_dir "${FLAGS_work_dir}" --outside_chroot
276
Alex Deymo83f2f702015-10-14 14:49:33 -0700277 # Chrome OS uses major_version 1 payloads for all versions, even if the
278 # updater supports a newer major version.
279 FORCE_MAJOR_VERSION="1"
280
Alex Deymo48b502a2015-09-17 19:00:18 -0700281 # When generating legacy Chrome OS images, we need to use "boot" and "system"
282 # for the partition names to be compatible with updating Brillo devices with
283 # Chrome OS images.
284 eval ${partitions_array}[boot]=\""${kernel}"\"
285 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700286
287 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700288 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700289 varname="${partitions_array}[${part}]"
290 printf "md5sum of %s: " "${varname}"
291 md5sum "${!varname}"
292 done
293}
294
Alex Deymo48b502a2015-09-17 19:00:18 -0700295# extract_image_brillo <target_files.zip> <partitions_array>
296#
297# Extract the A/B updated partitions from a Brillo target_files zip file into
298# new temporary files.
299extract_image_brillo() {
300 local image="$1"
301 local partitions_array="$2"
302
Alex Deymo48b502a2015-09-17 19:00:18 -0700303 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800304 local ab_partitions_list
305 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
306 CLEANUP_FILES+=("${ab_partitions_list}")
307 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
308 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
309 die "Invalid partition names found in the partition list."
310 fi
311 partitions=($(cat "${ab_partitions_list}"))
312 if [[ ${#partitions[@]} -eq 0 ]]; then
313 die "The list of partitions is empty. Can't generate a payload."
314 fi
315 else
316 warn "No ab_partitions.txt found. Using default."
317 fi
318 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700319
Alex Deymo83f2f702015-10-14 14:49:33 -0700320 # All Brillo updaters support major version 2.
321 FORCE_MAJOR_VERSION="2"
322
Alex Deymo48b502a2015-09-17 19:00:18 -0700323 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800324 # Source image
325 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700326 CLEANUP_FILES+=("${ue_config}")
327 if ! unzip -p "${image}" "META/update_engine_config.txt" \
328 >"${ue_config}"; then
329 warn "No update_engine_config.txt found. Assuming pre-release image, \
330using payload minor version 2"
331 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700332 # For delta payloads, we use the major and minor version supported by the
333 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700334 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
335 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700336 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
337 "PAYLOAD_MAJOR_VERSION" 2)
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800338 else
339 # Target image
340 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
341 CLEANUP_FILES+=("${postinstall_config}")
342 if unzip -p "${image}" "META/postinstall_config.txt" \
343 >"${postinstall_config}"; then
344 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
345 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700346 fi
347
348 local part part_file temp_raw filesize
349 for part in "${partitions[@]}"; do
350 part_file=$(create_tempfile "${part}.img.XXXXXX")
351 CLEANUP_FILES+=("${part_file}")
352 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
353
354 # If the partition is stored as an Android sparse image file, we need to
355 # convert them to a raw image for the update.
356 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
357 if [[ "${magic}" == "3aff26ed" ]]; then
358 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
359 CLEANUP_FILES+=("${temp_raw}")
360 echo "Converting Android sparse image ${part}.img to RAW."
361 simg2img "${part_file}" "${temp_raw}"
362 # At this point, we can drop the contents of the old part_file file, but
363 # we can't delete the file because it will be deleted in cleanup.
364 true >"${part_file}"
365 part_file="${temp_raw}"
366 fi
367
368 # delta_generator only supports images multiple of 4 KiB, so we pad with
369 # zeros if needed.
370 filesize=$(stat -c%s "${part_file}")
371 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
372 echo "Rounding up partition ${part}.img to multiple of 4 KiB."
373 : $(( filesize = (filesize + 4095) & -4096 ))
374 truncate --size="${filesize}" "${part_file}"
375 fi
376
377 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
378 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
379 done
380}
381
Jason Kusumabe998f42015-09-03 15:53:13 -0700382validate_generate() {
383 [[ -n "${FLAGS_payload}" ]] ||
384 die "Error: you must specify an output filename with --payload FILENAME"
385
386 [[ -n "${FLAGS_target_image}" ]] ||
387 die "Error: you must specify a target image with --target_image FILENAME"
388}
389
390cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700391 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700392 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700393 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700394 fi
395
Alex Deymo48b502a2015-09-17 19:00:18 -0700396 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700397
Alex Deymo48b502a2015-09-17 19:00:18 -0700398 extract_image "${FLAGS_target_image}" DST_PARTITIONS
Alex Deymo89ff9e32015-09-15 19:29:01 -0700399 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700400 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700401 fi
402
Alex Deymo48b502a2015-09-17 19:00:18 -0700403 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800404 # Common payload args:
405 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
406
407 local part old_partitions="" new_partitions="" partition_names=""
408 for part in "${!DST_PARTITIONS[@]}"; do
409 if [[ -n "${partition_names}" ]]; then
410 partition_names+=":"
411 new_partitions+=":"
412 old_partitions+=":"
413 fi
414 partition_names+="${part}"
415 new_partitions+="${DST_PARTITIONS[${part}]}"
416 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
417 done
418
419 # Target image args:
420 GENERATOR_ARGS+=(
421 -partition_names="${partition_names}"
422 -new_partitions="${new_partitions}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700423 )
424
Alex Deymo89ff9e32015-09-15 19:29:01 -0700425 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800426 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700427 GENERATOR_ARGS+=(
Alex Deymo168b5352015-11-04 13:51:52 -0800428 -old_partitions="${old_partitions}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700429 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700430 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
431 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
432 fi
433 fi
434
435 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
436 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700437 fi
438
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700439 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
440 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
441 fi
442
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800443 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
444 GENERATOR_ARGS+=(
445 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
446 )
447 fi
448
Jason Kusumabe998f42015-09-03 15:53:13 -0700449 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700450 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700451
Alex Deymo89ff9e32015-09-15 19:29:01 -0700452 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700453}
454
455validate_hash() {
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
Jason Kusumabe998f42015-09-03 15:53:13 -0700463 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiangbf1266f2015-10-26 11:29:24 -0700464 die "Error: you must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800465
466 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
467 die "Error: you must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700468}
469
470cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700471 "${GENERATOR}" \
472 -in_file="${FLAGS_unsigned_payload}" \
473 -signature_size="${FLAGS_signature_size}" \
474 -out_hash_file="${FLAGS_payload_hash_file}" \
475 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700476
Jason Kusumabe998f42015-09-03 15:53:13 -0700477 echo "Done generating hash."
478}
479
480validate_sign() {
481 [[ -n "${FLAGS_signature_size}" ]] ||
482 die "Error: you must specify signature size with --signature_size SIZES"
483
484 [[ -n "${FLAGS_unsigned_payload}" ]] ||
485 die "Error: you must specify the input unsigned payload with \
486--unsigned_payload FILENAME"
487
488 [[ -n "${FLAGS_payload}" ]] ||
489 die "Error: you must specify the output signed payload with \
490--payload FILENAME"
491
492 [[ -n "${FLAGS_payload_signature_file}" ]] ||
493 die "Error: you must specify the payload signature file with \
494--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700495
496 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
497 die "Error: you must specify the metadata signature file with \
498--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700499}
500
501cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700502 GENERATOR_ARGS=(
503 -in_file="${FLAGS_unsigned_payload}"
504 -signature_size="${FLAGS_signature_size}"
505 -signature_file="${FLAGS_payload_signature_file}"
506 -metadata_signature_file="${FLAGS_metadata_signature_file}"
507 -out_file="${FLAGS_payload}"
508 )
509
510 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
511 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
512 fi
513
514 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700515 echo "Done signing payload."
516}
517
Jason Kusumabe998f42015-09-03 15:53:13 -0700518# Sanity check that the real generator exists:
519GENERATOR="$(which delta_generator)"
520[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
521
522case "$COMMAND" in
523 generate) validate_generate
524 cmd_generate
525 ;;
526 hash) validate_hash
527 cmd_hash
528 ;;
529 sign) validate_sign
530 cmd_sign
531 ;;
532esac