brillo_update_payload revision 20bdc70e540e021c4a26553dfdbfd2fabe8ea432
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# Associative arrays for the .map files associated with each src/dst partition 191# file in SRC_PARTITIONS and DST_PARTITIONS. 192declare -A SRC_PARTITIONS_MAP 193declare -A DST_PARTITIONS_MAP 194 195# List of partition names in order. 196declare -a PARTITIONS_ORDER 197 198# A list of temporary files to remove during cleanup. 199CLEANUP_FILES=() 200 201# Global options to force the version of the payload. 202FORCE_MAJOR_VERSION="" 203FORCE_MINOR_VERSION="" 204 205# Path to the postinstall config file in target image if exists. 206POSTINSTALL_CONFIG_FILE="" 207 208# The fingerprint of zlib in the source image. 209ZLIB_FINGERPRINT="" 210 211# read_option_int <file.txt> <option_key> [default_value] 212# 213# Reads the unsigned integer value associated with |option_key| in a key=value 214# file |file.txt|. Prints the read value if found and valid, otherwise prints 215# the |default_value|. 216read_option_uint() { 217 local file_txt="$1" 218 local option_key="$2" 219 local default_value="${3:-}" 220 local value 221 if value=$(look "${option_key}=" "${file_txt}" | tail -n 1); then 222 if value=$(echo "${value}" | cut -f 2- -d "=" | grep -E "^[0-9]+$"); then 223 echo "${value}" 224 return 225 fi 226 fi 227 echo "${default_value}" 228} 229 230# truncate_file <file_path> <file_size> 231# 232# Truncate the given |file_path| to |file_size| using perl. 233# The truncate binary might not be available. 234truncate_file() { 235 local file_path="$1" 236 local file_size="$2" 237 perl -e "open(FILE, \"+<\", \$ARGV[0]); \ 238 truncate(FILE, ${file_size}); \ 239 close(FILE);" "${file_path}" 240} 241 242# Create a temporary file in the work_dir with an optional pattern name. 243# Prints the name of the newly created file. 244create_tempfile() { 245 local pattern="${1:-tempfile.XXXXXX}" 246 mktemp --tmpdir="${FLAGS_work_dir}" "${pattern}" 247} 248 249cleanup() { 250 local err="" 251 rm -f "${CLEANUP_FILES[@]}" || err=1 252 253 # If we are cleaning up after an error, or if we got an error during 254 # cleanup (even if we eventually succeeded) return a non-zero exit 255 # code. This triggers additional logging in most environments that call 256 # this script. 257 if [[ -n "${err}" ]]; then 258 die "Cleanup encountered an error." 259 fi 260} 261 262cleanup_on_error() { 263 trap - INT TERM ERR EXIT 264 cleanup 265 die "Cleanup success after an error." 266} 267 268cleanup_on_exit() { 269 trap - INT TERM ERR EXIT 270 cleanup 271} 272 273trap cleanup_on_error INT TERM ERR 274trap cleanup_on_exit EXIT 275 276 277# extract_image <image> <partitions_array> [partitions_order] 278# 279# Detect the format of the |image| file and extract its updatable partitions 280# into new temporary files. Add the list of partition names and its files to the 281# associative array passed in |partitions_array|. If |partitions_order| is 282# passed, set it to list of partition names in order. 283extract_image() { 284 local image="$1" 285 286 # Brillo images are zip files. We detect the 4-byte magic header of the zip 287 # file. 288 local magic=$(head --bytes=4 "${image}" | hexdump -e '1/1 "%.2x"') 289 if [[ "${magic}" == "504b0304" ]]; then 290 echo "Detected .zip file, extracting Brillo image." 291 extract_image_brillo "$@" 292 return 293 fi 294 295 # Chrome OS images are GPT partitioned disks. We should have the cgpt binary 296 # bundled here and we will use it to extract the partitions, so the GPT 297 # headers must be valid. 298 if cgpt show -q -n "${image}" >/dev/null; then 299 echo "Detected GPT image, extracting Chrome OS image." 300 extract_image_cros "$@" 301 return 302 fi 303 304 die "Couldn't detect the image format of ${image}" 305} 306 307# extract_image_cros <image.bin> <partitions_array> [partitions_order] 308# 309# Extract Chromium OS recovery images into new temporary files. 310extract_image_cros() { 311 local image="$1" 312 local partitions_array="$2" 313 local partitions_order="${3:-}" 314 315 local kernel root 316 kernel=$(create_tempfile "kernel.bin.XXXXXX") 317 CLEANUP_FILES+=("${kernel}") 318 root=$(create_tempfile "root.bin.XXXXXX") 319 CLEANUP_FILES+=("${root}") 320 321 cros_generate_update_payload --extract \ 322 --image "${image}" \ 323 --kern_path "${kernel}" --root_path "${root}" \ 324 --work_dir "${FLAGS_work_dir}" --outside_chroot 325 326 # Chrome OS uses major_version 1 payloads for all versions, even if the 327 # updater supports a newer major version. 328 FORCE_MAJOR_VERSION="1" 329 330 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then 331 # Copy from zlib_fingerprint in source image to stdout. 332 ZLIB_FINGERPRINT=$(e2cp "${root}":/etc/zlib_fingerprint -) 333 fi 334 335 # When generating legacy Chrome OS images, we need to use "boot" and "system" 336 # for the partition names to be compatible with updating Brillo devices with 337 # Chrome OS images. 338 eval ${partitions_array}[boot]=\""${kernel}"\" 339 eval ${partitions_array}[system]=\""${root}"\" 340 341 if [[ -n "${partitions_order}" ]]; then 342 eval "${partitions_order}=( \"system\" \"boot\" )" 343 fi 344 345 local part varname 346 for part in boot system; do 347 varname="${partitions_array}[${part}]" 348 printf "md5sum of %s: " "${varname}" 349 md5sum "${!varname}" 350 done 351} 352 353# extract_image_brillo <target_files.zip> <partitions_array> [partitions_order] 354# 355# Extract the A/B updated partitions from a Brillo target_files zip file into 356# new temporary files. 357extract_image_brillo() { 358 local image="$1" 359 local partitions_array="$2" 360 local partitions_order="${3:-}" 361 362 local partitions=( "boot" "system" ) 363 local ab_partitions_list 364 ab_partitions_list=$(create_tempfile "ab_partitions_list.XXXXXX") 365 CLEANUP_FILES+=("${ab_partitions_list}") 366 if unzip -p "${image}" "META/ab_partitions.txt" >"${ab_partitions_list}"; then 367 if grep -v -E '^[a-zA-Z0-9_-]*$' "${ab_partitions_list}" >&2; then 368 die "Invalid partition names found in the partition list." 369 fi 370 partitions=($(cat "${ab_partitions_list}")) 371 if [[ ${#partitions[@]} -eq 0 ]]; then 372 die "The list of partitions is empty. Can't generate a payload." 373 fi 374 else 375 warn "No ab_partitions.txt found. Using default." 376 fi 377 echo "List of A/B partitions: ${partitions[@]}" 378 379 if [[ -n "${partitions_order}" ]]; then 380 eval "${partitions_order}=(${partitions[@]})" 381 fi 382 383 # All Brillo updaters support major version 2. 384 FORCE_MAJOR_VERSION="2" 385 386 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then 387 # Source image 388 local ue_config=$(create_tempfile "ue_config.XXXXXX") 389 CLEANUP_FILES+=("${ue_config}") 390 if ! unzip -p "${image}" "META/update_engine_config.txt" \ 391 >"${ue_config}"; then 392 warn "No update_engine_config.txt found. Assuming pre-release image, \ 393using payload minor version 2" 394 fi 395 # For delta payloads, we use the major and minor version supported by the 396 # old updater. 397 FORCE_MINOR_VERSION=$(read_option_uint "${ue_config}" \ 398 "PAYLOAD_MINOR_VERSION" 2) 399 FORCE_MAJOR_VERSION=$(read_option_uint "${ue_config}" \ 400 "PAYLOAD_MAJOR_VERSION" 2) 401 402 # Brillo support for deltas started with minor version 3. 403 if [[ "${FORCE_MINOR_VERSION}" -le 2 ]]; then 404 warn "No delta support from minor version ${FORCE_MINOR_VERSION}. \ 405Disabling deltas for this source version." 406 exit ${EX_UNSUPPORTED_DELTA} 407 fi 408 409 if [[ "${FORCE_MINOR_VERSION}" -ge 4 ]]; then 410 ZLIB_FINGERPRINT=$(unzip -p "${image}" "META/zlib_fingerprint.txt") 411 fi 412 else 413 # Target image 414 local postinstall_config=$(create_tempfile "postinstall_config.XXXXXX") 415 CLEANUP_FILES+=("${postinstall_config}") 416 if unzip -p "${image}" "META/postinstall_config.txt" \ 417 >"${postinstall_config}"; then 418 POSTINSTALL_CONFIG_FILE="${postinstall_config}" 419 fi 420 fi 421 422 local part part_file temp_raw filesize 423 for part in "${partitions[@]}"; do 424 part_file=$(create_tempfile "${part}.img.XXXXXX") 425 CLEANUP_FILES+=("${part_file}") 426 unzip -p "${image}" "IMAGES/${part}.img" >"${part_file}" 427 428 # If the partition is stored as an Android sparse image file, we need to 429 # convert them to a raw image for the update. 430 local magic=$(head --bytes=4 "${part_file}" | hexdump -e '1/1 "%.2x"') 431 if [[ "${magic}" == "3aff26ed" ]]; then 432 temp_raw=$(create_tempfile "${part}.raw.XXXXXX") 433 CLEANUP_FILES+=("${temp_raw}") 434 echo "Converting Android sparse image ${part}.img to RAW." 435 simg2img "${part_file}" "${temp_raw}" 436 # At this point, we can drop the contents of the old part_file file, but 437 # we can't delete the file because it will be deleted in cleanup. 438 true >"${part_file}" 439 part_file="${temp_raw}" 440 fi 441 442 # Extract the .map file (if one is available). 443 part_map_file=$(create_tempfile "${part}.map.XXXXXX") 444 CLEANUP_FILES+=("${part_map_file}") 445 unzip -p "${image}" "IMAGES/${part}.map" >"${part_map_file}" || \ 446 part_map_file="" 447 448 # delta_generator only supports images multiple of 4 KiB. For target images 449 # we pad the data with zeros if needed, but for source images we truncate 450 # down the data since the last block of the old image could be padded on 451 # disk with unknown data. 452 filesize=$(stat -c%s "${part_file}") 453 if [[ $(( filesize % 4096 )) -ne 0 ]]; then 454 if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then 455 echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB." 456 : $(( filesize = filesize & -4096 )) 457 else 458 echo "Rounding UP partition ${part}.img to a multiple of 4 KiB." 459 : $(( filesize = (filesize + 4095) & -4096 )) 460 fi 461 truncate_file "${part_file}" "${filesize}" 462 fi 463 464 eval "${partitions_array}[\"${part}\"]=\"${part_file}\"" 465 eval "${partitions_array}_MAP[\"${part}\"]=\"${part_map_file}\"" 466 echo "Extracted ${partitions_array}[${part}]: ${filesize} bytes" 467 done 468} 469 470validate_generate() { 471 [[ -n "${FLAGS_payload}" ]] || 472 die "You must specify an output filename with --payload FILENAME" 473 474 [[ -n "${FLAGS_target_image}" ]] || 475 die "You must specify a target image with --target_image FILENAME" 476} 477 478cmd_generate() { 479 local payload_type="delta" 480 if [[ -z "${FLAGS_source_image}" ]]; then 481 payload_type="full" 482 fi 483 484 echo "Extracting images for ${payload_type} update." 485 486 extract_image "${FLAGS_target_image}" DST_PARTITIONS PARTITIONS_ORDER 487 if [[ "${payload_type}" == "delta" ]]; then 488 extract_image "${FLAGS_source_image}" SRC_PARTITIONS 489 fi 490 491 echo "Generating ${payload_type} update." 492 # Common payload args: 493 GENERATOR_ARGS=( -out_file="${FLAGS_payload}" ) 494 495 local part old_partitions="" new_partitions="" partition_names="" 496 local old_mapfiles="" new_mapfiles="" 497 for part in "${PARTITIONS_ORDER[@]}"; do 498 if [[ -n "${partition_names}" ]]; then 499 partition_names+=":" 500 new_partitions+=":" 501 old_partitions+=":" 502 new_mapfiles+=":" 503 old_mapfiles+=":" 504 fi 505 partition_names+="${part}" 506 new_partitions+="${DST_PARTITIONS[${part}]}" 507 old_partitions+="${SRC_PARTITIONS[${part}]:-}" 508 new_mapfiles+="${DST_PARTITIONS_MAP[${part}]:-}" 509 old_mapfiles+="${SRC_PARTITIONS_MAP[${part}]:-}" 510 done 511 512 # Target image args: 513 GENERATOR_ARGS+=( 514 -partition_names="${partition_names}" 515 -new_partitions="${new_partitions}" 516 -new_mapfiles="${new_mapfiles}" 517 ) 518 519 if [[ "${payload_type}" == "delta" ]]; then 520 # Source image args: 521 GENERATOR_ARGS+=( 522 -old_partitions="${old_partitions}" 523 -old_mapfiles="${old_mapfiles}" 524 ) 525 if [[ -n "${FORCE_MINOR_VERSION}" ]]; then 526 GENERATOR_ARGS+=( --minor_version="${FORCE_MINOR_VERSION}" ) 527 fi 528 if [[ -n "${ZLIB_FINGERPRINT}" ]]; then 529 GENERATOR_ARGS+=( --zlib_fingerprint="${ZLIB_FINGERPRINT}" ) 530 fi 531 fi 532 533 if [[ -n "${FORCE_MAJOR_VERSION}" ]]; then 534 GENERATOR_ARGS+=( --major_version="${FORCE_MAJOR_VERSION}" ) 535 fi 536 537 if [[ -n "${FLAGS_metadata_size_file}" ]]; then 538 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" ) 539 fi 540 541 if [[ -n "${POSTINSTALL_CONFIG_FILE}" ]]; then 542 GENERATOR_ARGS+=( 543 --new_postinstall_config_file="${POSTINSTALL_CONFIG_FILE}" 544 ) 545 fi 546 547 echo "Running delta_generator with args: ${GENERATOR_ARGS[@]}" 548 "${GENERATOR}" "${GENERATOR_ARGS[@]}" 549 550 echo "Done generating ${payload_type} update." 551} 552 553validate_hash() { 554 [[ -n "${FLAGS_signature_size}" ]] || 555 die "You must specify signature size with --signature_size SIZES" 556 557 [[ -n "${FLAGS_unsigned_payload}" ]] || 558 die "You must specify the input unsigned payload with \ 559--unsigned_payload FILENAME" 560 561 [[ -n "${FLAGS_payload_hash_file}" ]] || 562 die "You must specify --payload_hash_file FILENAME" 563 564 [[ -n "${FLAGS_metadata_hash_file}" ]] || 565 die "You must specify --metadata_hash_file FILENAME" 566} 567 568cmd_hash() { 569 "${GENERATOR}" \ 570 -in_file="${FLAGS_unsigned_payload}" \ 571 -signature_size="${FLAGS_signature_size}" \ 572 -out_hash_file="${FLAGS_payload_hash_file}" \ 573 -out_metadata_hash_file="${FLAGS_metadata_hash_file}" 574 575 echo "Done generating hash." 576} 577 578validate_sign() { 579 [[ -n "${FLAGS_signature_size}" ]] || 580 die "You must specify signature size with --signature_size SIZES" 581 582 [[ -n "${FLAGS_unsigned_payload}" ]] || 583 die "You must specify the input unsigned payload with \ 584--unsigned_payload FILENAME" 585 586 [[ -n "${FLAGS_payload}" ]] || 587 die "You must specify the output signed payload with --payload FILENAME" 588 589 [[ -n "${FLAGS_payload_signature_file}" ]] || 590 die "You must specify the payload signature file with \ 591--payload_signature_file SIGNATURES" 592 593 [[ -n "${FLAGS_metadata_signature_file}" ]] || 594 die "You must specify the metadata signature file with \ 595--metadata_signature_file SIGNATURES" 596} 597 598cmd_sign() { 599 GENERATOR_ARGS=( 600 -in_file="${FLAGS_unsigned_payload}" 601 -signature_size="${FLAGS_signature_size}" 602 -signature_file="${FLAGS_payload_signature_file}" 603 -metadata_signature_file="${FLAGS_metadata_signature_file}" 604 -out_file="${FLAGS_payload}" 605 ) 606 607 if [[ -n "${FLAGS_metadata_size_file}" ]]; then 608 GENERATOR_ARGS+=( --out_metadata_size_file="${FLAGS_metadata_size_file}" ) 609 fi 610 611 "${GENERATOR}" "${GENERATOR_ARGS[@]}" 612 echo "Done signing payload." 613} 614 615validate_properties() { 616 [[ -n "${FLAGS_payload}" ]] || 617 die "You must specify the payload file with --payload FILENAME" 618 619 [[ -n "${FLAGS_properties_file}" ]] || 620 die "You must specify a non empty --properties_file FILENAME" 621} 622 623cmd_properties() { 624 "${GENERATOR}" \ 625 -in_file="${FLAGS_payload}" \ 626 -properties_file="${FLAGS_properties_file}" 627} 628 629# Sanity check that the real generator exists: 630GENERATOR="$(which delta_generator || true)" 631[[ -x "${GENERATOR}" ]] || die "can't find delta_generator" 632 633case "$COMMAND" in 634 generate) validate_generate 635 cmd_generate 636 ;; 637 hash) validate_hash 638 cmd_hash 639 ;; 640 sign) validate_sign 641 cmd_sign 642 ;; 643 properties) validate_properties 644 cmd_properties 645 ;; 646esac 647