Add ADB over USB support (#417)

* figuring out adb device agent

* start adbd service in comma.sh

* implement thundercomm adb implementation

* UDC check

* setup_gadget_mode

* usb gadget created

* adb device shown

* android.rules

* check functionfs mount + add serial number

* 250 maxPower

* lil more

* fix

* doesn't seem to be needed

* cleanup

---------

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
This commit is contained in:
Mauricio Alvarez Leon
2025-01-23 16:31:22 -08:00
committed by GitHub
parent f08dee8d9d
commit 4e97605c0e
6 changed files with 86 additions and 0 deletions
+1
View File
@@ -113,6 +113,7 @@ rm -rf /var/lib/apt/lists/*
# Create dirs
mkdir /data && chown $USERNAME:$USERNAME /data
mkdir /persist && chown $USERNAME:$USERNAME /persist
mkdir /config && chown root:root /config
# Disable pstore service that moves files out of /sys/fs/pstore
systemctl disable systemd-pstore.service
+5
View File
@@ -0,0 +1,5 @@
[Path]
PathModified=/data/params/d/AdbEnabled
[Install]
WantedBy=multi-user.target
+10
View File
@@ -0,0 +1,10 @@
[Unit]
Description=ADB param watcher
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/comma/set_adb.sh
[Install]
WantedBy=multi-user.target
+2
View File
@@ -21,6 +21,8 @@ systemctl enable power_drop_monitor.service
systemctl enable brightnessd.service
systemctl enable ssh-param-watcher.path
systemctl enable ssh-param-watcher.service
systemctl enable adb-param-watcher.path
systemctl enable adb-param-watcher.service
systemctl enable logrotate-hourly.timer
# Disable some of our services
+2
View File
@@ -33,10 +33,12 @@ handle_setup_keys () {
ln -s /data/params/d_tmp /data/params/d
fi
echo -n 1 > /data/params/d/AdbEnabled
echo -n 1 > /data/params/d/SshEnabled
cp /usr/comma/setup_keys /data/params/d/GithubSshKeys
elif [[ -e /data/params/d/GithubSshKeys && -e /data/continue.sh ]]; then
if cmp -s /data/params/d/GithubSshKeys /usr/comma/setup_keys; then
rm /data/params/d/AdbEnabled
rm /data/params/d/SshEnabled
rm /data/params/d/GithubSshKeys
fi
+66
View File
@@ -0,0 +1,66 @@
#!/bin/bash
setup_gadget() {
# Check if /config is already mounted
if ! mountpoint -q /config; then
sudo mount -t configfs none /config
else
echo "/config is already mounted."
fi
# Create USB gadget directory structure
sudo mkdir -p /config/usb_gadget/g1
cd /config/usb_gadget/g1
sudo mkdir -p strings/0x409
sudo mkdir -p configs/c.1/strings/0x409
sudo mkdir -p functions/ncm.0
# Set Vendor and Product ID
echo 0x04D8 | sudo tee idVendor
echo 0x1234 | sudo tee idProduct
# Set strings
echo "$(cat /proc/cmdline | sed -e 's/^.*androidboot.serialno=//' -e 's/ .*$//')" | sudo tee strings/0x409/serialnumber
echo "comma.ai" | sudo tee strings/0x409/manufacturer
echo "Linux USB Gadget" | sudo tee strings/0x409/product
echo 250 | sudo tee configs/c.1/MaxPower
# Create ADB function
sudo mkdir -p functions/ffs.adb
sudo mkdir -p /dev/usb-ffs/adb
if ! mountpoint -q /dev/usb-ffs/adb; then
sudo mount -t functionfs adb /dev/usb-ffs/adb
else
echo "/dev/usb-ffs/adb is already mounted"
fi
# Link both functions to configuration
echo "NCM+ADB" | sudo tee configs/c.1/strings/0x409/configuration
sudo rm -f configs/c.1/ncm.0
sudo rm -f configs/c.1/ffs.adb
sudo ln -s functions/ncm.0 configs/c.1/
sudo ln -s functions/ffs.adb configs/c.1/
}
start_gadget() {
cd /config/usb_gadget/g1
echo "a600000.dwc3" | sudo tee UDC
}
stop_gadget() {
cd /config/usb_gadget/g1
echo "" | sudo tee UDC
}
ADB_PARAM="/data/params/d/AdbEnabled"
if [ -f "$ADB_PARAM" ] && [ "$(< $ADB_PARAM)" == "1" ]; then
echo "Enabling ADB"
setup_gadget
systemctl start adbd
sleep 1
start_gadget
else
echo "Disabling ADB"
systemctl stop adbd
stop_gadget
fi