blob: f9c70f3478e72b1d223fc32db9a1dbb35226b213 [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"
Kelvin Zhang22987982022-01-04 14:37:28 -0800215 DEFINE_string enable_zucchini "" \
216 "Optional: Whether to enable zucchini diffing"
Kelvin Zhang907b0e62022-01-10 06:23:10 -0800217 DEFINE_string enable_lz4diff "" \
218 "Optional: Whether to enable lz4 diffing for EROFS"
219 DEFINE_string liblz4_path "" \
220 "Required if --enabled_lz4diff true is passed. Path to liblz4.so. delta_generator will use this copy of liblz4.so for compression. It is important that this copy of liblz4.so is the same as the one on source build."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700221fi
222if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
223 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
224 DEFINE_string signature_size "" \
225 "Signature sizes in bytes in the following format: size1:size2[:...]"
226fi
227if [[ "${COMMAND}" == "hash" ]]; then
228 DEFINE_string metadata_hash_file "" \
229 "Optional: Path to output metadata hash file."
230 DEFINE_string payload_hash_file "" \
231 "Optional: Path to output payload hash file."
232fi
233if [[ "${COMMAND}" == "sign" ]]; then
234 DEFINE_string payload "" \
235 "Path to output the generated unsigned payload file."
236 DEFINE_string metadata_signature_file "" \
237 "The metatada signatures in the following format: \
238metadata_signature1:metadata_signature2[:...]"
239 DEFINE_string payload_signature_file "" \
240 "The payload signatures in the following format: \
241payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700242 DEFINE_string metadata_size_file "" \
243 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700244fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800245if [[ "${COMMAND}" == "properties" ]]; then
246 DEFINE_string payload "" \
247 "Path to the input signed or unsigned payload file."
248 DEFINE_string properties_file "-" \
249 "Path to output the extracted property files. If '-' is passed stdout will \
250be used."
251fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700252if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700253 DEFINE_string payload "" \
254 "Path to the input payload file."
255 DEFINE_string target_image "" \
256 "Path to the target image to verify upon."
257 DEFINE_string source_image "" \
258 "Optional: Path to a source image. If specified, the delta update is \
259applied to this."
260fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800261
Alex Deymo5fbb1102017-01-12 13:55:52 -0800262DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700263
264# Parse command line flag arguments
265FLAGS "$@" || exit 1
266eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700267set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700268
Alex Deymo5fbb1102017-01-12 13:55:52 -0800269# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
270# ${TMPDIR}.
271TMPDIR="${FLAGS_work_dir}"
272export TMPDIR
273
Alex Deymo89ff9e32015-09-15 19:29:01 -0700274# Associative arrays from partition name to file in the source and target
275# images. The size of the updated area must be the size of the file.
276declare -A SRC_PARTITIONS
277declare -A DST_PARTITIONS
278
Alex Deymo20bdc702016-12-07 21:07:11 -0800279# Associative arrays for the .map files associated with each src/dst partition
280# file in SRC_PARTITIONS and DST_PARTITIONS.
281declare -A SRC_PARTITIONS_MAP
282declare -A DST_PARTITIONS_MAP
283
Sen Jiang788c2d92016-03-09 12:48:40 -0800284# List of partition names in order.
285declare -a PARTITIONS_ORDER
286
Tao Bao96489902019-04-02 16:25:03 -0700287# A list of PIDs of the extract_image workers.
288EXTRACT_IMAGE_PIDS=()
289
Alex Deymo89ff9e32015-09-15 19:29:01 -0700290# A list of temporary files to remove during cleanup.
291CLEANUP_FILES=()
292
Alex Deymo48b502a2015-09-17 19:00:18 -0700293# Global options to force the version of the payload.
294FORCE_MAJOR_VERSION=""
295FORCE_MINOR_VERSION=""
296
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800297# Path to the postinstall config file in target image if exists.
298POSTINSTALL_CONFIG_FILE=""
299
Yifan Hong398cb542018-10-18 11:29:40 -0700300# Path to the dynamic partition info file in target image if exists.
301DYNAMIC_PARTITION_INFO_FILE=""
302
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500303# Path to the META/apex_info.pb found in target build
304APEX_INFO_FILE=""
305
Alex Deymoc97df432015-09-25 17:23:52 -0700306# read_option_int <file.txt> <option_key> [default_value]
307#
308# Reads the unsigned integer value associated with |option_key| in a key=value
309# file |file.txt|. Prints the read value if found and valid, otherwise prints
310# the |default_value|.
311read_option_uint() {
312 local file_txt="$1"
313 local option_key="$2"
314 local default_value="${3:-}"
315 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700316 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700317 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
318 echo "${value}"
319 return
320 fi
321 fi
322 echo "${default_value}"
323}
324
Sen Jiangd0e9a892016-07-22 16:28:07 -0700325# truncate_file <file_path> <file_size>
326#
Dan Willemsen32711862018-10-04 21:25:50 -0700327# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700328# The truncate binary might not be available.
329truncate_file() {
330 local file_path="$1"
331 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700332 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700333}
334
Alex Deymo89ff9e32015-09-15 19:29:01 -0700335# Create a temporary file in the work_dir with an optional pattern name.
336# Prints the name of the newly created file.
337create_tempfile() {
338 local pattern="${1:-tempfile.XXXXXX}"
339 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
340}
Jason Kusumabe998f42015-09-03 15:53:13 -0700341
342cleanup() {
343 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700344 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700345
346 # If we are cleaning up after an error, or if we got an error during
347 # cleanup (even if we eventually succeeded) return a non-zero exit
348 # code. This triggers additional logging in most environments that call
349 # this script.
350 if [[ -n "${err}" ]]; then
351 die "Cleanup encountered an error."
352 fi
353}
354
355cleanup_on_error() {
356 trap - INT TERM ERR EXIT
357 cleanup
358 die "Cleanup success after an error."
359}
360
361cleanup_on_exit() {
362 trap - INT TERM ERR EXIT
363 cleanup
364}
365
366trap cleanup_on_error INT TERM ERR
367trap cleanup_on_exit EXIT
368
Tianjie Xu14715ce2019-08-06 17:24:43 -0700369# extract_file <zip_file> <entry_name> <destination>
370#
371# Extracts |entry_name| from |zip_file| to |destination|.
372extract_file() {
373 local zip_file="$1"
374 local entry_name="$2"
375 local destination="$3"
376
377 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
378 # as the destination of the unzip, and move the file to the intended
379 # destination.
380 local output_directory=$(
381 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
382 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
383 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
384
385 mv "${output_directory}/${entry_name}" "${destination}"
386 rm -rf "${output_directory}"
387}
Alex Deymo48b502a2015-09-17 19:00:18 -0700388
Sen Jiang788c2d92016-03-09 12:48:40 -0800389# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700390#
391# Detect the format of the |image| file and extract its updatable partitions
392# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800393# associative array passed in |partitions_array|. If |partitions_order| is
394# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700395extract_image() {
396 local image="$1"
397
398 # Brillo images are zip files. We detect the 4-byte magic header of the zip
399 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800400 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700401 if [[ "${magic}" == "504b0304" ]]; then
402 echo "Detected .zip file, extracting Brillo image."
403 extract_image_brillo "$@"
404 return
405 fi
406
407 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
408 # bundled here and we will use it to extract the partitions, so the GPT
409 # headers must be valid.
410 if cgpt show -q -n "${image}" >/dev/null; then
411 echo "Detected GPT image, extracting Chrome OS image."
412 extract_image_cros "$@"
413 return
414 fi
415
416 die "Couldn't detect the image format of ${image}"
417}
418
Sen Jiang788c2d92016-03-09 12:48:40 -0800419# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700420#
Alex Deymo48b502a2015-09-17 19:00:18 -0700421# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700422extract_image_cros() {
423 local image="$1"
424 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800425 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700426
427 local kernel root
428 kernel=$(create_tempfile "kernel.bin.XXXXXX")
429 CLEANUP_FILES+=("${kernel}")
430 root=$(create_tempfile "root.bin.XXXXXX")
431 CLEANUP_FILES+=("${root}")
432
433 cros_generate_update_payload --extract \
434 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700435 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700436
Amin Hassani58e01d62018-09-19 14:56:15 -0700437 # Chrome OS now uses major_version 2 payloads for all boards.
438 # See crbug.com/794404 for more information.
439 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700440
Tudor Brindusdda79e22018-06-28 18:03:21 -0700441 eval ${partitions_array}[kernel]=\""${kernel}"\"
442 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700443
Sen Jiang788c2d92016-03-09 12:48:40 -0800444 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700445 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800446 fi
447
Alex Deymo89ff9e32015-09-15 19:29:01 -0700448 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700449 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700450 varname="${partitions_array}[${part}]"
451 printf "md5sum of %s: " "${varname}"
452 md5sum "${!varname}"
453 done
454}
455
Sen Jiang3e5804d2018-09-06 15:53:00 -0700456# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
457# <part_file> <part_map_file>
458#
459# Extract the <partition> from target_files zip file into <part_file> and its
460# map file into <part_map_file>.
461extract_partition_brillo() {
462 local image="$1"
463 local partitions_array="$2"
464 local part="$3"
465 local part_file="$4"
466 local part_map_file="$5"
467
468 # For each partition, we in turn look for its image file under IMAGES/ and
469 # RADIO/ in the given target_files zip file.
470 local path path_in_zip
471 for path in IMAGES RADIO; do
472 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
473 path_in_zip="${path}"
474 break
475 fi
476 done
477 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700478 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700479
480 # If the partition is stored as an Android sparse image file, we need to
481 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800482 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700483 if [[ "${magic}" == "3aff26ed" ]]; then
484 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
485 echo "Converting Android sparse image ${part}.img to RAW."
486 mv "${part_file}" "${temp_sparse}"
487 simg2img "${temp_sparse}" "${part_file}"
488 rm -f "${temp_sparse}"
489 fi
490
491 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700492 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
493 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
494 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700495
496 # delta_generator only supports images multiple of 4 KiB. For target images
497 # we pad the data with zeros if needed, but for source images we truncate
498 # down the data since the last block of the old image could be padded on
499 # disk with unknown data.
500 local filesize=$(stat -c%s "${part_file}")
501 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
502 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
503 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
504 : $(( filesize = filesize & -4096 ))
505 else
506 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
507 : $(( filesize = (filesize + 4095) & -4096 ))
508 fi
509 truncate_file "${part_file}" "${filesize}"
510 fi
511
512 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
513}
514
Sen Jiang788c2d92016-03-09 12:48:40 -0800515# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700516#
517# Extract the A/B updated partitions from a Brillo target_files zip file into
518# new temporary files.
519extract_image_brillo() {
520 local image="$1"
521 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800522 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700523
Alex Deymo48b502a2015-09-17 19:00:18 -0700524 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800525 local ab_partitions_list
526 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
527 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700528 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
529 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800530 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
531 die "Invalid partition names found in the partition list."
532 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700533 # Get partition list without duplicates.
534 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800535 if [[ ${#partitions[@]} -eq 0 ]]; then
536 die "The list of partitions is empty. Can't generate a payload."
537 fi
538 else
539 warn "No ab_partitions.txt found. Using default."
540 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700541 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700542
Sen Jiang788c2d92016-03-09 12:48:40 -0800543 if [[ -n "${partitions_order}" ]]; then
544 eval "${partitions_order}=(${partitions[@]})"
545 fi
546
Alex Deymo83f2f702015-10-14 14:49:33 -0700547 # All Brillo updaters support major version 2.
548 FORCE_MAJOR_VERSION="2"
549
Alex Deymo48b502a2015-09-17 19:00:18 -0700550 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800551 # Source image
552 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700553 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700554 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
555 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
556 else
Alex Deymoc97df432015-09-25 17:23:52 -0700557 warn "No update_engine_config.txt found. Assuming pre-release image, \
558using payload minor version 2"
559 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700560 # For delta payloads, we use the major and minor version supported by the
561 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700562 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
563 "PAYLOAD_MINOR_VERSION" 2)
Tianjief4502bb2021-09-08 19:08:53 -0700564 if [[ -n "${FLAGS_force_minor_version}" ]]; then
565 FORCE_MINOR_VERSION="${FLAGS_force_minor_version}"
566 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700567 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
568 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800569
570 # Brillo support for deltas started with minor version 3.
571 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
572 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
573Disabling deltas for this source version."
574 exit ${EX_UNSUPPORTED_DELTA}
575 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800576 else
577 # Target image
578 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
579 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700580 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
581 extract_file "${image}" "META/postinstall_config.txt" \
582 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800583 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
584 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700585 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
586 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700587 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
588 extract_file "${image}" "META/dynamic_partitions_info.txt" \
589 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700590 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
591 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500592 local apex_info=$(create_tempfile "apex_info.XXXXXX")
593 CLEANUP_FILES+=("${apex_info}")
594 if unzip -l "${image}" "META/apex_info.pb" > /dev/null; then
595 extract_file "${image}" "META/apex_info.pb" \
596 "${apex_info}"
597 APEX_INFO_FILE="${apex_info}"
598 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700599 fi
600
Sen Jiang3e5804d2018-09-06 15:53:00 -0700601 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700602 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700603 local part_file=$(create_tempfile "${part}.img.XXXXXX")
604 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
605 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
606 # Extract partitions in background.
607 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
608 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700609 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700610 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800611 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700612 done
613}
614
615# cleanup_partition_array <partitions_array>
616#
617# Remove all empty files in <partitions_array>.
618cleanup_partition_array() {
619 local partitions_array="$1"
620 # Have to use eval to iterate over associative array keys with variable array
621 # names, we should change it to use nameref once bash 4.3 is available
622 # everywhere.
623 for part in $(eval "echo \${!${partitions_array}[@]}"); do
624 local path="${partitions_array}[$part]"
625 if [[ ! -s "${!path}" ]]; then
626 eval "unset ${partitions_array}[${part}]"
627 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700628 done
629}
630
Tudor Brindusb432db82018-06-29 13:13:27 -0700631extract_payload_images() {
632 local payload_type=$1
633 echo "Extracting images for ${payload_type} update."
634
635 if [[ "${payload_type}" == "delta" ]]; then
636 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
637 fi
638 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700639 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
640 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
641 # as it gives the status of the last process it has waited for.
642 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
643 wait ${pid}
644 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700645 cleanup_partition_array SRC_PARTITIONS
646 cleanup_partition_array SRC_PARTITIONS_MAP
647 cleanup_partition_array DST_PARTITIONS
648 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700649}
650
651get_payload_type() {
652 if [[ -z "${FLAGS_source_image}" ]]; then
653 echo "full"
654 else
655 echo "delta"
656 fi
657}
658
Jason Kusumabe998f42015-09-03 15:53:13 -0700659validate_generate() {
660 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700661 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700662
663 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700664 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700665}
666
667cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700668 local payload_type=$(get_payload_type)
669 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700670
Alex Deymo48b502a2015-09-17 19:00:18 -0700671 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800672 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700673 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800674
675 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800676 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800677 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800678 if [[ -n "${partition_names}" ]]; then
679 partition_names+=":"
680 new_partitions+=":"
681 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800682 new_mapfiles+=":"
683 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800684 fi
685 partition_names+="${part}"
686 new_partitions+="${DST_PARTITIONS[${part}]}"
Kelvin Zhang999705e2020-11-03 10:07:09 -0500687 if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then
688 # Skip boot partition.
689 old_partitions+=""
690 else
691 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
692 fi
Alex Deymo20bdc702016-12-07 21:07:11 -0800693 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
694 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800695 done
696
697 # Target image args:
698 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700699 --partition_names="${partition_names}"
700 --new_partitions="${new_partitions}"
701 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700702 )
703
Tianjief5baff42020-07-17 21:43:22 -0700704 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
705 GENERATOR_ARGS+=( --is_partial_update="true" )
706 # Need at least minor version 7 for partial update, so generate with minor
707 # version 7 if we don't have a source image. Let the delta_generator to fail
708 # the other incompatiable minor versions.
709 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
710 FORCE_MINOR_VERSION="7"
711 fi
712 fi
713
Alex Deymo89ff9e32015-09-15 19:29:01 -0700714 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800715 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700716 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700717 --old_partitions="${old_partitions}"
718 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700719 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700720 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
721 GENERATOR_ARGS+=(
722 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
723 fi
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500724 if [[ -n "${FLAGS_disable_verity_computation}" ]]; then
725 GENERATOR_ARGS+=(
726 --disable_verity_computation="${FLAGS_disable_verity_computation}" )
727 fi
Kelvin Zhang0aa4fae2021-10-28 09:15:27 -0700728 if [[ -n "${FLAGS_compressor_types}" ]]; then
729 GENERATOR_ARGS+=(
730 --compressor_types="${FLAGS_compressor_types}" )
731 fi
Kelvin Zhang22987982022-01-04 14:37:28 -0800732 if [[ -n "${FLAGS_enable_zucchini}" ]]; then
733 GENERATOR_ARGS+=(
734 --enable_zucchini="${FLAGS_enable_zucchini}" )
735 fi
Kelvin Zhang907b0e62022-01-10 06:23:10 -0800736 if [[ -n "${FLAGS_enable_lz4diff}" ]]; then
737 if [[ "${FLAGS_enable_lz4diff}" == "true" && -z "${FLAGS_liblz4_path}" ]]; then
738 echo "--liblz4_path is required when enable_lz4diff is set to true."
739 exit 1
740 fi
741 GENERATOR_ARGS+=(
742 --enable_lz4diff="${FLAGS_enable_lz4diff}" )
743 fi
Kelvin Zhang413982e2021-03-02 15:34:50 -0500744 fi
745
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500746 if [[ -n "${FLAGS_enable_vabc_xor}" ]]; then
747 GENERATOR_ARGS+=(
748 --enable_vabc_xor="${FLAGS_enable_vabc_xor}" )
749 fi
750
Kelvin Zhang413982e2021-03-02 15:34:50 -0500751 if [[ -n "${FLAGS_disable_vabc}" ]]; then
752 GENERATOR_ARGS+=(
753 --disable_vabc="${FLAGS_disable_vabc}" )
Alex Deymo48b502a2015-09-17 19:00:18 -0700754 fi
755
Tianjief5baff42020-07-17 21:43:22 -0700756 # minor version is set only for delta or partial payload.
757 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
758 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
759 fi
760
Alex Deymo48b502a2015-09-17 19:00:18 -0700761 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
762 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700763 fi
764
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700765 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
766 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
767 fi
768
Sen Jiang8e768e92017-06-28 17:13:19 -0700769 if [[ -n "${FLAGS_max_timestamp}" ]]; then
770 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
771 fi
772
Kelvin Zhang1f496422020-08-11 17:18:23 -0400773 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
774 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
775 fi
776
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800777 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
778 GENERATOR_ARGS+=(
779 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
780 )
781 fi
782
Yifan Hong398cb542018-10-18 11:29:40 -0700783 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
784 GENERATOR_ARGS+=(
785 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
786 )
787 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500788 if [[ -n "{APEX_INFO_FILE}" ]]; then
789 GENERATOR_ARGS+=(
790 --apex_info_file="${APEX_INFO_FILE}"
791 )
792 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700793
Jason Kusumabe998f42015-09-03 15:53:13 -0700794 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Kelvin Zhang907b0e62022-01-10 06:23:10 -0800795 if [[ -n "${FLAGS_enable_lz4diff}" ]]; then
796 LD_PRELOAD=${LD_PRELOAD}:${FLAGS_liblz4_path} "${GENERATOR}" "${GENERATOR_ARGS[@]}"
797 else
798 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
799 fi
Jason Kusumabe998f42015-09-03 15:53:13 -0700800
Alex Deymo89ff9e32015-09-15 19:29:01 -0700801 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700802}
803
804validate_hash() {
805 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700806 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700807
808 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700809 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700810--unsigned_payload FILENAME"
811
Jason Kusumabe998f42015-09-03 15:53:13 -0700812 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700813 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800814
815 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700816 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700817}
818
819cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700820 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700821 --in_file="${FLAGS_unsigned_payload}" \
822 --signature_size="${FLAGS_signature_size}" \
823 --out_hash_file="${FLAGS_payload_hash_file}" \
824 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700825
Jason Kusumabe998f42015-09-03 15:53:13 -0700826 echo "Done generating hash."
827}
828
829validate_sign() {
830 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700831 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700832
833 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700834 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700835--unsigned_payload FILENAME"
836
837 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700838 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700839
840 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700841 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700842--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700843
844 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700845 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700846--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700847}
848
849cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700850 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700851 --in_file="${FLAGS_unsigned_payload}"
852 --signature_size="${FLAGS_signature_size}"
853 --payload_signature_file="${FLAGS_payload_signature_file}"
854 --metadata_signature_file="${FLAGS_metadata_signature_file}"
855 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700856 )
857
858 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
859 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
860 fi
861
862 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700863 echo "Done signing payload."
864}
865
Alex Deymo98e691c2016-02-04 21:05:45 -0800866validate_properties() {
867 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700868 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800869
870 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700871 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800872}
873
874cmd_properties() {
875 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700876 --in_file="${FLAGS_payload}" \
877 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800878}
879
Tudor Brindusb432db82018-06-29 13:13:27 -0700880validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700881 [[ -n "${FLAGS_payload}" ]] ||
882 die "Error: you must specify an input filename with --payload FILENAME"
883
884 [[ -n "${FLAGS_target_image}" ]] ||
885 die "Error: you must specify a target image with --target_image FILENAME"
886}
887
888cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700889 local payload_type=$(get_payload_type)
890 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700891
892 declare -A TMP_PARTITIONS
893 for part in "${PARTITIONS_ORDER[@]}"; do
894 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
895 echo "Creating temporary target partition ${tmp_part} for ${part}"
896 CLEANUP_FILES+=("${tmp_part}")
897 TMP_PARTITIONS[${part}]=${tmp_part}
898 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
899 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
900 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
901 done
902
903 echo "Verifying ${payload_type} update."
904 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700905 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700906
907 local part old_partitions="" new_partitions="" partition_names=""
908 for part in "${PARTITIONS_ORDER[@]}"; do
909 if [[ -n "${partition_names}" ]]; then
910 partition_names+=":"
911 new_partitions+=":"
912 old_partitions+=":"
913 fi
914 partition_names+="${part}"
915 new_partitions+="${TMP_PARTITIONS[${part}]}"
916 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
917 done
918
919 # Target image args:
920 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700921 --partition_names="${partition_names}"
922 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700923 )
924
925 if [[ "${payload_type}" == "delta" ]]; then
926 # Source image args:
927 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700928 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700929 )
930 fi
931
Amin Hassania566cb62017-08-23 12:36:55 -0700932 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
933 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
934 fi
935
Amin Hassani13520932017-07-26 11:26:05 -0700936 echo "Running delta_generator to verify ${payload_type} payload with args: \
937${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700938 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700939
Sen Jiang6feb15c2018-08-31 15:45:17 -0700940 echo "Done applying ${payload_type} update."
941 echo "Checking the newly generated partitions against the target partitions"
942 local need_pause=false
943 for part in "${PARTITIONS_ORDER[@]}"; do
944 local not_str=""
945 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
946 not_str="in"
947 need_pause=true
948 fi
949 echo "The new partition (${part}) is ${not_str}valid."
950 done
951 # All images will be cleaned up when script exits, pause here to give a chance
952 # to inspect the images.
953 if [[ "$need_pause" == true ]]; then
954 read -n1 -r -s -p "Paused to investigate invalid partitions, \
955press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700956 fi
957}
958
Tudor Brindusb432db82018-06-29 13:13:27 -0700959cmd_check() {
960 local payload_type=$(get_payload_type)
961 extract_payload_images ${payload_type}
962
963 local part dst_partitions="" src_partitions=""
964 for part in "${PARTITIONS_ORDER[@]}"; do
965 if [[ -n "${dst_partitions}" ]]; then
966 dst_partitions+=" "
967 src_partitions+=" "
968 fi
969 dst_partitions+="${DST_PARTITIONS[${part}]}"
970 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
971 done
972
973 # Common payload args:
974 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
975 --part_names ${PARTITIONS_ORDER[@]} \
976 --dst_part_paths ${dst_partitions} )
977
978 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
979 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
980 fi
981
982 echo "Checking ${payload_type} update."
983 check_update_payload ${PAYCHECK_ARGS[@]} --check
984}
985
Tianjiee283ce42020-07-29 11:37:51 -0700986# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700987[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700988[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
989
990case "$COMMAND" in
991 generate) validate_generate
992 cmd_generate
993 ;;
994 hash) validate_hash
995 cmd_hash
996 ;;
997 sign) validate_sign
998 cmd_sign
999 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -08001000 properties) validate_properties
1001 cmd_properties
1002 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -07001003 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -07001004 cmd_verify
1005 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -07001006 check) validate_verify_and_check
1007 cmd_check
1008 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -07001009esac