blob: 1c90e8b2f30c8d219f8ad8d2f80020a259d88cad [file] [log] [blame]
Adriana Kobylake882f912017-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 Kobylakf15e77c2017-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 Khance611932017-08-01 09:46:22 -050025# Attach the pnor mtd device to ubi.
Adriana Kobylake882f912017-07-06 10:31:59 -050026attach_ubi() {
27 pnormtd="$(findmtd pnor)"
28 pnor="${pnormtd#mtd}"
29 pnordev="/dev/mtd${pnor}"
30
Saqib Khan4f609b82017-08-02 15:13:26 -050031 if [ -d "/sys/class/ubi/ubi${pnor}" ]; then
32 # Already attached
33 return 0
34 fi
35
Adriana Kobylake882f912017-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 Kobylakf15e77c2017-07-06 11:26:42 -050055mount_squashfs() {
56 pnormtd="$(findmtd pnor)"
Michael Tritz45e55f82017-08-08 13:35:17 -050057 ubidev="/dev/ubi${pnormtd#mtd}"
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050058 mountdir="/media/${name}"
Michael Tritz45e55f82017-08-08 13:35:17 -050059 vol="$(findubi "${name}")"
Saqib Khan541e6222017-10-08 22:30:30 -050060 img="/tmp/images/${version}/pnor.xz.squashfs"
61 filesize="$(ls -sh $img | awk -F " " {'print $1'})"
Michael Tritz45e55f82017-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 Kobylakf15e77c2017-07-06 11:26:42 -050071
72 if [ ! -d "${mountdir}" ]; then
73 mkdir "${mountdir}"
74 fi
75
Saqib Khan541e6222017-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 Kobylakf15e77c2017-07-06 11:26:42 -050078 vol="$(findubi "${name}")"
Michael Tritz45e55f82017-08-08 13:35:17 -050079
80 if [ $? != 0 ]; then
81 echo "Unable to create RO volume!"
82 return 1
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050083 fi
84
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050085 ubidevid="${vol#ubi}"
Michael Tritz45e55f82017-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 Kobylakf15e77c2017-07-06 11:26:42 -050091 fi
92
Michael Tritz45e55f82017-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 Kobylakf15e77c2017-07-06 11:26:42 -0500105 fi
106}
107
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500108mount_ubi() {
109 pnormtd="$(findmtd pnor)"
110 pnor="${pnormtd#mtd}"
111 ubidev="/dev/ubi${pnor}"
Saqib Khan14ec4bd2017-08-18 11:14:17 -0500112 pnordev="/dev/mtd${pnor}"
Adriana Kobylakf287af32017-07-12 13:52:22 -0500113
114 if [[ "${name}" == "pnor-patch" ]]; then
Michael Tritz4adb1302017-11-13 11:05:03 -0600115 if [[ "$(fw_printenv fieldmode 2>/dev/null)" == "fieldmode=true" ]]; then
116 return 0
117 fi
Saqib Khan14ec4bd2017-08-18 11:14:17 -0500118 if [[ ! "$(hexdump -C -n 3 ${pnordev})" =~ "UBI" ]]; then
119 return 0
120 fi
Adriana Kobylakf287af32017-07-12 13:52:22 -0500121 mountdir="/usr/local/share/pnor"
122 else
123 mountdir="/media/${name}"
124 fi
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500125
Adriana Kobylakd3165592017-07-18 12:24:24 -0500126 if [[ "${name}" == "pnor-prsv" ]]; then
127 size="2MiB"
128 else
129 size="16MiB"
130 fi
131
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500132 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakf287af32017-07-12 13:52:22 -0500133 mkdir -p "${mountdir}"
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500134 fi
135
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500136 vol="$(findubi "${name}")"
137 if [ -z "${vol}" ]; then
Adriana Kobylakd3165592017-07-18 12:24:24 -0500138 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak6aff5742017-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 Kobylake5764af2017-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 Khan58c870d2017-07-26 23:48:20 -0500167remount_ubi() {
168 pnormtd="$(findmtd pnor)"
169 pnor="${pnormtd#mtd}"
Saqib Khance611932017-08-01 09:46:22 -0500170 pnordev="/dev/mtd${pnor}"
171
172 # Re-Attach the pnor mtd device to ubi
Saqib Khan7bfc76f2017-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 Khance611932017-08-01 09:46:22 -0500178 fi
179
Saqib Khan58c870d2017-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 Khan76b7b832017-08-01 11:32:27 -0500194 if [ ! -d "${mountdir}" ]; then
195 mkdir -p "${mountdir}"
196 fi
197
Saqib Khan58c870d2017-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 Khand536f0d2017-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 Khand2249d62017-08-17 11:40:40 -0500217 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
Saqib Khan94734082017-09-10 00:44:16 -0500218 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
Saqib Khand536f0d2017-08-02 12:48:12 -0500219
Saqib Khand2249d62017-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 Khand536f0d2017-08-02 12:48:12 -0500223
Saqib Khand2249d62017-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 Khand536f0d2017-08-02 12:48:12 -0500238 fi
239 done
240
Saqib Khand2249d62017-08-17 11:40:40 -0500241 # Return if no active version found
242 if [ -z $currentVersion ]; then
243 return 0;
244 fi
245
Saqib Khand536f0d2017-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 Khan94734082017-09-10 00:44:16 -0500254 rm -rf ${PNOR_PATCH_LOCATION}*
Saqib Khand536f0d2017-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 Tritze8219b72017-09-22 15:00:51 -0500268clear_ubi() {
269 mountdir="/media/${name}"
270 if is_mounted "${name}"; then
271 rm -rf $mountdir/..?* $mountdir/.[!.]* $mountdir/*
272 fi
273}
274
Michael Tritzcebaac02017-09-21 00:41:15 -0500275ubi_cleanup() {
276 # When ubi_cleanup is run, it expects one or no active version.
277 activeVersion=$(busctl --list --no-pager tree \
278 org.open_power.Software.Host.Updater | \
279 grep /xyz/openbmc_project/software/ | tail -c 9)
280
281 if [[ -z "$activeVersion" ]]; then
282 vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | cut -c 14-)
283 vols=(${vols})
284 else
285 vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | \
286 grep -v "$activeVersion" | cut -c 14-)
287 vols=(${vols})
288 fi
289
290 for (( index=0; index<${#vols[@]}; index++ )); do
291 name=${vols[index]}
292 umount_ubi
293 done
294}
295
Adriana Kobylake882f912017-07-06 10:31:59 -0500296case "$1" in
297 ubiattach)
298 attach_ubi
299 ;;
Adriana Kobylakf15e77c2017-07-06 11:26:42 -0500300 squashfsmount)
301 name="$2"
302 version="$3"
303 mount_squashfs
304 ;;
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500305 ubimount)
306 name="$2"
307 mount_ubi
308 ;;
Adriana Kobylake5764af2017-06-07 15:24:52 -0500309 ubiumount)
310 name="$2"
311 umount_ubi
312 ;;
Saqib Khan58c870d2017-07-26 23:48:20 -0500313 ubiremount)
314 remount_ubi
315 ;;
Saqib Khand536f0d2017-08-02 12:48:12 -0500316 updatesymlinks)
317 update_symlinks
318 ;;
Michael Tritze8219b72017-09-22 15:00:51 -0500319 ubiclear)
320 name="$2"
321 clear_ubi
322 ;;
Michael Tritzcebaac02017-09-21 00:41:15 -0500323 ubicleanup)
324 ubi_cleanup
325 ;;
Adriana Kobylake882f912017-07-06 10:31:59 -0500326 *)
327 echo "Invalid argument"
328 exit 1
329 ;;
330esac
331rc=$?
332if [ ${rc} -ne 0 ]; then
333 echo "$0: error ${rc}"
334 exit ${rc}
335fi