-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathS49usbgadget
executable file
·136 lines (115 loc) · 4.56 KB
/
S49usbgadget
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
#!/bin/sh
# Setup Linux USB Gadget for adb, rndis
# this is meant to be placed at /etc/init.d/S49usbgadget on the system_a partition of device
# Available options:
# usb_f_rndis.ko
# usb_f_fs.ko
# usb_f_midi.ko
# usb_f_mtp.ko
# usb_f_ptp.ko
# usb_f_audio_source.ko
# usb_f_accessory.ko
######### Variables
LANG="0x409" # english
SERIAL_NUMBER="12345678"
# 18d1:4e40 Google Inc. Nexus 7
ID_VENDOR="0x18d1"
ID_PRODUCT="0x4e40"
MANUFACTURER="Spotify"
PRODUCT="Superbird"
ADBD_LOG_FILE="/tmp/adbd.log"
# Research
# starting point: https://github.com/frederic/superbird-bulkcmd/blob/main/scripts/enable-adb.sh.client
# info about configfs https://elinux.org/images/e/ef/USB_Gadget_Configfs_API_0.pdf
# info about usbnet and bridging https://developer.ridgerun.com/wiki/index.php/How_to_use_USB_device_networking
# more info, including for windows https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/ethernet-gadget
# a gist that was helpful: https://gist.github.com/geekman/5bdb5abdc9ec6ac91d5646de0c0c60c4
# https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
######### Functions
create_device() {
# create usb gadget device
ID_VEND="$1"
ID_PROD="$2"
BCD_DEVICE="$3"
BCD_USB="$4"
LANGUAGE="$5"
echo "### Creating device $ID_VEND $ID_PROD"
mkdir -p "/dev/usb-ffs"
mkdir -p "/dev/usb-ffs/adb"
mountpoint /sys/kernel/config/ || mount -t configfs none "/sys/kernel/config/"
mkdir -p "/sys/kernel/config/usb_gadget/g1"
echo "$ID_VEND" > "/sys/kernel/config/usb_gadget/g1/idVendor"
echo "$ID_PROD" > "/sys/kernel/config/usb_gadget/g1/idProduct"
echo "$BCD_DEVICE" > "/sys/kernel/config/usb_gadget/g1/bcdDevice"
echo "$BCD_USB" > "/sys/kernel/config/usb_gadget/g1/bcdUSB"
mkdir -p "/sys/kernel/config/usb_gadget/g1/strings/${LANGUAGE}"
sleep 1
}
configure_device() {
# configure usb gadget device
MANUF="$1"
PROD="$2"
SERIAL="$3"
LANGUAGE="$4"
CONFIG_ID="$5"
CONFIG_NAME="$6"
echo "### Configuring device as $MANUF $PROD"
echo "$MANUF" > "/sys/kernel/config/usb_gadget/g1/strings/${LANGUAGE}/manufacturer"
echo "$PROD" > "/sys/kernel/config/usb_gadget/g1/strings/${LANGUAGE}/product"
echo "$SERIAL" > "/sys/kernel/config/usb_gadget/g1/strings/${LANGUAGE}/serialnumber"
mkdir -p "/sys/kernel/config/usb_gadget/g1/configs/${CONFIG_ID}"
mkdir -p "/sys/kernel/config/usb_gadget/g1/configs/${CONFIG_ID}/strings/${LANGUAGE}"
echo "$CONFIG_NAME" > "/sys/kernel/config/usb_gadget/g1/configs/${CONFIG_ID}/strings/${LANGUAGE}/configuration"
echo 500 > "/sys/kernel/config/usb_gadget/g1/configs/${CONFIG_ID}/MaxPower"
ln -s "/sys/kernel/config/usb_gadget/g1/configs/${CONFIG_ID}" "/sys/kernel/config/usb_gadget/g1/os_desc/${CONFIG_ID}"
sleep 1
}
add_function(){
# add a function to existing config id
FUNCTION_NAME="$1"
CONFIG_ID="$2"
echo "### adding function $FUNCTION_NAME to config $CONFIG_ID"
mkdir -p "/sys/kernel/config/usb_gadget/g1/functions/${FUNCTION_NAME}"
ln -s "/sys/kernel/config/usb_gadget/g1/functions/${FUNCTION_NAME}" "/sys/kernel/config/usb_gadget/g1/configs/${CONFIG_ID}"
}
start_adb_daemon() {
# mount adb functionfs and start daemon
LOG_FILE="$1"
echo "### starting adb daemon"
mkdir -p /dev/usb-ffs/adb
mount -t functionfs adb /dev/usb-ffs/adb
if [ ! -f "/usr/bin/adbd" ]; then
echo "Unable to find adbd binary!"
else
/usr/bin/adbd > "$LOG_FILE" 2>&1 &
fi
sleep 5s
}
attach_driver(){
# attach the created gadget device to our UDC driver
UDC_DEVICE=$(/bin/ls -1 /sys/class/udc/) # ff400000.dwc2_a
echo "### Attaching gadget to UDC device: $UDC_DEVICE"
echo "$UDC_DEVICE" > /sys/kernel/config/usb_gadget/g1/UDC
sleep 1
}
configure_usbnet() {
DEVICE="$1"
NETWORK="$2" # just the first 3 octets
NETMASK="$3"
echo "### bringing up $DEVICE with ${NETWORK}.2"
ifconfig "$DEVICE" up
ifconfig "$DEVICE" "${NETWORK}.2" netmask "$NETMASK" broadcast "${NETWORK}.255"
echo "adding routes for $DEVICE"
ip route add default via "${NETWORK}.1" dev "$DEVICE"
sleep 1
}
######### Entrypoint
echo "### Configuring USB Gadget with adb and rndis"
create_device "$ID_VENDOR" "$ID_PRODUCT" "0x0223" "0x0200" "$LANG"
configure_device "$MANUFACTURER" "$PRODUCT" "$SERIAL_NUMBER" "$LANG" "b.1" "Multi-Function Device"
add_function "ffs.adb" "b.1" # adb
add_function "rndis.usb0" "b.1" # rndis usb ethernet
start_adb_daemon "$ADBD_LOG_FILE"
attach_driver
configure_usbnet "usb0" "192.168.7" "255.255.255.0"
echo "Done setting up USB Gadget"