blob: f82e6fb28d009baaefe45ef83c58d12533c8b156 [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}")"
60
61 if is_mounted "${name}"; then
62 echo "${name} is already mounted."
63 return 0
64 fi
65
66 if [ ! -z "${vol}" ]; then
67 ubirmvol "${ubidev}" -N "${name}"
68 fi
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050069
70 if [ ! -d "${mountdir}" ]; then
71 mkdir "${mountdir}"
72 fi
73
Adriana Kobylakd3165592017-07-18 12:24:24 -050074 # Create a static ubi volume of arbitrary size 24MB,
75 # the current pnor image is ~19MB
76 # TODO Set size based on file size openbmc/openbmc#1840
Michael Tritz45e55f82017-08-08 13:35:17 -050077 ubimkvol "${ubidev}" -N "${name}" -s 24MiB --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 img="/tmp/images/${version}/pnor.xz.squashfs"
87 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
88
89 if [ $? != 0 ]; then
90 echo "Unable to update RO volume!"
91 return 1
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050092 fi
93
Michael Tritz45e55f82017-08-08 13:35:17 -050094 ubiblock --create "/dev/ubi${ubidevid}"
95
96 if [ $? != 0 ]; then
97 echo "Unable to create UBI block for RO volume!"
98 return 1
99 fi
100
101 mount -t squashfs -o ro "/dev/ubiblock${ubidevid}" "${mountdir}"
102
103 if [ $? != 0 ]; then
104 echo "Unable to mount RO volume!"
105 return 1
Adriana Kobylakf15e77c2017-07-06 11:26:42 -0500106 fi
107}
108
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500109mount_ubi() {
110 pnormtd="$(findmtd pnor)"
111 pnor="${pnormtd#mtd}"
112 ubidev="/dev/ubi${pnor}"
Saqib Khan14ec4bd2017-08-18 11:14:17 -0500113 pnordev="/dev/mtd${pnor}"
Adriana Kobylakf287af32017-07-12 13:52:22 -0500114
115 if [[ "${name}" == "pnor-patch" ]]; then
Saqib Khan14ec4bd2017-08-18 11:14:17 -0500116 if [[ ! "$(hexdump -C -n 3 ${pnordev})" =~ "UBI" ]]; then
117 return 0
118 fi
Adriana Kobylakf287af32017-07-12 13:52:22 -0500119 mountdir="/usr/local/share/pnor"
120 else
121 mountdir="/media/${name}"
122 fi
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500123
Adriana Kobylakd3165592017-07-18 12:24:24 -0500124 if [[ "${name}" == "pnor-prsv" ]]; then
125 size="2MiB"
126 else
127 size="16MiB"
128 fi
129
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500130 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakf287af32017-07-12 13:52:22 -0500131 mkdir -p "${mountdir}"
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500132 fi
133
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500134 vol="$(findubi "${name}")"
135 if [ -z "${vol}" ]; then
Adriana Kobylakd3165592017-07-18 12:24:24 -0500136 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500137 fi
138
139 if ! is_mounted "${name}"; then
140 mountdev="ubi${pnor}:${name}"
141 mount -t ubifs "${mountdev}" "${mountdir}"
142 fi
143}
144
Adriana Kobylake5764af2017-06-07 15:24:52 -0500145umount_ubi() {
146 pnormtd="$(findmtd pnor)"
147 pnor="${pnormtd#mtd}"
148 ubidev="/dev/ubi${pnor}"
149 mountdir="/media/${name}"
150
151 if is_mounted "${name}"; then
152 umount "${mountdir}"
153 fi
154
155 vol="$(findubi "${name}")"
156 if [ -n "${vol}" ]; then
157 ubirmvol "${ubidev}" -N "${name}"
158 fi
159
160 if [ -d "${mountdir}" ]; then
161 rm -r "${mountdir}"
162 fi
163}
164
Saqib Khan58c870d2017-07-26 23:48:20 -0500165remount_ubi() {
166 pnormtd="$(findmtd pnor)"
167 pnor="${pnormtd#mtd}"
Saqib Khance611932017-08-01 09:46:22 -0500168 pnordev="/dev/mtd${pnor}"
169
170 # Re-Attach the pnor mtd device to ubi
Saqib Khan7bfc76f2017-08-07 12:51:48 -0500171 if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then
172 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
173 else
174 # Device not formatted as ubi.
175 return 0
Saqib Khance611932017-08-01 09:46:22 -0500176 fi
177
Saqib Khan58c870d2017-07-26 23:48:20 -0500178 # Get information on all ubi volumes
179 ubinfo=$(ubinfo -d ${pnor})
180 presentVolumes=${ubinfo##*:}
181 IFS=', ' read -r -a array <<< "$presentVolumes"
182 for element in ${array[@]};
183 do
184 elementProperties=$(ubinfo -d $pnor -n $element)
185 # Get ubi volume name by getting rid of additional properties
186 name=${elementProperties#*Name:}
187 name="${name%Character*}"
188 name="$(echo -e "${name}" | tr -d '[:space:]')"
189
190 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
191 mountdir="/media/${name}"
Saqib Khan76b7b832017-08-01 11:32:27 -0500192 if [ ! -d "${mountdir}" ]; then
193 mkdir -p "${mountdir}"
194 fi
195
Saqib Khan58c870d2017-07-26 23:48:20 -0500196 if [[ ${name} == pnor-ro* ]]
197 then
198 ubiblock --create /dev/ubi${pnor}_${element}
199 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
200 else
201 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
202 fi
203 fi
204 done
205}
206
Saqib Khand536f0d2017-08-02 12:48:12 -0500207update_symlinks() {
208 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
209 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
210 PNOR_RO_PREFIX="/media/pnor-ro-"
211 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
212 PNOR_RW_PREFIX="/media/pnor-rw-"
213 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
214 PNOR_PRSV="/media/pnor-prsv"
Saqib Khand2249d62017-08-17 11:40:40 -0500215 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
Saqib Khan94734082017-09-10 00:44:16 -0500216 PNOR_PATCH_LOCATION="/usr/local/share/pnor/"
Saqib Khand536f0d2017-08-02 12:48:12 -0500217
Saqib Khand2249d62017-08-17 11:40:40 -0500218 # Get a list of all active PNOR versions
219 data="$(ls -d ${PNOR_RO_PREFIX}*)"
220 IFS=$'\n' array=(${data})
Saqib Khand536f0d2017-08-02 12:48:12 -0500221
Saqib Khand2249d62017-08-17 11:40:40 -0500222 currentVersion=""
223 lowestPriority=255
224 for element in ${array[@]}; do
225 #Remove the PNOR_RO_PREFIX from the path to get version ID.
226 versionId="${element#${PNOR_RO_PREFIX}}"
227
228 # Get the priority of active versions from persistence files.
229 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
230 data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
231 priority="${data: -1}"
232 if [[ priority -le lowestPriority ]]; then
233 lowestPriority=${priority}
234 currentVersion=${versionId}
235 fi
Saqib Khand536f0d2017-08-02 12:48:12 -0500236 fi
237 done
238
Saqib Khand2249d62017-08-17 11:40:40 -0500239 # Return if no active version found
240 if [ -z $currentVersion ]; then
241 return 0;
242 fi
243
Saqib Khand536f0d2017-08-02 12:48:12 -0500244 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
245 mkdir -p "${PNOR_ACTIVE_PATH}"
246 fi
247
248 # If the RW or RO active links doesn't point to the version with
249 # lowest priority, then remove the symlink and create new ones.
250 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then
251 rm -f ${PNOR_RO_ACTIVE_PATH}
Saqib Khan94734082017-09-10 00:44:16 -0500252 rm -rf ${PNOR_PATCH_LOCATION}*
Saqib Khand536f0d2017-08-02 12:48:12 -0500253 ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
254 fi
255
256 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then
257 rm -f ${PNOR_RW_ACTIVE_PATH}
258 ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
259 fi
260
261 if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then
262 ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
263 fi
264}
265
Adriana Kobylake882f912017-07-06 10:31:59 -0500266case "$1" in
267 ubiattach)
268 attach_ubi
269 ;;
Adriana Kobylakf15e77c2017-07-06 11:26:42 -0500270 squashfsmount)
271 name="$2"
272 version="$3"
273 mount_squashfs
274 ;;
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500275 ubimount)
276 name="$2"
277 mount_ubi
278 ;;
Adriana Kobylake5764af2017-06-07 15:24:52 -0500279 ubiumount)
280 name="$2"
281 umount_ubi
282 ;;
Saqib Khan58c870d2017-07-26 23:48:20 -0500283 ubiremount)
284 remount_ubi
285 ;;
Saqib Khand536f0d2017-08-02 12:48:12 -0500286 updatesymlinks)
287 update_symlinks
288 ;;
Adriana Kobylake882f912017-07-06 10:31:59 -0500289 *)
290 echo "Invalid argument"
291 exit 1
292 ;;
293esac
294rc=$?
295if [ ${rc} -ne 0 ]; then
296 echo "$0: error ${rc}"
297 exit ${rc}
298fi