phosphor-software-manager: New service file to create ubi rw volume

Add service file and script to create a ubi read-write volume
to store the rwfs. The volume would be 4MB as that's the current
size of the rwfs image.

The volume is not tied to a version so that read-write data is
persisted through bmc code updates.

The mtd device location can be passed through the recipe.
Set it to the "pnor" mtd as the default. This can be changed via a
per-system bbappend to other chips if desired, such as the alternate
bmc chip.

Change-Id: Id3e40e28eaf60c9fdccdca1484d144014f2bc8df
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager.bb b/common/recipes-phosphor/flash/phosphor-software-manager.bb
index 5641cff..b7a1fd1 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -52,11 +52,28 @@
 
 FILES_${PN}-version += "${sbindir}/phosphor-version-software-manager"
 FILES_${PN}-download-mgr += "${sbindir}/phosphor-download-manager"
-FILES_${PN}-updater += "${sbindir}/phosphor-image-updater"
+FILES_${PN}-updater += " \
+    ${sbindir}/phosphor-image-updater \
+    ${sbindir}/obmc-flash-bmc \
+    "
 DBUS_SERVICE_${PN}-version += "xyz.openbmc_project.Software.Version.service"
 DBUS_SERVICE_${PN}-download-mgr += "xyz.openbmc_project.Software.Download.service"
 DBUS_SERVICE_${PN}-updater += "xyz.openbmc_project.Software.BMC.Updater.service"
 
+SYSTEMD_SERVICE_${PN}-updater += " \
+    obmc-flash-bmc-ubirw.service \
+    "
+
+# Name of the mtd device where the ubi volumes should be created
+BMC_RW_MTD ??= "pnor"
+SYSTEMD_SUBSTITUTIONS += "RW_MTD:${BMC_RW_MTD}:obmc-flash-bmc-ubirw.service"
+
+SRC_URI += "file://obmc-flash-bmc"
+do_install_append() {
+    install -d ${D}${sbindir}
+    install -m 0755 ${WORKDIR}/obmc-flash-bmc ${D}${sbindir}/obmc-flash-bmc
+}
+
 SRC_URI += "git://github.com/openbmc/phosphor-bmc-code-mgmt"
 SRCREV = "4c1aec09c68f27fcdcf0f419c9eb08a56c3ab772"
 
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
new file mode 100644
index 0000000..16c5f32
--- /dev/null
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+# Get the mtd device number (mtdX)
+findmtd() {
+  m="$(grep -xl "$1" /sys/class/mtd/*/name)"
+  m="${m%/name}"
+  m="${m##*/}"
+  echo "${m}"
+}
+
+# Get the ubi device number (ubiX_Y)
+findubi() {
+  u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
+  u="${u%/name}"
+  u="${u##*/}"
+  echo "${u}"
+}
+
+ubi_rw() {
+  rwmtd="$(findmtd "${reqmtd}")"
+  rw="${rwmtd#mtd}"
+  ubidev="/dev/ubi${rw}"
+
+  # Create a ubi volume of size 4MB, that is the current size of the rwfs image
+  vol="$(findubi "${name}")"
+  if [ -z "${vol}" ]; then
+    ubimkvol "${ubidev}" -N "${name}" -s 4MiB
+  fi
+}
+
+case "$1" in
+  ubirw)
+    reqmtd="$2"
+    name="$3"
+    ubi_rw
+    ;;
+  *)
+    echo "Invalid argument"
+    exit 1
+    ;;
+esac
+rc=$?
+if [ ${rc} -ne 0 ]; then
+  echo "$0: error ${rc}"
+  exit ${rc}
+fi
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubirw.service b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubirw.service
new file mode 100644
index 0000000..cef57e0
--- /dev/null
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubirw.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Create BMC read-write ubi volume
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart={sbindir}/obmc-flash-bmc ubirw {RW_MTD} rw