blob: 25cd647b6cb6a7f238c6d369479651c6fcd42806 [file] [log] [blame]
Bill Richardsoneff5b062010-03-30 14:17:34 -07001# 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 Richardsoneff5b062010-03-30 14:17:34 -070010# 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
19numsectors() {
Jie Sun1f9d4122010-04-12 17:04:36 -070020 if [ -b "${1}" ]; then
Bill Richardsoneff5b062010-03-30 14:17:34 -070021 dev=${1##*/}
Jie Sun1f9d4122010-04-12 17:04:36 -070022 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 Richardsoneff5b062010-03-30 14:17:34 -070031 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 Sun1f9d4122010-04-12 17:04:36 -070038 fi
Bill Richardsoneff5b062010-03-30 14:17:34 -070039}
40
Bill Richardsond6b71b02010-04-14 12:46:14 -070041# Round a number of 512-byte sectors up to an integral number of 2Mb
42# blocks. Divisor is 2 * 1024 * 1024 / 512 == 4096.
Bill Richardsoneff5b062010-03-30 14:17:34 -070043# Invoke as: subshell
44# Args: SECTORS
45# Return: Next largest multiple-of-8 sectors (ex: 4->8, 33->40, 32->32)
46roundup() {
47 local num=$1
Bill Richardsond6b71b02010-04-14 12:46:14 -070048 local div=${2:-4096}
49 local rem=$(( $num % $div ))
Bill Richardsoneff5b062010-03-30 14:17:34 -070050
Andrew de los Reyes344709b2010-04-05 10:49:08 -070051 if [ $rem -ne 0 ]; then
Bill Richardsond6b71b02010-04-14 12:46:14 -070052 num=$(($num + $div - $rem))
Bill Richardsoneff5b062010-03-30 14:17:34 -070053 fi
54 echo $num
55}
56
Bill Richardsond6b71b02010-04-14 12:46:14 -070057# Truncate a number of 512-byte sectors down to an integral number of 2Mb
58# blocks. Divisor is 2 * 1024 * 1024 / 512 == 4096.
Bill Richardsoneff5b062010-03-30 14:17:34 -070059# Invoke as: subshell
60# Args: SECTORS
61# Return: Next smallest multiple-of-8 sectors (ex: 4->0, 33->32, 32->32)
62rounddown() {
63 local num=$1
Bill Richardsond6b71b02010-04-14 12:46:14 -070064 local div=${2:-4096}
65 local rem=$(( $num % $div ))
Bill Richardsoneff5b062010-03-30 14:17:34 -070066
Andrew de los Reyes344709b2010-04-05 10:49:08 -070067 if [ $rem -ne 0 ]; then
Bill Richardsoneff5b062010-03-30 14:17:34 -070068 num=$(($num - $rem))
69 fi
70 echo $num
71}
72
Bill Richardson7c358a92010-06-11 09:16:03 -070073# Locate the cgpt tool. It should already be installed in the build chroot,
robotboya7684292010-04-21 14:46:00 -070074# but some of these functions may be invoked outside the chroot (by
Bill Richardsoneff5b062010-03-30 14:17:34 -070075# image_to_usb or similar), so we need to find it.
robotboya7684292010-04-21 14:46:00 -070076GPT=""
Yusuke Satod55cea92010-04-21 10:46:59 +090077
robotboya7684292010-04-21 14:46:00 -070078locate_gpt() {
79 if [ -z "$GPT" ]; then
Bill Richardson7c358a92010-06-11 09:16:03 -070080 GPT=$(which cgpt 2>/dev/null) || /bin/true
robotboya7684292010-04-21 14:46:00 -070081 if [ -z "$GPT" ]; then
Bill Richardson7c358a92010-06-11 09:16:03 -070082 if [ -x "${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" ]; then
83 GPT="${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt"
robotboya7684292010-04-21 14:46:00 -070084 else
Bill Richardson7c358a92010-06-11 09:16:03 -070085 echo "can't find cgpt tool" 1>&2
robotboya7684292010-04-21 14:46:00 -070086 exit 1
87 fi
88 fi
89 fi
90}
Bill Richardsoneff5b062010-03-30 14:17:34 -070091
92# This installs a GPT into the specified device or file, using the given
Andrew de los Reyes31e39f12010-04-01 15:24:21 -070093# 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 Richardsoneff5b062010-03-30 14:17:34 -070095# Invoke as: command (not subshell)
Tan Gao048e1312010-08-06 08:26:03 -070096# Args:
97# TARGET
98# ROOTFS_IMG_SECTORS
99# STATEFUL_IMG_SECTORS
100# PMBRCODE
101# ESP_IMG_SECTORS
102# FORCE_FULL
Bill Richardsoneff5b062010-03-30 14:17:34 -0700103# Return: nothing
104# Side effects: Sets these global variables describing the GPT partitions
Bill Richardsond6b71b02010-04-14 12:46:14 -0700105# (all units are 512-byte sectors):
106# NUM_ESP_SECTORS
Bill Richardsoneff5b062010-03-30 14:17:34 -0700107# NUM_KERN_SECTORS
Bill Richardsond6b71b02010-04-14 12:46:14 -0700108# NUM_OEM_SECTORS
109# NUM_RESERVED_SECTORS
Bill Richardsoneff5b062010-03-30 14:17:34 -0700110# NUM_ROOTFS_SECTORS
111# NUM_STATEFUL_SECTORS
Bill Richardson13dbcf22010-04-06 15:00:10 -0700112# START_ESP
Bill Richardsond6b71b02010-04-14 12:46:14 -0700113# 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 Richardsoneff5b062010-03-30 14:17:34 -0700120install_gpt() {
121 local outdev=$1
Tan Gao048e1312010-08-06 08:26:03 -0700122 local rootfs_img_sectors=$2
123 local stateful_img_sectors=$3
Tan Gao7bdbc1e2010-08-03 16:48:36 -0700124 local pmbrcode=$4
Tan Gao048e1312010-08-06 08:26:03 -0700125 local esp_img_sectors=$5
Tan Gao7bdbc1e2010-08-03 16:48:36 -0700126 local force_full="${6:-}"
127 local rootfs_size="${7:-1024}" # 1G
Bill Richardsoneff5b062010-03-30 14:17:34 -0700128
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 Richardsond6b71b02010-04-14 12:46:14 -0700131 # 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 Richardsoneff5b062010-03-30 14:17:34 -0700134 #
135 # PMBR (512 bytes)
136 # Primary GPT Header (512 bytes)
137 # Primary GPT Table (16K)
Bill Richardsond6b71b02010-04-14 12:46:14 -0700138 # 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 Richardsoneff5b062010-03-30 14:17:34 -0700143 # Kernel A partition 2
144 # Kernel B partition 4
Bill Richardsond6b71b02010-04-14 12:46:14 -0700145 # OEM Customization (16M) partition 8
146 # reserved space (64M)
147 # EFI System Partition (temporary) partition 12
Bill Richardsoneff5b062010-03-30 14:17:34 -0700148 # 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 Richardsond6b71b02010-04-14 12:46:14 -0700156 # 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 Richardsoneff5b062010-03-30 14:17:34 -0700161 #
Bill Richardsond6b71b02010-04-14 12:46:14 -0700162 # The EFI GPT spec requires that all valid partitions be at least one sector
163 # in size, and non-overlapping.
Bill Richardsoneff5b062010-03-30 14:17:34 -0700164
165 # Here are the size limits that we're currently requiring
166 local max_kern_sectors=32768 # 16M
Zelidrag Hornung246db7c2010-06-02 10:20:29 -0700167 local max_rootfs_sectors=$((${rootfs_size} * 2 * 1024)) # 1G by default
Bill Richardsond6b71b02010-04-14 12:46:14 -0700168 local max_oem_sectors=32768 # 16M
Bill Richardsoneff5b062010-03-30 14:17:34 -0700169 local max_reserved_sectors=131072 # 64M
Bill Richardson13dbcf22010-04-06 15:00:10 -0700170 local max_esp_sectors=32768 # 16M
Bill Richardsoneff5b062010-03-30 14:17:34 -0700171 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 Richardsond6b71b02010-04-14 12:46:14 -0700175 local num_gpt_table_sectors=32 # 16K
Bill Richardsoneff5b062010-03-30 14:17:34 -0700176 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 Richardsond6b71b02010-04-14 12:46:14 -0700179 # 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 Richardson7c358a92010-06-11 09:16:03 -0700184 local kern_c_priority=0
Bill Richardsond6b71b02010-04-14 12:46:14 -0700185 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 Richardsoneff5b062010-03-30 14:17:34 -0700193
robotboya7684292010-04-21 14:46:00 -0700194 locate_gpt
195
Bill Richardsoneff5b062010-03-30 14:17:34 -0700196 # What are we doing?
Andrew de los Reyes76658f02010-04-02 15:17:54 -0700197 if [ -b "$outdev" -o "$force_full" = "true" ]; then
Bill Richardsoneff5b062010-03-30 14:17:34 -0700198 # Block device, need to be root.
Andrew de los Reyes76658f02010-04-02 15:17:54 -0700199 if [ -b "$outdev" ]; then
Andrew de los Reyes31e39f12010-04-01 15:24:21 -0700200 local sudo=sudo
201 else
202 local sudo=""
203 fi
Bill Richardsoneff5b062010-03-30 14:17:34 -0700204
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 Richardsond6b71b02010-04-14 12:46:14 -0700208 NUM_OEM_SECTORS=$max_oem_sectors
Bill Richardson13dbcf22010-04-06 15:00:10 -0700209 NUM_ESP_SECTORS=$max_esp_sectors
Bill Richardsoneff5b062010-03-30 14:17:34 -0700210 NUM_RESERVED_SECTORS=$max_reserved_sectors
211
212 # Where do things go?
213 START_KERN_A=$start_useful
Bill Richardsond6b71b02010-04-14 12:46:14 -0700214 local num_kern_a_sectors=$NUM_KERN_SECTORS
Bill Richardson7c358a92010-06-11 09:16:03 -0700215 local kern_a_priority=15
Bill Richardsoneff5b062010-03-30 14:17:34 -0700216 START_KERN_B=$(($START_KERN_A + $NUM_KERN_SECTORS))
Bill Richardsond6b71b02010-04-14 12:46:14 -0700217 local num_kern_b_sectors=$NUM_KERN_SECTORS
Bill Richardson7c358a92010-06-11 09:16:03 -0700218 local kern_b_priority=15
Bill Richardsond6b71b02010-04-14 12:46:14 -0700219 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 Richardsoneff5b062010-03-30 14:17:34 -0700223
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 Richardsond6b71b02010-04-14 12:46:14 -0700229 local num_rootfs_a_sectors=$NUM_ROOTFS_SECTORS
Bill Richardsoneff5b062010-03-30 14:17:34 -0700230 START_ROOTFS_B=$(($START_ROOTFS_A - $NUM_ROOTFS_SECTORS))
Bill Richardsond6b71b02010-04-14 12:46:14 -0700231 local num_rootfs_b_sectors=$NUM_ROOTFS_SECTORS
Bill Richardsoneff5b062010-03-30 14:17:34 -0700232
233 NUM_STATEFUL_SECTORS=$(($START_ROOTFS_B - $START_STATEFUL))
234 else
235 # Just a local file.
236 local sudo=
237
Bill Richardsond6b71b02010-04-14 12:46:14 -0700238 # 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 Gao048e1312010-08-06 08:26:03 -0700242 NUM_STATEFUL_SECTORS=$(roundup $stateful_img_sectors)
Bill Richardson89822962010-08-03 12:50:45 -0700243 NUM_KERN_SECTORS=$max_kern_sectors
Bill Richardsond6b71b02010-04-14 12:46:14 -0700244 local num_kern_a_sectors=$NUM_KERN_SECTORS
Bill Richardson7c358a92010-06-11 09:16:03 -0700245 local kern_a_priority=15
Bill Richardsond6b71b02010-04-14 12:46:14 -0700246 local num_kern_b_sectors=1
Bill Richardson7c358a92010-06-11 09:16:03 -0700247 local kern_b_priority=0
Tan Gao048e1312010-08-06 08:26:03 -0700248 NUM_ROOTFS_SECTORS=$(roundup $rootfs_img_sectors)
Bill Richardsond6b71b02010-04-14 12:46:14 -0700249 local num_rootfs_a_sectors=$NUM_ROOTFS_SECTORS
250 local num_rootfs_b_sectors=1
251 NUM_OEM_SECTORS=$max_oem_sectors
Tan Gao048e1312010-08-06 08:26:03 -0700252 NUM_ESP_SECTORS=$(roundup $esp_img_sectors)
Bill Richardsond6b71b02010-04-14 12:46:14 -0700253 NUM_RESERVED_SECTORS=1
Bill Richardsoneff5b062010-03-30 14:17:34 -0700254
255 START_KERN_A=$start_useful
Bill Richardsond6b71b02010-04-14 12:46:14 -0700256 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 Gao19a0fcf2010-05-19 14:19:55 -0700261 START_ROOTFS_B=$(($START_KERN_B + $num_kern_b_sectors))
Bill Richardsond6b71b02010-04-14 12:46:14 -0700262 START_RESERVED=$(($START_ROOTFS_B + $num_rootfs_b_sectors))
Bill Richardsoneff5b062010-03-30 14:17:34 -0700263
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 Richardsond6b71b02010-04-14 12:46:14 -0700267 local start_gpt_footer=$(($START_RESERVED + $NUM_RESERVED_SECTORS))
Bill Richardsoneff5b062010-03-30 14:17:34 -0700268 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 Richardsond6b71b02010-04-14 12:46:14 -0700274 $sudo dd if=/dev/zero of=${outdev} bs=512 count=1 \
275 seek=$(($total_sectors - 1))
Bill Richardsoneff5b062010-03-30 14:17:34 -0700276 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 Richardson13dbcf22010-04-06 15:00:10 -0700286
Bill Richardsoneff5b062010-03-30 14:17:34 -0700287 # 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.
robotboya7684292010-04-21 14:46:00 -0700290
Bill Richardsoneff5b062010-03-30 14:17:34 -0700291 $sudo $GPT create ${outdev}
Bill Richardson13dbcf22010-04-06 15:00:10 -0700292
Bill Richardsoneff5b062010-03-30 14:17:34 -0700293 $sudo $GPT add -b ${START_STATEFUL} -s ${NUM_STATEFUL_SECTORS} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700294 -t data -l "STATE" ${outdev}
Bill Richardson13dbcf22010-04-06 15:00:10 -0700295
Bill Richardsond6b71b02010-04-14 12:46:14 -0700296 $sudo $GPT add -b ${START_KERN_A} -s ${num_kern_a_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700297 -t kernel -l "KERN-A" -S 0 -T 15 -P ${kern_a_priority} ${outdev}
Bill Richardson13dbcf22010-04-06 15:00:10 -0700298
Bill Richardsond6b71b02010-04-14 12:46:14 -0700299 $sudo $GPT add -b ${START_ROOTFS_A} -s ${num_rootfs_a_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700300 -t rootfs -l "ROOT-A" ${outdev}
Bill Richardson13dbcf22010-04-06 15:00:10 -0700301
Bill Richardsond6b71b02010-04-14 12:46:14 -0700302 $sudo $GPT add -b ${START_KERN_B} -s ${num_kern_b_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700303 -t kernel -l "KERN-B" -S 0 -T 15 -P ${kern_b_priority} ${outdev}
Bill Richardson13dbcf22010-04-06 15:00:10 -0700304
Bill Richardsond6b71b02010-04-14 12:46:14 -0700305 $sudo $GPT add -b ${START_ROOTFS_B} -s ${num_rootfs_b_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700306 -t rootfs -l "ROOT-B" ${outdev}
Bill Richardsoneff5b062010-03-30 14:17:34 -0700307
Bill Richardsond6b71b02010-04-14 12:46:14 -0700308 $sudo $GPT add -b ${start_kern_c} -s ${num_kern_c_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700309 -t kernel -l "KERN-C" -S 0 -T 15 -P ${kern_c_priority} ${outdev}
Bill Richardsond6b71b02010-04-14 12:46:14 -0700310
311 $sudo $GPT add -b ${start_rootfs_c} -s ${num_rootfs_c_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700312 -t rootfs -l "ROOT-C" ${outdev}
Bill Richardsond6b71b02010-04-14 12:46:14 -0700313
314 $sudo $GPT add -b ${START_OEM} -s ${NUM_OEM_SECTORS} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700315 -t data -l "OEM" ${outdev}
Bill Richardsond6b71b02010-04-14 12:46:14 -0700316
317 $sudo $GPT add -b ${start_future_9} -s ${num_future_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700318 -t reserved -l "reserved" ${outdev}
Bill Richardsond6b71b02010-04-14 12:46:14 -0700319
320 $sudo $GPT add -b ${start_future_10} -s ${num_future_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700321 -t reserved -l "reserved" ${outdev}
Bill Richardsond6b71b02010-04-14 12:46:14 -0700322
323 $sudo $GPT add -b ${start_future_11} -s ${num_future_sectors} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700324 -t reserved -l "reserved" ${outdev}
Bill Richardsond6b71b02010-04-14 12:46:14 -0700325
Bill Richardson13dbcf22010-04-06 15:00:10 -0700326 $sudo $GPT add -b ${START_ESP} -s ${NUM_ESP_SECTORS} \
Bill Richardson7c358a92010-06-11 09:16:03 -0700327 -t efi -l "EFI-SYSTEM" ${outdev}
Bill Richardson13dbcf22010-04-06 15:00:10 -0700328
Andrew de los Reyes43239d82010-07-15 22:06:42 -0700329 # 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 Richardson13dbcf22010-04-06 15:00:10 -0700333
Bill Richardsoneff5b062010-03-30 14:17:34 -0700334 # Display what we've got
Bill Richardson7c358a92010-06-11 09:16:03 -0700335 $sudo $GPT show ${outdev}
Bill Richardsoneff5b062010-03-30 14:17:34 -0700336
337 sync
338}
339
Bill Richardsoneff5b062010-03-30 14:17:34 -0700340# 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
344partoffset() {
Bill Richardson7c358a92010-06-11 09:16:03 -0700345 sudo $GPT show -b -i $2 $1
Bill Richardsoneff5b062010-03-30 14:17:34 -0700346}
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
352partsize() {
Bill Richardson7c358a92010-06-11 09:16:03 -0700353 sudo $GPT show -s -i $2 $1
Bill Richardsoneff5b062010-03-30 14:17:34 -0700354}
355
Jie Sun1f9d4122010-04-12 17:04:36 -0700356# 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).
359get_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 Drewry056e9c82010-08-06 16:10:59 -0500365 # Removes any trailing digits.
366 local block=$(echo "$partition" | sed -e 's/[0-9]*$//')
Jie Sun1f9d4122010-04-12 17:04:36 -0700367 # 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).
377get_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).
391make_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
410make_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}
robotboyd3cfe222010-04-30 09:51:23 -0700415 local EXTRA_BOOTARGS=${4}
Jie Sun1f9d4122010-04-12 17:04:36 -0700416
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"
robotboyd3cfe222010-04-30 09:51:23 -0700422 BOOTARGS="${BOOTARGS} ${EXTRA_BOOTARGS}"
Jie Sun1f9d4122010-04-12 17:04:36 -0700423
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 Richardson05c4e342010-05-14 17:00:21 -0700440
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.
444dont_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 Stewart04f3ef12010-05-27 15:56:42 -0700452
453list_usb_disks() {
454 local sd
455 for sd in /sys/block/sd*; do
Paul Stewart248f4642010-06-09 10:11:15 -0700456 if readlink -f ${sd}/device | grep -q usb &&
Paul Stewart04f3ef12010-05-27 15:56:42 -0700457 [ "$(cat ${sd}/removable)" = 1 ]; then
458 echo ${sd##*/}
459 fi
460 done
461}
462
463get_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}