Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | # |
| 5 | # This contains common constants and functions for installer scripts. This must |
| 6 | # evaluate properly for both /bin/bash and /bin/sh, since it's used both to |
| 7 | # create the initial image at compile time and to install or upgrade a running |
| 8 | # image. |
| 9 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 10 | # The GPT tables describe things in terms of 512-byte sectors, but some |
| 11 | # filesystems prefer 4096-byte blocks. These functions help with alignment |
| 12 | # issues. |
| 13 | |
| 14 | # This returns the size of a file or device in 512-byte sectors, rounded up if |
| 15 | # needed. |
| 16 | # Invoke as: subshell |
| 17 | # Args: FILENAME |
| 18 | # Return: whole number of sectors needed to fully contain FILENAME |
| 19 | numsectors() { |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 20 | if [ -b "${1}" ]; then |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 21 | dev=${1##*/} |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 22 | if [ -e /sys/block/$dev/size ]; then |
| 23 | cat /sys/block/$dev/size |
| 24 | else |
| 25 | part=${1##*/} |
| 26 | block=$(get_block_dev_from_partition_dev "${1}") |
| 27 | block=${block##*/} |
| 28 | cat /sys/block/$block/$part/size |
| 29 | fi |
| 30 | else |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 31 | local bytes=$(stat -c%s "$1") |
| 32 | local sectors=$(( $bytes / 512 )) |
| 33 | local rem=$(( $bytes % 512 )) |
| 34 | if [ $rem -ne 0 ]; then |
| 35 | sectors=$(( $sectors + 1 )) |
| 36 | fi |
| 37 | echo $sectors |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 38 | fi |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 41 | # Round a number of 512-byte sectors up to an integral number of 2Mb |
| 42 | # blocks. Divisor is 2 * 1024 * 1024 / 512 == 4096. |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 43 | # Invoke as: subshell |
| 44 | # Args: SECTORS |
| 45 | # Return: Next largest multiple-of-8 sectors (ex: 4->8, 33->40, 32->32) |
| 46 | roundup() { |
| 47 | local num=$1 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 48 | local div=${2:-4096} |
| 49 | local rem=$(( $num % $div )) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 50 | |
Andrew de los Reyes | 344709b | 2010-04-05 10:49:08 -0700 | [diff] [blame] | 51 | if [ $rem -ne 0 ]; then |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 52 | num=$(($num + $div - $rem)) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 53 | fi |
| 54 | echo $num |
| 55 | } |
| 56 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 57 | # Truncate a number of 512-byte sectors down to an integral number of 2Mb |
| 58 | # blocks. Divisor is 2 * 1024 * 1024 / 512 == 4096. |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 59 | # Invoke as: subshell |
| 60 | # Args: SECTORS |
| 61 | # Return: Next smallest multiple-of-8 sectors (ex: 4->0, 33->32, 32->32) |
| 62 | rounddown() { |
| 63 | local num=$1 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 64 | local div=${2:-4096} |
| 65 | local rem=$(( $num % $div )) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 66 | |
Andrew de los Reyes | 344709b | 2010-04-05 10:49:08 -0700 | [diff] [blame] | 67 | if [ $rem -ne 0 ]; then |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 68 | num=$(($num - $rem)) |
| 69 | fi |
| 70 | echo $num |
| 71 | } |
| 72 | |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 73 | # Locate the cgpt tool. It should already be installed in the build chroot, |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 74 | # but some of these functions may be invoked outside the chroot (by |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 75 | # image_to_usb or similar), so we need to find it. |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 76 | GPT="" |
Yusuke Sato | d55cea9 | 2010-04-21 10:46:59 +0900 | [diff] [blame] | 77 | |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 78 | locate_gpt() { |
| 79 | if [ -z "$GPT" ]; then |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 80 | GPT=$(which cgpt 2>/dev/null) || /bin/true |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 81 | if [ -z "$GPT" ]; then |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 82 | if [ -x "${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" ]; then |
| 83 | GPT="${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 84 | else |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 85 | echo "can't find cgpt tool" 1>&2 |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 86 | exit 1 |
| 87 | fi |
| 88 | fi |
| 89 | fi |
| 90 | } |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 91 | |
| 92 | # This installs a GPT into the specified device or file, using the given |
Andrew de los Reyes | 31e39f1 | 2010-04-01 15:24:21 -0700 | [diff] [blame] | 93 | # components. If the target is a block device or the FORCE_FULL arg is "true" |
| 94 | # we'll do a full install. Otherwise, it'll be just enough to boot. |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 95 | # Invoke as: command (not subshell) |
Tan Gao | 048e131 | 2010-08-06 08:26:03 -0700 | [diff] [blame] | 96 | # Args: |
| 97 | # TARGET |
| 98 | # ROOTFS_IMG_SECTORS |
| 99 | # STATEFUL_IMG_SECTORS |
| 100 | # PMBRCODE |
| 101 | # ESP_IMG_SECTORS |
| 102 | # FORCE_FULL |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 103 | # Return: nothing |
| 104 | # Side effects: Sets these global variables describing the GPT partitions |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 105 | # (all units are 512-byte sectors): |
| 106 | # NUM_ESP_SECTORS |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 107 | # NUM_KERN_SECTORS |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 108 | # NUM_OEM_SECTORS |
| 109 | # NUM_RESERVED_SECTORS |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 110 | # NUM_ROOTFS_SECTORS |
| 111 | # NUM_STATEFUL_SECTORS |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 112 | # START_ESP |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 113 | # START_KERN_A |
| 114 | # START_KERN_B |
| 115 | # START_OEM |
| 116 | # START_RESERVED |
| 117 | # START_ROOTFS_A |
| 118 | # START_ROOTFS_B |
| 119 | # START_STATEFUL |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 120 | install_gpt() { |
| 121 | local outdev=$1 |
Tan Gao | 048e131 | 2010-08-06 08:26:03 -0700 | [diff] [blame] | 122 | local rootfs_img_sectors=$2 |
| 123 | local stateful_img_sectors=$3 |
Tan Gao | 7bdbc1e | 2010-08-03 16:48:36 -0700 | [diff] [blame] | 124 | local pmbrcode=$4 |
Tan Gao | 048e131 | 2010-08-06 08:26:03 -0700 | [diff] [blame] | 125 | local esp_img_sectors=$5 |
Tan Gao | 7bdbc1e | 2010-08-03 16:48:36 -0700 | [diff] [blame] | 126 | local force_full="${6:-}" |
| 127 | local rootfs_size="${7:-1024}" # 1G |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 128 | |
| 129 | # The gpt tool requires a fixed-size target to work on, so we may have to |
| 130 | # create a file of the appropriate size. Let's figure out what that size is |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 131 | # now. The full partition layout will look something like this (indented |
| 132 | # lines indicate reserved regions that do not have any useful content at the |
| 133 | # moment). |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 134 | # |
| 135 | # PMBR (512 bytes) |
| 136 | # Primary GPT Header (512 bytes) |
| 137 | # Primary GPT Table (16K) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 138 | # Kernel C (placeholder for future use only) partition 6 |
| 139 | # Rootfs C (placeholder for future use only) partition 7 |
| 140 | # future use partition 9 |
| 141 | # future use partition 10 |
| 142 | # future use partition 11 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 143 | # Kernel A partition 2 |
| 144 | # Kernel B partition 4 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 145 | # OEM Customization (16M) partition 8 |
| 146 | # reserved space (64M) |
| 147 | # EFI System Partition (temporary) partition 12 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 148 | # Stateful partition (as large as possible) partition 1 |
| 149 | # Rootfs B partition 5 |
| 150 | # Rootfs A partition 3 |
| 151 | # Secondary GPT Table (16K) |
| 152 | # Secondary GPT Header (512 bytes) |
| 153 | # |
| 154 | # Please refer to the official ChromeOS documentation for the details and |
| 155 | # explanation behind the layout and partition numbering scheme. The short |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 156 | # version is that 1) we want to avoid ever changing the purpose or number of |
| 157 | # an existing partition, 2) we want to be able to add new partitions later |
| 158 | # without breaking current scripts, and 3) we may someday need to increase |
| 159 | # the size of the rootfs during an upgrade, which means shrinking the size of |
| 160 | # the stateful partition on a live system. |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 161 | # |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 162 | # The EFI GPT spec requires that all valid partitions be at least one sector |
| 163 | # in size, and non-overlapping. |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 164 | |
| 165 | # Here are the size limits that we're currently requiring |
| 166 | local max_kern_sectors=32768 # 16M |
Zelidrag Hornung | 246db7c | 2010-06-02 10:20:29 -0700 | [diff] [blame] | 167 | local max_rootfs_sectors=$((${rootfs_size} * 2 * 1024)) # 1G by default |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 168 | local max_oem_sectors=32768 # 16M |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 169 | local max_reserved_sectors=131072 # 64M |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 170 | local max_esp_sectors=32768 # 16M |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 171 | local min_stateful_sectors=262144 # 128M, expands to fill available space |
| 172 | |
| 173 | local num_pmbr_sectors=1 |
| 174 | local num_gpt_hdr_sectors=1 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 175 | local num_gpt_table_sectors=32 # 16K |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 176 | local num_footer_sectors=$(($num_gpt_hdr_sectors + $num_gpt_table_sectors)) |
| 177 | local num_header_sectors=$(($num_pmbr_sectors + $num_footer_sectors)) |
| 178 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 179 | # In order to align to a 4096-byte boundary, there should be several empty |
| 180 | # sectors available following the header. We'll pack the single-sector-sized |
| 181 | # unused partitions in there. |
| 182 | local start_kern_c=$(($num_header_sectors)) |
| 183 | local num_kern_c_sectors=1 |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 184 | local kern_c_priority=0 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 185 | local start_rootfs_c=$(($start_kern_c + 1)) |
| 186 | local num_rootfs_c_sectors=1 |
| 187 | local start_future_9=$(($start_rootfs_c + 1)) |
| 188 | local num_future_sectors=1 |
| 189 | local start_future_10=$(($start_future_9 + 1)) |
| 190 | local start_future_11=$(($start_future_10 + 1)) |
| 191 | |
| 192 | local start_useful=$(roundup $(($start_future_11 + 1))) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 193 | |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 194 | locate_gpt |
| 195 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 196 | # What are we doing? |
Andrew de los Reyes | 76658f0 | 2010-04-02 15:17:54 -0700 | [diff] [blame] | 197 | if [ -b "$outdev" -o "$force_full" = "true" ]; then |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 198 | # Block device, need to be root. |
Andrew de los Reyes | 76658f0 | 2010-04-02 15:17:54 -0700 | [diff] [blame] | 199 | if [ -b "$outdev" ]; then |
Andrew de los Reyes | 31e39f1 | 2010-04-01 15:24:21 -0700 | [diff] [blame] | 200 | local sudo=sudo |
| 201 | else |
| 202 | local sudo="" |
| 203 | fi |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 204 | |
| 205 | # Full install, use max sizes and create both A & B images. |
| 206 | NUM_KERN_SECTORS=$max_kern_sectors |
| 207 | NUM_ROOTFS_SECTORS=$max_rootfs_sectors |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 208 | NUM_OEM_SECTORS=$max_oem_sectors |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 209 | NUM_ESP_SECTORS=$max_esp_sectors |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 210 | NUM_RESERVED_SECTORS=$max_reserved_sectors |
| 211 | |
| 212 | # Where do things go? |
| 213 | START_KERN_A=$start_useful |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 214 | local num_kern_a_sectors=$NUM_KERN_SECTORS |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 215 | local kern_a_priority=15 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 216 | START_KERN_B=$(($START_KERN_A + $NUM_KERN_SECTORS)) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 217 | local num_kern_b_sectors=$NUM_KERN_SECTORS |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 218 | local kern_b_priority=15 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 219 | START_OEM=$(($START_KERN_B + $NUM_KERN_SECTORS)) |
| 220 | START_RESERVED=$(($START_OEM + $NUM_OEM_SECTORS)) |
| 221 | START_ESP=$(($START_RESERVED + $NUM_RESERVED_SECTORS)) |
| 222 | START_STATEFUL=$(($START_ESP + $NUM_ESP_SECTORS)) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 223 | |
| 224 | local total_sectors=$(numsectors $outdev) |
| 225 | local start_gpt_footer=$(($total_sectors - $num_footer_sectors)) |
| 226 | local end_useful=$(rounddown $start_gpt_footer) |
| 227 | |
| 228 | START_ROOTFS_A=$(($end_useful - $NUM_ROOTFS_SECTORS)) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 229 | local num_rootfs_a_sectors=$NUM_ROOTFS_SECTORS |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 230 | START_ROOTFS_B=$(($START_ROOTFS_A - $NUM_ROOTFS_SECTORS)) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 231 | local num_rootfs_b_sectors=$NUM_ROOTFS_SECTORS |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 232 | |
| 233 | NUM_STATEFUL_SECTORS=$(($START_ROOTFS_B - $START_STATEFUL)) |
| 234 | else |
| 235 | # Just a local file. |
| 236 | local sudo= |
| 237 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 238 | # We're just going to fill partitions 1, 2, 3, 8, and 12. The others will |
| 239 | # be present but as small as possible. The disk layout isn't crucial here, |
| 240 | # because we won't be able to upgrade this image in-place as it's only for |
| 241 | # installation purposes. |
Tan Gao | 048e131 | 2010-08-06 08:26:03 -0700 | [diff] [blame] | 242 | NUM_STATEFUL_SECTORS=$(roundup $stateful_img_sectors) |
Bill Richardson | 8982296 | 2010-08-03 12:50:45 -0700 | [diff] [blame] | 243 | NUM_KERN_SECTORS=$max_kern_sectors |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 244 | local num_kern_a_sectors=$NUM_KERN_SECTORS |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 245 | local kern_a_priority=15 |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 246 | local num_kern_b_sectors=1 |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 247 | local kern_b_priority=0 |
Tan Gao | 048e131 | 2010-08-06 08:26:03 -0700 | [diff] [blame] | 248 | NUM_ROOTFS_SECTORS=$(roundup $rootfs_img_sectors) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 249 | local num_rootfs_a_sectors=$NUM_ROOTFS_SECTORS |
| 250 | local num_rootfs_b_sectors=1 |
| 251 | NUM_OEM_SECTORS=$max_oem_sectors |
Tan Gao | 048e131 | 2010-08-06 08:26:03 -0700 | [diff] [blame] | 252 | NUM_ESP_SECTORS=$(roundup $esp_img_sectors) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 253 | NUM_RESERVED_SECTORS=1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 254 | |
| 255 | START_KERN_A=$start_useful |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 256 | START_ROOTFS_A=$(($START_KERN_A + $NUM_KERN_SECTORS)) |
| 257 | START_STATEFUL=$(($START_ROOTFS_A + $NUM_ROOTFS_SECTORS)) |
| 258 | START_OEM=$(($START_STATEFUL + $NUM_STATEFUL_SECTORS)) |
| 259 | START_ESP=$(($START_OEM + $NUM_OEM_SECTORS)) |
| 260 | START_KERN_B=$(($START_ESP + $NUM_ESP_SECTORS)) |
Tan Gao | 19a0fcf | 2010-05-19 14:19:55 -0700 | [diff] [blame] | 261 | START_ROOTFS_B=$(($START_KERN_B + $num_kern_b_sectors)) |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 262 | START_RESERVED=$(($START_ROOTFS_B + $num_rootfs_b_sectors)) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 263 | |
| 264 | # For minimal install, we're not worried about the secondary GPT header |
| 265 | # being at the end of the device because we're almost always writing to a |
| 266 | # file. If that's not true, the secondary will just be invalid. |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 267 | local start_gpt_footer=$(($START_RESERVED + $NUM_RESERVED_SECTORS)) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 268 | local end_useful=$start_gpt_footer |
| 269 | |
| 270 | local total_sectors=$(($start_gpt_footer + $num_footer_sectors)) |
| 271 | |
| 272 | # Create the image file if it doesn't exist. |
| 273 | if [ ! -e ${outdev} ]; then |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 274 | $sudo dd if=/dev/zero of=${outdev} bs=512 count=1 \ |
| 275 | seek=$(($total_sectors - 1)) |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 276 | fi |
| 277 | fi |
| 278 | |
| 279 | echo "Creating partition tables..." |
| 280 | |
| 281 | # Zap any old partitions (otherwise gpt complains). |
| 282 | $sudo dd if=/dev/zero of=${outdev} conv=notrunc bs=512 \ |
| 283 | count=$num_header_sectors |
| 284 | $sudo dd if=/dev/zero of=${outdev} conv=notrunc bs=512 \ |
| 285 | seek=${start_gpt_footer} count=$num_footer_sectors |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 286 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 287 | # Create the new GPT partitions. The order determines the partition number. |
| 288 | # Note that the partition label is in the GPT only. The filesystem label is |
| 289 | # what's used to populate /dev/disk/by-label/, and this is not that. |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 290 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 291 | $sudo $GPT create ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 292 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 293 | $sudo $GPT add -b ${START_STATEFUL} -s ${NUM_STATEFUL_SECTORS} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 294 | -t data -l "STATE" ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 295 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 296 | $sudo $GPT add -b ${START_KERN_A} -s ${num_kern_a_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 297 | -t kernel -l "KERN-A" -S 0 -T 15 -P ${kern_a_priority} ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 298 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 299 | $sudo $GPT add -b ${START_ROOTFS_A} -s ${num_rootfs_a_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 300 | -t rootfs -l "ROOT-A" ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 301 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 302 | $sudo $GPT add -b ${START_KERN_B} -s ${num_kern_b_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 303 | -t kernel -l "KERN-B" -S 0 -T 15 -P ${kern_b_priority} ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 304 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 305 | $sudo $GPT add -b ${START_ROOTFS_B} -s ${num_rootfs_b_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 306 | -t rootfs -l "ROOT-B" ${outdev} |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 307 | |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 308 | $sudo $GPT add -b ${start_kern_c} -s ${num_kern_c_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 309 | -t kernel -l "KERN-C" -S 0 -T 15 -P ${kern_c_priority} ${outdev} |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 310 | |
| 311 | $sudo $GPT add -b ${start_rootfs_c} -s ${num_rootfs_c_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 312 | -t rootfs -l "ROOT-C" ${outdev} |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 313 | |
| 314 | $sudo $GPT add -b ${START_OEM} -s ${NUM_OEM_SECTORS} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 315 | -t data -l "OEM" ${outdev} |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 316 | |
| 317 | $sudo $GPT add -b ${start_future_9} -s ${num_future_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 318 | -t reserved -l "reserved" ${outdev} |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 319 | |
| 320 | $sudo $GPT add -b ${start_future_10} -s ${num_future_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 321 | -t reserved -l "reserved" ${outdev} |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 322 | |
| 323 | $sudo $GPT add -b ${start_future_11} -s ${num_future_sectors} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 324 | -t reserved -l "reserved" ${outdev} |
Bill Richardson | d6b71b0 | 2010-04-14 12:46:14 -0700 | [diff] [blame] | 325 | |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 326 | $sudo $GPT add -b ${START_ESP} -s ${NUM_ESP_SECTORS} \ |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 327 | -t efi -l "EFI-SYSTEM" ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 328 | |
Andrew de los Reyes | 43239d8 | 2010-07-15 22:06:42 -0700 | [diff] [blame] | 329 | # Create the PMBR and instruct it to boot off the EFI partition (12). |
| 330 | # The EFI partition contains both the EFI bootloader and the legacy |
| 331 | # BIOS loader. |
| 332 | $sudo $GPT boot -p -b ${pmbrcode} -i 12 ${outdev} |
Bill Richardson | 13dbcf2 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 333 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 334 | # Display what we've got |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 335 | $sudo $GPT show ${outdev} |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 336 | |
| 337 | sync |
| 338 | } |
| 339 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 340 | # Read GPT table to find the starting location of a specific partition. |
| 341 | # Invoke as: subshell |
| 342 | # Args: DEVICE PARTNUM |
| 343 | # Returns: offset (in sectors) of partition PARTNUM |
| 344 | partoffset() { |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 345 | sudo $GPT show -b -i $2 $1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | # Read GPT table to find the size of a specific partition. |
| 349 | # Invoke as: subshell |
| 350 | # Args: DEVICE PARTNUM |
| 351 | # Returns: size (in sectors) of partition PARTNUM |
| 352 | partsize() { |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 353 | sudo $GPT show -s -i $2 $1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 356 | # Extract the whole disk block device from the partition device. |
| 357 | # This works for /dev/sda3 (-> /dev/sda) as well as /dev/mmcblk0p2 |
| 358 | # (-> /dev/mmcblk0). |
| 359 | get_block_dev_from_partition_dev() { |
| 360 | local partition=$1 |
| 361 | if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then |
| 362 | echo "Invalid partition name: $partition" >&2 |
| 363 | exit 1 |
| 364 | fi |
Will Drewry | 056e9c8 | 2010-08-06 16:10:59 -0500 | [diff] [blame^] | 365 | # Removes any trailing digits. |
| 366 | local block=$(echo "$partition" | sed -e 's/[0-9]*$//') |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 367 | # If needed, strip the trailing 'p'. |
| 368 | if (expr match "$block" ".*[0-9]p$" >/dev/null); then |
| 369 | echo "${block%p}" |
| 370 | else |
| 371 | echo "$block" |
| 372 | fi |
| 373 | } |
| 374 | |
| 375 | # Extract the partition number from the partition device. |
| 376 | # This works for /dev/sda3 (-> 3) as well as /dev/mmcblk0p2 (-> 2). |
| 377 | get_partition_number() { |
| 378 | local partition=$1 |
| 379 | if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then |
| 380 | echo "Invalid partition name: $partition" >&2 |
| 381 | exit 1 |
| 382 | fi |
| 383 | # Extract the last digit. |
| 384 | echo "$partition" | sed -e 's/^.*\([0-9]\)$/\1/' |
| 385 | } |
| 386 | |
| 387 | # Construct a partition device name from a whole disk block device and a |
| 388 | # partition number. |
| 389 | # This works for [/dev/sda, 3] (-> /dev/sda3) as well as [/dev/mmcblk0, 2] |
| 390 | # (-> /dev/mmcblk0p2). |
| 391 | make_partition_dev() { |
| 392 | local block=$1 |
| 393 | local num=$2 |
| 394 | # If the disk block device ends with a number, we add a 'p' before the |
| 395 | # partition number. |
| 396 | if (expr match "$block" ".*[0-9]$" >/dev/null) ; then |
| 397 | echo "${block}p${num}" |
| 398 | else |
| 399 | echo "${block}${num}" |
| 400 | fi |
| 401 | } |
| 402 | |
| 403 | # Construct a PMBR for the arm platform, Uboot will load PMBR and "autoscr" it. |
| 404 | # Arguments List: |
| 405 | # $1 : Kernel Partition Offset. |
| 406 | # $2 : Kernel Partition Size ( in Sectors ). |
| 407 | # $3 : DEVICE |
| 408 | # Return file path of the generated PMBR. |
| 409 | |
| 410 | make_arm_mbr() { |
| 411 | # Create the U-Boot script to copy the kernel into memory and boot it. |
| 412 | local KERNEL_OFFSET=$(printf "0x%08x" ${1}) |
| 413 | local KERNEL_SECS_HEX=$(printf "0x%08x" ${2}) |
| 414 | local DEVICE=${3} |
robotboy | d3cfe22 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 415 | local EXTRA_BOOTARGS=${4} |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 416 | |
| 417 | BOOTARGS="root=/dev/mmcblk${DEVICE}p3" |
| 418 | BOOTARGS="${BOOTARGS} init=/sbin/init" |
| 419 | BOOTARGS="${BOOTARGS} console=ttySAC2,115200" |
| 420 | BOOTARGS="${BOOTARGS} mem=1024M" |
| 421 | BOOTARGS="${BOOTARGS} rootwait" |
robotboy | d3cfe22 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 422 | BOOTARGS="${BOOTARGS} ${EXTRA_BOOTARGS}" |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 423 | |
| 424 | MBR_SCRIPT="/var/tmp/mbr_script" |
| 425 | echo -e "echo\necho ---- ChromeOS Boot ----\necho\n" \ |
| 426 | "setenv bootargs ${BOOTARGS}\n" \ |
| 427 | "mmc read ${DEVICE} C0008000 $KERNEL_OFFSET $KERNEL_SECS_HEX\n" \ |
| 428 | "bootm C0008000" > ${MBR_SCRIPT} |
| 429 | MKIMAGE="/usr/bin/mkimage" |
| 430 | if [ -x "$MKIMAGE" ]; then |
| 431 | MBR_SCRIPT_UIMG="${MBR_SCRIPT}.uimg" |
| 432 | "$MKIMAGE" -A "arm" -O linux -T script -a 0 -e 0 -n "COS boot" \ |
| 433 | -d ${MBR_SCRIPT} ${MBR_SCRIPT_UIMG} >&2 |
| 434 | else |
| 435 | echo "Error: u-boot mkimage not found or not executable." >&2 |
| 436 | exit 1 |
| 437 | fi |
| 438 | echo ${MBR_SCRIPT_UIMG} |
| 439 | } |
Bill Richardson | 05c4e34 | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 440 | |
| 441 | |
| 442 | # The scripts that source this file typically want to use the root password as |
| 443 | # confirmation, unless the --run_as_root flag is given. |
| 444 | dont_run_as_root() { |
| 445 | if [ $(id -u) -eq "0" -a "${FLAGS_run_as_root}" -eq "${FLAGS_FALSE}" ] |
| 446 | then |
| 447 | echo "Note: You must be the 'chronos' user to run this script. Unless" |
| 448 | echo "you pass --run_as_root and run as root." |
| 449 | exit 1 |
| 450 | fi |
| 451 | } |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 452 | |
| 453 | list_usb_disks() { |
| 454 | local sd |
| 455 | for sd in /sys/block/sd*; do |
Paul Stewart | 248f464 | 2010-06-09 10:11:15 -0700 | [diff] [blame] | 456 | if readlink -f ${sd}/device | grep -q usb && |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 457 | [ "$(cat ${sd}/removable)" = 1 ]; then |
| 458 | echo ${sd##*/} |
| 459 | fi |
| 460 | done |
| 461 | } |
| 462 | |
| 463 | get_disk_info() { |
| 464 | # look for a "given" file somewhere in the path upwards from the device |
| 465 | local dev_path=/sys/block/${1}/device |
| 466 | while [ -d "${dev_path}" -a "${dev_path}" != "/sys" ]; do |
| 467 | if [ -f "${dev_path}/${2}" ]; then |
| 468 | cat "${dev_path}/${2}" |
| 469 | return |
| 470 | fi |
| 471 | dev_path=$(readlink -f ${dev_path}/..) |
| 472 | done |
| 473 | echo '[Unknown]' |
| 474 | } |