blob: d8eed5732ab6de8e67d7a47585dfc19eecb68e9e [file] [log] [blame]
Adriana Kobylaka290eff2017-06-27 09:39:57 -05001#!/bin/sh
2
Adriana Kobylak54c9f792017-08-17 14:08:20 -05003# Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /"
4findrootmtd() {
5 rootmatch=" on / "
6 m="$(mount | grep "${rootmatch}" | grep "ubiblock")"
7 m="${m##*ubiblock}"
8 m="${m%_*}"
9 if [ -z "${m}" ]; then
10 # default to bmc mtd (0)
11 m=0
12 fi
13 echo "mtd${m}"
14}
15
Adriana Kobylak582bdea2017-08-22 14:51:18 -050016findrootubi() {
17 rootmatch=" on / "
18 m="$(mount | grep "${rootmatch}")"
19 m="${m##*ubiblock}"
20 m="${m% on*}"
21 echo "ubi${m}"
22}
23
Adriana Kobylaka290eff2017-06-27 09:39:57 -050024# Get the mtd device number (mtdX)
25findmtd() {
26 m="$(grep -xl "$1" /sys/class/mtd/*/name)"
27 m="${m%/name}"
28 m="${m##*/}"
29 echo "${m}"
30}
31
32# Get the ubi device number (ubiX_Y)
33findubi() {
34 u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
35 u="${u%/name}"
36 u="${u##*/}"
37 echo "${u}"
38}
39
Adriana Kobylakac5ce002017-09-13 12:59:26 -050040# Get the ubi device number (ubiX_Y) on a specific mtd
Adriana Kobylak582bdea2017-08-22 14:51:18 -050041findubi_onmtd() {
Adriana Kobylakac5ce002017-09-13 12:59:26 -050042 u="$(grep -xl "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
43 u="${u%/name}"
44 u="${u##*/}"
45 echo "${u}"
46}
47
48# Get all ubi device names on a specific mtd that match requested string
49findubiname_onmtd() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -050050 u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
51 u="${u%/name}"
52 u="${u##*/}"
53 echo "${u}"
54}
55
Adriana Kobylakac5ce002017-09-13 12:59:26 -050056# Get the name from the requested ubiX_Y volume
57findname() {
58 n="$(cat /sys/class/ubi/$1/name)"
59 echo "${n}"
60}
61
Adriana Kobylak582bdea2017-08-22 14:51:18 -050062# Make space on flash before creating new volumes. This can be enhanced
63# determine current flash usage. For now only keep a "keepmax" number of them
64ubi_remove_volumes()
65{
Adriana Kobylak582bdea2017-08-22 14:51:18 -050066 rootubi="$(findrootubi)"
Adriana Kobylakac5ce002017-09-13 12:59:26 -050067 rootname="$(findname "${rootubi}")"
68 rootversion="${rootname##*-}"
69 rootkernel="kernel-${rootversion}"
Adriana Kobylak582bdea2017-08-22 14:51:18 -050070
71 # Just keep max number of volumes before updating, don't delete the version
72 # the BMC is booted from, and when a version is identified to be deleted,
73 # delete both the rofs and kernel volumes for that version.
Adriana Kobylakac5ce002017-09-13 12:59:26 -050074 rmnames="$(findubiname_onmtd "${name%-*}-" "${ro}")"
Adriana Kobylak582bdea2017-08-22 14:51:18 -050075 rmnames=(${rmnames})
76 ubicount="${#rmnames[@]}"
77 while [ ${ubicount} -ge ${keepmax} ]; do
78 # Loop through existing volumes and skip currently active ones
79 for (( index=0; index<${#rmnames[@]}; index++ )); do
80 rmname="${rmnames[${index}]}"
81 rmversion="${rmname##*-}"
Adriana Kobylakac5ce002017-09-13 12:59:26 -050082 [ "${rmversion}" == "${version}" ] && continue
83 rmubi="$(findubi_onmtd "rofs-${rmversion}" "${ro}")"
Adriana Kobylak582bdea2017-08-22 14:51:18 -050084 if [[ ( "${rmubi}" != "${rootubi}" ) &&
Adriana Kobylakac5ce002017-09-13 12:59:26 -050085 ( "${rmname}" != "${rootkernel}" ) ]]; then
86 ubi_remove "rofs-${rmversion}" "${ro}"
87 ubi_remove "kernel-${rmversion}" "${ro}"
88 # Remove priority value
89 fw_setenv "${rmversion}"
Adriana Kobylak582bdea2017-08-22 14:51:18 -050090 break
91 fi
92 done
93 # Decrease count regardless to avoid an infinite loop
94 (( ubicount-- ))
95 done
96}
97
Adriana Kobylaka290eff2017-06-27 09:39:57 -050098ubi_rw() {
99 rwmtd="$(findmtd "${reqmtd}")"
100 rw="${rwmtd#mtd}"
101 ubidev="/dev/ubi${rw}"
102
Michael Tritz741aac12017-09-07 15:13:04 -0500103 if [ "$(fw_printenv rwreset 2>/dev/null)" == "rwreset=true" ]; then
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500104 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -0500105 fw_setenv rwreset
106 fi
107
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500108 # Create a ubi volume of size 4MB, that is the current size of the rwfs image
109 vol="$(findubi "${name}")"
110 if [ -z "${vol}" ]; then
111 ubimkvol "${ubidev}" -N "${name}" -s 4MiB
112 fi
113}
114
Adriana Kobylake9939492017-07-11 11:34:23 -0500115ubi_ro() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500116 keepmax=2 # Default 2 volumes per mtd
Adriana Kobylake9939492017-07-11 11:34:23 -0500117 romtd="$(findmtd "${reqmtd}")"
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500118 romtd2="$(findmtd "${reqmtd2}")"
119
120 if [ ! "${romtd}" == "${romtd2}" ]; then
121 # Request to use alternate mtd device, choose the non-root one
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500122 keepmax=1 # 1 volume on each of the requested mtds
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500123 rootmtd="$(findrootmtd)"
124 if [ "${rootmtd}" == "${romtd}" ]; then
125 romtd="${romtd2}"
126 fi
127 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500128 ro="${romtd#mtd}"
129 ubidev="/dev/ubi${ro}"
130
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500131 ubi_remove_volumes
132
Adriana Kobylake9939492017-07-11 11:34:23 -0500133 # Create a static ubi volume
134 # TODO Get the actual image size openbmc/openbmc#1840
135 vol="$(findubi "${name}")"
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500136 if [ ! -z "${vol}" ]; then
137 # Allow a duplicate kernel volume on the alt mtd
138 if [[ "${name}" =~ "kernel" ]]; then
Adriana Kobylakac5ce002017-09-13 12:59:26 -0500139 vol="$(findubi_onmtd "${name}" "${ro}")"
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500140 fi
141 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500142 if [ -z "${vol}" ]; then
143 ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
144 vol="$(findubi "${name}")"
145 fi
146}
147
148# Squashfs images need a ubi block
149ubi_block() {
150 vol="$(findubi "${name}")"
151 ubidevid="${vol#ubi}"
152 block="/dev/ubiblock${ubidevid}"
153 if [ ! -e "$block" ]; then
154 ubiblock --create "/dev/ubi${ubidevid}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500155 if [ $? != 0 ]; then
156 echo "Unable to create ubiblock ${name}:${ubidevid}"
157 return 1
158 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500159 fi
160}
161
162ubi_updatevol() {
163 vol="$(findubi "${name}")"
164 ubidevid="${vol#ubi}"
165 img="/tmp/images/${version}/${imgfile}"
166 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500167 if [ $? != 0 ]; then
168 echo "Unable to update volume ${name}!"
169 return 1
170 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500171}
172
Michael Tritza694eed2017-07-13 16:26:15 -0500173ubi_remove() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500174 rmname="$1"
Adriana Kobylakac5ce002017-09-13 12:59:26 -0500175 rmmtd="$2"
176 if [ ! -z "${rmmtd}" ]; then
177 vol="$(findubi_onmtd "${rmname}" "${rmmtd}")"
178 else
179 vol="$(findubi "${rmname}")"
180 fi
Michael Tritza694eed2017-07-13 16:26:15 -0500181
182 if [ ! -z "$vol" ]; then
183 vol="${vol%_*}"
184
Saqib Khanf8d2a662017-09-07 13:57:43 -0500185 if grep -q "$rmname" /proc/mounts; then
186 mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2)
Michael Tritza694eed2017-07-13 16:26:15 -0500187 umount "$mountdir"
188 rm -r "$mountdir"
189 fi
190
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500191 ubirmvol "/dev/${vol}" -N "$rmname"
Michael Tritza694eed2017-07-13 16:26:15 -0500192 fi
193}
194
Saqib Khanbb93e642017-08-10 11:24:38 -0500195remount_ubi() {
Saqib Khand6d1c442017-09-06 23:24:40 -0500196 bmcmtd="$(findmtd "bmc")"
197 altbmcmtd="$(findmtd "alt-bmc")"
198 mtds="${bmcmtd: -1}","${altbmcmtd: -1}"
Saqib Khanbb93e642017-08-10 11:24:38 -0500199
200 IFS=',' read -r -a mtds <<< "$mtds"
201 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
202 for mtd in ${mtds[@]}; do
203 # Re-attach mtd device to ubi if not already done
204 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
205 # Get information on all ubi volumes
206 ubinfo=$(ubinfo -d ${mtd})
207 presentVolumes=${ubinfo##*:}
208 IFS=', ' read -r -a array <<< "$presentVolumes"
209 for element in ${array[@]}; do
210 elementProperties=$(ubinfo -d $mtd -n $element)
211 # Get ubi volume name by getting rid of additional properties
212 name=${elementProperties#*Name:}
213 name="${name%Character*}"
214 name="$(echo -e "${name}" | tr -d '[:space:]')"
215
Adriana Kobylak2c7c8d12017-08-21 11:00:56 -0500216 if [[ ${name} == rofs-* ]]; then
Saqib Khanbb93e642017-08-10 11:24:38 -0500217 mountdir="/media/${name}"
Saqib Khand793f2d2017-09-13 14:05:31 -0500218
219 if [ ! -d ${mountdir} ]; then
220 mkdir -p "${mountdir}"
221 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
222 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
223 fi
Saqib Khanbb93e642017-08-10 11:24:38 -0500224 fi
225 done
226 done
227}
228
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500229# Read the current env variable and set it on the alternate boot env
230copy_env_var_to_alt() {
231 varName=$1
232 value="$(fw_printenv -n "${varName}")"
233 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
234}
235
Michael Tritza694eed2017-07-13 16:26:15 -0500236ubi_setenv() {
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500237 variable=$1
Michael Tritz8863f702017-08-25 15:47:07 -0500238 if [[ "$variable" == *"="* ]]; then
239 varName="${variable%=*}"
240 value="${variable##*=}"
241 fw_setenv "$varName" "$value"
242 else
243 fw_setenv "$variable"
244 fi
Michael Tritza694eed2017-07-13 16:26:15 -0500245}
246
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500247mtd_write() {
248 flashmtd="$(findmtd "${reqmtd}")"
249 img="/tmp/images/${version}/${imgfile}"
250 flashcp -v ${img} /dev/${flashmtd}
251}
252
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500253backup_env_vars() {
254 copy_env_var_to_alt kernelname
255 copy_env_var_to_alt ubiblock
256 copy_env_var_to_alt root
257}
258
259update_env_vars() {
260 vol="$(findubi rofs-"${version}")"
261 if [ -z "${vol}" ]; then
262 return 1
263 fi
264 ubidevid="${vol#ubi}"
265 block="/dev/ubiblock${ubidevid}"
266 if [ ! -e "${block}" ]; then
267 return 1
268 fi
269 ubi_setenv "kernelname=kernel-${version}"
270 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
271 ubi_setenv "root=${block}"
272}
273
Saqib Khandfc9b0c2017-09-08 10:10:08 -0500274#TODO: Replace the implementation with systemd-inhibitors lock
275# once systemd/systemd#949 is resolved
276rebootguardenable() {
277 dir="/run/systemd/system/"
278 file="reboot-guard.conf"
279 units=("reboot" "poweroff" "halt")
280
281 for unit in "${units[@]}"; do
282 mkdir -p ${dir}${unit}.target.d
283 echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file}
284 done
285}
286
287#TODO: Replace the implementation with systemd-inhibitors lock
288# once systemd/systemd#949 is resolved
289rebootguarddisable() {
290 dir="/run/systemd/system/"
291 file="reboot-guard.conf"
292 units=("reboot" "poweroff" "halt")
293
294 for unit in "${units[@]}"; do
295 rm -rf ${dir}${unit}.target.d/${file}
296 done
297}
298
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500299# Create a copy in the alt mtd
300create_vol_in_alt() {
301 alt="alt-bmc"
302 altmtd="$(findmtd "${alt}")"
303 if [ ! -z "${altmtd}" ]; then
304 reqmtd="${alt}"
305 reqmtd2="${alt}"
306 ubi_ro
307 ubi_updatevol
308 fi
309}
310
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500311case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500312 mtduboot)
313 reqmtd="$2"
314 version="$3"
315 imgfile="image-u-boot"
316 mtd_write
317 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500318 ubirw)
319 reqmtd="$2"
320 name="$3"
321 ubi_rw
322 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500323 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500324 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
325 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500326 name="$3"
327 version="$4"
328 imgfile="image-rofs"
329 imgsize="16MiB"
330 ubi_ro
Adriana Kobylake9939492017-07-11 11:34:23 -0500331 ubi_updatevol
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500332 ubi_block
Adriana Kobylake9939492017-07-11 11:34:23 -0500333 ;;
334 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500335 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
336 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500337 name="$3"
338 version="$4"
339 imgfile="image-kernel"
340 imgsize="4MiB"
341 ubi_ro
342 ubi_updatevol
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500343 create_vol_in_alt
Adriana Kobylake9939492017-07-11 11:34:23 -0500344 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500345 ubiremove)
346 name="$2"
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500347 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -0500348 ;;
349 ubisetenv)
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500350 ubi_setenv "$2"
Michael Tritza694eed2017-07-13 16:26:15 -0500351 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500352 ubiremount)
353 remount_ubi
354 ;;
Saqib Khan7e4a3f22017-09-12 09:23:28 -0500355 createenvbackup)
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500356 backup_env_vars
357 ;;
Saqib Khan7e4a3f22017-09-12 09:23:28 -0500358 updateubootvars)
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500359 version="$2"
360 update_env_vars
361 ;;
Saqib Khandfc9b0c2017-09-08 10:10:08 -0500362 rebootguardenable)
363 rebootguardenable
364 ;;
365 rebootguarddisable)
366 rebootguarddisable
367 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500368 *)
369 echo "Invalid argument"
370 exit 1
371 ;;
372esac
373rc=$?
374if [ ${rc} -ne 0 ]; then
375 echo "$0: error ${rc}"
376 exit ${rc}
377fi