blob: 375c0dfbfa8d4b0213345a4311511b65a45eb520 [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."
Sen Jiang8c3c80c2017-06-28 17:13:19 -0700146 DEFINE_string max_timestamp "" \
147 "Optional: The maximum unix timestamp of the OS allowed to apply this \
148payload, should be set to a number higher than the build timestamp of the \
149system running on the device, 0 if not specified."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700150fi
151if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
152 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
153 DEFINE_string signature_size "" \
154 "Signature sizes in bytes in the following format: size1:size2[:...]"
155fi
156if [[ "${COMMAND}" == "hash" ]]; then
157 DEFINE_string metadata_hash_file "" \
158 "Optional: Path to output metadata hash file."
159 DEFINE_string payload_hash_file "" \
160 "Optional: Path to output payload hash file."
161fi
162if [[ "${COMMAND}" == "sign" ]]; then
163 DEFINE_string payload "" \
164 "Path to output the generated unsigned payload file."
165 DEFINE_string metadata_signature_file "" \
166 "The metatada signatures in the following format: \
167metadata_signature1:metadata_signature2[:...]"
168 DEFINE_string payload_signature_file "" \
169 "The payload signatures in the following format: \
170payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700171 DEFINE_string metadata_size_file "" \
172 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700173fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800174if [[ "${COMMAND}" == "properties" ]]; then
175 DEFINE_string payload "" \
176 "Path to the input signed or unsigned payload file."
177 DEFINE_string properties_file "-" \
178 "Path to output the extracted property files. If '-' is passed stdout will \
179be used."
180fi
181
Alex Deymo5fbb1102017-01-12 13:55:52 -0800182DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700183
184# Parse command line flag arguments
185FLAGS "$@" || exit 1
186eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700187set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700188
Alex Deymo5fbb1102017-01-12 13:55:52 -0800189# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
190# ${TMPDIR}.
191TMPDIR="${FLAGS_work_dir}"
192export TMPDIR
193
Alex Deymo89ff9e32015-09-15 19:29:01 -0700194# Associative arrays from partition name to file in the source and target
195# images. The size of the updated area must be the size of the file.
196declare -A SRC_PARTITIONS
197declare -A DST_PARTITIONS
198
Alex Deymo20bdc702016-12-07 21:07:11 -0800199# Associative arrays for the .map files associated with each src/dst partition
200# file in SRC_PARTITIONS and DST_PARTITIONS.
201declare -A SRC_PARTITIONS_MAP
202declare -A DST_PARTITIONS_MAP
203
Sen Jiang788c2d92016-03-09 12:48:40 -0800204# List of partition names in order.
205declare -a PARTITIONS_ORDER
206
Alex Deymo89ff9e32015-09-15 19:29:01 -0700207# A list of temporary files to remove during cleanup.
208CLEANUP_FILES=()
209
Alex Deymo48b502a2015-09-17 19:00:18 -0700210# Global options to force the version of the payload.
211FORCE_MAJOR_VERSION=""
212FORCE_MINOR_VERSION=""
213
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800214# Path to the postinstall config file in target image if exists.
215POSTINSTALL_CONFIG_FILE=""
216
Sen Jiange0d04282016-03-01 14:22:52 -0800217# The fingerprint of zlib in the source image.
218ZLIB_FINGERPRINT=""
219
Alex Deymoc97df432015-09-25 17:23:52 -0700220# read_option_int <file.txt> <option_key> [default_value]
221#
222# Reads the unsigned integer value associated with |option_key| in a key=value
223# file |file.txt|. Prints the read value if found and valid, otherwise prints
224# the |default_value|.
225read_option_uint() {
226 local file_txt="$1"
227 local option_key="$2"
228 local default_value="${3:-}"
229 local value
230 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
231 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
232 echo "${value}"
233 return
234 fi
235 fi
236 echo "${default_value}"
237}
238
Sen Jiangd0e9a892016-07-22 16:28:07 -0700239# truncate_file <file_path> <file_size>
240#
241# Truncate the given |file_path| to |file_size| using perl.
242# The truncate binary might not be available.
243truncate_file() {
244 local file_path="$1"
245 local file_size="$2"
246 perl -e "open(FILE, \"+<\", \$ARGV[0]); \
247 truncate(FILE, ${file_size}); \
248 close(FILE);" "${file_path}"
249}
250
Alex Deymo89ff9e32015-09-15 19:29:01 -0700251# Create a temporary file in the work_dir with an optional pattern name.
252# Prints the name of the newly created file.
253create_tempfile() {
254 local pattern="${1:-tempfile.XXXXXX}"
255 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
256}
Jason Kusumabe998f42015-09-03 15:53:13 -0700257
258cleanup() {
259 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700260 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700261
262 # If we are cleaning up after an error, or if we got an error during
263 # cleanup (even if we eventually succeeded) return a non-zero exit
264 # code. This triggers additional logging in most environments that call
265 # this script.
266 if [[ -n "${err}" ]]; then
267 die "Cleanup encountered an error."
268 fi
269}
270
271cleanup_on_error() {
272 trap - INT TERM ERR EXIT
273 cleanup
274 die "Cleanup success after an error."
275}
276
277cleanup_on_exit() {
278 trap - INT TERM ERR EXIT
279 cleanup
280}
281
282trap cleanup_on_error INT TERM ERR
283trap cleanup_on_exit EXIT
284
Alex Deymo48b502a2015-09-17 19:00:18 -0700285
Sen Jiang788c2d92016-03-09 12:48:40 -0800286# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700287#
288# Detect the format of the |image| file and extract its updatable partitions
289# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800290# associative array passed in |partitions_array|. If |partitions_order| is
291# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700292extract_image() {
293 local image="$1"
294
295 # Brillo images are zip files. We detect the 4-byte magic header of the zip
296 # file.
297 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
298 if [[ "${magic}" == "504b0304" ]]; then
299 echo "Detected .zip file, extracting Brillo image."
300 extract_image_brillo "$@"
301 return
302 fi
303
304 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
305 # bundled here and we will use it to extract the partitions, so the GPT
306 # headers must be valid.
307 if cgpt show -q -n "${image}" >/dev/null; then
308 echo "Detected GPT image, extracting Chrome OS image."
309 extract_image_cros "$@"
310 return
311 fi
312
313 die "Couldn't detect the image format of ${image}"
314}
315
Sen Jiang788c2d92016-03-09 12:48:40 -0800316# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700317#
Alex Deymo48b502a2015-09-17 19:00:18 -0700318# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700319extract_image_cros() {
320 local image="$1"
321 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800322 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700323
324 local kernel root
325 kernel=$(create_tempfile "kernel.bin.XXXXXX")
326 CLEANUP_FILES+=("${kernel}")
327 root=$(create_tempfile "root.bin.XXXXXX")
328 CLEANUP_FILES+=("${root}")
329
330 cros_generate_update_payload --extract \
331 --image "${image}" \
332 --kern_path "${kernel}" --root_path "${root}" \
333 --work_dir "${FLAGS_work_dir}" --outside_chroot
334
Alex Deymo83f2f702015-10-14 14:49:33 -0700335 # Chrome OS uses major_version 1 payloads for all versions, even if the
336 # updater supports a newer major version.
337 FORCE_MAJOR_VERSION="1"
338
Sen Jiange0d04282016-03-01 14:22:52 -0800339 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
340 # Copy from zlib_fingerprint in source image to stdout.
341 ZLIB_FINGERPRINT=$(e2cp "${root}":/etc/zlib_fingerprint -)
342 fi
343
Alex Deymo48b502a2015-09-17 19:00:18 -0700344 # When generating legacy Chrome OS images, we need to use "boot" and "system"
345 # for the partition names to be compatible with updating Brillo devices with
346 # Chrome OS images.
347 eval ${partitions_array}[boot]=\""${kernel}"\"
348 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700349
Sen Jiang788c2d92016-03-09 12:48:40 -0800350 if [[ -n "${partitions_order}" ]]; then
351 eval "${partitions_order}=( \"system\" \"boot\" )"
352 fi
353
Alex Deymo89ff9e32015-09-15 19:29:01 -0700354 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700355 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700356 varname="${partitions_array}[${part}]"
357 printf "md5sum of %s: " "${varname}"
358 md5sum "${!varname}"
359 done
360}
361
Sen Jiang788c2d92016-03-09 12:48:40 -0800362# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700363#
364# Extract the A/B updated partitions from a Brillo target_files zip file into
365# new temporary files.
366extract_image_brillo() {
367 local image="$1"
368 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800369 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700370
Alex Deymo48b502a2015-09-17 19:00:18 -0700371 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800372 local ab_partitions_list
373 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
374 CLEANUP_FILES+=("${ab_partitions_list}")
375 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
376 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
377 die "Invalid partition names found in the partition list."
378 fi
379 partitions=($(cat "${ab_partitions_list}"))
380 if [[ ${#partitions[@]} -eq 0 ]]; then
381 die "The list of partitions is empty. Can't generate a payload."
382 fi
383 else
384 warn "No ab_partitions.txt found. Using default."
385 fi
386 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700387
Sen Jiang788c2d92016-03-09 12:48:40 -0800388 if [[ -n "${partitions_order}" ]]; then
389 eval "${partitions_order}=(${partitions[@]})"
390 fi
391
Alex Deymo83f2f702015-10-14 14:49:33 -0700392 # All Brillo updaters support major version 2.
393 FORCE_MAJOR_VERSION="2"
394
Alex Deymo48b502a2015-09-17 19:00:18 -0700395 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800396 # Source image
397 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700398 CLEANUP_FILES+=("${ue_config}")
399 if ! unzip -p "${image}" "META/update_engine_config.txt" \
400 >"${ue_config}"; then
401 warn "No update_engine_config.txt found. Assuming pre-release image, \
402using payload minor version 2"
403 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700404 # For delta payloads, we use the major and minor version supported by the
405 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700406 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
407 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700408 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
409 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800410
411 # Brillo support for deltas started with minor version 3.
412 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
413 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
414Disabling deltas for this source version."
415 exit ${EX_UNSUPPORTED_DELTA}
416 fi
Sen Jiange0d04282016-03-01 14:22:52 -0800417
418 if [[ "${FORCE_MINOR_VERSION}" -ge 4 ]]; then
419 ZLIB_FINGERPRINT=$(unzip -p "${image}" "META/zlib_fingerprint.txt")
420 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800421 else
422 # Target image
423 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
424 CLEANUP_FILES+=("${postinstall_config}")
425 if unzip -p "${image}" "META/postinstall_config.txt" \
426 >"${postinstall_config}"; then
427 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
428 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700429 fi
430
431 local part part_file temp_raw filesize
432 for part in "${partitions[@]}"; do
433 part_file=$(create_tempfile "${part}.img.XXXXXX")
434 CLEANUP_FILES+=("${part_file}")
435 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
436
437 # If the partition is stored as an Android sparse image file, we need to
438 # convert them to a raw image for the update.
439 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
440 if [[ "${magic}" == "3aff26ed" ]]; then
441 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
442 CLEANUP_FILES+=("${temp_raw}")
443 echo "Converting Android sparse image ${part}.img to RAW."
444 simg2img "${part_file}" "${temp_raw}"
445 # At this point, we can drop the contents of the old part_file file, but
446 # we can't delete the file because it will be deleted in cleanup.
447 true >"${part_file}"
448 part_file="${temp_raw}"
449 fi
450
Alex Deymo20bdc702016-12-07 21:07:11 -0800451 # Extract the .map file (if one is available).
452 part_map_file=$(create_tempfile "${part}.map.XXXXXX")
453 CLEANUP_FILES+=("${part_map_file}")
454 unzip -p "${image}" "IMAGES/${part}.map" >"${part_map_file}" || \
455 part_map_file=""
456
Alex Deymoa479a4d2016-05-11 18:13:49 -0700457 # delta_generator only supports images multiple of 4 KiB. For target images
458 # we pad the data with zeros if needed, but for source images we truncate
459 # down the data since the last block of the old image could be padded on
460 # disk with unknown data.
Alex Deymo48b502a2015-09-17 19:00:18 -0700461 filesize=$(stat -c%s "${part_file}")
462 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
Alex Deymoa479a4d2016-05-11 18:13:49 -0700463 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
464 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
465 : $(( filesize = filesize & -4096 ))
Alex Deymo04c23052017-04-12 18:15:30 -0700466 if [[ ${filesize} == 0 ]]; then
467 echo "Source partition ${part}.img is empty after rounding down," \
468 "skipping."
469 continue
470 fi
Alex Deymoa479a4d2016-05-11 18:13:49 -0700471 else
472 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
473 : $(( filesize = (filesize + 4095) & -4096 ))
474 fi
Sen Jiangd0e9a892016-07-22 16:28:07 -0700475 truncate_file "${part_file}" "${filesize}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700476 fi
477
478 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800479 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Alex Deymo48b502a2015-09-17 19:00:18 -0700480 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
481 done
482}
483
Jason Kusumabe998f42015-09-03 15:53:13 -0700484validate_generate() {
485 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700486 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700487
488 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700489 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700490}
491
492cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700493 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700494 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700495 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700496 fi
497
Alex Deymo48b502a2015-09-17 19:00:18 -0700498 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700499
Sen Jiang788c2d92016-03-09 12:48:40 -0800500 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Alex Deymo89ff9e32015-09-15 19:29:01 -0700501 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700502 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700503 fi
504
Alex Deymo48b502a2015-09-17 19:00:18 -0700505 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800506 # Common payload args:
507 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
508
509 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800510 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800511 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800512 if [[ -n "${partition_names}" ]]; then
513 partition_names+=":"
514 new_partitions+=":"
515 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800516 new_mapfiles+=":"
517 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800518 fi
519 partition_names+="${part}"
520 new_partitions+="${DST_PARTITIONS[${part}]}"
521 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800522 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
523 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800524 done
525
526 # Target image args:
527 GENERATOR_ARGS+=(
528 -partition_names="${partition_names}"
529 -new_partitions="${new_partitions}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800530 -new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700531 )
532
Alex Deymo89ff9e32015-09-15 19:29:01 -0700533 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800534 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700535 GENERATOR_ARGS+=(
Alex Deymo168b5352015-11-04 13:51:52 -0800536 -old_partitions="${old_partitions}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800537 -old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700538 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700539 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
540 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
541 fi
Sen Jiange0d04282016-03-01 14:22:52 -0800542 if [[ -n "${ZLIB_FINGERPRINT}" ]]; then
543 GENERATOR_ARGS+=( --zlib_fingerprint="${ZLIB_FINGERPRINT}" )
544 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700545 fi
546
547 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
548 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700549 fi
550
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700551 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
552 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
553 fi
554
Sen Jiang8c3c80c2017-06-28 17:13:19 -0700555 if [[ -n "${FLAGS_max_timestamp}" ]]; then
556 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
557 fi
558
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800559 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
560 GENERATOR_ARGS+=(
561 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
562 )
563 fi
564
Jason Kusumabe998f42015-09-03 15:53:13 -0700565 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700566 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700567
Alex Deymo89ff9e32015-09-15 19:29:01 -0700568 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700569}
570
571validate_hash() {
572 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700573 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700574
575 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700576 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700577--unsigned_payload FILENAME"
578
Jason Kusumabe998f42015-09-03 15:53:13 -0700579 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700580 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800581
582 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700583 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700584}
585
586cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700587 "${GENERATOR}" \
588 -in_file="${FLAGS_unsigned_payload}" \
589 -signature_size="${FLAGS_signature_size}" \
590 -out_hash_file="${FLAGS_payload_hash_file}" \
591 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700592
Jason Kusumabe998f42015-09-03 15:53:13 -0700593 echo "Done generating hash."
594}
595
596validate_sign() {
597 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700598 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700599
600 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700601 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700602--unsigned_payload FILENAME"
603
604 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700605 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700606
607 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700608 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700609--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700610
611 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700612 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700613--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700614}
615
616cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700617 GENERATOR_ARGS=(
618 -in_file="${FLAGS_unsigned_payload}"
619 -signature_size="${FLAGS_signature_size}"
620 -signature_file="${FLAGS_payload_signature_file}"
621 -metadata_signature_file="${FLAGS_metadata_signature_file}"
622 -out_file="${FLAGS_payload}"
623 )
624
625 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
626 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
627 fi
628
629 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700630 echo "Done signing payload."
631}
632
Alex Deymo98e691c2016-02-04 21:05:45 -0800633validate_properties() {
634 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700635 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800636
637 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700638 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800639}
640
641cmd_properties() {
642 "${GENERATOR}" \
643 -in_file="${FLAGS_payload}" \
644 -properties_file="${FLAGS_properties_file}"
645}
646
Jason Kusumabe998f42015-09-03 15:53:13 -0700647# Sanity check that the real generator exists:
Sen Jiang13519752016-08-02 16:10:52 -0700648GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700649[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
650
651case "$COMMAND" in
652 generate) validate_generate
653 cmd_generate
654 ;;
655 hash) validate_hash
656 cmd_hash
657 ;;
658 sign) validate_sign
659 cmd_sign
660 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800661 properties) validate_properties
662 cmd_properties
663 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700664esac