-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-go-gadget
executable file
·175 lines (156 loc) · 4.2 KB
/
go-go-gadget
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
#!/bin/bash
# Source: https://www.hardill.me.uk/wordpress/2019/11/02/pi4-usb-c-gadget/
RED="\e[1;31m"
YLW="\e[1;33m"
FIN="\e[0m"
run_as_root(){
if [ "$USER" != "root" ]; then
echo "Please run this as root or with sudo privileges."
exit 1
fi
}
internet_check(){
if wget -q --spider http://github.com; then :; else echo -e "This script requires an internet connection." && exit 1; fi
}
confirm(){
echo -e "${YLW}If you have no clue as to what USB Gadget"
echo -e "mode is, then please do not run this script${FIN}."
echo -e ""
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) echo && echo -e "${RED}Confirmed${FIN}!";;
n|N ) echo && echo -e "${YLW}You have chosen${FIN}... ${YLW}wisely${FIN}." && exit 0;;
* ) echo && echo -e "${YLW}Invalid choice${FIN}." && exit 0;;
esac
}
setup_overlays(){
sed -i "1 s|$| modules-load=dwc2,g_ether|" "/boot/cmdline.txt"
echo -e "" >> /boot/config.txt && echo -e "# enable USB OTG" >> /boot/config.txt
echo "dtoverlay=dwc2,dr_mode=peripheral" >> /boot/config.txt
echo "libcomposite" >> /etc/modules
}
install_dnsmasq(){
sudo apt update
sudo apt upgrade -y
sudo apt install dnsmasq -y
}
create_usb0(){
mkdir -p /etc/dnsmasq.d
tee /etc/dnsmasq.d/usb <<EOF
interface=usb0
dhcp-range=10.55.0.2,10.55.0.6,255.255.255.248,1h
dhcp-option=3,10.55.0.1
leasefile-ro
EOF
mkdir -p /etc/network/interfaces.d/
tee /etc/network/interfaces.d/usb0 <<EOF
auto usb0
allow-hotplug usb0
iface usb0 inet static
address 10.55.0.1
netmask 255.255.255.248
EOF
}
create_gadget_script(){
tee /usr/local/sbin/gadget-mode <<EOF
#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p pi4
cd pi4
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
echo "fedcba9876543211" > strings/0x409/serialnumber
echo "Ben Hardill" > strings/0x409/manufacturer
echo "PI4 USB Device" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
# Add functions here
# see gadget configurations below
# End functions
mkdir -p functions/ecm.usb0
HOST="00:dc:c8:f7:75:14" # "HostPC"
SELF="00:dd:dc:eb:6d:a1" # "BadUSB"
echo $HOST > functions/ecm.usb0/host_addr
echo $SELF > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
udevadm settle -t 5 || :
ls /sys/class/udc > UDC
ifup usb0
service dnsmasq restart
EOF
chown root:root /usr/local/sbin/gadget-mode
chmod +x /usr/local/sbin/gadget-mode
}
create_gadget_service(){
tee /etc/systemd/system/gadget.service <<EOF
[Unit]
Description=Enable Gadget Mode
ConditionPathExists=/usr/local/sbin/gadget-mode
After=systemd-modules-load.service
[Service]
ExecStart=/usr/local/sbin/gadget-mode &>/dev/null
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=sysinit.target
EOF
systemctl enable gadget
}
add_gadget_init(){
wget -cq --show-progress https://github.com/pyavitz/scripts/raw/master/gadget.init -P /etc/init.d/
if [ -e /etc/init.d/gadget.init ];
then mv -f /etc/init.d/gadget.init /etc/init.d/gadget;
else echo -e "Gadget init install failed!" && exit 0;
fi
if [ -e /etc/init.d/gadget ];
then chmod +x /etc/init.d/gadget && update-rc.d gadget defaults 2
fi
}
# The Magic
internet_check
run_as_root
echo
confirm
echo
echo -e "Pi4 USB-C Gadget Setup"
echo
echo -e "Installing dnsmasq ..."
install_dnsmasq
echo -e "Done."
echo
echo -e "Setting up overlays ..."
setup_overlays > /dev/null 2>&1
sleep 1s
echo -e "Done."
echo
echo -e "Creating usb0 file ..."
create_usb0 > /dev/null 2>&1
sleep 1s
echo -e "Done."
echo
echo -e "Creating gadget script ..."
create_gadget_script > /dev/null 2>&1
sleep 1s
echo -e "Done."
echo
echo -e "Creating gadget service ..."
if [ -e /etc/devuan_version ];
then add_gadget_init;
else create_gadget_service > /dev/null 2>&1;
fi
sleep 1s
echo -e "Done."
echo
echo -e "Install complete!"
echo
echo -e "Your Pi will show up as a ethernet device with an IP address of 10.55.0.1"
echo -e "and should assign the device you plug it into an IP address via DHCP. This"
echo -e "means you can just ssh to username@10.55.0.1 to start using it."
echo