blob: 298a9c41a9ab82824691cf6e37bf9ec0d31c2916 [file] [log] [blame]
Adriana Kobylake2538382017-07-06 10:31:59 -05001#!/bin/sh
2
3# Get the mtd device number (mtdX)
4findmtd() {
5 m="$(grep -xl "$1" /sys/class/mtd/*/name)"
6 m="${m%/name}"
7 m="${m##*/}"
8 echo "${m}"
9}
10
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050011# Get the ubi device number (ubiX_Y)
12findubi() {
13 u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
14 u="${u%/name}"
15 u="${u##*/}"
16 echo "${u}"
17}
18
19# Get the mount information
20is_mounted() {
21 grep -q "$1" /proc/mounts
22 return $?
23}
24
Saqib Khanbc0d3ed2017-08-01 09:46:22 -050025# Attach the pnor mtd device to ubi.
Adriana Kobylake2538382017-07-06 10:31:59 -050026attach_ubi() {
27 pnormtd="$(findmtd pnor)"
28 pnor="${pnormtd#mtd}"
29 pnordev="/dev/mtd${pnor}"
30
Saqib Khan3ab09ac2017-08-02 15:13:26 -050031 if [ -d "/sys/class/ubi/ubi${pnor}" ]; then
32 # Already attached
33 return 0
34 fi
35
Adriana Kobylake2538382017-07-06 10:31:59 -050036 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
37 rc=$?
38 if [ ${rc} -ne 0 ]; then
39 # Check the pnor mtd device is formatted as ubi by reading the first 3 byes,
40 # which should be the ascii chars 'UBI'
41 magic="$(hexdump -C -n 3 ${pnordev})"
42 if [[ "${magic}" =~ "UBI" ]]; then
43 # Device already formatted as ubi, ubiattach failed for some other reason
44 return ${rc}
45 else
46 # Format device as ubi
47 echo "Starting ubiformat ${pnordev}"
48 ubiformat "${pnordev}" -y -q
49 # Retry the ubiattach
50 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
51 fi
52 fi
53}
54
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050055mount_squashfs() {
56 pnormtd="$(findmtd pnor)"
Michael Tritz82cad842017-08-08 13:35:17 -050057 ubidev="/dev/ubi${pnormtd#mtd}"
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050058 mountdir="/media/${name}"
Michael Tritz82cad842017-08-08 13:35:17 -050059 vol="$(findubi "${name}")"
Saqib Khanc9f33772017-10-08 22:30:30 -050060 img="/tmp/images/${version}/pnor.xz.squashfs"
61 filesize="$(ls -sh $img | awk -F " " {'print $1'})"
Michael Tritz82cad842017-08-08 13:35:17 -050062
63 if is_mounted "${name}"; then
64 echo "${name} is already mounted."
65 return 0
66 fi
67
68 if [ ! -z "${vol}" ]; then
69 ubirmvol "${ubidev}" -N "${name}"
70 fi
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050071
72 if [ ! -d "${mountdir}" ]; then
73 mkdir "${mountdir}"
74 fi
75
Saqib Khanc9f33772017-10-08 22:30:30 -050076 # Set size of read-only partition equal to pnor.xz.squashfs
77 ubimkvol "${ubidev}" -N "${name}" -s "${filesize}"KiB --type=static
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050078 vol="$(findubi "${name}")"
Michael Tritz82cad842017-08-08 13:35:17 -050079
80 if [ $? != 0 ]; then
81 echo "Unable to create RO volume!"
82 return 1
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050083 fi
84
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050085 ubidevid="${vol#ubi}"
Michael Tritz82cad842017-08-08 13:35:17 -050086 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
87
88 if [ $? != 0 ]; then
89 echo "Unable to update RO volume!"
90 return 1
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050091 fi
92
Michael Tritz82cad842017-08-08 13:35:17 -050093 ubiblock --create "/dev/ubi${ubidevid}"
94
95 if [ $? != 0 ]; then
96 echo "Unable to create UBI block for RO volume!"
97 return 1
98 fi
99
100 mount -t squashfs -o ro "/dev/ubiblock${ubidevid}" "${mountdir}"
101
102 if [ $? != 0 ]; then
103 echo "Unable to mount RO volume!"
104 return 1
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -0500105 fi
106}
107
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500108mount_ubi() {
109 pnormtd="$(findmtd pnor)"
110 pnor="${pnormtd#mtd}"
111 ubidev="/dev/ubi${pnor}"
Saqib Khaneebcd5f2017-08-18 11:14:17 -0500112 pnordev="/dev/mtd${pnor}"
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500113
114 if [[ "${name}" == "pnor-patch" ]]; then
Michael Tritzfb1950f2017-11-13 11:05:03 -0600115 if [[ "$(fw_printenv fieldmode 2>/dev/null)" == "fieldmode=true" ]]; then
116 return 0
117 fi
Saqib Khaneebcd5f2017-08-18 11:14:17 -0500118 if [[ ! "$(hexdump -C -n 3 ${pnordev})" =~ "UBI" ]]; then
119 return 0
120 fi
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500121 mountdir="/usr/local/share/pnor"
122 else
123 mountdir="/media/${name}"
124 fi
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500125
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500126 if [[ "${name}" == "pnor-prsv" ]]; then
127 size="2MiB"
128 else
129 size="16MiB"
130 fi
131
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500132 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500133 mkdir -p "${mountdir}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500134 fi
135
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500136 vol="$(findubi "${name}")"
137 if [ -z "${vol}" ]; then
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500138 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500139 fi
140
141 if ! is_mounted "${name}"; then
142 mountdev="ubi${pnor}:${name}"
143 mount -t ubifs "${mountdev}" "${mountdir}"
144 fi
145}
146
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500147umount_ubi() {
148 pnormtd="$(findmtd pnor)"
149 pnor="${pnormtd#mtd}"
150 ubidev="/dev/ubi${pnor}"
151 mountdir="/media/${name}"
152
153 if is_mounted "${name}"; then
154 umount "${mountdir}"
155 fi
156
157 vol="$(findubi "${name}")"
158 if [ -n "${vol}" ]; then
159 ubirmvol "${ubidev}" -N "${name}"
160 fi
161
162 if [ -d "${mountdir}" ]; then
163 rm -r "${mountdir}"
164 fi
165}
166
Saqib Khanbea768c2017-07-26 23:48:20 -0500167remount_ubi() {
168 pnormtd="$(findmtd pnor)"
169 pnor="${pnormtd#mtd}"
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500170 pnordev="/dev/mtd${pnor}"
171
172 # Re-Attach the pnor mtd device to ubi
Saqib Khan688cbaa2017-08-07 12:51:48 -0500173 if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then
174 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
175 else
176 # Device not formatted as ubi.
177 return 0
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500178 fi
179
Saqib Khanbea768c2017-07-26 23:48:20 -0500180 # Get information on all ubi volumes
181 ubinfo=$(ubinfo -d ${pnor})
182 presentVolumes=${ubinfo##*:}
183 IFS=', ' read -r -a array <<< "$presentVolumes"
184 for element in ${array[@]};
185 do
186 elementProperties=$(ubinfo -d $pnor -n $element)
187 # Get ubi volume name by getting rid of additional properties
188 name=${elementProperties#*Name:}
189 name="${name%Character*}"
190 name="$(echo -e "${name}" | tr -d '[:space:]')"
191
192 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
193 mountdir="/media/${name}"
Saqib Khanbea979a2017-08-01 11:32:27 -0500194 if [ ! -d "${mountdir}" ]; then
195 mkdir -p "${mountdir}"
196 fi
197
Saqib Khanbea768c2017-07-26 23:48:20 -0500198 if [[ ${name} == pnor-ro* ]]
199 then
200 ubiblock --create /dev/ubi${pnor}_${element}
201 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
202 else
203 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
204 fi
205 fi
206 done
207}
208
Saqib Khanfa92e972017-08-02 12:48:12 -0500209update_symlinks() {
210 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
211 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
212 PNOR_RO_PREFIX="/media/pnor-ro-"
213 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
214 PNOR_RW_PREFIX="/media/pnor-rw-"
215 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
216 PNOR_PRSV="/media/pnor-prsv"
Saqib Khan405f71d2017-08-17 11:40:40 -0500217 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
Saqib Khan2743cb32017-09-10 00:44:16 -0500218 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
Saqib Khanfa92e972017-08-02 12:48:12 -0500219
Saqib Khan405f71d2017-08-17 11:40:40 -0500220 # Get a list of all active PNOR versions
221 data="$(ls -d ${PNOR_RO_PREFIX}*)"
222 IFS=$'\n' array=(${data})
Saqib Khanfa92e972017-08-02 12:48:12 -0500223
Saqib Khan405f71d2017-08-17 11:40:40 -0500224 currentVersion=""
225 lowestPriority=255
226 for element in ${array[@]}; do
227 #Remove the PNOR_RO_PREFIX from the path to get version ID.
228 versionId="${element#${PNOR_RO_PREFIX}}"
229
230 # Get the priority of active versions from persistence files.
231 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
232 data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
233 priority="${data: -1}"
234 if [[ priority -le lowestPriority ]]; then
235 lowestPriority=${priority}
236 currentVersion=${versionId}
237 fi
Saqib Khanfa92e972017-08-02 12:48:12 -0500238 fi
239 done
240
Saqib Khan405f71d2017-08-17 11:40:40 -0500241 # Return if no active version found
242 if [ -z $currentVersion ]; then
243 return 0;
244 fi
245
Saqib Khanfa92e972017-08-02 12:48:12 -0500246 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
247 mkdir -p "${PNOR_ACTIVE_PATH}"
248 fi
249
250 # If the RW or RO active links doesn't point to the version with
251 # lowest priority, then remove the symlink and create new ones.
252 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then
253 rm -f ${PNOR_RO_ACTIVE_PATH}
Saqib Khan2743cb32017-09-10 00:44:16 -0500254 rm -rf ${PNOR_PATCH_LOCATION}*
Saqib Khanfa92e972017-08-02 12:48:12 -0500255 ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
256 fi
257
258 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then
259 rm -f ${PNOR_RW_ACTIVE_PATH}
260 ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
261 fi
262
263 if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then
264 ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
265 fi
266}
267
Michael Tritzed6d7b02017-09-21 00:41:15 -0500268ubi_cleanup() {
269 # When ubi_cleanup is run, it expects one or no active version.
270 activeVersion=$(busctl --list --no-pager tree \
271 org.open_power.Software.Host.Updater | \
272 grep /xyz/openbmc_project/software/ | tail -c 9)
273
274 if [[ -z "$activeVersion" ]]; then
275 vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | cut -c 14-)
276 vols=(${vols})
277 else
278 vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | \
279 grep -v "$activeVersion" | cut -c 14-)
280 vols=(${vols})
281 fi
282
283 for (( index=0; index<${#vols[@]}; index++ )); do
284 name=${vols[index]}
285 umount_ubi
286 done
287}
288
Michael Tritzeee45422018-03-13 16:53:15 -0500289clear_volatile() {
290 service=$(mapper get-service /org/open_power/control/volatile)
291 clearVolatileEnabled=$(busctl get-property $service /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled)
292 if [[ "$clearVolatileEnabled" != "b true" ]]; then
293 return 0
294 fi
295
296 PNOR_TOC_FILE="pnor.toc"
297 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro/"
298 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw/"
299 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv/"
300
301 # toc partition string format:
302 # partition27=HB_VOLATILE,0x02ba9000,0x02bae000,00,ECC,VOLATILE,READWRITE
303 tocFilePath="${PNOR_RO_ACTIVE_PATH}${PNOR_TOC_FILE}"
304 volatiles=($(grep VOLATILE "${tocFilePath}" | grep -Eo '^partition([0-9]+)=([A-Za-z0-9_]+)'))
305 for (( index=0; index<${#volatiles[@]}; index++ )); do
306 volatileName="$(echo ${volatiles[${index}]} | awk -F '=' '{print $2}')"
307
308 rwVolatile="${PNOR_RW_ACTIVE_PATH}${volatileName}"
309 if [ -f "${rwVolatile}" ]; then
310 echo "Clear $rwVolatile"
311 rm "${rwVolatile}"
312 fi
313 prsvVolatile="${PNOR_PRSV_ACTIVE_PATH}${volatileName}"
314 if [ -f "${prsvVolatile}" ]; then
315 echo "Clear $prsvVolatile"
316 rm "${prsvVolatile}"
317 fi
318 done
319 # Always reset the sensor after clearing
320 busctl set-property $service /org/open_power/control/volatile xyz.openbmc_project.Object.Enable Enabled b false
321}
322
Adriana Kobylake2538382017-07-06 10:31:59 -0500323case "$1" in
324 ubiattach)
325 attach_ubi
326 ;;
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -0500327 squashfsmount)
328 name="$2"
329 version="$3"
330 mount_squashfs
331 ;;
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500332 ubimount)
333 name="$2"
334 mount_ubi
335 ;;
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500336 ubiumount)
337 name="$2"
338 umount_ubi
339 ;;
Saqib Khanbea768c2017-07-26 23:48:20 -0500340 ubiremount)
341 remount_ubi
342 ;;
Saqib Khanfa92e972017-08-02 12:48:12 -0500343 updatesymlinks)
344 update_symlinks
345 ;;
Michael Tritzed6d7b02017-09-21 00:41:15 -0500346 ubicleanup)
347 ubi_cleanup
348 ;;
Michael Tritzeee45422018-03-13 16:53:15 -0500349 clearvolatile)
350 clear_volatile
351 ;;
Adriana Kobylake2538382017-07-06 10:31:59 -0500352 *)
353 echo "Invalid argument"
354 exit 1
355 ;;
356esac
357rc=$?
358if [ ${rc} -ne 0 ]; then
359 echo "$0: error ${rc}"
360 exit ${rc}
361fi