summaryrefslogtreecommitdiff
path: root/packages/sharp-binary-only/sharp-sdmmc-support-2.4.18-rmk7-pxa3-embedix/sdcontrol
blob: a857817bbcec74b7f886f4ad6a789ceb742baf05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/sh
#
# sdcontrol 1.0 2001/8/8 21:33:19 (Hideki Hayami)
#
# Initialize or shutdown a SD card device
#
# The first argument should be either 'insert' of 'eject'.
#

ACTION=$1
DEVICE=/dev/mmcda1
MOUNT_POINT=/media/card
SMB_MOUNT=/home/samba/SD_Card
INSTALL_DIR=Documents/Install_Files

ENABLE_LOG="no"
LOGFILE="/tmp/sdcontrol.log"

vecho() {
	stamp="`date +"%d-%m-%y %H:%M:%S"`"
	echo -e "$1"
	test "$ENABLE_LOG" = "yes" -a -n "$LOGFILE" && echo -e "$1" | sed "s/\(.*\)/[$stamp]\ \1/">> "$LOGFILE"
}


# import FS mount options from fstab or use defaults
# if detection fails
SD_OPTS="`grep "$MOUNT_POINT" /etc/fstab | awk '{print $4}'`" 
if test -z "$SD_OPTS"
then
	SD_OPTS="noatime,sync"
	vecho "\nWARNING: Couldn't determine SD mount options from /etc/fstab, using defaults\n"
fi

# The "quiet" option fails on non-VFAT cards. Nice trick ;)
SD_OPTS_VFAT="$SD_OPTS,quiet,umask=000,iocharset=utf8"


###### for QPE ######
get_pid()
{
    echo $1
}

wait_release()
{
    count=1
    while true
    do
        umount $MOUNT_POINT
        if [ $? = 0 ]; then
            #echo umount >> /tmp/sd
            return
        fi
        echo count=$count >> /tmp/sd
        if [ `expr $count \>= 500` = 1 ]; then
            #echo time out >> /tmp/sd
            return
        fi
        count=`expr $count + 1`
        usleep 200000
    done
}

kill_task()
{
    ps_line=`ps ax | grep 'qpe$'` # no -w on busybox
    qpe_pid=`get_pid $ps_line`
    #echo qpe_pid = $qpe_pid >> /tmp/sd
    target_pids=`fuser -m $DEVICE | cut -d : -f2`
    #echo $target_pids >> /tmp/sd
    if [ "$target_pids" = "" ]; then
        return
    fi
    is_exist_qpe=`echo $target_pids | grep $qpe_pid` # no -w on busybox
    if [ "$is_exist_qpe" = "" ]; then
	kill -9 $target_pids
        #echo kill -9 $target_pids >> /tmp/sd
    else
        #echo "found qpe!!!" >> /tmp/sd
	target_pids=`echo $target_pids | sed -e "s/$qpe_pid//"`
	if [ "$target_pids" != "" ]; then
            kill -9 $target_pids
            #echo kill -9 $target_pids >> /tmp/sd
        fi
        wait_release
        exit 0
    fi
}
###### for QPE ######

