1#!/bin/sh
2
3update_symlinks() {
4  PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
5  PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
6  PNOR_RO_PREFIX="/media/pnor-ro-"
7  PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
8  PNOR_RW_PREFIX="/media/pnor-rw-"
9  PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
10  PNOR_PRSV="/media/pnor-prsv"
11  PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
12  PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
13
14  # Get a list of all active PNOR versions
15  data="$(ls -d ${PNOR_RO_PREFIX}*)"
16  IFS=$'\n'  array=(${data})
17
18  currentVersion=""
19  lowestPriority=255
20  for element in ${array[@]}; do
21    #Remove the PNOR_RO_PREFIX from the path to get version ID.
22    versionId="${element#${PNOR_RO_PREFIX}}"
23
24    # Get the priority of active versions from persistence files.
25    if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
26      data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
27      priority="${data: -1}"
28      if [[ priority -le lowestPriority  ]]; then
29        lowestPriority=${priority}
30        currentVersion=${versionId}
31      fi
32    fi
33  done
34
35  # Return if no active version found
36  if [ -z $currentVersion ]; then
37    return 0;
38  fi
39
40  if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
41        mkdir -p "${PNOR_ACTIVE_PATH}"
42  fi
43
44  # If the RW or RO active links doesn't point to the version with
45  # lowest priority, then remove the symlink and create new ones.
46  if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion}  ]]; then
47    rm -f ${PNOR_RO_ACTIVE_PATH}
48    rm -rf ${PNOR_PATCH_LOCATION}*
49    ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
50  fi
51
52  if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion}  ]]; then
53    rm -f ${PNOR_RW_ACTIVE_PATH}
54    ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
55  fi
56
57  if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH}  ]]; then
58    ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
59  fi
60}
61
62case "$1" in
63  updatesymlinks)
64    update_symlinks
65    ;;
66  *)
67    echo "Invalid argument"
68    exit 1
69    ;;
70esac
71rc=$?
72if [ ${rc} -ne 0 ]; then
73  echo "$0: error ${rc}"
74  exit ${rc}
75fi
76