~martijnbraam/thumbdrives

ref: 2024dbe250abc1533b29a0e9ce33ad7992ccf030 thumbdrives/src/pk-thumbdrive-action.sh -rw-r--r-- 2.3 KiB
2024dbe2Martijn Braam Patched up desktop file 2 years ago
                                                                                
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
#!/bin/sh
ACTION=$1
BACKING_FILE=$2
CONFIGFS=/sys/kernel/config/usb_gadget
GADGET=$CONFIGFS/thumbdrives

remove_gadget () {
	local gadget=$1

	# Disable the gadget
	echo "" > "$gadget"/UDC

	# Remove the functions from the config
	for function in "$gadget"/configs/*/*.*
	do
		rm "$function"
	done

	# Remove the language data from the config
	for lang in "$gadget"/configs/*/strings/*
	do
		rmdir "$lang"
	done

	# Remove the configurations
	for config in "$gadget"/configs/*/
	do
		rmdir "$config"
	done

	# Remove the defined functions
	for function in "$gadget"/functions/*/
	do
		rmdir "$function"
	done

	# Remove the defined language data
	for lang in "$gadget"/strings/*
	do
		rmdir "$lang"
	done

	# Remove the gadget
	rmdir "$gadget"
}

disable_existing_gadgets () {
	for gadget in "$CONFIGFS"/*/UDC
	do
		echo "" > "$gadget"
	done
}

create_gadget () {
	local backing="$1"
	local devtype="$2"

	mkdir $GADGET
	echo "0x1209" > $GADGET/idVendor # Generic
	echo "0x4202" > $GADGET/idProduct # Random id

	# English locale
	LOCALE=$GADGET/strings/0x409
	mkdir $LOCALE || echo "Could not create $LOCALE"
	echo "Phone" > $LOCALE/manufacturer
	echo "BLEH" > $LOCALE/product
	echo "Thumbdrives" > $LOCALE/serialnumber

	# Mass storage function
	FUNCTION=$GADGET/functions/mass_storage.0
	LUN=$FUNCTION/lun.0
	mkdir $FUNCTION || echo "Could not create $FUNCTION"
	mkdir $LUN || echo "Could not create $LUN"

	# Configuration instance
	CONFIG=$GADGET/configs/c.1
	LOCALE=$CONFIG/strings/0x409
	mkdir $CONFIG || echo "Coud not create $CONFIG"
	mkdir $LOCALE || echo "Coud not create $LOCALE"
	echo "Thumbdrive" > $LOCALE/configuration

	# Link mass storage gadget to backing file
	echo $backing > $LUN/file
	echo $devtype > $LUN/cdrom

	# Mass storage hardware name
	echo "Thumbdrives" > $LUN/inquiry_string

	# Add mass storage to the configuration
	ln -s $FUNCTION $CONFIG

	# Link to controller
	echo "$(ls /sys/class/udc)" > $GADGET/UDC || ( echo "Couldn't write to UDC" )
}


if [ "$ACTION" = "mount-mass-storage" ]
then

	[ -d $GADGET ] && remove_gadget $GADGET
	disable_existing_gadgets
	create_gadget "$BACKING_FILE" "0"
fi

if [ "$ACTION" = "mount-iso" ]
then

	[ -d $GADGET ] && remove_gadget $GADGET
	disable_existing_gadgets
	create_gadget "$BACKING_FILE" "1"
fi

if [ "$ACTION" = "umount" ]
then
	[ -d $GADGET ] && remove_gadget $GADGET
fi