blob: c6879cf7932b9839cd8bd66240969e8890c4b611 [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 Kobylaka290eff2017-06-27 09:39:57 -050016# Get the mtd device number (mtdX)
17findmtd() {
18 m="$(grep -xl "$1" /sys/class/mtd/*/name)"
19 m="${m%/name}"
20 m="${m##*/}"
21 echo "${m}"
22}
23
24# Get the ubi device number (ubiX_Y)
25findubi() {
26 u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
27 u="${u%/name}"
28 u="${u##*/}"
29 echo "${u}"
30}
31
32ubi_rw() {
33 rwmtd="$(findmtd "${reqmtd}")"
34 rw="${rwmtd#mtd}"
35 ubidev="/dev/ubi${rw}"
36
Michael Tritza694eed2017-07-13 16:26:15 -050037 if [ "$(fw_printenv rwreset)" == "rwreset=true" ]; then
38 ubi_remove
39 fw_setenv rwreset
40 fi
41
Adriana Kobylaka290eff2017-06-27 09:39:57 -050042 # Create a ubi volume of size 4MB, that is the current size of the rwfs image
43 vol="$(findubi "${name}")"
44 if [ -z "${vol}" ]; then
45 ubimkvol "${ubidev}" -N "${name}" -s 4MiB
46 fi
47}
48
Adriana Kobylake9939492017-07-11 11:34:23 -050049ubi_ro() {
50 romtd="$(findmtd "${reqmtd}")"
Adriana Kobylak54c9f792017-08-17 14:08:20 -050051 romtd2="$(findmtd "${reqmtd2}")"
52
53 if [ ! "${romtd}" == "${romtd2}" ]; then
54 # Request to use alternate mtd device, choose the non-root one
55 rootmtd="$(findrootmtd)"
56 if [ "${rootmtd}" == "${romtd}" ]; then
57 romtd="${romtd2}"
58 fi
59 fi
Adriana Kobylake9939492017-07-11 11:34:23 -050060 ro="${romtd#mtd}"
61 ubidev="/dev/ubi${ro}"
62
63 # Create a static ubi volume
64 # TODO Get the actual image size openbmc/openbmc#1840
65 vol="$(findubi "${name}")"
66 if [ -z "${vol}" ]; then
67 ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
68 vol="$(findubi "${name}")"
69 fi
70}
71
72# Squashfs images need a ubi block
73ubi_block() {
74 vol="$(findubi "${name}")"
75 ubidevid="${vol#ubi}"
76 block="/dev/ubiblock${ubidevid}"
77 if [ ! -e "$block" ]; then
78 ubiblock --create "/dev/ubi${ubidevid}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -050079 if [ $? != 0 ]; then
80 echo "Unable to create ubiblock ${name}:${ubidevid}"
81 return 1
82 fi
Adriana Kobylake9939492017-07-11 11:34:23 -050083 fi
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -050084 copy_env_var_to_alt ubiblock
85 ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')"
86 copy_env_var_to_alt root
87 ubi_setenv "root=${block}"
Adriana Kobylake9939492017-07-11 11:34:23 -050088}
89
90ubi_updatevol() {
91 vol="$(findubi "${name}")"
92 ubidevid="${vol#ubi}"
93 img="/tmp/images/${version}/${imgfile}"
94 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -050095 if [ $? != 0 ]; then
96 echo "Unable to update volume ${name}!"
97 return 1
98 fi
99 if [[ "${name}" =~ "kernel" ]]; then
100 copy_env_var_to_alt kernelname
101 ubi_setenv "kernelname=${name}"
102 fi
Adriana Kobylake9939492017-07-11 11:34:23 -0500103}
104
Michael Tritza694eed2017-07-13 16:26:15 -0500105ubi_remove() {
106 vol="$(findubi "${name}")"
107
108 if [ ! -z "$vol" ]; then
109 vol="${vol%_*}"
110
111 if grep -q "${vol}:$name" /proc/mounts; then
112 mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2)
113 umount "$mountdir"
114 rm -r "$mountdir"
115 fi
116
117 ubirmvol "/dev/${vol}" -N "$name"
118 fi
119}
120
Saqib Khanbb93e642017-08-10 11:24:38 -0500121remount_ubi() {
122 # Get a list of all devices formatted as UBI
123 for file in /dev/mtd*; do
124 if [[ $(hexdump -C -n 3 $file) =~ "UBI" ]]; then
125 mtd="${file: -1}"
126 if [[ $mtd =~ ^-?[0-9]+$ ]]; then
127 mtds=${mtd},${mtds}
128 fi
129 fi
130 done
131
132 IFS=',' read -r -a mtds <<< "$mtds"
133 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
134 for mtd in ${mtds[@]}; do
135 # Re-attach mtd device to ubi if not already done
136 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
137 # Get information on all ubi volumes
138 ubinfo=$(ubinfo -d ${mtd})
139 presentVolumes=${ubinfo##*:}
140 IFS=', ' read -r -a array <<< "$presentVolumes"
141 for element in ${array[@]}; do
142 elementProperties=$(ubinfo -d $mtd -n $element)
143 # Get ubi volume name by getting rid of additional properties
144 name=${elementProperties#*Name:}
145 name="${name%Character*}"
146 name="$(echo -e "${name}" | tr -d '[:space:]')"
147
Adriana Kobylak2c7c8d12017-08-21 11:00:56 -0500148 if [[ ${name} == rofs-* ]]; then
Saqib Khanbb93e642017-08-10 11:24:38 -0500149 mountdir="/media/${name}"
150 mkdir -p "${mountdir}"
151 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
152 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
153 fi
154 done
155 done
156}
157
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500158# Read the current env variable and set it on the alternate boot env
159copy_env_var_to_alt() {
160 varName=$1
161 value="$(fw_printenv -n "${varName}")"
162 fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}"
163}
164
Michael Tritza694eed2017-07-13 16:26:15 -0500165ubi_setenv() {
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500166 variable=$1
Adriana Kobylak9d16da62017-08-22 14:04:46 -0500167 varName="${variable%=*}"
168 value="${variable##*=}"
Michael Tritza694eed2017-07-13 16:26:15 -0500169 fw_setenv "$varName" "$value"
170}
171
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500172mtd_write() {
173 flashmtd="$(findmtd "${reqmtd}")"
174 img="/tmp/images/${version}/${imgfile}"
175 flashcp -v ${img} /dev/${flashmtd}
176}
177
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500178case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500179 mtduboot)
180 reqmtd="$2"
181 version="$3"
182 imgfile="image-u-boot"
183 mtd_write
184 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500185 ubirw)
186 reqmtd="$2"
187 name="$3"
188 ubi_rw
189 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500190 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500191 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
192 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500193 name="$3"
194 version="$4"
195 imgfile="image-rofs"
196 imgsize="16MiB"
197 ubi_ro
Adriana Kobylake9939492017-07-11 11:34:23 -0500198 ubi_updatevol
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500199 ubi_block
Adriana Kobylake9939492017-07-11 11:34:23 -0500200 ;;
201 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500202 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
203 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500204 name="$3"
205 version="$4"
206 imgfile="image-kernel"
207 imgsize="4MiB"
208 ubi_ro
209 ubi_updatevol
210 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500211 ubiremove)
212 name="$2"
213 ubi_remove
214 ;;
215 ubisetenv)
Adriana Kobylak8cdb82e2017-08-22 14:18:39 -0500216 ubi_setenv "$2"
Michael Tritza694eed2017-07-13 16:26:15 -0500217 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500218 ubiremount)
219 remount_ubi
220 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500221 *)
222 echo "Invalid argument"
223 exit 1
224 ;;
225esac
226rc=$?
227if [ ${rc} -ne 0 ]; then
228 echo "$0: error ${rc}"
229 exit ${rc}
230fi