blob: 0cdf74eff41b01756b92605759ce797e9efaeb20 [file] [log] [blame]
Adriana Kobylake882f912017-07-06 10:31:59 -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
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050011# 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
19# Get the mount information
20is_mounted() {
21 grep -q "$1" /proc/mounts
22 return $?
23}
24
Saqib Khance611932017-08-01 09:46:22 -050025# Attach the pnor mtd device to ubi.
Adriana Kobylake882f912017-07-06 10:31:59 -050026attach_ubi() {
27 pnormtd="$(findmtd pnor)"
28 pnor="${pnormtd#mtd}"
29 pnordev="/dev/mtd${pnor}"
30
Saqib Khan4f609b82017-08-02 15:13:26 -050031 if [ -d "/sys/class/ubi/ubi${pnor}" ]; then
32 # Already attached
33 return 0
34 fi
35
Adriana Kobylake882f912017-07-06 10:31:59 -050036 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
37 rc=$?
38 if [ ${rc} -ne 0 ]; then
39 # Check the pnor mtd device is formatted as ubi by reading the first 3 byes,
40 # which should be the ascii chars 'UBI'
41 magic="$(hexdump -C -n 3 ${pnordev})"
42 if [[ "${magic}" =~ "UBI" ]]; then
43 # Device already formatted as ubi, ubiattach failed for some other reason
44 return ${rc}
45 else
46 # Format device as ubi
47 echo "Starting ubiformat ${pnordev}"
48 ubiformat "${pnordev}" -y -q
49 # Retry the ubiattach
50 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
51 fi
52 fi
53}
54
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050055mount_squashfs() {
56 pnormtd="$(findmtd pnor)"
57 pnor="${pnormtd#mtd}"
58 ubidev="/dev/ubi${pnor}"
59 mountdir="/media/${name}"
60
61 if [ ! -d "${mountdir}" ]; then
62 mkdir "${mountdir}"
63 fi
64
Adriana Kobylakd3165592017-07-18 12:24:24 -050065 # Create a static ubi volume of arbitrary size 24MB,
66 # the current pnor image is ~19MB
67 # TODO Set size based on file size openbmc/openbmc#1840
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050068 vol="$(findubi "${name}")"
69 if [ -z "${vol}" ]; then
Adriana Kobylakd3165592017-07-18 12:24:24 -050070 ubimkvol "${ubidev}" -N "${name}" -s 24MiB --type=static
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050071 vol="$(findubi "${name}")"
72 fi
73
74 # Create a ubi block needed for read-only volumes,
75 # and update the volume with the pnor squashfs image
76 ubidevid="${vol#ubi}"
77 block="/dev/ubiblock${ubidevid}"
78 if [ ! -e "$block" ]; then
79 img="/tmp/images/${version}/pnor.xz.squashfs"
80 ubiblock --create "/dev/ubi${ubidevid}"
81 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
82 fi
83
84 if ! is_mounted "${name}"; then
85 mount -t squashfs -o ro "${block}" "${mountdir}"
86 fi
87}
88
Adriana Kobylak6aff5742017-07-06 11:29:46 -050089mount_ubi() {
90 pnormtd="$(findmtd pnor)"
91 pnor="${pnormtd#mtd}"
92 ubidev="/dev/ubi${pnor}"
Adriana Kobylakf287af32017-07-12 13:52:22 -050093
94 if [[ "${name}" == "pnor-patch" ]]; then
95 mountdir="/usr/local/share/pnor"
96 else
97 mountdir="/media/${name}"
98 fi
Adriana Kobylak6aff5742017-07-06 11:29:46 -050099
Adriana Kobylakd3165592017-07-18 12:24:24 -0500100 if [[ "${name}" == "pnor-prsv" ]]; then
101 size="2MiB"
102 else
103 size="16MiB"
104 fi
105
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500106 if [ ! -d "${mountdir}" ]; then
Adriana Kobylakf287af32017-07-12 13:52:22 -0500107 mkdir -p "${mountdir}"
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500108 fi
109
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500110 vol="$(findubi "${name}")"
111 if [ -z "${vol}" ]; then
Adriana Kobylakd3165592017-07-18 12:24:24 -0500112 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500113 fi
114
115 if ! is_mounted "${name}"; then
116 mountdev="ubi${pnor}:${name}"
117 mount -t ubifs "${mountdev}" "${mountdir}"
118 fi
119}
120
Adriana Kobylake5764af2017-06-07 15:24:52 -0500121umount_ubi() {
122 pnormtd="$(findmtd pnor)"
123 pnor="${pnormtd#mtd}"
124 ubidev="/dev/ubi${pnor}"
125 mountdir="/media/${name}"
126
127 if is_mounted "${name}"; then
128 umount "${mountdir}"
129 fi
130
131 vol="$(findubi "${name}")"
132 if [ -n "${vol}" ]; then
133 ubirmvol "${ubidev}" -N "${name}"
134 fi
135
136 if [ -d "${mountdir}" ]; then
137 rm -r "${mountdir}"
138 fi
139}
140
Saqib Khan58c870d2017-07-26 23:48:20 -0500141remount_ubi() {
142 pnormtd="$(findmtd pnor)"
143 pnor="${pnormtd#mtd}"
Saqib Khance611932017-08-01 09:46:22 -0500144 pnordev="/dev/mtd${pnor}"
145
146 # Re-Attach the pnor mtd device to ubi
147 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
148 rc=$?
149 if [ ${rc} -ne 0 ]; then
150 # Check the pnor mtd device is formatted as ubi by reading
151 # the first 3 byes, which should be the ascii chars 'UBI'
152 magic="$(hexdump -C -n 3 ${pnordev})"
153 if [[ ! "${magic}" =~ "UBI" ]]; then
154 # Device not formatted as ubi.
155 return
156 fi
157 fi
158
Saqib Khan58c870d2017-07-26 23:48:20 -0500159 # Get information on all ubi volumes
160 ubinfo=$(ubinfo -d ${pnor})
161 presentVolumes=${ubinfo##*:}
162 IFS=', ' read -r -a array <<< "$presentVolumes"
163 for element in ${array[@]};
164 do
165 elementProperties=$(ubinfo -d $pnor -n $element)
166 # Get ubi volume name by getting rid of additional properties
167 name=${elementProperties#*Name:}
168 name="${name%Character*}"
169 name="$(echo -e "${name}" | tr -d '[:space:]')"
170
171 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
172 mountdir="/media/${name}"
Saqib Khan76b7b832017-08-01 11:32:27 -0500173 if [ ! -d "${mountdir}" ]; then
174 mkdir -p "${mountdir}"
175 fi
176
Saqib Khan58c870d2017-07-26 23:48:20 -0500177 if [[ ${name} == pnor-ro* ]]
178 then
179 ubiblock --create /dev/ubi${pnor}_${element}
180 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
181 else
182 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
183 fi
184 fi
185 done
186}
187
Adriana Kobylake882f912017-07-06 10:31:59 -0500188case "$1" in
189 ubiattach)
190 attach_ubi
191 ;;
Adriana Kobylakf15e77c2017-07-06 11:26:42 -0500192 squashfsmount)
193 name="$2"
194 version="$3"
195 mount_squashfs
196 ;;
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500197 ubimount)
198 name="$2"
199 mount_ubi
200 ;;
Adriana Kobylake5764af2017-06-07 15:24:52 -0500201 ubiumount)
202 name="$2"
203 umount_ubi
204 ;;
Saqib Khan58c870d2017-07-26 23:48:20 -0500205 ubiremount)
206 remount_ubi
207 ;;
Adriana Kobylake882f912017-07-06 10:31:59 -0500208 *)
209 echo "Invalid argument"
210 exit 1
211 ;;
212esac
213rc=$?
214if [ ${rc} -ne 0 ]; then
215 echo "$0: error ${rc}"
216 exit ${rc}
217fi