blob: f621479b080ae1543737161bdac913a53210b213 [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."
Kelvin Zhangdde2ef42020-11-20 12:26:19 -0500201 DEFINE_string full_boot "" "Will include full boot image"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700202fi
203if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
204 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
205 DEFINE_string signature_size "" \
206 "Signature sizes in bytes in the following format: size1:size2[:...]"
207fi
208if [[ "${COMMAND}" == "hash" ]]; then
209 DEFINE_string metadata_hash_file "" \
210 "Optional: Path to output metadata hash file."
211 DEFINE_string payload_hash_file "" \
212 "Optional: Path to output payload hash file."
213fi
214if [[ "${COMMAND}" == "sign" ]]; then
215 DEFINE_string payload "" \
216 "Path to output the generated unsigned payload file."
217 DEFINE_string metadata_signature_file "" \
218 "The metatada signatures in the following format: \
219metadata_signature1:metadata_signature2[:...]"
220 DEFINE_string payload_signature_file "" \
221 "The payload signatures in the following format: \
222payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700223 DEFINE_string metadata_size_file "" \
224 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700225fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800226if [[ "${COMMAND}" == "properties" ]]; then
227 DEFINE_string payload "" \
228 "Path to the input signed or unsigned payload file."
229 DEFINE_string properties_file "-" \
230 "Path to output the extracted property files. If '-' is passed stdout will \
231be used."
232fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700233if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700234 DEFINE_string payload "" \
235 "Path to the input payload file."
236 DEFINE_string target_image "" \
237 "Path to the target image to verify upon."
238 DEFINE_string source_image "" \
239 "Optional: Path to a source image. If specified, the delta update is \
240applied to this."
241fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800242
Alex Deymo5fbb1102017-01-12 13:55:52 -0800243DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700244
245# Parse command line flag arguments
246FLAGS "$@" || exit 1
247eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700248set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700249
Alex Deymo5fbb1102017-01-12 13:55:52 -0800250# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
251# ${TMPDIR}.
252TMPDIR="${FLAGS_work_dir}"
253export TMPDIR
254
Alex Deymo89ff9e32015-09-15 19:29:01 -0700255# Associative arrays from partition name to file in the source and target
256# images. The size of the updated area must be the size of the file.
257declare -A SRC_PARTITIONS
258declare -A DST_PARTITIONS
259
Alex Deymo20bdc702016-12-07 21:07:11 -0800260# Associative arrays for the .map files associated with each src/dst partition
261# file in SRC_PARTITIONS and DST_PARTITIONS.
262declare -A SRC_PARTITIONS_MAP
263declare -A DST_PARTITIONS_MAP
264
Sen Jiang788c2d92016-03-09 12:48:40 -0800265# List of partition names in order.
266declare -a PARTITIONS_ORDER
267
Tao Bao96489902019-04-02 16:25:03 -0700268# A list of PIDs of the extract_image workers.
269EXTRACT_IMAGE_PIDS=()
270
Alex Deymo89ff9e32015-09-15 19:29:01 -0700271# A list of temporary files to remove during cleanup.
272CLEANUP_FILES=()
273
Alex Deymo48b502a2015-09-17 19:00:18 -0700274# Global options to force the version of the payload.
275FORCE_MAJOR_VERSION=""
276FORCE_MINOR_VERSION=""
277
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800278# Path to the postinstall config file in target image if exists.
279POSTINSTALL_CONFIG_FILE=""
280
Yifan Hong398cb542018-10-18 11:29:40 -0700281# Path to the dynamic partition info file in target image if exists.
282DYNAMIC_PARTITION_INFO_FILE=""
283
Alex Deymoc97df432015-09-25 17:23:52 -0700284# read_option_int <file.txt> <option_key> [default_value]
285#
286# Reads the unsigned integer value associated with |option_key| in a key=value
287# file |file.txt|. Prints the read value if found and valid, otherwise prints
288# the |default_value|.
289read_option_uint() {
290 local file_txt="$1"
291 local option_key="$2"
292 local default_value="${3:-}"
293 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700294 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700295 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
296 echo "${value}"
297 return
298 fi
299 fi
300 echo "${default_value}"
301}
302
Sen Jiangd0e9a892016-07-22 16:28:07 -0700303# truncate_file <file_path> <file_size>
304#
Dan Willemsen32711862018-10-04 21:25:50 -0700305# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700306# The truncate binary might not be available.
307truncate_file() {
308 local file_path="$1"
309 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700310 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700311}
312
Alex Deymo89ff9e32015-09-15 19:29:01 -0700313# Create a temporary file in the work_dir with an optional pattern name.
314# Prints the name of the newly created file.
315create_tempfile() {
316 local pattern="${1:-tempfile.XXXXXX}"
317 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
318}
Jason Kusumabe998f42015-09-03 15:53:13 -0700319
320cleanup() {
321 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700322 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700323
324 # If we are cleaning up after an error, or if we got an error during
325 # cleanup (even if we eventually succeeded) return a non-zero exit
326 # code. This triggers additional logging in most environments that call
327 # this script.
328 if [[ -n "${err}" ]]; then
329 die "Cleanup encountered an error."
330 fi
331}
332
333cleanup_on_error() {
334 trap - INT TERM ERR EXIT
335 cleanup
336 die "Cleanup success after an error."
337}
338
339cleanup_on_exit() {
340 trap - INT TERM ERR EXIT
341 cleanup
342}
343
344trap cleanup_on_error INT TERM ERR
345trap cleanup_on_exit EXIT
346
Tianjie Xu14715ce2019-08-06 17:24:43 -0700347# extract_file <zip_file> <entry_name> <destination>
348#
349# Extracts |entry_name| from |zip_file| to |destination|.
350extract_file() {
351 local zip_file="$1"
352 local entry_name="$2"
353 local destination="$3"
354
355 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
356 # as the destination of the unzip, and move the file to the intended
357 # destination.
358 local output_directory=$(
359 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
360 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
361 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
362
363 mv "${output_directory}/${entry_name}" "${destination}"
364 rm -rf "${output_directory}"
365}
Alex Deymo48b502a2015-09-17 19:00:18 -0700366
Sen Jiang788c2d92016-03-09 12:48:40 -0800367# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700368#
369# Detect the format of the |image| file and extract its updatable partitions
370# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800371# associative array passed in |partitions_array|. If |partitions_order| is
372# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700373extract_image() {
374 local image="$1"
375
376 # Brillo images are zip files. We detect the 4-byte magic header of the zip
377 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800378 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700379 if [[ "${magic}" == "504b0304" ]]; then
380 echo "Detected .zip file, extracting Brillo image."
381 extract_image_brillo "$@"
382 return
383 fi
384
385 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
386 # bundled here and we will use it to extract the partitions, so the GPT
387 # headers must be valid.
388 if cgpt show -q -n "${image}" >/dev/null; then
389 echo "Detected GPT image, extracting Chrome OS image."
390 extract_image_cros "$@"
391 return
392 fi
393
394 die "Couldn't detect the image format of ${image}"
395}
396
Sen Jiang788c2d92016-03-09 12:48:40 -0800397# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700398#
Alex Deymo48b502a2015-09-17 19:00:18 -0700399# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700400extract_image_cros() {
401 local image="$1"
402 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800403 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700404
405 local kernel root
406 kernel=$(create_tempfile "kernel.bin.XXXXXX")
407 CLEANUP_FILES+=("${kernel}")
408 root=$(create_tempfile "root.bin.XXXXXX")
409 CLEANUP_FILES+=("${root}")
410
411 cros_generate_update_payload --extract \
412 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700413 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700414
Amin Hassani58e01d62018-09-19 14:56:15 -0700415 # Chrome OS now uses major_version 2 payloads for all boards.
416 # See crbug.com/794404 for more information.
417 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700418
Tudor Brindusdda79e22018-06-28 18:03:21 -0700419 eval ${partitions_array}[kernel]=\""${kernel}"\"
420 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700421
Sen Jiang788c2d92016-03-09 12:48:40 -0800422 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700423 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800424 fi
425
Alex Deymo89ff9e32015-09-15 19:29:01 -0700426 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700427 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700428 varname="${partitions_array}[${part}]"
429 printf "md5sum of %s: " "${varname}"
430 md5sum "${!varname}"
431 done
432}
433
Sen Jiang3e5804d2018-09-06 15:53:00 -0700434# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
435# <part_file> <part_map_file>
436#
437# Extract the <partition> from target_files zip file into <part_file> and its
438# map file into <part_map_file>.
439extract_partition_brillo() {
440 local image="$1"
441 local partitions_array="$2"
442 local part="$3"
443 local part_file="$4"
444 local part_map_file="$5"
445
446 # For each partition, we in turn look for its image file under IMAGES/ and
447 # RADIO/ in the given target_files zip file.
448 local path path_in_zip
449 for path in IMAGES RADIO; do
450 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
451 path_in_zip="${path}"
452 break
453 fi
454 done
455 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700456 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700457
458 # If the partition is stored as an Android sparse image file, we need to
459 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800460 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700461 if [[ "${magic}" == "3aff26ed" ]]; then
462 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
463 echo "Converting Android sparse image ${part}.img to RAW."
464 mv "${part_file}" "${temp_sparse}"
465 simg2img "${temp_sparse}" "${part_file}"
466 rm -f "${temp_sparse}"
467 fi
468
469 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700470 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
471 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
472 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700473
474 # delta_generator only supports images multiple of 4 KiB. For target images
475 # we pad the data with zeros if needed, but for source images we truncate
476 # down the data since the last block of the old image could be padded on
477 # disk with unknown data.
478 local filesize=$(stat -c%s "${part_file}")
479 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
480 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
481 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
482 : $(( filesize = filesize & -4096 ))
483 else
484 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
485 : $(( filesize = (filesize + 4095) & -4096 ))
486 fi
487 truncate_file "${part_file}" "${filesize}"
488 fi
489
490 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
491}
492
Sen Jiang788c2d92016-03-09 12:48:40 -0800493# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700494#
495# Extract the A/B updated partitions from a Brillo target_files zip file into
496# new temporary files.
497extract_image_brillo() {
498 local image="$1"
499 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800500 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700501
Alex Deymo48b502a2015-09-17 19:00:18 -0700502 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800503 local ab_partitions_list
504 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
505 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700506 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
507 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800508 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
509 die "Invalid partition names found in the partition list."
510 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700511 # Get partition list without duplicates.
512 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800513 if [[ ${#partitions[@]} -eq 0 ]]; then
514 die "The list of partitions is empty. Can't generate a payload."
515 fi
516 else
517 warn "No ab_partitions.txt found. Using default."
518 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700519 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700520
Sen Jiang788c2d92016-03-09 12:48:40 -0800521 if [[ -n "${partitions_order}" ]]; then
522 eval "${partitions_order}=(${partitions[@]})"
523 fi
524
Alex Deymo83f2f702015-10-14 14:49:33 -0700525 # All Brillo updaters support major version 2.
526 FORCE_MAJOR_VERSION="2"
527
Alex Deymo48b502a2015-09-17 19:00:18 -0700528 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800529 # Source image
530 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700531 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700532 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
533 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
534 else
Alex Deymoc97df432015-09-25 17:23:52 -0700535 warn "No update_engine_config.txt found. Assuming pre-release image, \
536using payload minor version 2"
537 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700538 # For delta payloads, we use the major and minor version supported by the
539 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700540 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
541 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700542 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
543 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800544
545 # Brillo support for deltas started with minor version 3.
546 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
547 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
548Disabling deltas for this source version."
549 exit ${EX_UNSUPPORTED_DELTA}
550 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800551 else
552 # Target image
553 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
554 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700555 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
556 extract_file "${image}" "META/postinstall_config.txt" \
557 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800558 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
559 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700560 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
561 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700562 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
563 extract_file "${image}" "META/dynamic_partitions_info.txt" \
564 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700565 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
566 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700567 fi
568
Sen Jiang3e5804d2018-09-06 15:53:00 -0700569 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700570 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700571 local part_file=$(create_tempfile "${part}.img.XXXXXX")
572 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
573 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
574 # Extract partitions in background.
575 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
576 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700577 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700578 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800579 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700580 done
581}
582
583# cleanup_partition_array <partitions_array>
584#
585# Remove all empty files in <partitions_array>.
586cleanup_partition_array() {
587 local partitions_array="$1"
588 # Have to use eval to iterate over associative array keys with variable array
589 # names, we should change it to use nameref once bash 4.3 is available
590 # everywhere.
591 for part in $(eval "echo \${!${partitions_array}[@]}"); do
592 local path="${partitions_array}[$part]"
593 if [[ ! -s "${!path}" ]]; then
594 eval "unset ${partitions_array}[${part}]"
595 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700596 done
597}
598
Tudor Brindusb432db82018-06-29 13:13:27 -0700599extract_payload_images() {
600 local payload_type=$1
601 echo "Extracting images for ${payload_type} update."
602
603 if [[ "${payload_type}" == "delta" ]]; then
604 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
605 fi
606 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700607 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
608 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
609 # as it gives the status of the last process it has waited for.
610 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
611 wait ${pid}
612 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700613 cleanup_partition_array SRC_PARTITIONS
614 cleanup_partition_array SRC_PARTITIONS_MAP
615 cleanup_partition_array DST_PARTITIONS
616 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700617}
618
619get_payload_type() {
620 if [[ -z "${FLAGS_source_image}" ]]; then
621 echo "full"
622 else
623 echo "delta"
624 fi
625}
626
Jason Kusumabe998f42015-09-03 15:53:13 -0700627validate_generate() {
628 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700629 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700630
631 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700632 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700633}
634
635cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700636 local payload_type=$(get_payload_type)
637 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700638
Alex Deymo48b502a2015-09-17 19:00:18 -0700639 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800640 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700641 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800642
643 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800644 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800645 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800646 if [[ -n "${partition_names}" ]]; then
647 partition_names+=":"
648 new_partitions+=":"
649 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800650 new_mapfiles+=":"
651 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800652 fi
653 partition_names+="${part}"
654 new_partitions+="${DST_PARTITIONS[${part}]}"
Kelvin Zhang999705e2020-11-03 10:07:09 -0500655 if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then
656 # Skip boot partition.
657 old_partitions+=""
658 else
659 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
660 fi
Alex Deymo20bdc702016-12-07 21:07:11 -0800661 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
662 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800663 done
664
665 # Target image args:
666 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700667 --partition_names="${partition_names}"
668 --new_partitions="${new_partitions}"
669 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700670 )
671
Tianjief5baff42020-07-17 21:43:22 -0700672 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
673 GENERATOR_ARGS+=( --is_partial_update="true" )
674 # Need at least minor version 7 for partial update, so generate with minor
675 # version 7 if we don't have a source image. Let the delta_generator to fail
676 # the other incompatiable minor versions.
677 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
678 FORCE_MINOR_VERSION="7"
679 fi
680 fi
681
Alex Deymo89ff9e32015-09-15 19:29:01 -0700682 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800683 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700684 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700685 --old_partitions="${old_partitions}"
686 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700687 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700688 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
689 GENERATOR_ARGS+=(
690 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
691 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700692 fi
693
Tianjief5baff42020-07-17 21:43:22 -0700694 # minor version is set only for delta or partial payload.
695 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
696 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
697 fi
698
Alex Deymo48b502a2015-09-17 19:00:18 -0700699 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
700 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700701 fi
702
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700703 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
704 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
705 fi
706
Sen Jiang8e768e92017-06-28 17:13:19 -0700707 if [[ -n "${FLAGS_max_timestamp}" ]]; then
708 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
709 fi
710
Kelvin Zhang1f496422020-08-11 17:18:23 -0400711 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
712 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
713 fi
714
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800715 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
716 GENERATOR_ARGS+=(
717 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
718 )
719 fi
720
Yifan Hong398cb542018-10-18 11:29:40 -0700721 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
722 GENERATOR_ARGS+=(
723 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
724 )
725 fi
726
Jason Kusumabe998f42015-09-03 15:53:13 -0700727 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700728 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700729
Alex Deymo89ff9e32015-09-15 19:29:01 -0700730 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700731}
732
733validate_hash() {
734 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700735 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700736
737 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700738 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700739--unsigned_payload FILENAME"
740
Jason Kusumabe998f42015-09-03 15:53:13 -0700741 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700742 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800743
744 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700745 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700746}
747
748cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700749 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700750 --in_file="${FLAGS_unsigned_payload}" \
751 --signature_size="${FLAGS_signature_size}" \
752 --out_hash_file="${FLAGS_payload_hash_file}" \
753 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700754
Jason Kusumabe998f42015-09-03 15:53:13 -0700755 echo "Done generating hash."
756}
757
758validate_sign() {
759 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700760 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700761
762 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700763 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700764--unsigned_payload FILENAME"
765
766 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700767 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700768
769 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700770 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700771--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700772
773 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700774 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700775--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700776}
777
778cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700779 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700780 --in_file="${FLAGS_unsigned_payload}"
781 --signature_size="${FLAGS_signature_size}"
782 --payload_signature_file="${FLAGS_payload_signature_file}"
783 --metadata_signature_file="${FLAGS_metadata_signature_file}"
784 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700785 )
786
787 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
788 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
789 fi
790
791 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700792 echo "Done signing payload."
793}
794
Alex Deymo98e691c2016-02-04 21:05:45 -0800795validate_properties() {
796 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700797 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800798
799 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700800 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800801}
802
803cmd_properties() {
804 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700805 --in_file="${FLAGS_payload}" \
806 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800807}
808
Tudor Brindusb432db82018-06-29 13:13:27 -0700809validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700810 [[ -n "${FLAGS_payload}" ]] ||
811 die "Error: you must specify an input filename with --payload FILENAME"
812
813 [[ -n "${FLAGS_target_image}" ]] ||
814 die "Error: you must specify a target image with --target_image FILENAME"
815}
816
817cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700818 local payload_type=$(get_payload_type)
819 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700820
821 declare -A TMP_PARTITIONS
822 for part in "${PARTITIONS_ORDER[@]}"; do
823 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
824 echo "Creating temporary target partition ${tmp_part} for ${part}"
825 CLEANUP_FILES+=("${tmp_part}")
826 TMP_PARTITIONS[${part}]=${tmp_part}
827 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
828 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
829 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
830 done
831
832 echo "Verifying ${payload_type} update."
833 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700834 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700835
836 local part old_partitions="" new_partitions="" partition_names=""
837 for part in "${PARTITIONS_ORDER[@]}"; do
838 if [[ -n "${partition_names}" ]]; then
839 partition_names+=":"
840 new_partitions+=":"
841 old_partitions+=":"
842 fi
843 partition_names+="${part}"
844 new_partitions+="${TMP_PARTITIONS[${part}]}"
845 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
846 done
847
848 # Target image args:
849 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700850 --partition_names="${partition_names}"
851 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700852 )
853
854 if [[ "${payload_type}" == "delta" ]]; then
855 # Source image args:
856 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700857 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700858 )
859 fi
860
Amin Hassania566cb62017-08-23 12:36:55 -0700861 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
862 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
863 fi
864
Amin Hassani13520932017-07-26 11:26:05 -0700865 echo "Running delta_generator to verify ${payload_type} payload with args: \
866${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700867 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700868
Sen Jiang6feb15c2018-08-31 15:45:17 -0700869 echo "Done applying ${payload_type} update."
870 echo "Checking the newly generated partitions against the target partitions"
871 local need_pause=false
872 for part in "${PARTITIONS_ORDER[@]}"; do
873 local not_str=""
874 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
875 not_str="in"
876 need_pause=true
877 fi
878 echo "The new partition (${part}) is ${not_str}valid."
879 done
880 # All images will be cleaned up when script exits, pause here to give a chance
881 # to inspect the images.
882 if [[ "$need_pause" == true ]]; then
883 read -n1 -r -s -p "Paused to investigate invalid partitions, \
884press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700885 fi
886}
887
Tudor Brindusb432db82018-06-29 13:13:27 -0700888cmd_check() {
889 local payload_type=$(get_payload_type)
890 extract_payload_images ${payload_type}
891
892 local part dst_partitions="" src_partitions=""
893 for part in "${PARTITIONS_ORDER[@]}"; do
894 if [[ -n "${dst_partitions}" ]]; then
895 dst_partitions+=" "
896 src_partitions+=" "
897 fi
898 dst_partitions+="${DST_PARTITIONS[${part}]}"
899 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
900 done
901
902 # Common payload args:
903 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
904 --part_names ${PARTITIONS_ORDER[@]} \
905 --dst_part_paths ${dst_partitions} )
906
907 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
908 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
909 fi
910
911 echo "Checking ${payload_type} update."
912 check_update_payload ${PAYCHECK_ARGS[@]} --check
913}
914
Tianjiee283ce42020-07-29 11:37:51 -0700915# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700916[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700917[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
918
919case "$COMMAND" in
920 generate) validate_generate
921 cmd_generate
922 ;;
923 hash) validate_hash
924 cmd_hash
925 ;;
926 sign) validate_sign
927 cmd_sign
928 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800929 properties) validate_properties
930 cmd_properties
931 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700932 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700933 cmd_verify
934 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700935 check) validate_verify_and_check
936 cmd_check
937 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700938esac