blob: 911bfaeda3485aebb0e4ec3937721d005868b693 [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:
16# --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#
21# Hash command arguments:
22# --unsigned_payload the input unsigned payload to generate the hash from
23# --signature_size signature sizes in bytes in the following format:
Alex Deymo89ff9e32015-09-15 19:29:01 -070024# "size1:size2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070025# --payload_hash_file if defined, generate a payload hash and output to the
26# specified file
27# --metadata_hash_file if defined, generate a metadata hash and output to the
28# specified file
29#
30# Sign command arguments:
Alex Deymo89ff9e32015-09-15 19:29:01 -070031# --unsigned_payload the input unsigned payload to insert the signatures
32# --payload the output signed payload
33# --signature_size signature sizes in bytes in the following format:
34# "size1:size2[:...]"
35# --payload_signature_file the payload signature files in the following
36# format:
37# "payload_signature1:payload_signature2[:...]"
38# --metadata_signature_file the metadata signature files in the following
39# format:
40# "metadata_signature1:metadata_signature2[:...]"
Jason Kusumabe998f42015-09-03 15:53:13 -070041# Note that the number of signature sizes and payload signatures have to match.
42
43# Load common CrOS utilities. Inside the chroot this file is installed in
44# /usr/lib/crosutils. This script may also be called from a zipfile, in which
45# case common.sh will be in the current directory.
46find_common_sh() {
47 local thisdir="$(dirname "$(readlink -f "$0")")"
48 local common_paths=(/usr/lib/crosutils "${thisdir}")
49 local path
50
51 SCRIPT_ROOT="${common_paths[0]}"
52 for path in "${common_paths[@]}"; do
53 if [[ -r "${path}/common.sh" ]]; then
54 SCRIPT_ROOT="${path}"
55 break
56 fi
57 done
58
59 # We have to fake GCLIENT_ROOT in case we're running inside
60 # au_zip enviroment. GCLIENT_ROOT detection became fatal.
61 [[ "${SCRIPT_ROOT}" == "${thisdir}" ]] && export GCLIENT_ROOT="."
62}
63
64find_common_sh
65. "${SCRIPT_ROOT}/common.sh" || exit 1
66
Alex Deymoc64ffd52015-09-25 18:10:07 -070067HELP_GENERATE="generate: Generate an unsigned update payload."
68HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
69for signing."
70HELP_SIGN="sign: Insert the signatures into the unsigned payload."
71
72usage() {
73 echo "Supported commands:"
74 echo
75 echo "${HELP_GENERATE}"
76 echo "${HELP_HASH}"
77 echo "${HELP_SIGN}"
78 echo
79 echo "Use: \"$0 <command> --help\" for more options."
80}
81
82# Check that a command is specified.
Jason Kusumabe998f42015-09-03 15:53:13 -070083if [[ $# -lt 1 ]]; then
84 echo "Please specify a command [generate|hash|sign]"
85 exit 1
86fi
87
Alex Deymoc64ffd52015-09-25 18:10:07 -070088# Parse command.
89COMMAND="${1:-}"
90shift
91
92case "${COMMAND}" in
93 generate)
94 FLAGS_HELP="${HELP_GENERATE}"
95 ;;
96
97 hash)
98 FLAGS_HELP="${HELP_HASH}"
99 ;;
100
101 sign)
102 FLAGS_HELP="${HELP_SIGN}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700103 ;;
104 *)
Alex Deymoc64ffd52015-09-25 18:10:07 -0700105 echo "Unrecognized command: \"${COMMAND}\"" >&2
106 usage >&2
Jason Kusumabe998f42015-09-03 15:53:13 -0700107 exit 1
108 ;;
109esac
110
Jason Kusumabe998f42015-09-03 15:53:13 -0700111# Flags
Alex Deymoc64ffd52015-09-25 18:10:07 -0700112FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
113${FLAGS_HELP}"
114
115if [[ "${COMMAND}" == "generate" ]]; then
116 DEFINE_string payload "" \
117 "Path to output the generated unsigned payload file."
118 DEFINE_string target_image "" \
119 "Path to the target image that should be sent to clients."
120 DEFINE_string source_image "" \
121 "Optional: Path to a source image. If specified, this makes a delta update."
122fi
123if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
124 DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
125 DEFINE_string signature_size "" \
126 "Signature sizes in bytes in the following format: size1:size2[:...]"
127fi
128if [[ "${COMMAND}" == "hash" ]]; then
129 DEFINE_string metadata_hash_file "" \
130 "Optional: Path to output metadata hash file."
131 DEFINE_string payload_hash_file "" \
132 "Optional: Path to output payload hash file."
133fi
134if [[ "${COMMAND}" == "sign" ]]; then
135 DEFINE_string payload "" \
136 "Path to output the generated unsigned payload file."
137 DEFINE_string metadata_signature_file "" \
138 "The metatada signatures in the following format: \
139metadata_signature1:metadata_signature2[:...]"
140 DEFINE_string payload_signature_file "" \
141 "The payload signatures in the following format: \
142payload_signature1:payload_signature2[:...]"
143fi
Jason Kusumabe998f42015-09-03 15:53:13 -0700144DEFINE_string work_dir "/tmp" "Where to dump temporary files."
145
146# Parse command line flag arguments
147FLAGS "$@" || exit 1
148eval set -- "${FLAGS_ARGV}"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700149set -e
Jason Kusumabe998f42015-09-03 15:53:13 -0700150
Alex Deymo89ff9e32015-09-15 19:29:01 -0700151# Associative arrays from partition name to file in the source and target
152# images. The size of the updated area must be the size of the file.
153declare -A SRC_PARTITIONS
154declare -A DST_PARTITIONS
155
156# A list of temporary files to remove during cleanup.
157CLEANUP_FILES=()
158
Alex Deymo48b502a2015-09-17 19:00:18 -0700159# Global options to force the version of the payload.
160FORCE_MAJOR_VERSION=""
161FORCE_MINOR_VERSION=""
162
Alex Deymoc97df432015-09-25 17:23:52 -0700163# read_option_int <file.txt> <option_key> [default_value]
164#
165# Reads the unsigned integer value associated with |option_key| in a key=value
166# file |file.txt|. Prints the read value if found and valid, otherwise prints
167# the |default_value|.
168read_option_uint() {
169 local file_txt="$1"
170 local option_key="$2"
171 local default_value="${3:-}"
172 local value
173 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
174 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
175 echo "${value}"
176 return
177 fi
178 fi
179 echo "${default_value}"
180}
181
Alex Deymo89ff9e32015-09-15 19:29:01 -0700182# Create a temporary file in the work_dir with an optional pattern name.
183# Prints the name of the newly created file.
184create_tempfile() {
185 local pattern="${1:-tempfile.XXXXXX}"
186 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
187}
Jason Kusumabe998f42015-09-03 15:53:13 -0700188
189cleanup() {
190 local err=""
Alex Deymo89ff9e32015-09-15 19:29:01 -0700191 rm -f "${CLEANUP_FILES[@]}" || err=1
Jason Kusumabe998f42015-09-03 15:53:13 -0700192
193 # If we are cleaning up after an error, or if we got an error during
194 # cleanup (even if we eventually succeeded) return a non-zero exit
195 # code. This triggers additional logging in most environments that call
196 # this script.
197 if [[ -n "${err}" ]]; then
198 die "Cleanup encountered an error."
199 fi
200}
201
202cleanup_on_error() {
203 trap - INT TERM ERR EXIT
204 cleanup
205 die "Cleanup success after an error."
206}
207
208cleanup_on_exit() {
209 trap - INT TERM ERR EXIT
210 cleanup
211}
212
213trap cleanup_on_error INT TERM ERR
214trap cleanup_on_exit EXIT
215
Alex Deymo48b502a2015-09-17 19:00:18 -0700216
217# extract_image <image> <partitions_array>
218#
219# Detect the format of the |image| file and extract its updatable partitions
220# into new temporary files. Add the list of partition names and its files to the
221# associative array passed in |partitions_array|.
222extract_image() {
223 local image="$1"
224
225 # Brillo images are zip files. We detect the 4-byte magic header of the zip
226 # file.
227 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
228 if [[ "${magic}" == "504b0304" ]]; then
229 echo "Detected .zip file, extracting Brillo image."
230 extract_image_brillo "$@"
231 return
232 fi
233
234 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
235 # bundled here and we will use it to extract the partitions, so the GPT
236 # headers must be valid.
237 if cgpt show -q -n "${image}" >/dev/null; then
238 echo "Detected GPT image, extracting Chrome OS image."
239 extract_image_cros "$@"
240 return
241 fi
242
243 die "Couldn't detect the image format of ${image}"
244}
245
Alex Deymo89ff9e32015-09-15 19:29:01 -0700246# extract_image_cros <image.bin> <partitions_array>
247#
Alex Deymo48b502a2015-09-17 19:00:18 -0700248# Extract Chromium OS recovery images into new temporary files.
Alex Deymo89ff9e32015-09-15 19:29:01 -0700249extract_image_cros() {
250 local image="$1"
251 local partitions_array="$2"
252
253 local kernel root
254 kernel=$(create_tempfile "kernel.bin.XXXXXX")
255 CLEANUP_FILES+=("${kernel}")
256 root=$(create_tempfile "root.bin.XXXXXX")
257 CLEANUP_FILES+=("${root}")
258
259 cros_generate_update_payload --extract \
260 --image "${image}" \
261 --kern_path "${kernel}" --root_path "${root}" \
262 --work_dir "${FLAGS_work_dir}" --outside_chroot
263
Alex Deymo48b502a2015-09-17 19:00:18 -0700264 # When generating legacy Chrome OS images, we need to use "boot" and "system"
265 # for the partition names to be compatible with updating Brillo devices with
266 # Chrome OS images.
267 eval ${partitions_array}[boot]=\""${kernel}"\"
268 eval ${partitions_array}[system]=\""${root}"\"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700269
270 local part varname
Alex Deymo48b502a2015-09-17 19:00:18 -0700271 for part in boot system; do
Alex Deymo89ff9e32015-09-15 19:29:01 -0700272 varname="${partitions_array}[${part}]"
273 printf "md5sum of %s: " "${varname}"
274 md5sum "${!varname}"
275 done
276}
277
Alex Deymo48b502a2015-09-17 19:00:18 -0700278# extract_image_brillo <target_files.zip> <partitions_array>
279#
280# Extract the A/B updated partitions from a Brillo target_files zip file into
281# new temporary files.
282extract_image_brillo() {
283 local image="$1"
284 local partitions_array="$2"
285
286 # TODO(deymo): Read the list of partitions from the metadata. We should
287 # sanitize the list of partition names to be in [a-zA-Z0-9-]+.
288 local partitions=( "boot" "system" )
289
290 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
Alex Deymoc97df432015-09-25 17:23:52 -0700291 ue_config=$(create_tempfile "ue_config.XXXXXX")
292 CLEANUP_FILES+=("${ue_config}")
293 if ! unzip -p "${image}" "META/update_engine_config.txt" \
294 >"${ue_config}"; then
295 warn "No update_engine_config.txt found. Assuming pre-release image, \
296using payload minor version 2"
297 fi
298 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
299 "PAYLOAD_MINOR_VERSION" 2)
Alex Deymo48b502a2015-09-17 19:00:18 -0700300 fi
301
302 local part part_file temp_raw filesize
303 for part in "${partitions[@]}"; do
304 part_file=$(create_tempfile "${part}.img.XXXXXX")
305 CLEANUP_FILES+=("${part_file}")
306 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
307
308 # If the partition is stored as an Android sparse image file, we need to
309 # convert them to a raw image for the update.
310 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
311 if [[ "${magic}" == "3aff26ed" ]]; then
312 temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
313 CLEANUP_FILES+=("${temp_raw}")
314 echo "Converting Android sparse image ${part}.img to RAW."
315 simg2img "${part_file}" "${temp_raw}"
316 # At this point, we can drop the contents of the old part_file file, but
317 # we can't delete the file because it will be deleted in cleanup.
318 true >"${part_file}"
319 part_file="${temp_raw}"
320 fi
321
322 # delta_generator only supports images multiple of 4 KiB, so we pad with
323 # zeros if needed.
324 filesize=$(stat -c%s "${part_file}")
325 if [[ $(( filesize % 4096 )) -ne 0 ]]; then
326 echo "Rounding up partition ${part}.img to multiple of 4 KiB."
327 : $(( filesize = (filesize + 4095) & -4096 ))
328 truncate --size="${filesize}" "${part_file}"
329 fi
330
331 eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
332 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
333 done
334}
335
Jason Kusumabe998f42015-09-03 15:53:13 -0700336validate_generate() {
337 [[ -n "${FLAGS_payload}" ]] ||
338 die "Error: you must specify an output filename with --payload FILENAME"
339
340 [[ -n "${FLAGS_target_image}" ]] ||
341 die "Error: you must specify a target image with --target_image FILENAME"
342}
343
344cmd_generate() {
Alex Deymo89ff9e32015-09-15 19:29:01 -0700345 local payload_type="delta"
Jason Kusumabe998f42015-09-03 15:53:13 -0700346 if [[ -z "${FLAGS_source_image}" ]]; then
Alex Deymo89ff9e32015-09-15 19:29:01 -0700347 payload_type="full"
Jason Kusumabe998f42015-09-03 15:53:13 -0700348 fi
349
Alex Deymo48b502a2015-09-17 19:00:18 -0700350 echo "Extracting images for ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700351
Alex Deymo48b502a2015-09-17 19:00:18 -0700352 extract_image "${FLAGS_target_image}" DST_PARTITIONS
Alex Deymo89ff9e32015-09-15 19:29:01 -0700353 if [[ "${payload_type}" == "delta" ]]; then
Alex Deymo48b502a2015-09-17 19:00:18 -0700354 extract_image "${FLAGS_source_image}" SRC_PARTITIONS
Jason Kusumabe998f42015-09-03 15:53:13 -0700355 fi
356
Alex Deymo48b502a2015-09-17 19:00:18 -0700357 echo "Generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700358 GENERATOR_ARGS=(
359 # Common payload args:
360 -out_file="${FLAGS_payload}"
361 # Target image args:
Alex Deymo89ff9e32015-09-15 19:29:01 -0700362 # TODO(deymo): Pass the list of partitions to the generator.
Alex Deymo48b502a2015-09-17 19:00:18 -0700363 -new_image="${DST_PARTITIONS[system]}"
364 -new_kernel="${DST_PARTITIONS[boot]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700365 )
366
Alex Deymo89ff9e32015-09-15 19:29:01 -0700367 if [[ "${payload_type}" == "delta" ]]; then
Jason Kusumabe998f42015-09-03 15:53:13 -0700368 GENERATOR_ARGS+=(
369 # Source image args:
Alex Deymo48b502a2015-09-17 19:00:18 -0700370 -old_image="${SRC_PARTITIONS[system]}"
371 -old_kernel="${SRC_PARTITIONS[boot]}"
Jason Kusumabe998f42015-09-03 15:53:13 -0700372 )
Alex Deymo48b502a2015-09-17 19:00:18 -0700373 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
374 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
375 fi
376 fi
377
378 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
379 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
Jason Kusumabe998f42015-09-03 15:53:13 -0700380 fi
381
382 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
383 "${GENERATOR}" "${GENERATOR_ARGS[@]}"
384
Alex Deymo89ff9e32015-09-15 19:29:01 -0700385 echo "Done generating ${payload_type} update."
Jason Kusumabe998f42015-09-03 15:53:13 -0700386}
387
388validate_hash() {
389 [[ -n "${FLAGS_signature_size}" ]] ||
390 die "Error: you must specify signature size with --signature_size SIZES"
391
392 [[ -n "${FLAGS_unsigned_payload}" ]] ||
393 die "Error: you must specify the input unsigned payload with \
394--unsigned_payload FILENAME"
395
396 [[ -n "${FLAGS_metadata_hash_file}" ]] ||
397 [[ -n "${FLAGS_payload_hash_file}" ]] ||
398 die "Error: you must specify --metadata_hash_file FILENAME \
399or --payload_hash_file FILENAME"
400}
401
402cmd_hash() {
403 if [[ -n "${FLAGS_metadata_hash_file}" ]]; then
404 "${GENERATOR}" \
405 -in_file="${FLAGS_unsigned_payload}" \
406 -signature_size="${FLAGS_signature_size}" \
407 -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
408 fi
409
410 if [[ -n "${FLAGS_payload_hash_file}" ]]; then
411 "${GENERATOR}" \
412 -in_file="${FLAGS_unsigned_payload}" \
413 -signature_size="${FLAGS_signature_size}" \
414 -out_hash_file="${FLAGS_payload_hash_file}"
415 fi
416 echo "Done generating hash."
417}
418
419validate_sign() {
420 [[ -n "${FLAGS_signature_size}" ]] ||
421 die "Error: you must specify signature size with --signature_size SIZES"
422
423 [[ -n "${FLAGS_unsigned_payload}" ]] ||
424 die "Error: you must specify the input unsigned payload with \
425--unsigned_payload FILENAME"
426
427 [[ -n "${FLAGS_payload}" ]] ||
428 die "Error: you must specify the output signed payload with \
429--payload FILENAME"
430
431 [[ -n "${FLAGS_payload_signature_file}" ]] ||
432 die "Error: you must specify the payload signature file with \
433--payload_signature_file SIGNATURES"
Alex Deymo89ff9e32015-09-15 19:29:01 -0700434
435 [[ -n "${FLAGS_metadata_signature_file}" ]] ||
436 die "Error: you must specify the metadata signature file with \
437--metadata_signature_file SIGNATURES"
Jason Kusumabe998f42015-09-03 15:53:13 -0700438}
439
440cmd_sign() {
441 "${GENERATOR}" \
442 -in_file="${FLAGS_unsigned_payload}" \
443 -signature_size="${FLAGS_signature_size}" \
444 -signature_file="${FLAGS_payload_signature_file}" \
Alex Deymo89ff9e32015-09-15 19:29:01 -0700445 -metadata_signature_file="${FLAGS_metadata_signature_file}" \
Jason Kusumabe998f42015-09-03 15:53:13 -0700446 -out_file="${FLAGS_payload}"
447 echo "Done signing payload."
448}
449
450# TODO: Extract the input zip files once the format is finalized
451
452# Sanity check that the real generator exists:
453GENERATOR="$(which delta_generator)"
454[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
455
456case "$COMMAND" in
457 generate) validate_generate
458 cmd_generate
459 ;;
460 hash) validate_hash
461 cmd_hash
462 ;;
463 sign) validate_sign
464 cmd_sign
465 ;;
466esac