Mike Frysinger | 1963df1 | 2013-06-03 14:01:35 -0400 | [diff] [blame] | 1 | #!/bin/sh |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 2 | # 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 Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 11 | # 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 | |
Richard Barnette | 0c3aef0 | 2017-04-14 15:11:13 +0000 | [diff] [blame] | 15 | # These fixed partition numbers are deprecated. |
| 16 | # We should use values derived from disk_layout. |
| 17 | PARTITION_NUM_STATE=1 |
| 18 | PARTITION_NUM_KERN_A=2 |
| 19 | PARTITION_NUM_ROOT_A=3 |
| 20 | PARTITION_NUM_KERN_B=4 |
| 21 | PARTITION_NUM_ROOT_B=5 |
| 22 | PARTITION_NUM_KERN_C=6 |
| 23 | PARTITION_NUM_ROOT_C=7 |
| 24 | PARTITION_NUM_OEM=8 |
| 25 | PARTITION_NUM_RWFW=11 |
| 26 | PARTITION_NUM_EFI_SYSTEM=12 |
| 27 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 28 | # This returns the size of a file or device in 512-byte sectors, rounded up if |
| 29 | # needed. |
| 30 | # Invoke as: subshell |
| 31 | # Args: FILENAME |
| 32 | # Return: whole number of sectors needed to fully contain FILENAME |
| 33 | numsectors() { |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 34 | if [ -b "${1}" ]; then |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 35 | dev=${1##*/} |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 36 | if [ -e /sys/block/$dev/size ]; then |
| 37 | cat /sys/block/$dev/size |
| 38 | else |
| 39 | part=${1##*/} |
| 40 | block=$(get_block_dev_from_partition_dev "${1}") |
| 41 | block=${block##*/} |
| 42 | cat /sys/block/$block/$part/size |
| 43 | fi |
| 44 | else |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 45 | local bytes=$(stat -c%s "$1") |
| 46 | local sectors=$(( $bytes / 512 )) |
| 47 | local rem=$(( $bytes % 512 )) |
| 48 | if [ $rem -ne 0 ]; then |
| 49 | sectors=$(( $sectors + 1 )) |
| 50 | fi |
| 51 | echo $sectors |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 52 | fi |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 55 | # Locate the cgpt tool. It should already be installed in the build chroot, |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 56 | # but some of these functions may be invoked outside the chroot (by |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 57 | # image_to_usb or similar), so we need to find it. |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 58 | GPT="" |
Yusuke Sato | d55cea9 | 2010-04-21 10:46:59 +0900 | [diff] [blame] | 59 | |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 60 | locate_gpt() { |
| 61 | if [ -z "$GPT" ]; then |
Bill Richardson | f67f844 | 2010-12-01 08:27:37 -0800 | [diff] [blame] | 62 | if [ -x "${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" ]; then |
| 63 | GPT="${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" |
| 64 | else |
| 65 | GPT=$(which cgpt 2>/dev/null) || /bin/true |
| 66 | if [ -z "$GPT" ]; then |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 67 | echo "can't find cgpt tool" 1>&2 |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 68 | exit 1 |
| 69 | fi |
| 70 | fi |
| 71 | fi |
| 72 | } |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 73 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 74 | # Read GPT table to find the starting location of a specific partition. |
| 75 | # Invoke as: subshell |
| 76 | # Args: DEVICE PARTNUM |
| 77 | # Returns: offset (in sectors) of partition PARTNUM |
| 78 | partoffset() { |
Elly Jones | 7f1dc65 | 2011-08-12 15:44:18 -0400 | [diff] [blame] | 79 | sudo $GPT show -b -i $2 $1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | # Read GPT table to find the size of a specific partition. |
| 83 | # Invoke as: subshell |
| 84 | # Args: DEVICE PARTNUM |
| 85 | # Returns: size (in sectors) of partition PARTNUM |
| 86 | partsize() { |
Elly Jones | 7f1dc65 | 2011-08-12 15:44:18 -0400 | [diff] [blame] | 87 | sudo $GPT show -s -i $2 $1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 90 | # Extract the whole disk block device from the partition device. |
| 91 | # This works for /dev/sda3 (-> /dev/sda) as well as /dev/mmcblk0p2 |
| 92 | # (-> /dev/mmcblk0). |
| 93 | get_block_dev_from_partition_dev() { |
| 94 | local partition=$1 |
| 95 | if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then |
| 96 | echo "Invalid partition name: $partition" >&2 |
| 97 | exit 1 |
| 98 | fi |
Will Drewry | 056e9c8 | 2010-08-06 16:10:59 -0500 | [diff] [blame] | 99 | # Removes any trailing digits. |
| 100 | local block=$(echo "$partition" | sed -e 's/[0-9]*$//') |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 101 | # If needed, strip the trailing 'p'. |
| 102 | if (expr match "$block" ".*[0-9]p$" >/dev/null); then |
| 103 | echo "${block%p}" |
| 104 | else |
| 105 | echo "$block" |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | # Extract the partition number from the partition device. |
| 110 | # This works for /dev/sda3 (-> 3) as well as /dev/mmcblk0p2 (-> 2). |
| 111 | get_partition_number() { |
| 112 | local partition=$1 |
| 113 | if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then |
| 114 | echo "Invalid partition name: $partition" >&2 |
| 115 | exit 1 |
| 116 | fi |
| 117 | # Extract the last digit. |
| 118 | echo "$partition" | sed -e 's/^.*\([0-9]\)$/\1/' |
| 119 | } |
| 120 | |
| 121 | # Construct a partition device name from a whole disk block device and a |
| 122 | # partition number. |
| 123 | # This works for [/dev/sda, 3] (-> /dev/sda3) as well as [/dev/mmcblk0, 2] |
| 124 | # (-> /dev/mmcblk0p2). |
| 125 | make_partition_dev() { |
| 126 | local block=$1 |
| 127 | local num=$2 |
| 128 | # If the disk block device ends with a number, we add a 'p' before the |
| 129 | # partition number. |
| 130 | if (expr match "$block" ".*[0-9]$" >/dev/null) ; then |
| 131 | echo "${block}p${num}" |
| 132 | else |
| 133 | echo "${block}${num}" |
| 134 | fi |
| 135 | } |
| 136 | |
Gwendal Grignou | 20f0ca7 | 2014-03-13 16:14:43 -0700 | [diff] [blame] | 137 | # Return the type of device. |
| 138 | # |
| 139 | # The type can be: |
| 140 | # MMC, SD for device managed by the MMC stack |
| 141 | # ATA for ATA disk |
Deepti Patil | df2d269 | 2016-03-04 15:24:58 +0530 | [diff] [blame] | 142 | # NVME for NVMe device |
Gwendal Grignou | 20f0ca7 | 2014-03-13 16:14:43 -0700 | [diff] [blame] | 143 | # OTHER for other devices. |
| 144 | get_device_type() { |
| 145 | local dev="$(basename "$1")" |
| 146 | local vdr |
| 147 | local type_file |
| 148 | local vendor_file |
Deepti Patil | df2d269 | 2016-03-04 15:24:58 +0530 | [diff] [blame] | 149 | # True device path of a NVMe device is just a simple PCI device. |
| 150 | # (there are no other buses), |
| 151 | # Use the device name to identify the type precisely. |
| 152 | case "${dev}" in |
| 153 | nvme*) |
| 154 | echo "NVME" |
| 155 | return |
| 156 | ;; |
| 157 | esac |
| 158 | |
Gwendal Grignou | 20f0ca7 | 2014-03-13 16:14:43 -0700 | [diff] [blame] | 159 | type_file="/sys/block/${dev}/device/type" |
| 160 | # To detect device managed by the MMC stack |
| 161 | case $(readlink -f "${type_file}") in |
| 162 | *mmc*) |
| 163 | cat "${type_file}" |
| 164 | ;; |
| 165 | *usb*) |
| 166 | # Now if it contains 'usb', it is managed through |
| 167 | # a USB controller. |
| 168 | echo "USB" |
| 169 | ;; |
| 170 | *target*) |
| 171 | # Other SCSI devices. |
| 172 | # Check if it is an ATA device. |
| 173 | vdr="$(cat "/sys/block/${dev}/device/vendor")" |
| 174 | if [ "${vdr%% *}" = "ATA" ]; then |
| 175 | echo "ATA" |
| 176 | else |
| 177 | echo "OTHER" |
| 178 | fi |
| 179 | ;; |
| 180 | *) |
| 181 | echo "OTHER" |
| 182 | esac |
| 183 | } |
| 184 | |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 185 | # ATA disk have ATA as vendor. |
| 186 | # They may not contain ata in their device path if behind a SAS |
| 187 | # controller. |
| 188 | # Exclude disks with size 0, it means they did not spin up properly. |
| 189 | list_fixed_ata_disks() { |
| 190 | local sd |
| 191 | local remo |
| 192 | local vdr |
| 193 | local size |
| 194 | |
| 195 | for sd in /sys/block/sd*; do |
| 196 | if [ ! -r "${sd}/size" ]; then |
| 197 | continue |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 198 | fi |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 199 | size=$(cat "${sd}/size") |
| 200 | remo=$(cat "${sd}/removable") |
| 201 | vdr=$(cat "${sd}/device/vendor") |
| 202 | if [ "${vdr%% *}" = "ATA" -a ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then |
| 203 | echo "${sd##*/}" |
| 204 | fi |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 205 | done |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | # We assume we only have eMMC devices, not removable MMC devices. |
| 209 | # also, do not consider special hardware partitions non the eMMC, like boot. |
| 210 | # These devices are built on top of the eMMC sysfs path: |
| 211 | # /sys/block/mmcblk0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0 |
| 212 | # /sys/block/mmcblk0boot0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot0 |
| 213 | # /sys/block/mmcblk0boot1 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot1 |
| 214 | # /sys/block/mmcblk0rpmb -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0rpmb |
| 215 | # |
| 216 | # Their device link points back to mmcblk0, not to the hardware |
| 217 | # device (mmc0:0001). Therefore there is no type in their device link. |
| 218 | # (it should be /device/device/type) |
| 219 | list_fixed_mmc_disks() { |
| 220 | local mmc |
| 221 | local type_file |
| 222 | for mmc in /sys/block/mmcblk*; do |
| 223 | type_file="${mmc}/device/type" |
| 224 | if [ -r "${type_file}" ]; then |
| 225 | if [ "$(cat "${type_file}")" = "MMC" ]; then |
| 226 | echo "${mmc##*/}" |
| 227 | fi |
| 228 | fi |
| 229 | done |
| 230 | } |
| 231 | |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 232 | # NVMe device |
| 233 | # Exclude disks with size 0, it means they did not spin up properly. |
| 234 | list_fixed_nvme_disks() { |
| 235 | local nvme remo size |
| 236 | |
| 237 | for nvme in /sys/block/nvme*; do |
| 238 | if [ ! -r "${sd}/size" ]; then |
| 239 | continue |
| 240 | fi |
| 241 | size=$(cat "${sd}/size") |
| 242 | remo=$(cat "${sd}/removable") |
| 243 | if [ ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then |
| 244 | echo "${nvme##*/}" |
| 245 | fi |
| 246 | done |
| 247 | } |
| 248 | |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 249 | # Find the drive to install based on the build write_cgpt.sh |
| 250 | # script. If not found, return "" |
| 251 | get_fixed_dst_drive() { |
| 252 | local dev |
| 253 | if [ -z "${DEFAULT_ROOTDEV}" ]; then |
| 254 | dev="" |
| 255 | else |
| 256 | # No " here, the variable may contain wildcards. |
| 257 | dev="/dev/$(basename ${DEFAULT_ROOTDEV})" |
| 258 | if [ ! -b "${dev}" ]; then |
| 259 | # The device is not found |
| 260 | dev="" |
| 261 | fi |
| 262 | fi |
| 263 | echo "${dev}" |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 264 | } |
Liam McLoughlin | 2dd21d6 | 2012-07-09 17:45:06 -0700 | [diff] [blame] | 265 | |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 266 | edit_mbr() { |
Liam McLoughlin | 2dd21d6 | 2012-07-09 17:45:06 -0700 | [diff] [blame] | 267 | locate_gpt |
Steven 'Steve' Kendall | bde39b1 | 2015-09-30 12:59:02 -0400 | [diff] [blame] | 268 | local start_esp=$(partoffset "$1" ${PARTITION_NUM_EFI_SYSTEM}) |
| 269 | local num_esp_sectors=$(partsize "$1" ${PARTITION_NUM_EFI_SYSTEM}) |
Victor Dodon | f388860 | 2016-04-21 10:36:15 -0700 | [diff] [blame] | 270 | sfdisk -X dos "${1}" <<EOF |
Liam McLoughlin | 2dd21d6 | 2012-07-09 17:45:06 -0700 | [diff] [blame] | 271 | unit: sectors |
| 272 | |
| 273 | disk1 : start= $start_esp, size= $num_esp_sectors, Id= c, bootable |
| 274 | disk2 : start= 1, size= 1, Id= ee |
| 275 | EOF |
| 276 | } |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 277 | |
| 278 | install_hybrid_mbr() { |
| 279 | # Creates a hybrid MBR which points the MBR partition 1 to GPT |
| 280 | # partition 12 (ESP). This is useful on ARM boards that boot |
J. Richard Barnette | 2db77d4 | 2015-03-19 17:19:15 -0700 | [diff] [blame] | 281 | # from MBR formatted disks only. |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 282 | # |
| 283 | # Currently, this code path is used principally to install to |
| 284 | # SD cards using chromeos-install run from inside the chroot. |
J. Richard Barnette | 2db77d4 | 2015-03-19 17:19:15 -0700 | [diff] [blame] | 285 | # In that environment, `sfdisk` can be racing with udev, leading |
| 286 | # to EBUSY when it calls BLKRRPART for the target disk. We avoid |
| 287 | # the conflict by using `udevadm settle`, so that udev goes first. |
| 288 | # cf. crbug.com/343681. |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 289 | |
| 290 | echo "Creating hybrid MBR" |
| 291 | if ! edit_mbr "${1}"; then |
J. Richard Barnette | 2db77d4 | 2015-03-19 17:19:15 -0700 | [diff] [blame] | 292 | udevadm settle |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 293 | blockdev --rereadpt "${1}" |
| 294 | fi |
| 295 | } |
Gwendal Grignou | acb0635 | 2017-02-08 20:30:47 -0800 | [diff] [blame] | 296 | |
| 297 | ext4_dir_encryption_supported() { |
| 298 | # Can be set in the ebuild. |
| 299 | local direncryption_enabled=false |
| 300 | |
| 301 | # Return true if kernel support ext4 directory encryption. |
| 302 | ${direncryption_enabled} && \ |
| 303 | ! LC_LANG=C e4crypt get_policy / | grep -qF \ |
| 304 | -e "Operation not supported" \ |
| 305 | -e "Inappropriate ioctl for device" |
| 306 | } |