blob: dffd29871e59d73eae4b165251ec799a6c67f682 [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
Adriana Kobylake882f912017-07-06 10:31:59 -050025# Attach the pnor mtd device to ubi
26attach_ubi() {
27 pnormtd="$(findmtd pnor)"
28 pnor="${pnormtd#mtd}"
29 pnordev="/dev/mtd${pnor}"
30
31 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
32 rc=$?
33 if [ ${rc} -ne 0 ]; then
34 # Check the pnor mtd device is formatted as ubi by reading the first 3 byes,
35 # which should be the ascii chars 'UBI'
36 magic="$(hexdump -C -n 3 ${pnordev})"
37 if [[ "${magic}" =~ "UBI" ]]; then
38 # Device already formatted as ubi, ubiattach failed for some other reason
39 return ${rc}
40 else
41 # Format device as ubi
42 echo "Starting ubiformat ${pnordev}"
43 ubiformat "${pnordev}" -y -q
44 # Retry the ubiattach
45 ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}"
46 fi
47 fi
48}
49
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050050mount_squashfs() {
51 pnormtd="$(findmtd pnor)"
52 pnor="${pnormtd#mtd}"
53 ubidev="/dev/ubi${pnor}"
54 mountdir="/media/${name}"
55
56 if [ ! -d "${mountdir}" ]; then
57 mkdir "${mountdir}"
58 fi
59
Adriana Kobylakd3165592017-07-18 12:24:24 -050060 # Create a static ubi volume of arbitrary size 24MB,
61 # the current pnor image is ~19MB
62 # TODO Set size based on file size openbmc/openbmc#1840
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050063 vol="$(findubi "${name}")"
64 if [ -z "${vol}" ]; then
Adriana Kobylakd3165592017-07-18 12:24:24 -050065 ubimkvol "${ubidev}" -N "${name}" -s 24MiB --type=static
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050066 vol="$(findubi "${name}")"
67 fi
68
69 # Create a ubi block needed for read-only volumes,
70 # and update the volume with the pnor squashfs image
71 ubidevid="${vol#ubi}"
72 block="/dev/ubiblock${ubidevid}"
73 if [ ! -e "$block" ]; then
74 img="/tmp/images/${version}/pnor.xz.squashfs"
75 ubiblock --create "/dev/ubi${ubidevid}"
76 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
77 fi
78
79 if ! is_mounted "${name}"; then
80 mount -t squashfs -o ro "${block}" "${mountdir}"
81 fi
82}
83
Adriana Kobylak6aff5742017-07-06 11:29:46 -050084mount_ubi() {
85 pnormtd="$(findmtd pnor)"
86 pnor="${pnormtd#mtd}"
87 ubidev="/dev/ubi${pnor}"
88 mountdir="/media/${name}"
89
Adriana Kobylakd3165592017-07-18 12:24:24 -050090 if [[ "${name}" == "pnor-prsv" ]]; then
91 size="2MiB"
92 else
93 size="16MiB"
94 fi
95
Adriana Kobylak6aff5742017-07-06 11:29:46 -050096 if [ ! -d "${mountdir}" ]; then
97 mkdir "${mountdir}"
98 fi
99
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500100 vol="$(findubi "${name}")"
101 if [ -z "${vol}" ]; then
Adriana Kobylakd3165592017-07-18 12:24:24 -0500102 ubimkvol "${ubidev}" -N "${name}" -s "${size}"
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500103 fi
104
105 if ! is_mounted "${name}"; then
106 mountdev="ubi${pnor}:${name}"
107 mount -t ubifs "${mountdev}" "${mountdir}"
108 fi
109}
110
Adriana Kobylake5764af2017-06-07 15:24:52 -0500111umount_ubi() {
112 pnormtd="$(findmtd pnor)"
113 pnor="${pnormtd#mtd}"
114 ubidev="/dev/ubi${pnor}"
115 mountdir="/media/${name}"
116
117 if is_mounted "${name}"; then
118 umount "${mountdir}"
119 fi
120
121 vol="$(findubi "${name}")"
122 if [ -n "${vol}" ]; then
123 ubirmvol "${ubidev}" -N "${name}"
124 fi
125
126 if [ -d "${mountdir}" ]; then
127 rm -r "${mountdir}"
128 fi
129}
130
Saqib Khan58c870d2017-07-26 23:48:20 -0500131remount_ubi() {
132 pnormtd="$(findmtd pnor)"
133 pnor="${pnormtd#mtd}"
134 # Get information on all ubi volumes
135 ubinfo=$(ubinfo -d ${pnor})
136 presentVolumes=${ubinfo##*:}
137 IFS=', ' read -r -a array <<< "$presentVolumes"
138 for element in ${array[@]};
139 do
140 elementProperties=$(ubinfo -d $pnor -n $element)
141 # Get ubi volume name by getting rid of additional properties
142 name=${elementProperties#*Name:}
143 name="${name%Character*}"
144 name="$(echo -e "${name}" | tr -d '[:space:]')"
145
146 if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then
147 mountdir="/media/${name}"
148 if [[ ${name} == pnor-ro* ]]
149 then
150 ubiblock --create /dev/ubi${pnor}_${element}
151 mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}"
152 else
153 mount -t ubifs "ubi${pnor}:${name}" "${mountdir}"
154 fi
155 fi
156 done
157}
158
Adriana Kobylake882f912017-07-06 10:31:59 -0500159case "$1" in
160 ubiattach)
161 attach_ubi
162 ;;
Adriana Kobylakf15e77c2017-07-06 11:26:42 -0500163 squashfsmount)
164 name="$2"
165 version="$3"
166 mount_squashfs
167 ;;
Adriana Kobylak6aff5742017-07-06 11:29:46 -0500168 ubimount)
169 name="$2"
170 mount_ubi
171 ;;
Adriana Kobylake5764af2017-06-07 15:24:52 -0500172 ubiumount)
173 name="$2"
174 umount_ubi
175 ;;
Saqib Khan58c870d2017-07-26 23:48:20 -0500176 ubiremount)
177 remount_ubi
178 ;;
Adriana Kobylake882f912017-07-06 10:31:59 -0500179 *)
180 echo "Invalid argument"
181 exit 1
182 ;;
183esac
184rc=$?
185if [ ${rc} -ne 0 ]; then
186 echo "$0: error ${rc}"
187 exit ${rc}
188fi