use common usb gadget network scripts

This commit is contained in:
amazingfate
2024-01-30 01:07:06 +08:00
committed by Jianfeng Liu
parent 582d0e96b9
commit aa2c1f5e39
8 changed files with 11 additions and 158 deletions

View File

@@ -39,9 +39,9 @@ function post_family_tweaks_bsp__xiaomi-elish_firmware() {
# USB Gadget Network service
mkdir -p $destination/usr/local/bin/
mkdir -p $destination/usr/lib/systemd/system/
install -Dm655 $SRC/packages/bsp/xiaomi-elish/setup-usbgadget-network.sh $destination/usr/local/bin/
install -Dm655 $SRC/packages/bsp/xiaomi-elish/remove-usbgadget-network.sh $destination/usr/local/bin/
install -Dm644 $SRC/packages/bsp/xiaomi-elish/usbgadget-rndis.service $destination/usr/lib/systemd/system/
install -Dm655 $SRC/packages/bsp/usb-gadget-network/setup-usbgadget-network.sh $destination/usr/local/bin/
install -Dm655 $SRC/packages/bsp/usb-gadget-network/remove-usbgadget-network.sh $destination/usr/local/bin/
install -Dm644 $SRC/packages/bsp/usb-gadget-network/usbgadget-rndis.service $destination/usr/lib/systemd/system/
# Kernel postinst script to update abl boot partition
install -Dm655 $SRC/packages/bsp/xiaomi-elish/zz-update-abl-kernel $destination/etc/kernel/postinst.d/

View File

@@ -39,9 +39,9 @@ function post_family_tweaks_bsp__xiaomi-umi_firmware() {
# USB Gadget Network service
mkdir -p $destination/usr/local/bin/
mkdir -p $destination/usr/lib/systemd/system/
install -Dm655 $SRC/packages/bsp/xiaomi-umi/setup-usbgadget-network.sh $destination/usr/local/bin/
install -Dm655 $SRC/packages/bsp/xiaomi-umi/remove-usbgadget-network.sh $destination/usr/local/bin/
install -Dm644 $SRC/packages/bsp/xiaomi-umi/usbgadget-rndis.service $destination/usr/lib/systemd/system/
install -Dm655 $SRC/packages/bsp/usb-gadget-network/setup-usbgadget-network.sh $destination/usr/local/bin/
install -Dm655 $SRC/packages/bsp/usb-gadget-network/remove-usbgadget-network.sh $destination/usr/local/bin/
install -Dm644 $SRC/packages/bsp/usb-gadget-network/usbgadget-rndis.service $destination/usr/lib/systemd/system/
# Kernel postinst script to update abl boot partition
install -Dm655 $SRC/packages/bsp/xiaomi-umi/zz-update-abl-kernel $destination/etc/kernel/postinst.d/

View File

@@ -1,11 +1,10 @@
#!/bin/bash
deviceinfo_name="Xiaomi Mi 10"
deviceinfo_manufacturer="Xiaomi"
deviceinfo_codename="xiaomi-umi"
deviceinfo_name="Armbian USB Gadget Network"
deviceinfo_manufacturer="Armbian"
#deviceinfo_usb_idVendor=
#deviceinfo_usb_idProduct=
deviceinfo_usb_serialnumber="xiaomi-umi"
#deviceinfo_usb_serialnumber=
setup_usb_network_configfs() {
# See: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
@@ -48,11 +47,11 @@ setup_usb_network_configfs() {
echo " Couldn't create $CONFIGFS/g1/configs/c.1/strings/0x409"
echo "USB network" > $CONFIGFS/g1/configs/c.1/strings/0x409/configuration ||
echo " Couldn't write configration name"
# Link the network instance to the configuration
ln -s $CONFIGFS/g1/functions/"$usb_network_function" $CONFIGFS/g1/configs/c.1 ||
echo " Couldn't symlink $usb_network_function"
echo 0xEF > $CONFIGFS/g1/functions/"$usb_network_function"/class ||
echo " Couldn't write class"
echo 0x04 > $CONFIGFS/g1/functions/"$usb_network_function"/subclass ||

View File

@@ -1,95 +0,0 @@
#!/bin/bash
deviceinfo_name="Xiaomi Mi Pad 5 Pro"
deviceinfo_manufacturer="Xiaomi"
deviceinfo_codename="xiaomi-elish"
#deviceinfo_usb_idVendor=
#deviceinfo_usb_idProduct=
deviceinfo_usb_serialnumber="xiaomi-elish"
setup_usb_network_configfs() {
# See: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
CONFIGFS=/sys/kernel/config/usb_gadget
if ! [ -e "$CONFIGFS" ]; then
echo " $CONFIGFS does not exist, skipping configfs usb gadget"
return
fi
# Default values for USB-related deviceinfo variables
usb_idVendor="${deviceinfo_usb_idVendor:-0x18D1}" # default: Google Inc.
usb_idProduct="${deviceinfo_usb_idProduct:-0xD001}" # default: Nexus 4 (fastboot)
usb_serialnumber="${deviceinfo_usb_serialnumber:-postmarketOS}"
usb_network_function="rndis.0"
echo " Setting up an USB gadget through configfs"
# Create an usb gadet configuration
mkdir $CONFIGFS/g1 || echo " Couldn't create $CONFIGFS/g1"
echo "$usb_idVendor" > "$CONFIGFS/g1/idVendor"
echo "$usb_idProduct" > "$CONFIGFS/g1/idProduct"
# Create english (0x409) strings
mkdir $CONFIGFS/g1/strings/0x409 || echo " Couldn't create $CONFIGFS/g1/strings/0x409"
# shellcheck disable=SC2154
echo "$deviceinfo_manufacturer" > "$CONFIGFS/g1/strings/0x409/manufacturer"
echo "$usb_serialnumber" > "$CONFIGFS/g1/strings/0x409/serialnumber"
# shellcheck disable=SC2154
echo "$deviceinfo_name" > "$CONFIGFS/g1/strings/0x409/product"
# Create network function.
mkdir $CONFIGFS/g1/functions/"$usb_network_function" ||
echo " Couldn't create $CONFIGFS/g1/functions/$usb_network_function"
# Create configuration instance for the gadget
mkdir $CONFIGFS/g1/configs/c.1 ||
echo " Couldn't create $CONFIGFS/g1/configs/c.1"
mkdir $CONFIGFS/g1/configs/c.1/strings/0x409 ||
echo " Couldn't create $CONFIGFS/g1/configs/c.1/strings/0x409"
echo "USB network" > $CONFIGFS/g1/configs/c.1/strings/0x409/configuration ||
echo " Couldn't write configration name"
# Link the network instance to the configuration
ln -s $CONFIGFS/g1/functions/"$usb_network_function" $CONFIGFS/g1/configs/c.1 ||
echo " Couldn't symlink $usb_network_function"
# Check if there's an USB Device Controller
if [ -z "$(ls /sys/class/udc)" ]; then
echo " No USB Device Controller available"
return
fi
# Link the gadget instance to an USB Device Controller. This activates the gadget.
# See also: https://github.com/postmarketOS/pmbootstrap/issues/338
# shellcheck disable=SC2005
echo "$(ls /sys/class/udc)" > $CONFIGFS/g1/UDC || echo " Couldn't write UDC"
}
set_usbgadget_ipaddress() {
local host_ip="${unudhcpd_host_ip:-172.16.42.1}"
local client_ip="${unudhcpd_client_ip:-172.16.42.2}"
echo "Starting dnsmasq with server ip $host_ip, client ip: $client_ip"
# Get usb interface
INTERFACE=""
ip a add "${host_ip}/255.255.0.0" dev rndis0 2> /dev/null && ip link set rndis0 up && INTERFACE=rndis0
if [ -z $INTERFACE ]; then
ip a add "${host_ip}/255.255.0.0" dev usb0 2> /dev/null && ip link set usb0 up && INTERFACE=usb0
fi
if [ -z $INTERFACE ]; then
ip a add "${host_ip}/255.255.0.0" dev eth0 2> /dev/null && eth0 && INTERFACE=eth0
fi
if [ -z $INTERFACE ]; then
echo " Could not find an interface to run a dhcp server on"
echo " Interfaces:"
ip link
return
fi
echo " Using interface $INTERFACE"
echo " Starting the DHCP daemon"
ip a show $INTERFACE > /var/log/unudhcpd.log
nohup /usr/bin/unudhcpd -i "$INTERFACE" -s "$host_ip" -c "$client_ip" 2>&1 >> /var/log/unudhcpd.log &
}
setup_usb_network_configfs
set_usbgadget_ipaddress

View File

@@ -1,37 +0,0 @@
#!/bin/sh
SYSDIR=/sys/kernel/config/usb_gadget/
DEVDIR=$SYSDIR/g1
[ -d $DEVDIR ] || exit
echo '' > $DEVDIR/UDC
echo "Removing strings from configurations"
for dir in $DEVDIR/configs/*/strings/*; do
[ -d $dir ] && rmdir $dir
done
echo "Removing functions from configurations"
for func in $DEVDIR/configs/*.*/*.*; do
[ -e $func ] && rm $func
done
echo "Removing configurations"
for conf in $DEVDIR/configs/*; do
[ -d $conf ] && rmdir $conf
done
echo "Removing functions"
for func in $DEVDIR/functions/*.*; do
[ -d $func ] && rmdir $func
done
echo "Removing strings"
for str in $DEVDIR/strings/*; do
[ -d $str ] && rmdir $str
done
echo "Removing gadget"
rmdir $DEVDIR
pkill unudhcpd

View File

@@ -1,14 +0,0 @@
[Unit]
Description=USB Gadget RNDIS Network
After=modprobe@usbfunc:rndis.service NetworkManager.service systemd-networkd.service
Before=dnsmasq.service
Wants=modprobe@usbfunc:rndis.service
[Service]
Type=oneshot
RemainAfterExit=yes
SyslogIdentifier=gadget-func-rndis
WorkingDirectory=/sys/kernel/config/usb_gadget
ExecStart=/usr/local/bin/setup-usbgadget-network.sh
ExecStop=/usr/local/bin/remove-usbgadget-network.sh
[Install]
WantedBy=usb-gadget.target