blob: 1b9fcbf9cdfdc7c23f3754a007358daedce3d996 [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."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700181fi
182if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
183 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
184 DEFINE_string signature_size "" \
185 "Signature sizes in bytes in the following format: size1:size2[:...]"
186fi
187if [[ "${COMMAND}" == "hash" ]]; then
188 DEFINE_string metadata_hash_file "" \
189 "Optional: Path to output metadata hash file."
190 DEFINE_string payload_hash_file "" \
191 "Optional: Path to output payload hash file."
192fi
193if [[ "${COMMAND}" == "sign" ]]; then
194 DEFINE_string payload "" \
195 "Path to output the generated unsigned payload file."
196 DEFINE_string metadata_signature_file "" \
197 "The metatada signatures in the following format: \
198metadata_signature1:metadata_signature2[:...]"
199 DEFINE_string payload_signature_file "" \
200 "The payload signatures in the following format: \
201payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700202 DEFINE_string metadata_size_file "" \
203 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700204fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800205if [[ "${COMMAND}" == "properties" ]]; then
206 DEFINE_string payload "" \
207 "Path to the input signed or unsigned payload file."
208 DEFINE_string properties_file "-" \
209 "Path to output the extracted property files. If '-' is passed stdout will \
210be used."
211fi
Tudor Brindusb432db82018-06-29 13:13:27 -0700212if [[ "${COMMAND}" == "verify" || "${COMMAND}" == "check" ]]; then
Amin Hassani13520932017-07-26 11:26:05 -0700213 DEFINE_string payload "" \
214 "Path to the input payload file."
215 DEFINE_string target_image "" \
216 "Path to the target image to verify upon."
217 DEFINE_string source_image "" \
218 "Optional: Path to a source image. If specified, the delta update is \
219applied to this."
220fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800221
Alex Deymo5fbb1102017-01-12 13:55:52 -0800222DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700223
224# Parse command line flag arguments
225FLAGS "$@" || exit 1
226eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700227set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700228
Alex Deymo5fbb1102017-01-12 13:55:52 -0800229# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
230# ${TMPDIR}.
231TMPDIR="${FLAGS_work_dir}"
232export TMPDIR
233
Alex Deymo89ff9e32015-09-15 19:29:01 -0700234# Associative arrays from partition name to file in the source and target
235# images. The size of the updated area must be the size of the file.
236declare -A SRC_PARTITIONS
237declare -A DST_PARTITIONS
238
Alex Deymo20bdc702016-12-07 21:07:11 -0800239# Associative arrays for the .map files associated with each src/dst partition
240# file in SRC_PARTITIONS and DST_PARTITIONS.
241declare -A SRC_PARTITIONS_MAP
242declare -A DST_PARTITIONS_MAP
243
Sen Jiang788c2d92016-03-09 12:48:40 -0800244# List of partition names in order.
245declare -a PARTITIONS_ORDER
246
Alex Deymo89ff9e32015-09-15 19:29:01 -0700247# A list of temporary files to remove during cleanup.
248CLEANUP_FILES=()
249
Alex Deymo48b502a2015-09-17 19:00:18 -0700250# Global options to force the version of the payload.
251FORCE_MAJOR_VERSION=""
252FORCE_MINOR_VERSION=""
253
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800254# Path to the postinstall config file in target image if exists.
255POSTINSTALL_CONFIG_FILE=""
256
Alex Deymoc97df432015-09-25 17:23:52 -0700257# read_option_int <file.txt> <option_key> [default_value]
258#
259# Reads the unsigned integer value associated with |option_key| in a key=value
260# file |file.txt|. Prints the read value if found and valid, otherwise prints
261# the |default_value|.
262read_option_uint() {
263 local file_txt="$1"
264 local option_key="$2"
265 local default_value="${3:-}"
266 local value
267 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
268 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
269 echo "${value}"
270 return
271 fi
272 fi
273 echo "${default_value}"
274}
275
Sen Jiangd0e9a892016-07-22 16:28:07 -0700276# truncate_file <file_path> <file_size>
277#
278# Truncate the given |file_path| to |file_size| using perl.
279# The truncate binary might not be available.
280truncate_file() {
281 local file_path="$1"
282 local file_size="$2"
283 perl -e "open(FILE, \"+<\", \$ARGV[0]); \
284 truncate(FILE, ${file_size}); \
285 close(FILE);" "${file_path}"
286}
287
Alex Deymo89ff9e32015-09-15 19:29:01 -0700288# Create a temporary file in the work_dir with an optional pattern name.
289# Prints the name of the newly created file.
290create_tempfile() {
291 local pattern="${1:-tempfile.XXXXXX}"
292 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
293}
Jason Kusumabe998f42015-09-03 15:53:13 -0700294
295cleanup() {
296 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700297 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700298
299 # If we are cleaning up after an error, or if we got an error during
300 # cleanup (even if we eventually succeeded) return a non-zero exit
301 # code. This triggers additional logging in most environments that call
302 # this script.
303 if [[ -n "${err}" ]]; then
304 die "Cleanup encountered an error."
305 fi
306}
307
308cleanup_on_error() {
309 trap - INT TERM ERR EXIT
310 cleanup
311 die "Cleanup success after an error."
312}
313
314cleanup_on_exit() {
315 trap - INT TERM ERR EXIT
316 cleanup
317}
318
319trap cleanup_on_error INT TERM ERR
320trap cleanup_on_exit EXIT
321
Alex Deymo48b502a2015-09-17 19:00:18 -0700322
Sen Jiang788c2d92016-03-09 12:48:40 -0800323# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700324#
325# Detect the format of the |image| file and extract its updatable partitions
326# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800327# associative array passed in |partitions_array|. If |partitions_order| is
328# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700329extract_image() {
330 local image="$1"
331
332 # Brillo images are zip files. We detect the 4-byte magic header of the zip
333 # file.
334 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
335 if [[ "${magic}" == "504b0304" ]]; then
336 echo "Detected .zip file, extracting Brillo image."
337 extract_image_brillo "$@"
338 return
339 fi
340
341 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
342 # bundled here and we will use it to extract the partitions, so the GPT
343 # headers must be valid.
344 if cgpt show -q -n "${image}" >/dev/null; then
345 echo "Detected GPT image, extracting Chrome OS image."
346 extract_image_cros "$@"
347 return
348 fi
349
350 die "Couldn't detect the image format of ${image}"
351}
352
Sen Jiang788c2d92016-03-09 12:48:40 -0800353# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700354#
Alex Deymo48b502a2015-09-17 19:00:18 -0700355# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700356extract_image_cros() {
357 local image="$1"
358 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800359 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700360
361 local kernel root
362 kernel=$(create_tempfile "kernel.bin.XXXXXX")
363 CLEANUP_FILES+=("${kernel}")
364 root=$(create_tempfile "root.bin.XXXXXX")
365 CLEANUP_FILES+=("${root}")
366
367 cros_generate_update_payload --extract \
368 --image "${image}" \
Amin Hassani58e01d62018-09-19 14:56:15 -0700369 --kern_path "${kernel}" --root_path "${root}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700370
Amin Hassani58e01d62018-09-19 14:56:15 -0700371 # Chrome OS now uses major_version 2 payloads for all boards.
372 # See crbug.com/794404 for more information.
373 FORCE_MAJOR_VERSION="2"
Alex Deymo83f2f702015-10-14 14:49:33 -0700374
Tudor Brindusdda79e22018-06-28 18:03:21 -0700375 eval ${partitions_array}[kernel]=\""${kernel}"\"
376 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700377
Sen Jiang788c2d92016-03-09 12:48:40 -0800378 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700379 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800380 fi
381
Alex Deymo89ff9e32015-09-15 19:29:01 -0700382 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700383 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700384 varname="${partitions_array}[${part}]"
385 printf "md5sum of %s: " "${varname}"
386 md5sum "${!varname}"
387 done
388}
389
Sen Jiang788c2d92016-03-09 12:48:40 -0800390# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700391#
392# Extract the A/B updated partitions from a Brillo target_files zip file into
393# new temporary files.
394extract_image_brillo() {
395 local image="$1"
396 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800397 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700398
Alex Deymo48b502a2015-09-17 19:00:18 -0700399 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800400 local ab_partitions_list
401 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
402 CLEANUP_FILES+=("${ab_partitions_list}")
403 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
404 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
405 die "Invalid partition names found in the partition list."
406 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700407 # Get partition list without duplicates.
408 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800409 if [[ ${#partitions[@]} -eq 0 ]]; then
410 die "The list of partitions is empty. Can't generate a payload."
411 fi
412 else
413 warn "No ab_partitions.txt found. Using default."
414 fi
415 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700416
Sen Jiang788c2d92016-03-09 12:48:40 -0800417 if [[ -n "${partitions_order}" ]]; then
418 eval "${partitions_order}=(${partitions[@]})"
419 fi
420
Alex Deymo83f2f702015-10-14 14:49:33 -0700421 # All Brillo updaters support major version 2.
422 FORCE_MAJOR_VERSION="2"
423
Alex Deymo48b502a2015-09-17 19:00:18 -0700424 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800425 # Source image
426 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700427 CLEANUP_FILES+=("${ue_config}")
428 if ! unzip -p "${image}" "META/update_engine_config.txt" \
429 >"${ue_config}"; then
430 warn "No update_engine_config.txt found. Assuming pre-release image, \
431using payload minor version 2"
432 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700433 # For delta payloads, we use the major and minor version supported by the
434 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700435 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
436 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700437 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
438 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800439
440 # Brillo support for deltas started with minor version 3.
441 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
442 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
443Disabling deltas for this source version."
444 exit ${EX_UNSUPPORTED_DELTA}
445 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800446 else
447 # Target image
448 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
449 CLEANUP_FILES+=("${postinstall_config}")
450 if unzip -p "${image}" "META/postinstall_config.txt" \
451 >"${postinstall_config}"; then
452 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
453 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700454 fi
455
456 local part part_file temp_raw filesize
457 for part in "${partitions[@]}"; do
458 part_file=$(create_tempfile "${part}.img.XXXXXX")
459 CLEANUP_FILES+=("${part_file}")
Tao Bao07216d62018-04-11 11:36:38 -0700460
461 # For each partition, we in turn look for its image file under IMAGES/ and
462 # RADIO/ in the given target_files zip file.
463 local path path_in_zip
464 for path in IMAGES RADIO; do
465 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
466 path_in_zip="${path}"
467 break
468 fi
469 done
470 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
471 unzip -p "${image}" "${path_in_zip}/${part}.img" >"${part_file}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700472
473 # If the partition is stored as an Android sparse image file, we need to
474 # convert them to a raw image for the update.
475 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
476 if [[ "${magic}" == "3aff26ed" ]]; then
477 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
478 CLEANUP_FILES+=("${temp_raw}")
479 echo "Converting Android sparse image ${part}.img to RAW."
480 simg2img "${part_file}" "${temp_raw}"
481 # At this point, we can drop the contents of the old part_file file, but
482 # we can't delete the file because it will be deleted in cleanup.
483 true >"${part_file}"
484 part_file="${temp_raw}"
485 fi
486
Alex Deymo20bdc702016-12-07 21:07:11 -0800487 # Extract the .map file (if one is available).
488 part_map_file=$(create_tempfile "${part}.map.XXXXXX")
489 CLEANUP_FILES+=("${part_map_file}")
Tao Bao07216d62018-04-11 11:36:38 -0700490 unzip -p "${image}" "${path_in_zip}/${part}.map" >"${part_map_file}" || \
Alex Deymo20bdc702016-12-07 21:07:11 -0800491 part_map_file=""
492
Alex Deymoa479a4d2016-05-11 18:13:49 -0700493 # delta_generator only supports images multiple of 4 KiB. For target images
494 # we pad the data with zeros if needed, but for source images we truncate
495 # down the data since the last block of the old image could be padded on
496 # disk with unknown data.
Alex Deymo48b502a2015-09-17 19:00:18 -0700497 filesize=$(stat -c%s "${part_file}")
498 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
Alex Deymoa479a4d2016-05-11 18:13:49 -0700499 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
500 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
501 : $(( filesize = filesize & -4096 ))
Alex Deymo04c23052017-04-12 18:15:30 -0700502 if [[ ${filesize} == 0 ]]; then
503 echo "Source partition ${part}.img is empty after rounding down," \
504 "skipping."
505 continue
506 fi
Alex Deymoa479a4d2016-05-11 18:13:49 -0700507 else
508 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
509 : $(( filesize = (filesize + 4095) & -4096 ))
510 fi
Sen Jiangd0e9a892016-07-22 16:28:07 -0700511 truncate_file "${part_file}" "${filesize}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700512 fi
513
514 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800515 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Alex Deymo48b502a2015-09-17 19:00:18 -0700516 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
517 done
518}
519
Tudor Brindusb432db82018-06-29 13:13:27 -0700520extract_payload_images() {
521 local payload_type=$1
522 echo "Extracting images for ${payload_type} update."
523
524 if [[ "${payload_type}" == "delta" ]]; then
525 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
526 fi
527 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
528}
529
530get_payload_type() {
531 if [[ -z "${FLAGS_source_image}" ]]; then
532 echo "full"
533 else
534 echo "delta"
535 fi
536}
537
Jason Kusumabe998f42015-09-03 15:53:13 -0700538validate_generate() {
539 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700540 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700541
542 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700543 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700544}
545
546cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700547 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700548 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700549 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700550 fi
551
Alex Deymo48b502a2015-09-17 19:00:18 -0700552 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700553
Sen Jiang788c2d92016-03-09 12:48:40 -0800554 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Alex Deymo89ff9e32015-09-15 19:29:01 -0700555 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700556 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700557 fi
558
Alex Deymo48b502a2015-09-17 19:00:18 -0700559 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800560 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700561 GENERATOR_ARGS=( --out_file="${FLAGS_payload}" )
Alex Deymo168b5352015-11-04 13:51:52 -0800562
563 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800564 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800565 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800566 if [[ -n "${partition_names}" ]]; then
567 partition_names+=":"
568 new_partitions+=":"
569 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800570 new_mapfiles+=":"
571 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800572 fi
573 partition_names+="${part}"
574 new_partitions+="${DST_PARTITIONS[${part}]}"
575 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800576 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
577 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800578 done
579
580 # Target image args:
581 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700582 --partition_names="${partition_names}"
583 --new_partitions="${new_partitions}"
584 --new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700585 )
586
Alex Deymo89ff9e32015-09-15 19:29:01 -0700587 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800588 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700589 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700590 --old_partitions="${old_partitions}"
591 --old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700592 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700593 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
594 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
595 fi
596 fi
597
598 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
599 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700600 fi
601
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700602 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
603 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
604 fi
605
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800606 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
607 GENERATOR_ARGS+=(
608 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
609 )
610 fi
611
Jason Kusumabe998f42015-09-03 15:53:13 -0700612 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700613 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700614
Alex Deymo89ff9e32015-09-15 19:29:01 -0700615 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700616}
617
618validate_hash() {
619 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700620 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700621
622 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700623 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700624--unsigned_payload FILENAME"
625
Jason Kusumabe998f42015-09-03 15:53:13 -0700626 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700627 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800628
629 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700630 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700631}
632
633cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700634 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700635 --in_file="${FLAGS_unsigned_payload}" \
636 --signature_size="${FLAGS_signature_size}" \
637 --out_hash_file="${FLAGS_payload_hash_file}" \
638 --out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700639
Jason Kusumabe998f42015-09-03 15:53:13 -0700640 echo "Done generating hash."
641}
642
643validate_sign() {
644 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700645 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700646
647 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700648 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700649--unsigned_payload FILENAME"
650
651 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700652 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700653
654 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700655 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700656--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700657
658 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700659 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700660--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700661}
662
663cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700664 GENERATOR_ARGS=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700665 --in_file="${FLAGS_unsigned_payload}"
666 --signature_size="${FLAGS_signature_size}"
667 --payload_signature_file="${FLAGS_payload_signature_file}"
668 --metadata_signature_file="${FLAGS_metadata_signature_file}"
669 --out_file="${FLAGS_payload}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700670 )
671
672 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
673 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
674 fi
675
676 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700677 echo "Done signing payload."
678}
679
Alex Deymo98e691c2016-02-04 21:05:45 -0800680validate_properties() {
681 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700682 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800683
684 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700685 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800686}
687
688cmd_properties() {
689 "${GENERATOR}" \
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700690 --in_file="${FLAGS_payload}" \
691 --properties_file="${FLAGS_properties_file}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800692}
693
Tudor Brindusb432db82018-06-29 13:13:27 -0700694validate_verify_and_check() {
Amin Hassani13520932017-07-26 11:26:05 -0700695 [[ -n "${FLAGS_payload}" ]] ||
696 die "Error: you must specify an input filename with --payload FILENAME"
697
698 [[ -n "${FLAGS_target_image}" ]] ||
699 die "Error: you must specify a target image with --target_image FILENAME"
700}
701
702cmd_verify() {
Tudor Brindusb432db82018-06-29 13:13:27 -0700703 local payload_type=$(get_payload_type)
704 extract_payload_images ${payload_type}
Amin Hassani13520932017-07-26 11:26:05 -0700705
706 declare -A TMP_PARTITIONS
707 for part in "${PARTITIONS_ORDER[@]}"; do
708 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
709 echo "Creating temporary target partition ${tmp_part} for ${part}"
710 CLEANUP_FILES+=("${tmp_part}")
711 TMP_PARTITIONS[${part}]=${tmp_part}
712 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
713 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
714 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
715 done
716
717 echo "Verifying ${payload_type} update."
718 # Common payload args:
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700719 GENERATOR_ARGS=( --in_file="${FLAGS_payload}" )
Amin Hassani13520932017-07-26 11:26:05 -0700720
721 local part old_partitions="" new_partitions="" partition_names=""
722 for part in "${PARTITIONS_ORDER[@]}"; do
723 if [[ -n "${partition_names}" ]]; then
724 partition_names+=":"
725 new_partitions+=":"
726 old_partitions+=":"
727 fi
728 partition_names+="${part}"
729 new_partitions+="${TMP_PARTITIONS[${part}]}"
730 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
731 done
732
733 # Target image args:
734 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700735 --partition_names="${partition_names}"
736 --new_partitions="${new_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700737 )
738
739 if [[ "${payload_type}" == "delta" ]]; then
740 # Source image args:
741 GENERATOR_ARGS+=(
Tudor Brindus5ec5bd12018-07-11 11:02:44 -0700742 --old_partitions="${old_partitions}"
Amin Hassani13520932017-07-26 11:26:05 -0700743 )
744 fi
745
Amin Hassania566cb62017-08-23 12:36:55 -0700746 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
747 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
748 fi
749
Amin Hassani13520932017-07-26 11:26:05 -0700750 echo "Running delta_generator to verify ${payload_type} payload with args: \
751${GENERATOR_ARGS[@]}"
752 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
753
754 if [[ $? -eq 0 ]]; then
755 echo "Done applying ${payload_type} update."
756 echo "Checking the newly generated partitions against the target partitions"
757 for part in "${PARTITIONS_ORDER[@]}"; do
758 cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"
759 local not_str=""
760 if [[ $? -ne 0 ]]; then
761 not_str="in"
762 fi
763 echo "The new partition (${part}) is ${not_str}valid."
764 done
765 else
766 echo "Failed to apply ${payload_type} update."
767 fi
768}
769
Tudor Brindusb432db82018-06-29 13:13:27 -0700770cmd_check() {
771 local payload_type=$(get_payload_type)
772 extract_payload_images ${payload_type}
773
774 local part dst_partitions="" src_partitions=""
775 for part in "${PARTITIONS_ORDER[@]}"; do
776 if [[ -n "${dst_partitions}" ]]; then
777 dst_partitions+=" "
778 src_partitions+=" "
779 fi
780 dst_partitions+="${DST_PARTITIONS[${part}]}"
781 src_partitions+="${SRC_PARTITIONS[${part}]:-}"
782 done
783
784 # Common payload args:
785 PAYCHECK_ARGS=( "${FLAGS_payload}" --type ${payload_type} \
786 --part_names ${PARTITIONS_ORDER[@]} \
787 --dst_part_paths ${dst_partitions} )
788
789 if [[ ! -z "${SRC_PARTITIONS[@]}" ]]; then
790 PAYCHECK_ARGS+=( --src_part_paths ${src_partitions} )
791 fi
792
793 echo "Checking ${payload_type} update."
794 check_update_payload ${PAYCHECK_ARGS[@]} --check
795}
796
Jason Kusumabe998f42015-09-03 15:53:13 -0700797# Sanity check that the real generator exists:
Sen Jiang13519752016-08-02 16:10:52 -0700798GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700799[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
800
801case "$COMMAND" in
802 generate) validate_generate
803 cmd_generate
804 ;;
805 hash) validate_hash
806 cmd_hash
807 ;;
808 sign) validate_sign
809 cmd_sign
810 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800811 properties) validate_properties
812 cmd_properties
813 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700814 verify) validate_verify_and_check
Amin Hassani13520932017-07-26 11:26:05 -0700815 cmd_verify
816 ;;
Tudor Brindusb432db82018-06-29 13:13:27 -0700817 check) validate_verify_and_check
818 cmd_check
819 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700820esac