op_storage.sh: updates from pmprog
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_storage.sh
1 #!/bin/bash
2
3 mountmassstorage()
4 {
5         selection2=$(echo "$selection" | sed 's/ /\\\\040/g')
6         options=$(grep "${selection2}" /proc/mounts | awk '{print $4}' | sed "s/,codepage=[A-Za-z0-9]*,/,/g" | sed 's/\\040/\\ /g' )
7         device=$(grep "${selection2}" /proc/mounts | awk '{print substr($1,1,12)}')
8         device2=$(grep "${selection2}" /proc/mounts | awk '{print $1}')
9
10         if ! umount $device2; then
11                 zenity --title="Error" --error --text="Error.\nThe card could not be unmounted.\n\nPlease make sure to close any programs that currently access the SD Card." --timeout 10
12                 return 1
13         fi
14
15         delay=0
16
17         # remove other gadget modules
18         modules="$(lsmod | awk '{print $1}' | xargs echo)"
19         blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
20                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms"
21         restore_list=""
22         for mod in $modules; do
23                 if echo $blacklist | grep -q "\<$mod\>"; then
24                         restore_list="$restore_list $mod"
25                         rmmod $mod
26                         delay=1 # enough?
27                 fi
28         done
29
30         # driver race condition workaround (some kernels crash without wait)
31         sleep $delay
32
33         # switch to mass storage
34         modprobe g_file_storage file=$device stall=0 removable=1
35
36         zenity --title="Mass Storage Mode" --info --text="SD Card $selection is currently in Mass Storage Mode.\n\nClick on OK when you're finished with your data transfer\nand want to go back to normal mode."
37
38         rmmod g_file_storage
39
40         if [ ! -d "$selection" ]; then
41                 mkdir "$selection"
42         fi 
43         mount $device2 "$selection" -o $options
44
45         if [ -n "$restore_list" ]; then
46                 sleep $delay
47                 modprobe $restore_list
48         fi
49 }
50
51
52 if [ -z $1 ]; then
53         while selection=$(grep "/dev/mmcblk" /proc/mounts | cut -f 2 -d " " | sed 's/\\040/ /g' | zenity --title="SD Card Mass Storage" --width="380" --height="200" --list --text="Enable mass storage" --column="Select card"); do
54                 mountmassstorage
55         done
56 else
57
58         if [ "$1" == "list" ]; then
59                 grep "/dev/mmcblk" /proc/mounts | cut -f 2 -d " " | sed 's/\\040/ /g'
60         else
61                 selection=$1
62                 mountmassstorage
63         fi
64 fi
65