Move updatesymlinks to new vpnor feature

The updatesymlinks functionality is to support virtual pnor and it
does not depend on the filesystem type. Move it to a new vpnor
feature so that users can make use of this feature without being
tied to UBI, for example on a eMMC that has a combined BMC+PNOR
image.

Tested: Verified that the witherspoon image contained the new
        script and service file and that it powered on to the host.

Change-Id: Ic5e51dfde81718e5e285f010be67afbd58eac2e1
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/vpnor/Makefile.am.include b/vpnor/Makefile.am.include
new file mode 100644
index 0000000..49d9c1a
--- /dev/null
+++ b/vpnor/Makefile.am.include
@@ -0,0 +1,7 @@
+dist_bin_SCRIPTS += \
+	%reldir%/obmc-vpnor-util
+
+if HAVE_SYSTEMD
+systemdsystemunit_DATA += \
+	%reldir%/obmc-vpnor-updatesymlinks.service
+endif
diff --git a/vpnor/obmc-vpnor-updatesymlinks.service b/vpnor/obmc-vpnor-updatesymlinks.service
new file mode 100644
index 0000000..8184f51
--- /dev/null
+++ b/vpnor/obmc-vpnor-updatesymlinks.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=Updates symlinks for active PNOR version
+Before=mboxd.service
+Before=mboxd-reload@0.service
+After=org.open_power.Software.Host.Updater.service
+After=op-reset-chassis-running@0.service
+ConditionPathExists=!/run/openbmc/chassis@0-on
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart=/usr/bin/obmc-vpnor-util updatesymlinks
+
+[Install]
+WantedBy=mboxd.service mboxd-reload@.service
diff --git a/vpnor/obmc-vpnor-util b/vpnor/obmc-vpnor-util
new file mode 100644
index 0000000..25e6726
--- /dev/null
+++ b/vpnor/obmc-vpnor-util
@@ -0,0 +1,75 @@
+#!/bin/sh
+
+update_symlinks() {
+  PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
+  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"
+  PNOR_RW_PREFIX="/media/pnor-rw-"
+  PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
+  PNOR_PRSV="/media/pnor-prsv"
+  PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
+  PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
+
+  # Get a list of all active PNOR versions
+  data="$(ls -d ${PNOR_RO_PREFIX}*)"
+  IFS=$'\n'  array=(${data})
+
+  currentVersion=""
+  lowestPriority=255
+  for element in ${array[@]}; do
+    #Remove the PNOR_RO_PREFIX from the path to get version ID.
+    versionId="${element#${PNOR_RO_PREFIX}}"
+
+    # Get the priority of active versions from persistence files.
+    if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
+      data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
+      priority="${data: -1}"
+      if [[ priority -le lowestPriority  ]]; then
+        lowestPriority=${priority}
+        currentVersion=${versionId}
+      fi
+    fi
+  done
+
+  # Return if no active version found
+  if [ -z $currentVersion ]; then
+    return 0;
+  fi
+
+  if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
+        mkdir -p "${PNOR_ACTIVE_PATH}"
+  fi
+
+  # 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
+    rm -f ${PNOR_RO_ACTIVE_PATH}
+    rm -rf ${PNOR_PATCH_LOCATION}*
+    ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
+  fi
+
+  if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion}  ]]; then
+    rm -f ${PNOR_RW_ACTIVE_PATH}
+    ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
+  fi
+
+  if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH}  ]]; then
+    ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
+  fi
+}
+
+case "$1" in
+  updatesymlinks)
+    update_symlinks
+    ;;
+  *)
+    echo "Invalid argument"
+    exit 1
+    ;;
+esac
+rc=$?
+if [ ${rc} -ne 0 ]; then
+  echo "$0: error ${rc}"
+  exit ${rc}
+fi