blob: 2f5ef2270a3b29e5ac17cb378249ea429ed5d5d4 [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
Tianjief4502bb2021-09-08 19:08:53 -070041# --force_minor_version Override the minor version used for delta
42# generation.
Jason Kusumabe998f42015-09-03 15:53:13 -070043#
44# Hash command arguments:
45# --unsigned_payload the input unsigned payload to generate the hash from
46# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070047# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070048# --payload_hash_file if defined, generate a payload hash and output to the
49# specified file
50# --metadata_hash_file if defined, generate a metadata hash and output to the
51# specified file
52#
53# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070054# --unsigned_payload the input unsigned payload to insert the signatures
55# --payload the output signed payload
56# --signature_size signature sizes in bytes in the following format:
57# "size1:size2[:...]"
58# --payload_signature_file the payload signature files in the following
59# format:
60# "payload_signature1:payload_signature2[:...]"
61# --metadata_signature_file the metadata signature files in the following
62# format:
63# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070064# --metadata_size_file if defined, generate a file containing the size of
65# the signed payload metadata in bytes to the
66# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070067# Note that the number of signature sizes and payload signatures have to match.
Alex Deymo98e691c2016-02-04 21:05:45 -080068#
69# Properties command arguments:
70# --payload the input signed or unsigned payload
71# --properties_file the output path where to write the properties, or
72# '-' for stdout.
Amin Hassani13520932017-07-26 11:26:05 -070073# Verify command arguments:
74# --payload payload input file
75# --source_image verify payload to the specified source image.
76# --target_image the target image to verify upon.
Tudor Brindusb432db82018-06-29 13:13:27 -070077#
78# Check command arguments:
79# Symmetrical with the verify command.
Alex Deymo98e691c2016-02-04 21:05:45 -080080
Jason Kusumabe998f42015-09-03 15:53:13 -070081
Alex Deymo61e1fa82016-01-19 15:16:34 -080082# Exit codes:
83EX_UNSUPPORTED_DELTA=100
84
Jason Kusumaf514c542015-11-05 18:43:45 -080085warn() {
86 echo "brillo_update_payload: warning: $*" >&2
87}
88
Gilad Arnold957ce122015-10-14 16:02:55 -070089die() {
90 echo "brillo_update_payload: error: $*" >&2
91 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070092}
93
Parveen Kumarb31e1ac2020-10-16 15:30:09 -070094# Loads shflags. We first look at the default install location; then our own
95# directory; finally the parent directory.
Gilad Arnold957ce122015-10-14 16:02:55 -070096load_shflags() {
97 local my_dir="$(dirname "$(readlink -f "$0")")"
98 local path
Parveen Kumarb31e1ac2020-10-16 15:30:09 -070099 for path in /usr/share/misc \
100 "${my_dir}"/lib/shflags \
101 "${my_dir}"/../lib/shflags; do
Gilad Arnold957ce122015-10-14 16:02:55 -0700102 if [[ -r "${path}/shflags" ]]; then
103 . "${path}/shflags" || die "Could not load ${path}/shflags."
104 return
105 fi
106 done
107 die "Could not find shflags."
108}
109
110load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -0700111
Alex Deymoc64ffd52015-09-25 18:10:07 -0700112HELP_GENERATE="generate: Generate an unsigned update payload."
113HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
114for signing."
115HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -0800116HELP_PROPERTIES="properties: Extract payload properties to a file."
Tudor Brindusb432db82018-06-29 13:13:27 -0700117HELP_VERIFY="verify: Verify a (signed) update payload using delta_generator."
118HELP_CHECK="check: Check a (signed) update payload using paycheck (static \
119testing)."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700120
121usage() {
122 echo "Supported commands:"
123 echo
124 echo "${HELP_GENERATE}"
125 echo "${HELP_HASH}"
126 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800127 echo "${HELP_PROPERTIES}"
Amin Hassani13520932017-07-26 11:26:05 -0700128 echo "${HELP_VERIFY}"
Tudor Brindusb432db82018-06-29 13:13:27 -0700129 echo "${HELP_CHECK}"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700130 echo
131 echo "Use: \"$0 <command> --help\" for more options."
132}
133
134# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700135if [[ $# -lt 1 ]]; then
Tudor Brindusb432db82018-06-29 13:13:27 -0700136 echo "Please specify a command [generate|hash|sign|properties|verify|check]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700137 exit 1
138fi
139
Alex Deymoc64ffd52015-09-25 18:10:07 -0700140# Parse command.
141COMMAND="${1:-}"
142shift
143
144case "${COMMAND}" in
145 generate)
146 FLAGS_HELP="${HELP_GENERATE}"
147 ;;
148
149 hash)
150 FLAGS_HELP="${HELP_HASH}"
151 ;;
152
153 sign)
154 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700155 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800156
157 properties)
158 FLAGS_HELP="${HELP_PROPERTIES}"
159 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700160
161 verify)
162 FLAGS_HELP="${HELP_VERIFY}"
163 ;;
164
Tudor Brindusb432db82018-06-29 13:13:27 -0700165 check)
166 FLAGS_HELP="${HELP_CHECK}"
167 ;;
168
Jason Kusumabe998f42015-09-03 15:53:13 -0700169 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700170 echo "Unrecognized command: \"${COMMAND}\"" >&2
171 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700172 exit 1
173 ;;
174esac
175
Jason Kusumabe998f42015-09-03 15:53:13 -0700176# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700177FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
178${FLAGS_HELP}"
179
180if [[ "${COMMAND}" == "generate" ]]; then
181 DEFINE_string payload "" \
182 "Path to output the generated unsigned payload file."
183 DEFINE_string target_image "" \
184 "Path to the target image that should be sent to clients."
185 DEFINE_string source_image "" \
186 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700187 DEFINE_string metadata_size_file "" \
188 "Optional: Path to output metadata size."
Sen Jiang8e768e92017-06-28 17:13:19 -0700189 DEFINE_string max_timestamp "" \
190 "Optional: The maximum unix timestamp of the OS allowed to apply this \
191payload, should be set to a number higher than the build timestamp of the \
192system running on the device, 0 if not specified."
Kelvin Zhang1f496422020-08-11 17:18:23 -0400193 DEFINE_string partition_timestamps "" \
194 "Optional: Per-partition maximum unix timestamp of the OS allowed to \
195apply this payload. Should be a comma separated key value pairs. e.x.\
196system:1234,vendor:456"
Tianjie Xu72d512c2019-08-21 15:20:35 -0700197 DEFINE_string disable_fec_computation "" \
198 "Optional: Disables the on device fec data computation for incremental \
199update. This feature is enabled by default."
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500200 DEFINE_string disable_verity_computation "" \
201 "Optional: Disables the on device verity computation for incremental \
202update. This feature is enabled by default."
Tianjief5baff42020-07-17 21:43:22 -0700203 DEFINE_string is_partial_update "" \
204 "Optional: True if the payload is for partial update. i.e. it only updates \
205a subset of partitions on device."
Kelvin Zhangdde2ef42020-11-20 12:26:19 -0500206 DEFINE_string full_boot "" "Will include full boot image"
Kelvin Zhang9101ff32021-01-19 15:48:53 -0500207 DEFINE_string disable_vabc "" \
208 "Optional: Disables Virtual AB Compression when installing the OTA"
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500209 DEFINE_string enable_vabc_xor "" \
210 "Optional: Enable the use of Virtual AB Compression XOR feature"
Tianjief4502bb2021-09-08 19:08:53 -0700211 DEFINE_string force_minor_version "" \
212 "Optional: Override the minor version for the delta generation."
Kelvin Zhang0aa4fae2021-10-28 09:15:27 -0700213 DEFINE_string compressor_types "" \
214 "Optional: allowed compressor types. Colon separated, allowe values are bz2 and brotli"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700215fi
216if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
217 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
218 DEFINE_string signature_size "" \
219 "Signature sizes in bytes in the following format: size1:size2[:...]"
220fi
221if [[ "${COMMAND}" == "hash" ]]; then
222 DEFINE_string metadata_hash_file "" \
223 "Optional: Path to output metadata hash file."
224 DEFINE_string payload_hash_file "" \
225 "Optional: Path to output payload hash file."
226fi
227if [[ "${COMMAND}" == "sign" ]]; then
228 DEFINE_string payload "" \
229 "Path to output the generated unsigned payload file."
230 DEFINE_string metadata_signature_file "" \
231 "The metatada signatures in the following format: \
232metadata_signature1:metadata_signature2[:...]"
233 DEFINE_string payload_signature_file "" \
234 "The payload signatures in the following format: \
235payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700236 DEFINE_string metadata_size_file "" \
237 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700238fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800239if [[ "${COMMAND}" == "properties" ]]; then
240 DEFINE_string payload "" \
241 "Path to the input signed or unsigned payload file."
242 DEFINE_string properties_file "-" \
243 "Path to output the extracted property files. If '-' is passed stdout will \
244be used."
245fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700246if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700247 DEFINE_string payload "" \
248 "Path to the input payload file."
249 DEFINE_string target_image "" \
250 "Path to the target image to verify upon."
251 DEFINE_string source_image "" \
252 "Optional: Path to a source image. If specified, the delta update is \
253applied to this."
254fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800255
Alex Deymo5fbb1102017-01-12 13:55:52 -0800256DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700257
258# Parse command line flag arguments
259FLAGS "$@" || exit 1
260eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700261set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700262
Alex Deymo5fbb1102017-01-12 13:55:52 -0800263# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
264# ${TMPDIR}.
265TMPDIR="${FLAGS_work_dir}"
266export TMPDIR
267
Alex Deymo89ff9e32015-09-15 19:29:01 -0700268# Associative arrays from partition name to file in the source and target
269# images. The size of the updated area must be the size of the file.
270declare -A SRC_PARTITIONS
271declare -A DST_PARTITIONS
272
Alex Deymo20bdc702016-12-07 21:07:11 -0800273# Associative arrays for the .map files associated with each src/dst partition
274# file in SRC_PARTITIONS and DST_PARTITIONS.
275declare -A SRC_PARTITIONS_MAP
276declare -A DST_PARTITIONS_MAP
277
Sen Jiang788c2d92016-03-09 12:48:40 -0800278# List of partition names in order.
279declare -a PARTITIONS_ORDER
280
Tao Bao96489902019-04-02 16:25:03 -0700281# A list of PIDs of the extract_image workers.
282EXTRACT_IMAGE_PIDS=()
283
Alex Deymo89ff9e32015-09-15 19:29:01 -0700284# A list of temporary files to remove during cleanup.
285CLEANUP_FILES=()
286
Alex Deymo48b502a2015-09-17 19:00:18 -0700287# Global options to force the version of the payload.
288FORCE_MAJOR_VERSION=""
289FORCE_MINOR_VERSION=""
290
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800291# Path to the postinstall config file in target image if exists.
292POSTINSTALL_CONFIG_FILE=""
293
Yifan Hong398cb542018-10-18 11:29:40 -0700294# Path to the dynamic partition info file in target image if exists.
295DYNAMIC_PARTITION_INFO_FILE=""
296
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500297# Path to the META/apex_info.pb found in target build
298APEX_INFO_FILE=""
299
Alex Deymoc97df432015-09-25 17:23:52 -0700300# read_option_int <file.txt> <option_key> [default_value]
301#
302# Reads the unsigned integer value associated with |option_key| in a key=value
303# file |file.txt|. Prints the read value if found and valid, otherwise prints
304# the |default_value|.
305read_option_uint() {
306 local file_txt="$1"
307 local option_key="$2"
308 local default_value="${3:-}"
309 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700310 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700311 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
312 echo "${value}"
313 return
314 fi
315 fi
316 echo "${default_value}"
317}
318
Sen Jiangd0e9a892016-07-22 16:28:07 -0700319# truncate_file <file_path> <file_size>
320#
Dan Willemsen32711862018-10-04 21:25:50 -0700321# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700322# The truncate binary might not be available.
323truncate_file() {
324 local file_path="$1"
325 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700326 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700327}
328
Alex Deymo89ff9e32015-09-15 19:29:01 -0700329# Create a temporary file in the work_dir with an optional pattern name.
330# Prints the name of the newly created file.
331create_tempfile() {
332 local pattern="${1:-tempfile.XXXXXX}"
333 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
334}
Jason Kusumabe998f42015-09-03 15:53:13 -0700335
336cleanup() {
337 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700338 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700339
340 # If we are cleaning up after an error, or if we got an error during
341 # cleanup (even if we eventually succeeded) return a non-zero exit
342 # code. This triggers additional logging in most environments that call
343 # this script.
344 if [[ -n "${err}" ]]; then
345 die "Cleanup encountered an error."
346 fi
347}
348
349cleanup_on_error() {
350 trap - INT TERM ERR EXIT
351 cleanup
352 die "Cleanup success after an error."
353}
354
355cleanup_on_exit() {
356 trap - INT TERM ERR EXIT
357 cleanup
358}
359
360trap cleanup_on_error INT TERM ERR
361trap cleanup_on_exit EXIT
362
Tianjie Xu14715ce2019-08-06 17:24:43 -0700363# extract_file <zip_file> <entry_name> <destination>
364#
365# Extracts |entry_name| from |zip_file| to |destination|.
366extract_file() {
367 local zip_file="$1"
368 local entry_name="$2"
369 local destination="$3"
370
371 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
372 # as the destination of the unzip, and move the file to the intended
373 # destination.
374 local output_directory=$(
375 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
376 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
377 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
378
379 mv "${output_directory}/${entry_name}" "${destination}"
380 rm -rf "${output_directory}"
381}
Alex Deymo48b502a2015-09-17 19:00:18 -0700382
Sen Jiang788c2d92016-03-09 12:48:40 -0800383# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700384#
385# Detect the format of the |image| file and extract its updatable partitions
386# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800387# associative array passed in |partitions_array|. If |partitions_order| is
388# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700389extract_image() {
390 local image="$1"
391
392 # Brillo images are zip files. We detect the 4-byte magic header of the zip
393 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800394 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700395 if [[ "${magic}" == "504b0304" ]]; then
396 echo "Detected .zip file, extracting Brillo image."
397 extract_image_brillo "$@"
398 return
399 fi
400
401 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
402 # bundled here and we will use it to extract the partitions, so the GPT
403 # headers must be valid.
404 if cgpt show -q -n "${image}" >/dev/null; then
405 echo "Detected GPT image, extracting Chrome OS image."
406 extract_image_cros "$@"
407 return
408 fi
409
410 die "Couldn't detect the image format of ${image}"
411}
412
Sen Jiang788c2d92016-03-09 12:48:40 -0800413# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700414#
Alex Deymo48b502a2015-09-17 19:00:18 -0700415# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700416extract_image_cros() {
417 local image="$1"
418 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800419 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700420
421 local kernel root
422 kernel=$(create_tempfile "kernel.bin.XXXXXX")
423 CLEANUP_FILES+=("${kernel}")
424 root=$(create_tempfile "root.bin.XXXXXX")
425 CLEANUP_FILES+=("${root}")
426
427 cros_generate_update_payload --extract \
428 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700429 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700430
Amin Hassani58e01d62018-09-19 14:56:15 -0700431 # Chrome OS now uses major_version 2 payloads for all boards.
432 # See crbug.com/794404 for more information.
433 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700434
Tudor Brindusdda79e22018-06-28 18:03:21 -0700435 eval ${partitions_array}[kernel]=\""${kernel}"\"
436 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700437
Sen Jiang788c2d92016-03-09 12:48:40 -0800438 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700439 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800440 fi
441
Alex Deymo89ff9e32015-09-15 19:29:01 -0700442 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700443 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700444 varname="${partitions_array}[${part}]"
445 printf "md5sum of %s: " "${varname}"
446 md5sum "${!varname}"
447 done
448}
449
Sen Jiang3e5804d2018-09-06 15:53:00 -0700450# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
451# <part_file> <part_map_file>
452#
453# Extract the <partition> from target_files zip file into <part_file> and its
454# map file into <part_map_file>.
455extract_partition_brillo() {
456 local image="$1"
457 local partitions_array="$2"
458 local part="$3"
459 local part_file="$4"
460 local part_map_file="$5"
461
462 # For each partition, we in turn look for its image file under IMAGES/ and
463 # RADIO/ in the given target_files zip file.
464 local path path_in_zip
465 for path in IMAGES RADIO; do
466 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
467 path_in_zip="${path}"
468 break
469 fi
470 done
471 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700472 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700473
474 # If the partition is stored as an Android sparse image file, we need to
475 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800476 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700477 if [[ "${magic}" == "3aff26ed" ]]; then
478 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
479 echo "Converting Android sparse image ${part}.img to RAW."
480 mv "${part_file}" "${temp_sparse}"
481 simg2img "${temp_sparse}" "${part_file}"
482 rm -f "${temp_sparse}"
483 fi
484
485 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700486 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
487 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
488 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700489
490 # delta_generator only supports images multiple of 4 KiB. For target images
491 # we pad the data with zeros if needed, but for source images we truncate
492 # down the data since the last block of the old image could be padded on
493 # disk with unknown data.
494 local filesize=$(stat -c%s "${part_file}")
495 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
496 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
497 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
498 : $(( filesize = filesize & -4096 ))
499 else
500 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
501 : $(( filesize = (filesize + 4095) & -4096 ))
502 fi
503 truncate_file "${part_file}" "${filesize}"
504 fi
505
506 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
507}
508
Sen Jiang788c2d92016-03-09 12:48:40 -0800509# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700510#
511# Extract the A/B updated partitions from a Brillo target_files zip file into
512# new temporary files.
513extract_image_brillo() {
514 local image="$1"
515 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800516 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700517
Alex Deymo48b502a2015-09-17 19:00:18 -0700518 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800519 local ab_partitions_list
520 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
521 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700522 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
523 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800524 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
525 die "Invalid partition names found in the partition list."
526 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700527 # Get partition list without duplicates.
528 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800529 if [[ ${#partitions[@]} -eq 0 ]]; then
530 die "The list of partitions is empty. Can't generate a payload."
531 fi
532 else
533 warn "No ab_partitions.txt found. Using default."
534 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700535 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700536
Sen Jiang788c2d92016-03-09 12:48:40 -0800537 if [[ -n "${partitions_order}" ]]; then
538 eval "${partitions_order}=(${partitions[@]})"
539 fi
540
Alex Deymo83f2f702015-10-14 14:49:33 -0700541 # All Brillo updaters support major version 2.
542 FORCE_MAJOR_VERSION="2"
543
Alex Deymo48b502a2015-09-17 19:00:18 -0700544 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800545 # Source image
546 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700547 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700548 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
549 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
550 else
Alex Deymoc97df432015-09-25 17:23:52 -0700551 warn "No update_engine_config.txt found. Assuming pre-release image, \
552using payload minor version 2"
553 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700554 # For delta payloads, we use the major and minor version supported by the
555 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700556 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
557 "PAYLOAD_MINOR_VERSION" 2)
Tianjief4502bb2021-09-08 19:08:53 -0700558 if [[ -n "${FLAGS_force_minor_version}" ]]; then
559 FORCE_MINOR_VERSION="${FLAGS_force_minor_version}"
560 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700561 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
562 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800563
564 # Brillo support for deltas started with minor version 3.
565 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
566 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
567Disabling deltas for this source version."
568 exit ${EX_UNSUPPORTED_DELTA}
569 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800570 else
571 # Target image
572 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
573 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700574 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
575 extract_file "${image}" "META/postinstall_config.txt" \
576 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800577 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
578 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700579 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
580 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700581 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
582 extract_file "${image}" "META/dynamic_partitions_info.txt" \
583 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700584 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
585 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500586 local apex_info=$(create_tempfile "apex_info.XXXXXX")
587 CLEANUP_FILES+=("${apex_info}")
588 if unzip -l "${image}" "META/apex_info.pb" > /dev/null; then
589 extract_file "${image}" "META/apex_info.pb" \
590 "${apex_info}"
591 APEX_INFO_FILE="${apex_info}"
592 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700593 fi
594
Sen Jiang3e5804d2018-09-06 15:53:00 -0700595 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700596 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700597 local part_file=$(create_tempfile "${part}.img.XXXXXX")
598 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
599 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
600 # Extract partitions in background.
601 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
602 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700603 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700604 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800605 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700606 done
607}
608
609# cleanup_partition_array <partitions_array>
610#
611# Remove all empty files in <partitions_array>.
612cleanup_partition_array() {
613 local partitions_array="$1"
614 # Have to use eval to iterate over associative array keys with variable array
615 # names, we should change it to use nameref once bash 4.3 is available
616 # everywhere.
617 for part in $(eval "echo \${!${partitions_array}[@]}"); do
618 local path="${partitions_array}[$part]"
619 if [[ ! -s "${!path}" ]]; then
620 eval "unset ${partitions_array}[${part}]"
621 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700622 done
623}
624
Tudor Brindusb432db82018-06-29 13:13:27 -0700625extract_payload_images() {
626 local payload_type=$1
627 echo "Extracting images for ${payload_type} update."
628
629 if [[ "${payload_type}" == "delta" ]]; then
630 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
631 fi
632 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700633 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
634 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
635 # as it gives the status of the last process it has waited for.
636 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
637 wait ${pid}
638 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700639 cleanup_partition_array SRC_PARTITIONS
640 cleanup_partition_array SRC_PARTITIONS_MAP
641 cleanup_partition_array DST_PARTITIONS
642 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700643}
644
645get_payload_type() {
646 if [[ -z "${FLAGS_source_image}" ]]; then
647 echo "full"
648 else
649 echo "delta"
650 fi
651}
652
Jason Kusumabe998f42015-09-03 15:53:13 -0700653validate_generate() {
654 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700655 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700656
657 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700658 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700659}
660
661cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700662 local payload_type=$(get_payload_type)
663 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700664
Alex Deymo48b502a2015-09-17 19:00:18 -0700665 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800666 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700667 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800668
669 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800670 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800671 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800672 if [[ -n "${partition_names}" ]]; then
673 partition_names+=":"
674 new_partitions+=":"
675 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800676 new_mapfiles+=":"
677 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800678 fi
679 partition_names+="${part}"
680 new_partitions+="${DST_PARTITIONS[${part}]}"
Kelvin Zhang999705e2020-11-03 10:07:09 -0500681 if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then
682 # Skip boot partition.
683 old_partitions+=""
684 else
685 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
686 fi
Alex Deymo20bdc702016-12-07 21:07:11 -0800687 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
688 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800689 done
690
691 # Target image args:
692 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700693 --partition_names="${partition_names}"
694 --new_partitions="${new_partitions}"
695 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700696 )
697
Tianjief5baff42020-07-17 21:43:22 -0700698 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
699 GENERATOR_ARGS+=( --is_partial_update="true" )
700 # Need at least minor version 7 for partial update, so generate with minor
701 # version 7 if we don't have a source image. Let the delta_generator to fail
702 # the other incompatiable minor versions.
703 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
704 FORCE_MINOR_VERSION="7"
705 fi
706 fi
707
Alex Deymo89ff9e32015-09-15 19:29:01 -0700708 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800709 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700710 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700711 --old_partitions="${old_partitions}"
712 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700713 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700714 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
715 GENERATOR_ARGS+=(
716 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
717 fi
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500718 if [[ -n "${FLAGS_disable_verity_computation}" ]]; then
719 GENERATOR_ARGS+=(
720 --disable_verity_computation="${FLAGS_disable_verity_computation}" )
721 fi
Kelvin Zhang0aa4fae2021-10-28 09:15:27 -0700722 if [[ -n "${FLAGS_compressor_types}" ]]; then
723 GENERATOR_ARGS+=(
724 --compressor_types="${FLAGS_compressor_types}" )
725 fi
Kelvin Zhang413982e2021-03-02 15:34:50 -0500726 fi
727
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500728 if [[ -n "${FLAGS_enable_vabc_xor}" ]]; then
729 GENERATOR_ARGS+=(
730 --enable_vabc_xor="${FLAGS_enable_vabc_xor}" )
731 fi
732
Kelvin Zhang413982e2021-03-02 15:34:50 -0500733 if [[ -n "${FLAGS_disable_vabc}" ]]; then
734 GENERATOR_ARGS+=(
735 --disable_vabc="${FLAGS_disable_vabc}" )
Alex Deymo48b502a2015-09-17 19:00:18 -0700736 fi
737
Tianjief5baff42020-07-17 21:43:22 -0700738 # minor version is set only for delta or partial payload.
739 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
740 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
741 fi
742
Alex Deymo48b502a2015-09-17 19:00:18 -0700743 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
744 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700745 fi
746
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700747 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
748 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
749 fi
750
Sen Jiang8e768e92017-06-28 17:13:19 -0700751 if [[ -n "${FLAGS_max_timestamp}" ]]; then
752 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
753 fi
754
Kelvin Zhang1f496422020-08-11 17:18:23 -0400755 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
756 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
757 fi
758
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800759 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
760 GENERATOR_ARGS+=(
761 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
762 )
763 fi
764
Yifan Hong398cb542018-10-18 11:29:40 -0700765 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
766 GENERATOR_ARGS+=(
767 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
768 )
769 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500770 if [[ -n "{APEX_INFO_FILE}" ]]; then
771 GENERATOR_ARGS+=(
772 --apex_info_file="${APEX_INFO_FILE}"
773 )
774 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700775
Jason Kusumabe998f42015-09-03 15:53:13 -0700776 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700777 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700778
Alex Deymo89ff9e32015-09-15 19:29:01 -0700779 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700780}
781
782validate_hash() {
783 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700784 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700785
786 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700787 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700788--unsigned_payload FILENAME"
789
Jason Kusumabe998f42015-09-03 15:53:13 -0700790 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700791 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800792
793 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700794 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700795}
796
797cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700798 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700799 --in_file="${FLAGS_unsigned_payload}" \
800 --signature_size="${FLAGS_signature_size}" \
801 --out_hash_file="${FLAGS_payload_hash_file}" \
802 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700803
Jason Kusumabe998f42015-09-03 15:53:13 -0700804 echo "Done generating hash."
805}
806
807validate_sign() {
808 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700809 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700810
811 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700812 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700813--unsigned_payload FILENAME"
814
815 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700816 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700817
818 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700819 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700820--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700821
822 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700823 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700824--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700825}
826
827cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700828 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700829 --in_file="${FLAGS_unsigned_payload}"
830 --signature_size="${FLAGS_signature_size}"
831 --payload_signature_file="${FLAGS_payload_signature_file}"
832 --metadata_signature_file="${FLAGS_metadata_signature_file}"
833 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700834 )
835
836 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
837 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
838 fi
839
840 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700841 echo "Done signing payload."
842}
843
Alex Deymo98e691c2016-02-04 21:05:45 -0800844validate_properties() {
845 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700846 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800847
848 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700849 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800850}
851
852cmd_properties() {
853 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700854 --in_file="${FLAGS_payload}" \
855 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800856}
857
Tudor Brindusb432db82018-06-29 13:13:27 -0700858validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700859 [[ -n "${FLAGS_payload}" ]] ||
860 die "Error: you must specify an input filename with --payload FILENAME"
861
862 [[ -n "${FLAGS_target_image}" ]] ||
863 die "Error: you must specify a target image with --target_image FILENAME"
864}
865
866cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700867 local payload_type=$(get_payload_type)
868 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700869
870 declare -A TMP_PARTITIONS
871 for part in "${PARTITIONS_ORDER[@]}"; do
872 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
873 echo "Creating temporary target partition ${tmp_part} for ${part}"
874 CLEANUP_FILES+=("${tmp_part}")
875 TMP_PARTITIONS[${part}]=${tmp_part}
876 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
877 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
878 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
879 done
880
881 echo "Verifying ${payload_type} update."
882 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700883 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700884
885 local part old_partitions="" new_partitions="" partition_names=""
886 for part in "${PARTITIONS_ORDER[@]}"; do
887 if [[ -n "${partition_names}" ]]; then
888 partition_names+=":"
889 new_partitions+=":"
890 old_partitions+=":"
891 fi
892 partition_names+="${part}"
893 new_partitions+="${TMP_PARTITIONS[${part}]}"
894 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
895 done
896
897 # Target image args:
898 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700899 --partition_names="${partition_names}"
900 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700901 )
902
903 if [[ "${payload_type}" == "delta" ]]; then
904 # Source image args:
905 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700906 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700907 )
908 fi
909
Amin Hassania566cb62017-08-23 12:36:55 -0700910 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
911 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
912 fi
913
Amin Hassani13520932017-07-26 11:26:05 -0700914 echo "Running delta_generator to verify ${payload_type} payload with args: \
915${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700916 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700917
Sen Jiang6feb15c2018-08-31 15:45:17 -0700918 echo "Done applying ${payload_type} update."
919 echo "Checking the newly generated partitions against the target partitions"
920 local need_pause=false
921 for part in "${PARTITIONS_ORDER[@]}"; do
922 local not_str=""
923 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
924 not_str="in"
925 need_pause=true
926 fi
927 echo "The new partition (${part}) is ${not_str}valid."
928 done
929 # All images will be cleaned up when script exits, pause here to give a chance
930 # to inspect the images.
931 if [[ "$need_pause" == true ]]; then
932 read -n1 -r -s -p "Paused to investigate invalid partitions, \
933press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700934 fi
935}
936
Tudor Brindusb432db82018-06-29 13:13:27 -0700937cmd_check() {
938 local payload_type=$(get_payload_type)
939 extract_payload_images ${payload_type}
940
941 local part dst_partitions="" src_partitions=""
942 for part in "${PARTITIONS_ORDER[@]}"; do
943 if [[ -n "${dst_partitions}" ]]; then
944 dst_partitions+=" "
945 src_partitions+=" "
946 fi
947 dst_partitions+="${DST_PARTITIONS[${part}]}"
948 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
949 done
950
951 # Common payload args:
952 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
953 --part_names ${PARTITIONS_ORDER[@]} \
954 --dst_part_paths ${dst_partitions} )
955
956 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
957 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
958 fi
959
960 echo "Checking ${payload_type} update."
961 check_update_payload ${PAYCHECK_ARGS[@]} --check
962}
963
Tianjiee283ce42020-07-29 11:37:51 -0700964# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700965[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700966[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
967
968case "$COMMAND" in
969 generate) validate_generate
970 cmd_generate
971 ;;
972 hash) validate_hash
973 cmd_hash
974 ;;
975 sign) validate_sign
976 cmd_sign
977 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800978 properties) validate_properties
979 cmd_properties
980 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700981 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700982 cmd_verify
983 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700984 check) validate_verify_and_check
985 cmd_check
986 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700987esac