mmc-init: Add factory reset based on rwreset

To enable factory resets, the mmc-init.sh script checks on reboot if
the environment variable rwreset is set to "true". If it is, the rw
parts of the file system that users may have modified are reformatted
to remove all user changes and rwreset is set to "false".

Tested: Add a file to /var and use fw_setenv to set rwreset to true.
Reboot the machine and verify that the added file is gone and rwreset
is set to false.

Signed-off-by: Isaac Kurth <isaac.kurth@ibm.com>
Change-Id: I2d8b4f4eaf8ff6df092893760aaae9db2ce3917b
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init.bb b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init.bb
index 0c7a88c..aa55d1b 100644
--- a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init.bb
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init.bb
@@ -8,8 +8,10 @@
 inherit allarch
 
 RDEPENDS_${PN} += " \
+    ${@d.getVar('PREFERRED_PROVIDER_u-boot-fw-utils', True) or 'u-boot-fw-utils'} \
     ${VIRTUAL-RUNTIME_base-utils} \
     e2fsprogs-e2fsck \
+    e2fsprogs-mke2fs \
     gptfdisk \
     parted \
     udev \
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
index 3fad5e6..575b760 100644
--- a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
@@ -49,6 +49,18 @@
 fi
 
 rwfsdev="/dev/disk/by-partlabel/rwfs"
+mkdir -p /var/lock
+if test $(fw_printenv -n rwreset) = "true"; then
+    echo "Factory reset requested."
+    if ! mkfs.ext4 -F "${rwfsdev}"; then
+        echo "Reformat for factory reset failed."
+        /bin/sh
+    else
+        fw_setenv rwreset
+        echo "Formatting of rwfs is complete."
+    fi
+fi
+
 fsck.ext4 -p "${rwfsdev}"
 if ! mount "${rwfsdev}" $rodir/var -t ext4 -o rw; then
     /bin/sh