blob: 9af24ac8a89e1ed66a4316de3471abd9c2d21aa0 [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 Kobylaka290eff2017-06-27 09:39:57 -0500148case "$1" in
149 ubirw)
150 reqmtd="$2"
151 name="$3"
152 ubi_rw
153 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500154 ubiro)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500155 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
156 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500157 name="$3"
158 version="$4"
159 imgfile="image-rofs"
160 imgsize="16MiB"
161 ubi_ro
162 ubi_block
163 ubi_updatevol
164 ;;
165 ubikernel)
Adriana Kobylak54c9f792017-08-17 14:08:20 -0500166 reqmtd="$(echo "$2" | cut -d "+" -f 1)"
167 reqmtd2="$(echo "$2" | cut -d "+" -f 2)"
Adriana Kobylake9939492017-07-11 11:34:23 -0500168 name="$3"
169 version="$4"
170 imgfile="image-kernel"
171 imgsize="4MiB"
172 ubi_ro
173 ubi_updatevol
174 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500175 ubiremove)
176 name="$2"
177 ubi_remove
178 ;;
179 ubisetenv)
180 variable="$2"
181 ubi_setenv
182 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500183 ubiremount)
184 remount_ubi
185 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500186 *)
187 echo "Invalid argument"
188 exit 1
189 ;;
190esac
191rc=$?
192if [ ${rc} -ne 0 ]; then
193 echo "$0: error ${rc}"
194 exit ${rc}
195fi