brillo_update_payload revision e0d04282910d423ddc950dd38224c001efc41d42
1#!/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#  properties  generate a properties file from a payload
15#
16#  Generate command arguments:
17#  --payload             generated unsigned payload output file
18#  --source_image        if defined, generate a delta payload from the specified
19#                        image to the target_image
20#  --target_image        the target image that should be sent to clients
21#  --metadata_size_file  if defined, generate a file containing the size of the payload
22#                        metadata in bytes to the specified file
23#
24#  Hash command arguments:
25#  --unsigned_payload    the input unsigned payload to generate the hash from
26#  --signature_size      signature sizes in bytes in the following format:
27#                        "size1:size2[:...]"
28#  --payload_hash_file   if defined, generate a payload hash and output to the
29#                        specified file
30#  --metadata_hash_file  if defined, generate a metadata hash and output to the
31#                        specified file
32#
33#  Sign command arguments:
34#  --unsigned_payload        the input unsigned payload to insert the signatures
35#  --payload                 the output signed payload
36#  --signature_size          signature sizes in bytes in the following format:
37#                            "size1:size2[:...]"
38#  --payload_signature_file  the payload signature files in the following
39#                            format:
40#                            "payload_signature1:payload_signature2[:...]"
41#  --metadata_signature_file the metadata signature files in the following
42#                            format:
43#                            "metadata_signature1:metadata_signature2[:...]"
44#  --metadata_size_file      if defined, generate a file containing the size of
45#                            the signed payload metadata in bytes to the
46#                            specified file
47#  Note that the number of signature sizes and payload signatures have to match.
48#
49#  Properties command arguments:
50#  --payload                 the input signed or unsigned payload
51#  --properties_file         the output path where to write the properties, or
52#                            '-' for stdout.
53
54
55# Exit codes:
56EX_UNSUPPORTED_DELTA=100
57
58warn() {
59  echo "brillo_update_payload: warning: $*" >&2
60}
61
62die() {
63  echo "brillo_update_payload: error: $*" >&2
64  exit 1
65}
66
67# Loads shflags. We first look at the default install location; then look for
68# crosutils (chroot); finally check our own directory (au-generator zipfile).
69load_shflags() {
70  local my_dir="$(dirname "$(readlink -f "$0")")"
71  local path
72  for path in /usr/share/misc {/usr/lib/crosutils,"${my_dir}"}/lib/shflags; do
73    if [[ -r "${path}/shflags" ]]; then
74      . "${path}/shflags" || die "Could not load ${path}/shflags."
75      return
76    fi
77  done
78  die "Could not find shflags."
79}
80
81load_shflags
82
83HELP_GENERATE="generate: Generate an unsigned update payload."
84HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
85for signing."
86HELP_SIGN="sign: Insert the signatures into the unsigned payload."
87HELP_PROPERTIES="properties: Extract payload properties to a file."
88
89usage() {
90  echo "Supported commands:"
91  echo
92  echo "${HELP_GENERATE}"
93  echo "${HELP_HASH}"
94  echo "${HELP_SIGN}"
95  echo "${HELP_PROPERTIES}"
96  echo
97  echo "Use: \"$0 <command> --help\" for more options."
98}
99
100# Check that a command is specified.
101if [[ $# -lt 1 ]]; then
102  echo "Please specify a command [generate|hash|sign|properties]"
103  exit 1
104fi
105
106# Parse command.
107COMMAND="${1:-}"
108shift
109
110case "${COMMAND}" in
111  generate)
112    FLAGS_HELP="${HELP_GENERATE}"
113    ;;
114
115  hash)
116    FLAGS_HELP="${HELP_HASH}"
117    ;;
118
119  sign)
120    FLAGS_HELP="${HELP_SIGN}"
121    ;;
122
123  properties)
124    FLAGS_HELP="${HELP_PROPERTIES}"
125    ;;
126  *)
127    echo "Unrecognized command: \"${COMMAND}\"" >&2
128    usage >&2
129    exit 1
130    ;;
131esac
132
133# Flags
134FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
135${FLAGS_HELP}"
136
137if [[ "${COMMAND}" == "generate" ]]; then
138  DEFINE_string payload "" \
139    "Path to output the generated unsigned payload file."
140  DEFINE_string target_image "" \
141    "Path to the target image that should be sent to clients."
142  DEFINE_string source_image "" \
143    "Optional: Path to a source image. If specified, this makes a delta update."
144  DEFINE_string metadata_size_file "" \
145    "Optional: Path to output metadata size."
146fi
147if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
148  DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
149  DEFINE_string signature_size "" \
150    "Signature sizes in bytes in the following format: size1:size2[:...]"
151fi
152if [[ "${COMMAND}" == "hash" ]]; then
153  DEFINE_string metadata_hash_file "" \
154    "Optional: Path to output metadata hash file."
155  DEFINE_string payload_hash_file "" \
156    "Optional: Path to output payload hash file."
157fi
158if [[ "${COMMAND}" == "sign" ]]; then
159  DEFINE_string payload "" \
160    "Path to output the generated unsigned payload file."
161  DEFINE_string metadata_signature_file "" \
162    "The metatada signatures in the following format: \
163metadata_signature1:metadata_signature2[:...]"
164  DEFINE_string payload_signature_file "" \
165    "The payload signatures in the following format: \
166payload_signature1:payload_signature2[:...]"
167  DEFINE_string metadata_size_file "" \
168    "Optional: Path to output metadata size."
169fi
170if [[ "${COMMAND}" == "properties" ]]; then
171  DEFINE_string payload "" \
172    "Path to the input signed or unsigned payload file."
173  DEFINE_string properties_file "-" \
174    "Path to output the extracted property files. If '-' is passed stdout will \
175be used."
176fi
177
178DEFINE_string work_dir "/tmp" "Where to dump temporary files."
179
180# Parse command line flag arguments
181FLAGS "$@" || exit 1
182eval set -- "${FLAGS_ARGV}"
183set -e
184
185# Associative arrays from partition name to file in the source and target
186# images. The size of the updated area must be the size of the file.
187declare -A SRC_PARTITIONS
188declare -A DST_PARTITIONS
189
190# List of partition names in order.
191declare -a PARTITIONS_ORDER
192
193# A list of temporary files to remove during cleanup.
194CLEANUP_FILES=()
195
196# Global options to force the version of the payload.
197FORCE_MAJOR_VERSION=""
198FORCE_MINOR_VERSION=""
199
200# Path to the postinstall config file in target image if exists.
201POSTINSTALL_CONFIG_FILE=""
202
203# The fingerprint of zlib in the source image.
204ZLIB_FINGERPRINT=""
205
206# read_option_int <file.txt> <option_key> [default_value]
207#
208# Reads the unsigned integer value associated with |option_key| in a key=value
209# file |file.txt|. Prints the read value if found and valid, otherwise prints
210# the |default_value|.
211read_option_uint() {
212  local file_txt="$1"
213  local option_key="$2"
214  local default_value="${3:-}"
215  local value
216  if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then
217    if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then
218      echo "${value}"
219      return
220    fi
221  fi
222  echo "${default_value}"
223}
224
225# Create a temporary file in the work_dir with an optional pattern name.
226# Prints the name of the newly created file.
227create_tempfile() {
228  local pattern="${1:-tempfile.XXXXXX}"
229  mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}"
230}
231
232cleanup() {
233  local err=""
234  rm -f "${CLEANUP_FILES[@]}" || err=1
235
236  # If we are cleaning up after an error, or if we got an error during
237  # cleanup (even if we eventually succeeded) return a non-zero exit
238  # code. This triggers additional logging in most environments that call
239  # this script.
240  if [[ -n "${err}" ]]; then
241    die "Cleanup encountered an error."
242  fi
243}
244
245cleanup_on_error() {
246  trap - INT TERM ERR EXIT
247  cleanup
248  die "Cleanup success after an error."
249}
250
251cleanup_on_exit() {
252  trap - INT TERM ERR EXIT
253  cleanup
254}
255
256trap cleanup_on_error INT TERM ERR
257trap cleanup_on_exit EXIT
258
259
260# extract_image <image> <partitions_array> [partitions_order]
261#
262# Detect the format of the |image| file and extract its updatable partitions
263# into new temporary files. Add the list of partition names and its files to the
264# associative array passed in |partitions_array|. If |partitions_order| is
265# passed, set it to list of partition names in order.
266extract_image() {
267  local image="$1"
268
269  # Brillo images are zip files. We detect the 4-byte magic header of the zip
270  # file.
271  local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"')
272  if [[ "${magic}" == "504b0304" ]]; then
273    echo "Detected .zip file, extracting Brillo image."
274    extract_image_brillo "$@"
275    return
276  fi
277
278  # Chrome OS images are GPT partitioned disks. We should have the cgpt binary
279  # bundled here and we will use it to extract the partitions, so the GPT
280  # headers must be valid.
281  if cgpt show -q -n "${image}" >/dev/null; then
282    echo "Detected GPT image, extracting Chrome OS image."
283    extract_image_cros "$@"
284    return
285  fi
286
287  die "Couldn't detect the image format of ${image}"
288}
289
290# extract_image_cros <image.bin> <partitions_array> [partitions_order]
291#
292# Extract Chromium OS recovery images into new temporary files.
293extract_image_cros() {
294  local image="$1"
295  local partitions_array="$2"
296  local partitions_order="${3:-}"
297
298  local kernel root
299  kernel=$(create_tempfile "kernel.bin.XXXXXX")
300  CLEANUP_FILES+=("${kernel}")
301  root=$(create_tempfile "root.bin.XXXXXX")
302  CLEANUP_FILES+=("${root}")
303
304  cros_generate_update_payload --extract \
305    --image "${image}" \
306    --kern_path "${kernel}" --root_path "${root}" \
307    --work_dir "${FLAGS_work_dir}" --outside_chroot
308
309  # Chrome OS uses major_version 1 payloads for all versions, even if the
310  # updater supports a newer major version.
311  FORCE_MAJOR_VERSION="1"
312
313  if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
314    # Copy from zlib_fingerprint in source image to stdout.
315    ZLIB_FINGERPRINT=$(e2cp "${root}":/etc/zlib_fingerprint -)
316  fi
317
318  # When generating legacy Chrome OS images, we need to use "boot" and "system"
319  # for the partition names to be compatible with updating Brillo devices with
320  # Chrome OS images.
321  eval ${partitions_array}[boot]=\""${kernel}"\"
322  eval ${partitions_array}[system]=\""${root}"\"
323
324  if [[ -n "${partitions_order}" ]]; then
325    eval "${partitions_order}=( \"system\" \"boot\" )"
326  fi
327
328  local part varname
329  for part in boot system; do
330    varname="${partitions_array}[${part}]"
331    printf "md5sum of %s: " "${varname}"
332    md5sum "${!varname}"
333  done
334}
335
336# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order]
337#
338# Extract the A/B updated partitions from a Brillo target_files zip file into
339# new temporary files.
340extract_image_brillo() {
341  local image="$1"
342  local partitions_array="$2"
343  local partitions_order="${3:-}"
344
345  local partitions=( "boot" "system" )
346  local ab_partitions_list
347  ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX")
348  CLEANUP_FILES+=("${ab_partitions_list}")
349  if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then
350    if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then
351      die "Invalid partition names found in the partition list."
352    fi
353    partitions=($(cat "${ab_partitions_list}"))
354    if [[ ${#partitions[@]} -eq 0 ]]; then
355      die "The list of partitions is empty. Can't generate a payload."
356    fi
357  else
358    warn "No ab_partitions.txt found. Using default."
359  fi
360  echo "List of A/B partitions: ${partitions[@]}"
361
362  if [[ -n "${partitions_order}" ]]; then
363    eval "${partitions_order}=(${partitions[@]})"
364  fi
365
366  # All Brillo updaters support major version 2.
367  FORCE_MAJOR_VERSION="2"
368
369  if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
370    # Source image
371    local ue_config=$(create_tempfile "ue_config.XXXXXX")
372    CLEANUP_FILES+=("${ue_config}")
373    if ! unzip -p "${image}" "META/update_engine_config.txt" \
374        >"${ue_config}"; then
375      warn "No update_engine_config.txt found. Assuming pre-release image, \
376using payload minor version 2"
377    fi
378    # For delta payloads, we use the major and minor version supported by the
379    # old updater.
380    FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \
381      "PAYLOAD_MINOR_VERSION" 2)
382    FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \
383      "PAYLOAD_MAJOR_VERSION" 2)
384
385    # Brillo support for deltas started with minor version 3.
386    if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then
387      warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \
388Disabling deltas for this source version."
389      exit ${EX_UNSUPPORTED_DELTA}
390    fi
391
392    if [[ "${FORCE_MINOR_VERSION}" -ge 4 ]]; then
393      ZLIB_FINGERPRINT=$(unzip -p "${image}" "META/zlib_fingerprint.txt")
394    fi
395  else
396    # Target image
397    local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX")
398    CLEANUP_FILES+=("${postinstall_config}")
399    if unzip -p "${image}" "META/postinstall_config.txt" \
400        >"${postinstall_config}"; then
401      POSTINSTALL_CONFIG_FILE="${postinstall_config}"
402    fi
403  fi
404
405  local part part_file temp_raw filesize
406  for part in "${partitions[@]}"; do
407    part_file=$(create_tempfile "${part}.img.XXXXXX")
408    CLEANUP_FILES+=("${part_file}")
409    unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}"
410
411    # If the partition is stored as an Android sparse image file, we need to
412    # convert them to a raw image for the update.
413    local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"')
414    if [[ "${magic}" == "3aff26ed" ]]; then
415      temp_raw=$(create_tempfile "${part}.raw.XXXXXX")
416      CLEANUP_FILES+=("${temp_raw}")
417      echo "Converting Android sparse image ${part}.img to RAW."
418      simg2img "${part_file}" "${temp_raw}"
419      # At this point, we can drop the contents of the old part_file file, but
420      # we can't delete the file because it will be deleted in cleanup.
421      true >"${part_file}"
422      part_file="${temp_raw}"
423    fi
424
425    # delta_generator only supports images multiple of 4 KiB, so we pad with
426    # zeros if needed.
427    filesize=$(stat -c%s "${part_file}")
428    if [[ $(( filesize % 4096 )) -ne 0 ]]; then
429      echo "Rounding up partition ${part}.img to multiple of 4 KiB."
430      : $(( filesize = (filesize + 4095) & -4096 ))
431      truncate --size="${filesize}" "${part_file}"
432    fi
433
434    eval "${partitions_array}[\"${part}\"]=\"${part_file}\""
435    echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes"
436  done
437}
438
439validate_generate() {
440  [[ -n "${FLAGS_payload}" ]] ||
441    die "Error: you must specify an output filename with --payload FILENAME"
442
443  [[ -n "${FLAGS_target_image}" ]] ||
444    die "Error: you must specify a target image with --target_image FILENAME"
445}
446
447cmd_generate() {
448  local payload_type="delta"
449  if [[ -z "${FLAGS_source_image}" ]]; then
450    payload_type="full"
451  fi
452
453  echo "Extracting images for ${payload_type} update."
454
455  extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER
456  if [[ "${payload_type}" == "delta" ]]; then
457    extract_image "${FLAGS_source_image}" SRC_PARTITIONS
458  fi
459
460  echo "Generating ${payload_type} update."
461  # Common payload args:
462  GENERATOR_ARGS=( -out_file="${FLAGS_payload}" )
463
464  local part old_partitions="" new_partitions="" partition_names=""
465  for part in "${PARTITIONS_ORDER[@]}"; do
466    if [[ -n "${partition_names}" ]]; then
467      partition_names+=":"
468      new_partitions+=":"
469      old_partitions+=":"
470    fi
471    partition_names+="${part}"
472    new_partitions+="${DST_PARTITIONS[${part}]}"
473    old_partitions+="${SRC_PARTITIONS[${part}]:-}"
474  done
475
476  # Target image args:
477  GENERATOR_ARGS+=(
478    -partition_names="${partition_names}"
479    -new_partitions="${new_partitions}"
480  )
481
482  if [[ "${payload_type}" == "delta" ]]; then
483    # Source image args:
484    GENERATOR_ARGS+=(
485      -old_partitions="${old_partitions}"
486    )
487    if [[ -n "${FORCE_MINOR_VERSION}" ]]; then
488      GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" )
489    fi
490    if [[ -n "${ZLIB_FINGERPRINT}" ]]; then
491      GENERATOR_ARGS+=( --zlib_fingerprint="${ZLIB_FINGERPRINT}" )
492    fi
493  fi
494
495  if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then
496    GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" )
497  fi
498
499  if [[ -n "${FLAGS_metadata_size_file}" ]]; then
500    GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
501  fi
502
503  if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then
504    GENERATOR_ARGS+=(
505      --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}"
506    )
507  fi
508
509  echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}"
510  "${GENERATOR}" "${GENERATOR_ARGS[@]}"
511
512  echo "Done generating ${payload_type} update."
513}
514
515validate_hash() {
516  [[ -n "${FLAGS_signature_size}" ]] ||
517    die "Error: you must specify signature size with --signature_size SIZES"
518
519  [[ -n "${FLAGS_unsigned_payload}" ]] ||
520    die "Error: you must specify the input unsigned payload with \
521--unsigned_payload FILENAME"
522
523  [[ -n "${FLAGS_payload_hash_file}" ]] ||
524    die "Error: you must specify --payload_hash_file FILENAME"
525
526  [[ -n "${FLAGS_metadata_hash_file}" ]] ||
527    die "Error: you must specify --metadata_hash_file FILENAME"
528}
529
530cmd_hash() {
531  "${GENERATOR}" \
532      -in_file="${FLAGS_unsigned_payload}" \
533      -signature_size="${FLAGS_signature_size}" \
534      -out_hash_file="${FLAGS_payload_hash_file}" \
535      -out_metadata_hash_file="${FLAGS_metadata_hash_file}"
536
537  echo "Done generating hash."
538}
539
540validate_sign() {
541  [[ -n "${FLAGS_signature_size}" ]] ||
542    die "Error: you must specify signature size with --signature_size SIZES"
543
544  [[ -n "${FLAGS_unsigned_payload}" ]] ||
545    die "Error: you must specify the input unsigned payload with \
546--unsigned_payload FILENAME"
547
548  [[ -n "${FLAGS_payload}" ]] ||
549    die "Error: you must specify the output signed payload with \
550--payload FILENAME"
551
552  [[ -n "${FLAGS_payload_signature_file}" ]] ||
553    die "Error: you must specify the payload signature file with \
554--payload_signature_file SIGNATURES"
555
556  [[ -n "${FLAGS_metadata_signature_file}" ]] ||
557    die "Error: you must specify the metadata signature file with \
558--metadata_signature_file SIGNATURES"
559}
560
561cmd_sign() {
562  GENERATOR_ARGS=(
563    -in_file="${FLAGS_unsigned_payload}"
564    -signature_size="${FLAGS_signature_size}"
565    -signature_file="${FLAGS_payload_signature_file}"
566    -metadata_signature_file="${FLAGS_metadata_signature_file}"
567    -out_file="${FLAGS_payload}"
568  )
569
570  if [[ -n "${FLAGS_metadata_size_file}" ]]; then
571    GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" )
572  fi
573
574  "${GENERATOR}" "${GENERATOR_ARGS[@]}"
575  echo "Done signing payload."
576}
577
578validate_properties() {
579  [[ -n "${FLAGS_payload}" ]] ||
580    die "Error: you must specify the payload file with --payload FILENAME"
581
582  [[ -n "${FLAGS_properties_file}" ]] ||
583    die "Error: you must specify a non empty --properties_file FILENAME"
584}
585
586cmd_properties() {
587  "${GENERATOR}" \
588      -in_file="${FLAGS_payload}" \
589      -properties_file="${FLAGS_properties_file}"
590}
591
592# Sanity check that the real generator exists:
593GENERATOR="$(which delta_generator)"
594[[ -x "${GENERATOR}" ]] || die "can't find delta_generator"
595
596case "$COMMAND" in
597  generate) validate_generate
598            cmd_generate
599            ;;
600  hash) validate_hash
601        cmd_hash
602        ;;
603  sign) validate_sign
604        cmd_sign
605        ;;
606  properties) validate_properties
607              cmd_properties
608              ;;
609esac
610