blob: 2b8394718a396de520b2762f2bd6eea5732a610b [file] [log] [blame]
Adriana Kobylake2538382017-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 Kobylakfc4d0e72017-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 Kobylake2538382017-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 Kobylakfc4d0e72017-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
60 # Create a static ubi volume of arbitrary size 32MB,
61 # the volume will shrink to fit the pnor image if smaller
62 vol="$(findubi "${name}")"
63 if [ -z "${vol}" ]; then
64 ubimkvol "${ubidev}" -N "${name}" -s 32MiB --type=static
65 vol="$(findubi "${name}")"
66 fi
67
68 # Create a ubi block needed for read-only volumes,
69 # and update the volume with the pnor squashfs image
70 ubidevid="${vol#ubi}"
71 block="/dev/ubiblock${ubidevid}"
72 if [ ! -e "$block" ]; then
73 img="/tmp/images/${version}/pnor.xz.squashfs"
74 ubiblock --create "/dev/ubi${ubidevid}"
75 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
76 fi
77
78 if ! is_mounted "${name}"; then
79 mount -t squashfs -o ro "${block}" "${mountdir}"
80 fi
81}
82
Adriana Kobylak77da88c2017-07-06 11:29:46 -050083mount_ubi() {
84 pnormtd="$(findmtd pnor)"
85 pnor="${pnormtd#mtd}"
86 ubidev="/dev/ubi${pnor}"
87 mountdir="/media/${name}"
88
89 if [ ! -d "${mountdir}" ]; then
90 mkdir "${mountdir}"
91 fi
92
93 # Create a dynamic ubi volume of initial arbitrary size 1MB,
94 # the volume will grow dynamically as needed
95 vol="$(findubi "${name}")"
96 if [ -z "${vol}" ]; then
97 ubimkvol "${ubidev}" -N "${name}" -s 1MiB
98 fi
99
100 if ! is_mounted "${name}"; then
101 mountdev="ubi${pnor}:${name}"
102 mount -t ubifs "${mountdev}" "${mountdir}"
103 fi
104}
105
Adriana Kobylake2538382017-07-06 10:31:59 -0500106case "$1" in
107 ubiattach)
108 attach_ubi
109 ;;
Adriana Kobylakfc4d0e72017-07-06 11:26:42 -0500110 squashfsmount)
111 name="$2"
112 version="$3"
113 mount_squashfs
114 ;;
Adriana Kobylak77da88c2017-07-06 11:29:46 -0500115 ubimount)
116 name="$2"
117 mount_ubi
118 ;;
Adriana Kobylake2538382017-07-06 10:31:59 -0500119 *)
120 echo "Invalid argument"
121 exit 1
122 ;;
123esac
124rc=$?
125if [ ${rc} -ne 0 ]; then
126 echo "$0: error ${rc}"
127 exit ${rc}
128fi