blob: 70ab7e23911a2b4b3d3510a78732098dce4244f8 [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.
Jason Kusumabe998f42015-09-03 15:53:13 -070028#
29# Generate command arguments:
Jason Kusuma9a4cae22015-10-08 18:17:57 -070030# --payload generated unsigned payload output file
31# --source_image if defined, generate a delta payload from the specified
32# image to the target_image
33# --target_image the target image that should be sent to clients
Amin Hassani13520932017-07-26 11:26:05 -070034# --metadata_size_file if defined, generate a file containing the size of the
35# payload metadata in bytes to the specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070036#
37# Hash command arguments:
38# --unsigned_payload the input unsigned payload to generate the hash from
39# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070040# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070041# --payload_hash_file if defined, generate a payload hash and output to the
42# specified file
43# --metadata_hash_file if defined, generate a metadata hash and output to the
44# specified file
45#
46# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070047# --unsigned_payload the input unsigned payload to insert the signatures
48# --payload the output signed payload
49# --signature_size signature sizes in bytes in the following format:
50# "size1:size2[:...]"
51# --payload_signature_file the payload signature files in the following
52# format:
53# "payload_signature1:payload_signature2[:...]"
54# --metadata_signature_file the metadata signature files in the following
55# format:
56# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070057# --metadata_size_file if defined, generate a file containing the size of
58# the signed payload metadata in bytes to the
59# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070060# Note that the number of signature sizes and payload signatures have to match.
Alex Deymo98e691c2016-02-04 21:05:45 -080061#
62# Properties command arguments:
63# --payload the input signed or unsigned payload
64# --properties_file the output path where to write the properties, or
65# '-' for stdout.
Amin Hassani13520932017-07-26 11:26:05 -070066# Verify command arguments:
67# --payload payload input file
68# --source_image verify payload to the specified source image.
69# --target_image the target image to verify upon.
Alex Deymo98e691c2016-02-04 21:05:45 -080070
Jason Kusumabe998f42015-09-03 15:53:13 -070071
Alex Deymo61e1fa82016-01-19 15:16:34 -080072# Exit codes:
73EX_UNSUPPORTED_DELTA=100
74
Jason Kusumaf514c542015-11-05 18:43:45 -080075warn() {
76 echo "brillo_update_payload: warning: $*" >&2
77}
78
Gilad Arnold957ce122015-10-14 16:02:55 -070079die() {
80 echo "brillo_update_payload: error: $*" >&2
81 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070082}
83
Gilad Arnold957ce122015-10-14 16:02:55 -070084# Loads shflags. We first look at the default install location; then look for
Amin Hassani2a14d412018-05-31 13:01:09 -070085# crosutils (chroot); finally check our own directory.
Gilad Arnold957ce122015-10-14 16:02:55 -070086load_shflags() {
87 local my_dir="$(dirname "$(readlink -f "$0")")"
88 local path
89 for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do
90 if [[ -r "${path}/shflags" ]]; then
91 . "${path}/shflags" || die "Could not load ${path}/shflags."
92 return
93 fi
94 done
95 die "Could not find shflags."
96}
97
98load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -070099
Alex Deymoc64ffd52015-09-25 18:10:07 -0700100HELP_GENERATE="generate: Generate an unsigned update payload."
101HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
102for signing."
103HELP_SIGN="sign: Insert the signatures into the unsigned payload."
Alex Deymo98e691c2016-02-04 21:05:45 -0800104HELP_PROPERTIES="properties: Extract payload properties to a file."
Amin Hassani13520932017-07-26 11:26:05 -0700105HELP_VERIFY="verify: Verify a (signed) update payload."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700106
107usage() {
108 echo "Supported commands:"
109 echo
110 echo "${HELP_GENERATE}"
111 echo "${HELP_HASH}"
112 echo "${HELP_SIGN}"
Alex Deymo98e691c2016-02-04 21:05:45 -0800113 echo "${HELP_PROPERTIES}"
Amin Hassani13520932017-07-26 11:26:05 -0700114 echo "${HELP_VERIFY}"
Alex Deymoc64ffd52015-09-25 18:10:07 -0700115 echo
116 echo "Use: \"$0 <command> --help\" for more options."
117}
118
119# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -0700120if [[ $# -lt 1 ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700121 echo "Please specify a command [generate|hash|sign|properties|verify]"
Jason Kusumabe998f42015-09-03 15:53:13 -0700122 exit 1
123fi
124
Alex Deymoc64ffd52015-09-25 18:10:07 -0700125# Parse command.
126COMMAND="${1:-}"
127shift
128
129case "${COMMAND}" in
130 generate)
131 FLAGS_HELP="${HELP_GENERATE}"
132 ;;
133
134 hash)
135 FLAGS_HELP="${HELP_HASH}"
136 ;;
137
138 sign)
139 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700140 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800141
142 properties)
143 FLAGS_HELP="${HELP_PROPERTIES}"
144 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700145
146 verify)
147 FLAGS_HELP="${HELP_VERIFY}"
148 ;;
149
Jason Kusumabe998f42015-09-03 15:53:13 -0700150 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700151 echo "Unrecognized command: \"${COMMAND}\"" >&2
152 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700153 exit 1
154 ;;
155esac
156
Jason Kusumabe998f42015-09-03 15:53:13 -0700157# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700158FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
159${FLAGS_HELP}"
160
161if [[ "${COMMAND}" == "generate" ]]; then
162 DEFINE_string payload "" \
163 "Path to output the generated unsigned payload file."
164 DEFINE_string target_image "" \
165 "Path to the target image that should be sent to clients."
166 DEFINE_string source_image "" \
167 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700168 DEFINE_string metadata_size_file "" \
169 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700170fi
171if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
172 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
173 DEFINE_string signature_size "" \
174 "Signature sizes in bytes in the following format: size1:size2[:...]"
175fi
176if [[ "${COMMAND}" == "hash" ]]; then
177 DEFINE_string metadata_hash_file "" \
178 "Optional: Path to output metadata hash file."
179 DEFINE_string payload_hash_file "" \
180 "Optional: Path to output payload hash file."
181fi
182if [[ "${COMMAND}" == "sign" ]]; then
183 DEFINE_string payload "" \
184 "Path to output the generated unsigned payload file."
185 DEFINE_string metadata_signature_file "" \
186 "The metatada signatures in the following format: \
187metadata_signature1:metadata_signature2[:...]"
188 DEFINE_string payload_signature_file "" \
189 "The payload signatures in the following format: \
190payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700191 DEFINE_string metadata_size_file "" \
192 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700193fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800194if [[ "${COMMAND}" == "properties" ]]; then
195 DEFINE_string payload "" \
196 "Path to the input signed or unsigned payload file."
197 DEFINE_string properties_file "-" \
198 "Path to output the extracted property files. If '-' is passed stdout will \
199be used."
200fi
Amin Hassani13520932017-07-26 11:26:05 -0700201if [[ "${COMMAND}" == "verify" ]]; then
202 DEFINE_string payload "" \
203 "Path to the input payload file."
204 DEFINE_string target_image "" \
205 "Path to the target image to verify upon."
206 DEFINE_string source_image "" \
207 "Optional: Path to a source image. If specified, the delta update is \
208applied to this."
209fi
Alex Deymo98e691c2016-02-04 21:05:45 -0800210
Alex Deymo5fbb1102017-01-12 13:55:52 -0800211DEFINE_string work_dir "${TMPDIR:-/tmp}" "Where to dump temporary files."
Jason Kusumabe998f42015-09-03 15:53:13 -0700212
213# Parse command line flag arguments
214FLAGS "$@" || exit 1
215eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700216set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700217
Alex Deymo5fbb1102017-01-12 13:55:52 -0800218# Override the TMPDIR with the passed work_dir flags, which anyway defaults to
219# ${TMPDIR}.
220TMPDIR="${FLAGS_work_dir}"
221export TMPDIR
222
Alex Deymo89ff9e32015-09-15 19:29:01 -0700223# Associative arrays from partition name to file in the source and target
224# images. The size of the updated area must be the size of the file.
225declare -A SRC_PARTITIONS
226declare -A DST_PARTITIONS
227
Alex Deymo20bdc702016-12-07 21:07:11 -0800228# Associative arrays for the .map files associated with each src/dst partition
229# file in SRC_PARTITIONS and DST_PARTITIONS.
230declare -A SRC_PARTITIONS_MAP
231declare -A DST_PARTITIONS_MAP
232
Sen Jiang788c2d92016-03-09 12:48:40 -0800233# List of partition names in order.
234declare -a PARTITIONS_ORDER
235
Alex Deymo89ff9e32015-09-15 19:29:01 -0700236# A list of temporary files to remove during cleanup.
237CLEANUP_FILES=()
238
Alex Deymo48b502a2015-09-17 19:00:18 -0700239# Global options to force the version of the payload.
240FORCE_MAJOR_VERSION=""
241FORCE_MINOR_VERSION=""
242
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800243# Path to the postinstall config file in target image if exists.
244POSTINSTALL_CONFIG_FILE=""
245
Alex Deymoc97df432015-09-25 17:23:52 -0700246# read_option_int <file.txt> <option_key> [default_value]
247#
248# Reads the unsigned integer value associated with |option_key| in a key=value
249# file |file.txt|. Prints the read value if found and valid, otherwise prints
250# the |default_value|.
251read_option_uint() {
252 local file_txt="$1"
253 local option_key="$2"
254 local default_value="${3:-}"
255 local value
256 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
257 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
258 echo "${value}"
259 return
260 fi
261 fi
262 echo "${default_value}"
263}
264
Sen Jiangd0e9a892016-07-22 16:28:07 -0700265# truncate_file <file_path> <file_size>
266#
267# Truncate the given |file_path| to |file_size| using perl.
268# The truncate binary might not be available.
269truncate_file() {
270 local file_path="$1"
271 local file_size="$2"
272 perl -e "open(FILE, \"+<\", \$ARGV[0]); \
273 truncate(FILE, ${file_size}); \
274 close(FILE);" "${file_path}"
275}
276
Alex Deymo89ff9e32015-09-15 19:29:01 -0700277# Create a temporary file in the work_dir with an optional pattern name.
278# Prints the name of the newly created file.
279create_tempfile() {
280 local pattern="${1:-tempfile.XXXXXX}"
281 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
282}
Jason Kusumabe998f42015-09-03 15:53:13 -0700283
284cleanup() {
285 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700286 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700287
288 # If we are cleaning up after an error, or if we got an error during
289 # cleanup (even if we eventually succeeded) return a non-zero exit
290 # code. This triggers additional logging in most environments that call
291 # this script.
292 if [[ -n "${err}" ]]; then
293 die "Cleanup encountered an error."
294 fi
295}
296
297cleanup_on_error() {
298 trap - INT TERM ERR EXIT
299 cleanup
300 die "Cleanup success after an error."
301}
302
303cleanup_on_exit() {
304 trap - INT TERM ERR EXIT
305 cleanup
306}
307
308trap cleanup_on_error INT TERM ERR
309trap cleanup_on_exit EXIT
310
Alex Deymo48b502a2015-09-17 19:00:18 -0700311
Sen Jiang788c2d92016-03-09 12:48:40 -0800312# extract_image <image> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700313#
314# Detect the format of the |image| file and extract its updatable partitions
315# into new temporary files. Add the list of partition names and its files to the
Sen Jiang788c2d92016-03-09 12:48:40 -0800316# associative array passed in |partitions_array|. If |partitions_order| is
317# passed, set it to list of partition names in order.
Alex Deymo48b502a2015-09-17 19:00:18 -0700318extract_image() {
319 local image="$1"
320
321 # Brillo images are zip files. We detect the 4-byte magic header of the zip
322 # file.
323 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
324 if [[ "${magic}" == "504b0304" ]]; then
325 echo "Detected .zip file, extracting Brillo image."
326 extract_image_brillo "$@"
327 return
328 fi
329
330 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
331 # bundled here and we will use it to extract the partitions, so the GPT
332 # headers must be valid.
333 if cgpt show -q -n "${image}" >/dev/null; then
334 echo "Detected GPT image, extracting Chrome OS image."
335 extract_image_cros "$@"
336 return
337 fi
338
339 die "Couldn't detect the image format of ${image}"
340}
341
Sen Jiang788c2d92016-03-09 12:48:40 -0800342# extract_image_cros <image.bin> <partitions_array> [partitions_order]
Alex Deymo89ff9e32015-09-15 19:29:01 -0700343#
Alex Deymo48b502a2015-09-17 19:00:18 -0700344# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700345extract_image_cros() {
346 local image="$1"
347 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800348 local partitions_order="${3:-}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700349
350 local kernel root
351 kernel=$(create_tempfile "kernel.bin.XXXXXX")
352 CLEANUP_FILES+=("${kernel}")
353 root=$(create_tempfile "root.bin.XXXXXX")
354 CLEANUP_FILES+=("${root}")
355
356 cros_generate_update_payload --extract \
357 --image "${image}" \
358 --kern_path "${kernel}" --root_path "${root}" \
359 --work_dir "${FLAGS_work_dir}" --outside_chroot
360
Alex Deymo83f2f702015-10-14 14:49:33 -0700361 # Chrome OS uses major_version 1 payloads for all versions, even if the
362 # updater supports a newer major version.
363 FORCE_MAJOR_VERSION="1"
364
Tudor Brindusdda79e22018-06-28 18:03:21 -0700365 eval ${partitions_array}[kernel]=\""${kernel}"\"
366 eval ${partitions_array}[root]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700367
Sen Jiang788c2d92016-03-09 12:48:40 -0800368 if [[ -n "${partitions_order}" ]]; then
Tudor Brindusdda79e22018-06-28 18:03:21 -0700369 eval "${partitions_order}=( \"root\" \"kernel\" )"
Sen Jiang788c2d92016-03-09 12:48:40 -0800370 fi
371
Alex Deymo89ff9e32015-09-15 19:29:01 -0700372 local part varname
Tudor Brindusdda79e22018-06-28 18:03:21 -0700373 for part in kernel root; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700374 varname="${partitions_array}[${part}]"
375 printf "md5sum of %s: " "${varname}"
376 md5sum "${!varname}"
377 done
378}
379
Sen Jiang788c2d92016-03-09 12:48:40 -0800380# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
Alex Deymo48b502a2015-09-17 19:00:18 -0700381#
382# Extract the A/B updated partitions from a Brillo target_files zip file into
383# new temporary files.
384extract_image_brillo() {
385 local image="$1"
386 local partitions_array="$2"
Sen Jiang788c2d92016-03-09 12:48:40 -0800387 local partitions_order="${3:-}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700388
Alex Deymo48b502a2015-09-17 19:00:18 -0700389 local partitions=( "boot" "system" )
Alex Deymo168b5352015-11-04 13:51:52 -0800390 local ab_partitions_list
391 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
392 CLEANUP_FILES+=("${ab_partitions_list}")
393 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
394 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
395 die "Invalid partition names found in the partition list."
396 fi
Sen Jiang34c711a2017-10-25 17:25:21 -0700397 # Get partition list without duplicates.
398 partitions=($(awk '!seen[$0]++' "${ab_partitions_list}"))
Alex Deymo168b5352015-11-04 13:51:52 -0800399 if [[ ${#partitions[@]} -eq 0 ]]; then
400 die "The list of partitions is empty. Can't generate a payload."
401 fi
402 else
403 warn "No ab_partitions.txt found. Using default."
404 fi
405 echo "List of A/B partitions: ${partitions[@]}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700406
Sen Jiang788c2d92016-03-09 12:48:40 -0800407 if [[ -n "${partitions_order}" ]]; then
408 eval "${partitions_order}=(${partitions[@]})"
409 fi
410
Alex Deymo83f2f702015-10-14 14:49:33 -0700411 # All Brillo updaters support major version 2.
412 FORCE_MAJOR_VERSION="2"
413
Alex Deymo48b502a2015-09-17 19:00:18 -0700414 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800415 # Source image
416 local ue_config=$(create_tempfile "ue_config.XXXXXX")
Alex Deymoc97df432015-09-25 17:23:52 -0700417 CLEANUP_FILES+=("${ue_config}")
418 if ! unzip -p "${image}" "META/update_engine_config.txt" \
419 >"${ue_config}"; then
420 warn "No update_engine_config.txt found. Assuming pre-release image, \
421using payload minor version 2"
422 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700423 # For delta payloads, we use the major and minor version supported by the
424 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700425 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
426 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700427 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
428 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo61e1fa82016-01-19 15:16:34 -0800429
430 # Brillo support for deltas started with minor version 3.
431 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
432 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
433Disabling deltas for this source version."
434 exit ${EX_UNSUPPORTED_DELTA}
435 fi
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800436 else
437 # Target image
438 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
439 CLEANUP_FILES+=("${postinstall_config}")
440 if unzip -p "${image}" "META/postinstall_config.txt" \
441 >"${postinstall_config}"; then
442 POSTINSTALL_CONFIG_FILE="${postinstall_config}"
443 fi
Alex Deymo48b502a2015-09-17 19:00:18 -0700444 fi
445
446 local part part_file temp_raw filesize
447 for part in "${partitions[@]}"; do
448 part_file=$(create_tempfile "${part}.img.XXXXXX")
449 CLEANUP_FILES+=("${part_file}")
Tao Bao07216d62018-04-11 11:36:38 -0700450
451 # For each partition, we in turn look for its image file under IMAGES/ and
452 # RADIO/ in the given target_files zip file.
453 local path path_in_zip
454 for path in IMAGES RADIO; do
455 if unzip -l "${image}" "${path}/${part}.img" >/dev/null; then
456 path_in_zip="${path}"
457 break
458 fi
459 done
460 [[ -n "${path_in_zip}" ]] || die "Failed to find ${part}.img"
461 unzip -p "${image}" "${path_in_zip}/${part}.img" >"${part_file}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700462
463 # If the partition is stored as an Android sparse image file, we need to
464 # convert them to a raw image for the update.
465 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
466 if [[ "${magic}" == "3aff26ed" ]]; then
467 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
468 CLEANUP_FILES+=("${temp_raw}")
469 echo "Converting Android sparse image ${part}.img to RAW."
470 simg2img "${part_file}" "${temp_raw}"
471 # At this point, we can drop the contents of the old part_file file, but
472 # we can't delete the file because it will be deleted in cleanup.
473 true >"${part_file}"
474 part_file="${temp_raw}"
475 fi
476
Alex Deymo20bdc702016-12-07 21:07:11 -0800477 # Extract the .map file (if one is available).
478 part_map_file=$(create_tempfile "${part}.map.XXXXXX")
479 CLEANUP_FILES+=("${part_map_file}")
Tao Bao07216d62018-04-11 11:36:38 -0700480 unzip -p "${image}" "${path_in_zip}/${part}.map" >"${part_map_file}" || \
Alex Deymo20bdc702016-12-07 21:07:11 -0800481 part_map_file=""
482
Alex Deymoa479a4d2016-05-11 18:13:49 -0700483 # delta_generator only supports images multiple of 4 KiB. For target images
484 # we pad the data with zeros if needed, but for source images we truncate
485 # down the data since the last block of the old image could be padded on
486 # disk with unknown data.
Alex Deymo48b502a2015-09-17 19:00:18 -0700487 filesize=$(stat -c%s "${part_file}")
488 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
Alex Deymoa479a4d2016-05-11 18:13:49 -0700489 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
490 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
491 : $(( filesize = filesize & -4096 ))
Alex Deymo04c23052017-04-12 18:15:30 -0700492 if [[ ${filesize} == 0 ]]; then
493 echo "Source partition ${part}.img is empty after rounding down," \
494 "skipping."
495 continue
496 fi
Alex Deymoa479a4d2016-05-11 18:13:49 -0700497 else
498 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
499 : $(( filesize = (filesize + 4095) & -4096 ))
500 fi
Sen Jiangd0e9a892016-07-22 16:28:07 -0700501 truncate_file "${part_file}" "${filesize}"
Alex Deymo48b502a2015-09-17 19:00:18 -0700502 fi
503
504 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
Alex Deymo20bdc702016-12-07 21:07:11 -0800505 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\""
Alex Deymo48b502a2015-09-17 19:00:18 -0700506 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
507 done
508}
509
Jason Kusumabe998f42015-09-03 15:53:13 -0700510validate_generate() {
511 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700512 die "You must specify an output filename with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700513
514 [[ -n "${FLAGS_target_image}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700515 die "You must specify a target image with --target_image FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700516}
517
518cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700519 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700520 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700521 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700522 fi
523
Alex Deymo48b502a2015-09-17 19:00:18 -0700524 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700525
Sen Jiang788c2d92016-03-09 12:48:40 -0800526 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
Alex Deymo89ff9e32015-09-15 19:29:01 -0700527 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700528 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700529 fi
530
Alex Deymo48b502a2015-09-17 19:00:18 -0700531 echo "Generating ${payload_type} update."
Alex Deymo168b5352015-11-04 13:51:52 -0800532 # Common payload args:
533 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
534
535 local part old_partitions="" new_partitions="" partition_names=""
Alex Deymo20bdc702016-12-07 21:07:11 -0800536 local old_mapfiles="" new_mapfiles=""
Sen Jiang788c2d92016-03-09 12:48:40 -0800537 for part in "${PARTITIONS_ORDER[@]}"; do
Alex Deymo168b5352015-11-04 13:51:52 -0800538 if [[ -n "${partition_names}" ]]; then
539 partition_names+=":"
540 new_partitions+=":"
541 old_partitions+=":"
Alex Deymo20bdc702016-12-07 21:07:11 -0800542 new_mapfiles+=":"
543 old_mapfiles+=":"
Alex Deymo168b5352015-11-04 13:51:52 -0800544 fi
545 partition_names+="${part}"
546 new_partitions+="${DST_PARTITIONS[${part}]}"
547 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800548 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}"
549 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}"
Alex Deymo168b5352015-11-04 13:51:52 -0800550 done
551
552 # Target image args:
553 GENERATOR_ARGS+=(
554 -partition_names="${partition_names}"
555 -new_partitions="${new_partitions}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800556 -new_mapfiles="${new_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700557 )
558
Alex Deymo89ff9e32015-09-15 19:29:01 -0700559 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo168b5352015-11-04 13:51:52 -0800560 # Source image args:
Jason Kusumabe998f42015-09-03 15:53:13 -0700561 GENERATOR_ARGS+=(
Alex Deymo168b5352015-11-04 13:51:52 -0800562 -old_partitions="${old_partitions}"
Alex Deymo20bdc702016-12-07 21:07:11 -0800563 -old_mapfiles="${old_mapfiles}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700564 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700565 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
566 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
567 fi
568 fi
569
570 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
571 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700572 fi
573
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700574 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
575 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
576 fi
577
Sen Jiang6f7b22c2015-11-12 15:50:39 -0800578 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
579 GENERATOR_ARGS+=(
580 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
581 )
582 fi
583
Jason Kusumabe998f42015-09-03 15:53:13 -0700584 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700585 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700586
Alex Deymo89ff9e32015-09-15 19:29:01 -0700587 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700588}
589
590validate_hash() {
591 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700592 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700593
594 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700595 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700596--unsigned_payload FILENAME"
597
Jason Kusumabe998f42015-09-03 15:53:13 -0700598 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700599 die "You must specify --payload_hash_file FILENAME"
Jason Kusumaf514c542015-11-05 18:43:45 -0800600
601 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700602 die "You must specify --metadata_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700603}
604
605cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700606 "${GENERATOR}" \
607 -in_file="${FLAGS_unsigned_payload}" \
608 -signature_size="${FLAGS_signature_size}" \
609 -out_hash_file="${FLAGS_payload_hash_file}" \
610 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700611
Jason Kusumabe998f42015-09-03 15:53:13 -0700612 echo "Done generating hash."
613}
614
615validate_sign() {
616 [[ -n "${FLAGS_signature_size}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700617 die "You must specify signature size with --signature_size SIZES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700618
619 [[ -n "${FLAGS_unsigned_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700620 die "You must specify the input unsigned payload with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700621--unsigned_payload FILENAME"
622
623 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700624 die "You must specify the output signed payload with --payload FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700625
626 [[ -n "${FLAGS_payload_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700627 die "You must specify the payload signature file with \
Jason Kusumabe998f42015-09-03 15:53:13 -0700628--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700629
630 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700631 die "You must specify the metadata signature file with \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700632--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700633}
634
635cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700636 GENERATOR_ARGS=(
637 -in_file="${FLAGS_unsigned_payload}"
638 -signature_size="${FLAGS_signature_size}"
Amin Hassanib4778292018-05-21 11:48:15 -0700639 -payload_signature_file="${FLAGS_payload_signature_file}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700640 -metadata_signature_file="${FLAGS_metadata_signature_file}"
641 -out_file="${FLAGS_payload}"
642 )
643
644 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
645 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
646 fi
647
648 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700649 echo "Done signing payload."
650}
651
Alex Deymo98e691c2016-02-04 21:05:45 -0800652validate_properties() {
653 [[ -n "${FLAGS_payload}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700654 die "You must specify the payload file with --payload FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800655
656 [[ -n "${FLAGS_properties_file}" ]] ||
Sen Jiang53f04d72016-07-13 16:43:39 -0700657 die "You must specify a non empty --properties_file FILENAME"
Alex Deymo98e691c2016-02-04 21:05:45 -0800658}
659
660cmd_properties() {
661 "${GENERATOR}" \
662 -in_file="${FLAGS_payload}" \
663 -properties_file="${FLAGS_properties_file}"
664}
665
Amin Hassani13520932017-07-26 11:26:05 -0700666validate_verify() {
667 [[ -n "${FLAGS_payload}" ]] ||
668 die "Error: you must specify an input filename with --payload FILENAME"
669
670 [[ -n "${FLAGS_target_image}" ]] ||
671 die "Error: you must specify a target image with --target_image FILENAME"
672}
673
674cmd_verify() {
675 local payload_type="delta"
676 if [[ -z "${FLAGS_source_image}" ]]; then
677 payload_type="full"
678 fi
679
680 echo "Extracting images for ${payload_type} update."
681
682 if [[ "${payload_type}" == "delta" ]]; then
683 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
684 fi
685 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
686
687 declare -A TMP_PARTITIONS
688 for part in "${PARTITIONS_ORDER[@]}"; do
689 local tmp_part=$(create_tempfile "tmp_part.bin.XXXXXX")
690 echo "Creating temporary target partition ${tmp_part} for ${part}"
691 CLEANUP_FILES+=("${tmp_part}")
692 TMP_PARTITIONS[${part}]=${tmp_part}
693 local FILESIZE=$(stat -c%s "${DST_PARTITIONS[${part}]}")
694 echo "Truncating ${TMP_PARTITIONS[${part}]} to ${FILESIZE}"
695 truncate_file "${TMP_PARTITIONS[${part}]}" "${FILESIZE}"
696 done
697
698 echo "Verifying ${payload_type} update."
699 # Common payload args:
700 GENERATOR_ARGS=( -in_file="${FLAGS_payload}" )
701
702 local part old_partitions="" new_partitions="" partition_names=""
703 for part in "${PARTITIONS_ORDER[@]}"; do
704 if [[ -n "${partition_names}" ]]; then
705 partition_names+=":"
706 new_partitions+=":"
707 old_partitions+=":"
708 fi
709 partition_names+="${part}"
710 new_partitions+="${TMP_PARTITIONS[${part}]}"
711 old_partitions+="${SRC_PARTITIONS[${part}]:-}"
712 done
713
714 # Target image args:
715 GENERATOR_ARGS+=(
716 -partition_names="${partition_names}"
717 -new_partitions="${new_partitions}"
718 )
719
720 if [[ "${payload_type}" == "delta" ]]; then
721 # Source image args:
722 GENERATOR_ARGS+=(
723 -old_partitions="${old_partitions}"
724 )
725 fi
726
Amin Hassania566cb62017-08-23 12:36:55 -0700727 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
728 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
729 fi
730
Amin Hassani13520932017-07-26 11:26:05 -0700731 echo "Running delta_generator to verify ${payload_type} payload with args: \
732${GENERATOR_ARGS[@]}"
733 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
734
735 if [[ $? -eq 0 ]]; then
736 echo "Done applying ${payload_type} update."
737 echo "Checking the newly generated partitions against the target partitions"
738 for part in "${PARTITIONS_ORDER[@]}"; do
739 cmp "${TMP_PARTITIONS[${part}]}" "${DST_PARTITIONS[${part}]}"
740 local not_str=""
741 if [[ $? -ne 0 ]]; then
742 not_str="in"
743 fi
744 echo "The new partition (${part}) is ${not_str}valid."
745 done
746 else
747 echo "Failed to apply ${payload_type} update."
748 fi
749}
750
Jason Kusumabe998f42015-09-03 15:53:13 -0700751# Sanity check that the real generator exists:
Sen Jiang13519752016-08-02 16:10:52 -0700752GENERATOR="$(which delta_generator || true)"
Jason Kusumabe998f42015-09-03 15:53:13 -0700753[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
754
755case "$COMMAND" in
756 generate) validate_generate
757 cmd_generate
758 ;;
759 hash) validate_hash
760 cmd_hash
761 ;;
762 sign) validate_sign
763 cmd_sign
764 ;;
Alex Deymo98e691c2016-02-04 21:05:45 -0800765 properties) validate_properties
766 cmd_properties
767 ;;
Amin Hassani13520932017-07-26 11:26:05 -0700768 verify) validate_verify
769 cmd_verify
770 ;;
Jason Kusumabe998f42015-09-03 15:53:13 -0700771esac