main/postmarketos-initramfs: support reading USB network UDC name from deviceinfo (MR 4750)

Before this change the script assumed that the device has only one UDC.
However it is possible for a device to have multiple UDCs, in which case
the deviceinfo is the appropriate place to specify which one should be used.
This change adds an optional deviceinfo variable `deviceinfo_usb_network_udc`
to specify the UDC that should be used.

Also, before this change the script assumed the USB network interface name is
"usb0". But other gadget drivers like `g_cdc` can claim USB interfaces
before the initramfs script gets around to creating one for USB networking,
so the interface might be "usb1" or something else. The configfs gadget itself
can provide the actual interface name, so this change makes the script
use that.
This commit is contained in:
Arnav Singh 2024-01-17 12:43:16 -08:00 committed by Clayton Craft
parent cf69326222
commit 2a0fab1a77
No known key found for this signature in database
GPG key ID: 4A4CED6D7EDF950A
2 changed files with 26 additions and 15 deletions

View file

@ -1,7 +1,7 @@
# Maintainer: Oliver Smith <ollieparanoid@postmarketos.org>
# Co-Maintainer: Clayton Craft <clayton@craftyguy.net>
pkgname=postmarketos-initramfs
pkgver=2.4.0
pkgver=2.5.0
pkgrel=0
pkgdesc="Base files for the postmarketOS initramfs / initramfs-extra"
url="https://postmarketos.org"
@ -88,7 +88,7 @@ sha512sums="
ab41b45b0613f25a61114ed8c8b92bc53c60838f6e2e0ba18c76e5369b2984e6023a0661887692673aca3f647f268c468a468f6b1ac424cfee609017a89481dd 00-initramfs-base.files
8a4adad3785af474b36a09a05f6a3b2c4b4f43aac331a53b903abfa51ea12be1e3d1d807b7a6e66a1346815f3b0044daf8cd62e21e2dc75d2db13ee265a72985 00-initramfs-extra-base.files
4bfcb41b2d5ccd577a6203a9479e5bdf056064d20f02f9120f1a074c7c280c0231cf380ed6be7c80733ed4ae101385c14f123a8a41780bbb7d0851387cd9c613 init.sh
58ee2b36b858c9c70fabbcb9a12a754afc97e6dfe09ddb7a2ec02c6fb593cc40677ae7049ba5ffda7797cba20e4d35f0d320c5fb4d3072cdbdf172cce6665bb0 init_functions.sh
87602909fa9f233385528e41e5395cce076a4d22df858b56b333457815c6978f1b9ca918f0c9ec56f4424ed6c2624d0c6054eaf6e687d60175952cc35c010098 init_functions.sh
ba3275a9af788c7c782322a22a0f144d5e50e3498ea6886486a29331f23ae89cd32d500a3635cfa7cab369afba92edc18aeca64ccbf0cd589061cce23d15b46c unudhcpd.conf
675e7d5bee39b2df7d322117f8dcaccc274d61beaf4d50ead19bbf2109446d64b1c0aa0c5b4f9846eb6c1c403418f28f6364eff4537ba41120fbfcbc484b7da7 mdev.conf
"

View file

@ -570,17 +570,19 @@ setup_usb_network_android() {
setup_usb_configfs_udc() {
# Check if there's an USB Device Controller
local _udc_dev
_udc_dev=$(ls /sys/class/udc)
local _udc_dev="${deviceinfo_usb_network_udc:-}"
if [ -z "$_udc_dev" ]; then
echo " No USB Device Controller available"
return
_udc_dev=$(ls /sys/class/udc)
if [ -z "$_udc_dev" ]; then
echo " No USB Device Controller available"
return
fi
fi
# Remove any existing UDC to avoid "write error: Resource busy" when setting UDC again
echo "" > /config/usb_gadget/g1/UDC || echo " Couldn't write to clear UDC"
# Link the gadget instance to an USB Device Controller. This activates the gadget.
# See also: https://github.com/postmarketOS/pmbootstrap/issues/338
# See also: https://gitlab.com/postmarketOS/pmbootstrap/issues/338
echo "$_udc_dev" > /config/usb_gadget/g1/UDC || echo " Couldn't write new UDC"
}
@ -672,17 +674,26 @@ start_unudhcpd() {
local host_ip="${unudhcpd_host_ip:-172.16.42.1}"
local client_ip="${unudhcpd_client_ip:-172.16.42.2}"
echo "Starting unudhcpd with server ip $host_ip, client ip: $client_ip"
# Get usb interface
INTERFACE=""
ifconfig rndis0 "$host_ip" 2>/dev/null && INTERFACE=rndis0
if [ -z $INTERFACE ]; then
ifconfig usb0 "$host_ip" 2>/dev/null && INTERFACE=usb0
fi
if [ -z $INTERFACE ]; then
ifconfig eth0 "$host_ip" 2>/dev/null && INTERFACE=eth0
usb_network_function="${deviceinfo_usb_network_function:-ncm.usb0}"
usb_network_function_fallback="rndis.usb0"
INTERFACE="$(
cat "/config/usb_gadget/g1/functions/$usb_network_function/ifname" 2>/dev/null ||
cat "/config/usb_gadget/g1/functions/$usb_network_function_fallback/ifname" 2>/dev/null ||
echo ''
)"
if [ -n "$INTERFACE" ]; then
ifconfig "$INTERFACE" "$host_ip"
elif ifconfig rndis0 "$host_ip" 2>/dev/null; then
INTERFACE=rndis0
elif ifconfig usb0 "$host_ip" 2>/dev/null; then
INTERFACE=usb0
elif ifconfig eth0 "$host_ip" 2>/dev/null; then
INTERFACE=eth0
fi
if [ -z $INTERFACE ]; then
if [ -z "$INTERFACE" ]; then
echo " Could not find an interface to run a dhcp server on"
echo " Interfaces:"
ip link