openpower-software-manager: Support migration to ubifs

A ubiattach is the first step for any ubi operations. If this
service fails, it could be that the pnor chip is formatted
differently, as with the case of flashing it with pflash.
Enhance the service to:
  Check if the chip is formatted as a ubi device.
  If it's not, format it as ubi with a new ubiformat script.
  Reattempt the attach.
This allows a pnor code update without the user having
to manually reformat the chip.

Closes openbmc/openbmc#1637

Change-Id: Id73b5eae40af68cd49e0ba0deb56efc36bd03981
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager.bb b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager.bb
index bf8a122..931a07d 100644
--- a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager.bb
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager.bb
@@ -24,6 +24,17 @@
         sdbusplus \
         "
 
+S = "${WORKDIR}"
+SRC_URI += " \
+        file://bios-ubiattach \
+        file://bios-ubiformat"
+
+do_install() {
+        install -d ${D}${sbindir}
+        install -m 0755 ${WORKDIR}/bios-ubiattach ${D}${sbindir}/bios-ubiattach
+        install -m 0755 ${WORKDIR}/bios-ubiformat ${D}${sbindir}/bios-ubiformat
+}
+
 DBUS_SERVICE_${PN} += "org.open_power.Software.Host.Updater.service"
 SYSTEMD_SERVICE_${PN} += " \
         obmc-flash-bios-ubiattach.service \
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/bios-ubiattach b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/bios-ubiattach
new file mode 100644
index 0000000..accf7d8
--- /dev/null
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/bios-ubiattach
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+# Get the pnor mtd device number
+pnor=`grep pnor /proc/mtd |cut -c 4`
+
+# Attach the pnor mtd device to ubi
+ubiattach /dev/ubi_ctrl -m $pnor
+if [ $? -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 /dev/mtd$pnor`
+  if [[ $magic == *"UBI"* ]]
+  then
+    # Device already formatted as ubi, ubiattach failed for some other reason
+    exit -1
+  else
+    # Format device as ubi
+    bios-ubiformat
+    # Retry the ubiattach
+    ubiattach /dev/ubi_ctrl -m $pnor
+  fi
+fi
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/bios-ubiformat b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/bios-ubiformat
new file mode 100644
index 0000000..d62a6b2
--- /dev/null
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/bios-ubiformat
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# Get the mtd device number for pnor
+pnor=`grep pnor /proc/mtd |cut -c 4`
+
+# Format the pnor mtd device as ubi
+echo "Starting ubiformat /dev/mtd$pnor"
+ubiformat /dev/mtd$pnor -y -q
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiattach.service b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiattach.service
index 90013ff..f90c437 100644
--- a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiattach.service
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-ubiattach.service
@@ -4,5 +4,5 @@
 [Service]
 Type=oneshot
 RemainAfterExit=yes
-ExecStart=/bin/sh -c 'grep pnor /proc/mtd |cut -c 4 |xargs {sbindir}/ubiattach /dev/ubi_ctrl -m'
+ExecStart={sbindir}/bios-ubiattach
 ExecStop=/bin/sh -c 'grep pnor /proc/mtd |cut -c 4 |xargs {sbindir}/ubidetach /dev/ubi_ctrl -m'