mmc: Add read-only directory to support mboxd

The mboxd implementation assumes that there is a read-only and
read-write directory. During a write request, mboxd copies the
contents of the read-only to the read-write directory. To continue
to support mboxd, mount a read-only directory of the hostfw image
for mboxd to use. Otherwise the write command fails (can't copy
the file to itself) and mboxd exits.

Tested: Verified mboxd didn't fail and system powered on.

Change-Id: Idcb8ff0c5eee0179d5d70e60e5e01c7d0f76b880
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/mmc/obmc-flash-bios b/mmc/obmc-flash-bios
index 0a496ec..4cc6bb5 100644
--- a/mmc/obmc-flash-bios
+++ b/mmc/obmc-flash-bios
@@ -2,9 +2,13 @@
 
 mmc_init() {
   base_dir="/media/hostfw"
+  ro_dir="${base_dir}/running-ro"
   running_dir="${base_dir}/running"
   prsv_dir="${base_dir}/prsv"
 
+  if [ ! -d "${ro_dir}" ]; then
+    mkdir -p "${ro_dir}"
+  fi
   if [ ! -d "${running_dir}" ]; then
     mkdir -p ${running_dir}
   fi
@@ -12,8 +16,12 @@
     mkdir -p "${prsv_dir}"
   fi
 
-  # Determine if the running dir contains the running version
+  # Mount the image that corresponds to the boot label as read-only to be used
+  # to populate the running directory.
   boot_label="$(fw_printenv -n bootside)"
+  mount ${base_dir}/hostfw-${boot_label} ${ro_dir} -o ro
+
+  # Determine if the running dir contains the running version
   running_label=""
   running_label_file="${running_dir}/partlabel"
   if [ -f "${running_label_file}" ]; then
@@ -34,16 +42,11 @@
     fi
 
     # Copy contents of running image to running dir
-    tmp_dir="/tmp/hostfw"
-    mkdir -p "${tmp_dir}"
-    mount ${base_dir}/hostfw-${boot_label} ${tmp_dir} -o ro
     rm -f ${running_dir}/*
-    cp -p ${tmp_dir}/* ${running_dir}/
-    umount ${tmp_dir}
-    rm -rf ${tmp_dir}
+    cp -p ${ro_dir}/* ${running_dir}/
 
-    # Restore the preserved partitions
-    cp -p ${prsv_dir}/* ${running_dir}/ || true
+    # Restore the preserved partitions. Ignore error, there may be none.
+    cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
     rm -f ${prsv_dir}/*
 
     # Save the label
diff --git a/mmc/obmc-flash-bios-init.service b/mmc/obmc-flash-bios-init.service
index 191a859..9fe2f7f 100644
--- a/mmc/obmc-flash-bios-init.service
+++ b/mmc/obmc-flash-bios-init.service
@@ -1,5 +1,6 @@
 [Unit]
 Description=Setup Host FW directories
+Before=mboxd.service
 Before=pldmd.service
 After=xyz.openbmc_project.Software.BMC.Updater.service
 
diff --git a/mmc/obmc-flash-bios-patch.service b/mmc/obmc-flash-bios-patch.service
index 226c019..2b3a941 100644
--- a/mmc/obmc-flash-bios-patch.service
+++ b/mmc/obmc-flash-bios-patch.service
@@ -1,5 +1,6 @@
 [Unit]
 Description=Create patch directory for Host FW
+Before=mboxd.service
 Before=pldmd.service
 Wants=usr-local.mount
 After=usr-local.mount
diff --git a/vpnor/obmc-vpnor-updatesymlinks.service b/vpnor/obmc-vpnor-updatesymlinks.service
index 31ca628..7a4ed54 100644
--- a/vpnor/obmc-vpnor-updatesymlinks.service
+++ b/vpnor/obmc-vpnor-updatesymlinks.service
@@ -13,4 +13,5 @@
 ExecStart=/usr/bin/obmc-vpnor-util updatesymlinks
 
 [Install]
+WantedBy=pldmd.service
 WantedBy=mboxd.service mboxd-reload@.service
diff --git a/vpnor/obmc-vpnor-util b/vpnor/obmc-vpnor-util
index b529f47..9043926 100644
--- a/vpnor/obmc-vpnor-util
+++ b/vpnor/obmc-vpnor-util
@@ -47,6 +47,7 @@
   PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
   HOSTFW_PATCH_LOCATION="/usr/local/share/hostfw/"
   MMC_BASE_PATH="/media/hostfw"
+  MMC_RO_PATH="${MMC_BASE_PATH}/running-ro"
   MMC_RUNNING_PATH="${MMC_BASE_PATH}/running"
   MMC_ALTERNATE_PATH="${MMC_BASE_PATH}/alternate"
   HOSTFW_RUNNING_PATH="${HOSTFW_ACTIVE_PATH}/running"
@@ -80,13 +81,14 @@
       prsvTarget="${PNOR_PRSV}"
   elif [ -d "${MMC_BASE_PATH}" ]; then
       # Use eMMC
-      roTarget="${MMC_RUNNING_PATH}"
+      roTarget="${MMC_RO_PATH}"
       rwTarget="${MMC_RUNNING_PATH}"
       prsvTarget="${MMC_RUNNING_PATH}"
 
       if [ ! -d "${HOSTFW_ACTIVE_PATH}" ]; then
           mkdir -p "${HOSTFW_ACTIVE_PATH}"
       fi
+      # Symlinks used by PLDM
       if [[ $(readlink -f "${HOSTFW_RUNNING_PATH}") != ${MMC_RUNNING_PATH} ]]; then
           rm -f ${HOSTFW_RUNNING_PATH}
           ln -sfv ${MMC_RUNNING_PATH} ${HOSTFW_RUNNING_PATH}