blob: d92fad5d91a1e86cb85c1a107185008f1eaecc6f [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
Saqib Khaneebcd5f2017-08-18 11:14:17 -0500115 if [[ ! "$(hexdump -C -n 3 ${pnordev})" =~ "UBI" ]]; then
116 return 0
117 fi
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500118 mountdir="/usr/local/share/pnor"
119 else
120 mountdir="/media/${name}"
121 fi
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500122
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500123 if [[ "${name}" == "pnor-prsv" ]]; then
124 size="2MiB"
125 else
126 size="16MiB"
127 fi
128
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500129 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500130 mkdir -p "${mountdir}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500131 fi
132
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500133 vol="$(findubi "${name}")"
134 if [ -z "${vol}" ]; then
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500135 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500136 fi
137
138 if ! is_mounted "${name}"; then
139 mountdev="ubi${pnor}:${name}"
140 mount -t ubifs "${mountdev}" "${mountdir}"
141 fi
142}
143
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500144umount_ubi() {
145 pnormtd="$(findmtd pnor)"
146 pnor="${pnormtd#mtd}"
147 ubidev="/dev/ubi${pnor}"
148 mountdir="/media/${name}"
149
150 if is_mounted "${name}"; then
151 umount "${mountdir}"
152 fi
153
154 vol="$(findubi "${name}")"
155 if [ -n "${vol}" ]; then
156 ubirmvol "${ubidev}" -N "${name}"
157 fi
158
159 if [ -d "${mountdir}" ]; then
160 rm -r "${mountdir}"
161 fi
162}
163
Saqib Khanbea768c2017-07-26 23:48:20 -0500164remount_ubi() {
165 pnormtd="$(findmtd pnor)"
166 pnor="${pnormtd#mtd}"
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500167 pnordev="/dev/mtd${pnor}"
168
169 # Re-Attach the pnor mtd device to ubi
Saqib Khan688cbaa2017-08-07 12:51:48 -0500170 if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then
171 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
172 else
173 # Device not formatted as ubi.
174 return 0
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500175 fi
176
Saqib Khanbea768c2017-07-26 23:48:20 -0500177 # Get information on all ubi volumes
178 ubinfo=$(ubinfo -d ${pnor})
179 presentVolumes=${ubinfo##*:}
180 IFS=', ' read -r -a array <<< "$presentVolumes"
181 for element in ${array[@]};
182 do
183 elementProperties=$(ubinfo -d $pnor -n $element)
184 # Get ubi volume name by getting rid of additional properties
185 name=${elementProperties#*Name:}
186 name="${name%Character*}"
187 name="$(echo -e "${name}" | tr -d '[:space:]')"
188
189 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
190 mountdir="/media/${name}"
Saqib Khanbea979a2017-08-01 11:32:27 -0500191 if [ ! -d "${mountdir}" ]; then
192 mkdir -p "${mountdir}"
193 fi
194
Saqib Khanbea768c2017-07-26 23:48:20 -0500195 if [[ ${name} == pnor-ro* ]]
196 then
197 ubiblock --create /dev/ubi${pnor}_${element}
198 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
199 else
200 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
201 fi
202 fi
203 done
204}
205
Saqib Khanfa92e972017-08-02 12:48:12 -0500206update_symlinks() {
207 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
208 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
209 PNOR_RO_PREFIX="/media/pnor-ro-"
210 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
211 PNOR_RW_PREFIX="/media/pnor-rw-"
212 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
213 PNOR_PRSV="/media/pnor-prsv"
Saqib Khan405f71d2017-08-17 11:40:40 -0500214 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
Saqib Khan2743cb32017-09-10 00:44:16 -0500215 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
Saqib Khanfa92e972017-08-02 12:48:12 -0500216
Saqib Khan405f71d2017-08-17 11:40:40 -0500217 # Get a list of all active PNOR versions
218 data="$(ls -d ${PNOR_RO_PREFIX}*)"
219 IFS=$'\n' array=(${data})
Saqib Khanfa92e972017-08-02 12:48:12 -0500220
Saqib Khan405f71d2017-08-17 11:40:40 -0500221 currentVersion=""
222 lowestPriority=255
223 for element in ${array[@]}; do
224 #Remove the PNOR_RO_PREFIX from the path to get version ID.
225 versionId="${element#${PNOR_RO_PREFIX}}"
226
227 # Get the priority of active versions from persistence files.
228 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
229 data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
230 priority="${data: -1}"
231 if [[ priority -le lowestPriority ]]; then
232 lowestPriority=${priority}
233 currentVersion=${versionId}
234 fi
Saqib Khanfa92e972017-08-02 12:48:12 -0500235 fi
236 done
237
Saqib Khan405f71d2017-08-17 11:40:40 -0500238 # Return if no active version found
239 if [ -z $currentVersion ]; then
240 return 0;
241 fi
242
Saqib Khanfa92e972017-08-02 12:48:12 -0500243 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
244 mkdir -p "${PNOR_ACTIVE_PATH}"
245 fi
246
247 # If the RW or RO active links doesn't point to the version with
248 # lowest priority, then remove the symlink and create new ones.
249 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then
250 rm -f ${PNOR_RO_ACTIVE_PATH}
Saqib Khan2743cb32017-09-10 00:44:16 -0500251 rm -rf ${PNOR_PATCH_LOCATION}*
Saqib Khanfa92e972017-08-02 12:48:12 -0500252 ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
253 fi
254
255 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then
256 rm -f ${PNOR_RW_ACTIVE_PATH}
257 ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
258 fi
259
260 if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then
261 ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
262 fi
263}
264
Michael Tritzf15008a2017-09-22 15:00:51 -0500265clear_ubi() {
266 mountdir="/media/${name}"
267 if is_mounted "${name}"; then
268 rm -rf $mountdir/..?* $mountdir/.[!.]* $mountdir/*
269 fi
270}
271
Michael Tritzed6d7b02017-09-21 00:41:15 -0500272ubi_cleanup() {
273 # When ubi_cleanup is run, it expects one or no active version.
274 activeVersion=$(busctl --list --no-pager tree \
275 org.open_power.Software.Host.Updater | \
276 grep /xyz/openbmc_project/software/ | tail -c 9)
277
278 if [[ -z "$activeVersion" ]]; then
279 vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | cut -c 14-)
280 vols=(${vols})
281 else
282 vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | \
283 grep -v "$activeVersion" | cut -c 14-)
284 vols=(${vols})
285 fi
286
287 for (( index=0; index<${#vols[@]}; index++ )); do
288 name=${vols[index]}
289 umount_ubi
290 done
291}
292
Adriana Kobylake2538382017-07-06 10:31:59 -0500293case "$1" in
294 ubiattach)
295 attach_ubi
296 ;;
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -0500297 squashfsmount)
298 name="$2"
299 version="$3"
300 mount_squashfs
301 ;;
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500302 ubimount)
303 name="$2"
304 mount_ubi
305 ;;
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500306 ubiumount)
307 name="$2"
308 umount_ubi
309 ;;
Saqib Khanbea768c2017-07-26 23:48:20 -0500310 ubiremount)
311 remount_ubi
312 ;;
Saqib Khanfa92e972017-08-02 12:48:12 -0500313 updatesymlinks)
314 update_symlinks
315 ;;
Michael Tritzf15008a2017-09-22 15:00:51 -0500316 ubiclear)
317 name="$2"
318 clear_ubi
319 ;;
Michael Tritzed6d7b02017-09-21 00:41:15 -0500320 ubicleanup)
321 ubi_cleanup
322 ;;
Adriana Kobylake2538382017-07-06 10:31:59 -0500323 *)
324 echo "Invalid argument"
325 exit 1
326 ;;
327esac
328rc=$?
329if [ ${rc} -ne 0 ]; then
330 echo "$0: error ${rc}"
331 exit ${rc}
332fi