blob: 3d102c776894fb65844a5114daae472b19fa81f4 [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
Alex Deymo98e691c2016-02-04 21:05:45 -080014# properties generate a properties file from a payload
Jason Kusumabe998f42015-09-03 15:53:13 -070015#
16# Generate command arguments:
Jason Kusuma9a4cae22015-10-08 18:17:57 -070017# --payload generated unsigned payload output file
18# --source_image if defined, generate a delta payload from the specified
19# image to the target_image
20# --target_image the target image that should be sent to clients
21# --metadata_size_file if defined, generate a file containing the size of the payload
22# metadata in bytes to the specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070023#
24# Hash command arguments:
25# --unsigned_payload the input unsigned payload to generate the hash from
26# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070027# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070028# --payload_hash_file if defined, generate a payload hash and output to the
29# specified file
30# --metadata_hash_file if defined, generate a metadata hash and output to the
31# specified file
32#
33# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070034# --unsigned_payload the input unsigned payload to insert the signatures
35# --payload the output signed payload
36# --signature_size signature sizes in bytes in the following format:
37# "size1:size2[:...]"
38# --payload_signature_file the payload signature files in the following
39# format:
40# "payload_signature1:payload_signature2[:...]"
41# --metadata_signature_file the metadata signature files in the following
42# format:
43# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070044# --metadata_size_file if defined, generate a file containing the size of
45# the signed payload metadata in bytes to the
46# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070047# Note that the number of signature sizes and payload signatures have to match.
Alex Deymo98e691c2016-02-04 21:05:45 -080048#
49# Properties command arguments:
50# --payload the input signed or unsigned payload
51# --properties_file the output path where to write the properties, or
52# '-' for stdout.
53
Jason Kusumabe998f42015-09-03 15:53:13 -070054
Alex Deymo61e1fa82016-01-19 15:16:34 -080055# Exit codes:
56EX_UNSUPPORTED_DELTA=100
57
Jason Kusumaf514c542015-11-05 18:43:45 -080058warn() {
59 echo "brillo_update_payload: warning: $*" >&2
60}
61
Gilad Arnold957ce122015-10-14 16:02:55 -070062die() {
63 echo "brillo_update_payload: error: $*" >&2
64 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070065}
66
Gilad Arnold957ce122015-10-14 16:02:55 -070067# Loads shflags. We first look at the default install location; then look for
68# crosutils (chroot); finally check our own directory (au-generator zipfile).
69load_shflags() {
70 local my_dir="$(dirname "$(readlink -f "$0")")"
71 local path
72 for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do
73 if [[ -r "${path}/shflags" ]]; then
74 . "${path}/shflags" || die "Could not load ${path}/shflags."
75 return
76 fi
77 done
78 die "Could not find shflags."
79}
80
81load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -070082
Alex Deymoc64ffd52015-09-25 18:10:07 -070083HELP_GENERATE="generate: Generate an unsigned update payload."
84HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
85for signing."
86HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -080087HELP_PROPERTIES="properties: Extract payload properties to a file."
Alex Deymoc64ffd52015-09-25 18:10:07 -070088
89usage() {
90 echo "Supported commands:"
91 echo
92 echo "${HELP_GENERATE}"
93 echo "${HELP_HASH}"
94 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -080095 echo "${HELP_PROPERTIES}"
Alex Deymoc64ffd52015-09-25 18:10:07 -070096 echo
97 echo "Use: \"$0 <command> --help\" for more options."
98}
99
100# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700101if [[ $# -lt 1 ]]; then
Alex Deymo98e691c2016-02-04 21:05:45 -0800102 echo "Please specify a command [generate|hash|sign|properties]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700103 exit 1
104fi
105
Alex Deymoc64ffd52015-09-25 18:10:07 -0700106# Parse command.
107COMMAND="${1:-}"
108shift
109
110case "${COMMAND}" in
111 generate)
112 FLAGS_HELP="${HELP_GENERATE}"
113 ;;
114
115 hash)
116 FLAGS_HELP="${HELP_HASH}"
117 ;;
118
119 sign)
120 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700121 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800122
123 properties)
124 FLAGS_HELP="${HELP_PROPERTIES}"
125 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700126 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700127 echo "Unrecognized command: \"${COMMAND}\"" >&2
128 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700129 exit 1
130 ;;
131esac
132
Jason Kusumabe998f42015-09-03 15:53:13 -0700133# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700134FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
135${FLAGS_HELP}"
136
137if [[ "${COMMAND}" == "generate" ]]; then
138 DEFINE_string payload "" \
139 "Path to output the generated unsigned payload file."
140 DEFINE_string target_image "" \
141 "Path to the target image that should be sent to clients."
142 DEFINE_string source_image "" \
143 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700144 DEFINE_string metadata_size_file "" \
145 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700146fi
147if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
148 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
149 DEFINE_string signature_size "" \
150 "Signature sizes in bytes in the following format: size1:size2[:...]"
151fi
152if [[ "${COMMAND}" == "hash" ]]; then
153 DEFINE_string metadata_hash_file "" \
154 "Optional: Path to output metadata hash file."
155 DEFINE_string payload_hash_file "" \
156 "Optional: Path to output payload hash file."
157fi
158if [[ "${COMMAND}" == "sign" ]]; then
159 DEFINE_string payload "" \
160 "Path to output the generated unsigned payload file."
161 DEFINE_string metadata_signature_file "" \
162 "The metatada signatures in the following format: \
163metadata_signature1:metadata_signature2[:...]"
164 DEFINE_string payload_signature_file "" \
165 "The payload signatures in the following format: \
166payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700167 DEFINE_string metadata_size_file "" \
168 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700169fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800170if [[ "${COMMAND}" == "properties" ]]; then
171 DEFINE_string payload "" \
172 "Path to the input signed or unsigned payload file."
173 DEFINE_string properties_file "-" \
174 "Path to output the extracted property files. If '-' is passed stdout will \
175be used."
176fi
177
Jason Kusumabe998f42015-09-03 15:53:13 -0700178DEFINE_string work_dir "/tmp" "Where to dump temporary files."
179
180# Parse command line flag arguments
181FLAGS "$@" || exit 1
182eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700183set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700184
Alex Deymo89ff9e32015-09-15 19:29:01 -0700185# Associative arrays from partition name to file in the source and target
186# images. The size of the updated area must be the size of the file.
187declare -A SRC_PARTITIONS
188declare -A DST_PARTITIONS
189
190# A list of temporary files to remove during cleanup.
191CLEANUP_FILES=()
192
Alex Deymo48b502a2015-09-17 19:00:18 -0700193# Global options to force the version of the payload.
194FORCE_MAJOR_VERSION=""
195FORCE_MINOR_VERSION=""
196
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800197# Path to the postinstall config file in target image if exists.
198POSTINSTALL_CONFIG_FILE=""
199
Sen Jiang3a92aa22016-03-01 14:22:52 -0800200# The fingerprint of zlib in the source image.
201ZLIB_FINGERPRINT=""
202
Alex Deymoc97df432015-09-25 17:23:52 -0700203# read_option_int <file.txt> <option_key> [default_value]
204#
205# Reads the unsigned integer value associated with |option_key| in a key=value
206# file |file.txt|. Prints the read value if found and valid, otherwise prints
207# the |default_value|.
208read_option_uint() {
209 local file_txt="$1"
210 local option_key="$2"
211 local default_value="${3:-}"
212 local value
213 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
214 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
215 echo "${value}"
216 return
217 fi
218 fi
219 echo "${default_value}"
220}
221
Alex Deymo89ff9e32015-09-15 19:29:01 -0700222# Create a temporary file in the work_dir with an optional pattern name.
223# Prints the name of the newly created file.
224create_tempfile() {
225 local pattern="${1:-tempfile.XXXXXX}"
226 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
227}
Jason Kusumabe998f42015-09-03 15:53:13 -0700228
229cleanup() {
230 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700231 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700232
233 # If we are cleaning up after an error, or if we got an error during
234 # cleanup (even if we eventually succeeded) return a non-zero exit
235 # code. This triggers additional logging in most environments that call
236 # this script.
237 if [[ -n "${err}" ]]; then
238 die "Cleanup encountered an error."
239 fi
240}
241
242cleanup_on_error() {
243 trap - INT TERM ERR EXIT
244 cleanup
245 die "Cleanup success after an error."
246}
247
248cleanup_on_exit() {
249 trap - INT TERM ERR EXIT
250 cleanup
251}
252
253trap cleanup_on_error INT TERM ERR
254trap cleanup_on_exit EXIT
255
Alex Deymo48b502a2015-09-17 19:00:18 -0700256
257# extract_image <image> <partitions_array>
258#
259# Detect the format of the |image| file and extract its updatable partitions
260# into new temporary files. Add the list of partition names and its files to the
261# associative array passed in |partitions_array|.
262extract_image() {
263 local image="$1"
264
265 # Brillo images are zip files. We detect the 4-byte magic header of the zip
266 # file.
267 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
268 if [[ "${magic}" == "504b0304" ]]; then
269 echo "Detected .zip file, extracting Brillo image."
270 extract_image_brillo "$@"
271 return
272 fi
273
274 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
275 # bundled here and we will use it to extract the partitions, so the GPT
276 # headers must be valid.
277 if cgpt show -q -n "${image}" >/dev/null; then
278 echo "Detected GPT image, extracting Chrome OS image."
279 extract_image_cros "$@"
280 return
281 fi
282
283 die "Couldn't detect the image format of ${image}"
284}
285
Alex Deymo89ff9e32015-09-15 19:29:01 -0700286# extract_image_cros <image.bin> <partitions_array>
287#
Alex Deymo48b502a2015-09-17 19:00:18 -0700288# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700289extract_image_cros() {
290 local image="$1"
291 local partitions_array="$2"
292
293 local kernel root
294 kernel=$(create_tempfile "kernel.bin.XXXXXX")
295 CLEANUP_FILES+=("${kernel}")
296 root=$(create_tempfile "root.bin.XXXXXX")
297 CLEANUP_FILES+=("${root}")
298
299 cros_generate_update_payload --extract \
300 --image "${image}" \
301 --kern_path "${kernel}" --root_path "${root}" \
302 --work_dir "${FLAGS_work_dir}" --outside_chroot
303
Alex Deymo83f2f702015-10-14 14:49:33 -0700304 # Chrome OS uses major_version 1 payloads for all versions, even if the
305 # updater supports a newer major version.
306 FORCE_MAJOR_VERSION="1"
307
Sen Jiang3a92aa22016-03-01 14:22:52 -0800308 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
309 # Copy from zlib_fingerprint in source image to stdout.
310 ZLIB_FINGERPRINT=$(e2cp "${root}":/etc/zlib_fingerprint -)
311 fi
312
Alex Deymo48b502a2015-09-17 19:00:18 -0700313 # When generating legacy Chrome OS images, we need to use "boot" and "system"
314 # for the partition names to be compatible with updating Brillo devices with
315 # Chrome OS images.
316 eval ${partitions_array}[boot]=\""${kernel}"\"
317 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700318
319 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700320 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700321 varname="${partitions_array}[${part}]"
322 printf "md5sum of %s: " "${varname}"
323 md5sum "${!varname}"
324 done
325}
326
Alex Deymo48b502a2015-09-17 19:00:18 -0700327# extract_image_brillo <target_files.zip> <partitions_array>
328#
329# Extract the A/B updated partitions from a Brillo target_files zip file into
330# new temporary files.
331extract_image_brillo() {
332 local image="$1"
333 local partitions_array="$2"
334
Alex Deymo48b502a2015-09-17 19:00:18 -0700335 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800336 local ab_partitions_list
337 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
338 CLEANUP_FILES+=("${ab_partitions_list}")
339 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
340 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
341 die "Invalid partition names found in the partition list."
342 fi
343 partitions=($(cat "${ab_partitions_list}"))
344 if [[ ${#partitions[@]} -eq 0 ]]; then
345 die "The list of partitions is empty. Can't generate a payload."
346 fi
347 else
348 warn "No ab_partitions.txt found. Using default."
349 fi
350 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700351
Alex Deymo83f2f702015-10-14 14:49:33 -0700352 # All Brillo updaters support major version 2.
353 FORCE_MAJOR_VERSION="2"
354
Alex Deymo48b502a2015-09-17 19:00:18 -0700355 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800356 # Source image
357 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700358 CLEANUP_FILES+=("${ue_config}")
359 if ! unzip -p "${image}" "META/update_engine_config.txt" \
360 >"${ue_config}"; then
361 warn "No update_engine_config.txt found. Assuming pre-release image, \
362using payload minor version 2"
363 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700364 # For delta payloads, we use the major and minor version supported by the
365 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700366 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
367 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700368 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
369 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800370
371 # Brillo support for deltas started with minor version 3.
372 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
373 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
374Disabling deltas for this source version."
375 exit ${EX_UNSUPPORTED_DELTA}
376 fi
Sen Jiang3a92aa22016-03-01 14:22:52 -0800377
378 if [[ "${FORCE_MINOR_VERSION}" -ge 4 ]]; then
379 ZLIB_FINGERPRINT=$(unzip -p "${image}" "META/zlib_fingerprint.txt")
380 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800381 else
382 # Target image
383 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
384 CLEANUP_FILES+=("${postinstall_config}")
385 if unzip -p "${image}" "META/postinstall_config.txt" \
386 >"${postinstall_config}"; then
387 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
388 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700389 fi
390
391 local part part_file temp_raw filesize
392 for part in "${partitions[@]}"; do
393 part_file=$(create_tempfile "${part}.img.XXXXXX")
394 CLEANUP_FILES+=("${part_file}")
395 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
396
397 # If the partition is stored as an Android sparse image file, we need to
398 # convert them to a raw image for the update.
399 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
400 if [[ "${magic}" == "3aff26ed" ]]; then
401 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
402 CLEANUP_FILES+=("${temp_raw}")
403 echo "Converting Android sparse image ${part}.img to RAW."
404 simg2img "${part_file}" "${temp_raw}"
405 # At this point, we can drop the contents of the old part_file file, but
406 # we can't delete the file because it will be deleted in cleanup.
407 true >"${part_file}"
408 part_file="${temp_raw}"
409 fi
410
411 # delta_generator only supports images multiple of 4 KiB, so we pad with
412 # zeros if needed.
413 filesize=$(stat -c%s "${part_file}")
414 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
415 echo "Rounding up partition ${part}.img to multiple of 4 KiB."
416 : $(( filesize = (filesize + 4095) & -4096 ))
417 truncate --size="${filesize}" "${part_file}"
418 fi
419
420 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
421 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
422 done
423}
424
Jason Kusumabe998f42015-09-03 15:53:13 -0700425validate_generate() {
426 [[ -n "${FLAGS_payload}" ]] ||
427 die "Error: you must specify an output filename with --payload FILENAME"
428
429 [[ -n "${FLAGS_target_image}" ]] ||
430 die "Error: you must specify a target image with --target_image FILENAME"
431}
432
433cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700434 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700435 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700436 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700437 fi
438
Alex Deymo48b502a2015-09-17 19:00:18 -0700439 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700440
Alex Deymo48b502a2015-09-17 19:00:18 -0700441 extract_image "${FLAGS_target_image}" DST_PARTITIONS
Alex Deymo89ff9e32015-09-15 19:29:01 -0700442 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700443 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700444 fi
445
Alex Deymo48b502a2015-09-17 19:00:18 -0700446 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800447 # Common payload args:
448 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
449
450 local part old_partitions="" new_partitions="" partition_names=""
451 for part in "${!DST_PARTITIONS[@]}"; do
452 if [[ -n "${partition_names}" ]]; then
453 partition_names+=":"
454 new_partitions+=":"
455 old_partitions+=":"
456 fi
457 partition_names+="${part}"
458 new_partitions+="${DST_PARTITIONS[${part}]}"
459 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
460 done
461
462 # Target image args:
463 GENERATOR_ARGS+=(
464 -partition_names="${partition_names}"
465 -new_partitions="${new_partitions}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700466 )
467
Alex Deymo89ff9e32015-09-15 19:29:01 -0700468 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800469 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700470 GENERATOR_ARGS+=(
Alex Deymo168b5352015-11-04 13:51:52 -0800471 -old_partitions="${old_partitions}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700472 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700473 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
474 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
475 fi
Sen Jiang3a92aa22016-03-01 14:22:52 -0800476 if [[ -n "${ZLIB_FINGERPRINT}" ]]; then
477 GENERATOR_ARGS+=( --zlib_fingerprint="${ZLIB_FINGERPRINT}" )
478 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700479 fi
480
481 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
482 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700483 fi
484
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700485 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
486 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
487 fi
488
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800489 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
490 GENERATOR_ARGS+=(
491 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
492 )
493 fi
494
Jason Kusumabe998f42015-09-03 15:53:13 -0700495 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700496 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700497
Alex Deymo89ff9e32015-09-15 19:29:01 -0700498 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700499}
500
501validate_hash() {
502 [[ -n "${FLAGS_signature_size}" ]] ||
503 die "Error: you must specify signature size with --signature_size SIZES"
504
505 [[ -n "${FLAGS_unsigned_payload}" ]] ||
506 die "Error: you must specify the input unsigned payload with \
507--unsigned_payload FILENAME"
508
Jason Kusumabe998f42015-09-03 15:53:13 -0700509 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiangbf1266f2015-10-26 11:29:24 -0700510 die "Error: you must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800511
512 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
513 die "Error: you must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700514}
515
516cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700517 "${GENERATOR}" \
518 -in_file="${FLAGS_unsigned_payload}" \
519 -signature_size="${FLAGS_signature_size}" \
520 -out_hash_file="${FLAGS_payload_hash_file}" \
521 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700522
Jason Kusumabe998f42015-09-03 15:53:13 -0700523 echo "Done generating hash."
524}
525
526validate_sign() {
527 [[ -n "${FLAGS_signature_size}" ]] ||
528 die "Error: you must specify signature size with --signature_size SIZES"
529
530 [[ -n "${FLAGS_unsigned_payload}" ]] ||
531 die "Error: you must specify the input unsigned payload with \
532--unsigned_payload FILENAME"
533
534 [[ -n "${FLAGS_payload}" ]] ||
535 die "Error: you must specify the output signed payload with \
536--payload FILENAME"
537
538 [[ -n "${FLAGS_payload_signature_file}" ]] ||
539 die "Error: you must specify the payload signature file with \
540--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700541
542 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
543 die "Error: you must specify the metadata signature file with \
544--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700545}
546
547cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700548 GENERATOR_ARGS=(
549 -in_file="${FLAGS_unsigned_payload}"
550 -signature_size="${FLAGS_signature_size}"
551 -signature_file="${FLAGS_payload_signature_file}"
552 -metadata_signature_file="${FLAGS_metadata_signature_file}"
553 -out_file="${FLAGS_payload}"
554 )
555
556 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
557 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
558 fi
559
560 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700561 echo "Done signing payload."
562}
563
Alex Deymo98e691c2016-02-04 21:05:45 -0800564validate_properties() {
565 [[ -n "${FLAGS_payload}" ]] ||
566 die "Error: you must specify the payload file with --payload FILENAME"
567
568 [[ -n "${FLAGS_properties_file}" ]] ||
569 die "Error: you must specify a non empty --properties_file FILENAME"
570}
571
572cmd_properties() {
573 "${GENERATOR}" \
574 -in_file="${FLAGS_payload}" \
575 -properties_file="${FLAGS_properties_file}"
576}
577
Jason Kusumabe998f42015-09-03 15:53:13 -0700578# Sanity check that the real generator exists:
579GENERATOR="$(which delta_generator)"
580[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
581
582case "$COMMAND" in
583 generate) validate_generate
584 cmd_generate
585 ;;
586 hash) validate_hash
587 cmd_hash
588 ;;
589 sign) validate_sign
590 cmd_sign
591 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800592 properties) validate_properties
593 cmd_properties
594 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700595esac