From 29529ea9c1569ca5fee2f677f7478d5dac171d5c Mon Sep 17 00:00:00 2001 From: assaf <445552+asssaf@users.noreply.github.com> Date: Mon, 3 Aug 2026 22:10:00 -0700 Subject: [PATCH] add gadget, getty, dhcpd and hostapd --- .../etc/init.d/rcS.d/10-services-extra.sh | 8 ++ extrarootfs/etc/init.d/rcS.d/20-getty.sh | 27 ++++ extrarootfs/etc/init.d/rcS.d/50-dhcpd.sh | 35 +++++ extrarootfs/etc/init.d/rcS.d/60-hostapd.sh | 32 +++++ extrarootfs/etc/sv/busybox-syslogd/run | 3 - extrarootfs/etc/sv/dhcpd@/log/run | 7 + extrarootfs/etc/sv/dhcpd@/run | 36 +++++ extrarootfs/etc/sv/gadget/finish | 7 + extrarootfs/etc/sv/gadget/log/run | 7 + extrarootfs/etc/sv/gadget/log/supervise | 1 + extrarootfs/etc/sv/gadget/run | 9 ++ extrarootfs/etc/sv/gadget/service | 130 ++++++++++++++++++ extrarootfs/etc/sv/gadget/supervise | 1 + extrarootfs/etc/sv/getty@/log/run | 7 + extrarootfs/etc/sv/getty@/run | 19 +++ extrarootfs/etc/sv/hostapd@/finish | 7 + extrarootfs/etc/sv/hostapd@/log/run | 7 + extrarootfs/etc/sv/hostapd@/run | 49 +++++++ rootfs/etc/init.d/rcS | 5 +- rootfs/etc/init.d/rcS.d/10-services.sh | 8 ++ 20 files changed, 399 insertions(+), 6 deletions(-) create mode 100755 extrarootfs/etc/init.d/rcS.d/10-services-extra.sh create mode 100755 extrarootfs/etc/init.d/rcS.d/20-getty.sh create mode 100755 extrarootfs/etc/init.d/rcS.d/50-dhcpd.sh create mode 100755 extrarootfs/etc/init.d/rcS.d/60-hostapd.sh delete mode 100755 extrarootfs/etc/sv/busybox-syslogd/run create mode 100755 extrarootfs/etc/sv/dhcpd@/log/run create mode 100755 extrarootfs/etc/sv/dhcpd@/run create mode 100755 extrarootfs/etc/sv/gadget/finish create mode 100755 extrarootfs/etc/sv/gadget/log/run create mode 120000 extrarootfs/etc/sv/gadget/log/supervise create mode 100755 extrarootfs/etc/sv/gadget/run create mode 100755 extrarootfs/etc/sv/gadget/service create mode 120000 extrarootfs/etc/sv/gadget/supervise create mode 100755 extrarootfs/etc/sv/getty@/log/run create mode 100755 extrarootfs/etc/sv/getty@/run create mode 100755 extrarootfs/etc/sv/hostapd@/finish create mode 100755 extrarootfs/etc/sv/hostapd@/log/run create mode 100755 extrarootfs/etc/sv/hostapd@/run create mode 100755 rootfs/etc/init.d/rcS.d/10-services.sh diff --git a/extrarootfs/etc/init.d/rcS.d/10-services-extra.sh b/extrarootfs/etc/init.d/rcS.d/10-services-extra.sh new file mode 100755 index 0000000..482dce8 --- /dev/null +++ b/extrarootfs/etc/init.d/rcS.d/10-services-extra.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eu + +for SERVICE in gadget sshd +do + ln -s "/etc/sv/${SERVICE}" /var/service +done diff --git a/extrarootfs/etc/init.d/rcS.d/20-getty.sh b/extrarootfs/etc/init.d/rcS.d/20-getty.sh new file mode 100755 index 0000000..b96620c --- /dev/null +++ b/extrarootfs/etc/init.d/rcS.d/20-getty.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -eu + +function run_service() { + local DEVICE="$1" + local BASE="/etc/sv" + local SERVICE_TEMPLATE="getty@" + local SERVICE="${SERVICE_TEMPLATE}${DEVICE}" + local SERVICE_TEMPLATE_DIR="${BASE}/${SERVICE_TEMPLATE}" + local SERVICE_DIR="${BASE}/${SERVICE}" + + cp -as "${SERVICE_TEMPLATE_DIR}" "${SERVICE_DIR}" + rm -f "${SERVICE_DIR}/supervise" "${SERVICE_DIR}/log/supervise" + + ln -s "${SERVICE_DIR}" /var/service +} + +for i in $(cat /proc/cmdline) +do + case $i in + getty=*) + device=${i#*=} + grep -q "^${device}:" /etc/inittab || run_service $device + ;; + esac +done diff --git a/extrarootfs/etc/init.d/rcS.d/50-dhcpd.sh b/extrarootfs/etc/init.d/rcS.d/50-dhcpd.sh new file mode 100755 index 0000000..72707ad --- /dev/null +++ b/extrarootfs/etc/init.d/rcS.d/50-dhcpd.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +set -eu + +function run_service() { + local DEVICE="$1" + local NETWORK="$2" + local BASE="/etc/sv" + local SERVICE_TEMPLATE="dhcpd@" + local SERVICE="${SERVICE_TEMPLATE}${DEVICE}" + local SERVICE_TEMPLATE_DIR="${BASE}/${SERVICE_TEMPLATE}" + local SERVICE_DIR="${BASE}/${SERVICE}" + + cp -as "${SERVICE_TEMPLATE_DIR}" "${SERVICE_DIR}" + rm -f "${SERVICE_DIR}/supervise" "${SERVICE_DIR}/log/supervise" + cat > "${SERVICE_DIR}/conf" < "${SERVICE_DIR}/conf" <&1 -exec busybox syslogd -n -O - > /dev/ttyAMA0 2>&1 diff --git a/extrarootfs/etc/sv/dhcpd@/log/run b/extrarootfs/etc/sv/dhcpd@/log/run new file mode 100755 index 0000000..3c9241a --- /dev/null +++ b/extrarootfs/etc/sv/dhcpd@/log/run @@ -0,0 +1,7 @@ +#!/bin/sh + +LOG_DIR="$( cd "$(dirname $0)/.." && echo ${PWD} )" +SERVICE_DIR="${LOG_DIR%/log}" +SERVICE_NAME="${SERVICE_DIR##*/}" + +exec vlogger -t "${SERVICE_NAME}" -p daemon diff --git a/extrarootfs/etc/sv/dhcpd@/run b/extrarootfs/etc/sv/dhcpd@/run new file mode 100755 index 0000000..d5aa87a --- /dev/null +++ b/extrarootfs/etc/sv/dhcpd@/run @@ -0,0 +1,36 @@ +#!/bin/sh + +set -eu + +exec 2>&1 + +[ -r ./conf ] && . ./conf + +modprobe brcmfmac + +echo "Waiting for ${DEVICE}..." +while ! grep -q "${DEVICE}:" /proc/net/dev +do + sleep 5 +done + + +echo "Starting dhcp server for ${DEVICE}" + +ifconfig $DEVICE ${NETWORK}.1 netmask 255.255.255.0 broadcast ${NETWORK}.255 up + +cat > "/run/udhcpd-${DEVICE}.conf" <&1 + +exec "$(dirname $0)/service" stop diff --git a/extrarootfs/etc/sv/gadget/log/run b/extrarootfs/etc/sv/gadget/log/run new file mode 100755 index 0000000..3c9241a --- /dev/null +++ b/extrarootfs/etc/sv/gadget/log/run @@ -0,0 +1,7 @@ +#!/bin/sh + +LOG_DIR="$( cd "$(dirname $0)/.." && echo ${PWD} )" +SERVICE_DIR="${LOG_DIR%/log}" +SERVICE_NAME="${SERVICE_DIR##*/}" + +exec vlogger -t "${SERVICE_NAME}" -p daemon diff --git a/extrarootfs/etc/sv/gadget/log/supervise b/extrarootfs/etc/sv/gadget/log/supervise new file mode 120000 index 0000000..cbfa6d5 --- /dev/null +++ b/extrarootfs/etc/sv/gadget/log/supervise @@ -0,0 +1 @@ +/run/runit/supervise.gadget-log \ No newline at end of file diff --git a/extrarootfs/etc/sv/gadget/run b/extrarootfs/etc/sv/gadget/run new file mode 100755 index 0000000..e44534e --- /dev/null +++ b/extrarootfs/etc/sv/gadget/run @@ -0,0 +1,9 @@ +#!/bin/sh + +set -eu + +exec 2>&1 + +$(dirname $0)/service start + +sleep infinity diff --git a/extrarootfs/etc/sv/gadget/service b/extrarootfs/etc/sv/gadget/service new file mode 100755 index 0000000..acd6603 --- /dev/null +++ b/extrarootfs/etc/sv/gadget/service @@ -0,0 +1,130 @@ +#!/bin/sh + +set -eux + +exec 2>&1 + +CONFIG_PATH="/sys/kernel/config" +GADGET_PATH="${CONFIG_PATH}/usb_gadget/pi" + +ID_VENDOR_ETHERNET="0x0bda" +ID_PRODUCT_ETHERNET="0x8153" +ID_VENDOR_LINUX_FOUNDATION="0x1d6b" +ID_PRODUCT_COMPOSITE="0x0104" + +ID_VENDOR="$ID_VENDOR_ETHERNET" +ID_PRODUCT="$ID_PRODUCT_ETHERNET" +SERIAL_NUMBER="fedcba9876543210" + +function enable() { + ls /sys/class/udc > "${GADGET_PATH}/UDC" +} + + +function disable() { + echo "" > "${GADGET_PATH}/UDC" +} + + +function create_configuration() { + mkdir -p "${GADGET_PATH}/configs/c.1/strings/0x409" + echo "Config 1: ECM network" > "${GADGET_PATH}/configs/c.1/strings/0x409/configuration" + echo 250 > "${GADGET_PATH}/configs/c.1/MaxPower" +} + + +function remove_configuration() { + rmdir "${GADGET_PATH}/configs/c.1/strings/0x409" + rmdir "${GADGET_PATH}/configs/c.1" +} + + + +function create_serial_function() { + mkdir -p "${GADGET_PATH}/functions/acm.usb0" + ln -s "${GADGET_PATH}/functions/acm.usb0" "${GADGET_PATH}/configs/c.1/" +} + + +function remove_serial_function() { + rm "${GADGET_PATH}/configs/c.1/acm.usb0" + rmdir "${GADGET_PATH}/functions/acm.usb0" +} + + +function create_ethernet_function() { + mkdir -p "${GADGET_PATH}/functions/ecm.usb0" + # first byte of address must be even + #HOST="48:6f:73:74:50:43" # "HostPC" + #SELF="42:61:64:55:53:42" # "BadUSB" + #echo $HOST > "${GADGET_PATH}/functions/ecm.usb0/host_addr" + #echo $SELF > "${GADGET_PATH}/functions/ecm.usb0/dev_addr" + ln -s "${GADGET_PATH}/functions/ecm.usb0" "${GADGET_PATH}/configs/c.1/" +} + + +function remove_ethernet_function() { + rm "${GADGET_PATH}/configs/c.1/ecm.usb0" + rmdir "${GADGET_PATH}/functions/ecm.usb0" +} + + +function create_gadget() { + mkdir "$GADGET_PATH" + + echo "$ID_VENDOR" > "${GADGET_PATH}/idVendor" + echo "$ID_PRODUCT" > "${GADGET_PATH}/idProduct" + + echo 0x0100 > "${GADGET_PATH}/bcdDevice" # v1.0.0 + echo 0x0200 > "${GADGET_PATH}/bcdUSB" # USB2 + + mkdir -p "${GADGET_PATH}/strings/0x409" + echo "$SERIAL_NUMBER" > "${GADGET_PATH}/strings/0x409/serialnumber" + echo "nobody" > "${GADGET_PATH}/strings/0x409/manufacturer" + echo "Composite USB Device" > "${GADGET_PATH}/strings/0x409/product" + +} + + +function remove_gadget() { + rmdir "${GADGET_PATH}/strings/0x409" + sleep 1 + rmdir "${GADGET_PATH}" +} + + +function start() { + modprobe libcomposite + mount -t configfs none "$CONFIG_PATH" + + create_gadget + create_configuration + create_serial_function + create_ethernet_function + enable + sv up getty@ttyGS0 +} + + +function stop() { + sv down getty@ttyGS0 + ifconfig usb0 down + disable + + remove_ethernet_function + remove_serial_function + remove_configuration + remove_gadget + + umount "$CONFIG_PATH" +} + + +case $1 in + start|stop|enable|disable) + $1 + ;; + *) + echo "Invalid command: $1" + ;; +esac diff --git a/extrarootfs/etc/sv/gadget/supervise b/extrarootfs/etc/sv/gadget/supervise new file mode 120000 index 0000000..b53eea6 --- /dev/null +++ b/extrarootfs/etc/sv/gadget/supervise @@ -0,0 +1 @@ +/run/runit/supervise.gadget \ No newline at end of file diff --git a/extrarootfs/etc/sv/getty@/log/run b/extrarootfs/etc/sv/getty@/log/run new file mode 100755 index 0000000..3c9241a --- /dev/null +++ b/extrarootfs/etc/sv/getty@/log/run @@ -0,0 +1,7 @@ +#!/bin/sh + +LOG_DIR="$( cd "$(dirname $0)/.." && echo ${PWD} )" +SERVICE_DIR="${LOG_DIR%/log}" +SERVICE_NAME="${SERVICE_DIR##*/}" + +exec vlogger -t "${SERVICE_NAME}" -p daemon diff --git a/extrarootfs/etc/sv/getty@/run b/extrarootfs/etc/sv/getty@/run new file mode 100755 index 0000000..22d8ff0 --- /dev/null +++ b/extrarootfs/etc/sv/getty@/run @@ -0,0 +1,19 @@ +#!/bin/sh + +set -eu + +exec 2>&1 + +SERVICE_DIR="$( cd "$(dirname $0)" && echo ${PWD} )" +SERVICE_NAME="${SERVICE_DIR##*/}" +DEVICE="${SERVICE_NAME##*@}" + +while [ ! -e "/dev/$DEVICE" ] +do + echo "Waiting for ${DEVICE}..." + sleep 5 +done + +echo "Starting getty for ${DEVICE}" + +exec /sbin/getty -L "/dev/${DEVICE}" 115200 vt100 diff --git a/extrarootfs/etc/sv/hostapd@/finish b/extrarootfs/etc/sv/hostapd@/finish new file mode 100755 index 0000000..8a6cd33 --- /dev/null +++ b/extrarootfs/etc/sv/hostapd@/finish @@ -0,0 +1,7 @@ +#!/bin/sh + +set -eu + +exec 2>&1 + +grep -q "ap0:" /proc/net/dev && iw dev ap0 del diff --git a/extrarootfs/etc/sv/hostapd@/log/run b/extrarootfs/etc/sv/hostapd@/log/run new file mode 100755 index 0000000..3c9241a --- /dev/null +++ b/extrarootfs/etc/sv/hostapd@/log/run @@ -0,0 +1,7 @@ +#!/bin/sh + +LOG_DIR="$( cd "$(dirname $0)/.." && echo ${PWD} )" +SERVICE_DIR="${LOG_DIR%/log}" +SERVICE_NAME="${SERVICE_DIR##*/}" + +exec vlogger -t "${SERVICE_NAME}" -p daemon diff --git a/extrarootfs/etc/sv/hostapd@/run b/extrarootfs/etc/sv/hostapd@/run new file mode 100755 index 0000000..e9715cb --- /dev/null +++ b/extrarootfs/etc/sv/hostapd@/run @@ -0,0 +1,49 @@ +#!/bin/sh +exec 2>&1 + +[ -r "./conf" ] && . ./conf + +: ${SSID:=test} +: ${PASSPHRASE:=testtest} +: ${MODE:=g} +: ${VIF:=ap0} + +echo "Waiting for ${DEVICE}..." +while ! grep -q "${DEVICE}:" /proc/net/dev +do + sleep 5 +done + + +echo "Starting hostapd for ${DEVICE}" + +# get phy index +INDEX="$(iw dev $DEVICE info | sed -n 's/^.*wiphy \([[:digit:]]\+\).*$/\1/p')" + +iw phy phy${INDEX} interface add ${VIF} type __ap + +# get current wpa channel: +CHANNEL="$(iw dev $DEVICE info |sed -n 's/^.*channel \([[:digit:]]\+\).*$/\1/p')" +echo "${DEVICE} is using channel ${CHANNEL}" + + +cat > /run/hostapd-${DEVICE}.conf <&1 diff --git a/rootfs/etc/init.d/rcS b/rootfs/etc/init.d/rcS index 21c0881..94d449c 100755 --- a/rootfs/etc/init.d/rcS +++ b/rootfs/etc/init.d/rcS @@ -16,8 +16,7 @@ echo -e "$PASSWORD\n$PASSWORD" | /usr/sbin/passwd "$USER" echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/user -# this can be moved to "once" -for SERVICE in busybox-klogd busybox-syslogd dhcpc busybox-ntpd +for f in /etc/init.d/rcS.d/* do - ln -s "/etc/sv/${SERVICE}" /var/service + [ -x "$f" ] && $f done diff --git a/rootfs/etc/init.d/rcS.d/10-services.sh b/rootfs/etc/init.d/rcS.d/10-services.sh new file mode 100755 index 0000000..ff3b531 --- /dev/null +++ b/rootfs/etc/init.d/rcS.d/10-services.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eu + +for SERVICE in busybox-klogd busybox-syslogd dhcpc busybox-ntpd +do + ln -s "/etc/sv/${SERVICE}" /var/service +done