blob: d17ee5c3675f7b3861edc6f8725eeb0111d185b9 [file] [log] [blame]
Adriana Kobylaka290eff2017-06-27 09:39:57 -05001#!/bin/sh
2
3# Get the mtd device number (mtdX)
4findmtd() {
5 m="$(grep -xl "$1" /sys/class/mtd/*/name)"
6 m="${m%/name}"
7 m="${m##*/}"
8 echo "${m}"
9}
10
11# Get the ubi device number (ubiX_Y)
12findubi() {
13 u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
14 u="${u%/name}"
15 u="${u##*/}"
16 echo "${u}"
17}
18
19ubi_rw() {
20 rwmtd="$(findmtd "${reqmtd}")"
21 rw="${rwmtd#mtd}"
22 ubidev="/dev/ubi${rw}"
23
Michael Tritza694eed2017-07-13 16:26:15 -050024 if [ "$(fw_printenv rwreset)" == "rwreset=true" ]; then
25 ubi_remove
26 fw_setenv rwreset
27 fi
28
Adriana Kobylaka290eff2017-06-27 09:39:57 -050029 # Create a ubi volume of size 4MB, that is the current size of the rwfs image
30 vol="$(findubi "${name}")"
31 if [ -z "${vol}" ]; then
32 ubimkvol "${ubidev}" -N "${name}" -s 4MiB
33 fi
34}
35
Adriana Kobylake9939492017-07-11 11:34:23 -050036ubi_ro() {
37 romtd="$(findmtd "${reqmtd}")"
38 ro="${romtd#mtd}"
39 ubidev="/dev/ubi${ro}"
40
41 # Create a static ubi volume
42 # TODO Get the actual image size openbmc/openbmc#1840
43 vol="$(findubi "${name}")"
44 if [ -z "${vol}" ]; then
45 ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
46 vol="$(findubi "${name}")"
47 fi
48}
49
50# Squashfs images need a ubi block
51ubi_block() {
52 vol="$(findubi "${name}")"
53 ubidevid="${vol#ubi}"
54 block="/dev/ubiblock${ubidevid}"
55 if [ ! -e "$block" ]; then
56 ubiblock --create "/dev/ubi${ubidevid}"
57 fi
58}
59
60ubi_updatevol() {
61 vol="$(findubi "${name}")"
62 ubidevid="${vol#ubi}"
63 img="/tmp/images/${version}/${imgfile}"
64 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
65}
66
Michael Tritza694eed2017-07-13 16:26:15 -050067ubi_remove() {
68 vol="$(findubi "${name}")"
69
70 if [ ! -z "$vol" ]; then
71 vol="${vol%_*}"
72
73 if grep -q "${vol}:$name" /proc/mounts; then
74 mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2)
75 umount "$mountdir"
76 rm -r "$mountdir"
77 fi
78
79 ubirmvol "/dev/${vol}" -N "$name"
80 fi
81}
82
Saqib Khanbb93e642017-08-10 11:24:38 -050083remount_ubi() {
84 # Get a list of all devices formatted as UBI
85 for file in /dev/mtd*; do
86 if [[ $(hexdump -C -n 3 $file) =~ "UBI" ]]; then
87 mtd="${file: -1}"
88 if [[ $mtd =~ ^-?[0-9]+$ ]]; then
89 mtds=${mtd},${mtds}
90 fi
91 fi
92 done
93
94 IFS=',' read -r -a mtds <<< "$mtds"
95 mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
96 for mtd in ${mtds[@]}; do
97 # Re-attach mtd device to ubi if not already done
98 ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null
99 # Get information on all ubi volumes
100 ubinfo=$(ubinfo -d ${mtd})
101 presentVolumes=${ubinfo##*:}
102 IFS=', ' read -r -a array <<< "$presentVolumes"
103 for element in ${array[@]}; do
104 elementProperties=$(ubinfo -d $mtd -n $element)
105 # Get ubi volume name by getting rid of additional properties
106 name=${elementProperties#*Name:}
107 name="${name%Character*}"
108 name="$(echo -e "${name}" | tr -d '[:space:]')"
109
110 if [[ ${name} == ro-* ]]; then
111 mountdir="/media/${name}"
112 mkdir -p "${mountdir}"
113 ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null
114 mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}"
115 fi
116 done
117 done
118}
119
Michael Tritza694eed2017-07-13 16:26:15 -0500120ubi_setenv() {
121 varName="${variable%"\x3d"*}"
122 value="${variable##*"\x3d"}"
123 fw_setenv "$varName" "$value"
124}
125
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500126case "$1" in
127 ubirw)
128 reqmtd="$2"
129 name="$3"
130 ubi_rw
131 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -0500132 ubiro)
133 reqmtd="$2"
134 name="$3"
135 version="$4"
136 imgfile="image-rofs"
137 imgsize="16MiB"
138 ubi_ro
139 ubi_block
140 ubi_updatevol
141 ;;
142 ubikernel)
143 reqmtd="$2"
144 name="$3"
145 version="$4"
146 imgfile="image-kernel"
147 imgsize="4MiB"
148 ubi_ro
149 ubi_updatevol
150 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500151 ubiremove)
152 name="$2"
153 ubi_remove
154 ;;
155 ubisetenv)
156 variable="$2"
157 ubi_setenv
158 ;;
Saqib Khanbb93e642017-08-10 11:24:38 -0500159 ubiremount)
160 remount_ubi
161 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500162 *)
163 echo "Invalid argument"
164 exit 1
165 ;;
166esac
167rc=$?
168if [ ${rc} -ne 0 ]; then
169 echo "$0: error ${rc}"
170 exit ${rc}
171fi