case "$ACTION" in
'insert')
	
	
	vecho "Beginning SD auto-mount..:"
	
	ps ax > "$LOGFILE-ps"
	
	# Read available partitions from /proc/partitions.
	OK_PARTS="`cat /proc/partitions |awk '{print $4}'| grep mmcd`"
	
	cat /proc/partitions |awk '{print $4}'| grep mmcd > "$LOGFILE-part"	
	
	if test "`echo "$OK_PARTS" | wc -l | awk '{print $1}`" -gt 1
	then
		OK_PARTS="`echo "$OK_PARTS" | grep -v "^mmcda$"`"
	fi
	
	if test -z "$OK_PARTS"
	then
		vecho "\n\nWARNING: Trying failsafe partition mode\n\n"
		OK_PARTS="mmcda1 mmcda2 mmcda3 mmcda4"
	fi

	
	vecho "-> Valid SD partitions are: [$OK_PARTS]"
	
	# Allow for "#" in fstab.
	fstab_txt="`cat /etc/fstab | grep -v ^#`"
	
	cnt=1
	for part in $OK_PARTS
	do
		vecho "\n* * * * * Working on [/dev/$part] * * * * *\n"
		# Read the mount-point for this partition from fstab 
		FS_MOUNT_POINT="`echo "$fstab_txt" | grep "/dev/$part" | awk '{print $2}`"


		# Mount the first valid partition as /mnt/card if there was no entry in fstab
		if test -z "$FS_MOUNT_POINT"
		then
			# See if /mnt/card is already mounted
			if ! mount|awk '{print $3}'|grep "^$MOUNT_POINT$" 2>&1 >/dev/null
			then
				# Check if another *existing* partition is configured as /mnt/card				
				if ! echo "$fstab_txt" | grep "$MOUNT_POINT" | awk '{print $2}'| grep "^$MOUNT_POINT$" 2>&1 >/dev/null
				then
					# As /mnt/card wasn't configured in fstab, we simply mount the
					# first unconfigured partition we find
					FS_MOUNT_POINT="$MOUNT_POINT"
				else
					# Go through all partitions, and check if one of them is configured
					# as /mnt/card
					for xpart in $OK_PARTS
					do
						if echo "$fstab_txt" | grep "$xpart" | awk '{print $2}'| grep "^$MOUNT_POINT$" 2>&1 >/dev/null
						then
							vecho "-> $MOUNT_POINT is reserved by fstab"
							found=1
						fi
					done
					
					if test "$found" = 1 
					then	
						# Another existing partition is configured for /mnt/card, leave it alone.
						FS_MOUNT_POINT="$MOUNT_POINT$cnt"
						let cnt=$cnt+1
					else
						FS_MOUNT_POINT="$MOUNT_POINT"
					fi					
					
				fi
			else
				FS_MOUNT_POINT="$MOUNT_POINT$cnt"
				let cnt=$cnt+1				
			fi
			vecho "-> Using mountpoint [$FS_MOUNT_POINT]"
		else
			vecho "-> Using mountpoint [$FS_MOUNT_POINT] from fstab"
		fi

		! test -d $FS_MOUNT_POINT && mkdir -p $FS_MOUNT_POINT	
			
		DEVICE="/dev/$part"
		if ! test -e "$DEVICE"
		then
			DEV_NUM="`echo "$DEVICE" | sed -n "s/.*\([0-9]\)/\1/p"`"
			cd /dev			
			mknod mmcda$DEV_NUM b 60 $DEV_NUM
		fi
				
		vecho "-> Trying VFAT mount [$SD_OPTS_VFAT]..."
        	mount $FSTYPE -o $SD_OPTS_VFAT $DEVICE $FS_MOUNT_POINT 2>&1 >/dev/null
		MOUNT_RES=`mount | grep $DEVICE`


		if [ "$MOUNT_RES" = "" ]; then
			vecho "-> Trying ext2 mount [$SD_OPTS]..."
	        	mount $FSTYPE -o $SD_OPTS $DEVICE $FS_MOUNT_POINT
		else
			vecho "-> VFAT mount was successfull"
		fi

		MOUNT_RES=`mount | grep $DEVICE`
		if [ "$MOUNT_RES" = "" ]; then
			vecho "-> Trying failsafe mount..."
	        	mount $FSTYPE $DEVICE $FS_MOUNT_POINT
		else
			vecho "-> EXT2 mount was successfull"			
		fi

		# Um what was the function of that?
#		chkmntsh ${MOUNT_POINT}

	done
	
	# I have no idea what this is good for....
	if [ -d $SMB_MOUNT ] ; then
		rm -rf $SMB_MOUNT
	fi
	ln -s /mnt/card $SMB_MOUNT
	mkdir -p $FS_MOUNT_POINT/$INSTALL_DIR

        ;;
'eject')
	# Doesn't work as "fuser" isn't in a base OZ 3.5.[1|2] ROM
	for part in `mount | grep mmcda|awk '{print $1}'`
	do	
		DEVICE="$part"
        	fuser -s -m $DEVICE
        	if [ $? = 1 ]; then
                	umount $DEVICE
                	rm $SMB_MOUNT
		else
			exit 1
        	fi
	done
        ;;
'compeject')
	for part in `mount | grep mmcda|awk '{print $1}'`
	do
		DEVICE="$part"
	
	        kill_task       # for QPE
		
       		 #fuser -k -m $DEVICE > /dev/null
	        umount $DEVICE
        	if [ $? != 0 ]; then
                	usleep 500000
                	umount $DEVICE
        	fi
		rm $SMB_MOUNT
	done
        ;;
'change')
        $0 compeject
        $0 insert
        ;;
'*')
        exit 1
        ;;
esac

exit 0