Revert "Revert "Add/Remove functions for disk manipulation.""

This reverts commit dcd9ab93ada0340d0eb2f4990bf6829b268a7ba0.
Identical to commit e35a7e55874ec42157cc051acd1a3d1540258663

Put functions used for listing different types of storage devices
together.
BUG=None
TEST=On link, falco, peach-pi
TEST= Build images [test, factory and vm]
TEST= Check that storage_info is working
TEST= Reinstall machines.

CQ-DEPEND=CL:193424,CL:193425,CL:193426,CL:193428,CL:193429,CL:193449,CL:193451,CL:193452,CL:193453,CL:193455,CL:193470,CL:193471,CL:193473,CL:193474,CL:193490
Change-Id: I2650603af9f107916264a0110b8b04e607cc5f7f
Reviewed-on: https://chromium-review.googlesource.com/193454
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Tested-by: Gwendal Grignou <gwendal@chromium.org>
Commit-Queue: Puthikorn Voravootivat <puthik@chromium.org>
diff --git a/chromeos-common.sh b/share/chromeos-common.sh
similarity index 68%
rename from chromeos-common.sh
rename to share/chromeos-common.sh
index 5789f83..8f58b8c 100644
--- a/chromeos-common.sh
+++ b/share/chromeos-common.sh
@@ -39,38 +39,6 @@
   fi
 }
 
-# Round a number of 512-byte sectors up to an integral number of 2Mb
-# blocks. Divisor is 2 * 1024 * 1024 / 512 == 4096.
-# Invoke as: subshell
-# Args: SECTORS
-# Return: Next largest multiple-of-8 sectors (ex: 4->8, 33->40, 32->32)
-roundup() {
-  local num=$1
-  local div=${2:-4096}
-  local rem=$(( $num % $div ))
-
-  if [ $rem -ne 0 ]; then
-    num=$(($num + $div - $rem))
-  fi
-  echo $num
-}
-
-# Truncate a number of 512-byte sectors down to an integral number of 2Mb
-# blocks. Divisor is 2 * 1024 * 1024 / 512 == 4096.
-# Invoke as: subshell
-# Args: SECTORS
-# Return: Next smallest multiple-of-8 sectors (ex: 4->0, 33->32, 32->32)
-rounddown() {
-  local num=$1
-  local div=${2:-4096}
-  local rem=$(( $num % $div ))
-
-  if [ $rem -ne 0 ]; then
-    num=$(($num - $rem))
-  fi
-  echo $num
-}
-
 # Locate the cgpt tool. It should already be installed in the build chroot,
 # but some of these functions may be invoked outside the chroot (by
 # image_to_usb or similar), so we need to find it.
@@ -153,44 +121,98 @@
   fi
 }
 
-# Find the uuid for a (disk, partnum) pair (e.g., ("/dev/sda", 3))
-part_index_to_uuid() {
-  local dev="$1"
-  local idx="$2"
-
-  sudo $GPT show -i "$idx" -u "$dev"
-}
-
+# List target devices functions.
 list_usb_disks() {
   local sd
+  local remo
+  local size
+
   for sd in /sys/block/sd*; do
+    if [ ! -r "${sd}/size" ]; then
+      continue
+    fi
+    size=$(cat "${sd}/size")
+    remo=$(cat "${sd}/removable")
     if readlink -f ${sd}/device | grep -q usb &&
-      [ "$(cat ${sd}/removable)" = 1 -a "$(cat ${sd}/size)" != 0 ]; then
-      echo ${sd##*/}
+      [ ${remo:-0} -eq 1 -a ${size:-0} -gt 0 ]; then
+      echo "${sd##*/}"
     fi
   done
 }
 
+# list mmc devices, including sd cards (for installation support candidates).
 list_mmc_disks() {
   local mmc
   for mmc in /sys/block/mmcblk*; do
-    if readlink -f ${mmc}/device | grep -q mmc; then
-      echo ${mmc##*/}
+    # We only select deivce that are 1GB or larger.
+    if [ "$(cat "${mmc}/size")" -ge 2097152 ]; then
+      echo "${mmc##*/}"
     fi
   done
 }
 
-get_disk_info() {
-  # look for a "given" file somewhere in the path upwards from the device
-  local dev_path=/sys/block/${1}/device
-  while [ -d "${dev_path}" -a "${dev_path}" != "/sys" ]; do
-    if [ -f "${dev_path}/${2}" ]; then
-      cat "${dev_path}/${2}"
-      return
+# ATA disk have ATA as vendor.
+# They may not contain ata in their device path if behind a SAS
+# controller.
+# Exclude disks with size 0, it means they did not spin up properly.
+list_fixed_ata_disks() {
+  local sd
+  local remo
+  local vdr
+  local size
+
+  for sd in /sys/block/sd*; do
+    if [ ! -r "${sd}/size" ]; then
+      continue
     fi
-    dev_path=$(readlink -f ${dev_path}/..)
+    size=$(cat "${sd}/size")
+    remo=$(cat "${sd}/removable")
+    vdr=$(cat "${sd}/device/vendor")
+    if [ "${vdr%% *}" = "ATA" -a ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then
+      echo "${sd##*/}"
+    fi
   done
-  echo '[Unknown]'
+}
+
+# We assume we only have eMMC devices, not removable MMC devices.
+# also, do not consider special hardware partitions non the eMMC, like boot.
+# These devices are built on top of the eMMC sysfs path:
+# /sys/block/mmcblk0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0
+# /sys/block/mmcblk0boot0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot0
+# /sys/block/mmcblk0boot1 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot1
+# /sys/block/mmcblk0rpmb -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0rpmb
+#
+# Their device link points back to mmcblk0, not to the hardware
+# device (mmc0:0001). Therefore there is no type in their device link.
+# (it should be /device/device/type)
+list_fixed_mmc_disks() {
+  local mmc
+  local type_file
+  for mmc in /sys/block/mmcblk*; do
+    type_file="${mmc}/device/type"
+    if [ -r "${type_file}" ]; then
+      if [ "$(cat "${type_file}")" = "MMC" ]; then
+        echo "${mmc##*/}"
+      fi
+    fi
+  done
+}
+
+# Find the drive to install based on the build write_cgpt.sh
+# script. If not found, return ""
+get_fixed_dst_drive() {
+  local dev
+  if [ -z "${DEFAULT_ROOTDEV}" ]; then
+    dev=""
+  else
+    # No " here, the variable may contain wildcards.
+    dev="/dev/$(basename ${DEFAULT_ROOTDEV})"
+    if [ ! -b "${dev}" ]; then
+      # The device is not found
+      dev=""
+    fi
+  fi
+  echo "${dev}"
 }
 
 legacy_offset_size_export() {