blob: fe3414a1f6672c1973514e8d2a645931904dfe39 [file] [log] [blame]
Adriana Kobylakfc886ca2021-04-26 16:07:21 +00001#!/bin/bash
Adriana Kobylakf3dfe652019-10-08 11:35:55 -05002
Patrick Williams15513db2022-12-08 06:23:24 -06003function clear_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
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -05008 fi
Adriana Kobylak4e82bc82021-09-28 19:25:39 +00009
Patrick Williams15513db2022-12-08 06:23:24 -060010 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 if [ ! -f "${tocFilePath}" ]; then
19 tocFilePath="${PNOR_RW_ACTIVE_PATH}${PNOR_TOC_FILE}"
Adriana Kobylak4e82bc82021-09-28 19:25:39 +000020 fi
Patrick Williams15513db2022-12-08 06:23:24 -060021 mapfile -t volatiles < <(grep VOLATILE "${tocFilePath}" | grep -Eo '^partition([0-9]+)=([A-Za-z0-9_]+)')
22 for (( index=0; index<${#volatiles[@]}; index++ )); do
23 volatileName="$(echo "${volatiles[${index}]}" | awk -F '=' '{print $2}')"
24
25 rwVolatile="${PNOR_RW_ACTIVE_PATH}${volatileName}"
26 if [ -f "${rwVolatile}" ]; then
27 echo "Clear $rwVolatile"
28 rm "${rwVolatile}"
29 fi
30 prsvVolatile="${PNOR_PRSV_ACTIVE_PATH}${volatileName}"
31 if [ -f "${prsvVolatile}" ]; then
32 echo "Clear $prsvVolatile"
33 rm "${prsvVolatile}"
34 fi
35
36 if [ ! -f "${PNOR_RO_ACTIVE_PATH}${volatileName}" ]; then
37 cp -a "${PNOR_RO_ACTIVE_PATH}81e0066f.lid" "${PNOR_PRSV_ACTIVE_PATH}"
38 ln -s "81e0066f.lid" "${PNOR_PRSV_ACTIVE_PATH}${volatileName}"
39 fi
40 done
41 # Always reset the sensor after clearing
42 busctl set-property "$service" /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled b false
Adriana Kobylakbbb5e3b2020-06-19 10:22:13 -050043}
44
Patrick Williams15513db2022-12-08 06:23:24 -060045function update_symlinks() {
46 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
47 HOSTFW_ACTIVE_PATH="/var/lib/phosphor-software-manager/hostfw/"
48 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
49 PNOR_RO_PREFIX="/media/pnor-ro-"
50 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
51 PNOR_RW_PREFIX="/media/pnor-rw-"
52 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
53 PNOR_PRSV="/media/pnor-prsv"
54 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
55 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
56 MMC_BASE_PATH="/media/hostfw"
57 MMC_RO_PATH="${MMC_BASE_PATH}/running-ro"
58 MMC_RUNNING_PATH="${MMC_BASE_PATH}/running"
59 MMC_ALTERNATE_PATH="${MMC_BASE_PATH}/alternate"
60 MMC_STAGING_PATH="${MMC_BASE_PATH}/staging"
61 MMC_NVRAM_PATH="${MMC_BASE_PATH}/nvram"
62 HOSTFW_RUNNING_PATH="${HOSTFW_ACTIVE_PATH}/running"
63 HOSTFW_ALTERNATE_PATH="${HOSTFW_ACTIVE_PATH}/alternate"
64 HOSTFW_STAGING_PATH="${HOSTFW_ACTIVE_PATH}/staging"
65 HOSTFW_NVRAM_PATH="${HOSTFW_ACTIVE_PATH}/nvram"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050066
Patrick Williams15513db2022-12-08 06:23:24 -060067 # Get a list of all active PNOR versions
68 data="$(ls -d ${PNOR_RO_PREFIX}* 2>/dev/null)"
69 mapfile -t array <<< "${data}"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050070
Patrick Williams15513db2022-12-08 06:23:24 -060071 currentVersion=""
72 lowestPriority=255
73 for element in "${array[@]}"; do
74 #Remove the PNOR_RO_PREFIX from the path to get version ID.
75 versionId="${element#"${PNOR_RO_PREFIX}"}"
Adriana Kobylakf3dfe652019-10-08 11:35:55 -050076
Patrick Williams15513db2022-12-08 06:23:24 -060077 # Get the priority of active versions from persistence files.
78 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
79 data="$(grep -r "priority" "${PERSISTENCE_PATH}${versionId}")"
80 priority="${data: -1}"
81 if [[ priority -le lowestPriority ]]; then
82 lowestPriority=${priority}
83 currentVersion=${versionId}
84 fi
85 fi
86 done
87
88 if [ -n "$currentVersion" ]; then
89 # Use active version
90 roTarget="${PNOR_RO_PREFIX}${currentVersion}"
91 rwTarget="${PNOR_RW_PREFIX}${currentVersion}"
92 prsvTarget="${PNOR_PRSV}"
93 elif [ -d "${MMC_BASE_PATH}" ]; then
94 # Use eMMC
95 roTarget="${MMC_RO_PATH}"
96 rwTarget="${MMC_RUNNING_PATH}"
97 prsvTarget="${MMC_RUNNING_PATH}"
98
99 if [ ! -d "${HOSTFW_ACTIVE_PATH}" ]; then
100 mkdir -p "${HOSTFW_ACTIVE_PATH}"
101 fi
102 # Symlinks used by PLDM
103 if [[ $(readlink -f "${HOSTFW_RUNNING_PATH}") != "${MMC_RUNNING_PATH}" ]]; then
104 rm -f ${HOSTFW_RUNNING_PATH}
105 ln -sfv ${MMC_RUNNING_PATH} ${HOSTFW_RUNNING_PATH}
106 fi
107 if [[ $(readlink -f "${HOSTFW_ALTERNATE_PATH}") != "${MMC_ALTERNATE_PATH}" ]]; then
108 rm -f ${HOSTFW_ALTERNATE_PATH}
109 ln -sfv ${MMC_ALTERNATE_PATH} ${HOSTFW_ALTERNATE_PATH}
110 fi
111 if [[ $(readlink -f "${HOSTFW_STAGING_PATH}") != "${MMC_STAGING_PATH}" ]]; then
112 rm -f ${HOSTFW_STAGING_PATH}
113 ln -sfv ${MMC_STAGING_PATH} ${HOSTFW_STAGING_PATH}
114 fi
115 if [[ $(readlink -f "${HOSTFW_NVRAM_PATH}") != "${MMC_NVRAM_PATH}" ]]; then
116 rm -f ${HOSTFW_NVRAM_PATH}
117 ln -sfv ${MMC_NVRAM_PATH} ${HOSTFW_NVRAM_PATH}
118 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500119 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500120
Patrick Williams15513db2022-12-08 06:23:24 -0600121 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500122 mkdir -p "${PNOR_ACTIVE_PATH}"
Patrick Williams15513db2022-12-08 06:23:24 -0600123 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500124
Patrick Williams15513db2022-12-08 06:23:24 -0600125 # If the RW or RO active links doesn't point to the version with
126 # lowest priority, then remove the symlink and create new ones.
127 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != "${roTarget}" ]]; then
128 rm -f ${PNOR_RO_ACTIVE_PATH}
129 rm -rf ${PNOR_PATCH_LOCATION}*
Andrew Geisslereab7ca82023-05-09 08:24:32 -0500130 ln -sfv "${roTarget}" ${PNOR_RO_ACTIVE_PATH}
Patrick Williams15513db2022-12-08 06:23:24 -0600131 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500132
Patrick Williams15513db2022-12-08 06:23:24 -0600133 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != "${rwTarget}" ]]; then
134 rm -f ${PNOR_RW_ACTIVE_PATH}
Andrew Geisslereab7ca82023-05-09 08:24:32 -0500135 ln -sfv "${rwTarget}" ${PNOR_RW_ACTIVE_PATH}
Patrick Williams15513db2022-12-08 06:23:24 -0600136 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500137
Patrick Williams15513db2022-12-08 06:23:24 -0600138 if [[ $(readlink -f "${PNOR_PRSV_ACTIVE_PATH}") != "${prsvTarget}" ]]; then
139 rm -f ${PNOR_PRSV_ACTIVE_PATH}
Andrew Geisslereab7ca82023-05-09 08:24:32 -0500140 ln -sfv "${prsvTarget}" ${PNOR_PRSV_ACTIVE_PATH}
Patrick Williams15513db2022-12-08 06:23:24 -0600141 fi
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500142}
143
144case "$1" in
Patrick Williams15513db2022-12-08 06:23:24 -0600145 clearvolatile)
146 clear_volatile
147 ;;
148 updatesymlinks)
149 update_symlinks
150 ;;
151 *)
152 echo "Invalid argument"
153 exit 1
154 ;;
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500155esac
156rc=$?
157if [ ${rc} -ne 0 ]; then
Patrick Williams15513db2022-12-08 06:23:24 -0600158 echo "$0: error ${rc}"
159 exit ${rc}
Adriana Kobylakf3dfe652019-10-08 11:35:55 -0500160fi