blob: 116b486de8fb0e99b56e495e9863ad6c89b0dc01 [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 Kobylak582bdea2017-08-22 14:51:18 -050040# Get all ubi devices on a specific mtd that match requested string
41findubi_onmtd() {
42 u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)"
43 u="${u%/name}"
44 u="${u##*/}"
45 echo "${u}"
46}
47
48# Make space on flash before creating new volumes. This can be enhanced
49# determine current flash usage. For now only keep a "keepmax" number of them
50ubi_remove_volumes()
51{
52 kernelname="$(fw_printenv -n kernelname)"
53 curversion="${kernelname##*-}"
54 rootubi="$(findrootubi)"
55
56 # Just keep max number of volumes before updating, don't delete the version
57 # the BMC is booted from, and when a version is identified to be deleted,
58 # delete both the rofs and kernel volumes for that version.
59 rmnames="$(findubi_onmtd "${name%-*}-" "${ro}")"
60 rmnames=(${rmnames})
61 ubicount="${#rmnames[@]}"
62 while [ ${ubicount} -ge ${keepmax} ]; do
63 # Loop through existing volumes and skip currently active ones
64 for (( index=0; index<${#rmnames[@]}; index++ )); do
65 rmname="${rmnames[${index}]}"
66 rmversion="${rmname##*-}"
67 rmubi="$(findubi "rofs-${rmversion}")"
68 if [[ ( "${rmubi}" != "${rootubi}" ) &&
69 ( "${rmname}" != "${kernelname}" ) ]]; then
70 ubi_remove "rofs-${rmversion}"
71 ubi_remove "kernel-${rmversion}"
Adriana Kobylakddc9dca2017-09-05 15:44:35 -050072 # Remove the kernel copy in alt if any
73 if [[ "${name}" =~ "kernel" ]]; then
74 ubi_remove "kernel-${rmversion}" "${ro}"
75 fi
Adriana Kobylak582bdea2017-08-22 14:51:18 -050076 break
77 fi
78 done
79 # Decrease count regardless to avoid an infinite loop
80 (( ubicount-- ))
81 done
82}
83
Adriana Kobylaka290eff2017-06-27 09:39:57 -050084ubi_rw() {
85 rwmtd="$(findmtd "${reqmtd}")"
86 rw="${rwmtd#mtd}"
87 ubidev="/dev/ubi${rw}"
88
Michael Tritz741aac12017-09-07 15:13:04 -050089 if [ "$(fw_printenv rwreset 2>/dev/null)" == "rwreset=true" ]; then
Adriana Kobylak582bdea2017-08-22 14:51:18 -050090 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -050091 fw_setenv rwreset
92 fi
93
Adriana Kobylaka290eff2017-06-27 09:39:57 -050094 # Create a ubi volume of size 4MB, that is the current size of the rwfs image
95 vol="$(findubi "${name}")"
96 if [ -z "${vol}" ]; then
97 ubimkvol "${ubidev}" -N "${name}" -s 4MiB
98 fi
99}
100
Adriana Kobylake9939492017-07-11 11:34:23 -0500101ubi_ro() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500102 keepmax=2 # Default 2 volumes per mtd
Adriana Kobylake9939492017-07-11 11:34:23 -0500103 romtd="$(findmtd "${reqmtd}")"
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500104 romtd2="$(findmtd "${reqmtd2}")"
105
106 if [ ! "${romtd}" == "${romtd2}" ]; then
107 # Request to use alternate mtd device, choose the non-root one
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500108 keepmax=1 # 1 volume on each of the requested mtds
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500109 rootmtd="$(findrootmtd)"
110 if [ "${rootmtd}" == "${romtd}" ]; then
111 romtd="${romtd2}"
112 fi
113 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500114 ro="${romtd#mtd}"
115 ubidev="/dev/ubi${ro}"
116
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500117 ubi_remove_volumes
118
Adriana Kobylake9939492017-07-11 11:34:23 -0500119 # Create a static ubi volume
120 # TODO Get the actual image size openbmc/openbmc#1840
121 vol="$(findubi "${name}")"
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500122 if [ ! -z "${vol}" ]; then
123 # Allow a duplicate kernel volume on the alt mtd
124 if [[ "${name}" =~ "kernel" ]]; then
125 vol="$(findubi_onmtd "${name%-*}-" "${ro}")"
126 fi
127 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500128 if [ -z "${vol}" ]; then
129 ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
130 vol="$(findubi "${name}")"
131 fi
132}
133
134# Squashfs images need a ubi block
135ubi_block() {
136 vol="$(findubi "${name}")"
137 ubidevid="${vol#ubi}"
138 block="/dev/ubiblock${ubidevid}"
139 if [ ! -e "$block" ]; then
140 ubiblock --create "/dev/ubi${ubidevid}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500141 if [ $? != 0 ]; then
142 echo "Unable to create ubiblock ${name}:${ubidevid}"
143 return 1
144 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500145 fi
146}
147
148ubi_updatevol() {
149 vol="$(findubi "${name}")"
150 ubidevid="${vol#ubi}"
151 img="/tmp/images/${version}/${imgfile}"
152 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500153 if [ $? != 0 ]; then
154 echo "Unable to update volume ${name}!"
155 return 1
156 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500157}
158
Michael Tritza694eed2017-07-13 16:26:15 -0500159ubi_remove() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500160 rmname="$1"
161 vol="$(findubi "${rmname}")"
Michael Tritza694eed2017-07-13 16:26:15 -0500162
163 if [ ! -z "$vol" ]; then
164 vol="${vol%_*}"
165
Saqib Khanf8d2a662017-09-07 13:57:43 -0500166 if grep -q "$rmname" /proc/mounts; then
167 mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2)
Michael Tritza694eed2017-07-13 16:26:15 -0500168 umount "$mountdir"
169 rm -r "$mountdir"
170 fi
171
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500172 ubirmvol "/dev/${vol}" -N "$rmname"
Michael Tritza694eed2017-07-13 16:26:15 -0500173 fi
174}
175
Saqib Khanbb93e642017-08-10 11:24:38 -0500176remount_ubi() {
Saqib Khand6d1c442017-09-06 23:24:40 -0500177 bmcmtd="$(findmtd "bmc")"
178 altbmcmtd="$(findmtd "alt-bmc")"
179 mtds="${bmcmtd: -1}","${altbmcmtd: -1}"
Saqib Khanbb93e642017-08-10 11:24:38 -0500180
181 IFS=',' read -r -a mtds <<< "$mtds"
182 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
183 for mtd in ${mtds[@]}; do
184 # Re-attach mtd device to ubi if not already done
185 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
186 # Get information on all ubi volumes
187 ubinfo=$(ubinfo -d ${mtd})
188 presentVolumes=${ubinfo##*:}
189 IFS=', ' read -r -a array <<< "$presentVolumes"
190 for element in ${array[@]}; do
191 elementProperties=$(ubinfo -d $mtd -n $element)
192 # Get ubi volume name by getting rid of additional properties
193 name=${elementProperties#*Name:}
194 name="${name%Character*}"
195 name="$(echo -e "${name}" | tr -d '[:space:]')"
196
Adriana Kobylak2c7c8d12017-08-21 11:00:56 -0500197 if [[ ${name} == rofs-* ]]; then
Saqib Khanbb93e642017-08-10 11:24:38 -0500198 mountdir="/media/${name}"
199 mkdir -p "${mountdir}"
200 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
201 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
202 fi
203 done
204 done
205}
206
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500207# Read the current env variable and set it on the alternate boot env
208copy_env_var_to_alt() {
209 varName=$1
210 value="$(fw_printenv -n "${varName}")"
211 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
212}
213
Michael Tritza694eed2017-07-13 16:26:15 -0500214ubi_setenv() {
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500215 variable=$1
Michael Tritz8863f702017-08-25 15:47:07 -0500216 if [[ "$variable" == *"="* ]]; then
217 varName="${variable%=*}"
218 value="${variable##*=}"
219 fw_setenv "$varName" "$value"
220 else
221 fw_setenv "$variable"
222 fi
Michael Tritza694eed2017-07-13 16:26:15 -0500223}
224
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500225mtd_write() {
226 flashmtd="$(findmtd "${reqmtd}")"
227 img="/tmp/images/${version}/${imgfile}"
228 flashcp -v ${img} /dev/${flashmtd}
229}
230
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500231backup_env_vars() {
232 copy_env_var_to_alt kernelname
233 copy_env_var_to_alt ubiblock
234 copy_env_var_to_alt root
235}
236
237update_env_vars() {
238 vol="$(findubi rofs-"${version}")"
239 if [ -z "${vol}" ]; then
240 return 1
241 fi
242 ubidevid="${vol#ubi}"
243 block="/dev/ubiblock${ubidevid}"
244 if [ ! -e "${block}" ]; then
245 return 1
246 fi
247 ubi_setenv "kernelname=kernel-${version}"
248 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
249 ubi_setenv "root=${block}"
250}
251
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500252# Create a copy in the alt mtd
253create_vol_in_alt() {
254 alt="alt-bmc"
255 altmtd="$(findmtd "${alt}")"
256 if [ ! -z "${altmtd}" ]; then
257 reqmtd="${alt}"
258 reqmtd2="${alt}"
259 ubi_ro
260 ubi_updatevol
261 fi
262}
263
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500264case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500265 mtduboot)
266 reqmtd="$2"
267 version="$3"
268 imgfile="image-u-boot"
269 mtd_write
270 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500271 ubirw)
272 reqmtd="$2"
273 name="$3"
274 ubi_rw
275 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500276 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500277 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
278 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500279 name="$3"
280 version="$4"
281 imgfile="image-rofs"
282 imgsize="16MiB"
283 ubi_ro
Adriana Kobylake9939492017-07-11 11:34:23 -0500284 ubi_updatevol
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500285 ubi_block
Adriana Kobylake9939492017-07-11 11:34:23 -0500286 ;;
287 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500288 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
289 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500290 name="$3"
291 version="$4"
292 imgfile="image-kernel"
293 imgsize="4MiB"
294 ubi_ro
295 ubi_updatevol
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500296 create_vol_in_alt
Adriana Kobylake9939492017-07-11 11:34:23 -0500297 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500298 ubiremove)
299 name="$2"
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500300 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -0500301 ;;
302 ubisetenv)
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500303 ubi_setenv "$2"
Michael Tritza694eed2017-07-13 16:26:15 -0500304 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500305 ubiremount)
306 remount_ubi
307 ;;
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500308 preupdate)
309 backup_env_vars
310 ;;
311 postupdate)
312 version="$2"
313 update_env_vars
314 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500315 *)
316 echo "Invalid argument"
317 exit 1
318 ;;
319esac
320rc=$?
321if [ ${rc} -ne 0 ]; then
322 echo "$0: error ${rc}"
323 exit ${rc}
324fi