OLD | NEW |
| (Empty) |
1 #!/bin/bash | |
2 # | |
3 # Copyright (c) 2010 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 populate payload (partition B) of a Chrome OS recovery installer | |
8 # See install_gpt() in chromeos-common.sh for partition size details. | |
9 # Currently, rootfs is set to 1GB and kernel is set to 16MB. | |
10 # As long as the payload is large enough, we'll copy from src_image to | |
11 # dst_image, without ANY version checking. | |
12 | |
13 # Need this script to get default board | |
14 . "$(dirname "$0")/common.sh" | |
15 | |
16 # Need this script for functions related to gpt images. | |
17 . "$(dirname "$0")/chromeos-common.sh" | |
18 | |
19 # We need to be in the chroot to use "gpt" command | |
20 assert_inside_chroot | |
21 | |
22 get_default_board | |
23 | |
24 DEFINE_string board "$DEFAULT_BOARD" \ | |
25 "The board to build an image for." b | |
26 DEFINE_string src_image "" \ | |
27 "Path to a pristine image to be written to a recovery image." s | |
28 DEFINE_string dst_image "" \ | |
29 "Path to a recovery image." d | |
30 DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts. Default: False" y | |
31 | |
32 SCRIPT_NAME="populate_recovery_payload" | |
33 | |
34 # Parse command line | |
35 FLAGS "$@" || exit 1 | |
36 eval set -- "${FLAGS_ARGV}" | |
37 | |
38 # Make sure board is set | |
39 if [ -z $FLAGS_board ] ; then | |
40 setup_board_warning | |
41 die "${SCRIPT_NAME} failed. No board set" | |
42 fi | |
43 | |
44 # Make sure image path is set | |
45 if [ -z $FLAGS_src_image ] ; then | |
46 die "${SCRIPT_NAME} failed. No --src_image set" | |
47 fi | |
48 | |
49 if [ -z $FLAGS_dst_image ] ; then | |
50 die "${SCRIPT_NAME} failed. No --dst_image set" | |
51 fi | |
52 | |
53 # Turn path into an absolute path. | |
54 FLAGS_src_image=`eval readlink -f ${FLAGS_src_image}` | |
55 | |
56 # Abort early if we can't find the image | |
57 if [ ! -f $FLAGS_src_image ] ; then | |
58 die "${SCRIPT_NAME} failed. No image found at $FLAGS_src_image" | |
59 fi | |
60 | |
61 FLAGS_dst_image=`eval readlink -f ${FLAGS_dst_image}` | |
62 if [ ! -f $FLAGS_dst_image ] ; then | |
63 die "${SCRIPT_NAME} failed. No image found at $FLAGS_dst_image" | |
64 fi | |
65 | |
66 # Look up partition number | |
67 function get_part_num() { | |
68 local image_type=$1 | |
69 local part_type=$2 | |
70 local part_num= | |
71 | |
72 case "${image_type}" in | |
73 "src" ) | |
74 case "${part_type}" in | |
75 "rootfs" ) part_num=3;; | |
76 "kern" ) part_num=2;; | |
77 esac | |
78 ;; | |
79 "dst" ) | |
80 case "${part_type}" in | |
81 "rootfs" ) part_num=5;; | |
82 "kern" ) part_num=4;; | |
83 esac | |
84 ;; | |
85 esac | |
86 | |
87 echo "${part_num}" | |
88 } | |
89 | |
90 # main process begins here. | |
91 | |
92 # Confirm user wants to (re-)populate payload partition | |
93 if [ $FLAGS_yes -ne $FLAGS_TRUE ]; then | |
94 read -p "Overwriting payload partition of recovery image ${FLAGS_dst_image}; \ | |
95 are you sure (y/N)? " SURE | |
96 SURE="${SURE:0:1}" # Get just the first character | |
97 if [ "$SURE" != "y" ]; then | |
98 echo "Ok, better safe than sorry. Abort." | |
99 exit 1 | |
100 fi | |
101 fi | |
102 echo "About to overwrite payload partition..." | |
103 | |
104 set -e | |
105 | |
106 # Look up partition size and offset for rootfs, kernel on both images. | |
107 # Assume 512-byte sector/block size | |
108 SRC_ROOTFS_PART=$(get_part_num "src" "rootfs") | |
109 SRC_ROOTFS_OFFSET=$(partoffset "${FLAGS_src_image}" ${SRC_ROOTFS_PART}) | |
110 SRC_ROOTFS_SECTORS=$(partsize "${FLAGS_src_image}" ${SRC_ROOTFS_PART}) | |
111 | |
112 DST_ROOTFS_PART=$(get_part_num "dst" "rootfs") | |
113 DST_ROOTFS_OFFSET=$(partoffset "${FLAGS_dst_image}" ${DST_ROOTFS_PART}) | |
114 DST_ROOTFS_SECTORS=$(partsize "${FLAGS_dst_image}" ${DST_ROOTFS_PART}) | |
115 | |
116 # Verify source partition is not larger than destination partition | |
117 if [ $SRC_ROOTFS_SECTORS -gt $DST_ROOTFS_SECTORS ]; then | |
118 echo "Rootfs partition is too large to be copied to payload. Source has \ | |
119 ${SRC_ROOTFS_SECTORS} sectors while destination only has \ | |
120 ${DST_ROOTFS_SECTORS} sectors. Abort." | |
121 exit 1 | |
122 fi | |
123 | |
124 SRC_KERN_PART=$(get_part_num "src" "kern") | |
125 SRC_KERN_OFFSET=$(partoffset "${FLAGS_src_image}" ${SRC_KERN_PART}) | |
126 SRC_KERN_SECTORS=$(partsize "${FLAGS_src_image}" ${SRC_KERN_PART}) | |
127 | |
128 DST_KERN_PART=$(get_part_num "dst" "kern") | |
129 DST_KERN_OFFSET=$(partoffset "${FLAGS_dst_image}" ${DST_KERN_PART}) | |
130 DST_KERN_SECTORS=$(partsize "${FLAGS_dst_image}" ${DST_KERN_PART}) | |
131 | |
132 if [ $SRC_KERN_SECTORS -gt $DST_KERN_SECTORS ]; then | |
133 echo "Kernel partition is too large to be copied to payload. Source has \ | |
134 ${SRC_KERN_SECTORS} sectors while destination only has \ | |
135 ${DST_KERN_SECTORS} sectors. Abort." | |
136 exit 1 | |
137 fi | |
138 | |
139 # Check if we can use 2MB for faster write | |
140 SECTOR_SIZE=512 # default sector size in bytes | |
141 IDEAL_BLOCK_SIZE=$((2 * 1024 * 1024)) # large sector size in bytes | |
142 ROOTFS_BS=$SECTOR_SIZE | |
143 ROOTFS_COUNT=$SRC_ROOTFS_SECTORS # number of SECTOR_SIZE sectors | |
144 ROOTFS_SKIP=$SRC_ROOTFS_OFFSET # number of SECTOR_SIZE sectors | |
145 ROOTFS_SEEK=$DST_ROOTFS_OFFSET # number of SECTOR_SIZE sectors | |
146 if [ $(( $SRC_ROOTFS_OFFSET % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ] && \ | |
147 [ $(( $SRC_ROOTFS_SECTORS % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ] \ | |
148 && \ | |
149 [ $(( $DST_ROOTFS_OFFSET % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ] && \ | |
150 [ $(( $DST_ROOTFS_SECTORS % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ]; \ | |
151 then | |
152 ROOTFS_BS=$IDEAL_BLOCK_SIZE # increase sector size to 2MB | |
153 # Update number of sectors | |
154 ROOTFS_COUNT=$(( ($SRC_ROOTFS_SECTORS * $SECTOR_SIZE) / $IDEAL_BLOCK_SIZE )) | |
155 ROOTFS_SKIP=$(( ($SRC_ROOTFS_OFFSET * $SECTOR_SIZE) / $IDEAL_BLOCK_SIZE )) | |
156 ROOTFS_SEEK=$(( ($DST_ROOTFS_OFFSET * $SECTOR_SIZE) / $IDEAL_BLOCK_SIZE )) | |
157 echo "Use 2MB for rootfs block size" | |
158 fi | |
159 | |
160 KERN_BS=$SECTOR_SIZE | |
161 KERN_COUNT=$SRC_KERN_SECTORS # number of SECTOR_SIZE sectors | |
162 KERN_SKIP=$SRC_KERN_OFFSET # number of SECTOR_SIZE sectors | |
163 KERN_SEEK=$DST_KERN_OFFSET # number of SECTOR_SIZE sectors | |
164 if [ $(( $SRC_KERN_OFFSET % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ] && \ | |
165 [ $(( $SRC_KERN_SECTORS % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ] && \ | |
166 [ $(( $DST_KERN_OFFSET % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ] && \ | |
167 [ $(( $DST_KERN_SECTORS % ($IDEAL_BLOCK_SIZE / $SECTOR_SIZE) )) = "0" ]; \ | |
168 then | |
169 KERN_BS=$IDEAL_BLOCK_SIZE | |
170 KERN_COUNT=$(( ($SRC_KERN_SECTORS * $SECTOR_SIZE) / $IDEAL_BLOCK_SIZE )) | |
171 KERN_SKIP=$(( ($SRC_KERN_OFFSET * $SECTOR_SIZE) / $IDEAL_BLOCK_SIZE )) | |
172 KERN_SEEK=$(( ($DST_KERN_OFFSET * $SECTOR_SIZE) / $IDEAL_BLOCK_SIZE )) | |
173 echo "Use 2MB for kernel block size" | |
174 fi | |
175 | |
176 # Copy new partitions to dst_image | |
177 echo "Copying rootfs partition to payload ..." | |
178 sudo dd if=$FLAGS_src_image of=$FLAGS_dst_image conv=notrunc bs=$ROOTFS_BS \ | |
179 skip=$ROOTFS_SKIP seek=$ROOTFS_SEEK count=$ROOTFS_COUNT | |
180 | |
181 echo "Copying kernel partition to payload ..." | |
182 sudo dd if=$FLAGS_src_image of=$FLAGS_dst_image conv=notrunc bs=$KERN_BS \ | |
183 skip=$KERN_SKIP seek=$KERN_SEEK count=$KERN_COUNT | |
184 | |
185 echo "New partitions copied to payload. Done." | |
186 exit 0 | |
OLD | NEW |