obmc-flash-bmc: Add ExecStartPre and Post to update environment variables

Backup the environment variables before performing an update.
Update the environment variables that point to the new volumes only
if all the ExecStart commands are successful. Also ensure that the
volumes exist so that the env variables point to an existing volume.

Change-Id: I4f63076730f0843677b6ac8bf166c02ae36eaf2e
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
index 57b557f..d94f472 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -133,10 +133,6 @@
       return 1
     fi
   fi
-  copy_env_var_to_alt ubiblock
-  ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
-  copy_env_var_to_alt root
-  ubi_setenv "root=${block}"
 }
 
 ubi_updatevol() {
@@ -148,10 +144,6 @@
     echo "Unable to update volume ${name}!"
     return 1
   fi
-  if [[ "${name}" =~ "kernel" ]]; then
-    copy_env_var_to_alt kernelname
-    ubi_setenv "kernelname=${name}"
-  fi
 }
 
 ubi_remove() {
@@ -226,6 +218,27 @@
   flashcp -v ${img} /dev/${flashmtd}
 }
 
+backup_env_vars() {
+  copy_env_var_to_alt kernelname
+  copy_env_var_to_alt ubiblock
+  copy_env_var_to_alt root
+}
+
+update_env_vars() {
+  vol="$(findubi rofs-"${version}")"
+  if [ -z "${vol}" ]; then
+    return 1
+  fi
+  ubidevid="${vol#ubi}"
+  block="/dev/ubiblock${ubidevid}"
+  if [ ! -e "${block}" ]; then
+    return 1
+  fi
+  ubi_setenv "kernelname=kernel-${version}"
+  ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
+  ubi_setenv "root=${block}"
+}
+
 case "$1" in
   mtduboot)
     reqmtd="$2"
@@ -269,6 +282,13 @@
   ubiremount)
     remount_ubi
     ;;
+  preupdate)
+    backup_env_vars
+    ;;
+  postupdate)
+    version="$2"
+    update_env_vars
+    ;;
   *)
     echo "Invalid argument"
     exit 1