fstab: require service file to mount /var - fixes BMC factory reset

This commit adds a service file requirement to fstab for mounting
the ubi0:rwfs volume to /var. The required file is
obmc-flash-bmc-reset.service, which, when called, will perform a
BMC factory reset if the corresponding environment variable is set.

This functionality was moved out of obmc-flash-bmc-ubirw.service because
the factory reset is not the only procedure that calls that service.
Moving this function to a new service avoids unexpected behaviors.

Resolves openbmc/openbmc#2326

Change-Id: If2a9c650f315cff23040194ec87f16c05622c85e
Signed-off-by: Michael Tritz <mtritz@us.ibm.com>
diff --git a/meta-phosphor/common/recipes-core/base-files/base-files/fstab b/meta-phosphor/common/recipes-core/base-files/base-files/fstab
index ba65689..7d6ac6c 100644
--- a/meta-phosphor/common/recipes-core/base-files/base-files/fstab
+++ b/meta-phosphor/common/recipes-core/base-files/base-files/fstab
@@ -4,7 +4,7 @@
 proc                 /proc                proc       defaults              0  0
 devpts               /dev/pts             devpts     mode=0620,gid=5       0  0
 tmpfs                /run                 tmpfs      mode=0755,nodev,nosuid,strictatime 0  0
-ubi0:rwfs            /var                 ubifs      defaults              0  0
+ubi0:rwfs            /var                 ubifs      defaults,x-systemd.requires=obmc-flash-bmc-reset.service 0  0
 overlay              /etc                 overlay    defaults,x-systemd.requires=prepare-overlay.service,lowerdir=/etc,upperdir=/var/persist/etc,workdir=/var/persist/etc-work 0  0
 tmpfs                /var/volatile        tmpfs      defaults              0  0
 tmpfs                /media               tmpfs      defaults              0  0
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 052d913..14dd7d9 100644
--- a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -78,6 +78,7 @@
     reboot-guard-enable.service \
     reboot-guard-disable.service \
     obmc-flash-bmc-cleanup.service \
+    obmc-flash-bmc-reset.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 2025a05..1a20660 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
@@ -100,11 +100,6 @@
   rw="${rwmtd#mtd}"
   ubidev="/dev/ubi${rw}"
 
-  if [ "$(fw_printenv rwreset 2>/dev/null)" == "rwreset=true" ]; then
-    ubi_remove "${name}"
-    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
@@ -328,6 +323,19 @@
   fi
 }
 
+factory_reset() {
+    # A lock file for printenv exists on /var, which isn't mounted when this
+    # function is called. We have to read the rwreset variable out of file.
+    ubootmtd="$(findmtd "u-boot-env")"
+    grep -q -x "rwreset=true" /dev/$ubootmtd
+    if [ "$?" = "0" ]; then
+      ubi_remove "rwfs"
+      reqmtd="bmc"
+      name="rwfs"
+      ubi_rw
+    fi
+}
+
 case "$1" in
   mtduboot)
     reqmtd="$2"
@@ -388,6 +396,9 @@
   rebootguarddisable)
     rebootguarddisable
     ;;
+  reset)
+    factory_reset
+    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-reset.service b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-reset.service
new file mode 100644
index 0000000..38dc60e
--- /dev/null
+++ b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-reset.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Factory reset BMC if reset enabled
+DefaultDependencies=no
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart=/usr/bin/env obmc-flash-bmc reset
+SyslogIdentifier=obmc-flash-bmc
diff --git a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service
index ff571d7..388bb1a 100644
--- a/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service
+++ b/meta-phosphor/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiremount.service
@@ -2,6 +2,8 @@
 Description=Remount the ubi volume after a reboot
 Wants=xyz.openbmc_project.Software.BMC.Updater.service
 Before=xyz.openbmc_project.Software.BMC.Updater.service
+Wants=obmc-flash-bmc-setenv@rwreset.service
+After=obmc-flash-bmc-setenv@rwreset.service
 
 [Service]
 Type=oneshot