blob: f36e995259739b06cd6bc04f56f91bad2c8ea917 [file] [log] [blame]
Jason Kusumabe998f42015-09-03 15:53:13 -07001#!/bin/bash
2
Amin Hassani13520932017-07-26 11:26:05 -07003#
4# Copyright (C) 2015 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
Jason Kusumabe998f42015-09-03 15:53:13 -070018
19# Script to generate a Brillo update for use by the update engine.
20#
21# usage: brillo_update_payload COMMAND [ARGS]
22# The following commands are supported:
23# generate generate an unsigned payload
24# hash generate a payload or metadata hash
25# sign generate a signed payload
Alex Deymo98e691c2016-02-04 21:05:45 -080026# properties generate a properties file from a payload
Amin Hassani13520932017-07-26 11:26:05 -070027# verify verify a payload by recreating a target image.
Tudor Brindusb432db82018-06-29 13:13:27 -070028# check verify a payload using paycheck (static testing)
Jason Kusumabe998f42015-09-03 15:53:13 -070029#
30# Generate command arguments:
Tianjie Xu72d512c2019-08-21 15:20:35 -070031# --payload generated unsigned payload output file
32# --source_image if defined, generate a delta payload from the
33# specified image to the target_image
34# --target_image the target image that should be sent to clients
35# --metadata_size_file if defined, generate a file containing the size
36# of the ayload metadata in bytes to the specified
37# file
38# --disable_fec_computation Disable the on device fec data computation for
39# incremental update. This feature is enabled by
40# default
Jason Kusumabe998f42015-09-03 15:53:13 -070041#
42# Hash command arguments:
43# --unsigned_payload the input unsigned payload to generate the hash from
44# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070045# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070046# --payload_hash_file if defined, generate a payload hash and output to the
47# specified file
48# --metadata_hash_file if defined, generate a metadata hash and output to the
49# specified file
50#
51# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070052# --unsigned_payload the input unsigned payload to insert the signatures
53# --payload the output signed payload
54# --signature_size signature sizes in bytes in the following format:
55# "size1:size2[:...]"
56# --payload_signature_file the payload signature files in the following
57# format:
58# "payload_signature1:payload_signature2[:...]"
59# --metadata_signature_file the metadata signature files in the following
60# format:
61# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070062# --metadata_size_file if defined, generate a file containing the size of
63# the signed payload metadata in bytes to the
64# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070065# Note that the number of signature sizes and payload signatures have to match.
Alex Deymo98e691c2016-02-04 21:05:45 -080066#
67# Properties command arguments:
68# --payload the input signed or unsigned payload
69# --properties_file the output path where to write the properties, or
70# '-' for stdout.
Amin Hassani13520932017-07-26 11:26:05 -070071# Verify command arguments:
72# --payload payload input file
73# --source_image verify payload to the specified source image.
74# --target_image the target image to verify upon.
Tudor Brindusb432db82018-06-29 13:13:27 -070075#
76# Check command arguments:
77# Symmetrical with the verify command.
Alex Deymo98e691c2016-02-04 21:05:45 -080078
Jason Kusumabe998f42015-09-03 15:53:13 -070079
Alex Deymo61e1fa82016-01-19 15:16:34 -080080# Exit codes:
81EX_UNSUPPORTED_DELTA=100
82
Jason Kusumaf514c542015-11-05 18:43:45 -080083warn() {
84 echo "brillo_update_payload: warning: $*" >&2
85}
86
Gilad Arnold957ce122015-10-14 16:02:55 -070087die() {
88 echo "brillo_update_payload: error: $*" >&2
89 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070090}
91
Parveen Kumarb31e1ac2020-10-16 15:30:09 -070092# Loads shflags. We first look at the default install location; then our own
93# directory; finally the parent directory.
Gilad Arnold957ce122015-10-14 16:02:55 -070094load_shflags() {
95 local my_dir="$(dirname "$(readlink -f "$0")")"
96 local path
Parveen Kumarb31e1ac2020-10-16 15:30:09 -070097 for path in /usr/share/misc \
98 "${my_dir}"/lib/shflags \
99 "${my_dir}"/../lib/shflags; do
Gilad Arnold957ce122015-10-14 16:02:55 -0700100 if [[ -r "${path}/shflags" ]]; then
101 . "${path}/shflags" || die "Could not load ${path}/shflags."
102 return
103 fi
104 done
105 die "Could not find shflags."
106}
107
108load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -0700109
Alex Deymoc64ffd52015-09-25 18:10:07 -0700110HELP_GENERATE="generate: Generate an unsigned update payload."
111HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
112for signing."
113HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -0800114HELP_PROPERTIES="properties: Extract payload properties to a file."
Tudor Brindusb432db82018-06-29 13:13:27 -0700115HELP_VERIFY="verify: Verify a (signed) update payload using delta_generator."
116HELP_CHECK="check: Check a (signed) update payload using paycheck (static \
117testing)."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700118
119usage() {
120 echo "Supported commands:"
121 echo
122 echo "${HELP_GENERATE}"
123 echo "${HELP_HASH}"
124 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800125 echo "${HELP_PROPERTIES}"
Amin Hassani13520932017-07-26 11:26:05 -0700126 echo "${HELP_VERIFY}"
Tudor Brindusb432db82018-06-29 13:13:27 -0700127 echo "${HELP_CHECK}"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700128 echo
129 echo "Use: \"$0 <command> --help\" for more options."
130}
131
132# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700133if [[ $# -lt 1 ]]; then
Tudor Brindusb432db82018-06-29 13:13:27 -0700134 echo "Please specify a command [generate|hash|sign|properties|verify|check]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700135 exit 1
136fi
137
Alex Deymoc64ffd52015-09-25 18:10:07 -0700138# Parse command.
139COMMAND="${1:-}"
140shift
141
142case "${COMMAND}" in
143 generate)
144 FLAGS_HELP="${HELP_GENERATE}"
145 ;;
146
147 hash)
148 FLAGS_HELP="${HELP_HASH}"
149 ;;
150
151 sign)
152 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700153 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800154
155 properties)
156 FLAGS_HELP="${HELP_PROPERTIES}"
157 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700158
159 verify)
160 FLAGS_HELP="${HELP_VERIFY}"
161 ;;
162
Tudor Brindusb432db82018-06-29 13:13:27 -0700163 check)
164 FLAGS_HELP="${HELP_CHECK}"
165 ;;
166
Jason Kusumabe998f42015-09-03 15:53:13 -0700167 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700168 echo "Unrecognized command: \"${COMMAND}\"" >&2
169 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700170 exit 1
171 ;;
172esac
173
Jason Kusumabe998f42015-09-03 15:53:13 -0700174# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700175FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
176${FLAGS_HELP}"
177
178if [[ "${COMMAND}" == "generate" ]]; then
179 DEFINE_string payload "" \
180 "Path to output the generated unsigned payload file."
181 DEFINE_string target_image "" \
182 "Path to the target image that should be sent to clients."
183 DEFINE_string source_image "" \
184 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700185 DEFINE_string metadata_size_file "" \
186 "Optional: Path to output metadata size."
Sen Jiang8e768e92017-06-28 17:13:19 -0700187 DEFINE_string max_timestamp "" \
188 "Optional: The maximum unix timestamp of the OS allowed to apply this \
189payload, should be set to a number higher than the build timestamp of the \
190system running on the device, 0 if not specified."
Kelvin Zhang1f496422020-08-11 17:18:23 -0400191 DEFINE_string partition_timestamps "" \
192 "Optional: Per-partition maximum unix timestamp of the OS allowed to \
193apply this payload. Should be a comma separated key value pairs. e.x.\
194system:1234,vendor:456"
Tianjie Xu72d512c2019-08-21 15:20:35 -0700195 DEFINE_string disable_fec_computation "" \
196 "Optional: Disables the on device fec data computation for incremental \
197update. This feature is enabled by default."
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500198 DEFINE_string disable_verity_computation "" \
199 "Optional: Disables the on device verity computation for incremental \
200update. This feature is enabled by default."
Tianjief5baff42020-07-17 21:43:22 -0700201 DEFINE_string is_partial_update "" \
202 "Optional: True if the payload is for partial update. i.e. it only updates \
203a subset of partitions on device."
Kelvin Zhangdde2ef42020-11-20 12:26:19 -0500204 DEFINE_string full_boot "" "Will include full boot image"
Kelvin Zhang9101ff32021-01-19 15:48:53 -0500205 DEFINE_string disable_vabc "" \
206 "Optional: Disables Virtual AB Compression when installing the OTA"
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500207 DEFINE_string enable_vabc_xor "" \
208 "Optional: Enable the use of Virtual AB Compression XOR feature"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700209fi
210if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
211 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
212 DEFINE_string signature_size "" \
213 "Signature sizes in bytes in the following format: size1:size2[:...]"
214fi
215if [[ "${COMMAND}" == "hash" ]]; then
216 DEFINE_string metadata_hash_file "" \
217 "Optional: Path to output metadata hash file."
218 DEFINE_string payload_hash_file "" \
219 "Optional: Path to output payload hash file."
220fi
221if [[ "${COMMAND}" == "sign" ]]; then
222 DEFINE_string payload "" \
223 "Path to output the generated unsigned payload file."
224 DEFINE_string metadata_signature_file "" \
225 "The metatada signatures in the following format: \
226metadata_signature1:metadata_signature2[:...]"
227 DEFINE_string payload_signature_file "" \
228 "The payload signatures in the following format: \
229payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700230 DEFINE_string metadata_size_file "" \
231 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700232fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800233if [[ "${COMMAND}" == "properties" ]]; then
234 DEFINE_string payload "" \
235 "Path to the input signed or unsigned payload file."
236 DEFINE_string properties_file "-" \
237 "Path to output the extracted property files. If '-' is passed stdout will \
238be used."
239fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700240if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700241 DEFINE_string payload "" \
242 "Path to the input payload file."
243 DEFINE_string target_image "" \
244 "Path to the target image to verify upon."
245 DEFINE_string source_image "" \
246 "Optional: Path to a source image. If specified, the delta update is \
247applied to this."
248fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800249
Alex Deymo5fbb1102017-01-12 13:55:52 -0800250DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700251
252# Parse command line flag arguments
253FLAGS "$@" || exit 1
254eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700255set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700256
Alex Deymo5fbb1102017-01-12 13:55:52 -0800257# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
258# ${TMPDIR}.
259TMPDIR="${FLAGS_work_dir}"
260export TMPDIR
261
Alex Deymo89ff9e32015-09-15 19:29:01 -0700262# Associative arrays from partition name to file in the source and target
263# images. The size of the updated area must be the size of the file.
264declare -A SRC_PARTITIONS
265declare -A DST_PARTITIONS
266
Alex Deymo20bdc702016-12-07 21:07:11 -0800267# Associative arrays for the .map files associated with each src/dst partition
268# file in SRC_PARTITIONS and DST_PARTITIONS.
269declare -A SRC_PARTITIONS_MAP
270declare -A DST_PARTITIONS_MAP
271
Sen Jiang788c2d92016-03-09 12:48:40 -0800272# List of partition names in order.
273declare -a PARTITIONS_ORDER
274
Tao Bao96489902019-04-02 16:25:03 -0700275# A list of PIDs of the extract_image workers.
276EXTRACT_IMAGE_PIDS=()
277
Alex Deymo89ff9e32015-09-15 19:29:01 -0700278# A list of temporary files to remove during cleanup.
279CLEANUP_FILES=()
280
Alex Deymo48b502a2015-09-17 19:00:18 -0700281# Global options to force the version of the payload.
282FORCE_MAJOR_VERSION=""
283FORCE_MINOR_VERSION=""
284
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800285# Path to the postinstall config file in target image if exists.
286POSTINSTALL_CONFIG_FILE=""
287
Yifan Hong398cb542018-10-18 11:29:40 -0700288# Path to the dynamic partition info file in target image if exists.
289DYNAMIC_PARTITION_INFO_FILE=""
290
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500291# Path to the META/apex_info.pb found in target build
292APEX_INFO_FILE=""
293
Alex Deymoc97df432015-09-25 17:23:52 -0700294# read_option_int <file.txt> <option_key> [default_value]
295#
296# Reads the unsigned integer value associated with |option_key| in a key=value
297# file |file.txt|. Prints the read value if found and valid, otherwise prints
298# the |default_value|.
299read_option_uint() {
300 local file_txt="$1"
301 local option_key="$2"
302 local default_value="${3:-}"
303 local value
Tao Baoc288d5b2019-10-03 13:47:06 -0700304 if value=$(grep "^${option_key}=" "${file_txt}" | tail -n 1); then
Alex Deymoc97df432015-09-25 17:23:52 -0700305 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
306 echo "${value}"
307 return
308 fi
309 fi
310 echo "${default_value}"
311}
312
Sen Jiangd0e9a892016-07-22 16:28:07 -0700313# truncate_file <file_path> <file_size>
314#
Dan Willemsen32711862018-10-04 21:25:50 -0700315# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700316# The truncate binary might not be available.
317truncate_file() {
318 local file_path="$1"
319 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700320 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700321}
322
Alex Deymo89ff9e32015-09-15 19:29:01 -0700323# Create a temporary file in the work_dir with an optional pattern name.
324# Prints the name of the newly created file.
325create_tempfile() {
326 local pattern="${1:-tempfile.XXXXXX}"
327 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
328}
Jason Kusumabe998f42015-09-03 15:53:13 -0700329
330cleanup() {
331 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700332 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700333
334 # If we are cleaning up after an error, or if we got an error during
335 # cleanup (even if we eventually succeeded) return a non-zero exit
336 # code. This triggers additional logging in most environments that call
337 # this script.
338 if [[ -n "${err}" ]]; then
339 die "Cleanup encountered an error."
340 fi
341}
342
343cleanup_on_error() {
344 trap - INT TERM ERR EXIT
345 cleanup
346 die "Cleanup success after an error."
347}
348
349cleanup_on_exit() {
350 trap - INT TERM ERR EXIT
351 cleanup
352}
353
354trap cleanup_on_error INT TERM ERR
355trap cleanup_on_exit EXIT
356
Tianjie Xu14715ce2019-08-06 17:24:43 -0700357# extract_file <zip_file> <entry_name> <destination>
358#
359# Extracts |entry_name| from |zip_file| to |destination|.
360extract_file() {
361 local zip_file="$1"
362 local entry_name="$2"
363 local destination="$3"
364
365 # unzip -p won't report error upon ENOSPC. Therefore, create a temp directory
366 # as the destination of the unzip, and move the file to the intended
367 # destination.
368 local output_directory=$(
369 mktemp --directory --tmpdir="${FLAGS_work_dir}" "TEMP.XXXXXX")
370 unzip "${zip_file}" "${entry_name}" -d "${output_directory}" ||
371 { rm -rf "${output_directory}"; die "Failed to extract ${entry_name}"; }
372
373 mv "${output_directory}/${entry_name}" "${destination}"
374 rm -rf "${output_directory}"
375}
Alex Deymo48b502a2015-09-17 19:00:18 -0700376
Sen Jiang788c2d92016-03-09 12:48:40 -0800377# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700378#
379# Detect the format of the |image| file and extract its updatable partitions
380# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800381# associative array passed in |partitions_array|. If |partitions_order| is
382# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700383extract_image() {
384 local image="$1"
385
386 # Brillo images are zip files. We detect the 4-byte magic header of the zip
387 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800388 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700389 if [[ "${magic}" == "504b0304" ]]; then
390 echo "Detected .zip file, extracting Brillo image."
391 extract_image_brillo "$@"
392 return
393 fi
394
395 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
396 # bundled here and we will use it to extract the partitions, so the GPT
397 # headers must be valid.
398 if cgpt show -q -n "${image}" >/dev/null; then
399 echo "Detected GPT image, extracting Chrome OS image."
400 extract_image_cros "$@"
401 return
402 fi
403
404 die "Couldn't detect the image format of ${image}"
405}
406
Sen Jiang788c2d92016-03-09 12:48:40 -0800407# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700408#
Alex Deymo48b502a2015-09-17 19:00:18 -0700409# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700410extract_image_cros() {
411 local image="$1"
412 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800413 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700414
415 local kernel root
416 kernel=$(create_tempfile "kernel.bin.XXXXXX")
417 CLEANUP_FILES+=("${kernel}")
418 root=$(create_tempfile "root.bin.XXXXXX")
419 CLEANUP_FILES+=("${root}")
420
421 cros_generate_update_payload --extract \
422 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700423 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700424
Amin Hassani58e01d62018-09-19 14:56:15 -0700425 # Chrome OS now uses major_version 2 payloads for all boards.
426 # See crbug.com/794404 for more information.
427 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700428
Tudor Brindusdda79e22018-06-28 18:03:21 -0700429 eval ${partitions_array}[kernel]=\""${kernel}"\"
430 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700431
Sen Jiang788c2d92016-03-09 12:48:40 -0800432 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700433 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800434 fi
435
Alex Deymo89ff9e32015-09-15 19:29:01 -0700436 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700437 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700438 varname="${partitions_array}[${part}]"
439 printf "md5sum of %s: " "${varname}"
440 md5sum "${!varname}"
441 done
442}
443
Sen Jiang3e5804d2018-09-06 15:53:00 -0700444# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
445# <part_file> <part_map_file>
446#
447# Extract the <partition> from target_files zip file into <part_file> and its
448# map file into <part_map_file>.
449extract_partition_brillo() {
450 local image="$1"
451 local partitions_array="$2"
452 local part="$3"
453 local part_file="$4"
454 local part_map_file="$5"
455
456 # For each partition, we in turn look for its image file under IMAGES/ and
457 # RADIO/ in the given target_files zip file.
458 local path path_in_zip
459 for path in IMAGES RADIO; do
460 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
461 path_in_zip="${path}"
462 break
463 fi
464 done
465 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
Tianjie Xu14715ce2019-08-06 17:24:43 -0700466 extract_file "${image}" "${path_in_zip}/${part}.img" "${part_file}"
Sen Jiang3e5804d2018-09-06 15:53:00 -0700467
468 # If the partition is stored as an Android sparse image file, we need to
469 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800470 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700471 if [[ "${magic}" == "3aff26ed" ]]; then
472 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
473 echo "Converting Android sparse image ${part}.img to RAW."
474 mv "${part_file}" "${temp_sparse}"
475 simg2img "${temp_sparse}" "${part_file}"
476 rm -f "${temp_sparse}"
477 fi
478
479 # Extract the .map file (if one is available).
Tianjie Xu14715ce2019-08-06 17:24:43 -0700480 if unzip -l "${image}" "${path_in_zip}/${part}.map" > /dev/null; then
481 extract_file "${image}" "${path_in_zip}/${part}.map" "${part_map_file}"
482 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700483
484 # delta_generator only supports images multiple of 4 KiB. For target images
485 # we pad the data with zeros if needed, but for source images we truncate
486 # down the data since the last block of the old image could be padded on
487 # disk with unknown data.
488 local filesize=$(stat -c%s "${part_file}")
489 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
490 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
491 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
492 : $(( filesize = filesize & -4096 ))
493 else
494 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
495 : $(( filesize = (filesize + 4095) & -4096 ))
496 fi
497 truncate_file "${part_file}" "${filesize}"
498 fi
499
500 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
501}
502
Sen Jiang788c2d92016-03-09 12:48:40 -0800503# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700504#
505# Extract the A/B updated partitions from a Brillo target_files zip file into
506# new temporary files.
507extract_image_brillo() {
508 local image="$1"
509 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800510 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700511
Alex Deymo48b502a2015-09-17 19:00:18 -0700512 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800513 local ab_partitions_list
514 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
515 CLEANUP_FILES+=("${ab_partitions_list}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700516 if unzip -l "${image}" "META/ab_partitions.txt" > /dev/null; then
517 extract_file "${image}" "META/ab_partitions.txt" "${ab_partitions_list}"
Alex Deymo168b5352015-11-04 13:51:52 -0800518 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
519 die "Invalid partition names found in the partition list."
520 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700521 # Get partition list without duplicates.
522 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800523 if [[ ${#partitions[@]} -eq 0 ]]; then
524 die "The list of partitions is empty. Can't generate a payload."
525 fi
526 else
527 warn "No ab_partitions.txt found. Using default."
528 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700529 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700530
Sen Jiang788c2d92016-03-09 12:48:40 -0800531 if [[ -n "${partitions_order}" ]]; then
532 eval "${partitions_order}=(${partitions[@]})"
533 fi
534
Alex Deymo83f2f702015-10-14 14:49:33 -0700535 # All Brillo updaters support major version 2.
536 FORCE_MAJOR_VERSION="2"
537
Alex Deymo48b502a2015-09-17 19:00:18 -0700538 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800539 # Source image
540 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700541 CLEANUP_FILES+=("${ue_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700542 if unzip -l "${image}" "META/update_engine_config.txt" > /dev/null; then
543 extract_file "${image}" "META/update_engine_config.txt" "${ue_config}"
544 else
Alex Deymoc97df432015-09-25 17:23:52 -0700545 warn "No update_engine_config.txt found. Assuming pre-release image, \
546using payload minor version 2"
547 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700548 # For delta payloads, we use the major and minor version supported by the
549 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700550 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
551 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700552 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
553 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800554
555 # Brillo support for deltas started with minor version 3.
556 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
557 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
558Disabling deltas for this source version."
559 exit ${EX_UNSUPPORTED_DELTA}
560 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800561 else
562 # Target image
563 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
564 CLEANUP_FILES+=("${postinstall_config}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700565 if unzip -l "${image}" "META/postinstall_config.txt" > /dev/null; then
566 extract_file "${image}" "META/postinstall_config.txt" \
567 "${postinstall_config}"
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800568 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
569 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700570 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
571 CLEANUP_FILES+=("${dynamic_partitions_info}")
Tianjie Xu14715ce2019-08-06 17:24:43 -0700572 if unzip -l "${image}" "META/dynamic_partitions_info.txt" > /dev/null; then
573 extract_file "${image}" "META/dynamic_partitions_info.txt" \
574 "${dynamic_partitions_info}"
Yifan Hong398cb542018-10-18 11:29:40 -0700575 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
576 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500577 local apex_info=$(create_tempfile "apex_info.XXXXXX")
578 CLEANUP_FILES+=("${apex_info}")
579 if unzip -l "${image}" "META/apex_info.pb" > /dev/null; then
580 extract_file "${image}" "META/apex_info.pb" \
581 "${apex_info}"
582 APEX_INFO_FILE="${apex_info}"
583 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700584 fi
585
Sen Jiang3e5804d2018-09-06 15:53:00 -0700586 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700587 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700588 local part_file=$(create_tempfile "${part}.img.XXXXXX")
589 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
590 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
591 # Extract partitions in background.
592 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
593 "${part_file}" "${part_map_file}" &
Tao Bao96489902019-04-02 16:25:03 -0700594 EXTRACT_IMAGE_PIDS+=("$!")
Alex Deymo48b502a2015-09-17 19:00:18 -0700595 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800596 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700597 done
598}
599
600# cleanup_partition_array <partitions_array>
601#
602# Remove all empty files in <partitions_array>.
603cleanup_partition_array() {
604 local partitions_array="$1"
605 # Have to use eval to iterate over associative array keys with variable array
606 # names, we should change it to use nameref once bash 4.3 is available
607 # everywhere.
608 for part in $(eval "echo \${!${partitions_array}[@]}"); do
609 local path="${partitions_array}[$part]"
610 if [[ ! -s "${!path}" ]]; then
611 eval "unset ${partitions_array}[${part}]"
612 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700613 done
614}
615
Tudor Brindusb432db82018-06-29 13:13:27 -0700616extract_payload_images() {
617 local payload_type=$1
618 echo "Extracting images for ${payload_type} update."
619
620 if [[ "${payload_type}" == "delta" ]]; then
621 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
622 fi
623 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Tao Bao96489902019-04-02 16:25:03 -0700624 # Wait for all subprocesses to finish. Not using `wait` since it doesn't die
625 # on non-zero subprocess exit code. Not using `wait ${EXTRACT_IMAGE_PIDS[@]}`
626 # as it gives the status of the last process it has waited for.
627 for pid in ${EXTRACT_IMAGE_PIDS[@]}; do
628 wait ${pid}
629 done
Sen Jiang3e5804d2018-09-06 15:53:00 -0700630 cleanup_partition_array SRC_PARTITIONS
631 cleanup_partition_array SRC_PARTITIONS_MAP
632 cleanup_partition_array DST_PARTITIONS
633 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700634}
635
636get_payload_type() {
637 if [[ -z "${FLAGS_source_image}" ]]; then
638 echo "full"
639 else
640 echo "delta"
641 fi
642}
643
Jason Kusumabe998f42015-09-03 15:53:13 -0700644validate_generate() {
645 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700646 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700647
648 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700649 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700650}
651
652cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700653 local payload_type=$(get_payload_type)
654 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700655
Alex Deymo48b502a2015-09-17 19:00:18 -0700656 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800657 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700658 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800659
660 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800661 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800662 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800663 if [[ -n "${partition_names}" ]]; then
664 partition_names+=":"
665 new_partitions+=":"
666 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800667 new_mapfiles+=":"
668 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800669 fi
670 partition_names+="${part}"
671 new_partitions+="${DST_PARTITIONS[${part}]}"
Kelvin Zhang999705e2020-11-03 10:07:09 -0500672 if [ "${FLAGS_full_boot}" == "true" ] && [ "${part}" == "boot" ]; then
673 # Skip boot partition.
674 old_partitions+=""
675 else
676 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
677 fi
Alex Deymo20bdc702016-12-07 21:07:11 -0800678 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
679 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800680 done
681
682 # Target image args:
683 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700684 --partition_names="${partition_names}"
685 --new_partitions="${new_partitions}"
686 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700687 )
688
Tianjief5baff42020-07-17 21:43:22 -0700689 if [[ "${FLAGS_is_partial_update}" == "true" ]]; then
690 GENERATOR_ARGS+=( --is_partial_update="true" )
691 # Need at least minor version 7 for partial update, so generate with minor
692 # version 7 if we don't have a source image. Let the delta_generator to fail
693 # the other incompatiable minor versions.
694 if [[ -z "${FORCE_MINOR_VERSION}" ]]; then
695 FORCE_MINOR_VERSION="7"
696 fi
697 fi
698
Alex Deymo89ff9e32015-09-15 19:29:01 -0700699 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800700 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700701 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700702 --old_partitions="${old_partitions}"
703 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700704 )
Tianjie Xu72d512c2019-08-21 15:20:35 -0700705 if [[ -n "${FLAGS_disable_fec_computation}" ]]; then
706 GENERATOR_ARGS+=(
707 --disable_fec_computation="${FLAGS_disable_fec_computation}" )
708 fi
Kelvin Zhang098e79a2020-11-19 17:40:56 -0500709 if [[ -n "${FLAGS_disable_verity_computation}" ]]; then
710 GENERATOR_ARGS+=(
711 --disable_verity_computation="${FLAGS_disable_verity_computation}" )
712 fi
Kelvin Zhang413982e2021-03-02 15:34:50 -0500713 fi
714
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500715 if [[ -n "${FLAGS_enable_vabc_xor}" ]]; then
716 GENERATOR_ARGS+=(
717 --enable_vabc_xor="${FLAGS_enable_vabc_xor}" )
718 fi
719
Kelvin Zhang413982e2021-03-02 15:34:50 -0500720 if [[ -n "${FLAGS_disable_vabc}" ]]; then
721 GENERATOR_ARGS+=(
722 --disable_vabc="${FLAGS_disable_vabc}" )
Alex Deymo48b502a2015-09-17 19:00:18 -0700723 fi
724
Tianjief5baff42020-07-17 21:43:22 -0700725 # minor version is set only for delta or partial payload.
726 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
727 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
728 fi
729
Alex Deymo48b502a2015-09-17 19:00:18 -0700730 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
731 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700732 fi
733
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700734 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
735 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
736 fi
737
Sen Jiang8e768e92017-06-28 17:13:19 -0700738 if [[ -n "${FLAGS_max_timestamp}" ]]; then
739 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
740 fi
741
Kelvin Zhang1f496422020-08-11 17:18:23 -0400742 if [[ -n "${FLAGS_partition_timestamps}" ]]; then
743 GENERATOR_ARGS+=( --partition_timestamps="${FLAGS_partition_timestamps}" )
744 fi
745
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800746 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
747 GENERATOR_ARGS+=(
748 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
749 )
750 fi
751
Yifan Hong398cb542018-10-18 11:29:40 -0700752 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
753 GENERATOR_ARGS+=(
754 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
755 )
756 fi
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500757 if [[ -n "{APEX_INFO_FILE}" ]]; then
758 GENERATOR_ARGS+=(
759 --apex_info_file="${APEX_INFO_FILE}"
760 )
761 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700762
Jason Kusumabe998f42015-09-03 15:53:13 -0700763 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700764 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700765
Alex Deymo89ff9e32015-09-15 19:29:01 -0700766 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700767}
768
769validate_hash() {
770 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700771 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700772
773 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700774 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700775--unsigned_payload FILENAME"
776
Jason Kusumabe998f42015-09-03 15:53:13 -0700777 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700778 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800779
780 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700781 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700782}
783
784cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700785 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700786 --in_file="${FLAGS_unsigned_payload}" \
787 --signature_size="${FLAGS_signature_size}" \
788 --out_hash_file="${FLAGS_payload_hash_file}" \
789 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700790
Jason Kusumabe998f42015-09-03 15:53:13 -0700791 echo "Done generating hash."
792}
793
794validate_sign() {
795 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700796 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700797
798 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700799 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700800--unsigned_payload FILENAME"
801
802 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700803 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700804
805 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700806 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700807--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700808
809 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700810 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700811--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700812}
813
814cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700815 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700816 --in_file="${FLAGS_unsigned_payload}"
817 --signature_size="${FLAGS_signature_size}"
818 --payload_signature_file="${FLAGS_payload_signature_file}"
819 --metadata_signature_file="${FLAGS_metadata_signature_file}"
820 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700821 )
822
823 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
824 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
825 fi
826
827 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700828 echo "Done signing payload."
829}
830
Alex Deymo98e691c2016-02-04 21:05:45 -0800831validate_properties() {
832 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700833 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800834
835 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700836 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800837}
838
839cmd_properties() {
840 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700841 --in_file="${FLAGS_payload}" \
842 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800843}
844
Tudor Brindusb432db82018-06-29 13:13:27 -0700845validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700846 [[ -n "${FLAGS_payload}" ]] ||
847 die "Error: you must specify an input filename with --payload FILENAME"
848
849 [[ -n "${FLAGS_target_image}" ]] ||
850 die "Error: you must specify a target image with --target_image FILENAME"
851}
852
853cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700854 local payload_type=$(get_payload_type)
855 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700856
857 declare -A TMP_PARTITIONS
858 for part in "${PARTITIONS_ORDER[@]}"; do
859 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
860 echo "Creating temporary target partition ${tmp_part} for ${part}"
861 CLEANUP_FILES+=("${tmp_part}")
862 TMP_PARTITIONS[${part}]=${tmp_part}
863 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
864 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
865 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
866 done
867
868 echo "Verifying ${payload_type} update."
869 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700870 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700871
872 local part old_partitions="" new_partitions="" partition_names=""
873 for part in "${PARTITIONS_ORDER[@]}"; do
874 if [[ -n "${partition_names}" ]]; then
875 partition_names+=":"
876 new_partitions+=":"
877 old_partitions+=":"
878 fi
879 partition_names+="${part}"
880 new_partitions+="${TMP_PARTITIONS[${part}]}"
881 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
882 done
883
884 # Target image args:
885 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700886 --partition_names="${partition_names}"
887 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700888 )
889
890 if [[ "${payload_type}" == "delta" ]]; then
891 # Source image args:
892 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700893 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700894 )
895 fi
896
Amin Hassania566cb62017-08-23 12:36:55 -0700897 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
898 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
899 fi
900
Amin Hassani13520932017-07-26 11:26:05 -0700901 echo "Running delta_generator to verify ${payload_type} payload with args: \
902${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700903 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700904
Sen Jiang6feb15c2018-08-31 15:45:17 -0700905 echo "Done applying ${payload_type} update."
906 echo "Checking the newly generated partitions against the target partitions"
907 local need_pause=false
908 for part in "${PARTITIONS_ORDER[@]}"; do
909 local not_str=""
910 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
911 not_str="in"
912 need_pause=true
913 fi
914 echo "The new partition (${part}) is ${not_str}valid."
915 done
916 # All images will be cleaned up when script exits, pause here to give a chance
917 # to inspect the images.
918 if [[ "$need_pause" == true ]]; then
919 read -n1 -r -s -p "Paused to investigate invalid partitions, \
920press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700921 fi
922}
923
Tudor Brindusb432db82018-06-29 13:13:27 -0700924cmd_check() {
925 local payload_type=$(get_payload_type)
926 extract_payload_images ${payload_type}
927
928 local part dst_partitions="" src_partitions=""
929 for part in "${PARTITIONS_ORDER[@]}"; do
930 if [[ -n "${dst_partitions}" ]]; then
931 dst_partitions+=" "
932 src_partitions+=" "
933 fi
934 dst_partitions+="${DST_PARTITIONS[${part}]}"
935 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
936 done
937
938 # Common payload args:
939 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
940 --part_names ${PARTITIONS_ORDER[@]} \
941 --dst_part_paths ${dst_partitions} )
942
943 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
944 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
945 fi
946
947 echo "Checking ${payload_type} update."
948 check_update_payload ${PAYCHECK_ARGS[@]} --check
949}
950
Tianjiee283ce42020-07-29 11:37:51 -0700951# Check that the real generator exists:
Yifan Hong3756c3e2020-07-24 20:25:51 -0700952[[ -x "${GENERATOR}" ]] || GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700953[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
954
955case "$COMMAND" in
956 generate) validate_generate
957 cmd_generate
958 ;;
959 hash) validate_hash
960 cmd_hash
961 ;;
962 sign) validate_sign
963 cmd_sign
964 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800965 properties) validate_properties
966 cmd_properties
967 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700968 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700969 cmd_verify
970 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700971 check) validate_verify_and_check
972 cmd_check
973 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700974esac