blob: d94f472ce5ad46ef467bbe34048987bbe1b847e7 [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}"
72 break
73 fi
74 done
75 # Decrease count regardless to avoid an infinite loop
76 (( ubicount-- ))
77 done
78}
79
Adriana Kobylaka290eff2017-06-27 09:39:57 -050080ubi_rw() {
81 rwmtd="$(findmtd "${reqmtd}")"
82 rw="${rwmtd#mtd}"
83 ubidev="/dev/ubi${rw}"
84
Michael Tritz741aac12017-09-07 15:13:04 -050085 if [ "$(fw_printenv rwreset 2>/dev/null)" == "rwreset=true" ]; then
Adriana Kobylak582bdea2017-08-22 14:51:18 -050086 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -050087 fw_setenv rwreset
88 fi
89
Adriana Kobylaka290eff2017-06-27 09:39:57 -050090 # Create a ubi volume of size 4MB, that is the current size of the rwfs image
91 vol="$(findubi "${name}")"
92 if [ -z "${vol}" ]; then
93 ubimkvol "${ubidev}" -N "${name}" -s 4MiB
94 fi
95}
96
Adriana Kobylake9939492017-07-11 11:34:23 -050097ubi_ro() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -050098 keepmax=2 # Default 2 volumes per mtd
Adriana Kobylake9939492017-07-11 11:34:23 -050099 romtd="$(findmtd "${reqmtd}")"
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500100 romtd2="$(findmtd "${reqmtd2}")"
101
102 if [ ! "${romtd}" == "${romtd2}" ]; then
103 # Request to use alternate mtd device, choose the non-root one
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500104 keepmax=1 # 1 volume on each of the requested mtds
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500105 rootmtd="$(findrootmtd)"
106 if [ "${rootmtd}" == "${romtd}" ]; then
107 romtd="${romtd2}"
108 fi
109 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500110 ro="${romtd#mtd}"
111 ubidev="/dev/ubi${ro}"
112
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500113 ubi_remove_volumes
114
Adriana Kobylake9939492017-07-11 11:34:23 -0500115 # Create a static ubi volume
116 # TODO Get the actual image size openbmc/openbmc#1840
117 vol="$(findubi "${name}")"
118 if [ -z "${vol}" ]; then
119 ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
120 vol="$(findubi "${name}")"
121 fi
122}
123
124# Squashfs images need a ubi block
125ubi_block() {
126 vol="$(findubi "${name}")"
127 ubidevid="${vol#ubi}"
128 block="/dev/ubiblock${ubidevid}"
129 if [ ! -e "$block" ]; then
130 ubiblock --create "/dev/ubi${ubidevid}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500131 if [ $? != 0 ]; then
132 echo "Unable to create ubiblock ${name}:${ubidevid}"
133 return 1
134 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500135 fi
136}
137
138ubi_updatevol() {
139 vol="$(findubi "${name}")"
140 ubidevid="${vol#ubi}"
141 img="/tmp/images/${version}/${imgfile}"
142 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500143 if [ $? != 0 ]; then
144 echo "Unable to update volume ${name}!"
145 return 1
146 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500147}
148
Michael Tritza694eed2017-07-13 16:26:15 -0500149ubi_remove() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500150 rmname="$1"
151 vol="$(findubi "${rmname}")"
Michael Tritza694eed2017-07-13 16:26:15 -0500152
153 if [ ! -z "$vol" ]; then
154 vol="${vol%_*}"
155
Saqib Khanf8d2a662017-09-07 13:57:43 -0500156 if grep -q "$rmname" /proc/mounts; then
157 mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2)
Michael Tritza694eed2017-07-13 16:26:15 -0500158 umount "$mountdir"
159 rm -r "$mountdir"
160 fi
161
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500162 ubirmvol "/dev/${vol}" -N "$rmname"
Michael Tritza694eed2017-07-13 16:26:15 -0500163 fi
164}
165
Saqib Khanbb93e642017-08-10 11:24:38 -0500166remount_ubi() {
Saqib Khand6d1c442017-09-06 23:24:40 -0500167 bmcmtd="$(findmtd "bmc")"
168 altbmcmtd="$(findmtd "alt-bmc")"
169 mtds="${bmcmtd: -1}","${altbmcmtd: -1}"
Saqib Khanbb93e642017-08-10 11:24:38 -0500170
171 IFS=',' read -r -a mtds <<< "$mtds"
172 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
173 for mtd in ${mtds[@]}; do
174 # Re-attach mtd device to ubi if not already done
175 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
176 # Get information on all ubi volumes
177 ubinfo=$(ubinfo -d ${mtd})
178 presentVolumes=${ubinfo##*:}
179 IFS=', ' read -r -a array <<< "$presentVolumes"
180 for element in ${array[@]}; do
181 elementProperties=$(ubinfo -d $mtd -n $element)
182 # Get ubi volume name by getting rid of additional properties
183 name=${elementProperties#*Name:}
184 name="${name%Character*}"
185 name="$(echo -e "${name}" | tr -d '[:space:]')"
186
Adriana Kobylak2c7c8d12017-08-21 11:00:56 -0500187 if [[ ${name} == rofs-* ]]; then
Saqib Khanbb93e642017-08-10 11:24:38 -0500188 mountdir="/media/${name}"
189 mkdir -p "${mountdir}"
190 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
191 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
192 fi
193 done
194 done
195}
196
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500197# Read the current env variable and set it on the alternate boot env
198copy_env_var_to_alt() {
199 varName=$1
200 value="$(fw_printenv -n "${varName}")"
201 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
202}
203
Michael Tritza694eed2017-07-13 16:26:15 -0500204ubi_setenv() {
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500205 variable=$1
Michael Tritz8863f702017-08-25 15:47:07 -0500206 if [[ "$variable" == *"="* ]]; then
207 varName="${variable%=*}"
208 value="${variable##*=}"
209 fw_setenv "$varName" "$value"
210 else
211 fw_setenv "$variable"
212 fi
Michael Tritza694eed2017-07-13 16:26:15 -0500213}
214
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500215mtd_write() {
216 flashmtd="$(findmtd "${reqmtd}")"
217 img="/tmp/images/${version}/${imgfile}"
218 flashcp -v ${img} /dev/${flashmtd}
219}
220
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500221backup_env_vars() {
222 copy_env_var_to_alt kernelname
223 copy_env_var_to_alt ubiblock
224 copy_env_var_to_alt root
225}
226
227update_env_vars() {
228 vol="$(findubi rofs-"${version}")"
229 if [ -z "${vol}" ]; then
230 return 1
231 fi
232 ubidevid="${vol#ubi}"
233 block="/dev/ubiblock${ubidevid}"
234 if [ ! -e "${block}" ]; then
235 return 1
236 fi
237 ubi_setenv "kernelname=kernel-${version}"
238 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
239 ubi_setenv "root=${block}"
240}
241
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500242case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500243 mtduboot)
244 reqmtd="$2"
245 version="$3"
246 imgfile="image-u-boot"
247 mtd_write
248 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500249 ubirw)
250 reqmtd="$2"
251 name="$3"
252 ubi_rw
253 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500254 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500255 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
256 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500257 name="$3"
258 version="$4"
259 imgfile="image-rofs"
260 imgsize="16MiB"
261 ubi_ro
Adriana Kobylake9939492017-07-11 11:34:23 -0500262 ubi_updatevol
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500263 ubi_block
Adriana Kobylake9939492017-07-11 11:34:23 -0500264 ;;
265 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500266 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
267 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500268 name="$3"
269 version="$4"
270 imgfile="image-kernel"
271 imgsize="4MiB"
272 ubi_ro
273 ubi_updatevol
274 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500275 ubiremove)
276 name="$2"
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500277 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -0500278 ;;
279 ubisetenv)
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500280 ubi_setenv "$2"
Michael Tritza694eed2017-07-13 16:26:15 -0500281 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500282 ubiremount)
283 remount_ubi
284 ;;
Adriana Kobylake2fd46d2017-09-05 12:38:19 -0500285 preupdate)
286 backup_env_vars
287 ;;
288 postupdate)
289 version="$2"
290 update_env_vars
291 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500292 *)
293 echo "Invalid argument"
294 exit 1
295 ;;
296esac
297rc=$?
298if [ ${rc} -ne 0 ]; then
299 echo "$0: error ${rc}"
300 exit ${rc}
301fi