blob: 8486c7b2a08161b0c97d8c499819d3bc8c862c9b [file] [log] [blame]
Mike Frysinger1963df12013-06-03 14:01:35 -04001#!/bin/sh
Bill Richardsoneff5b062010-03-30 14:17:34 -07002# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# This contains common constants and functions for installer scripts. This must
7# evaluate properly for both /bin/bash and /bin/sh, since it's used both to
8# create the initial image at compile time and to install or upgrade a running
9# image.
10
Bill Richardsoneff5b062010-03-30 14:17:34 -070011# The GPT tables describe things in terms of 512-byte sectors, but some
12# filesystems prefer 4096-byte blocks. These functions help with alignment
13# issues.
14
Steven 'Steve' Kendallbde39b12015-09-30 12:59:02 -040015PARTITION_NUM_STATE=1
16PARTITION_NUM_KERN_A=2
17PARTITION_NUM_ROOT_A=3
18PARTITION_NUM_KERN_B=4
19PARTITION_NUM_ROOT_B=5
20PARTITION_NUM_KERN_C=6
21PARTITION_NUM_ROOT_C=7
22PARTITION_NUM_OEM=8
23PARTITION_NUM_RWFW=11
24PARTITION_NUM_EFI_SYSTEM=12
25
Bill Richardsoneff5b062010-03-30 14:17:34 -070026# This returns the size of a file or device in 512-byte sectors, rounded up if
27# needed.
28# Invoke as: subshell
29# Args: FILENAME
30# Return: whole number of sectors needed to fully contain FILENAME
31numsectors() {
Jie Sun1f9d4122010-04-12 17:04:36 -070032 if [ -b "${1}" ]; then
Bill Richardsoneff5b062010-03-30 14:17:34 -070033 dev=${1##*/}
Jie Sun1f9d4122010-04-12 17:04:36 -070034 if [ -e /sys/block/$dev/size ]; then
35 cat /sys/block/$dev/size
36 else
37 part=${1##*/}
38 block=$(get_block_dev_from_partition_dev "${1}")
39 block=${block##*/}
40 cat /sys/block/$block/$part/size
41 fi
42 else
Bill Richardsoneff5b062010-03-30 14:17:34 -070043 local bytes=$(stat -c%s "$1")
44 local sectors=$(( $bytes / 512 ))
45 local rem=$(( $bytes % 512 ))
46 if [ $rem -ne 0 ]; then
47 sectors=$(( $sectors + 1 ))
48 fi
49 echo $sectors
Jie Sun1f9d4122010-04-12 17:04:36 -070050 fi
Bill Richardsoneff5b062010-03-30 14:17:34 -070051}
52
Bill Richardson7c358a92010-06-11 09:16:03 -070053# Locate the cgpt tool. It should already be installed in the build chroot,
robotboya7684292010-04-21 14:46:00 -070054# but some of these functions may be invoked outside the chroot (by
Bill Richardsoneff5b062010-03-30 14:17:34 -070055# image_to_usb or similar), so we need to find it.
robotboya7684292010-04-21 14:46:00 -070056GPT=""
Yusuke Satod55cea92010-04-21 10:46:59 +090057
robotboya7684292010-04-21 14:46:00 -070058locate_gpt() {
59 if [ -z "$GPT" ]; then
Bill Richardsonf67f8442010-12-01 08:27:37 -080060 if [ -x "${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" ]; then
61 GPT="${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt"
62 else
63 GPT=$(which cgpt 2>/dev/null) || /bin/true
64 if [ -z "$GPT" ]; then
Bill Richardson7c358a92010-06-11 09:16:03 -070065 echo "can't find cgpt tool" 1>&2
robotboya7684292010-04-21 14:46:00 -070066 exit 1
67 fi
68 fi
69 fi
70}
Bill Richardsoneff5b062010-03-30 14:17:34 -070071
Bill Richardsoneff5b062010-03-30 14:17:34 -070072# Read GPT table to find the starting location of a specific partition.
73# Invoke as: subshell
74# Args: DEVICE PARTNUM
75# Returns: offset (in sectors) of partition PARTNUM
76partoffset() {
Elly Jones7f1dc652011-08-12 15:44:18 -040077 sudo $GPT show -b -i $2 $1
Bill Richardsoneff5b062010-03-30 14:17:34 -070078}
79
80# Read GPT table to find the size of a specific partition.
81# Invoke as: subshell
82# Args: DEVICE PARTNUM
83# Returns: size (in sectors) of partition PARTNUM
84partsize() {
Elly Jones7f1dc652011-08-12 15:44:18 -040085 sudo $GPT show -s -i $2 $1
Bill Richardsoneff5b062010-03-30 14:17:34 -070086}
87
Jie Sun1f9d4122010-04-12 17:04:36 -070088# Extract the whole disk block device from the partition device.
89# This works for /dev/sda3 (-> /dev/sda) as well as /dev/mmcblk0p2
90# (-> /dev/mmcblk0).
91get_block_dev_from_partition_dev() {
92 local partition=$1
93 if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then
94 echo "Invalid partition name: $partition" >&2
95 exit 1
96 fi
Will Drewry056e9c82010-08-06 16:10:59 -050097 # Removes any trailing digits.
98 local block=$(echo "$partition" | sed -e 's/[0-9]*$//')
Jie Sun1f9d4122010-04-12 17:04:36 -070099 # If needed, strip the trailing 'p'.
100 if (expr match "$block" ".*[0-9]p$" >/dev/null); then
101 echo "${block%p}"
102 else
103 echo "$block"
104 fi
105}
106
107# Extract the partition number from the partition device.
108# This works for /dev/sda3 (-> 3) as well as /dev/mmcblk0p2 (-> 2).
109get_partition_number() {
110 local partition=$1
111 if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then
112 echo "Invalid partition name: $partition" >&2
113 exit 1
114 fi
115 # Extract the last digit.
116 echo "$partition" | sed -e 's/^.*\([0-9]\)$/\1/'
117}
118
119# Construct a partition device name from a whole disk block device and a
120# partition number.
121# This works for [/dev/sda, 3] (-> /dev/sda3) as well as [/dev/mmcblk0, 2]
122# (-> /dev/mmcblk0p2).
123make_partition_dev() {
124 local block=$1
125 local num=$2
126 # If the disk block device ends with a number, we add a 'p' before the
127 # partition number.
128 if (expr match "$block" ".*[0-9]$" >/dev/null) ; then
129 echo "${block}p${num}"
130 else
131 echo "${block}${num}"
132 fi
133}
134
Gwendal Grignou20f0ca72014-03-13 16:14:43 -0700135# Return the type of device.
136#
137# The type can be:
138# MMC, SD for device managed by the MMC stack
139# ATA for ATA disk
140# OTHER for other devices.
141get_device_type() {
142 local dev="$(basename "$1")"
143 local vdr
144 local type_file
145 local vendor_file
146 type_file="/sys/block/${dev}/device/type"
147 # To detect device managed by the MMC stack
148 case $(readlink -f "${type_file}") in
149 *mmc*)
150 cat "${type_file}"
151 ;;
152 *usb*)
153 # Now if it contains 'usb', it is managed through
154 # a USB controller.
155 echo "USB"
156 ;;
157 *target*)
158 # Other SCSI devices.
159 # Check if it is an ATA device.
160 vdr="$(cat "/sys/block/${dev}/device/vendor")"
161 if [ "${vdr%% *}" = "ATA" ]; then
162 echo "ATA"
163 else
164 echo "OTHER"
165 fi
166 ;;
167 *)
168 echo "OTHER"
169 esac
170}
171
Gwendal Grignou732af512014-04-07 20:07:29 +0000172# ATA disk have ATA as vendor.
173# They may not contain ata in their device path if behind a SAS
174# controller.
175# Exclude disks with size 0, it means they did not spin up properly.
176list_fixed_ata_disks() {
177 local sd
178 local remo
179 local vdr
180 local size
181
182 for sd in /sys/block/sd*; do
183 if [ ! -r "${sd}/size" ]; then
184 continue
Paul Stewart04f3ef12010-05-27 15:56:42 -0700185 fi
Gwendal Grignou732af512014-04-07 20:07:29 +0000186 size=$(cat "${sd}/size")
187 remo=$(cat "${sd}/removable")
188 vdr=$(cat "${sd}/device/vendor")
189 if [ "${vdr%% *}" = "ATA" -a ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then
190 echo "${sd##*/}"
191 fi
Paul Stewart04f3ef12010-05-27 15:56:42 -0700192 done
Gwendal Grignou732af512014-04-07 20:07:29 +0000193}
194
195# We assume we only have eMMC devices, not removable MMC devices.
196# also, do not consider special hardware partitions non the eMMC, like boot.
197# These devices are built on top of the eMMC sysfs path:
198# /sys/block/mmcblk0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0
199# /sys/block/mmcblk0boot0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot0
200# /sys/block/mmcblk0boot1 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot1
201# /sys/block/mmcblk0rpmb -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0rpmb
202#
203# Their device link points back to mmcblk0, not to the hardware
204# device (mmc0:0001). Therefore there is no type in their device link.
205# (it should be /device/device/type)
206list_fixed_mmc_disks() {
207 local mmc
208 local type_file
209 for mmc in /sys/block/mmcblk*; do
210 type_file="${mmc}/device/type"
211 if [ -r "${type_file}" ]; then
212 if [ "$(cat "${type_file}")" = "MMC" ]; then
213 echo "${mmc##*/}"
214 fi
215 fi
216 done
217}
218
219# Find the drive to install based on the build write_cgpt.sh
220# script. If not found, return ""
221get_fixed_dst_drive() {
222 local dev
223 if [ -z "${DEFAULT_ROOTDEV}" ]; then
224 dev=""
225 else
226 # No " here, the variable may contain wildcards.
227 dev="/dev/$(basename ${DEFAULT_ROOTDEV})"
228 if [ ! -b "${dev}" ]; then
229 # The device is not found
230 dev=""
231 fi
232 fi
233 echo "${dev}"
Paul Stewart04f3ef12010-05-27 15:56:42 -0700234}
Liam McLoughlin2dd21d62012-07-09 17:45:06 -0700235
Liam McLoughlin87aa6982012-08-13 13:49:34 -0700236legacy_offset_size_export() {
237 # Exports all the variables that install_gpt did previously.
238 # This should disappear eventually, but it's here to make existing
239 # code work for now.
240
Steven 'Steve' Kendallbde39b12015-09-30 12:59:02 -0400241 START_STATEFUL=$(partoffset $1 ${PARTITION_NUM_STATE})
242 START_KERN_A=$(partoffset $1 ${PARTITION_NUM_KERN_A})
243 START_ROOTFS_A=$(partoffset $1 ${PARTITION_NUM_ROOT_A})
244 START_KERN_B=$(partoffset $1 ${PARTITION_NUM_KERN_B})
245 START_ROOTFS_B=$(partoffset $1 ${PARTITION_NUM_ROOT_B})
246 START_OEM=$(partoffset $1 ${PARTITION_NUM_OEM})
247 START_RWFW=$(partoffset $1 ${PARTITION_NUM_RWFW})
248 START_ESP=$(partoffset $1 ${PARTITION_NUM_EFI_SYSTEM})
Liam McLoughlin87aa6982012-08-13 13:49:34 -0700249
Steven 'Steve' Kendallbde39b12015-09-30 12:59:02 -0400250 NUM_STATEFUL_SECTORS=$(partsize $1 ${PARTITION_NUM_STATE})
251 NUM_KERN_SECTORS=$(partsize $1 ${PARTITION_NUM_KERN_A})
252 NUM_ROOTFS_SECTORS=$(partsize $1 ${PARTITION_NUM_ROOT_A})
253 NUM_OEM_SECTORS=$(partsize $1 ${PARTITION_NUM_OEM})
254 NUM_RWFW_SECTORS=$(partsize $1 ${PARTITION_NUM_RWFW})
255 NUM_ESP_SECTORS=$(partsize $1 ${PARTITION_NUM_EFI_SYSTEM})
Liam McLoughlin87aa6982012-08-13 13:49:34 -0700256
Steven 'Steve' Kendallbde39b12015-09-30 12:59:02 -0400257 STATEFUL_IMG_SECTORS=$(partsize $1 ${PARTITION_NUM_STATE})
258 KERNEL_IMG_SECTORS=$(partsize $1 ${PARTITION_NUM_KERN_A})
259 ROOTFS_IMG_SECTORS=$(partsize $1 ${PARTITION_NUM_ROOT_A})
260 OEM_IMG_SECTORS=$(partsize $1 ${PARTITION_NUM_OEM})
261 ESP_IMG_SECTORS=$(partsize $1 ${PARTITION_NUM_EFI_SYSTEM})
Liam McLoughlin87aa6982012-08-13 13:49:34 -0700262}
263
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700264edit_mbr() {
Liam McLoughlin2dd21d62012-07-09 17:45:06 -0700265 locate_gpt
Steven 'Steve' Kendallbde39b12015-09-30 12:59:02 -0400266 local start_esp=$(partoffset "$1" ${PARTITION_NUM_EFI_SYSTEM})
267 local num_esp_sectors=$(partsize "$1" ${PARTITION_NUM_EFI_SYSTEM})
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700268 sfdisk "${1}" <<EOF
Liam McLoughlin2dd21d62012-07-09 17:45:06 -0700269unit: sectors
270
271disk1 : start= $start_esp, size= $num_esp_sectors, Id= c, bootable
272disk2 : start= 1, size= 1, Id= ee
273EOF
274}
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700275
276install_hybrid_mbr() {
277 # Creates a hybrid MBR which points the MBR partition 1 to GPT
278 # partition 12 (ESP). This is useful on ARM boards that boot
J. Richard Barnette2db77d42015-03-19 17:19:15 -0700279 # from MBR formatted disks only.
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700280 #
281 # Currently, this code path is used principally to install to
282 # SD cards using chromeos-install run from inside the chroot.
J. Richard Barnette2db77d42015-03-19 17:19:15 -0700283 # In that environment, `sfdisk` can be racing with udev, leading
284 # to EBUSY when it calls BLKRRPART for the target disk. We avoid
285 # the conflict by using `udevadm settle`, so that udev goes first.
286 # cf. crbug.com/343681.
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700287
288 echo "Creating hybrid MBR"
289 if ! edit_mbr "${1}"; then
J. Richard Barnette2db77d42015-03-19 17:19:15 -0700290 udevadm settle
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700291 blockdev --rereadpt "${1}"
292 fi
293}