BMC: Mount ubi volume on BMC reboot

- Create a static service file that will be run
  on all BMC boot to remount the bmc ubi volume.
- Read the ubinfo for all available volumes from
  obmc-flash-bmc script.
- Re-Mount the available volumes on /media/ dir
  using the volume names.

Change-Id: I8f3a0b41257927fa0e69001278cc836ba0a52dda
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager.bb b/common/recipes-phosphor/flash/phosphor-software-manager.bb
index ef8d30e..96a66e2 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -72,6 +72,7 @@
     obmc-flash-bmc-ubirw-remove.service \
     obmc-flash-bmc-ubiro-remove@.service \
     usr-local.mount \
+    obmc-flash-bmc-ubiremount.service \
     "
 
 # Name of the mtd device where the ubi volumes should be created
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
index 5c960f3..d17ee5c 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -80,6 +80,43 @@
     fi
 }
 
+remount_ubi() {
+  # Get a list of all devices formatted as UBI
+  for file in /dev/mtd*; do
+    if [[ $(hexdump -C -n 3 $file) =~ "UBI" ]]; then
+      mtd="${file: -1}"
+      if [[ $mtd =~ ^-?[0-9]+$ ]]; then
+        mtds=${mtd},${mtds}
+      fi
+    fi
+  done
+
+  IFS=',' read -r -a mtds <<< "$mtds"
+  mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
+  for mtd in ${mtds[@]}; do
+    # Re-attach mtd device to ubi if not already done
+    ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
+    # Get information on all ubi volumes
+    ubinfo=$(ubinfo -d ${mtd})
+    presentVolumes=${ubinfo##*:}
+    IFS=', ' read -r -a array <<< "$presentVolumes"
+    for element in ${array[@]}; do
+      elementProperties=$(ubinfo -d $mtd -n $element)
+      # Get ubi volume name by getting rid of additional properties
+      name=${elementProperties#*Name:}
+      name="${name%Character*}"
+      name="$(echo -e "${name}" | tr -d '[:space:]')"
+
+      if [[ ${name} == ro-* ]]; then
+        mountdir="/media/${name}"
+        mkdir -p "${mountdir}"
+        ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
+        mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
+      fi
+    done
+  done
+}
+
 ubi_setenv() {
     varName="${variable%"\x3d"*}"
     value="${variable##*"\x3d"}"
@@ -119,6 +156,9 @@
     variable="$2"
     ubi_setenv
     ;;
+  ubiremount)
+    remount_ubi
+    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service
new file mode 100644
index 0000000..36b5551
--- /dev/null
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Remount the ubi volume after a reboot
+Wants=xyz.openbmc_project.Software.BMC.Updater.service
+Before=xyz.openbmc_project.Software.BMC.Updater.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart={sbindir}/obmc-flash-bmc ubiremount
+
+[Install]
+WantedBy=multi-user.target