blob: 380f974e5c67af8075c2f43127a4a8dadb50e318 [file] [log] [blame]
Adriana Kobylakfc886ca2021-04-26 16:07:21 +00001#!/bin/bash
Adriana Kobylakf3dfe652019-10-08 11:35:55 -05002
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -05003clear_volatile() {
4 service=$(mapper get-service /org/open_power/control/volatile)
Adriana Kobylakfc886ca2021-04-26 16:07:21 +00005 clearVolatileEnabled=$(busctl get-property "$service" /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled)
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -05006 if [[ "$clearVolatileEnabled" != "b true" ]]; then
7 return 0
8 fi
9
10 PNOR_TOC_FILE="pnor.toc"
11 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro/"
12 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw/"
13 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv/"
14
15 # toc partition string format:
16 # partition27=HB_VOLATILE,0x02ba9000,0x02bae000,00,ECC,VOLATILE,READWRITE
17 tocFilePath="${PNOR_RO_ACTIVE_PATH}${PNOR_TOC_FILE}"
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000018 mapfile -t volatiles < <(grep VOLATILE "${tocFilePath}" | grep -Eo '^partition([0-9]+)=([A-Za-z0-9_]+)')
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -050019 for (( index=0; index<${#volatiles[@]}; index++ )); do
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000020 volatileName="$(echo "${volatiles[${index}]}" | awk -F '=' '{print $2}')"
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -050021
22 rwVolatile="${PNOR_RW_ACTIVE_PATH}${volatileName}"
23 if [ -f "${rwVolatile}" ]; then
24 echo "Clear $rwVolatile"
25 rm "${rwVolatile}"
26 fi
27 prsvVolatile="${PNOR_PRSV_ACTIVE_PATH}${volatileName}"
28 if [ -f "${prsvVolatile}" ]; then
29 echo "Clear $prsvVolatile"
30 rm "${prsvVolatile}"
31 fi
32 done
33 # Always reset the sensor after clearing
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000034 busctl set-property "$service" /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled b false
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -050035}
36
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050037update_symlinks() {
38 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050039 HOSTFW_ACTIVE_PATH="/var/lib/phosphor-software-manager/hostfw/"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050040 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
41 PNOR_RO_PREFIX="/media/pnor-ro-"
42 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
43 PNOR_RW_PREFIX="/media/pnor-rw-"
44 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
45 PNOR_PRSV="/media/pnor-prsv"
46 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
47 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050048 MMC_BASE_PATH="/media/hostfw"
Adriana Kobylak98f22322020-07-20 10:39:41 -050049 MMC_RO_PATH="${MMC_BASE_PATH}/running-ro"
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050050 MMC_RUNNING_PATH="${MMC_BASE_PATH}/running"
51 MMC_ALTERNATE_PATH="${MMC_BASE_PATH}/alternate"
Adriana Kobylak87372ae2020-09-01 11:30:59 -050052 MMC_STAGING_PATH="${MMC_BASE_PATH}/staging"
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050053 HOSTFW_RUNNING_PATH="${HOSTFW_ACTIVE_PATH}/running"
54 HOSTFW_ALTERNATE_PATH="${HOSTFW_ACTIVE_PATH}/alternate"
Adriana Kobylak87372ae2020-09-01 11:30:59 -050055 HOSTFW_STAGING_PATH="${HOSTFW_ACTIVE_PATH}/staging"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050056
57 # Get a list of all active PNOR versions
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050058 data="$(ls -d ${PNOR_RO_PREFIX}* 2>/dev/null)"
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000059 mapfile -t array <<< "${data}"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050060
61 currentVersion=""
62 lowestPriority=255
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000063 for element in "${array[@]}"; do
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050064 #Remove the PNOR_RO_PREFIX from the path to get version ID.
65 versionId="${element#${PNOR_RO_PREFIX}}"
66
67 # Get the priority of active versions from persistence files.
68 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000069 data="$(grep -r "priority" "${PERSISTENCE_PATH}${versionId}")"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050070 priority="${data: -1}"
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000071 if [[ priority -le lowestPriority ]]; then
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050072 lowestPriority=${priority}
73 currentVersion=${versionId}
74 fi
75 fi
76 done
77
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000078 if [ -n "$currentVersion" ]; then
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050079 # Use active version
80 roTarget="${PNOR_RO_PREFIX}${currentVersion}"
81 rwTarget="${PNOR_RW_PREFIX}${currentVersion}"
82 prsvTarget="${PNOR_PRSV}"
83 elif [ -d "${MMC_BASE_PATH}" ]; then
84 # Use eMMC
Adriana Kobylak98f22322020-07-20 10:39:41 -050085 roTarget="${MMC_RO_PATH}"
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050086 rwTarget="${MMC_RUNNING_PATH}"
87 prsvTarget="${MMC_RUNNING_PATH}"
88
89 if [ ! -d "${HOSTFW_ACTIVE_PATH}" ]; then
90 mkdir -p "${HOSTFW_ACTIVE_PATH}"
91 fi
Adriana Kobylak98f22322020-07-20 10:39:41 -050092 # Symlinks used by PLDM
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000093 if [[ $(readlink -f "${HOSTFW_RUNNING_PATH}") != "${MMC_RUNNING_PATH}" ]]; then
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050094 rm -f ${HOSTFW_RUNNING_PATH}
95 ln -sfv ${MMC_RUNNING_PATH} ${HOSTFW_RUNNING_PATH}
96 fi
Adriana Kobylakfc886ca2021-04-26 16:07:21 +000097 if [[ $(readlink -f "${HOSTFW_ALTERNATE_PATH}") != "${MMC_ALTERNATE_PATH}" ]]; then
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -050098 rm -f ${HOSTFW_ALTERNATE_PATH}
99 ln -sfv ${MMC_ALTERNATE_PATH} ${HOSTFW_ALTERNATE_PATH}
100 fi
Adriana Kobylakfc886ca2021-04-26 16:07:21 +0000101 if [[ $(readlink -f "${HOSTFW_STAGING_PATH}") != "${MMC_STAGING_PATH}" ]]; then
Adriana Kobylak87372ae2020-09-01 11:30:59 -0500102 rm -f ${HOSTFW_STAGING_PATH}
103 ln -sfv ${MMC_STAGING_PATH} ${HOSTFW_STAGING_PATH}
104 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500105 fi
106
107 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
108 mkdir -p "${PNOR_ACTIVE_PATH}"
109 fi
110
111 # If the RW or RO active links doesn't point to the version with
112 # lowest priority, then remove the symlink and create new ones.
Adriana Kobylakfc886ca2021-04-26 16:07:21 +0000113 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != "${roTarget}" ]]; then
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500114 rm -f ${PNOR_RO_ACTIVE_PATH}
115 rm -rf ${PNOR_PATCH_LOCATION}*
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -0500116 ln -sfv ${roTarget} ${PNOR_RO_ACTIVE_PATH}
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500117 fi
118
Adriana Kobylakfc886ca2021-04-26 16:07:21 +0000119 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != "${rwTarget}" ]]; then
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500120 rm -f ${PNOR_RW_ACTIVE_PATH}
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -0500121 ln -sfv ${rwTarget} ${PNOR_RW_ACTIVE_PATH}
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500122 fi
123
Adriana Kobylakfc886ca2021-04-26 16:07:21 +0000124 if [[ $(readlink -f "${PNOR_PRSV_ACTIVE_PATH}") != "${prsvTarget}" ]]; then
Adriana Kobylak0e0a3b12020-07-15 16:47:03 -0500125 rm -f ${PNOR_PRSV_ACTIVE_PATH}
126 ln -sfv ${prsvTarget} ${PNOR_PRSV_ACTIVE_PATH}
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500127 fi
128}
129
130case "$1" in
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -0500131 clearvolatile)
132 clear_volatile
133 ;;
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500134 updatesymlinks)
135 update_symlinks
136 ;;
137 *)
138 echo "Invalid argument"
139 exit 1
140 ;;
141esac
142rc=$?
143if [ ${rc} -ne 0 ]; then
144 echo "$0: error ${rc}"
145 exit ${rc}
146fi