blob: 2abf8fb22768894cacf415a52f88dddd146a4f7f [file] [log] [blame]
Adriana Kobylakf3dfe652019-10-08 11:35:55 -05001#!/bin/sh
2
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -05003clear_volatile() {
4 service=$(mapper get-service /org/open_power/control/volatile)
5 clearVolatileEnabled=$(busctl get-property $service /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled)
6 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}"
18 volatiles=($(grep VOLATILE "${tocFilePath}" | grep -Eo '^partition([0-9]+)=([A-Za-z0-9_]+)'))
19 for (( index=0; index<${#volatiles[@]}; index++ )); do
20 volatileName="$(echo ${volatiles[${index}]} | awk -F '=' '{print $2}')"
21
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
34 busctl set-property $service /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled b false
35}
36
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050037update_symlinks() {
38 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
39 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
40 PNOR_RO_PREFIX="/media/pnor-ro-"
41 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
42 PNOR_RW_PREFIX="/media/pnor-rw-"
43 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
44 PNOR_PRSV="/media/pnor-prsv"
45 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
46 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
47
48 # Get a list of all active PNOR versions
49 data="$(ls -d ${PNOR_RO_PREFIX}*)"
50 IFS=$'\n' array=(${data})
51
52 currentVersion=""
53 lowestPriority=255
54 for element in ${array[@]}; do
55 #Remove the PNOR_RO_PREFIX from the path to get version ID.
56 versionId="${element#${PNOR_RO_PREFIX}}"
57
58 # Get the priority of active versions from persistence files.
59 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
60 data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
61 priority="${data: -1}"
62 if [[ priority -le lowestPriority ]]; then
63 lowestPriority=${priority}
64 currentVersion=${versionId}
65 fi
66 fi
67 done
68
69 # Return if no active version found
70 if [ -z $currentVersion ]; then
71 return 0;
72 fi
73
74 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
75 mkdir -p "${PNOR_ACTIVE_PATH}"
76 fi
77
78 # If the RW or RO active links doesn't point to the version with
79 # lowest priority, then remove the symlink and create new ones.
80 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then
81 rm -f ${PNOR_RO_ACTIVE_PATH}
82 rm -rf ${PNOR_PATCH_LOCATION}*
83 ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
84 fi
85
86 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then
87 rm -f ${PNOR_RW_ACTIVE_PATH}
88 ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
89 fi
90
91 if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then
92 ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
93 fi
94}
95
96case "$1" in
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -050097 clearvolatile)
98 clear_volatile
99 ;;
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500100 updatesymlinks)
101 update_symlinks
102 ;;
103 *)
104 echo "Invalid argument"
105 exit 1
106 ;;
107esac
108rc=$?
109if [ ${rc} -ne 0 ]; then
110 echo "$0: error ${rc}"
111 exit ${rc}
112fi