]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/sharp-binary-only/sharp-sdmmc-support/sdcontrol
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / sharp-binary-only / sharp-sdmmc-support / sdcontrol
1 #!/bin/sh
2 #
3 # sdcontrol 1.0 2001/8/8 21:33:19 (Hideki Hayami)
4 #
5 # Initialize or shutdown a SD card device
6 #
7 # The first argument should be either 'insert' of 'eject'.
8 #
9
10 ACTION=$1
11 DEVICE=/dev/mmcda1
12 MOUNT_POINT=/media/card
13 SMB_MOUNT=/home/samba/SD_Card
14 INSTALL_DIR=Documents/Install_Files
15
16 ENABLE_LOG="no"
17 LOGFILE="/tmp/sdcontrol.log"
18
19 DEBUG=0
20
21 vecho() {
22         stamp="`date +"%d-%m-%y %H:%M:%S"`"
23         echo -e "$1"
24         test "$ENABLE_LOG" = "yes" -a -n "$LOGFILE" && echo -e "$1" | sed "s/\(.*\)/[$stamp]\ \1/">> "$LOGFILE"
25 }
26
27
28 # import FS mount options from fstab or use defaults
29 # if detection fails
30 SD_OPTS="`grep "$MOUNT_POINT" /etc/fstab | head -1 | awk '{print $4}'`" 
31 if test -z "$SD_OPTS"
32 then
33         SD_OPTS="noatime,sync"
34         vecho "\nWARNING: Couldn't determine SD mount options from /etc/fstab, using defaults\n"
35 fi
36
37 # The "quiet" option fails on non-VFAT cards. Nice trick ;)
38 SD_OPTS_VFAT="$SD_OPTS,quiet,umask=000,iocharset=utf8"
39
40
41 ###### for QPE ######
42 get_pid()
43 {
44     echo $1
45 }
46
47 decho() {
48         test "$DEBUG" = 1 && echo "<DEBUG> $*"
49 }
50
51 wait_release()
52 {
53     count=1
54     while true
55     do
56         umount $MOUNT_POINT
57         if [ $? = 0 ]; then
58             #echo umount >> /tmp/sd
59             return
60         fi
61         
62         if ! (mount | grep -q "$MOUNT_POINT")
63         then
64                 return
65         fi
66         
67         echo count=$count >> /tmp/sd
68         if [ `expr $count \>= 500` = 1 ]; then
69             #echo time out >> /tmp/sd
70             return
71         fi
72         count=`expr $count + 1`
73         usleep 200000 || sleep 2
74     done
75 }
76
77 kill_task()
78 {
79   
80     if ! test -z "`which fuser`"
81     then  
82             echo "Using 'fuser' to kill  \"busy\" tasks"
83             ps_line=`ps ax | grep 'qpe$' | grep -v grep` # no -w on busybox
84             decho "* 1 *"
85             decho "ps_line [$ps_line]"
86             qpe_pid=`get_pid $ps_line`
87             
88             decho "qpe_pid [$qpe_pid]"
89             decho "* 1.1 *"
90             
91             target_pids=`fuser -m $1 | cut -d : -f2 | sed "s/[a-z]//g"` >/dev/null 2>&1
92                 
93             decho "* 1.2 *"
94             #echo $target_pids >> /tmp/sd
95             if ! (echo "$target_pids" | grep -q "[0-9]"); then
96                 decho "* 2 *"
97                 return
98             fi
99             decho "Killing PIDs: [$target_pids]"
100             decho "* 3 *"
101             ! test -z "$qpe_pid" && is_exist_qpe=`echo $target_pids | grep "$qpe_pid"` # no -w on busybox
102             
103             decho "is_exist_qpe [$is_exist_qpe]"
104             if [ "$is_exist_qpe" = "" ]; then
105                 kill -9 $target_pids
106                 decho "* 4 *"
107                 #echo kill -9 $target_pids >> /tmp/sd
108             else
109                 decho "* 5 *"
110                 #echo "found qpe!!!" >> /tmp/sd
111                 target_pids=`echo $target_pids | sed -e "s/$qpe_pid//"`
112                 if (echo "$target_pids" | grep -q "[0-9]"); then
113                         echo "* 6 *"
114                     kill -9 $target_pids
115                     
116                     #echo kill -9 $target_pids >> /tmp/sd
117                 fi
118                 wait_release
119 #               exit 0
120             fi
121     else
122         echo "No 'fuser' found. Running tasks may keep partitions busy."
123     fi
124 }
125 ###### for QPE ######
126
127 case "$ACTION" in
128 'insert')
129         
130         
131         vecho "Beginning SD auto-mount..:"
132         
133         test "$ENABLE_LOG" = yes && ps ax > "$LOGFILE-ps"
134         
135         # Read available partitions from /proc/partitions.
136         OK_PARTS=` head -20 /proc/partitions |grep mmcda | sort| uniq | awk '{print $4}'`
137         
138         decho "OK_PARTS [$OK_PARTS]"
139         
140         test "$ENABLE_LOG" = yes && echo $OK_PARTS > "$LOGFILE-part"
141         
142         if test "`echo "$OK_PARTS" | wc -l | awk '{print $1}`" -gt 1
143         then
144                 OK_PARTS="`echo "$OK_PARTS" | grep -v "^mmcda$"`"
145         fi
146         
147         if test -z "$OK_PARTS"
148         then
149                 vecho "\n\nWARNING: Trying failsafe partition mode\n\n"
150                 OK_PARTS="mmcda1 mmcda2 mmcda3 mmcda4"
151         fi
152
153         
154         vecho "-> Valid SD partitions are: [$OK_PARTS]"
155         
156         # Allow for "#" in fstab.
157         fstab_txt="`cat /etc/fstab | grep -v ^#`"
158         
159         cnt=1
160         for part in $OK_PARTS
161         do
162                 vecho "\n* * * * * Working on [/dev/$part] * * * * *\n"
163                 # Read the mount-point for this partition from fstab 
164                 FS_MOUNT_POINT="`echo "$fstab_txt" | grep "/dev/$part" | awk '{print $2}`"
165
166
167                 # Mount the first valid partition as /mnt/card if there was no entry in fstab
168                 if test -z "$FS_MOUNT_POINT"
169                 then
170                         # See if /mnt/card is already mounted
171                         if ! mount|awk '{print $3}'|grep "^$MOUNT_POINT$" 2>&1 >/dev/null
172                         then
173                                 # Check if another *existing* partition is configured as /mnt/card                              
174                                 if ! echo "$fstab_txt" | grep "$MOUNT_POINT" | awk '{print $2}'| grep "^$MOUNT_POINT$" 2>&1 >/dev/null
175                                 then
176                                         # As /mnt/card wasn't configured in fstab, we simply mount the
177                                         # first unconfigured partition we find
178                                         FS_MOUNT_POINT="$MOUNT_POINT"
179                                 else
180                                         # Go through all partitions, and check if one of them is configured
181                                         # as /mnt/card
182                                         for xpart in $OK_PARTS
183                                         do
184                                                 if echo "$fstab_txt" | grep "$xpart" | awk '{print $2}'| grep "^$MOUNT_POINT$" 2>&1 >/dev/null
185                                                 then
186                                                         vecho "-> $MOUNT_POINT is reserved by fstab"
187                                                         found=1
188                                                 fi
189                                         done
190                                         
191                                         if test "$found" = 1 
192                                         then    
193                                                 # Another existing partition is configured for /mnt/card, leave it alone.
194                                                 FS_MOUNT_POINT="$MOUNT_POINT$cnt"
195                                                 let cnt=$cnt+1
196                                         else
197                                                 FS_MOUNT_POINT="$MOUNT_POINT"
198                                         fi                                      
199                                         
200                                 fi
201                         else
202                                 FS_MOUNT_POINT="$MOUNT_POINT$cnt"
203                                 let cnt=$cnt+1                          
204                         fi
205                         vecho "-> Using mountpoint [$FS_MOUNT_POINT]"
206                 else
207                         vecho "-> Using mountpoint [$FS_MOUNT_POINT] from fstab"
208                 fi
209
210                 ! test -d $FS_MOUNT_POINT && mkdir -p $FS_MOUNT_POINT   
211                         
212                 DEVICE="/dev/$part"
213                 if ! test -e "$DEVICE"
214                 then
215                         DEV_NUM="`echo "$DEVICE" | sed -n "s/.*\([0-9]\)/\1/p"`"
216                         cd /dev                 
217                         mknod mmcda$DEV_NUM b 60 $DEV_NUM
218                 fi
219                                 
220                 vecho "-> Trying VFAT mount [$SD_OPTS_VFAT]..."
221                 mount $FSTYPE -o $SD_OPTS_VFAT $DEVICE $FS_MOUNT_POINT >/dev/null 2>&1 
222                 MOUNT_RES=`mount | grep $DEVICE`
223
224
225                 if [ "$MOUNT_RES" = "" ]; then
226                         vecho "-> Trying ext2 mount [$SD_OPTS]..."
227                         mount $FSTYPE -o $SD_OPTS $DEVICE $FS_MOUNT_POINT
228                 else
229                         vecho "-> VFAT mount was successfull"
230                 fi
231
232                 MOUNT_RES=`mount | grep $DEVICE`
233                 if [ "$MOUNT_RES" = "" ]; then
234                         vecho "-> Trying failsafe mount..."
235                         mount $FSTYPE $DEVICE $FS_MOUNT_POINT
236                 else
237                         vecho "-> EXT2 mount was successfull"                   
238                 fi
239
240                 # Um what was the function of that?
241 #               chkmntsh ${MOUNT_POINT}
242
243         done
244         
245         # I have no idea what this is good for....
246         if [ -d $SMB_MOUNT ] ; then
247                 rm -rf $SMB_MOUNT
248                 ln -s /mnt/card $SMB_MOUNT
249                 mkdir -p $FS_MOUNT_POINT/$INSTALL_DIR           
250         fi
251
252
253         ;;
254 'eject')
255         # Doesn't work as "fuser" isn't in a base OZ 3.5.[1|2] ROM
256         for part in `mount | grep mmcda|awk '{print $1}'`
257         do      
258                 DEVICE="$part"
259                 fuser -s -m $DEVICE
260                 if [ $? = 1 ]; then
261                         umount $DEVICE
262                         rm $SMB_MOUNT
263                 else
264                         exit 1
265                 fi
266         done
267         ;;
268 'compeject')
269         for part in `mount | grep mmcda|awk '{print $3}'`
270         do               
271                 found_something=1
272                 kill_task "$part"      # for QPE
273                 
274                 umount $part >/dev/null 2>&1 && echo "Umounted [$part]" || echo -e "\n* * * WARNING: umount $part failed! * * *"
275
276                 test -e "$SMB_MOUNT" && rm $SMB_MOUNT
277         done
278         test -z "$found_something" && echo "Nothing to do."
279         ;;
280 'change')
281         $0 compeject
282         $0 insert
283         ;;
284 '*')
285         exit 1
286         ;;
287 esac
288
289 exit 0
290