vpnor: Add hostfw symlinks

On eMMC, in addition to the PNOR files, there are hostfw
files. Create symlinks for the 2 versions on flash, so that
pldm (which will replace mboxd) can read/write.

Tested: Both pnor and hostfw links are created:
root@rainier:~# ls -l /var/lib/phosphor-software-manager/*
/var/lib/phosphor-software-manager/hostfw:
lrwxrwxrwx    1 root     root            23 Jul 16 20:37 alternate -> /media/hostfw/alternate
lrwxrwxrwx    1 root     root            21 Jul 16 20:37 running -> /media/hostfw/running

/var/lib/phosphor-software-manager/pnor:
lrwxrwxrwx    1 root     root            21 Jul 16 20:37 prsv -> /media/hostfw/running
lrwxrwxrwx    1 root     root            21 Jul 16 20:37 ro -> /media/hostfw/running
lrwxrwxrwx    1 root     root            21 Jul 16 20:37 rw -> /media/hostfw/running

Change-Id: I2fe726641611112bc7a447eed5c2d64db268ce8e
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/vpnor/obmc-vpnor-updatesymlinks.service b/vpnor/obmc-vpnor-updatesymlinks.service
index 8184f51..31ca628 100644
--- a/vpnor/obmc-vpnor-updatesymlinks.service
+++ b/vpnor/obmc-vpnor-updatesymlinks.service
@@ -1,5 +1,6 @@
 [Unit]
 Description=Updates symlinks for active PNOR version
+Before=pldmd.service
 Before=mboxd.service
 Before=mboxd-reload@0.service
 After=org.open_power.Software.Host.Updater.service
diff --git a/vpnor/obmc-vpnor-util b/vpnor/obmc-vpnor-util
index 2abf8fb..b529f47 100644
--- a/vpnor/obmc-vpnor-util
+++ b/vpnor/obmc-vpnor-util
@@ -36,6 +36,7 @@
 
 update_symlinks() {
   PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
+  HOSTFW_ACTIVE_PATH="/var/lib/phosphor-software-manager/hostfw/"
   PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
   PNOR_RO_PREFIX="/media/pnor-ro-"
   PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
@@ -44,9 +45,15 @@
   PNOR_PRSV="/media/pnor-prsv"
   PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
   PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
+  HOSTFW_PATCH_LOCATION="/usr/local/share/hostfw/"
+  MMC_BASE_PATH="/media/hostfw"
+  MMC_RUNNING_PATH="${MMC_BASE_PATH}/running"
+  MMC_ALTERNATE_PATH="${MMC_BASE_PATH}/alternate"
+  HOSTFW_RUNNING_PATH="${HOSTFW_ACTIVE_PATH}/running"
+  HOSTFW_ALTERNATE_PATH="${HOSTFW_ACTIVE_PATH}/alternate"
 
   # Get a list of all active PNOR versions
-  data="$(ls -d ${PNOR_RO_PREFIX}*)"
+  data="$(ls -d ${PNOR_RO_PREFIX}* 2>/dev/null)"
   IFS=$'\n'  array=(${data})
 
   currentVersion=""
@@ -66,9 +73,28 @@
     fi
   done
 
-  # Return if no active version found
-  if [ -z $currentVersion ]; then
-    return 0;
+  if [ ! -z $currentVersion ]; then
+      # Use active version
+      roTarget="${PNOR_RO_PREFIX}${currentVersion}"
+      rwTarget="${PNOR_RW_PREFIX}${currentVersion}"
+      prsvTarget="${PNOR_PRSV}"
+  elif [ -d "${MMC_BASE_PATH}" ]; then
+      # Use eMMC
+      roTarget="${MMC_RUNNING_PATH}"
+      rwTarget="${MMC_RUNNING_PATH}"
+      prsvTarget="${MMC_RUNNING_PATH}"
+
+      if [ ! -d "${HOSTFW_ACTIVE_PATH}" ]; then
+          mkdir -p "${HOSTFW_ACTIVE_PATH}"
+      fi
+      if [[ $(readlink -f "${HOSTFW_RUNNING_PATH}") != ${MMC_RUNNING_PATH} ]]; then
+          rm -f ${HOSTFW_RUNNING_PATH}
+          ln -sfv ${MMC_RUNNING_PATH} ${HOSTFW_RUNNING_PATH}
+      fi
+      if [[ $(readlink -f "${HOSTFW_ALTERNATE_PATH}") != ${MMC_ALTERNATE_PATH} ]]; then
+          rm -f ${HOSTFW_ALTERNATE_PATH}
+          ln -sfv ${MMC_ALTERNATE_PATH} ${HOSTFW_ALTERNATE_PATH}
+      fi
   fi
 
   if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
@@ -77,19 +103,20 @@
 
   # If the RW or RO active links doesn't point to the version with
   # lowest priority, then remove the symlink and create new ones.
-  if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion}  ]]; then
+  if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${roTarget}  ]]; then
     rm -f ${PNOR_RO_ACTIVE_PATH}
     rm -rf ${PNOR_PATCH_LOCATION}*
-    ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
+    ln -sfv ${roTarget} ${PNOR_RO_ACTIVE_PATH}
   fi
 
-  if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion}  ]]; then
+  if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${rwTarget}  ]]; then
     rm -f ${PNOR_RW_ACTIVE_PATH}
-    ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
+    ln -sfv ${rwTarget} ${PNOR_RW_ACTIVE_PATH}
   fi
 
-  if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH}  ]]; then
-    ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
+  if [[ $(readlink -f "${PNOR_PRSV_ACTIVE_PATH}") != ${prsvTarget} ]]; then
+    rm -f ${PNOR_PRSV_ACTIVE_PATH}
+    ln -sfv ${prsvTarget} ${PNOR_PRSV_ACTIVE_PATH}
   fi
 }