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/Makefile.am b/Makefile.am
index 11e5a96..fc31b9f 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,6 +2,8 @@
 
 ACLOCAL_AMFLAGS = -Im4
 
+dist_bin_SCRIPTS =
+
 bin_PROGRAMS = \
 	openpower-update-manager \
 	openpower-pnor-msl
@@ -34,6 +36,9 @@
 	image_verify.cpp
 endif
 
+if VIRTUAL_PNOR_ENABLED
+include vpnor/Makefile.am.include
+endif
 
 openpower_pnor_msl_SOURCES = \
 	msl_verify.cpp \
diff --git a/configure.ac b/configure.ac
index b43e50d..07cc7de 100755
--- a/configure.ac
+++ b/configure.ac
@@ -116,6 +116,12 @@
     [AC_DEFINE([WANT_SIGNATURE_VERIFY],[],[Enable image signature validation.])])
 AM_CONDITIONAL([WANT_SIGNATURE_VERIFY_BUILD], [test "x$enable_verify_pnor_signature" == "xyes"])
 
+# setup virtual pnor support
+AC_ARG_ENABLE([virtual-pnor],
+    AS_HELP_STRING([--enable-virtual-pnor], [Enable support for virtual pnor])
+)
+AM_CONDITIONAL([VIRTUAL_PNOR_ENABLED], [test "x$enable_virtual_pnor" == "xyes"])
+
 AC_DEFINE(CHASSIS_STATE_PATH, "/xyz/openbmc_project/state/chassis0",
     [The chassis state path.])
 AC_DEFINE(CHASSIS_STATE_OBJ, "xyz.openbmc_project.State.Chassis",
diff --git a/ubi/Makefile.am.include b/ubi/Makefile.am.include
index 98ea301..2e5b6e5 100644
--- a/ubi/Makefile.am.include
+++ b/ubi/Makefile.am.include
@@ -4,7 +4,7 @@
 	%reldir%/serialize.cpp \
 	%reldir%/watch.cpp
 
-dist_bin_SCRIPTS = \
+dist_bin_SCRIPTS += \
 	%reldir%/obmc-flash-bios
 
 if HAVE_SYSTEMD
@@ -17,6 +17,5 @@
 	%reldir%/obmc-flash-bios-ubipatch.service \
 	%reldir%/obmc-flash-bios-ubiremount.service \
 	%reldir%/obmc-flash-bios-ubiumount-ro@.service \
-	%reldir%/obmc-flash-bios-ubiumount-rw@.service \
-	%reldir%/obmc-flash-bios-updatesymlinks.service
+	%reldir%/obmc-flash-bios-ubiumount-rw@.service
 endif
diff --git a/ubi/obmc-flash-bios b/ubi/obmc-flash-bios
index ff88185..b15ddc6 100644
--- a/ubi/obmc-flash-bios
+++ b/ubi/obmc-flash-bios
@@ -207,65 +207,6 @@
   done
 }
 
-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
-}
-
 ubi_cleanup() {
     # When ubi_cleanup is run, it expects one or no active version.
     activeVersion=$(busctl --list --no-pager tree \
@@ -341,9 +282,6 @@
   ubiremount)
     remount_ubi
     ;;
-  updatesymlinks)
-    update_symlinks
-    ;;
   ubicleanup)
     ubi_cleanup
     ;;
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/ubi/obmc-flash-bios-updatesymlinks.service b/vpnor/obmc-vpnor-updatesymlinks.service
similarity index 86%
rename from ubi/obmc-flash-bios-updatesymlinks.service
rename to vpnor/obmc-vpnor-updatesymlinks.service
index d486088..8184f51 100644
--- a/ubi/obmc-flash-bios-updatesymlinks.service
+++ b/vpnor/obmc-vpnor-updatesymlinks.service
@@ -9,7 +9,7 @@
 [Service]
 Type=oneshot
 RemainAfterExit=no
-ExecStart=/usr/bin/obmc-flash-bios updatesymlinks
+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