blob: 935dc73530d3905581d82dde4a6d3f7a50f263ca [file] [log] [blame]
Jason Kusumabe998f42015-09-03 15:53:13 -07001#!/bin/bash
2
3# Copyright 2015 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to generate a Brillo update for use by the update engine.
8#
9# usage: brillo_update_payload COMMAND [ARGS]
10# The following commands are supported:
11# generate generate an unsigned payload
12# hash generate a payload or metadata hash
13# sign generate a signed payload
14#
15# Generate command arguments:
Jason Kusuma9a4cae22015-10-08 18:17:57 -070016# --payload generated unsigned payload output file
17# --source_image if defined, generate a delta payload from the specified
18# image to the target_image
19# --target_image the target image that should be sent to clients
20# --metadata_size_file if defined, generate a file containing the size of the payload
21# metadata in bytes to the specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070022#
23# Hash command arguments:
24# --unsigned_payload the input unsigned payload to generate the hash from
25# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070026# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070027# --payload_hash_file if defined, generate a payload hash and output to the
28# specified file
29# --metadata_hash_file if defined, generate a metadata hash and output to the
30# specified file
31#
32# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070033# --unsigned_payload the input unsigned payload to insert the signatures
34# --payload the output signed payload
35# --signature_size signature sizes in bytes in the following format:
36# "size1:size2[:...]"
37# --payload_signature_file the payload signature files in the following
38# format:
39# "payload_signature1:payload_signature2[:...]"
40# --metadata_signature_file the metadata signature files in the following
41# format:
42# "metadata_signature1:metadata_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -070043# --metadata_size_file if defined, generate a file containing the size of
44# the signed payload metadata in bytes to the
45# specified file
Jason Kusumabe998f42015-09-03 15:53:13 -070046# Note that the number of signature sizes and payload signatures have to match.
47
Gilad Arnold957ce122015-10-14 16:02:55 -070048die() {
49 echo "brillo_update_payload: error: $*" >&2
50 exit 1
Jason Kusumabe998f42015-09-03 15:53:13 -070051}
52
Gilad Arnold957ce122015-10-14 16:02:55 -070053# Loads shflags. We first look at the default install location; then look for
54# crosutils (chroot); finally check our own directory (au-generator zipfile).
55load_shflags() {
56 local my_dir="$(dirname "$(readlink -f "$0")")"
57 local path
58 for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do
59 if [[ -r "${path}/shflags" ]]; then
60 . "${path}/shflags" || die "Could not load ${path}/shflags."
61 return
62 fi
63 done
64 die "Could not find shflags."
65}
66
67load_shflags
Jason Kusumabe998f42015-09-03 15:53:13 -070068
Alex Deymoc64ffd52015-09-25 18:10:07 -070069HELP_GENERATE="generate: Generate an unsigned update payload."
70HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
71for signing."
72HELP_SIGN="sign: Insert the signatures into the unsigned payload."
73
74usage() {
75 echo "Supported commands:"
76 echo
77 echo "${HELP_GENERATE}"
78 echo "${HELP_HASH}"
79 echo "${HELP_SIGN}"
80 echo
81 echo "Use: \"$0 <command> --help\" for more options."
82}
83
84# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -070085if [[ $# -lt 1 ]]; then
86 echo "Please specify a command [generate|hash|sign]"
87 exit 1
88fi
89
Alex Deymoc64ffd52015-09-25 18:10:07 -070090# Parse command.
91COMMAND="${1:-}"
92shift
93
94case "${COMMAND}" in
95 generate)
96 FLAGS_HELP="${HELP_GENERATE}"
97 ;;
98
99 hash)
100 FLAGS_HELP="${HELP_HASH}"
101 ;;
102
103 sign)
104 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700105 ;;
106 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700107 echo "Unrecognized command: \"${COMMAND}\"" >&2
108 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700109 exit 1
110 ;;
111esac
112
Jason Kusumabe998f42015-09-03 15:53:13 -0700113# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700114FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
115${FLAGS_HELP}"
116
117if [[ "${COMMAND}" == "generate" ]]; then
118 DEFINE_string payload "" \
119 "Path to output the generated unsigned payload file."
120 DEFINE_string target_image "" \
121 "Path to the target image that should be sent to clients."
122 DEFINE_string source_image "" \
123 "Optional: Path to a source image. If specified, this makes a delta update."
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700124 DEFINE_string metadata_size_file "" \
125 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700126fi
127if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
128 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
129 DEFINE_string signature_size "" \
130 "Signature sizes in bytes in the following format: size1:size2[:...]"
131fi
132if [[ "${COMMAND}" == "hash" ]]; then
133 DEFINE_string metadata_hash_file "" \
134 "Optional: Path to output metadata hash file."
135 DEFINE_string payload_hash_file "" \
136 "Optional: Path to output payload hash file."
137fi
138if [[ "${COMMAND}" == "sign" ]]; then
139 DEFINE_string payload "" \
140 "Path to output the generated unsigned payload file."
141 DEFINE_string metadata_signature_file "" \
142 "The metatada signatures in the following format: \
143metadata_signature1:metadata_signature2[:...]"
144 DEFINE_string payload_signature_file "" \
145 "The payload signatures in the following format: \
146payload_signature1:payload_signature2[:...]"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700147 DEFINE_string metadata_size_file "" \
148 "Optional: Path to output metadata size."
Alex Deymoc64ffd52015-09-25 18:10:07 -0700149fi
Jason Kusumabe998f42015-09-03 15:53:13 -0700150DEFINE_string work_dir "/tmp" "Where to dump temporary files."
151
152# Parse command line flag arguments
153FLAGS "$@" || exit 1
154eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700155set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700156
Alex Deymo89ff9e32015-09-15 19:29:01 -0700157# Associative arrays from partition name to file in the source and target
158# images. The size of the updated area must be the size of the file.
159declare -A SRC_PARTITIONS
160declare -A DST_PARTITIONS
161
162# A list of temporary files to remove during cleanup.
163CLEANUP_FILES=()
164
Alex Deymo48b502a2015-09-17 19:00:18 -0700165# Global options to force the version of the payload.
166FORCE_MAJOR_VERSION=""
167FORCE_MINOR_VERSION=""
168
Alex Deymoc97df432015-09-25 17:23:52 -0700169# read_option_int <file.txt> <option_key> [default_value]
170#
171# Reads the unsigned integer value associated with |option_key| in a key=value
172# file |file.txt|. Prints the read value if found and valid, otherwise prints
173# the |default_value|.
174read_option_uint() {
175 local file_txt="$1"
176 local option_key="$2"
177 local default_value="${3:-}"
178 local value
179 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
180 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
181 echo "${value}"
182 return
183 fi
184 fi
185 echo "${default_value}"
186}
187
Alex Deymo89ff9e32015-09-15 19:29:01 -0700188# Create a temporary file in the work_dir with an optional pattern name.
189# Prints the name of the newly created file.
190create_tempfile() {
191 local pattern="${1:-tempfile.XXXXXX}"
192 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
193}
Jason Kusumabe998f42015-09-03 15:53:13 -0700194
195cleanup() {
196 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700197 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700198
199 # If we are cleaning up after an error, or if we got an error during
200 # cleanup (even if we eventually succeeded) return a non-zero exit
201 # code. This triggers additional logging in most environments that call
202 # this script.
203 if [[ -n "${err}" ]]; then
204 die "Cleanup encountered an error."
205 fi
206}
207
208cleanup_on_error() {
209 trap - INT TERM ERR EXIT
210 cleanup
211 die "Cleanup success after an error."
212}
213
214cleanup_on_exit() {
215 trap - INT TERM ERR EXIT
216 cleanup
217}
218
219trap cleanup_on_error INT TERM ERR
220trap cleanup_on_exit EXIT
221
Alex Deymo48b502a2015-09-17 19:00:18 -0700222
223# extract_image <image> <partitions_array>
224#
225# Detect the format of the |image| file and extract its updatable partitions
226# into new temporary files. Add the list of partition names and its files to the
227# associative array passed in |partitions_array|.
228extract_image() {
229 local image="$1"
230
231 # Brillo images are zip files. We detect the 4-byte magic header of the zip
232 # file.
233 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
234 if [[ "${magic}" == "504b0304" ]]; then
235 echo "Detected .zip file, extracting Brillo image."
236 extract_image_brillo "$@"
237 return
238 fi
239
240 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
241 # bundled here and we will use it to extract the partitions, so the GPT
242 # headers must be valid.
243 if cgpt show -q -n "${image}" >/dev/null; then
244 echo "Detected GPT image, extracting Chrome OS image."
245 extract_image_cros "$@"
246 return
247 fi
248
249 die "Couldn't detect the image format of ${image}"
250}
251
Alex Deymo89ff9e32015-09-15 19:29:01 -0700252# extract_image_cros <image.bin> <partitions_array>
253#
Alex Deymo48b502a2015-09-17 19:00:18 -0700254# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700255extract_image_cros() {
256 local image="$1"
257 local partitions_array="$2"
258
259 local kernel root
260 kernel=$(create_tempfile "kernel.bin.XXXXXX")
261 CLEANUP_FILES+=("${kernel}")
262 root=$(create_tempfile "root.bin.XXXXXX")
263 CLEANUP_FILES+=("${root}")
264
265 cros_generate_update_payload --extract \
266 --image "${image}" \
267 --kern_path "${kernel}" --root_path "${root}" \
268 --work_dir "${FLAGS_work_dir}" --outside_chroot
269
Alex Deymo83f2f702015-10-14 14:49:33 -0700270 # Chrome OS uses major_version 1 payloads for all versions, even if the
271 # updater supports a newer major version.
272 FORCE_MAJOR_VERSION="1"
273
Alex Deymo48b502a2015-09-17 19:00:18 -0700274 # When generating legacy Chrome OS images, we need to use "boot" and "system"
275 # for the partition names to be compatible with updating Brillo devices with
276 # Chrome OS images.
277 eval ${partitions_array}[boot]=\""${kernel}"\"
278 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700279
280 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700281 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700282 varname="${partitions_array}[${part}]"
283 printf "md5sum of %s: " "${varname}"
284 md5sum "${!varname}"
285 done
286}
287
Alex Deymo48b502a2015-09-17 19:00:18 -0700288# extract_image_brillo <target_files.zip> <partitions_array>
289#
290# Extract the A/B updated partitions from a Brillo target_files zip file into
291# new temporary files.
292extract_image_brillo() {
293 local image="$1"
294 local partitions_array="$2"
295
296 # TODO(deymo): Read the list of partitions from the metadata. We should
297 # sanitize the list of partition names to be in [a-zA-Z0-9-]+.
298 local partitions=( "boot" "system" )
299
Alex Deymo83f2f702015-10-14 14:49:33 -0700300 # All Brillo updaters support major version 2.
301 FORCE_MAJOR_VERSION="2"
302
Alex Deymo48b502a2015-09-17 19:00:18 -0700303 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Alex Deymoc97df432015-09-25 17:23:52 -0700304 ue_config=$(create_tempfile "ue_config.XXXXXX")
305 CLEANUP_FILES+=("${ue_config}")
306 if ! unzip -p "${image}" "META/update_engine_config.txt" \
307 >"${ue_config}"; then
308 warn "No update_engine_config.txt found. Assuming pre-release image, \
309using payload minor version 2"
310 fi
Alex Deymo83f2f702015-10-14 14:49:33 -0700311 # For delta payloads, we use the major and minor version supported by the
312 # old updater.
Alex Deymoc97df432015-09-25 17:23:52 -0700313 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
314 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo83f2f702015-10-14 14:49:33 -0700315 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
316 "PAYLOAD_MAJOR_VERSION" 2)
Alex Deymo48b502a2015-09-17 19:00:18 -0700317 fi
318
319 local part part_file temp_raw filesize
320 for part in "${partitions[@]}"; do
321 part_file=$(create_tempfile "${part}.img.XXXXXX")
322 CLEANUP_FILES+=("${part_file}")
323 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
324
325 # If the partition is stored as an Android sparse image file, we need to
326 # convert them to a raw image for the update.
327 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
328 if [[ "${magic}" == "3aff26ed" ]]; then
329 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
330 CLEANUP_FILES+=("${temp_raw}")
331 echo "Converting Android sparse image ${part}.img to RAW."
332 simg2img "${part_file}" "${temp_raw}"
333 # At this point, we can drop the contents of the old part_file file, but
334 # we can't delete the file because it will be deleted in cleanup.
335 true >"${part_file}"
336 part_file="${temp_raw}"
337 fi
338
339 # delta_generator only supports images multiple of 4 KiB, so we pad with
340 # zeros if needed.
341 filesize=$(stat -c%s "${part_file}")
342 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
343 echo "Rounding up partition ${part}.img to multiple of 4 KiB."
344 : $(( filesize = (filesize + 4095) & -4096 ))
345 truncate --size="${filesize}" "${part_file}"
346 fi
347
348 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
349 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
350 done
351}
352
Jason Kusumabe998f42015-09-03 15:53:13 -0700353validate_generate() {
354 [[ -n "${FLAGS_payload}" ]] ||
355 die "Error: you must specify an output filename with --payload FILENAME"
356
357 [[ -n "${FLAGS_target_image}" ]] ||
358 die "Error: you must specify a target image with --target_image FILENAME"
359}
360
361cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700362 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700363 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700364 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700365 fi
366
Alex Deymo48b502a2015-09-17 19:00:18 -0700367 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700368
Alex Deymo48b502a2015-09-17 19:00:18 -0700369 extract_image "${FLAGS_target_image}" DST_PARTITIONS
Alex Deymo89ff9e32015-09-15 19:29:01 -0700370 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700371 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700372 fi
373
Alex Deymo48b502a2015-09-17 19:00:18 -0700374 echo "Generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700375 GENERATOR_ARGS=(
376 # Common payload args:
377 -out_file="${FLAGS_payload}"
378 # Target image args:
Alex Deymo89ff9e32015-09-15 19:29:01 -0700379 # TODO(deymo): Pass the list of partitions to the generator.
Alex Deymo48b502a2015-09-17 19:00:18 -0700380 -new_image="${DST_PARTITIONS[system]}"
381 -new_kernel="${DST_PARTITIONS[boot]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700382 )
383
Alex Deymo89ff9e32015-09-15 19:29:01 -0700384 if [[ "${payload_type}" == "delta" ]]; then
Jason Kusumabe998f42015-09-03 15:53:13 -0700385 GENERATOR_ARGS+=(
386 # Source image args:
Alex Deymo48b502a2015-09-17 19:00:18 -0700387 -old_image="${SRC_PARTITIONS[system]}"
388 -old_kernel="${SRC_PARTITIONS[boot]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700389 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700390 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
391 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
392 fi
393 fi
394
395 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
396 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700397 fi
398
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700399 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
400 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
401 fi
402
Jason Kusumabe998f42015-09-03 15:53:13 -0700403 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700404 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700405
Alex Deymo89ff9e32015-09-15 19:29:01 -0700406 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700407}
408
409validate_hash() {
410 [[ -n "${FLAGS_signature_size}" ]] ||
411 die "Error: you must specify signature size with --signature_size SIZES"
412
413 [[ -n "${FLAGS_unsigned_payload}" ]] ||
414 die "Error: you must specify the input unsigned payload with \
415--unsigned_payload FILENAME"
416
Jason Kusumabe998f42015-09-03 15:53:13 -0700417 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiangbf1266f2015-10-26 11:29:24 -0700418 die "Error: you must specify --payload_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700419}
420
421cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700422 "${GENERATOR}" \
423 -in_file="${FLAGS_unsigned_payload}" \
424 -signature_size="${FLAGS_signature_size}" \
425 -out_hash_file="${FLAGS_payload_hash_file}" \
426 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700427
Jason Kusumabe998f42015-09-03 15:53:13 -0700428 echo "Done generating hash."
429}
430
431validate_sign() {
432 [[ -n "${FLAGS_signature_size}" ]] ||
433 die "Error: you must specify signature size with --signature_size SIZES"
434
435 [[ -n "${FLAGS_unsigned_payload}" ]] ||
436 die "Error: you must specify the input unsigned payload with \
437--unsigned_payload FILENAME"
438
439 [[ -n "${FLAGS_payload}" ]] ||
440 die "Error: you must specify the output signed payload with \
441--payload FILENAME"
442
443 [[ -n "${FLAGS_payload_signature_file}" ]] ||
444 die "Error: you must specify the payload signature file with \
445--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700446
447 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
448 die "Error: you must specify the metadata signature file with \
449--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700450}
451
452cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700453 GENERATOR_ARGS=(
454 -in_file="${FLAGS_unsigned_payload}"
455 -signature_size="${FLAGS_signature_size}"
456 -signature_file="${FLAGS_payload_signature_file}"
457 -metadata_signature_file="${FLAGS_metadata_signature_file}"
458 -out_file="${FLAGS_payload}"
459 )
460
461 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
462 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
463 fi
464
465 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700466 echo "Done signing payload."
467}
468
469# TODO: Extract the input zip files once the format is finalized
470
471# Sanity check that the real generator exists:
472GENERATOR="$(which delta_generator)"
473[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
474
475case "$COMMAND" in
476 generate) validate_generate
477 cmd_generate
478 ;;
479 hash) validate_hash
480 cmd_hash
481 ;;
482 sign) validate_sign
483 cmd_sign
484 ;;
485esac