blob: d0dca1c40bfc253a4ba461c6b43dc0bdc89503f5 [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}"
Saqib Khand793f2d2017-09-13 14:05:31 -0500199
200 if [ ! -d ${mountdir} ]; then
201 mkdir -p "${mountdir}"
202 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
203 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
204 fi
Saqib Khanbb93e642017-08-10 11:24:38 -0500205 fi
206 done
207 done
208}
209
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500210# Read the current env variable and set it on the alternate boot env
211copy_env_var_to_alt() {
212 varName=$1
213 value="$(fw_printenv -n "${varName}")"
214 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
215}
216
Michael Tritza694eed2017-07-13 16:26:15 -0500217ubi_setenv() {
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500218 variable=$1
Michael Tritz8863f702017-08-25 15:47:07 -0500219 if [[ "$variable" == *"="* ]]; then
220 varName="${variable%=*}"
221 value="${variable##*=}"
222 fw_setenv "$varName" "$value"
223 else
224 fw_setenv "$variable"
225 fi
Michael Tritza694eed2017-07-13 16:26:15 -0500226}
227
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500228mtd_write() {
229 flashmtd="$(findmtd "${reqmtd}")"
230 img="/tmp/images/${version}/${imgfile}"
231 flashcp -v ${img} /dev/${flashmtd}
232}
233
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500234backup_env_vars() {
235 copy_env_var_to_alt kernelname
236 copy_env_var_to_alt ubiblock
237 copy_env_var_to_alt root
238}
239
240update_env_vars() {
241 vol="$(findubi rofs-"${version}")"
242 if [ -z "${vol}" ]; then
243 return 1
244 fi
245 ubidevid="${vol#ubi}"
246 block="/dev/ubiblock${ubidevid}"
247 if [ ! -e "${block}" ]; then
248 return 1
249 fi
250 ubi_setenv "kernelname=kernel-${version}"
251 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
252 ubi_setenv "root=${block}"
253}
254
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500255# Create a copy in the alt mtd
256create_vol_in_alt() {
257 alt="alt-bmc"
258 altmtd="$(findmtd "${alt}")"
259 if [ ! -z "${altmtd}" ]; then
260 reqmtd="${alt}"
261 reqmtd2="${alt}"
262 ubi_ro
263 ubi_updatevol
264 fi
265}
266
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500267case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500268 mtduboot)
269 reqmtd="$2"
270 version="$3"
271 imgfile="image-u-boot"
272 mtd_write
273 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500274 ubirw)
275 reqmtd="$2"
276 name="$3"
277 ubi_rw
278 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500279 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500280 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
281 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500282 name="$3"
283 version="$4"
284 imgfile="image-rofs"
285 imgsize="16MiB"
286 ubi_ro
Adriana Kobylake9939492017-07-11 11:34:23 -0500287 ubi_updatevol
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500288 ubi_block
Adriana Kobylake9939492017-07-11 11:34:23 -0500289 ;;
290 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500291 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
292 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500293 name="$3"
294 version="$4"
295 imgfile="image-kernel"
296 imgsize="4MiB"
297 ubi_ro
298 ubi_updatevol
Adriana Kobylakddc9dca2017-09-05 15:44:35 -0500299 create_vol_in_alt
Adriana Kobylake9939492017-07-11 11:34:23 -0500300 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500301 ubiremove)
302 name="$2"
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500303 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -0500304 ;;
305 ubisetenv)
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500306 ubi_setenv "$2"
Michael Tritza694eed2017-07-13 16:26:15 -0500307 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500308 ubiremount)
309 remount_ubi
310 ;;
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500311 preupdate)
312 backup_env_vars
313 ;;
314 postupdate)
315 version="$2"
316 update_env_vars
317 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500318 *)
319 echo "Invalid argument"
320 exit 1
321 ;;
322esac
323rc=$?
324if [ ${rc} -ne 0 ]; then
325 echo "$0: error ${rc}"
326 exit ${rc}
327fi