blob: d1a17816b371bf191b221c43ede16ac4a8a485f3 [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"
Kelvin Zhang9101ff32021-01-19 15:48:53 -0500203 DEFINE_string disable_vabc "" \
204 "Optional: Disables Virtual AB Compression when installing the OTA"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700205fi
206if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
207 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
208 DEFINE_string signature_size "" \
209 "Signature sizes in bytes in the following format: size1:size2[:...]"
210fi
211if [[ "${COMMAND}" == "hash" ]]; then
212 DEFINE_string metadata_hash_file "" \
213 "Optional: Path to output metadata hash file."
214 DEFINE_string payload_hash_file "" \
215 "Optional: Path to output payload hash file."
216fi
217if [[ "${COMMAND}" == "sign" ]]; then
218 DEFINE_string payload "" \
219 "Path to output the generated unsigned payload file."
220 DEFINE_string metadata_signature_file "" \
221 "The metatada signatures in the following format: \
222metadata_signature1:metadata_signature2[:...]"
223 DEFINE_string payload_signature_file "" \
224 "The payload signatures in the following format: \
225payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700226 DEFINE_string metadata_size_file "" \
227 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700228fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800229if [[ "${COMMAND}" == "properties" ]]; then
230 DEFINE_string payload "" \
231 "Path to the input signed or unsigned payload file."
232 DEFINE_string properties_file "-" \
233 "Path to output the extracted property files. If '-' is passed stdout will \
234be used."
235fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700236if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700237 DEFINE_string payload "" \
238 "Path to the input payload file."
239 DEFINE_string target_image "" \
240 "Path to the target image to verify upon."
241 DEFINE_string source_image "" \
242 "Optional: Path to a source image. If specified, the delta update is \
243applied to this."
244fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800245
Alex Deymo5fbb1102017-01-12 13:55:52 -0800246DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700247
248# Parse command line flag arguments
249FLAGS "$@" || exit 1
250eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700251set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700252
Alex Deymo5fbb1102017-01-12 13:55:52 -0800253# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
254# ${TMPDIR}.
255TMPDIR="${FLAGS_work_dir}"
256export TMPDIR
257
Alex Deymo89ff9e32015-09-15 19:29:01 -0700258# Associative arrays from partition name to file in the source and target
259# images. The size of the updated area must be the size of the file.
260declare -A SRC_PARTITIONS
261declare -A DST_PARTITIONS
262
Alex Deymo20bdc702016-12-07 21:07:11 -0800263# Associative arrays for the .map files associated with each src/dst partition
264# file in SRC_PARTITIONS and DST_PARTITIONS.
265declare -A SRC_PARTITIONS_MAP
266declare -A DST_PARTITIONS_MAP
267
Sen Jiang788c2d92016-03-09 12:48:40 -0800268# List of partition names in order.
269declare -a PARTITIONS_ORDER
270
Tao Bao96489902019-04-02 16:25:03 -0700271# A list of PIDs of the extract_image workers.
272EXTRACT_IMAGE_PIDS=()
273
Alex Deymo89ff9e32015-09-15 19:29:01 -0700274# A list of temporary files to remove during cleanup.
275CLEANUP_FILES=()
276
Alex Deymo48b502a2015-09-17 19:00:18 -0700277# Global options to force the version of the payload.
278FORCE_MAJOR_VERSION=""
279FORCE_MINOR_VERSION=""
280
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800281# Path to the postinstall config file in target image if exists.
282POSTINSTALL_CONFIG_FILE=""
283
Yifan Hong398cb542018-10-18 11:29:40 -0700284# Path to the dynamic partition info file in target image if exists.
285DYNAMIC_PARTITION_INFO_FILE=""
286
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500287# Path to the META/apex_info.pb found in target build
288APEX_INFO_FILE=""
289
Alex Deymoc97df432015-09-25 17:23:52 -0700290# read_option_int <file.txt> <option_key> [default_value]
291#
292# Reads the unsigned integer value associated with |option_key| in a key=value
293# file |file.txt|. Prints the read value if found and valid, otherwise prints
294# the |default_value|.
295read_option_uint() {
296 local file_txt="$1"
297 local option_key="$2"
298 local default_value="${3:-}"
299 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700300 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700301 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
302 echo "${value}"
303 return
304 fi
305 fi
306 echo "${default_value}"
307}
308
Sen Jiangd0e9a892016-07-22 16:28:07 -0700309# truncate_file <file_path> <file_size>
310#
Dan Willemsen32711862018-10-04 21:25:50 -0700311# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700312# The truncate binary might not be available.
313truncate_file() {
314 local file_path="$1"
315 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700316 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700317}
318
Alex Deymo89ff9e32015-09-15 19:29:01 -0700319# Create a temporary file in the work_dir with an optional pattern name.
320# Prints the name of the newly created file.
321create_tempfile() {
322 local pattern="${1:-tempfile.XXXXXX}"
323 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
324}
Jason Kusumabe998f42015-09-03 15:53:13 -0700325
326cleanup() {
327 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700328 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700329
330 # If we are cleaning up after an error, or if we got an error during
331 # cleanup (even if we eventually succeeded) return a non-zero exit
332 # code. This triggers additional logging in most environments that call
333 # this script.
334 if [[ -n "${err}" ]]; then
335 die "Cleanup encountered an error."
336 fi
337}
338
339cleanup_on_error() {
340 trap - INT TERM ERR EXIT
341 cleanup
342 die "Cleanup success after an error."
343}
344
345cleanup_on_exit() {
346 trap - INT TERM ERR EXIT
347 cleanup
348}
349
350trap cleanup_on_error INT TERM ERR
351trap cleanup_on_exit EXIT
352
Tianjie Xu14715ce2019-08-06 17:24:43 -0700353# extract_file <zip_file> <entry_name> <destination>
354#
355# Extracts |entry_name| from |zip_file| to |destination|.
356extract_file() {
357 local zip_file="$1"
358 local entry_name="$2"
359 local destination="$3"
360
361 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
362 # as the destination of the unzip, and move the file to the intended
363 # destination.
364 local output_directory=$(
365 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
366 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
367 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
368
369 mv "${output_directory}/${entry_name}" "${destination}"
370 rm -rf "${output_directory}"
371}
Alex Deymo48b502a2015-09-17 19:00:18 -0700372
Sen Jiang788c2d92016-03-09 12:48:40 -0800373# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700374#
375# Detect the format of the |image| file and extract its updatable partitions
376# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800377# associative array passed in |partitions_array|. If |partitions_order| is
378# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700379extract_image() {
380 local image="$1"
381
382 # Brillo images are zip files. We detect the 4-byte magic header of the zip
383 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800384 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700385 if [[ "${magic}" == "504b0304" ]]; then
386 echo "Detected .zip file, extracting Brillo image."
387 extract_image_brillo "$@"
388 return
389 fi
390
391 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
392 # bundled here and we will use it to extract the partitions, so the GPT
393 # headers must be valid.
394 if cgpt show -q -n "${image}" >/dev/null; then
395 echo "Detected GPT image, extracting Chrome OS image."
396 extract_image_cros "$@"
397 return
398 fi
399
400 die "Couldn't detect the image format of ${image}"
401}
402
Sen Jiang788c2d92016-03-09 12:48:40 -0800403# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700404#
Alex Deymo48b502a2015-09-17 19:00:18 -0700405# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700406extract_image_cros() {
407 local image="$1"
408 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800409 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700410
411 local kernel root
412 kernel=$(create_tempfile "kernel.bin.XXXXXX")
413 CLEANUP_FILES+=("${kernel}")
414 root=$(create_tempfile "root.bin.XXXXXX")
415 CLEANUP_FILES+=("${root}")
416
417 cros_generate_update_payload --extract \
418 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700419 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700420
Amin Hassani58e01d62018-09-19 14:56:15 -0700421 # Chrome OS now uses major_version 2 payloads for all boards.
422 # See crbug.com/794404 for more information.
423 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700424
Tudor Brindusdda79e22018-06-28 18:03:21 -0700425 eval ${partitions_array}[kernel]=\""${kernel}"\"
426 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700427
Sen Jiang788c2d92016-03-09 12:48:40 -0800428 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700429 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800430 fi
431
Alex Deymo89ff9e32015-09-15 19:29:01 -0700432 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700433 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700434 varname="${partitions_array}[${part}]"
435 printf "md5sum of %s: " "${varname}"
436 md5sum "${!varname}"
437 done
438}
439
Sen Jiang3e5804d2018-09-06 15:53:00 -0700440# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
441# <part_file> <part_map_file>
442#
443# Extract the <partition> from target_files zip file into <part_file> and its
444# map file into <part_map_file>.
445extract_partition_brillo() {
446 local image="$1"
447 local partitions_array="$2"
448 local part="$3"
449 local part_file="$4"
450 local part_map_file="$5"
451
452 # For each partition, we in turn look for its image file under IMAGES/ and
453 # RADIO/ in the given target_files zip file.
454 local path path_in_zip
455 for path in IMAGES RADIO; do
456 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
457 path_in_zip="${path}"
458 break
459 fi
460 done
461 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700462 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700463
464 # If the partition is stored as an Android sparse image file, we need to
465 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800466 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700467 if [[ "${magic}" == "3aff26ed" ]]; then
468 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
469 echo "Converting Android sparse image ${part}.img to RAW."
470 mv "${part_file}" "${temp_sparse}"
471 simg2img "${temp_sparse}" "${part_file}"
472 rm -f "${temp_sparse}"
473 fi
474
475 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700476 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
477 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
478 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700479
480 # delta_generator only supports images multiple of 4 KiB. For target images
481 # we pad the data with zeros if needed, but for source images we truncate
482 # down the data since the last block of the old image could be padded on
483 # disk with unknown data.
484 local filesize=$(stat -c%s "${part_file}")
485 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
486 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
487 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
488 : $(( filesize = filesize & -4096 ))
489 else
490 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
491 : $(( filesize = (filesize + 4095) & -4096 ))
492 fi
493 truncate_file "${part_file}" "${filesize}"
494 fi
495
496 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
497}
498
Sen Jiang788c2d92016-03-09 12:48:40 -0800499# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700500#
501# Extract the A/B updated partitions from a Brillo target_files zip file into
502# new temporary files.
503extract_image_brillo() {
504 local image="$1"
505 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800506 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700507
Alex Deymo48b502a2015-09-17 19:00:18 -0700508 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800509 local ab_partitions_list
510 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
511 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700512 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
513 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800514 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
515 die "Invalid partition names found in the partition list."
516 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700517 # Get partition list without duplicates.
518 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800519 if [[ ${#partitions[@]} -eq 0 ]]; then
520 die "The list of partitions is empty. Can't generate a payload."
521 fi
522 else
523 warn "No ab_partitions.txt found. Using default."
524 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700525 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700526
Sen Jiang788c2d92016-03-09 12:48:40 -0800527 if [[ -n "${partitions_order}" ]]; then
528 eval "${partitions_order}=(${partitions[@]})"
529 fi
530
Alex Deymo83f2f702015-10-14 14:49:33 -0700531 # All Brillo updaters support major version 2.
532 FORCE_MAJOR_VERSION="2"
533
Alex Deymo48b502a2015-09-17 19:00:18 -0700534 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800535 # Source image
536 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700537 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700538 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
539 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
540 else
Alex Deymoc97df432015-09-25 17:23:52 -0700541 warn "No update_engine_config.txt found. Assuming pre-release image, \
542using payload minor version 2"
543 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700544 # For delta payloads, we use the major and minor version supported by the
545 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700546 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
547 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700548 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
549 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800550
551 # Brillo support for deltas started with minor version 3.
552 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
553 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
554Disabling deltas for this source version."
555 exit ${EX_UNSUPPORTED_DELTA}
556 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800557 else
558 # Target image
559 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
560 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700561 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
562 extract_file "${image}" "META/postinstall_config.txt" \
563 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800564 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
565 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700566 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
567 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700568 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
569 extract_file "${image}" "META/dynamic_partitions_info.txt" \
570 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700571 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
572 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500573 local apex_info=$(create_tempfile "apex_info.XXXXXX")
574 CLEANUP_FILES+=("${apex_info}")
575 if unzip -l "${image}" "META/apex_info.pb" > /dev/null; then
576 extract_file "${image}" "META/apex_info.pb" \
577 "${apex_info}"
578 APEX_INFO_FILE="${apex_info}"
579 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700580 fi
581
Sen Jiang3e5804d2018-09-06 15:53:00 -0700582 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700583 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700584 local part_file=$(create_tempfile "${part}.img.XXXXXX")
585 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
586 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
587 # Extract partitions in background.
588 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
589 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700590 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700591 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800592 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700593 done
594}
595
596# cleanup_partition_array <partitions_array>
597#
598# Remove all empty files in <partitions_array>.
599cleanup_partition_array() {
600 local partitions_array="$1"
601 # Have to use eval to iterate over associative array keys with variable array
602 # names, we should change it to use nameref once bash 4.3 is available
603 # everywhere.
604 for part in $(eval "echo \${!${partitions_array}[@]}"); do
605 local path="${partitions_array}[$part]"
606 if [[ ! -s "${!path}" ]]; then
607 eval "unset ${partitions_array}[${part}]"
608 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700609 done
610}
611
Tudor Brindusb432db82018-06-29 13:13:27 -0700612extract_payload_images() {
613 local payload_type=$1
614 echo "Extracting images for ${payload_type} update."
615
616 if [[ "${payload_type}" == "delta" ]]; then
617 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
618 fi
619 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700620 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
621 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
622 # as it gives the status of the last process it has waited for.
623 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
624 wait ${pid}
625 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700626 cleanup_partition_array SRC_PARTITIONS
627 cleanup_partition_array SRC_PARTITIONS_MAP
628 cleanup_partition_array DST_PARTITIONS
629 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700630}
631
632get_payload_type() {
633 if [[ -z "${FLAGS_source_image}" ]]; then
634 echo "full"
635 else
636 echo "delta"
637 fi
638}
639
Jason Kusumabe998f42015-09-03 15:53:13 -0700640validate_generate() {
641 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700642 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700643
644 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700645 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700646}
647
648cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700649 local payload_type=$(get_payload_type)
650 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700651
Alex Deymo48b502a2015-09-17 19:00:18 -0700652 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800653 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700654 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800655
656 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800657 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800658 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800659 if [[ -n "${partition_names}" ]]; then
660 partition_names+=":"
661 new_partitions+=":"
662 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800663 new_mapfiles+=":"
664 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800665 fi
666 partition_names+="${part}"
667 new_partitions+="${DST_PARTITIONS[${part}]}"
Kelvin Zhang999705e2020-11-03 10:07:09 -0500668 if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then
669 # Skip boot partition.
670 old_partitions+=""
671 else
672 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
673 fi
Alex Deymo20bdc702016-12-07 21:07:11 -0800674 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
675 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800676 done
677
678 # Target image args:
679 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700680 --partition_names="${partition_names}"
681 --new_partitions="${new_partitions}"
682 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700683 )
684
Tianjief5baff42020-07-17 21:43:22 -0700685 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
686 GENERATOR_ARGS+=( --is_partial_update="true" )
687 # Need at least minor version 7 for partial update, so generate with minor
688 # version 7 if we don't have a source image. Let the delta_generator to fail
689 # the other incompatiable minor versions.
690 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
691 FORCE_MINOR_VERSION="7"
692 fi
693 fi
694
Alex Deymo89ff9e32015-09-15 19:29:01 -0700695 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800696 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700697 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700698 --old_partitions="${old_partitions}"
699 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700700 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700701 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
702 GENERATOR_ARGS+=(
703 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
704 fi
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500705 if [[ -n "${FLAGS_disable_verity_computation}" ]]; then
706 GENERATOR_ARGS+=(
707 --disable_verity_computation="${FLAGS_disable_verity_computation}" )
708 fi
Kelvin Zhang413982e2021-03-02 15:34:50 -0500709 fi
710
711 if [[ -n "${FLAGS_disable_vabc}" ]]; then
712 GENERATOR_ARGS+=(
713 --disable_vabc="${FLAGS_disable_vabc}" )
Alex Deymo48b502a2015-09-17 19:00:18 -0700714 fi
715
Tianjief5baff42020-07-17 21:43:22 -0700716 # minor version is set only for delta or partial payload.
717 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
718 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
719 fi
720
Alex Deymo48b502a2015-09-17 19:00:18 -0700721 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
722 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700723 fi
724
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700725 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
726 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
727 fi
728
Sen Jiang8e768e92017-06-28 17:13:19 -0700729 if [[ -n "${FLAGS_max_timestamp}" ]]; then
730 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
731 fi
732
Kelvin Zhang1f496422020-08-11 17:18:23 -0400733 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
734 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
735 fi
736
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800737 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
738 GENERATOR_ARGS+=(
739 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
740 )
741 fi
742
Yifan Hong398cb542018-10-18 11:29:40 -0700743 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
744 GENERATOR_ARGS+=(
745 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
746 )
747 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500748 if [[ -n "{APEX_INFO_FILE}" ]]; then
749 GENERATOR_ARGS+=(
750 --apex_info_file="${APEX_INFO_FILE}"
751 )
752 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700753
Jason Kusumabe998f42015-09-03 15:53:13 -0700754 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700755 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700756
Alex Deymo89ff9e32015-09-15 19:29:01 -0700757 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700758}
759
760validate_hash() {
761 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700762 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700763
764 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700765 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700766--unsigned_payload FILENAME"
767
Jason Kusumabe998f42015-09-03 15:53:13 -0700768 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700769 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800770
771 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700772 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700773}
774
775cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700776 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700777 --in_file="${FLAGS_unsigned_payload}" \
778 --signature_size="${FLAGS_signature_size}" \
779 --out_hash_file="${FLAGS_payload_hash_file}" \
780 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700781
Jason Kusumabe998f42015-09-03 15:53:13 -0700782 echo "Done generating hash."
783}
784
785validate_sign() {
786 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700787 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700788
789 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700790 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700791--unsigned_payload FILENAME"
792
793 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700794 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700795
796 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700797 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700798--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700799
800 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700801 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700802--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700803}
804
805cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700806 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700807 --in_file="${FLAGS_unsigned_payload}"
808 --signature_size="${FLAGS_signature_size}"
809 --payload_signature_file="${FLAGS_payload_signature_file}"
810 --metadata_signature_file="${FLAGS_metadata_signature_file}"
811 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700812 )
813
814 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
815 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
816 fi
817
818 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700819 echo "Done signing payload."
820}
821
Alex Deymo98e691c2016-02-04 21:05:45 -0800822validate_properties() {
823 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700824 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800825
826 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700827 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800828}
829
830cmd_properties() {
831 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700832 --in_file="${FLAGS_payload}" \
833 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800834}
835
Tudor Brindusb432db82018-06-29 13:13:27 -0700836validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700837 [[ -n "${FLAGS_payload}" ]] ||
838 die "Error: you must specify an input filename with --payload FILENAME"
839
840 [[ -n "${FLAGS_target_image}" ]] ||
841 die "Error: you must specify a target image with --target_image FILENAME"
842}
843
844cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700845 local payload_type=$(get_payload_type)
846 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700847
848 declare -A TMP_PARTITIONS
849 for part in "${PARTITIONS_ORDER[@]}"; do
850 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
851 echo "Creating temporary target partition ${tmp_part} for ${part}"
852 CLEANUP_FILES+=("${tmp_part}")
853 TMP_PARTITIONS[${part}]=${tmp_part}
854 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
855 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
856 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
857 done
858
859 echo "Verifying ${payload_type} update."
860 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700861 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700862
863 local part old_partitions="" new_partitions="" partition_names=""
864 for part in "${PARTITIONS_ORDER[@]}"; do
865 if [[ -n "${partition_names}" ]]; then
866 partition_names+=":"
867 new_partitions+=":"
868 old_partitions+=":"
869 fi
870 partition_names+="${part}"
871 new_partitions+="${TMP_PARTITIONS[${part}]}"
872 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
873 done
874
875 # Target image args:
876 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700877 --partition_names="${partition_names}"
878 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700879 )
880
881 if [[ "${payload_type}" == "delta" ]]; then
882 # Source image args:
883 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700884 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700885 )
886 fi
887
Amin Hassania566cb62017-08-23 12:36:55 -0700888 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
889 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
890 fi
891
Amin Hassani13520932017-07-26 11:26:05 -0700892 echo "Running delta_generator to verify ${payload_type} payload with args: \
893${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700894 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700895
Sen Jiang6feb15c2018-08-31 15:45:17 -0700896 echo "Done applying ${payload_type} update."
897 echo "Checking the newly generated partitions against the target partitions"
898 local need_pause=false
899 for part in "${PARTITIONS_ORDER[@]}"; do
900 local not_str=""
901 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
902 not_str="in"
903 need_pause=true
904 fi
905 echo "The new partition (${part}) is ${not_str}valid."
906 done
907 # All images will be cleaned up when script exits, pause here to give a chance
908 # to inspect the images.
909 if [[ "$need_pause" == true ]]; then
910 read -n1 -r -s -p "Paused to investigate invalid partitions, \
911press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700912 fi
913}
914
Tudor Brindusb432db82018-06-29 13:13:27 -0700915cmd_check() {
916 local payload_type=$(get_payload_type)
917 extract_payload_images ${payload_type}
918
919 local part dst_partitions="" src_partitions=""
920 for part in "${PARTITIONS_ORDER[@]}"; do
921 if [[ -n "${dst_partitions}" ]]; then
922 dst_partitions+=" "
923 src_partitions+=" "
924 fi
925 dst_partitions+="${DST_PARTITIONS[${part}]}"
926 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
927 done
928
929 # Common payload args:
930 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
931 --part_names ${PARTITIONS_ORDER[@]} \
932 --dst_part_paths ${dst_partitions} )
933
934 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
935 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
936 fi
937
938 echo "Checking ${payload_type} update."
939 check_update_payload ${PAYCHECK_ARGS[@]} --check
940}
941
Tianjiee283ce42020-07-29 11:37:51 -0700942# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700943[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700944[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
945
946case "$COMMAND" in
947 generate) validate_generate
948 cmd_generate
949 ;;
950 hash) validate_hash
951 cmd_hash
952 ;;
953 sign) validate_sign
954 cmd_sign
955 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800956 properties) validate_properties
957 cmd_properties
958 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700959 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700960 cmd_verify
961 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700962 check) validate_verify_and_check
963 cmd_check
964 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700965esac