blob: cab5f8614c477bf31f2ad22e1c33ab15981334d7 [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
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 Kobylake882f912017-07-06 10:31:59 -050083case "$1" in
84 ubiattach)
85 attach_ubi
86 ;;
Adriana Kobylakf15e77c2017-07-06 11:26:42 -050087 squashfsmount)
88 name="$2"
89 version="$3"
90 mount_squashfs
91 ;;
Adriana Kobylake882f912017-07-06 10:31:59 -050092 *)
93 echo "Invalid argument"
94 exit 1
95 ;;
96esac
97rc=$?
98if [ ${rc} -ne 0 ]; then
99 echo "$0: error ${rc}"
100 exit ${rc}
101fi