BMC Code Mgmt: Service files for factory reset

This commit extends the obmc-flash-bmc script and includes a pair of
service files.

A U-Boot environment variable will be set to indicate that the BMC
read-write volume needs to be reset. When this variable is present, the
read-write volume will be removed and recreated.

Change-Id: I6e39727cd3898d66bdbc63e9071f3a8b20a77673
Signed-off-by: Michael Tritz <mtritz@us.ibm.com>
diff --git a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager.bb b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager.bb
index acdd9a3..f6f47b8 100644
--- a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -63,6 +63,7 @@
 SYSTEMD_SERVICE_${PN}-updater += " \
     obmc-flash-bmc-ubirw.service \
     obmc-flash-bmc-ubiro@.service \
+    obmc-flash-bmc-setenv@.service \
     "
 
 # Name of the mtd device where the ubi volumes should be created
diff --git a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
index a451503..5c960f3 100644
--- a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
+++ b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -21,6 +21,11 @@
   rw="${rwmtd#mtd}"
   ubidev="/dev/ubi${rw}"
 
+  if [ "$(fw_printenv rwreset)" == "rwreset=true" ]; then
+    ubi_remove
+    fw_setenv rwreset
+  fi
+
   # Create a ubi volume of size 4MB, that is the current size of the rwfs image
   vol="$(findubi "${name}")"
   if [ -z "${vol}" ]; then
@@ -59,6 +64,28 @@
   ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
 }
 
+ubi_remove() {
+    vol="$(findubi "${name}")"
+
+    if [ ! -z "$vol" ]; then
+        vol="${vol%_*}"
+
+        if grep -q "${vol}:$name" /proc/mounts; then
+            mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2)
+            umount "$mountdir"
+            rm -r "$mountdir"
+        fi
+
+        ubirmvol "/dev/${vol}" -N "$name"
+    fi
+}
+
+ubi_setenv() {
+    varName="${variable%"\x3d"*}"
+    value="${variable##*"\x3d"}"
+    fw_setenv "$varName" "$value"
+}
+
 case "$1" in
   ubirw)
     reqmtd="$2"
@@ -84,6 +111,14 @@
     ubi_ro
     ubi_updatevol
     ;;
+  ubiremove)
+    name="$2"
+    ubi_remove
+    ;;
+  ubisetenv)
+    variable="$2"
+    ubi_setenv
+    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-setenv@.service b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-setenv@.service
new file mode 100644
index 0000000..5c0650b
--- /dev/null
+++ b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-setenv@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Set U-Boot environment variable
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart={sbindir}/obmc-flash-bmc ubisetenv %i