blob: b365b79b64a56ceaa29886d484dfc6967c27a7de [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)"
57 pnor="${pnormtd#mtd}"
58 ubidev="/dev/ubi${pnor}"
59 mountdir="/media/${name}"
60
61 if [ ! -d "${mountdir}" ]; then
62 mkdir "${mountdir}"
63 fi
64
Adriana Kobylak3d29ad42017-07-18 12:24:24 -050065 # Create a static ubi volume of arbitrary size 24MB,
66 # the current pnor image is ~19MB
67 # TODO Set size based on file size openbmc/openbmc#1840
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050068 vol="$(findubi "${name}")"
69 if [ -z "${vol}" ]; then
Adriana Kobylak3d29ad42017-07-18 12:24:24 -050070 ubimkvol "${ubidev}" -N "${name}" -s 24MiB --type=static
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -050071 vol="$(findubi "${name}")"
72 fi
73
74 # Create a ubi block needed for read-only volumes,
75 # and update the volume with the pnor squashfs image
76 ubidevid="${vol#ubi}"
77 block="/dev/ubiblock${ubidevid}"
78 if [ ! -e "$block" ]; then
79 img="/tmp/images/${version}/pnor.xz.squashfs"
80 ubiblock --create "/dev/ubi${ubidevid}"
81 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
82 fi
83
84 if ! is_mounted "${name}"; then
85 mount -t squashfs -o ro "${block}" "${mountdir}"
86 fi
87}
88
Adriana Kobylak77da88c2017-07-06 11:29:46 -050089mount_ubi() {
90 pnormtd="$(findmtd pnor)"
91 pnor="${pnormtd#mtd}"
92 ubidev="/dev/ubi${pnor}"
Adriana Kobylakd8b85752017-07-12 13:52:22 -050093
94 if [[ "${name}" == "pnor-patch" ]]; then
95 mountdir="/usr/local/share/pnor"
96 else
97 mountdir="/media/${name}"
98 fi
Adriana Kobylak77da88c2017-07-06 11:29:46 -050099
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500100 if [[ "${name}" == "pnor-prsv" ]]; then
101 size="2MiB"
102 else
103 size="16MiB"
104 fi
105
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500106 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakd8b85752017-07-12 13:52:22 -0500107 mkdir -p "${mountdir}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500108 fi
109
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500110 vol="$(findubi "${name}")"
111 if [ -z "${vol}" ]; then
Adriana Kobylak3d29ad42017-07-18 12:24:24 -0500112 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500113 fi
114
115 if ! is_mounted "${name}"; then
116 mountdev="ubi${pnor}:${name}"
117 mount -t ubifs "${mountdev}" "${mountdir}"
118 fi
119}
120
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500121umount_ubi() {
122 pnormtd="$(findmtd pnor)"
123 pnor="${pnormtd#mtd}"
124 ubidev="/dev/ubi${pnor}"
125 mountdir="/media/${name}"
126
127 if is_mounted "${name}"; then
128 umount "${mountdir}"
129 fi
130
131 vol="$(findubi "${name}")"
132 if [ -n "${vol}" ]; then
133 ubirmvol "${ubidev}" -N "${name}"
134 fi
135
136 if [ -d "${mountdir}" ]; then
137 rm -r "${mountdir}"
138 fi
139}
140
Saqib Khanbea768c2017-07-26 23:48:20 -0500141remount_ubi() {
142 pnormtd="$(findmtd pnor)"
143 pnor="${pnormtd#mtd}"
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500144 pnordev="/dev/mtd${pnor}"
145
146 # Re-Attach the pnor mtd device to ubi
Saqib Khan688cbaa2017-08-07 12:51:48 -0500147 if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then
148 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
149 else
150 # Device not formatted as ubi.
151 return 0
Saqib Khanbc0d3ed2017-08-01 09:46:22 -0500152 fi
153
Saqib Khanbea768c2017-07-26 23:48:20 -0500154 # Get information on all ubi volumes
155 ubinfo=$(ubinfo -d ${pnor})
156 presentVolumes=${ubinfo##*:}
157 IFS=', ' read -r -a array <<< "$presentVolumes"
158 for element in ${array[@]};
159 do
160 elementProperties=$(ubinfo -d $pnor -n $element)
161 # Get ubi volume name by getting rid of additional properties
162 name=${elementProperties#*Name:}
163 name="${name%Character*}"
164 name="$(echo -e "${name}" | tr -d '[:space:]')"
165
166 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
167 mountdir="/media/${name}"
Saqib Khanbea979a2017-08-01 11:32:27 -0500168 if [ ! -d "${mountdir}" ]; then
169 mkdir -p "${mountdir}"
170 fi
171
Saqib Khanbea768c2017-07-26 23:48:20 -0500172 if [[ ${name} == pnor-ro* ]]
173 then
174 ubiblock --create /dev/ubi${pnor}_${element}
175 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
176 else
177 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
178 fi
179 fi
180 done
181}
182
Saqib Khanfa92e972017-08-02 12:48:12 -0500183update_symlinks() {
184 PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/"
185 PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro"
186 PNOR_RO_PREFIX="/media/pnor-ro-"
187 PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw"
188 PNOR_RW_PREFIX="/media/pnor-rw-"
189 PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv"
190 PNOR_PRSV="/media/pnor-prsv"
191
192 # Get the current PNOR version using the priorities stored in
193 # the persistence storage files.
194 persistencePath="/var/lib/obmc/openpower-pnor-code-mgmt/"
195 data="$(grep -r "priority" ${persistencePath})"
196 if [[ -z "$data" ]]; then
197 return 0;
198 fi
199 IFS=$'\n' array=(${data})
200 for element in ${array[@]};
201 do
202 element="${element#$persistencePath}"
203 version="$( cut -d ':' -f 1 <<< "$element" )";
204 priority="${element: -1}"
205
206 if [[ priority -le lowestPriority ]]; then
207 lowestPriority=${priority}
208 currentVersion=${version}
209 fi
210 done
211
212 if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then
213 mkdir -p "${PNOR_ACTIVE_PATH}"
214 fi
215
216 # If the RW or RO active links doesn't point to the version with
217 # lowest priority, then remove the symlink and create new ones.
218 if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion} ]]; then
219 rm -f ${PNOR_RO_ACTIVE_PATH}
220 ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH}
221 fi
222
223 if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion} ]]; then
224 rm -f ${PNOR_RW_ACTIVE_PATH}
225 ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH}
226 fi
227
228 if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH} ]]; then
229 ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH}
230 fi
231}
232
Adriana Kobylake2538382017-07-06 10:31:59 -0500233case "$1" in
234 ubiattach)
235 attach_ubi
236 ;;
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -0500237 squashfsmount)
238 name="$2"
239 version="$3"
240 mount_squashfs
241 ;;
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500242 ubimount)
243 name="$2"
244 mount_ubi
245 ;;
Adriana Kobylakee51eb32017-06-07 15:24:52 -0500246 ubiumount)
247 name="$2"
248 umount_ubi
249 ;;
Saqib Khanbea768c2017-07-26 23:48:20 -0500250 ubiremount)
251 remount_ubi
252 ;;
Saqib Khanfa92e972017-08-02 12:48:12 -0500253 updatesymlinks)
254 update_symlinks
255 ;;
Adriana Kobylake2538382017-07-06 10:31:59 -0500256 *)
257 echo "Invalid argument"
258 exit 1
259 ;;
260esac
261rc=$?
262if [ ${rc} -ne 0 ]; then
263 echo "$0: error ${rc}"
264 exit ${rc}
265fi