blob: befd08678b2f7864285a28fae056f61d501a6cdd [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}")"
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 Kobylakfc4d0e72017-07-06 11:26:42 -050069
70 if [ ! -d "${mountdir}" ]; then
71 mkdir "${mountdir}"
72 fi
73
Adriana Kobylak3d29ad42017-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 Tritz82cad842017-08-08 13:35:17 -050077 ubimkvol "${ubidev}" -N "${name}" -s 24MiB --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 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 Kobylakfc4d0e72017-07-06 11:26:42 -050092 fi
93
Michael Tritz82cad842017-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 Kobylakfc4d0e72017-07-06 11:26:42 -0500106 fi
107}
108
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500109mount_ubi() {
110 pnormtd="$(findmtd pnor)"
111 pnor="${pnormtd#mtd}"
112 ubidev="/dev/ubi${pnor}"
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500113
114 if [[ "${name}" == "pnor-patch" ]]; then
115 mountdir="/usr/local/share/pnor"
116 else
117 mountdir="/media/${name}"
118 fi
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500119
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500120 if [[ "${name}" == "pnor-prsv" ]]; then
121 size="2MiB"
122 else
123 size="16MiB"
124 fi
125
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500126 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500127 mkdir -p "${mountdir}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500128 fi
129
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500130 vol="$(findubi "${name}")"
131 if [ -z "${vol}" ]; then
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500132 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500133 fi
134
135 if ! is_mounted "${name}"; then
136 mountdev="ubi${pnor}:${name}"
137 mount -t ubifs "${mountdev}" "${mountdir}"
138 fi
139}
140
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500141umount_ubi() {
142 pnormtd="$(findmtd pnor)"
143 pnor="${pnormtd#mtd}"
144 ubidev="/dev/ubi${pnor}"
145 mountdir="/media/${name}"
146
147 if is_mounted "${name}"; then
148 umount "${mountdir}"
149 fi
150
151 vol="$(findubi "${name}")"
152 if [ -n "${vol}" ]; then
153 ubirmvol "${ubidev}" -N "${name}"
154 fi
155
156 if [ -d "${mountdir}" ]; then
157 rm -r "${mountdir}"
158 fi
159}
160
Saqib Khanbea768c2017-07-26 23:48:20 -0500161remount_ubi() {
162 pnormtd="$(findmtd pnor)"
163 pnor="${pnormtd#mtd}"
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500164 pnordev="/dev/mtd${pnor}"
165
166 # Re-Attach the pnor mtd device to ubi
Saqib Khan688cbaa2017-08-07 12:51:48 -0500167 if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then
168 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
169 else
170 # Device not formatted as ubi.
171 return 0
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500172 fi
173
Saqib Khanbea768c2017-07-26 23:48:20 -0500174 # Get information on all ubi volumes
175 ubinfo=$(ubinfo -d ${pnor})
176 presentVolumes=${ubinfo##*:}
177 IFS=', ' read -r -a array <<< "$presentVolumes"
178 for element in ${array[@]};
179 do
180 elementProperties=$(ubinfo -d $pnor -n $element)
181 # Get ubi volume name by getting rid of additional properties
182 name=${elementProperties#*Name:}
183 name="${name%Character*}"
184 name="$(echo -e "${name}" | tr -d '[:space:]')"
185
186 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
187 mountdir="/media/${name}"
Saqib Khanbea979a2017-08-01 11:32:27 -0500188 if [ ! -d "${mountdir}" ]; then
189 mkdir -p "${mountdir}"
190 fi
191
Saqib Khanbea768c2017-07-26 23:48:20 -0500192 if [[ ${name} == pnor-ro* ]]
193 then
194 ubiblock --create /dev/ubi${pnor}_${element}
195 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
196 else
197 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
198 fi
199 fi
200 done
201}
202
Saqib Khanfa92e972017-08-02 12:48:12 -0500203update_symlinks() {
204 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
205 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
206 PNOR_RO_PREFIX="/media/pnor-ro-"
207 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
208 PNOR_RW_PREFIX="/media/pnor-rw-"
209 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
210 PNOR_PRSV="/media/pnor-prsv"
Saqib Khan405f71d2017-08-17 11:40:40 -0500211 PERSISTENCE_PATH="/var/lib/obmc/openpower-pnor-code-mgmt/"
Saqib Khanfa92e972017-08-02 12:48:12 -0500212
Saqib Khan405f71d2017-08-17 11:40:40 -0500213 # Get a list of all active PNOR versions
214 data="$(ls -d ${PNOR_RO_PREFIX}*)"
215 IFS=$'\n' array=(${data})
Saqib Khanfa92e972017-08-02 12:48:12 -0500216
Saqib Khan405f71d2017-08-17 11:40:40 -0500217 currentVersion=""
218 lowestPriority=255
219 for element in ${array[@]}; do
220 #Remove the PNOR_RO_PREFIX from the path to get version ID.
221 versionId="${element#${PNOR_RO_PREFIX}}"
222
223 # Get the priority of active versions from persistence files.
224 if [[ -f "${PERSISTENCE_PATH}${versionId}" ]]; then
225 data="$(grep -r "priority" ${PERSISTENCE_PATH}${versionId})"
226 priority="${data: -1}"
227 if [[ priority -le lowestPriority ]]; then
228 lowestPriority=${priority}
229 currentVersion=${versionId}
230 fi
Saqib Khanfa92e972017-08-02 12:48:12 -0500231 fi
232 done
233
Saqib Khan405f71d2017-08-17 11:40:40 -0500234 # Return if no active version found
235 if [ -z $currentVersion ]; then
236 return 0;
237 fi
238
Saqib Khanfa92e972017-08-02 12:48:12 -0500239 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
240 mkdir -p "${PNOR_ACTIVE_PATH}"
241 fi
242
243 # If the RW or RO active links doesn't point to the version with
244 # lowest priority, then remove the symlink and create new ones.
245 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then
246 rm -f ${PNOR_RO_ACTIVE_PATH}
247 ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
248 fi
249
250 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then
251 rm -f ${PNOR_RW_ACTIVE_PATH}
252 ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
253 fi
254
255 if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then
256 ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
257 fi
258}
259
Adriana Kobylake2538382017-07-06 10:31:59 -0500260case "$1" in
261 ubiattach)
262 attach_ubi
263 ;;
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -0500264 squashfsmount)
265 name="$2"
266 version="$3"
267 mount_squashfs
268 ;;
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500269 ubimount)
270 name="$2"
271 mount_ubi
272 ;;
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500273 ubiumount)
274 name="$2"
275 umount_ubi
276 ;;
Saqib Khanbea768c2017-07-26 23:48:20 -0500277 ubiremount)
278 remount_ubi
279 ;;
Saqib Khanfa92e972017-08-02 12:48:12 -0500280 updatesymlinks)
281 update_symlinks
282 ;;
Adriana Kobylake2538382017-07-06 10:31:59 -0500283 *)
284 echo "Invalid argument"
285 exit 1
286 ;;
287esac
288rc=$?
289if [ ${rc} -ne 0 ]; then
290 echo "$0: error ${rc}"
291 exit ${rc}
292fi