blob: 50102c712c4479679344ff2b0cb9b2db18c4aa23 [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 Deymo48b502a2015-09-17 19:00:18 -0700270 # When generating legacy Chrome OS images, we need to use "boot" and "system"
271 # for the partition names to be compatible with updating Brillo devices with
272 # Chrome OS images.
273 eval ${partitions_array}[boot]=\""${kernel}"\"
274 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700275
276 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700277 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700278 varname="${partitions_array}[${part}]"
279 printf "md5sum of %s: " "${varname}"
280 md5sum "${!varname}"
281 done
282}
283
Alex Deymo48b502a2015-09-17 19:00:18 -0700284# extract_image_brillo <target_files.zip> <partitions_array>
285#
286# Extract the A/B updated partitions from a Brillo target_files zip file into
287# new temporary files.
288extract_image_brillo() {
289 local image="$1"
290 local partitions_array="$2"
291
292 # TODO(deymo): Read the list of partitions from the metadata. We should
293 # sanitize the list of partition names to be in [a-zA-Z0-9-]+.
294 local partitions=( "boot" "system" )
295
296 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Alex Deymoc97df432015-09-25 17:23:52 -0700297 ue_config=$(create_tempfile "ue_config.XXXXXX")
298 CLEANUP_FILES+=("${ue_config}")
299 if ! unzip -p "${image}" "META/update_engine_config.txt" \
300 >"${ue_config}"; then
301 warn "No update_engine_config.txt found. Assuming pre-release image, \
302using payload minor version 2"
303 fi
304 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
305 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo48b502a2015-09-17 19:00:18 -0700306 fi
307
308 local part part_file temp_raw filesize
309 for part in "${partitions[@]}"; do
310 part_file=$(create_tempfile "${part}.img.XXXXXX")
311 CLEANUP_FILES+=("${part_file}")
312 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
313
314 # If the partition is stored as an Android sparse image file, we need to
315 # convert them to a raw image for the update.
316 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
317 if [[ "${magic}" == "3aff26ed" ]]; then
318 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
319 CLEANUP_FILES+=("${temp_raw}")
320 echo "Converting Android sparse image ${part}.img to RAW."
321 simg2img "${part_file}" "${temp_raw}"
322 # At this point, we can drop the contents of the old part_file file, but
323 # we can't delete the file because it will be deleted in cleanup.
324 true >"${part_file}"
325 part_file="${temp_raw}"
326 fi
327
328 # delta_generator only supports images multiple of 4 KiB, so we pad with
329 # zeros if needed.
330 filesize=$(stat -c%s "${part_file}")
331 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
332 echo "Rounding up partition ${part}.img to multiple of 4 KiB."
333 : $(( filesize = (filesize + 4095) & -4096 ))
334 truncate --size="${filesize}" "${part_file}"
335 fi
336
337 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
338 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
339 done
340}
341
Jason Kusumabe998f42015-09-03 15:53:13 -0700342validate_generate() {
343 [[ -n "${FLAGS_payload}" ]] ||
344 die "Error: you must specify an output filename with --payload FILENAME"
345
346 [[ -n "${FLAGS_target_image}" ]] ||
347 die "Error: you must specify a target image with --target_image FILENAME"
348}
349
350cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700351 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700352 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700353 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700354 fi
355
Alex Deymo48b502a2015-09-17 19:00:18 -0700356 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700357
Alex Deymo48b502a2015-09-17 19:00:18 -0700358 extract_image "${FLAGS_target_image}" DST_PARTITIONS
Alex Deymo89ff9e32015-09-15 19:29:01 -0700359 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700360 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700361 fi
362
Alex Deymo48b502a2015-09-17 19:00:18 -0700363 echo "Generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700364 GENERATOR_ARGS=(
365 # Common payload args:
366 -out_file="${FLAGS_payload}"
367 # Target image args:
Alex Deymo89ff9e32015-09-15 19:29:01 -0700368 # TODO(deymo): Pass the list of partitions to the generator.
Alex Deymo48b502a2015-09-17 19:00:18 -0700369 -new_image="${DST_PARTITIONS[system]}"
370 -new_kernel="${DST_PARTITIONS[boot]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700371 )
372
Alex Deymo89ff9e32015-09-15 19:29:01 -0700373 if [[ "${payload_type}" == "delta" ]]; then
Jason Kusumabe998f42015-09-03 15:53:13 -0700374 GENERATOR_ARGS+=(
375 # Source image args:
Alex Deymo48b502a2015-09-17 19:00:18 -0700376 -old_image="${SRC_PARTITIONS[system]}"
377 -old_kernel="${SRC_PARTITIONS[boot]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700378 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700379 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
380 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
381 fi
382 fi
383
384 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
385 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700386 fi
387
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700388 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
389 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
390 fi
391
Jason Kusumabe998f42015-09-03 15:53:13 -0700392 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700393 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700394
Alex Deymo89ff9e32015-09-15 19:29:01 -0700395 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700396}
397
398validate_hash() {
399 [[ -n "${FLAGS_signature_size}" ]] ||
400 die "Error: you must specify signature size with --signature_size SIZES"
401
402 [[ -n "${FLAGS_unsigned_payload}" ]] ||
403 die "Error: you must specify the input unsigned payload with \
404--unsigned_payload FILENAME"
405
Jason Kusumabe998f42015-09-03 15:53:13 -0700406 [[ -n "${FLAGS_payload_hash_file}" ]] ||
Sen Jiangbf1266f2015-10-26 11:29:24 -0700407 die "Error: you must specify --payload_hash_file FILENAME"
Jason Kusumabe998f42015-09-03 15:53:13 -0700408}
409
410cmd_hash() {
Sen Jiangbf1266f2015-10-26 11:29:24 -0700411 "${GENERATOR}" \
412 -in_file="${FLAGS_unsigned_payload}" \
413 -signature_size="${FLAGS_signature_size}" \
414 -out_hash_file="${FLAGS_payload_hash_file}" \
415 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700416
Jason Kusumabe998f42015-09-03 15:53:13 -0700417 echo "Done generating hash."
418}
419
420validate_sign() {
421 [[ -n "${FLAGS_signature_size}" ]] ||
422 die "Error: you must specify signature size with --signature_size SIZES"
423
424 [[ -n "${FLAGS_unsigned_payload}" ]] ||
425 die "Error: you must specify the input unsigned payload with \
426--unsigned_payload FILENAME"
427
428 [[ -n "${FLAGS_payload}" ]] ||
429 die "Error: you must specify the output signed payload with \
430--payload FILENAME"
431
432 [[ -n "${FLAGS_payload_signature_file}" ]] ||
433 die "Error: you must specify the payload signature file with \
434--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700435
436 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
437 die "Error: you must specify the metadata signature file with \
438--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700439}
440
441cmd_sign() {
Jason Kusuma9a4cae22015-10-08 18:17:57 -0700442 GENERATOR_ARGS=(
443 -in_file="${FLAGS_unsigned_payload}"
444 -signature_size="${FLAGS_signature_size}"
445 -signature_file="${FLAGS_payload_signature_file}"
446 -metadata_signature_file="${FLAGS_metadata_signature_file}"
447 -out_file="${FLAGS_payload}"
448 )
449
450 if [[ -n "${FLAGS_metadata_size_file}" ]]; then
451 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
452 fi
453
454 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700455 echo "Done signing payload."
456}
457
458# TODO: Extract the input zip files once the format is finalized
459
460# Sanity check that the real generator exists:
461GENERATOR="$(which delta_generator)"
462[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
463
464case "$COMMAND" in
465 generate) validate_generate
466 cmd_generate
467 ;;
468 hash) validate_hash
469 cmd_hash
470 ;;
471 sign) validate_sign
472 cmd_sign
473 ;;
474esac