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