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