blob: c4522259737dcc0a9d4b1f6641aa322752a51019 [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}"
79 fi
80}
81
82ubi_updatevol() {
83 vol="$(findubi "${name}")"
84 ubidevid="${vol#ubi}"
85 img="/tmp/images/${version}/${imgfile}"
86 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
87}
88
Michael Tritza694eed2017-07-13 16:26:15 -050089ubi_remove() {
90 vol="$(findubi "${name}")"
91
92 if [ ! -z "$vol" ]; then
93 vol="${vol%_*}"
94
95 if grep -q "${vol}:$name" /proc/mounts; then
96 mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2)
97 umount "$mountdir"
98 rm -r "$mountdir"
99 fi
100
101 ubirmvol "/dev/${vol}" -N "$name"
102 fi
103}
104
Saqib Khanbb93e642017-08-10 11:24:38 -0500105remount_ubi() {
106 # Get a list of all devices formatted as UBI
107 for file in /dev/mtd*; do
108 if [[ $(hexdump -C -n 3 $file) =~ "UBI" ]]; then
109 mtd="${file: -1}"
110 if [[ $mtd =~ ^-?[0-9]+$ ]]; then
111 mtds=${mtd},${mtds}
112 fi
113 fi
114 done
115
116 IFS=',' read -r -a mtds <<< "$mtds"
117 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
118 for mtd in ${mtds[@]}; do
119 # Re-attach mtd device to ubi if not already done
120 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
121 # Get information on all ubi volumes
122 ubinfo=$(ubinfo -d ${mtd})
123 presentVolumes=${ubinfo##*:}
124 IFS=', ' read -r -a array <<< "$presentVolumes"
125 for element in ${array[@]}; do
126 elementProperties=$(ubinfo -d $mtd -n $element)
127 # Get ubi volume name by getting rid of additional properties
128 name=${elementProperties#*Name:}
129 name="${name%Character*}"
130 name="$(echo -e "${name}" | tr -d '[:space:]')"
131
Adriana Kobylak2c7c8d12017-08-21 11:00:56 -0500132 if [[ ${name} == rofs-* ]]; then
Saqib Khanbb93e642017-08-10 11:24:38 -0500133 mountdir="/media/${name}"
134 mkdir -p "${mountdir}"
135 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
136 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
137 fi
138 done
139 done
140}
141
Michael Tritza694eed2017-07-13 16:26:15 -0500142ubi_setenv() {
143 varName="${variable%"\x3d"*}"
144 value="${variable##*"\x3d"}"
145 fw_setenv "$varName" "$value"
146}
147
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500148mtd_write() {
149 flashmtd="$(findmtd "${reqmtd}")"
150 img="/tmp/images/${version}/${imgfile}"
151 flashcp -v ${img} /dev/${flashmtd}
152}
153
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500154case "$1" in
Adriana Kobylak1fa612c2017-08-17 21:56:14 -0500155 mtduboot)
156 reqmtd="$2"
157 version="$3"
158 imgfile="image-u-boot"
159 mtd_write
160 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500161 ubirw)
162 reqmtd="$2"
163 name="$3"
164 ubi_rw
165 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500166 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500167 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
168 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500169 name="$3"
170 version="$4"
171 imgfile="image-rofs"
172 imgsize="16MiB"
173 ubi_ro
174 ubi_block
175 ubi_updatevol
176 ;;
177 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500178 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
179 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500180 name="$3"
181 version="$4"
182 imgfile="image-kernel"
183 imgsize="4MiB"
184 ubi_ro
185 ubi_updatevol
186 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500187 ubiremove)
188 name="$2"
189 ubi_remove
190 ;;
191 ubisetenv)
192 variable="$2"
193 ubi_setenv
194 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500195 ubiremount)
196 remount_ubi
197 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500198 *)
199 echo "Invalid argument"
200 exit 1
201 ;;
202esac
203rc=$?
204if [ ${rc} -ne 0 ]; then
205 echo "$0: error ${rc}"
206 exit ${rc}
207fi