blob: 77d372c3fe2b97a0d085fd97c52a9e747bfe4d09 [file] [log] [blame]
Jason Kusumabe998f42015-09-03 15:53:13 -07001#!/bin/bash
2
Amin Hassani13520932017-07-26 11:26:05 -07003#
4# Copyright (C) 2015 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
Jason Kusumabe998f42015-09-03 15:53:13 -070018
19# Script to generate a Brillo update for use by the update engine.
20#
21# usage: brillo_update_payload COMMAND [ARGS]
22# The following commands are supported:
23# generate generate an unsigned payload
24# hash generate a payload or metadata hash
25# sign generate a signed payload
Alex Deymo98e691c2016-02-04 21:05:45 -080026# properties generate a properties file from a payload
Amin Hassani13520932017-07-26 11:26:05 -070027# verify verify a payload by recreating a target image.
Tudor Brindusb432db82018-06-29 13:13:27 -070028# check verify a payload using paycheck (static testing)
Jason Kusumabe998f42015-09-03 15:53:13 -070029#
30# Generate command arguments:
Tianjie Xu72d512c2019-08-21 15:20:35 -070031# --payload generated unsigned payload output file
32# --source_image if defined, generate a delta payload from the
33# specified image to the target_image
34# --target_image the target image that should be sent to clients
35# --metadata_size_file if defined, generate a file containing the size
36# of the ayload metadata in bytes to the specified
37# file
38# --disable_fec_computation Disable the on device fec data computation for
39# incremental update. This feature is enabled by
40# default
Jason Kusumabe998f42015-09-03 15:53:13 -070041#
42# Hash command arguments:
43# --unsigned_payload the input unsigned payload to generate the hash from
44# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070045# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070046# --payload_hash_file if defined, generate a payload hash and output to the
47# specified file
48# --metadata_hash_file if defined, generate a metadata hash and output to the
49# specified file
50#
51# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070052# --unsigned_payload the input unsigned payload to insert the signatures
53# --payload the output signed payload
54# --signature_size signature sizes in bytes in the following format:
55# "size1:size2[:...]"
56# --payload_signature_file the payload signature files in the following
57# format:
58# "payload_signature1:payload_signature2[:...]"
59# --metadata_signature_file the metadata signature files in the following
60# format:
61# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070062# --metadata_size_file if defined, generate a file containing the size of
63# the signed payload metadata in bytes to the
64# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070065# Note that the number of signature sizes and payload signatures have to match.
Alex Deymo98e691c2016-02-04 21:05:45 -080066#
67# Properties command arguments:
68# --payload the input signed or unsigned payload
69# --properties_file the output path where to write the properties, or
70# '-' for stdout.
Amin Hassani13520932017-07-26 11:26:05 -070071# Verify command arguments:
72# --payload payload input file
73# --source_image verify payload to the specified source image.
74# --target_image the target image to verify upon.
Tudor Brindusb432db82018-06-29 13:13:27 -070075#
76# Check command arguments:
77# Symmetrical with the verify command.
Alex Deymo98e691c2016-02-04 21:05:45 -080078
Jason Kusumabe998f42015-09-03 15:53:13 -070079
Alex Deymo61e1fa82016-01-19 15:16:34 -080080# Exit codes:
81EX_UNSUPPORTED_DELTA=100
82
Jason Kusumaf514c542015-11-05 18:43:45 -080083warn() {
84 echo "brillo_update_payload: warning: $*" >&2
85}
86
Gilad Arnold957ce122015-10-14 16:02:55 -070087die() {
88 echo "brillo_update_payload: error: $*" >&2
89 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070090}
91
Parveen Kumarb31e1ac2020-10-16 15:30:09 -070092# Loads shflags. We first look at the default install location; then our own
93# directory; finally the parent directory.
Gilad Arnold957ce122015-10-14 16:02:55 -070094load_shflags() {
95 local my_dir="$(dirname "$(readlink -f "$0")")"
96 local path
Parveen Kumarb31e1ac2020-10-16 15:30:09 -070097 for path in /usr/share/misc \
98 "${my_dir}"/lib/shflags \
99 "${my_dir}"/../lib/shflags; do
Gilad Arnold957ce122015-10-14 16:02:55 -0700100 if [[ -r "${path}/shflags" ]]; then
101 . "${path}/shflags" || die "Could not load ${path}/shflags."
102 return
103 fi
104 done
105 die "Could not find shflags."
106}
107
108load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -0700109
Alex Deymoc64ffd52015-09-25 18:10:07 -0700110HELP_GENERATE="generate: Generate an unsigned update payload."
111HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
112for signing."
113HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -0800114HELP_PROPERTIES="properties: Extract payload properties to a file."
Tudor Brindusb432db82018-06-29 13:13:27 -0700115HELP_VERIFY="verify: Verify a (signed) update payload using delta_generator."
116HELP_CHECK="check: Check a (signed) update payload using paycheck (static \
117testing)."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700118
119usage() {
120 echo "Supported commands:"
121 echo
122 echo "${HELP_GENERATE}"
123 echo "${HELP_HASH}"
124 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800125 echo "${HELP_PROPERTIES}"
Amin Hassani13520932017-07-26 11:26:05 -0700126 echo "${HELP_VERIFY}"
Tudor Brindusb432db82018-06-29 13:13:27 -0700127 echo "${HELP_CHECK}"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700128 echo
129 echo "Use: \"$0 <command> --help\" for more options."
130}
131
132# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700133if [[ $# -lt 1 ]]; then
Tudor Brindusb432db82018-06-29 13:13:27 -0700134 echo "Please specify a command [generate|hash|sign|properties|verify|check]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700135 exit 1
136fi
137
Alex Deymoc64ffd52015-09-25 18:10:07 -0700138# Parse command.
139COMMAND="${1:-}"
140shift
141
142case "${COMMAND}" in
143 generate)
144 FLAGS_HELP="${HELP_GENERATE}"
145 ;;
146
147 hash)
148 FLAGS_HELP="${HELP_HASH}"
149 ;;
150
151 sign)
152 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700153 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800154
155 properties)
156 FLAGS_HELP="${HELP_PROPERTIES}"
157 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700158
159 verify)
160 FLAGS_HELP="${HELP_VERIFY}"
161 ;;
162
Tudor Brindusb432db82018-06-29 13:13:27 -0700163 check)
164 FLAGS_HELP="${HELP_CHECK}"
165 ;;
166
Jason Kusumabe998f42015-09-03 15:53:13 -0700167 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700168 echo "Unrecognized command: \"${COMMAND}\"" >&2
169 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700170 exit 1
171 ;;
172esac
173
Jason Kusumabe998f42015-09-03 15:53:13 -0700174# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700175FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
176${FLAGS_HELP}"
177
178if [[ "${COMMAND}" == "generate" ]]; then
179 DEFINE_string payload "" \
180 "Path to output the generated unsigned payload file."
181 DEFINE_string target_image "" \
182 "Path to the target image that should be sent to clients."
183 DEFINE_string source_image "" \
184 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700185 DEFINE_string metadata_size_file "" \
186 "Optional: Path to output metadata size."
Sen Jiang8e768e92017-06-28 17:13:19 -0700187 DEFINE_string max_timestamp "" \
188 "Optional: The maximum unix timestamp of the OS allowed to apply this \
189payload, should be set to a number higher than the build timestamp of the \
190system running on the device, 0 if not specified."
Kelvin Zhang1f496422020-08-11 17:18:23 -0400191 DEFINE_string partition_timestamps "" \
192 "Optional: Per-partition maximum unix timestamp of the OS allowed to \
193apply this payload. Should be a comma separated key value pairs. e.x.\
194system:1234,vendor:456"
Tianjie Xu72d512c2019-08-21 15:20:35 -0700195 DEFINE_string disable_fec_computation "" \
196 "Optional: Disables the on device fec data computation for incremental \
197update. This feature is enabled by default."
Tianjief5baff42020-07-17 21:43:22 -0700198 DEFINE_string is_partial_update "" \
199 "Optional: True if the payload is for partial update. i.e. it only updates \
200a subset of partitions on device."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700201fi
202if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
203 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
204 DEFINE_string signature_size "" \
205 "Signature sizes in bytes in the following format: size1:size2[:...]"
206fi
207if [[ "${COMMAND}" == "hash" ]]; then
208 DEFINE_string metadata_hash_file "" \
209 "Optional: Path to output metadata hash file."
210 DEFINE_string payload_hash_file "" \
211 "Optional: Path to output payload hash file."
212fi
213if [[ "${COMMAND}" == "sign" ]]; then
214 DEFINE_string payload "" \
215 "Path to output the generated unsigned payload file."
216 DEFINE_string metadata_signature_file "" \
217 "The metatada signatures in the following format: \
218metadata_signature1:metadata_signature2[:...]"
219 DEFINE_string payload_signature_file "" \
220 "The payload signatures in the following format: \
221payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700222 DEFINE_string metadata_size_file "" \
223 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700224fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800225if [[ "${COMMAND}" == "properties" ]]; then
226 DEFINE_string payload "" \
227 "Path to the input signed or unsigned payload file."
228 DEFINE_string properties_file "-" \
229 "Path to output the extracted property files. If '-' is passed stdout will \
230be used."
231fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700232if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700233 DEFINE_string payload "" \
234 "Path to the input payload file."
235 DEFINE_string target_image "" \
236 "Path to the target image to verify upon."
237 DEFINE_string source_image "" \
238 "Optional: Path to a source image. If specified, the delta update is \
239applied to this."
240fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800241
Alex Deymo5fbb1102017-01-12 13:55:52 -0800242DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700243
244# Parse command line flag arguments
245FLAGS "$@" || exit 1
246eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700247set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700248
Alex Deymo5fbb1102017-01-12 13:55:52 -0800249# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
250# ${TMPDIR}.
251TMPDIR="${FLAGS_work_dir}"
252export TMPDIR
253
Alex Deymo89ff9e32015-09-15 19:29:01 -0700254# Associative arrays from partition name to file in the source and target
255# images. The size of the updated area must be the size of the file.
256declare -A SRC_PARTITIONS
257declare -A DST_PARTITIONS
258
Alex Deymo20bdc702016-12-07 21:07:11 -0800259# Associative arrays for the .map files associated with each src/dst partition
260# file in SRC_PARTITIONS and DST_PARTITIONS.
261declare -A SRC_PARTITIONS_MAP
262declare -A DST_PARTITIONS_MAP
263
Sen Jiang788c2d92016-03-09 12:48:40 -0800264# List of partition names in order.
265declare -a PARTITIONS_ORDER
266
Tao Bao96489902019-04-02 16:25:03 -0700267# A list of PIDs of the extract_image workers.
268EXTRACT_IMAGE_PIDS=()
269
Alex Deymo89ff9e32015-09-15 19:29:01 -0700270# A list of temporary files to remove during cleanup.
271CLEANUP_FILES=()
272
Alex Deymo48b502a2015-09-17 19:00:18 -0700273# Global options to force the version of the payload.
274FORCE_MAJOR_VERSION=""
275FORCE_MINOR_VERSION=""
276
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800277# Path to the postinstall config file in target image if exists.
278POSTINSTALL_CONFIG_FILE=""
279
Yifan Hong398cb542018-10-18 11:29:40 -0700280# Path to the dynamic partition info file in target image if exists.
281DYNAMIC_PARTITION_INFO_FILE=""
282
Alex Deymoc97df432015-09-25 17:23:52 -0700283# read_option_int <file.txt> <option_key> [default_value]
284#
285# Reads the unsigned integer value associated with |option_key| in a key=value
286# file |file.txt|. Prints the read value if found and valid, otherwise prints
287# the |default_value|.
288read_option_uint() {
289 local file_txt="$1"
290 local option_key="$2"
291 local default_value="${3:-}"
292 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700293 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700294 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
295 echo "${value}"
296 return
297 fi
298 fi
299 echo "${default_value}"
300}
301
Sen Jiangd0e9a892016-07-22 16:28:07 -0700302# truncate_file <file_path> <file_size>
303#
Dan Willemsen32711862018-10-04 21:25:50 -0700304# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700305# The truncate binary might not be available.
306truncate_file() {
307 local file_path="$1"
308 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700309 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700310}
311
Alex Deymo89ff9e32015-09-15 19:29:01 -0700312# Create a temporary file in the work_dir with an optional pattern name.
313# Prints the name of the newly created file.
314create_tempfile() {
315 local pattern="${1:-tempfile.XXXXXX}"
316 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
317}
Jason Kusumabe998f42015-09-03 15:53:13 -0700318
319cleanup() {
320 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700321 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700322
323 # If we are cleaning up after an error, or if we got an error during
324 # cleanup (even if we eventually succeeded) return a non-zero exit
325 # code. This triggers additional logging in most environments that call
326 # this script.
327 if [[ -n "${err}" ]]; then
328 die "Cleanup encountered an error."
329 fi
330}
331
332cleanup_on_error() {
333 trap - INT TERM ERR EXIT
334 cleanup
335 die "Cleanup success after an error."
336}
337
338cleanup_on_exit() {
339 trap - INT TERM ERR EXIT
340 cleanup
341}
342
343trap cleanup_on_error INT TERM ERR
344trap cleanup_on_exit EXIT
345
Tianjie Xu14715ce2019-08-06 17:24:43 -0700346# extract_file <zip_file> <entry_name> <destination>
347#
348# Extracts |entry_name| from |zip_file| to |destination|.
349extract_file() {
350 local zip_file="$1"
351 local entry_name="$2"
352 local destination="$3"
353
354 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
355 # as the destination of the unzip, and move the file to the intended
356 # destination.
357 local output_directory=$(
358 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
359 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
360 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
361
362 mv "${output_directory}/${entry_name}" "${destination}"
363 rm -rf "${output_directory}"
364}
Alex Deymo48b502a2015-09-17 19:00:18 -0700365
Sen Jiang788c2d92016-03-09 12:48:40 -0800366# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700367#
368# Detect the format of the |image| file and extract its updatable partitions
369# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800370# associative array passed in |partitions_array|. If |partitions_order| is
371# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700372extract_image() {
373 local image="$1"
374
375 # Brillo images are zip files. We detect the 4-byte magic header of the zip
376 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800377 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700378 if [[ "${magic}" == "504b0304" ]]; then
379 echo "Detected .zip file, extracting Brillo image."
380 extract_image_brillo "$@"
381 return
382 fi
383
384 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
385 # bundled here and we will use it to extract the partitions, so the GPT
386 # headers must be valid.
387 if cgpt show -q -n "${image}" >/dev/null; then
388 echo "Detected GPT image, extracting Chrome OS image."
389 extract_image_cros "$@"
390 return
391 fi
392
393 die "Couldn't detect the image format of ${image}"
394}
395
Sen Jiang788c2d92016-03-09 12:48:40 -0800396# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700397#
Alex Deymo48b502a2015-09-17 19:00:18 -0700398# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700399extract_image_cros() {
400 local image="$1"
401 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800402 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700403
404 local kernel root
405 kernel=$(create_tempfile "kernel.bin.XXXXXX")
406 CLEANUP_FILES+=("${kernel}")
407 root=$(create_tempfile "root.bin.XXXXXX")
408 CLEANUP_FILES+=("${root}")
409
410 cros_generate_update_payload --extract \
411 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700412 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700413
Amin Hassani58e01d62018-09-19 14:56:15 -0700414 # Chrome OS now uses major_version 2 payloads for all boards.
415 # See crbug.com/794404 for more information.
416 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700417
Tudor Brindusdda79e22018-06-28 18:03:21 -0700418 eval ${partitions_array}[kernel]=\""${kernel}"\"
419 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700420
Sen Jiang788c2d92016-03-09 12:48:40 -0800421 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700422 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800423 fi
424
Alex Deymo89ff9e32015-09-15 19:29:01 -0700425 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700426 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700427 varname="${partitions_array}[${part}]"
428 printf "md5sum of %s: " "${varname}"
429 md5sum "${!varname}"
430 done
431}
432
Sen Jiang3e5804d2018-09-06 15:53:00 -0700433# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
434# <part_file> <part_map_file>
435#
436# Extract the <partition> from target_files zip file into <part_file> and its
437# map file into <part_map_file>.
438extract_partition_brillo() {
439 local image="$1"
440 local partitions_array="$2"
441 local part="$3"
442 local part_file="$4"
443 local part_map_file="$5"
444
445 # For each partition, we in turn look for its image file under IMAGES/ and
446 # RADIO/ in the given target_files zip file.
447 local path path_in_zip
448 for path in IMAGES RADIO; do
449 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
450 path_in_zip="${path}"
451 break
452 fi
453 done
454 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700455 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700456
457 # If the partition is stored as an Android sparse image file, we need to
458 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800459 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700460 if [[ "${magic}" == "3aff26ed" ]]; then
461 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
462 echo "Converting Android sparse image ${part}.img to RAW."
463 mv "${part_file}" "${temp_sparse}"
464 simg2img "${temp_sparse}" "${part_file}"
465 rm -f "${temp_sparse}"
466 fi
467
468 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700469 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
470 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
471 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700472
473 # delta_generator only supports images multiple of 4 KiB. For target images
474 # we pad the data with zeros if needed, but for source images we truncate
475 # down the data since the last block of the old image could be padded on
476 # disk with unknown data.
477 local filesize=$(stat -c%s "${part_file}")
478 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
479 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
480 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
481 : $(( filesize = filesize & -4096 ))
482 else
483 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
484 : $(( filesize = (filesize + 4095) & -4096 ))
485 fi
486 truncate_file "${part_file}" "${filesize}"
487 fi
488
489 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
490}
491
Sen Jiang788c2d92016-03-09 12:48:40 -0800492# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700493#
494# Extract the A/B updated partitions from a Brillo target_files zip file into
495# new temporary files.
496extract_image_brillo() {
497 local image="$1"
498 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800499 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700500
Alex Deymo48b502a2015-09-17 19:00:18 -0700501 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800502 local ab_partitions_list
503 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
504 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700505 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
506 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800507 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
508 die "Invalid partition names found in the partition list."
509 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700510 # Get partition list without duplicates.
511 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800512 if [[ ${#partitions[@]} -eq 0 ]]; then
513 die "The list of partitions is empty. Can't generate a payload."
514 fi
515 else
516 warn "No ab_partitions.txt found. Using default."
517 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700518 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700519
Sen Jiang788c2d92016-03-09 12:48:40 -0800520 if [[ -n "${partitions_order}" ]]; then
521 eval "${partitions_order}=(${partitions[@]})"
522 fi
523
Alex Deymo83f2f702015-10-14 14:49:33 -0700524 # All Brillo updaters support major version 2.
525 FORCE_MAJOR_VERSION="2"
526
Alex Deymo48b502a2015-09-17 19:00:18 -0700527 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800528 # Source image
529 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700530 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700531 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
532 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
533 else
Alex Deymoc97df432015-09-25 17:23:52 -0700534 warn "No update_engine_config.txt found. Assuming pre-release image, \
535using payload minor version 2"
536 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700537 # For delta payloads, we use the major and minor version supported by the
538 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700539 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
540 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700541 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
542 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800543
544 # Brillo support for deltas started with minor version 3.
545 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
546 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
547Disabling deltas for this source version."
548 exit ${EX_UNSUPPORTED_DELTA}
549 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800550 else
551 # Target image
552 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
553 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700554 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
555 extract_file "${image}" "META/postinstall_config.txt" \
556 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800557 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
558 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700559 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
560 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700561 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
562 extract_file "${image}" "META/dynamic_partitions_info.txt" \
563 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700564 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
565 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700566 fi
567
Sen Jiang3e5804d2018-09-06 15:53:00 -0700568 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700569 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700570 local part_file=$(create_tempfile "${part}.img.XXXXXX")
571 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
572 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
573 # Extract partitions in background.
574 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
575 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700576 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700577 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800578 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700579 done
580}
581
582# cleanup_partition_array <partitions_array>
583#
584# Remove all empty files in <partitions_array>.
585cleanup_partition_array() {
586 local partitions_array="$1"
587 # Have to use eval to iterate over associative array keys with variable array
588 # names, we should change it to use nameref once bash 4.3 is available
589 # everywhere.
590 for part in $(eval "echo \${!${partitions_array}[@]}"); do
591 local path="${partitions_array}[$part]"
592 if [[ ! -s "${!path}" ]]; then
593 eval "unset ${partitions_array}[${part}]"
594 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700595 done
596}
597
Tudor Brindusb432db82018-06-29 13:13:27 -0700598extract_payload_images() {
599 local payload_type=$1
600 echo "Extracting images for ${payload_type} update."
601
602 if [[ "${payload_type}" == "delta" ]]; then
603 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
604 fi
605 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700606 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
607 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
608 # as it gives the status of the last process it has waited for.
609 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
610 wait ${pid}
611 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700612 cleanup_partition_array SRC_PARTITIONS
613 cleanup_partition_array SRC_PARTITIONS_MAP
614 cleanup_partition_array DST_PARTITIONS
615 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700616}
617
618get_payload_type() {
619 if [[ -z "${FLAGS_source_image}" ]]; then
620 echo "full"
621 else
622 echo "delta"
623 fi
624}
625
Jason Kusumabe998f42015-09-03 15:53:13 -0700626validate_generate() {
627 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700628 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700629
630 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700631 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700632}
633
634cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700635 local payload_type=$(get_payload_type)
636 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700637
Alex Deymo48b502a2015-09-17 19:00:18 -0700638 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800639 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700640 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800641
642 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800643 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800644 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800645 if [[ -n "${partition_names}" ]]; then
646 partition_names+=":"
647 new_partitions+=":"
648 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800649 new_mapfiles+=":"
650 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800651 fi
652 partition_names+="${part}"
653 new_partitions+="${DST_PARTITIONS[${part}]}"
654 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800655 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
656 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800657 done
658
659 # Target image args:
660 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700661 --partition_names="${partition_names}"
662 --new_partitions="${new_partitions}"
663 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700664 )
665
Tianjief5baff42020-07-17 21:43:22 -0700666 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
667 GENERATOR_ARGS+=( --is_partial_update="true" )
668 # Need at least minor version 7 for partial update, so generate with minor
669 # version 7 if we don't have a source image. Let the delta_generator to fail
670 # the other incompatiable minor versions.
671 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
672 FORCE_MINOR_VERSION="7"
673 fi
674 fi
675
Alex Deymo89ff9e32015-09-15 19:29:01 -0700676 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800677 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700678 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700679 --old_partitions="${old_partitions}"
680 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700681 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700682 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
683 GENERATOR_ARGS+=(
684 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
685 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700686 fi
687
Tianjief5baff42020-07-17 21:43:22 -0700688 # minor version is set only for delta or partial payload.
689 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
690 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
691 fi
692
Alex Deymo48b502a2015-09-17 19:00:18 -0700693 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
694 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700695 fi
696
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700697 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
698 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
699 fi
700
Sen Jiang8e768e92017-06-28 17:13:19 -0700701 if [[ -n "${FLAGS_max_timestamp}" ]]; then
702 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
703 fi
704
Kelvin Zhang1f496422020-08-11 17:18:23 -0400705 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
706 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
707 fi
708
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800709 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
710 GENERATOR_ARGS+=(
711 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
712 )
713 fi
714
Yifan Hong398cb542018-10-18 11:29:40 -0700715 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
716 GENERATOR_ARGS+=(
717 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
718 )
719 fi
720
Jason Kusumabe998f42015-09-03 15:53:13 -0700721 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700722 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700723
Alex Deymo89ff9e32015-09-15 19:29:01 -0700724 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700725}
726
727validate_hash() {
728 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700729 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700730
731 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700732 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700733--unsigned_payload FILENAME"
734
Jason Kusumabe998f42015-09-03 15:53:13 -0700735 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700736 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800737
738 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700739 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700740}
741
742cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700743 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700744 --in_file="${FLAGS_unsigned_payload}" \
745 --signature_size="${FLAGS_signature_size}" \
746 --out_hash_file="${FLAGS_payload_hash_file}" \
747 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700748
Jason Kusumabe998f42015-09-03 15:53:13 -0700749 echo "Done generating hash."
750}
751
752validate_sign() {
753 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700754 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700755
756 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700757 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700758--unsigned_payload FILENAME"
759
760 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700761 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700762
763 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700764 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700765--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700766
767 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700768 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700769--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700770}
771
772cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700773 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700774 --in_file="${FLAGS_unsigned_payload}"
775 --signature_size="${FLAGS_signature_size}"
776 --payload_signature_file="${FLAGS_payload_signature_file}"
777 --metadata_signature_file="${FLAGS_metadata_signature_file}"
778 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700779 )
780
781 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
782 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
783 fi
784
785 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700786 echo "Done signing payload."
787}
788
Alex Deymo98e691c2016-02-04 21:05:45 -0800789validate_properties() {
790 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700791 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800792
793 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700794 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800795}
796
797cmd_properties() {
798 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700799 --in_file="${FLAGS_payload}" \
800 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800801}
802
Tudor Brindusb432db82018-06-29 13:13:27 -0700803validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700804 [[ -n "${FLAGS_payload}" ]] ||
805 die "Error: you must specify an input filename with --payload FILENAME"
806
807 [[ -n "${FLAGS_target_image}" ]] ||
808 die "Error: you must specify a target image with --target_image FILENAME"
809}
810
811cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700812 local payload_type=$(get_payload_type)
813 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700814
815 declare -A TMP_PARTITIONS
816 for part in "${PARTITIONS_ORDER[@]}"; do
817 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
818 echo "Creating temporary target partition ${tmp_part} for ${part}"
819 CLEANUP_FILES+=("${tmp_part}")
820 TMP_PARTITIONS[${part}]=${tmp_part}
821 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
822 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
823 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
824 done
825
826 echo "Verifying ${payload_type} update."
827 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700828 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700829
830 local part old_partitions="" new_partitions="" partition_names=""
831 for part in "${PARTITIONS_ORDER[@]}"; do
832 if [[ -n "${partition_names}" ]]; then
833 partition_names+=":"
834 new_partitions+=":"
835 old_partitions+=":"
836 fi
837 partition_names+="${part}"
838 new_partitions+="${TMP_PARTITIONS[${part}]}"
839 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
840 done
841
842 # Target image args:
843 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700844 --partition_names="${partition_names}"
845 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700846 )
847
848 if [[ "${payload_type}" == "delta" ]]; then
849 # Source image args:
850 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700851 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700852 )
853 fi
854
Amin Hassania566cb62017-08-23 12:36:55 -0700855 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
856 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
857 fi
858
Amin Hassani13520932017-07-26 11:26:05 -0700859 echo "Running delta_generator to verify ${payload_type} payload with args: \
860${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700861 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700862
Sen Jiang6feb15c2018-08-31 15:45:17 -0700863 echo "Done applying ${payload_type} update."
864 echo "Checking the newly generated partitions against the target partitions"
865 local need_pause=false
866 for part in "${PARTITIONS_ORDER[@]}"; do
867 local not_str=""
868 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
869 not_str="in"
870 need_pause=true
871 fi
872 echo "The new partition (${part}) is ${not_str}valid."
873 done
874 # All images will be cleaned up when script exits, pause here to give a chance
875 # to inspect the images.
876 if [[ "$need_pause" == true ]]; then
877 read -n1 -r -s -p "Paused to investigate invalid partitions, \
878press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700879 fi
880}
881
Tudor Brindusb432db82018-06-29 13:13:27 -0700882cmd_check() {
883 local payload_type=$(get_payload_type)
884 extract_payload_images ${payload_type}
885
886 local part dst_partitions="" src_partitions=""
887 for part in "${PARTITIONS_ORDER[@]}"; do
888 if [[ -n "${dst_partitions}" ]]; then
889 dst_partitions+=" "
890 src_partitions+=" "
891 fi
892 dst_partitions+="${DST_PARTITIONS[${part}]}"
893 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
894 done
895
896 # Common payload args:
897 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
898 --part_names ${PARTITIONS_ORDER[@]} \
899 --dst_part_paths ${dst_partitions} )
900
901 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
902 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
903 fi
904
905 echo "Checking ${payload_type} update."
906 check_update_payload ${PAYCHECK_ARGS[@]} --check
907}
908
Tianjiee283ce42020-07-29 11:37:51 -0700909# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700910[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700911[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
912
913case "$COMMAND" in
914 generate) validate_generate
915 cmd_generate
916 ;;
917 hash) validate_hash
918 cmd_hash
919 ;;
920 sign) validate_sign
921 cmd_sign
922 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800923 properties) validate_properties
924 cmd_properties
925 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700926 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700927 cmd_verify
928 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700929 check) validate_verify_and_check
930 cmd_check
931 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700932esac