blob: c88709cef4def369c00cca47f443067f39da7a25 [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:
Jason Kusuma9a4cae22015-10-08 18:17:57 -070031# --payload generated unsigned payload output file
32# --source_image if defined, generate a delta payload from the specified
33# image to the target_image
34# --target_image the target image that should be sent to clients
Amin Hassani13520932017-07-26 11:26:05 -070035# --metadata_size_file if defined, generate a file containing the size of the
36# payload metadata in bytes to the specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070037#
38# Hash command arguments:
39# --unsigned_payload the input unsigned payload to generate the hash from
40# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070041# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070042# --payload_hash_file if defined, generate a payload hash and output to the
43# specified file
44# --metadata_hash_file if defined, generate a metadata hash and output to the
45# specified file
46#
47# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070048# --unsigned_payload the input unsigned payload to insert the signatures
49# --payload the output signed payload
50# --signature_size signature sizes in bytes in the following format:
51# "size1:size2[:...]"
52# --payload_signature_file the payload signature files in the following
53# format:
54# "payload_signature1:payload_signature2[:...]"
55# --metadata_signature_file the metadata signature files in the following
56# format:
57# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070058# --metadata_size_file if defined, generate a file containing the size of
59# the signed payload metadata in bytes to the
60# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070061# Note that the number of signature sizes and payload signatures have to match.
Alex Deymo98e691c2016-02-04 21:05:45 -080062#
63# Properties command arguments:
64# --payload the input signed or unsigned payload
65# --properties_file the output path where to write the properties, or
66# '-' for stdout.
Amin Hassani13520932017-07-26 11:26:05 -070067# Verify command arguments:
68# --payload payload input file
69# --source_image verify payload to the specified source image.
70# --target_image the target image to verify upon.
Tudor Brindusb432db82018-06-29 13:13:27 -070071#
72# Check command arguments:
73# Symmetrical with the verify command.
Alex Deymo98e691c2016-02-04 21:05:45 -080074
Jason Kusumabe998f42015-09-03 15:53:13 -070075
Alex Deymo61e1fa82016-01-19 15:16:34 -080076# Exit codes:
77EX_UNSUPPORTED_DELTA=100
78
Jason Kusumaf514c542015-11-05 18:43:45 -080079warn() {
80 echo "brillo_update_payload: warning: $*" >&2
81}
82
Gilad Arnold957ce122015-10-14 16:02:55 -070083die() {
84 echo "brillo_update_payload: error: $*" >&2
85 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070086}
87
Gilad Arnold957ce122015-10-14 16:02:55 -070088# Loads shflags. We first look at the default install location; then look for
Amin Hassani2a14d412018-05-31 13:01:09 -070089# crosutils (chroot); finally check our own directory.
Gilad Arnold957ce122015-10-14 16:02:55 -070090load_shflags() {
91 local my_dir="$(dirname "$(readlink -f "$0")")"
92 local path
Mike Frysinger26bb8652018-10-12 02:15:09 -040093 for path in /usr/share/misc "${my_dir}"/lib/shflags; do
Gilad Arnold957ce122015-10-14 16:02:55 -070094 if [[ -r "${path}/shflags" ]]; then
95 . "${path}/shflags" || die "Could not load ${path}/shflags."
96 return
97 fi
98 done
99 die "Could not find shflags."
100}
101
102load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -0700103
Alex Deymoc64ffd52015-09-25 18:10:07 -0700104HELP_GENERATE="generate: Generate an unsigned update payload."
105HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
106for signing."
107HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -0800108HELP_PROPERTIES="properties: Extract payload properties to a file."
Tudor Brindusb432db82018-06-29 13:13:27 -0700109HELP_VERIFY="verify: Verify a (signed) update payload using delta_generator."
110HELP_CHECK="check: Check a (signed) update payload using paycheck (static \
111testing)."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700112
113usage() {
114 echo "Supported commands:"
115 echo
116 echo "${HELP_GENERATE}"
117 echo "${HELP_HASH}"
118 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800119 echo "${HELP_PROPERTIES}"
Amin Hassani13520932017-07-26 11:26:05 -0700120 echo "${HELP_VERIFY}"
Tudor Brindusb432db82018-06-29 13:13:27 -0700121 echo "${HELP_CHECK}"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700122 echo
123 echo "Use: \"$0 <command> --help\" for more options."
124}
125
126# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700127if [[ $# -lt 1 ]]; then
Tudor Brindusb432db82018-06-29 13:13:27 -0700128 echo "Please specify a command [generate|hash|sign|properties|verify|check]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700129 exit 1
130fi
131
Alex Deymoc64ffd52015-09-25 18:10:07 -0700132# Parse command.
133COMMAND="${1:-}"
134shift
135
136case "${COMMAND}" in
137 generate)
138 FLAGS_HELP="${HELP_GENERATE}"
139 ;;
140
141 hash)
142 FLAGS_HELP="${HELP_HASH}"
143 ;;
144
145 sign)
146 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700147 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800148
149 properties)
150 FLAGS_HELP="${HELP_PROPERTIES}"
151 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700152
153 verify)
154 FLAGS_HELP="${HELP_VERIFY}"
155 ;;
156
Tudor Brindusb432db82018-06-29 13:13:27 -0700157 check)
158 FLAGS_HELP="${HELP_CHECK}"
159 ;;
160
Jason Kusumabe998f42015-09-03 15:53:13 -0700161 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700162 echo "Unrecognized command: \"${COMMAND}\"" >&2
163 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700164 exit 1
165 ;;
166esac
167
Jason Kusumabe998f42015-09-03 15:53:13 -0700168# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700169FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
170${FLAGS_HELP}"
171
172if [[ "${COMMAND}" == "generate" ]]; then
173 DEFINE_string payload "" \
174 "Path to output the generated unsigned payload file."
175 DEFINE_string target_image "" \
176 "Path to the target image that should be sent to clients."
177 DEFINE_string source_image "" \
178 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700179 DEFINE_string metadata_size_file "" \
180 "Optional: Path to output metadata size."
Sen Jiang8e768e92017-06-28 17:13:19 -0700181 DEFINE_string max_timestamp "" \
182 "Optional: The maximum unix timestamp of the OS allowed to apply this \
183payload, should be set to a number higher than the build timestamp of the \
184system running on the device, 0 if not specified."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700185fi
186if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
187 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
188 DEFINE_string signature_size "" \
189 "Signature sizes in bytes in the following format: size1:size2[:...]"
190fi
191if [[ "${COMMAND}" == "hash" ]]; then
192 DEFINE_string metadata_hash_file "" \
193 "Optional: Path to output metadata hash file."
194 DEFINE_string payload_hash_file "" \
195 "Optional: Path to output payload hash file."
196fi
197if [[ "${COMMAND}" == "sign" ]]; then
198 DEFINE_string payload "" \
199 "Path to output the generated unsigned payload file."
200 DEFINE_string metadata_signature_file "" \
201 "The metatada signatures in the following format: \
202metadata_signature1:metadata_signature2[:...]"
203 DEFINE_string payload_signature_file "" \
204 "The payload signatures in the following format: \
205payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700206 DEFINE_string metadata_size_file "" \
207 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700208fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800209if [[ "${COMMAND}" == "properties" ]]; then
210 DEFINE_string payload "" \
211 "Path to the input signed or unsigned payload file."
212 DEFINE_string properties_file "-" \
213 "Path to output the extracted property files. If '-' is passed stdout will \
214be used."
215fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700216if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700217 DEFINE_string payload "" \
218 "Path to the input payload file."
219 DEFINE_string target_image "" \
220 "Path to the target image to verify upon."
221 DEFINE_string source_image "" \
222 "Optional: Path to a source image. If specified, the delta update is \
223applied to this."
224fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800225
Alex Deymo5fbb1102017-01-12 13:55:52 -0800226DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700227
228# Parse command line flag arguments
229FLAGS "$@" || exit 1
230eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700231set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700232
Alex Deymo5fbb1102017-01-12 13:55:52 -0800233# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
234# ${TMPDIR}.
235TMPDIR="${FLAGS_work_dir}"
236export TMPDIR
237
Alex Deymo89ff9e32015-09-15 19:29:01 -0700238# Associative arrays from partition name to file in the source and target
239# images. The size of the updated area must be the size of the file.
240declare -A SRC_PARTITIONS
241declare -A DST_PARTITIONS
242
Alex Deymo20bdc702016-12-07 21:07:11 -0800243# Associative arrays for the .map files associated with each src/dst partition
244# file in SRC_PARTITIONS and DST_PARTITIONS.
245declare -A SRC_PARTITIONS_MAP
246declare -A DST_PARTITIONS_MAP
247
Sen Jiang788c2d92016-03-09 12:48:40 -0800248# List of partition names in order.
249declare -a PARTITIONS_ORDER
250
Alex Deymo89ff9e32015-09-15 19:29:01 -0700251# A list of temporary files to remove during cleanup.
252CLEANUP_FILES=()
253
Alex Deymo48b502a2015-09-17 19:00:18 -0700254# Global options to force the version of the payload.
255FORCE_MAJOR_VERSION=""
256FORCE_MINOR_VERSION=""
257
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800258# Path to the postinstall config file in target image if exists.
259POSTINSTALL_CONFIG_FILE=""
260
Yifan Hong398cb542018-10-18 11:29:40 -0700261# Path to the dynamic partition info file in target image if exists.
262DYNAMIC_PARTITION_INFO_FILE=""
263
Alex Deymoc97df432015-09-25 17:23:52 -0700264# read_option_int <file.txt> <option_key> [default_value]
265#
266# Reads the unsigned integer value associated with |option_key| in a key=value
267# file |file.txt|. Prints the read value if found and valid, otherwise prints
268# the |default_value|.
269read_option_uint() {
270 local file_txt="$1"
271 local option_key="$2"
272 local default_value="${3:-}"
273 local value
274 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
275 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
276 echo "${value}"
277 return
278 fi
279 fi
280 echo "${default_value}"
281}
282
Sen Jiangd0e9a892016-07-22 16:28:07 -0700283# truncate_file <file_path> <file_size>
284#
Dan Willemsen32711862018-10-04 21:25:50 -0700285# Truncate the given |file_path| to |file_size| using python.
Sen Jiangd0e9a892016-07-22 16:28:07 -0700286# The truncate binary might not be available.
287truncate_file() {
288 local file_path="$1"
289 local file_size="$2"
Dan Willemsen32711862018-10-04 21:25:50 -0700290 python -c "open(\"${file_path}\", 'a').truncate(${file_size})"
Sen Jiangd0e9a892016-07-22 16:28:07 -0700291}
292
Alex Deymo89ff9e32015-09-15 19:29:01 -0700293# Create a temporary file in the work_dir with an optional pattern name.
294# Prints the name of the newly created file.
295create_tempfile() {
296 local pattern="${1:-tempfile.XXXXXX}"
297 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
298}
Jason Kusumabe998f42015-09-03 15:53:13 -0700299
300cleanup() {
301 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700302 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700303
304 # If we are cleaning up after an error, or if we got an error during
305 # cleanup (even if we eventually succeeded) return a non-zero exit
306 # code. This triggers additional logging in most environments that call
307 # this script.
308 if [[ -n "${err}" ]]; then
309 die "Cleanup encountered an error."
310 fi
311}
312
313cleanup_on_error() {
314 trap - INT TERM ERR EXIT
315 cleanup
316 die "Cleanup success after an error."
317}
318
319cleanup_on_exit() {
320 trap - INT TERM ERR EXIT
321 cleanup
322}
323
324trap cleanup_on_error INT TERM ERR
325trap cleanup_on_exit EXIT
326
Alex Deymo48b502a2015-09-17 19:00:18 -0700327
Sen Jiang788c2d92016-03-09 12:48:40 -0800328# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700329#
330# Detect the format of the |image| file and extract its updatable partitions
331# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800332# associative array passed in |partitions_array|. If |partitions_order| is
333# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700334extract_image() {
335 local image="$1"
336
337 # Brillo images are zip files. We detect the 4-byte magic header of the zip
338 # file.
Elliott Hughes5df503c2018-11-27 16:57:34 -0800339 local magic=$(xxd -p -l4 "${image}")
Alex Deymo48b502a2015-09-17 19:00:18 -0700340 if [[ "${magic}" == "504b0304" ]]; then
341 echo "Detected .zip file, extracting Brillo image."
342 extract_image_brillo "$@"
343 return
344 fi
345
346 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
347 # bundled here and we will use it to extract the partitions, so the GPT
348 # headers must be valid.
349 if cgpt show -q -n "${image}" >/dev/null; then
350 echo "Detected GPT image, extracting Chrome OS image."
351 extract_image_cros "$@"
352 return
353 fi
354
355 die "Couldn't detect the image format of ${image}"
356}
357
Sen Jiang788c2d92016-03-09 12:48:40 -0800358# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700359#
Alex Deymo48b502a2015-09-17 19:00:18 -0700360# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700361extract_image_cros() {
362 local image="$1"
363 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800364 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700365
366 local kernel root
367 kernel=$(create_tempfile "kernel.bin.XXXXXX")
368 CLEANUP_FILES+=("${kernel}")
369 root=$(create_tempfile "root.bin.XXXXXX")
370 CLEANUP_FILES+=("${root}")
371
372 cros_generate_update_payload --extract \
373 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700374 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700375
Amin Hassani58e01d62018-09-19 14:56:15 -0700376 # Chrome OS now uses major_version 2 payloads for all boards.
377 # See crbug.com/794404 for more information.
378 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700379
Tudor Brindusdda79e22018-06-28 18:03:21 -0700380 eval ${partitions_array}[kernel]=\""${kernel}"\"
381 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700382
Sen Jiang788c2d92016-03-09 12:48:40 -0800383 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700384 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800385 fi
386
Alex Deymo89ff9e32015-09-15 19:29:01 -0700387 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700388 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700389 varname="${partitions_array}[${part}]"
390 printf "md5sum of %s: " "${varname}"
391 md5sum "${!varname}"
392 done
393}
394
Sen Jiang3e5804d2018-09-06 15:53:00 -0700395# extract_partition_brillo <target_files.zip> <partitions_array> <partition>
396# <part_file> <part_map_file>
397#
398# Extract the <partition> from target_files zip file into <part_file> and its
399# map file into <part_map_file>.
400extract_partition_brillo() {
401 local image="$1"
402 local partitions_array="$2"
403 local part="$3"
404 local part_file="$4"
405 local part_map_file="$5"
406
407 # For each partition, we in turn look for its image file under IMAGES/ and
408 # RADIO/ in the given target_files zip file.
409 local path path_in_zip
410 for path in IMAGES RADIO; do
411 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
412 path_in_zip="${path}"
413 break
414 fi
415 done
416 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
417 unzip -p "${image}" "${path_in_zip}/${part}.img" >"${part_file}"
418
419 # If the partition is stored as an Android sparse image file, we need to
420 # convert them to a raw image for the update.
Yifan Hong4b821d72018-12-07 17:26:04 -0800421 local magic=$(xxd -p -l4 "${part_file}")
Sen Jiang3e5804d2018-09-06 15:53:00 -0700422 if [[ "${magic}" == "3aff26ed" ]]; then
423 local temp_sparse=$(create_tempfile "${part}.sparse.XXXXXX")
424 echo "Converting Android sparse image ${part}.img to RAW."
425 mv "${part_file}" "${temp_sparse}"
426 simg2img "${temp_sparse}" "${part_file}"
427 rm -f "${temp_sparse}"
428 fi
429
430 # Extract the .map file (if one is available).
431 unzip -p "${image}" "${path_in_zip}/${part}.map" >"${part_map_file}" \
432 2>/dev/null || true
433
434 # delta_generator only supports images multiple of 4 KiB. For target images
435 # we pad the data with zeros if needed, but for source images we truncate
436 # down the data since the last block of the old image could be padded on
437 # disk with unknown data.
438 local filesize=$(stat -c%s "${part_file}")
439 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
440 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
441 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
442 : $(( filesize = filesize & -4096 ))
443 else
444 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
445 : $(( filesize = (filesize + 4095) & -4096 ))
446 fi
447 truncate_file "${part_file}" "${filesize}"
448 fi
449
450 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
451}
452
Sen Jiang788c2d92016-03-09 12:48:40 -0800453# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700454#
455# Extract the A/B updated partitions from a Brillo target_files zip file into
456# new temporary files.
457extract_image_brillo() {
458 local image="$1"
459 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800460 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700461
Alex Deymo48b502a2015-09-17 19:00:18 -0700462 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800463 local ab_partitions_list
464 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
465 CLEANUP_FILES+=("${ab_partitions_list}")
466 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
467 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
468 die "Invalid partition names found in the partition list."
469 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700470 # Get partition list without duplicates.
471 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800472 if [[ ${#partitions[@]} -eq 0 ]]; then
473 die "The list of partitions is empty. Can't generate a payload."
474 fi
475 else
476 warn "No ab_partitions.txt found. Using default."
477 fi
Sen Jiang3e5804d2018-09-06 15:53:00 -0700478 echo "List of A/B partitions for ${partitions_array}: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700479
Sen Jiang788c2d92016-03-09 12:48:40 -0800480 if [[ -n "${partitions_order}" ]]; then
481 eval "${partitions_order}=(${partitions[@]})"
482 fi
483
Alex Deymo83f2f702015-10-14 14:49:33 -0700484 # All Brillo updaters support major version 2.
485 FORCE_MAJOR_VERSION="2"
486
Alex Deymo48b502a2015-09-17 19:00:18 -0700487 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800488 # Source image
489 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700490 CLEANUP_FILES+=("${ue_config}")
491 if ! unzip -p "${image}" "META/update_engine_config.txt" \
492 >"${ue_config}"; then
493 warn "No update_engine_config.txt found. Assuming pre-release image, \
494using payload minor version 2"
495 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700496 # For delta payloads, we use the major and minor version supported by the
497 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700498 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
499 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700500 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
501 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800502
503 # Brillo support for deltas started with minor version 3.
504 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
505 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
506Disabling deltas for this source version."
507 exit ${EX_UNSUPPORTED_DELTA}
508 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800509 else
510 # Target image
511 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
512 CLEANUP_FILES+=("${postinstall_config}")
513 if unzip -p "${image}" "META/postinstall_config.txt" \
514 >"${postinstall_config}"; then
515 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
516 fi
Yifan Hong398cb542018-10-18 11:29:40 -0700517 local dynamic_partitions_info=$(create_tempfile "dynamic_partitions_info.XXXXXX")
518 CLEANUP_FILES+=("${dynamic_partitions_info}")
519 if unzip -p "${image}" "META/dynamic_partitions_info.txt" \
520 >"${dynamic_partitions_info}"; then
521 DYNAMIC_PARTITION_INFO_FILE="${dynamic_partitions_info}"
522 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700523 fi
524
Sen Jiang3e5804d2018-09-06 15:53:00 -0700525 local part
Alex Deymo48b502a2015-09-17 19:00:18 -0700526 for part in "${partitions[@]}"; do
Sen Jiang3e5804d2018-09-06 15:53:00 -0700527 local part_file=$(create_tempfile "${part}.img.XXXXXX")
528 local part_map_file=$(create_tempfile "${part}.map.XXXXXX")
529 CLEANUP_FILES+=("${part_file}" "${part_map_file}")
530 # Extract partitions in background.
531 extract_partition_brillo "${image}" "${partitions_array}" "${part}" \
532 "${part_file}" "${part_map_file}" &
Alex Deymo48b502a2015-09-17 19:00:18 -0700533 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800534 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Sen Jiang3e5804d2018-09-06 15:53:00 -0700535 done
536}
537
538# cleanup_partition_array <partitions_array>
539#
540# Remove all empty files in <partitions_array>.
541cleanup_partition_array() {
542 local partitions_array="$1"
543 # Have to use eval to iterate over associative array keys with variable array
544 # names, we should change it to use nameref once bash 4.3 is available
545 # everywhere.
546 for part in $(eval "echo \${!${partitions_array}[@]}"); do
547 local path="${partitions_array}[$part]"
548 if [[ ! -s "${!path}" ]]; then
549 eval "unset ${partitions_array}[${part}]"
550 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700551 done
552}
553
Tudor Brindusb432db82018-06-29 13:13:27 -0700554extract_payload_images() {
555 local payload_type=$1
556 echo "Extracting images for ${payload_type} update."
557
558 if [[ "${payload_type}" == "delta" ]]; then
559 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
560 fi
561 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Sen Jiang3e5804d2018-09-06 15:53:00 -0700562 # Wait for all subprocesses.
563 wait
564 cleanup_partition_array SRC_PARTITIONS
565 cleanup_partition_array SRC_PARTITIONS_MAP
566 cleanup_partition_array DST_PARTITIONS
567 cleanup_partition_array DST_PARTITIONS_MAP
Tudor Brindusb432db82018-06-29 13:13:27 -0700568}
569
570get_payload_type() {
571 if [[ -z "${FLAGS_source_image}" ]]; then
572 echo "full"
573 else
574 echo "delta"
575 fi
576}
577
Jason Kusumabe998f42015-09-03 15:53:13 -0700578validate_generate() {
579 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700580 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700581
582 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700583 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700584}
585
586cmd_generate() {
Sen Jiang3e5804d2018-09-06 15:53:00 -0700587 local payload_type=$(get_payload_type)
588 extract_payload_images ${payload_type}
Jason Kusumabe998f42015-09-03 15:53:13 -0700589
Alex Deymo48b502a2015-09-17 19:00:18 -0700590 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800591 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700592 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800593
594 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800595 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800596 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800597 if [[ -n "${partition_names}" ]]; then
598 partition_names+=":"
599 new_partitions+=":"
600 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800601 new_mapfiles+=":"
602 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800603 fi
604 partition_names+="${part}"
605 new_partitions+="${DST_PARTITIONS[${part}]}"
606 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800607 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
608 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800609 done
610
611 # Target image args:
612 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700613 --partition_names="${partition_names}"
614 --new_partitions="${new_partitions}"
615 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700616 )
617
Alex Deymo89ff9e32015-09-15 19:29:01 -0700618 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800619 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700620 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700621 --old_partitions="${old_partitions}"
622 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700623 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700624 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
625 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
626 fi
627 fi
628
629 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
630 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700631 fi
632
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700633 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
634 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
635 fi
636
Sen Jiang8e768e92017-06-28 17:13:19 -0700637 if [[ -n "${FLAGS_max_timestamp}" ]]; then
638 GENERATOR_ARGS+=( --max_timestamp="${FLAGS_max_timestamp}" )
639 fi
640
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800641 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
642 GENERATOR_ARGS+=(
643 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
644 )
645 fi
646
Yifan Hong398cb542018-10-18 11:29:40 -0700647 if [[ -n "{DYNAMIC_PARTITION_INFO_FILE}" ]]; then
648 GENERATOR_ARGS+=(
649 --dynamic_partition_info_file="${DYNAMIC_PARTITION_INFO_FILE}"
650 )
651 fi
652
Jason Kusumabe998f42015-09-03 15:53:13 -0700653 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700654 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700655
Alex Deymo89ff9e32015-09-15 19:29:01 -0700656 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700657}
658
659validate_hash() {
660 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700661 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700662
663 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700664 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700665--unsigned_payload FILENAME"
666
Jason Kusumabe998f42015-09-03 15:53:13 -0700667 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700668 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800669
670 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700671 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700672}
673
674cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700675 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700676 --in_file="${FLAGS_unsigned_payload}" \
677 --signature_size="${FLAGS_signature_size}" \
678 --out_hash_file="${FLAGS_payload_hash_file}" \
679 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700680
Jason Kusumabe998f42015-09-03 15:53:13 -0700681 echo "Done generating hash."
682}
683
684validate_sign() {
685 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700686 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700687
688 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700689 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700690--unsigned_payload FILENAME"
691
692 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700693 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700694
695 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700696 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700697--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700698
699 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700700 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700701--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700702}
703
704cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700705 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700706 --in_file="${FLAGS_unsigned_payload}"
707 --signature_size="${FLAGS_signature_size}"
708 --payload_signature_file="${FLAGS_payload_signature_file}"
709 --metadata_signature_file="${FLAGS_metadata_signature_file}"
710 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700711 )
712
713 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
714 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
715 fi
716
717 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700718 echo "Done signing payload."
719}
720
Alex Deymo98e691c2016-02-04 21:05:45 -0800721validate_properties() {
722 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700723 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800724
725 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700726 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800727}
728
729cmd_properties() {
730 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700731 --in_file="${FLAGS_payload}" \
732 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800733}
734
Tudor Brindusb432db82018-06-29 13:13:27 -0700735validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700736 [[ -n "${FLAGS_payload}" ]] ||
737 die "Error: you must specify an input filename with --payload FILENAME"
738
739 [[ -n "${FLAGS_target_image}" ]] ||
740 die "Error: you must specify a target image with --target_image FILENAME"
741}
742
743cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700744 local payload_type=$(get_payload_type)
745 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700746
747 declare -A TMP_PARTITIONS
748 for part in "${PARTITIONS_ORDER[@]}"; do
749 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
750 echo "Creating temporary target partition ${tmp_part} for ${part}"
751 CLEANUP_FILES+=("${tmp_part}")
752 TMP_PARTITIONS[${part}]=${tmp_part}
753 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
754 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
755 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
756 done
757
758 echo "Verifying ${payload_type} update."
759 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700760 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700761
762 local part old_partitions="" new_partitions="" partition_names=""
763 for part in "${PARTITIONS_ORDER[@]}"; do
764 if [[ -n "${partition_names}" ]]; then
765 partition_names+=":"
766 new_partitions+=":"
767 old_partitions+=":"
768 fi
769 partition_names+="${part}"
770 new_partitions+="${TMP_PARTITIONS[${part}]}"
771 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
772 done
773
774 # Target image args:
775 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700776 --partition_names="${partition_names}"
777 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700778 )
779
780 if [[ "${payload_type}" == "delta" ]]; then
781 # Source image args:
782 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700783 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700784 )
785 fi
786
Amin Hassania566cb62017-08-23 12:36:55 -0700787 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
788 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
789 fi
790
Amin Hassani13520932017-07-26 11:26:05 -0700791 echo "Running delta_generator to verify ${payload_type} payload with args: \
792${GENERATOR_ARGS[@]}"
Sen Jiang6feb15c2018-08-31 15:45:17 -0700793 "${GENERATOR}" "${GENERATOR_ARGS[@]}" || true
Amin Hassani13520932017-07-26 11:26:05 -0700794
Sen Jiang6feb15c2018-08-31 15:45:17 -0700795 echo "Done applying ${payload_type} update."
796 echo "Checking the newly generated partitions against the target partitions"
797 local need_pause=false
798 for part in "${PARTITIONS_ORDER[@]}"; do
799 local not_str=""
800 if ! cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"; then
801 not_str="in"
802 need_pause=true
803 fi
804 echo "The new partition (${part}) is ${not_str}valid."
805 done
806 # All images will be cleaned up when script exits, pause here to give a chance
807 # to inspect the images.
808 if [[ "$need_pause" == true ]]; then
809 read -n1 -r -s -p "Paused to investigate invalid partitions, \
810press any key to exit."
Amin Hassani13520932017-07-26 11:26:05 -0700811 fi
812}
813
Tudor Brindusb432db82018-06-29 13:13:27 -0700814cmd_check() {
815 local payload_type=$(get_payload_type)
816 extract_payload_images ${payload_type}
817
818 local part dst_partitions="" src_partitions=""
819 for part in "${PARTITIONS_ORDER[@]}"; do
820 if [[ -n "${dst_partitions}" ]]; then
821 dst_partitions+=" "
822 src_partitions+=" "
823 fi
824 dst_partitions+="${DST_PARTITIONS[${part}]}"
825 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
826 done
827
828 # Common payload args:
829 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
830 --part_names ${PARTITIONS_ORDER[@]} \
831 --dst_part_paths ${dst_partitions} )
832
833 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
834 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
835 fi
836
837 echo "Checking ${payload_type} update."
838 check_update_payload ${PAYCHECK_ARGS[@]} --check
839}
840
Jason Kusumabe998f42015-09-03 15:53:13 -0700841# Sanity check that the real generator exists:
Sen Jiang13519752016-08-02 16:10:52 -0700842GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700843[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
844
845case "$COMMAND" in
846 generate) validate_generate
847 cmd_generate
848 ;;
849 hash) validate_hash
850 cmd_hash
851 ;;
852 sign) validate_sign
853 cmd_sign
854 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800855 properties) validate_properties
856 cmd_properties
857 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700858 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700859 cmd_verify
860 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700861 check) validate_verify_and_check
862 cmd_check
863 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700864esac