blob: fb6b8626b7cbd5438f6d668cadd477df33b9e994 [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
Alex Deymo20bdc702016-12-07 21:07:11 -0800190# Associative arrays for the .map files associated with each src/dst partition
191# file in SRC_PARTITIONS and DST_PARTITIONS.
192declare -A SRC_PARTITIONS_MAP
193declare -A DST_PARTITIONS_MAP
194
Sen Jiang788c2d92016-03-09 12:48:40 -0800195# List of partition names in order.
196declare -a PARTITIONS_ORDER
197
Alex Deymo89ff9e32015-09-15 19:29:01 -0700198# A list of temporary files to remove during cleanup.
199CLEANUP_FILES=()
200
Alex Deymo48b502a2015-09-17 19:00:18 -0700201# Global options to force the version of the payload.
202FORCE_MAJOR_VERSION=""
203FORCE_MINOR_VERSION=""
204
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800205# Path to the postinstall config file in target image if exists.
206POSTINSTALL_CONFIG_FILE=""
207
Sen Jiange0d04282016-03-01 14:22:52 -0800208# The fingerprint of zlib in the source image.
209ZLIB_FINGERPRINT=""
210
Alex Deymoc97df432015-09-25 17:23:52 -0700211# read_option_int <file.txt> <option_key> [default_value]
212#
213# Reads the unsigned integer value associated with |option_key| in a key=value
214# file |file.txt|. Prints the read value if found and valid, otherwise prints
215# the |default_value|.
216read_option_uint() {
217 local file_txt="$1"
218 local option_key="$2"
219 local default_value="${3:-}"
220 local value
221 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
222 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
223 echo "${value}"
224 return
225 fi
226 fi
227 echo "${default_value}"
228}
229
Sen Jiangd0e9a892016-07-22 16:28:07 -0700230# truncate_file <file_path> <file_size>
231#
232# Truncate the given |file_path| to |file_size| using perl.
233# The truncate binary might not be available.
234truncate_file() {
235 local file_path="$1"
236 local file_size="$2"
237 perl -e "open(FILE, \"+<\", \$ARGV[0]); \
238 truncate(FILE, ${file_size}); \
239 close(FILE);" "${file_path}"
240}
241
Alex Deymo89ff9e32015-09-15 19:29:01 -0700242# Create a temporary file in the work_dir with an optional pattern name.
243# Prints the name of the newly created file.
244create_tempfile() {
245 local pattern="${1:-tempfile.XXXXXX}"
246 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
247}
Jason Kusumabe998f42015-09-03 15:53:13 -0700248
249cleanup() {
250 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700251 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700252
253 # If we are cleaning up after an error, or if we got an error during
254 # cleanup (even if we eventually succeeded) return a non-zero exit
255 # code. This triggers additional logging in most environments that call
256 # this script.
257 if [[ -n "${err}" ]]; then
258 die "Cleanup encountered an error."
259 fi
260}
261
262cleanup_on_error() {
263 trap - INT TERM ERR EXIT
264 cleanup
265 die "Cleanup success after an error."
266}
267
268cleanup_on_exit() {
269 trap - INT TERM ERR EXIT
270 cleanup
271}
272
273trap cleanup_on_error INT TERM ERR
274trap cleanup_on_exit EXIT
275
Alex Deymo48b502a2015-09-17 19:00:18 -0700276
Sen Jiang788c2d92016-03-09 12:48:40 -0800277# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700278#
279# Detect the format of the |image| file and extract its updatable partitions
280# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800281# associative array passed in |partitions_array|. If |partitions_order| is
282# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700283extract_image() {
284 local image="$1"
285
286 # Brillo images are zip files. We detect the 4-byte magic header of the zip
287 # file.
288 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
289 if [[ "${magic}" == "504b0304" ]]; then
290 echo "Detected .zip file, extracting Brillo image."
291 extract_image_brillo "$@"
292 return
293 fi
294
295 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
296 # bundled here and we will use it to extract the partitions, so the GPT
297 # headers must be valid.
298 if cgpt show -q -n "${image}" >/dev/null; then
299 echo "Detected GPT image, extracting Chrome OS image."
300 extract_image_cros "$@"
301 return
302 fi
303
304 die "Couldn't detect the image format of ${image}"
305}
306
Sen Jiang788c2d92016-03-09 12:48:40 -0800307# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700308#
Alex Deymo48b502a2015-09-17 19:00:18 -0700309# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700310extract_image_cros() {
311 local image="$1"
312 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800313 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700314
315 local kernel root
316 kernel=$(create_tempfile "kernel.bin.XXXXXX")
317 CLEANUP_FILES+=("${kernel}")
318 root=$(create_tempfile "root.bin.XXXXXX")
319 CLEANUP_FILES+=("${root}")
320
321 cros_generate_update_payload --extract \
322 --image "${image}" \
323 --kern_path "${kernel}" --root_path "${root}" \
324 --work_dir "${FLAGS_work_dir}" --outside_chroot
325
Alex Deymo83f2f702015-10-14 14:49:33 -0700326 # Chrome OS uses major_version 1 payloads for all versions, even if the
327 # updater supports a newer major version.
328 FORCE_MAJOR_VERSION="1"
329
Sen Jiange0d04282016-03-01 14:22:52 -0800330 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
331 # Copy from zlib_fingerprint in source image to stdout.
332 ZLIB_FINGERPRINT=$(e2cp "${root}":/etc/zlib_fingerprint -)
333 fi
334
Alex Deymo48b502a2015-09-17 19:00:18 -0700335 # When generating legacy Chrome OS images, we need to use "boot" and "system"
336 # for the partition names to be compatible with updating Brillo devices with
337 # Chrome OS images.
338 eval ${partitions_array}[boot]=\""${kernel}"\"
339 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700340
Sen Jiang788c2d92016-03-09 12:48:40 -0800341 if [[ -n "${partitions_order}" ]]; then
342 eval "${partitions_order}=( \"system\" \"boot\" )"
343 fi
344
Alex Deymo89ff9e32015-09-15 19:29:01 -0700345 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700346 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700347 varname="${partitions_array}[${part}]"
348 printf "md5sum of %s: " "${varname}"
349 md5sum "${!varname}"
350 done
351}
352
Sen Jiang788c2d92016-03-09 12:48:40 -0800353# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700354#
355# Extract the A/B updated partitions from a Brillo target_files zip file into
356# new temporary files.
357extract_image_brillo() {
358 local image="$1"
359 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800360 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700361
Alex Deymo48b502a2015-09-17 19:00:18 -0700362 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800363 local ab_partitions_list
364 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
365 CLEANUP_FILES+=("${ab_partitions_list}")
366 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
367 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
368 die "Invalid partition names found in the partition list."
369 fi
370 partitions=($(cat "${ab_partitions_list}"))
371 if [[ ${#partitions[@]} -eq 0 ]]; then
372 die "The list of partitions is empty. Can't generate a payload."
373 fi
374 else
375 warn "No ab_partitions.txt found. Using default."
376 fi
377 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700378
Sen Jiang788c2d92016-03-09 12:48:40 -0800379 if [[ -n "${partitions_order}" ]]; then
380 eval "${partitions_order}=(${partitions[@]})"
381 fi
382
Alex Deymo83f2f702015-10-14 14:49:33 -0700383 # All Brillo updaters support major version 2.
384 FORCE_MAJOR_VERSION="2"
385
Alex Deymo48b502a2015-09-17 19:00:18 -0700386 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800387 # Source image
388 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700389 CLEANUP_FILES+=("${ue_config}")
390 if ! unzip -p "${image}" "META/update_engine_config.txt" \
391 >"${ue_config}"; then
392 warn "No update_engine_config.txt found. Assuming pre-release image, \
393using payload minor version 2"
394 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700395 # For delta payloads, we use the major and minor version supported by the
396 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700397 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
398 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700399 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
400 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800401
402 # Brillo support for deltas started with minor version 3.
403 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
404 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
405Disabling deltas for this source version."
406 exit ${EX_UNSUPPORTED_DELTA}
407 fi
Sen Jiange0d04282016-03-01 14:22:52 -0800408
409 if [[ "${FORCE_MINOR_VERSION}" -ge 4 ]]; then
410 ZLIB_FINGERPRINT=$(unzip -p "${image}" "META/zlib_fingerprint.txt")
411 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800412 else
413 # Target image
414 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
415 CLEANUP_FILES+=("${postinstall_config}")
416 if unzip -p "${image}" "META/postinstall_config.txt" \
417 >"${postinstall_config}"; then
418 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
419 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700420 fi
421
422 local part part_file temp_raw filesize
423 for part in "${partitions[@]}"; do
424 part_file=$(create_tempfile "${part}.img.XXXXXX")
425 CLEANUP_FILES+=("${part_file}")
426 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
427
428 # If the partition is stored as an Android sparse image file, we need to
429 # convert them to a raw image for the update.
430 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
431 if [[ "${magic}" == "3aff26ed" ]]; then
432 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
433 CLEANUP_FILES+=("${temp_raw}")
434 echo "Converting Android sparse image ${part}.img to RAW."
435 simg2img "${part_file}" "${temp_raw}"
436 # At this point, we can drop the contents of the old part_file file, but
437 # we can't delete the file because it will be deleted in cleanup.
438 true >"${part_file}"
439 part_file="${temp_raw}"
440 fi
441
Alex Deymo20bdc702016-12-07 21:07:11 -0800442 # Extract the .map file (if one is available).
443 part_map_file=$(create_tempfile "${part}.map.XXXXXX")
444 CLEANUP_FILES+=("${part_map_file}")
445 unzip -p "${image}" "IMAGES/${part}.map" >"${part_map_file}" || \
446 part_map_file=""
447
Alex Deymoa479a4d2016-05-11 18:13:49 -0700448 # delta_generator only supports images multiple of 4 KiB. For target images
449 # we pad the data with zeros if needed, but for source images we truncate
450 # down the data since the last block of the old image could be padded on
451 # disk with unknown data.
Alex Deymo48b502a2015-09-17 19:00:18 -0700452 filesize=$(stat -c%s "${part_file}")
453 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
Alex Deymoa479a4d2016-05-11 18:13:49 -0700454 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
455 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
456 : $(( filesize = filesize & -4096 ))
457 else
458 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
459 : $(( filesize = (filesize + 4095) & -4096 ))
460 fi
Sen Jiangd0e9a892016-07-22 16:28:07 -0700461 truncate_file "${part_file}" "${filesize}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700462 fi
463
464 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800465 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Alex Deymo48b502a2015-09-17 19:00:18 -0700466 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
467 done
468}
469
Jason Kusumabe998f42015-09-03 15:53:13 -0700470validate_generate() {
471 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700472 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700473
474 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700475 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700476}
477
478cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700479 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700480 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700481 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700482 fi
483
Alex Deymo48b502a2015-09-17 19:00:18 -0700484 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700485
Sen Jiang788c2d92016-03-09 12:48:40 -0800486 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Alex Deymo89ff9e32015-09-15 19:29:01 -0700487 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700488 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700489 fi
490
Alex Deymo48b502a2015-09-17 19:00:18 -0700491 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800492 # Common payload args:
493 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
494
495 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800496 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800497 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800498 if [[ -n "${partition_names}" ]]; then
499 partition_names+=":"
500 new_partitions+=":"
501 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800502 new_mapfiles+=":"
503 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800504 fi
505 partition_names+="${part}"
506 new_partitions+="${DST_PARTITIONS[${part}]}"
507 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800508 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
509 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800510 done
511
512 # Target image args:
513 GENERATOR_ARGS+=(
514 -partition_names="${partition_names}"
515 -new_partitions="${new_partitions}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800516 -new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700517 )
518
Alex Deymo89ff9e32015-09-15 19:29:01 -0700519 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800520 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700521 GENERATOR_ARGS+=(
Alex Deymo168b5352015-11-04 13:51:52 -0800522 -old_partitions="${old_partitions}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800523 -old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700524 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700525 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
526 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
527 fi
Sen Jiange0d04282016-03-01 14:22:52 -0800528 if [[ -n "${ZLIB_FINGERPRINT}" ]]; then
529 GENERATOR_ARGS+=( --zlib_fingerprint="${ZLIB_FINGERPRINT}" )
530 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700531 fi
532
533 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
534 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700535 fi
536
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700537 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
538 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
539 fi
540
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800541 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
542 GENERATOR_ARGS+=(
543 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
544 )
545 fi
546
Jason Kusumabe998f42015-09-03 15:53:13 -0700547 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700548 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700549
Alex Deymo89ff9e32015-09-15 19:29:01 -0700550 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700551}
552
553validate_hash() {
554 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700555 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700556
557 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700558 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700559--unsigned_payload FILENAME"
560
Jason Kusumabe998f42015-09-03 15:53:13 -0700561 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700562 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800563
564 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700565 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700566}
567
568cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700569 "${GENERATOR}" \
570 -in_file="${FLAGS_unsigned_payload}" \
571 -signature_size="${FLAGS_signature_size}" \
572 -out_hash_file="${FLAGS_payload_hash_file}" \
573 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700574
Jason Kusumabe998f42015-09-03 15:53:13 -0700575 echo "Done generating hash."
576}
577
578validate_sign() {
579 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700580 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700581
582 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700583 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700584--unsigned_payload FILENAME"
585
586 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700587 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700588
589 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700590 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700591--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700592
593 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700594 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700595--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700596}
597
598cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700599 GENERATOR_ARGS=(
600 -in_file="${FLAGS_unsigned_payload}"
601 -signature_size="${FLAGS_signature_size}"
602 -signature_file="${FLAGS_payload_signature_file}"
603 -metadata_signature_file="${FLAGS_metadata_signature_file}"
604 -out_file="${FLAGS_payload}"
605 )
606
607 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
608 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
609 fi
610
611 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700612 echo "Done signing payload."
613}
614
Alex Deymo98e691c2016-02-04 21:05:45 -0800615validate_properties() {
616 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700617 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800618
619 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700620 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800621}
622
623cmd_properties() {
624 "${GENERATOR}" \
625 -in_file="${FLAGS_payload}" \
626 -properties_file="${FLAGS_properties_file}"
627}
628
Jason Kusumabe998f42015-09-03 15:53:13 -0700629# Sanity check that the real generator exists:
Sen Jiang13519752016-08-02 16:10:52 -0700630GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700631[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
632
633case "$COMMAND" in
634 generate) validate_generate
635 cmd_generate
636 ;;
637 hash) validate_hash
638 cmd_hash
639 ;;
640 sign) validate_sign
641 cmd_sign
642 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800643 properties) validate_properties
644 cmd_properties
645 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700646esac