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##*:}
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiremount.service b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiremount.service
index fd839f3..0b5a077 100644
--- a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiremount.service
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiremount.service
@@ -1,7 +1,5 @@
 [Unit]
 Description=Remount the squashfs and ubi volumes after a reboot
-Requires=obmc-flash-bios-ubiattach.service
-After=obmc-flash-bios-ubiattach.service
 Wants=org.open_power.Software.Host.Updater.service
 Before=org.open_power.Software.Host.Updater.service