blob: 60e976be389ba7d57ea17340e83b4da2423ee856 [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 Tritza694eed2017-07-13 16:26:15 -050085 if [ "$(fw_printenv rwreset)" == "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
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500136 copy_env_var_to_alt ubiblock
137 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
138 copy_env_var_to_alt root
139 ubi_setenv "root=${block}"
Adriana Kobylake9939492017-07-11 11:34:23 -0500140}
141
142ubi_updatevol() {
143 vol="$(findubi "${name}")"
144 ubidevid="${vol#ubi}"
145 img="/tmp/images/${version}/${imgfile}"
146 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500147 if [ $? != 0 ]; then
148 echo "Unable to update volume ${name}!"
149 return 1
150 fi
151 if [[ "${name}" =~ "kernel" ]]; then
152 copy_env_var_to_alt kernelname
153 ubi_setenv "kernelname=${name}"
154 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500155}
156
Michael Tritza694eed2017-07-13 16:26:15 -0500157ubi_remove() {
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500158 rmname="$1"
159 vol="$(findubi "${rmname}")"
Michael Tritza694eed2017-07-13 16:26:15 -0500160
161 if [ ! -z "$vol" ]; then
162 vol="${vol%_*}"
163
Saqib Khanf8d2a662017-09-07 13:57:43 -0500164 if grep -q "$rmname" /proc/mounts; then
165 mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2)
Michael Tritza694eed2017-07-13 16:26:15 -0500166 umount "$mountdir"
167 rm -r "$mountdir"
168 fi
169
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500170 ubirmvol "/dev/${vol}" -N "$rmname"
Michael Tritza694eed2017-07-13 16:26:15 -0500171 fi
172}
173
Saqib Khanbb93e642017-08-10 11:24:38 -0500174remount_ubi() {
Saqib Khand6d1c442017-09-06 23:24:40 -0500175 bmcmtd="$(findmtd "bmc")"
176 altbmcmtd="$(findmtd "alt-bmc")"
177 mtds="${bmcmtd: -1}","${altbmcmtd: -1}"
Saqib Khanbb93e642017-08-10 11:24:38 -0500178
179 IFS=',' read -r -a mtds <<< "$mtds"
180 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
181 for mtd in ${mtds[@]}; do
182 # Re-attach mtd device to ubi if not already done
183 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
184 # Get information on all ubi volumes
185 ubinfo=$(ubinfo -d ${mtd})
186 presentVolumes=${ubinfo##*:}
187 IFS=', ' read -r -a array <<< "$presentVolumes"
188 for element in ${array[@]}; do
189 elementProperties=$(ubinfo -d $mtd -n $element)
190 # Get ubi volume name by getting rid of additional properties
191 name=${elementProperties#*Name:}
192 name="${name%Character*}"
193 name="$(echo -e "${name}" | tr -d '[:space:]')"
194
Adriana Kobylak2c7c8d12017-08-21 11:00:56 -0500195 if [[ ${name} == rofs-* ]]; then
Saqib Khanbb93e642017-08-10 11:24:38 -0500196 mountdir="/media/${name}"
197 mkdir -p "${mountdir}"
198 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
199 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
200 fi
201 done
202 done
203}
204
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500205# Read the current env variable and set it on the alternate boot env
206copy_env_var_to_alt() {
207 varName=$1
208 value="$(fw_printenv -n "${varName}")"
209 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
210}
211
Michael Tritza694eed2017-07-13 16:26:15 -0500212ubi_setenv() {
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500213 variable=$1
Michael Tritz8863f702017-08-25 15:47:07 -0500214 if [[ "$variable" == *"="* ]]; then
215 varName="${variable%=*}"
216 value="${variable##*=}"
217 fw_setenv "$varName" "$value"
218 else
219 fw_setenv "$variable"
220 fi
Michael Tritza694eed2017-07-13 16:26:15 -0500221}
222
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500223mtd_write() {
224 flashmtd="$(findmtd "${reqmtd}")"
225 img="/tmp/images/${version}/${imgfile}"
226 flashcp -v ${img} /dev/${flashmtd}
227}
228
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500229case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500230 mtduboot)
231 reqmtd="$2"
232 version="$3"
233 imgfile="image-u-boot"
234 mtd_write
235 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500236 ubirw)
237 reqmtd="$2"
238 name="$3"
239 ubi_rw
240 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500241 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500242 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
243 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500244 name="$3"
245 version="$4"
246 imgfile="image-rofs"
247 imgsize="16MiB"
248 ubi_ro
Adriana Kobylake9939492017-07-11 11:34:23 -0500249 ubi_updatevol
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500250 ubi_block
Adriana Kobylake9939492017-07-11 11:34:23 -0500251 ;;
252 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500253 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
254 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500255 name="$3"
256 version="$4"
257 imgfile="image-kernel"
258 imgsize="4MiB"
259 ubi_ro
260 ubi_updatevol
261 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500262 ubiremove)
263 name="$2"
Adriana Kobylak582bdea2017-08-22 14:51:18 -0500264 ubi_remove "${name}"
Michael Tritza694eed2017-07-13 16:26:15 -0500265 ;;
266 ubisetenv)
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500267 ubi_setenv "$2"
Michael Tritza694eed2017-07-13 16:26:15 -0500268 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500269 ubiremount)
270 remount_ubi
271 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500272 *)
273 echo "Invalid argument"
274 exit 1
275 ;;
276esac
277rc=$?
278if [ ${rc} -ne 0 ]; then
279 echo "$0: error ${rc}"
280 exit ${rc}
281fi