2020-03-16 21:13:39 +00:00
|
|
|
## SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
## Init functions for JumpDrive
|
|
|
|
## Copyright (C) 2020 - postmarketOS
|
|
|
|
## Copyright (C) 2020 - Danctl12 <danct12@disroot.org>
|
|
|
|
|
2020-03-13 08:38:01 +00:00
|
|
|
setup_usb_configfs() {
|
|
|
|
# See: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
|
|
|
|
CONFIGFS=/config/usb_gadget
|
|
|
|
|
|
|
|
if ! [ -e "$CONFIGFS" ]; then
|
2020-03-17 18:19:34 +00:00
|
|
|
fatal_error "$CONFIGFS does not exist"
|
2020-03-13 08:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Default values for USB-related deviceinfo variables
|
2020-03-16 11:13:57 +00:00
|
|
|
usb_idVendor="0x1209" # Generic
|
|
|
|
usb_idProduct="0x4201" # Random ID
|
|
|
|
usb_serialnumber="Jumpdrive"
|
2020-03-13 08:38:01 +00:00
|
|
|
usb_rndis_function="rndis.usb0"
|
|
|
|
usb_mass_storage_function="mass_storage.0"
|
|
|
|
|
|
|
|
echo "Setting up an USB gadget through configfs..."
|
|
|
|
# Create an usb gadet configuration
|
2020-03-17 18:19:34 +00:00
|
|
|
mkdir $CONFIGFS/g1 || ( fatal_error "Couldn't create $CONFIGFS/g1" )
|
2020-03-13 08:38:01 +00:00
|
|
|
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
|
2021-01-24 05:17:57 +00:00
|
|
|
echo "$MANUFACTURER" > "$CONFIGFS/g1/strings/0x409/manufacturer"
|
2020-03-13 08:38:01 +00:00
|
|
|
echo "$usb_serialnumber" > "$CONFIGFS/g1/strings/0x409/serialnumber"
|
|
|
|
# shellcheck disable=SC2154
|
2021-01-24 05:17:57 +00:00
|
|
|
echo "$PRODUCT" > "$CONFIGFS/g1/strings/0x409/product"
|
2020-03-13 08:38:01 +00:00
|
|
|
|
|
|
|
# Create rndis/mass_storage function
|
|
|
|
mkdir $CONFIGFS/g1/functions/"$usb_rndis_function" \
|
|
|
|
|| echo " Couldn't create $CONFIGFS/g1/functions/$usb_rndis_function"
|
|
|
|
mkdir $CONFIGFS/g1/functions/"$usb_mass_storage_function" \
|
|
|
|
|| echo " Couldn't create $CONFIGFS/g1/functions/$usb_mass_storage_function"
|
2020-04-11 00:55:59 +00:00
|
|
|
mkdir $CONFIGFS/g1/functions/"$usb_mass_storage_function/lun.1" \
|
|
|
|
|| echo " Couldn't create $CONFIGFS/g1/functions/$usb_mass_storage_function/lun.1"
|
2020-03-13 08:38:01 +00:00
|
|
|
|
|
|
|
# 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 "rndis" > $CONFIGFS/g1/configs/c.1/strings/0x409/configuration \
|
|
|
|
|| echo " Couldn't write configration name"
|
|
|
|
|
2020-03-17 18:19:34 +00:00
|
|
|
# Make sure the node for the eMMC exists
|
|
|
|
if [ -z "$(ls $EMMC)" ]; then
|
|
|
|
fatal_error "$EMMC could not be opened, possible eMMC defect"
|
2020-03-13 08:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Set up mass storage to internal EMMC
|
2020-03-17 18:19:34 +00:00
|
|
|
echo $EMMC > $CONFIGFS/g1/functions/"$usb_mass_storage_function"/lun.0/file
|
2020-04-11 00:55:59 +00:00
|
|
|
echo $SD > $CONFIGFS/g1/functions/"$usb_mass_storage_function"/lun.1/file
|
2020-03-13 08:38:01 +00:00
|
|
|
|
2020-03-25 10:09:35 +00:00
|
|
|
# Rename the mass storage device
|
2020-04-11 00:55:59 +00:00
|
|
|
echo "JumpDrive eMMC" > $CONFIGFS/g1/functions/"$usb_mass_storage_function"/lun.0/inquiry_string
|
|
|
|
echo "JumpDrive microSD" > $CONFIGFS/g1/functions/"$usb_mass_storage_function"/lun.1/inquiry_string
|
2020-03-25 10:09:35 +00:00
|
|
|
|
2020-03-13 08:38:01 +00:00
|
|
|
# Link the rndis/mass_storage instance to the configuration
|
|
|
|
ln -s $CONFIGFS/g1/functions/"$usb_rndis_function" $CONFIGFS/g1/configs/c.1 \
|
|
|
|
|| echo " Couldn't symlink $usb_rndis_function"
|
|
|
|
ln -s $CONFIGFS/g1/functions/"$usb_mass_storage_function" $CONFIGFS/g1/configs/c.1 \
|
|
|
|
|| echo " Couldn't symlink $usb_mass_storage_function"
|
|
|
|
|
|
|
|
# Check if there's an USB Device Controller
|
|
|
|
if [ -z "$(ls /sys/class/udc)" ]; then
|
2020-03-17 18:19:34 +00:00
|
|
|
fatal_error "No USB Device Controller available"
|
2020-03-13 08:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# shellcheck disable=SC2005
|
2020-03-17 18:19:34 +00:00
|
|
|
echo "$(ls /sys/class/udc)" > $CONFIGFS/g1/UDC || ( fatal_error "Couldn't write to UDC" )
|
2020-03-13 08:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_telnetd() {
|
|
|
|
echo "Starting telnet daemon..."
|
|
|
|
{
|
|
|
|
echo "#!/bin/sh"
|
2020-06-01 13:27:12 +00:00
|
|
|
echo "echo \"Welcome to Jumpdrive Shell!\""
|
2020-03-13 08:38:01 +00:00
|
|
|
echo "sh"
|
|
|
|
} >/telnet_connect.sh
|
|
|
|
chmod +x /telnet_connect.sh
|
|
|
|
telnetd -b "${IP}:23" -l /telnet_connect.sh
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
start_udhcpd() {
|
|
|
|
# Only run once
|
|
|
|
[ -e /etc/udhcpd.conf ] && return
|
|
|
|
|
|
|
|
# Get usb interface
|
|
|
|
INTERFACE=""
|
|
|
|
ifconfig rndis0 "$IP" 2>/dev/null && INTERFACE=rndis0
|
|
|
|
if [ -z $INTERFACE ]; then
|
|
|
|
ifconfig usb0 "$IP" 2>/dev/null && INTERFACE=usb0
|
|
|
|
fi
|
|
|
|
if [ -z $INTERFACE ]; then
|
|
|
|
ifconfig eth0 "$IP" 2>/dev/null && INTERFACE=eth0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z $INTERFACE ]; then
|
|
|
|
echo "Could not find an interface to run a DHCP server on, this is not good."
|
|
|
|
echo "Interfaces:"
|
|
|
|
ip link
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Network interface $INTERFACE is used"
|
|
|
|
|
|
|
|
# Create /etc/udhcpd.conf
|
|
|
|
{
|
|
|
|
echo "start 172.16.42.2"
|
|
|
|
echo "end 172.16.42.2"
|
|
|
|
echo "auto_time 0"
|
|
|
|
echo "decline_time 0"
|
|
|
|
echo "conflict_time 0"
|
|
|
|
echo "lease_file /var/udhcpd.leases"
|
|
|
|
echo "interface $INTERFACE"
|
|
|
|
echo "option subnet 255.255.255.0"
|
|
|
|
} >/etc/udhcpd.conf
|
|
|
|
|
|
|
|
echo "Started udhcpd daemon for rescue purposes"
|
|
|
|
udhcpd
|
|
|
|
}
|
|
|
|
|
2020-06-01 16:29:02 +00:00
|
|
|
start_serial_getty() {
|
|
|
|
if [ -n "$SERIAL_CON" ] && [ -n "$SERIAL_BAUD" ]; then
|
|
|
|
# Serial console isn't supposed to be quitted, so if task is finished, relaunch it.
|
|
|
|
sh -c "while true; do getty -l /bin/sh -n $SERIAL_BAUD $SERIAL_CON linux; done" &
|
|
|
|
else
|
|
|
|
echo "Not setting up serial shell, SERIAL_CON and/or SERIAL_BAUD is not defined."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:19:34 +00:00
|
|
|
fatal_error() {
|
|
|
|
clear
|
|
|
|
|
|
|
|
# Move cursor into position for error message
|
|
|
|
echo -e "\033[$ERRORLINES;0H"
|
|
|
|
|
|
|
|
gzip -c -d error.ppm.gz > /error.ppm
|
|
|
|
fbsplash -s /error.ppm
|
|
|
|
|
|
|
|
# Print the error message over the error splash
|
|
|
|
echo " $1"
|
|
|
|
|
|
|
|
loop_forever
|
2020-03-13 08:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop_forever() {
|
|
|
|
while true; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|