blob: 1e729bdf9e26a79d488b601de43c46fc6df50164 [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
Gilad Arnold957ce122015-10-14 16:02:55 -070092# Loads shflags. We first look at the default install location; then look for
Amin Hassani2a14d412018-05-31 13:01:09 -070093# crosutils (chroot); finally check our own directory.
Gilad Arnold957ce122015-10-14 16:02:55 -070094load_shflags() {
95 local my_dir="$(dirname "$(readlink -f "$0")")"
96 local path
Mike Frysinger26bb8652018-10-12 02:15:09 -040097 for path in /usr/share/misc "${my_dir}"/lib/shflags; do
Gilad Arnold957ce122015-10-14 16:02:55 -070098 if [[ -r "${path}/shflags" ]]; then
99 . "${path}/shflags" || die "Could not load ${path}/shflags."
100 return
101 fi
102 done
103 die "Could not find shflags."
104}
105
106load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -0700107
Alex Deymoc64ffd52015-09-25 18:10:07 -0700108HELP_GENERATE="generate: Generate an unsigned update payload."
109HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
110for signing."
111HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -0800112HELP_PROPERTIES="properties: Extract payload properties to a file."
Tudor Brindusb432db82018-06-29 13:13:27 -0700113HELP_VERIFY="verify: Verify a (signed) update payload using delta_generator."
114HELP_CHECK="check: Check a (signed) update payload using paycheck (static \
115testing)."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700116
117usage() {
118 echo "Supported commands:"
119 echo
120 echo "${HELP_GENERATE}"
121 echo "${HELP_HASH}"
122 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800123 echo "${HELP_PROPERTIES}"
Amin Hassani13520932017-07-26 11:26:05 -0700124 echo "${HELP_VERIFY}"
Tudor Brindusb432db82018-06-29 13:13:27 -0700125 echo "${HELP_CHECK}"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700126 echo
127 echo "Use: \"$0 <command> --help\" for more options."
128}
129
130# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700131if [[ $# -lt 1 ]]; then
Tudor Brindusb432db82018-06-29 13:13:27 -0700132 echo "Please specify a command [generate|hash|sign|properties|verify|check]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700133 exit 1
134fi
135
Alex Deymoc64ffd52015-09-25 18:10:07 -0700136# Parse command.
137COMMAND="${1:-}"
138shift
139
140case "${COMMAND}" in
141 generate)
142 FLAGS_HELP="${HELP_GENERATE}"
143 ;;
144
145 hash)
146 FLAGS_HELP="${HELP_HASH}"
147 ;;
148
149 sign)
150 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700151 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800152
153 properties)
154 FLAGS_HELP="${HELP_PROPERTIES}"
155 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700156
157 verify)
158 FLAGS_HELP="${HELP_VERIFY}"
159 ;;
160
Tudor Brindusb432db82018-06-29 13:13:27 -0700161 check)
162 FLAGS_HELP="${HELP_CHECK}"
163 ;;
164
Jason Kusumabe998f42015-09-03 15:53:13 -0700165 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700166 echo "Unrecognized command: \"${COMMAND}\"" >&2
167 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700168 exit 1
169 ;;
170esac
171
Jason Kusumabe998f42015-09-03 15:53:13 -0700172# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700173FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
174${FLAGS_HELP}"
175
176if [[ "${COMMAND}" == "generate" ]]; then
177 DEFINE_string payload "" \
178 "Path to output the generated unsigned payload file."
179 DEFINE_string target_image "" \
180 "Path to the target image that should be sent to clients."
181 DEFINE_string source_image "" \
182 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700183 DEFINE_string metadata_size_file "" \
184 "Optional: Path to output metadata size."
Sen Jiang8e768e92017-06-28 17:13:19 -0700185 DEFINE_string max_timestamp "" \
186 "Optional: The maximum unix timestamp of the OS allowed to apply this \
187payload, should be set to a number higher than the build timestamp of the \
188system running on the device, 0 if not specified."
Kelvin Zhang1f496422020-08-11 17:18:23 -0400189 DEFINE_string partition_timestamps "" \
190 "Optional: Per-partition maximum unix timestamp of the OS allowed to \
191apply this payload. Should be a comma separated key value pairs. e.x.\
192system:1234,vendor:456"
Tianjie Xu72d512c2019-08-21 15:20:35 -0700193 DEFINE_string disable_fec_computation "" \
194 "Optional: Disables the on device fec data computation for incremental \
195update. This feature is enabled by default."
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500196 DEFINE_string disable_verity_computation "" \
197 "Optional: Disables the on device verity computation for incremental \
198update. This feature is enabled by default."
Tianjief5baff42020-07-17 21:43:22 -0700199 DEFINE_string is_partial_update "" \
200 "Optional: True if the payload is for partial update. i.e. it only updates \
201a subset of partitions on device."
Kelvin Zhangdde2ef42020-11-20 12:26:19 -0500202 DEFINE_string full_boot "" "Will include full boot image"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700203fi
204if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
205 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
206 DEFINE_string signature_size "" \
207 "Signature sizes in bytes in the following format: size1:size2[:...]"
208fi
209if [[ "${COMMAND}" == "hash" ]]; then
210 DEFINE_string metadata_hash_file "" \
211 "Optional: Path to output metadata hash file."
212 DEFINE_string payload_hash_file "" \
213 "Optional: Path to output payload hash file."
214fi
215if [[ "${COMMAND}" == "sign" ]]; then
216 DEFINE_string payload "" \
217 "Path to output the generated unsigned payload file."
218 DEFINE_string metadata_signature_file "" \
219 "The metatada signatures in the following format: \
220metadata_signature1:metadata_signature2[:...]"
221 DEFINE_string payload_signature_file "" \
222 "The payload signatures in the following format: \
223payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700224 DEFINE_string metadata_size_file "" \
225 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700226fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800227if [[ "${COMMAND}" == "properties" ]]; then
228 DEFINE_string payload "" \
229 "Path to the input signed or unsigned payload file."
230 DEFINE_string properties_file "-" \
231 "Path to output the extracted property files. If '-' is passed stdout will \
232be used."
233fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700234if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700235 DEFINE_string payload "" \
236 "Path to the input payload file."
237 DEFINE_string target_image "" \
238 "Path to the target image to verify upon."
239 DEFINE_string source_image "" \
240 "Optional: Path to a source image. If specified, the delta update is \
241applied to this."
242fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800243
Alex Deymo5fbb1102017-01-12 13:55:52 -0800244DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700245
246# Parse command line flag arguments
247FLAGS "$@" || exit 1
248eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700249set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700250
Alex Deymo5fbb1102017-01-12 13:55:52 -0800251# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
252# ${TMPDIR}.
253TMPDIR="${FLAGS_work_dir}"
254export TMPDIR
255
Alex Deymo89ff9e32015-09-15 19:29:01 -0700256# Associative arrays from partition name to file in the source and target
257# images. The size of the updated area must be the size of the file.
258declare -A SRC_PARTITIONS
259declare -A DST_PARTITIONS
260
Alex Deymo20bdc702016-12-07 21:07:11 -0800261# Associative arrays for the .map files associated with each src/dst partition
262# file in SRC_PARTITIONS and DST_PARTITIONS.
263declare -A SRC_PARTITIONS_MAP
264declare -A DST_PARTITIONS_MAP
265
Sen Jiang788c2d92016-03-09 12:48:40 -0800266# List of partition names in order.
267declare -a PARTITIONS_ORDER
268
Tao Bao96489902019-04-02 16:25:03 -0700269# A list of PIDs of the extract_image workers.
270EXTRACT_IMAGE_PIDS=()
271
Alex Deymo89ff9e32015-09-15 19:29:01 -0700272# A list of temporary files to remove during cleanup.
273CLEANUP_FILES=()
274
Alex Deymo48b502a2015-09-17 19:00:18 -0700275# Global options to force the version of the payload.
276FORCE_MAJOR_VERSION=""
277FORCE_MINOR_VERSION=""
278
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800279# Path to the postinstall config file in target image if exists.
280POSTINSTALL_CONFIG_FILE=""
281
Yifan Hong398cb542018-10-18 11:29:40 -0700282# Path to the dynamic partition info file in target image if exists.
283DYNAMIC_PARTITION_INFO_FILE=""
284
Alex Deymoc97df432015-09-25 17:23:52 -0700285# read_option_int <file.txt> <option_key> [default_value]
286#
287# Reads the unsigned integer value associated with |option_key| in a key=value
288# file |file.txt|. Prints the read value if found and valid, otherwise prints
289# the |default_value|.
290read_option_uint() {
291 local file_txt="$1"
292 local option_key="$2"
293 local default_value="${3:-}"
294 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700295 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700296 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
297 echo "${value}"
298 return
299 fi
300 fi
301 echo "${default_value}"
302}
303
Sen Jiangd0e9a892016-07-22 16:28:07 -0700304# truncate_file <file_path> <file_size>
305#
Dan Willemsen32711862018-10-04 21:25:50 -0700306# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700307# The truncate binary might not be available.
308truncate_file() {
309 local file_path="$1"
310 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700311 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700312}
313
Alex Deymo89ff9e32015-09-15 19:29:01 -0700314# Create a temporary file in the work_dir with an optional pattern name.
315# Prints the name of the newly created file.
316create_tempfile() {
317 local pattern="${1:-tempfile.XXXXXX}"
318 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
319}
Jason Kusumabe998f42015-09-03 15:53:13 -0700320
321cleanup() {
322 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700323 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700324
325 # If we are cleaning up after an error, or if we got an error during
326 # cleanup (even if we eventually succeeded) return a non-zero exit
327 # code. This triggers additional logging in most environments that call
328 # this script.
329 if [[ -n "${err}" ]]; then
330 die "Cleanup encountered an error."
331 fi
332}
333
334cleanup_on_error() {
335 trap - INT TERM ERR EXIT
336 cleanup
337 die "Cleanup success after an error."
338}
339
340cleanup_on_exit() {
341 trap - INT TERM ERR EXIT
342 cleanup
343}
344
345trap cleanup_on_error INT TERM ERR
346trap cleanup_on_exit EXIT
347
Tianjie Xu14715ce2019-08-06 17:24:43 -0700348# extract_file <zip_file> <entry_name> <destination>
349#
350# Extracts |entry_name| from |zip_file| to |destination|.
351extract_file() {
352 local zip_file="$1"
353 local entry_name="$2"
354 local destination="$3"
355
356 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
357 # as the destination of the unzip, and move the file to the intended
358 # destination.
359 local output_directory=$(
360 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
361 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
362 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
363
364 mv "${output_directory}/${entry_name}" "${destination}"
365 rm -rf "${output_directory}"
366}
Alex Deymo48b502a2015-09-17 19:00:18 -0700367
Sen Jiang788c2d92016-03-09 12:48:40 -0800368# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700369#
370# Detect the format of the |image| file and extract its updatable partitions
371# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800372# associative array passed in |partitions_array|. If |partitions_order| is
373# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700374extract_image() {
375 local image="$1"
376
377 # Brillo images are zip files. We detect the 4-byte magic header of the zip
378 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800379 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700380 if [[ "${magic}" == "504b0304" ]]; then
381 echo "Detected .zip file, extracting Brillo image."
382 extract_image_brillo "$@"
383 return
384 fi
385
386 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
387 # bundled here and we will use it to extract the partitions, so the GPT
388 # headers must be valid.
389 if cgpt show -q -n "${image}" >/dev/null; then
390 echo "Detected GPT image, extracting Chrome OS image."
391 extract_image_cros "$@"
392 return
393 fi
394
395 die "Couldn't detect the image format of ${image}"
396}
397
Sen Jiang788c2d92016-03-09 12:48:40 -0800398# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700399#
Alex Deymo48b502a2015-09-17 19:00:18 -0700400# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700401extract_image_cros() {
402 local image="$1"
403 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800404 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700405
406 local kernel root
407 kernel=$(create_tempfile "kernel.bin.XXXXXX")
408 CLEANUP_FILES+=("${kernel}")
409 root=$(create_tempfile "root.bin.XXXXXX")
410 CLEANUP_FILES+=("${root}")
411
412 cros_generate_update_payload --extract \
413 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700414 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700415
Amin Hassani58e01d62018-09-19 14:56:15 -0700416 # Chrome OS now uses major_version 2 payloads for all boards.
417 # See crbug.com/794404 for more information.
418 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700419
Tudor Brindusdda79e22018-06-28 18:03:21 -0700420 eval ${partitions_array}[kernel]=\""${kernel}"\"
421 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700422
Sen Jiang788c2d92016-03-09 12:48:40 -0800423 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700424 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800425 fi
426
Alex Deymo89ff9e32015-09-15 19:29:01 -0700427 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700428 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700429 varname="${partitions_array}[${part}]"
430 printf "md5sum of %s: " "${varname}"
431 md5sum "${!varname}"
432 done
433}
434
Sen Jiang3e5804d2018-09-06 15:53:00 -0700435# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
436# <part_file> <part_map_file>
437#
438# Extract the <partition> from target_files zip file into <part_file> and its
439# map file into <part_map_file>.
440extract_partition_brillo() {
441 local image="$1"
442 local partitions_array="$2"
443 local part="$3"
444 local part_file="$4"
445 local part_map_file="$5"
446
447 # For each partition, we in turn look for its image file under IMAGES/ and
448 # RADIO/ in the given target_files zip file.
449 local path path_in_zip
450 for path in IMAGES RADIO; do
451 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
452 path_in_zip="${path}"
453 break
454 fi
455 done
456 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700457 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700458
459 # If the partition is stored as an Android sparse image file, we need to
460 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800461 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700462 if [[ "${magic}" == "3aff26ed" ]]; then
463 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
464 echo "Converting Android sparse image ${part}.img to RAW."
465 mv "${part_file}" "${temp_sparse}"
466 simg2img "${temp_sparse}" "${part_file}"
467 rm -f "${temp_sparse}"
468 fi
469
470 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700471 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
472 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
473 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700474
475 # delta_generator only supports images multiple of 4 KiB. For target images
476 # we pad the data with zeros if needed, but for source images we truncate
477 # down the data since the last block of the old image could be padded on
478 # disk with unknown data.
479 local filesize=$(stat -c%s "${part_file}")
480 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
481 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
482 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
483 : $(( filesize = filesize & -4096 ))
484 else
485 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
486 : $(( filesize = (filesize + 4095) & -4096 ))
487 fi
488 truncate_file "${part_file}" "${filesize}"
489 fi
490
491 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
492}
493
Sen Jiang788c2d92016-03-09 12:48:40 -0800494# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700495#
496# Extract the A/B updated partitions from a Brillo target_files zip file into
497# new temporary files.
498extract_image_brillo() {
499 local image="$1"
500 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800501 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700502
Alex Deymo48b502a2015-09-17 19:00:18 -0700503 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800504 local ab_partitions_list
505 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
506 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700507 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
508 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800509 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
510 die "Invalid partition names found in the partition list."
511 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700512 # Get partition list without duplicates.
513 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800514 if [[ ${#partitions[@]} -eq 0 ]]; then
515 die "The list of partitions is empty. Can't generate a payload."
516 fi
517 else
518 warn "No ab_partitions.txt found. Using default."
519 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700520 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700521
Sen Jiang788c2d92016-03-09 12:48:40 -0800522 if [[ -n "${partitions_order}" ]]; then
523 eval "${partitions_order}=(${partitions[@]})"
524 fi
525
Alex Deymo83f2f702015-10-14 14:49:33 -0700526 # All Brillo updaters support major version 2.
527 FORCE_MAJOR_VERSION="2"
528
Alex Deymo48b502a2015-09-17 19:00:18 -0700529 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800530 # Source image
531 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700532 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700533 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
534 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
535 else
Alex Deymoc97df432015-09-25 17:23:52 -0700536 warn "No update_engine_config.txt found. Assuming pre-release image, \
537using payload minor version 2"
538 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700539 # For delta payloads, we use the major and minor version supported by the
540 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700541 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
542 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700543 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
544 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800545
546 # Brillo support for deltas started with minor version 3.
547 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
548 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
549Disabling deltas for this source version."
550 exit ${EX_UNSUPPORTED_DELTA}
551 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800552 else
553 # Target image
554 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
555 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700556 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
557 extract_file "${image}" "META/postinstall_config.txt" \
558 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800559 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
560 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700561 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
562 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700563 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
564 extract_file "${image}" "META/dynamic_partitions_info.txt" \
565 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700566 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
567 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700568 fi
569
Sen Jiang3e5804d2018-09-06 15:53:00 -0700570 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700571 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700572 local part_file=$(create_tempfile "${part}.img.XXXXXX")
573 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
574 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
575 # Extract partitions in background.
576 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
577 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700578 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700579 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800580 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700581 done
582}
583
584# cleanup_partition_array <partitions_array>
585#
586# Remove all empty files in <partitions_array>.
587cleanup_partition_array() {
588 local partitions_array="$1"
589 # Have to use eval to iterate over associative array keys with variable array
590 # names, we should change it to use nameref once bash 4.3 is available
591 # everywhere.
592 for part in $(eval "echo \${!${partitions_array}[@]}"); do
593 local path="${partitions_array}[$part]"
594 if [[ ! -s "${!path}" ]]; then
595 eval "unset ${partitions_array}[${part}]"
596 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700597 done
598}
599
Tudor Brindusb432db82018-06-29 13:13:27 -0700600extract_payload_images() {
601 local payload_type=$1
602 echo "Extracting images for ${payload_type} update."
603
604 if [[ "${payload_type}" == "delta" ]]; then
605 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
606 fi
607 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700608 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
609 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
610 # as it gives the status of the last process it has waited for.
611 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
612 wait ${pid}
613 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700614 cleanup_partition_array SRC_PARTITIONS
615 cleanup_partition_array SRC_PARTITIONS_MAP
616 cleanup_partition_array DST_PARTITIONS
617 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700618}
619
620get_payload_type() {
621 if [[ -z "${FLAGS_source_image}" ]]; then
622 echo "full"
623 else
624 echo "delta"
625 fi
626}
627
Jason Kusumabe998f42015-09-03 15:53:13 -0700628validate_generate() {
629 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700630 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700631
632 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700633 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700634}
635
636cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700637 local payload_type=$(get_payload_type)
638 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700639
Alex Deymo48b502a2015-09-17 19:00:18 -0700640 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800641 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700642 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800643
644 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800645 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800646 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800647 if [[ -n "${partition_names}" ]]; then
648 partition_names+=":"
649 new_partitions+=":"
650 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800651 new_mapfiles+=":"
652 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800653 fi
654 partition_names+="${part}"
655 new_partitions+="${DST_PARTITIONS[${part}]}"
Kelvin Zhang999705e2020-11-03 10:07:09 -0500656 if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then
657 # Skip boot partition.
658 old_partitions+=""
659 else
660 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
661 fi
Alex Deymo20bdc702016-12-07 21:07:11 -0800662 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
663 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800664 done
665
666 # Target image args:
667 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700668 --partition_names="${partition_names}"
669 --new_partitions="${new_partitions}"
670 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700671 )
672
Tianjief5baff42020-07-17 21:43:22 -0700673 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
674 GENERATOR_ARGS+=( --is_partial_update="true" )
675 # Need at least minor version 7 for partial update, so generate with minor
676 # version 7 if we don't have a source image. Let the delta_generator to fail
677 # the other incompatiable minor versions.
678 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
679 FORCE_MINOR_VERSION="7"
680 fi
681 fi
682
Alex Deymo89ff9e32015-09-15 19:29:01 -0700683 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800684 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700685 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700686 --old_partitions="${old_partitions}"
687 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700688 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700689 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
690 GENERATOR_ARGS+=(
691 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
692 fi
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500693 if [[ -n "${FLAGS_disable_verity_computation}" ]]; then
694 GENERATOR_ARGS+=(
695 --disable_verity_computation="${FLAGS_disable_verity_computation}" )
696 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700697 fi
698
Tianjief5baff42020-07-17 21:43:22 -0700699 # minor version is set only for delta or partial payload.
700 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
701 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
702 fi
703
Alex Deymo48b502a2015-09-17 19:00:18 -0700704 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
705 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700706 fi
707
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700708 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
709 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
710 fi
711
Sen Jiang8e768e92017-06-28 17:13:19 -0700712 if [[ -n "${FLAGS_max_timestamp}" ]]; then
713 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
714 fi
715
Kelvin Zhang1f496422020-08-11 17:18:23 -0400716 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
717 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
718 fi
719
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800720 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
721 GENERATOR_ARGS+=(
722 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
723 )
724 fi
725
Yifan Hong398cb542018-10-18 11:29:40 -0700726 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
727 GENERATOR_ARGS+=(
728 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
729 )
730 fi
731
Jason Kusumabe998f42015-09-03 15:53:13 -0700732 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700733 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700734
Alex Deymo89ff9e32015-09-15 19:29:01 -0700735 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700736}
737
738validate_hash() {
739 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700740 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700741
742 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700743 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700744--unsigned_payload FILENAME"
745
Jason Kusumabe998f42015-09-03 15:53:13 -0700746 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700747 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800748
749 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700750 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700751}
752
753cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700754 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700755 --in_file="${FLAGS_unsigned_payload}" \
756 --signature_size="${FLAGS_signature_size}" \
757 --out_hash_file="${FLAGS_payload_hash_file}" \
758 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700759
Jason Kusumabe998f42015-09-03 15:53:13 -0700760 echo "Done generating hash."
761}
762
763validate_sign() {
764 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700765 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700766
767 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700768 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700769--unsigned_payload FILENAME"
770
771 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700772 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700773
774 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700775 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700776--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700777
778 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700779 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700780--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700781}
782
783cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700784 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700785 --in_file="${FLAGS_unsigned_payload}"
786 --signature_size="${FLAGS_signature_size}"
787 --payload_signature_file="${FLAGS_payload_signature_file}"
788 --metadata_signature_file="${FLAGS_metadata_signature_file}"
789 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700790 )
791
792 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
793 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
794 fi
795
796 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700797 echo "Done signing payload."
798}
799
Alex Deymo98e691c2016-02-04 21:05:45 -0800800validate_properties() {
801 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700802 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800803
804 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700805 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800806}
807
808cmd_properties() {
809 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700810 --in_file="${FLAGS_payload}" \
811 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800812}
813
Tudor Brindusb432db82018-06-29 13:13:27 -0700814validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700815 [[ -n "${FLAGS_payload}" ]] ||
816 die "Error: you must specify an input filename with --payload FILENAME"
817
818 [[ -n "${FLAGS_target_image}" ]] ||
819 die "Error: you must specify a target image with --target_image FILENAME"
820}
821
822cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700823 local payload_type=$(get_payload_type)
824 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700825
826 declare -A TMP_PARTITIONS
827 for part in "${PARTITIONS_ORDER[@]}"; do
828 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
829 echo "Creating temporary target partition ${tmp_part} for ${part}"
830 CLEANUP_FILES+=("${tmp_part}")
831 TMP_PARTITIONS[${part}]=${tmp_part}
832 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
833 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
834 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
835 done
836
837 echo "Verifying ${payload_type} update."
838 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700839 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700840
841 local part old_partitions="" new_partitions="" partition_names=""
842 for part in "${PARTITIONS_ORDER[@]}"; do
843 if [[ -n "${partition_names}" ]]; then
844 partition_names+=":"
845 new_partitions+=":"
846 old_partitions+=":"
847 fi
848 partition_names+="${part}"
849 new_partitions+="${TMP_PARTITIONS[${part}]}"
850 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
851 done
852
853 # Target image args:
854 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700855 --partition_names="${partition_names}"
856 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700857 )
858
859 if [[ "${payload_type}" == "delta" ]]; then
860 # Source image args:
861 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700862 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700863 )
864 fi
865
Amin Hassania566cb62017-08-23 12:36:55 -0700866 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
867 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
868 fi
869
Amin Hassani13520932017-07-26 11:26:05 -0700870 echo "Running delta_generator to verify ${payload_type} payload with args: \
871${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700872 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700873
Sen Jiang6feb15c2018-08-31 15:45:17 -0700874 echo "Done applying ${payload_type} update."
875 echo "Checking the newly generated partitions against the target partitions"
876 local need_pause=false
877 for part in "${PARTITIONS_ORDER[@]}"; do
878 local not_str=""
879 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
880 not_str="in"
881 need_pause=true
882 fi
883 echo "The new partition (${part}) is ${not_str}valid."
884 done
885 # All images will be cleaned up when script exits, pause here to give a chance
886 # to inspect the images.
887 if [[ "$need_pause" == true ]]; then
888 read -n1 -r -s -p "Paused to investigate invalid partitions, \
889press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700890 fi
891}
892
Tudor Brindusb432db82018-06-29 13:13:27 -0700893cmd_check() {
894 local payload_type=$(get_payload_type)
895 extract_payload_images ${payload_type}
896
897 local part dst_partitions="" src_partitions=""
898 for part in "${PARTITIONS_ORDER[@]}"; do
899 if [[ -n "${dst_partitions}" ]]; then
900 dst_partitions+=" "
901 src_partitions+=" "
902 fi
903 dst_partitions+="${DST_PARTITIONS[${part}]}"
904 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
905 done
906
907 # Common payload args:
908 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
909 --part_names ${PARTITIONS_ORDER[@]} \
910 --dst_part_paths ${dst_partitions} )
911
912 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
913 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
914 fi
915
916 echo "Checking ${payload_type} update."
917 check_update_payload ${PAYCHECK_ARGS[@]} --check
918}
919
Tianjiee283ce42020-07-29 11:37:51 -0700920# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700921[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700922[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
923
924case "$COMMAND" in
925 generate) validate_generate
926 cmd_generate
927 ;;
928 hash) validate_hash
929 cmd_hash
930 ;;
931 sign) validate_sign
932 cmd_sign
933 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800934 properties) validate_properties
935 cmd_properties
936 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700937 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700938 cmd_verify
939 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700940 check) validate_verify_and_check
941 cmd_check
942 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700943esac