OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 | 2 |
3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 # This script takes a path to a rootfs.ext2 which was generated by | 7 # This script takes a path to a rootfs.ext2 which was generated by |
8 # build_image.sh and generates an image that can be used for auto | 8 # build_image.sh and generates an image that can be used for auto |
9 # update. | 9 # update. |
10 | 10 |
11 set -e | 11 set -e |
12 | 12 |
13 if [ -z "$2" -o -z "$1" ]; then | 13 LIB_IMAGE_COMMON="$(dirname "$0")/image_common.sh" |
14 echo "usage: $0 path/to/kernel_partition_img path/to/rootfs_partition_img" | 14 if ! . "$LIB_IMAGE_COMMON"; then |
| 15 echo "Missing required library: $LIB_IMAGE_COMMON. Cannot continue." |
15 exit 1 | 16 exit 1 |
16 fi | 17 fi |
17 | 18 |
| 19 if [ -z "$2" -o -z "$1" ]; then |
| 20 echo "usage: $0 path/to/kernel_partition_img path/to/rootfs_partition_img" |
| 21 echo " or $0 path/to/chromiumos_img kern_part_no rootfs_part_no" |
| 22 exit 1 |
| 23 fi |
| 24 |
18 if [ "$CROS_GENERATE_UPDATE_PAYLOAD_CALLED" != "1" ]; then | 25 if [ "$CROS_GENERATE_UPDATE_PAYLOAD_CALLED" != "1" ]; then |
19 echo "WARNING:" | 26 echo "WARNING:" |
20 echo "This script should only be called from cros_generate_update_payload" | 27 echo "This script should only be called from cros_generate_update_payload" |
21 echo "Please run that script with --help to see how to use it." | 28 echo "Please run that script with --help to see how to use it." |
22 fi | 29 fi |
23 | 30 |
24 if [ $(whoami) = "root" ]; then | 31 if [ $(whoami) = "root" ]; then |
25 echo "running $0 as root which is unneccessary" | 32 echo "running $0 as root which is unneccessary" |
26 fi | 33 fi |
27 | 34 |
28 KPART="$1" | 35 # Determine the offset size, and file name of parameters |
29 ROOT_PART="$2" | 36 if [ -z "$3" ]; then |
30 | 37 # kernnel_img rootfs_img |
31 KPART_SIZE=$(stat -c%s "$KPART") | 38 KPART="$1" |
| 39 ROOT_PART="$2" |
| 40 KPART_SIZE=$(stat -c%s "$KPART") |
| 41 ROOT_PART_SIZE=$(stat -c%s "$ROOT_PART") |
| 42 KPART_OFFSET=0 |
| 43 KPART_SECTORS=$((KPART_SIZE / 512)) |
| 44 ROOT_OFFSET=0 |
| 45 ROOT_SECTORS=$((ROOT_PART_SIZE / 512)) |
| 46 else |
| 47 # chromiumos_img kern_part_no rootfs_part_no |
| 48 KPART="$1" |
| 49 ROOT_PART="$1" |
| 50 KPART_OFFSET="$(part_offset "$KPART" "$2")" || |
| 51 err_die "cannot retieve kernel partition offset" |
| 52 KPART_SECTORS="$(part_size "$KPART" "$2")" || |
| 53 err_die "cannot retieve kernel partition size" |
| 54 ROOT_OFFSET="$(part_offset "$ROOT_PART" "$3")" || |
| 55 err_die "cannot retieve root partition offset" |
| 56 ROOT_SECTORS="$(part_size "$ROOT_PART" "$3")" || |
| 57 err_die "cannot retieve root partition size" |
| 58 KPART_SIZE=$((KPART_SECTORS * 512)) |
| 59 fi |
32 | 60 |
33 # Sanity check size. | 61 # Sanity check size. |
34 if [ "$KPART_SIZE" -gt $((16 * 1024 * 1024)) ]; then | 62 if [ "$KPART_SIZE" -gt $((16 * 1024 * 1024)) ]; then |
35 echo "Kernel partition size ($KPART_SIZE bytes) greater than 16 MiB." | 63 echo "Kernel partition size ($KPART_SIZE bytes) greater than 16 MiB." |
36 echo "That's too big." | 64 echo "That's too big." |
37 exit 1 | 65 exit 1 |
38 fi | 66 fi |
39 | 67 |
40 FINAL_OUT_FILE=$(dirname "$1")/update.gz | 68 FINAL_OUT_FILE=$(dirname "$1")/update.gz |
41 UNCOMPRESSED_OUT_FILE="$FINAL_OUT_FILE.uncompressed" | |
42 | 69 |
43 # First, write size of kernel partition in big endian as uint64 to out file | 70 # Update payload format: |
44 # printf converts it to a number like 00000000003d0900. sed converts it to: | 71 # [kernel_size: big-endian uint64][kernel_blob][rootfs_blob] |
45 # \\x00\\x00\\x00\\x00\\x00\\x3d\\x09\\x00, then xargs converts it to binary | |
46 # with echo. | |
47 printf %016x "$KPART_SIZE" | \ | |
48 sed 's/\([0-9a-f][0-9a-f]\)/\\\\x\1/g' | \ | |
49 xargs echo -ne > "$UNCOMPRESSED_OUT_FILE" | |
50 | 72 |
51 # Next, write kernel partition to the out file | 73 # Prepare kernel_size by using printf as a number like 00000000003d0900, then |
52 cat "$KPART" >> "$UNCOMPRESSED_OUT_FILE" | 74 # sed to convert as: \x00\x00\x00\x00\x00\x3d\x09\x00, finally echo -e to |
| 75 # convert into binary. |
| 76 KPART_SIZE_SIGNATURE="$(printf "%016x" "$KPART_SIZE" | |
| 77 sed 's/\([0-9a-f][0-9a-f]\)/\\x\1/g')" |
53 | 78 |
54 # Sanity check size of output file now | 79 # Build the blob! |
55 if [ $(stat -c%s "$UNCOMPRESSED_OUT_FILE") -ne $((8 + $KPART_SIZE)) ]; then | 80 CS_AND_RET_CODES="$( |
56 echo "Kernel partition changed size during image generation. Aborting." | 81 (echo -en "$KPART_SIZE_SIGNATURE" |
57 exit 1 | 82 echo "Compressing kernel..." >&2 |
58 fi | 83 dump_partial_file "$KPART" "$KPART_OFFSET" "$KPART_SECTORS" |
| 84 echo "Compressing rootfs..." >&2 |
| 85 dump_partial_file "$ROOT_PART" "$ROOT_OFFSET" "$ROOT_SECTORS") | |
| 86 gzip_compress -9 -c | |
| 87 tee "$FINAL_OUT_FILE" | |
| 88 openssl sha1 -binary | |
| 89 openssl base64 | |
| 90 tr '\n' ' ' |
| 91 echo ${PIPESTATUS[*]})" |
59 | 92 |
60 # Put rootfs into the out file | 93 EXPECTED_RET_CODES="0 0 0 0 0 0" |
61 cat "$ROOT_PART" >> "$UNCOMPRESSED_OUT_FILE" | |
62 | |
63 # compress and hash | |
64 CS_AND_RET_CODES=$(gzip -c "$UNCOMPRESSED_OUT_FILE" | \ | |
65 tee "$FINAL_OUT_FILE" | openssl sha1 -binary | \ | |
66 openssl base64 | tr '\n' ' '; \ | |
67 echo ${PIPESTATUS[*]}) | |
68 EXPECTED_RET_CODES="0 0 0 0 0" | |
69 set -- $CS_AND_RET_CODES | 94 set -- $CS_AND_RET_CODES |
70 CALC_CS="$1" | 95 CALC_CS="$1" |
71 shift | 96 shift |
72 RET_CODES="$@" | 97 RET_CODES="$@" |
73 if [ "$RET_CODES" != "$EXPECTED_RET_CODES" ]; then | 98 if [ "$RET_CODES" != "$EXPECTED_RET_CODES" ]; then |
74 echo compression/hash failed. $RET_CODES | 99 echo compression/hash failed. $RET_CODES |
75 exit 1 | 100 exit 1 |
76 fi | 101 fi |
77 | 102 |
78 rm "$UNCOMPRESSED_OUT_FILE" | |
79 | |
80 echo Success. hash is "$CALC_CS" | 103 echo Success. hash is "$CALC_CS" |
OLD | NEW |