#!/bin/bash -e
SERIAL=$(grep Serial /proc/cpuinfo | sed 's/Serial\s*: 0000\(\w*\)/\1/')
BASEMAC="$(echo $(echo ${SERIAL} | cut -b 3-) | sed 's/\(\w\w\)/:\1/g' | cut -b 2-)"
DEV_MAC="02:${BASEMAC}"
HOST_MAC="12:${BASEMAC}"
DIAG_IP="192.168.99.1/30" # Default
MSFILE=''
if [ $(grep "^zefie_diag_ip=" /boot/config.txt | wc -l) -eq 1 ]; then
DIAG_IP="$(grep "^zefie_diag_ip=" /boot/config.txt | cut -d'=' -f2)"
fi
if [ $(grep "^zefie_msd_file=" /boot/config.txt | wc -l) -eq 1 ]; then
MSFILE="$(grep "^zefie_msd_file=" /boot/config.txt | cut -d'=' -f2)"
if [ ! -f "${MSFILE}" ]; then
# Unset since file doesn't exist
MSFILE='';
fi
fi
if [ $(grep "^zefie_enable_configfs=1" /boot/config.txt | wc -l) -eq 1 ]; then
# Use configfs
modprobe libcomposite
CONFIGFS="/sys/kernel/config/usb_gadget"
G="${CONFIGFS}/gadget"
mkdir ${G}
mkdir -p ${G}/strings/0x409
echo 0x1d6b > ${G}/idVendor # Linux Foundation
echo 0x0104 > ${G}/idProduct # Multifunction Composite Gadget
echo 0x0201 > ${G}/bcdDevice # v2.0.1
echo 0x0200 > ${G}/bcdUSB # USB 2.0
echo "${SERIAL}" > ${G}/strings/0x409/serialnumber
echo "Zefie Networks" > ${G}/strings/0x409/manufacturer
echo "Pi Zero Gadget" > ${G}/strings/0x409/product
# Main Config
mkdir -p ${G}/configs/c.1
echo 1 > ${G}/configs/c.1/MaxPower
mkdir -p ${G}/configs/c.1/strings/0x409
echo "Config 1" > ${G}/configs/c.1/strings/0x409/configuration
# Serial 1 (tty)
mkdir -p ${G}/functions/acm.0
ln -s ${G}/functions/acm.0 ${G}/configs/c.1
if [ $(grep "^zefie_second_serial=1" /boot/config.txt | wc -l) -eq 1 ]; then
# Serial 2
mkdir -p ${G}/functions/acm.1
ln -s ${G}/functions/acm.1 ${G}/configs/c.1
fi
# RNDIS Ether
mkdir -p ${G}/functions/rndis.0
echo "${DEV_MAC}" > ${G}/functions/rndis.0/dev_addr
echo "${HOST_MAC}" > ${G}/functions/rndis.0/host_addr
ln -s ${G}/functions/rndis.0 ${G}/configs/c.1
# Mass Storage
if [ ! -z "${MSFILE}" ]; then
mkdir -p ${G}/functions/mass_storage.0
echo 0 > ${G}/functions/mass_storage.0/stall
mkdir -p ${G}/functions/mass_storage.0/lun.0
echo 1 > ${G}/functions/mass_storage.0/lun.0/removable
if [ $(grep "^zefie_msd_cdrom=1" /boot/config.txt | wc -l) -eq 1 ]; then
echo 1 > ${G}/functions/mass_storage.0/lun.0/cdrom
echo 1 > ${G}/functions/mass_storage.0/lun.0/ro
else
echo 0 > ${G}/functions/mass_storage.0/lun.0/cdrom
echo 0 > ${G}/functions/mass_storage.0/lun.0/ro
fi
echo 0 > ${G}/functions/mass_storage.0/lun.0/nofua
echo ${MSFILE} > ${G}/functions/mass_storage.0/lun.0/file
ln -s ${G}/functions/mass_storage.0 ${G}/configs/c.1
fi
# Windows Compatibility
echo 1 > ${G}/os_desc/use
echo "0xcd" > ${G}/os_desc/b_vendor_code
echo "MSFT100" > ${G}/os_desc/qw_sign
echo "RNDIS" > ${G}/functions/rndis.0/os_desc/interface.rndis/compatible_id
echo 5162001 > ${G}/functions/rndis.0/os_desc/interface.rndis/sub_compatible_id
ln -s ${G}/configs/c.1 ${G}/os_desc
# Enable it all
echo 20980000.usb > ${G}/UDC
else
# Use g_ether
modprobe g_ether host_addr=${HOST_MAC} dev_addr=${DEV_MAC}
fi
# We have ethernet regardless, so we set this.
ip addr add ${DIAG_IP} dev usb0
ip link set usb0 up
Comments