openpower-software-manager:re-attach pnor mtd device to ubi

- Removed the ubiattach dependency from ubiremount.service,
  since it was attempting to reformat the pnor chip on every
  BMC reboot.
- Attempt to reattach the pnor mtd device to ubi only if it
  is formatted as ubi.

This approach will help preserve the systems that are not
formatted as ubi.

Resolves openbmc/openbmc#2061

Change-Id: I83698c05fd58a0f01c597583109d4009b02239a0
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
index dffd298..0896569 100644
--- a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
@@ -22,7 +22,7 @@
   return $?
 }
 
-# Attach the pnor mtd device to ubi
+# Attach the pnor mtd device to ubi.
 attach_ubi() {
   pnormtd="$(findmtd pnor)"
   pnor="${pnormtd#mtd}"
@@ -131,6 +131,21 @@
 remount_ubi() {
   pnormtd="$(findmtd pnor)"
   pnor="${pnormtd#mtd}"
+  pnordev="/dev/mtd${pnor}"
+
+  # Re-Attach the pnor mtd device to ubi
+  ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
+  rc=$?
+  if [ ${rc} -ne 0 ]; then
+    # Check the pnor mtd device is formatted as ubi by reading
+    # the first 3 byes, which should be the ascii chars 'UBI'
+    magic="$(hexdump -C -n 3 ${pnordev})"
+    if [[ ! "${magic}" =~ "UBI" ]]; then
+      # Device not formatted as ubi.
+      return
+    fi
+  fi
+
   # Get information on all ubi volumes
   ubinfo=$(ubinfo -d ${pnor})
   presentVolumes=${ubinfo##*:}