1.17. USB support¶
1.17.1. USB host support¶
barebox has support for both USB host and USB device mode. USB devices take a long time to probe, so they are not probed automatically. Probing has to be triggered using the usb - (re-)detect USB devices or detect - detect devices command. USB devices in barebox are not hot-pluggable. It is expected that USB devices are not disconnected while barebox is running.
1.17.1.1. USB Networking¶
barebox supports r8152, ASIX-compatible devices, the SMSC95xx and CDC EEM devices. After detection, the device shows up as an extra network device (e.g. eth1) and can be used like a regular network device.
For CDC EEM host support, see EEM host.
To use a USB network device together with the ifup - bring a network interface up command, add the
following to /env/network/eth0-discover:
#!/bin/sh
usb
Alternatively, a detect -a (all) can be forced in ifup by setting
global.net.ifup_force_detect=1.
1.17.1.2. USB mass storage¶
barebox supports USB mass storage devices. After probing them with the usb - (re-)detect USB devices
command, they show up as /dev/diskx and can be used like any other device.
1.17.2. USB device support¶
barebox supports several different USB gadget drivers:
Device Firmware Upgrade (DFU)
Android Fastboot
CDC EEM (Ethernet Emulation Model)
USB mass storage
serial gadget
The recommended way to use USB gadget is with the usbgadget - Create USB Gadget multifunction device command. While there are individual commands for dfu - device firmware update and usbserial - serial gadget enable/disable, the usbgadget - Create USB Gadget multifunction device command supports registering composite gadgets, which export multiple functions at once. This happens in the “background” without impacting use of the shell.
1.17.2.1. Partition description¶
The USB gadget commands for Android Fastboot, DFU and the mass storage gadget
take a partition description which describes which barebox partitions are exported via USB.
The partition description is referred to as <desc> in the command help texts. It has
the general form partition(name)flags,partition(name)flags,....
The partition field is the partition as accessible in barebox. This can be a
path in /dev/, but could also be a regular file.
The name field is the name under which the partition shall be exported. This is the name under which the partition can be found with the host tool.
Several flags are supported, each denoted by a single character:
sSafe mode. The file is downloaded completely before it is written (DFU specific)rReadback. The partition is allowed to be read back (DFU specific)cThe file shall be created if it doesn’t exist. Needed when a regular file is exported.uThe partition is a MTD device and shall be flashed with a UBI image.oThe partition is optional, i.e. if it is not available at initialization time, it is skippedinstead of aborting the initialization. This is currently only supported for fastboot.
Example:
/dev/nand0.barebox.bb(barebox)sr,/kernel(kernel)rc
Board code authors are encouraged to provide a default environment containing partitions with descriptive names. For boards where this is not specified, there exist a number of partition specifiers for automatically generating entries:
blockexports all registered block devices (e.g. eMMC and SD)autocurrently equivalent toblock. May be extended to other flashabledevices, like EEPROMs, MTD or UBI volumes in future
Example usage of exporting registered block devices, barebox update handlers and a single file that is created on flashing:
detect -a # optional. Detects everything, so auto can register it
usbgadget -A auto,/tmp/fitimage(fitimage)c -b
1.17.2.2. DFU¶
USB Device Firmware Upgrade (DFU) is an official USB device class specification of the USB Implementers Forum. It provides a vendor-independent way to update the firmware of embedded devices. The current specification is version 1.1 and can be downloaded here: http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf
On the barebox side, the update is handled with the usbgadget - Create USB Gadget multifunction device or the dfu - device firmware update command.
On the host side, the tool dfu-util can be used to update the partitions. It is available for most distributions and typically supports the following options:
dfu-util -h
Usage: dfu-util [options] ...
-h --help Print this help message
-V --version Print the version number
-v --verbose Print verbose debug statements
-l --list List the currently attached DFU capable USB devices
-e --detach Detach the currently attached DFU capable USB devices
-d --device vendor:product Specify Vendor/Product ID of DFU device
-p --path bus-port. ... .port Specify path to DFU device
-c --cfg config_nr Specify the Configuration of DFU device
-i --intf intf_nr Specify the DFU Interface number
-a --alt alt Specify the Altsetting of the DFU Interface
by name or by number
-t --transfer-size Specify the number of bytes per USB Transfer
-U --upload file Read firmware from device into <file>
-D --download file Write firmware from <file> into device
-R --reset Issue USB Reset signalling once we're finished
-s --dfuse-address address ST DfuSe mode, specify target address for
raw file download or upload. Not applicable for
DfuSe file (.dfu) downloads
To update the kernel for the above example, you would use something like the following:
dfu-util -D arch/arm/boot/zImage -a kernel
The dfu-util command automatically finds DFU-capable devices. If there are
multiple devices found, you need to identify one with the -d/-p options.
1.17.2.3. USB serial console¶
barebox can provide a serial console over USB. This can be initialized with the
usbserial - serial gadget enable/disable command. Once the host is plugged in it should show a
new serial device, on Linux for example /dev/ttyACM0.
1.17.2.4. Android Fastboot support¶
barebox has support for the android fastboot protocol. There is no dedicated command for initializing the fastboot protocol, instead it is integrated into the Multifunction Composite Gadget, see usbgadget - Create USB Gadget multifunction device for a usage description.
The Fastboot gadget supports the following commands:
fastboot flash
fastboot getvar
fastboot boot
fastboot reboot
fastboot erase
fastboot flash additionally supports image types UBI and Barebox. For UBI
Images and a MTD device as target, ubiformat is called. For a Barebox image
with an available barebox update handler for the fastboot exported device, the
barebox_update is called (exported as bbu-<update_handler_name> fastboot
partition).
The barebox Fastboot gadget supports the following non standard extensions:
fastboot getvar allShows a list of all variables, including exported partitionsfastboot oem getenv <varname>Shows a barebox environment variablefastboot oem setenv <varname>=<value>Sets a barebox environment variablefastboot oem exec <cmd>executes a shell command. Note the output can’t be seen on the host, but the fastboot command returns successfully when the barebox command was successful and it fails when the barebox command fails.
Example booting kernel/devicetree/initrd with fastboot
In Barebox start the fastboot gadget:
usbgadget -A /kernel(kernel)c,/initrd(initrd)c,/devicetree(devicetree)c
On the host you can use this script to start a kernel with kernel, devicetree and initrd:
#!/usr/bin/env bash
set -e
set -v
if [ "$#" -lt 3 ]
then
echo "USAGE: $0 <KERNEL> <DT> <INITRD> [<ARGS>]"
exit 0
fi
kernel=$1
dt=$2
initrd=$3
shift 3
fastboot -i 7531 flash kernel $kernel
fastboot -i 7531 flash devicetree $dt
fastboot -i 7531 flash initrd $initrd
fastboot -i 7531 oem exec 'global linux.bootargs.fa'$ct'=rdinit=/sbin/init'
if [ $# -gt 0 ]
then
ct=1
for i in $*
do
fastboot -i 7531 oem exec 'global linux.bootargs.fa'$ct'='"\"$i\""
ct=$(($ct + 1))
done
fi
timeout -k 5 3 fastboot -i 7531 oem exec -- bootm -o /devicetree -r /initrd /kernel
1.17.2.5. USB Mass storage gadget¶
Example exporting barebox block devices to a USB host:
usbgadget -S /dev/mmc0(emmc),/dev/mmc1(sd)
1.17.2.6. CDC EEM (Ethernet Emulation Model)¶
CDC EEM is a lightweight USB Ethernet protocol defined in the USB CDC EEM specification (CDC_EEM10.pdf). Unlike CDC ECM or RNDIS, EEM requires only bulk endpoints and no separate control interface, making it simpler to implement and more efficient for embedded use cases.
barebox supports EEM both as a USB gadget (device mode) and as a USB host driver.
1.17.2.6.1. EEM gadget¶
The EEM gadget creates a network interface on the barebox device, allowing Ethernet communication with a connected USB host. This is useful for network boot, firmware updates, or any scenario requiring network access during early boot.
To start an EEM gadget:
usbgadget -e
This creates the network interface usb0 on the barebox side, named like
the interface of the Linux USB Ethernet gadget. The gadget can be combined
with other USB functions, for example with a serial console:
usbgadget -e -a
Once the gadget is running and a host is connected, configure the IP addresses on both sides and use the link like a regular network interface:
usb0.mode=static
usb0.ipaddr=172.20.0.2
usb0.netmask=255.255.255.0
usb0.serverip=172.20.0.1
ifup usb0
ping 172.20.0.1
On a Linux host, the device will appear as a usb0 (or similar) network
interface after loading the cdc_eem kernel module.
Its linux.devname device parameter defaults to usb0 as well, so the
ip= kernel command line constructed by barebox names the right device
when the booted kernel brings up an EEM gadget itself (see NFS root over EEM
below).
1.17.2.6.2. EEM host¶
barebox can also drive EEM devices from a USB host port, e.g. another board running barebox or Linux with an EEM gadget.
To use, enable CONFIG_USB_NET_CDC_EEM and probe the USB bus:
usb
Any connected EEM device will appear as a network interface (e.g. eth1)
and can be used like any other network device. This can be combined with the
ifup - bring a network interface up command by adding a usb call to
/env/network/eth0-discover (see USB Networking above).
1.17.2.6.3. Linux host setup for EEM¶
When barebox runs an EEM gadget, the connected Linux host needs to be
configured to communicate over the USB link. The Linux kernel’s cdc_eem
driver (CONFIG_USB_NET_CDC_EEM) is included in most distribution kernels
and loads automatically when the gadget is connected. See
Documentation/usb/gadget-testing.rst in the Linux kernel source for
general USB gadget Ethernet testing information.
The interface is typically named usb0. On systems with predictable
network interface names (systemd v197+), it may be renamed. Check ip link
or dmesg output to find the actual name. EEM carries no MAC address, so
the host picks a random one each time the gadget connects.
Quick static setup for development use:
# On the Linux host:
sudo ip link set usb0 up
sudo ip addr add 172.20.0.1/24 dev usb0
# In barebox:
usbgadget -e
usb0.mode=static
usb0.ipaddr=172.20.0.2
usb0.netmask=255.255.255.0
ifup usb0
ping 172.20.0.1
With NetworkManager, a connection profile matched by driver instead of interface name or MAC address configures the host side whenever the gadget appears:
nmcli connection add type ethernet con-name barebox-eem match.driver cdc_eem \
ipv4.method manual ipv4.addresses 172.20.0.1/24
DHCP with dnsmasq – useful when Linux will be booted with ip=dhcp
on the kernel command line (see also
Configuration of dnsmasq for DHCP and TFTP):
sudo ip addr add 172.20.0.1/24 dev usb0
sudo ip link set usb0 up
dnsmasq --port=0 --interface=usb0 --bind-interfaces --no-daemon \
--log-queries --dhcp-range=172.20.0.10,172.20.0.50 \
--dhcp-leasefile=/tmp/dnsmasq-usb0.leases --pid-file=/tmp/dnsmasq-usb0.pid
Internet sharing via NAT – allows barebox or a Linux kernel booted via barebox to reach the internet through the host:
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
Replace eth0 with the host’s internet-facing interface.
NFS root over EEM – the embedded device boots Linux and mounts its root filesystem from the host. On the host, export the root filesystem:
# /etc/exports
/srv/nfs/rootfs 172.20.0.0/24(rw,no_root_squash,no_subtree_check)
sudo exportfs -ra
sudo systemctl restart nfs-server
The USB link goes down while barebox hands over to Linux, so the kernel has
to bring up an EEM gadget of its own before it mounts the root filesystem,
e.g. with the legacy Ethernet gadget built in (CONFIG_USB_ETH=y and
CONFIG_USB_ETH_EEM=y), which Linux names usb0.
ifup constructs a matching kernel ip= parameter in
usb0.linux.bootargs, including usb0 as the device name (see
Documentation/admin-guide/nfs/nfsroot.rst in the Linux kernel source for
the full ip= syntax). A typical barebox boot setup:
usbgadget -e
usb0.mode=static
usb0.ipaddr=172.20.0.2
usb0.netmask=255.255.255.0
usb0.serverip=172.20.0.1
ifup usb0
global linux.bootargs.nfsroot="root=/dev/nfs nfsroot=172.20.0.1:/srv/nfs/rootfs,v3,tcp"
global linux.bootargs.ip="${usb0.linux.bootargs}"
The kernel command line then carries the configured addresses and usb0
as the network device:
ip=172.20.0.2:172.20.0.1:0.0.0.0:255.255.255.0::usb0:
1.17.2.7. USB Composite Multifunction Gadget¶
With the Composite Multifunction Gadget it is possible to create a USB device with multiple functions. A useful combination is creating a Fastboot gadget and a USB serial console. This combination can be created with:
usbgadget -A /dev/mmc2.0(root),/dev/mmc2.1(data) -a
The -A option will create a Fastboot function providing /dev/mmc2.0 as root
partition and /dev/mmc2.1 as data partition. The -a option will create a
USB CDC ACM compliant serial device.
An EEM network interface can also be part of a composite gadget, for example combining Fastboot with EEM for network-based workflows:
usbgadget -A /dev/mmc2.0(root) -e
Unlike the dfu - device firmware update command the usbgadget command returns immediately
after creating the gadget. The gadget can be removed with usbgadget -d.
1.17.3. USB OTG support¶
barebox does not have true USB OTG support. However, barebox supports some USB cores in
both host and device mode. If these are specified for otg in the device tree
(dr_mode = “otg”;) barebox registers an OTG device which can be used to decide which
mode shall be used. The device has a mode parameter which by default has the
value otg. Setting this to host or peripheral puts the device in the corresponding
mode. Once a specific mode has been selected it can’t be changed later anymore.
barebox:/ devinfo otg0
Parameters:
mode: otg ("otg", "host", "peripheral")
barebox:/ otg0.mode=host
musb-hdrc: ConfigData=0xde (UTMI-8, dyn FIFOs, bulk combine, bulk split, HB-ISO Rx, HB-ISO Tx, SoftConn)
musb-hdrc: MHDRC RTL version 2.0
musb-hdrc: setup fifo_mode 4
musb-hdrc: 28/31 max ep, 16384/16384 memory
barebox:/
1.17.4. USB Type-C support¶
barebox can usually stay oblivious to the type of connector used. Sometimes though, board code and user scripts may want to base their decisions on how a USB-C connector is connected. Type C drivers can thus register with the Type C driver core to export a number of device parameters:
$typec0.usb_role= {none,device,host}$typec0.pwr_role= {sink,source}$typec0.accessory= {none,audio,debug}
Currently, only the TUSB320 is supported, but it’s straight-forward to port more drivers from Linux.
1.17.5. USB Gadget autostart Support¶
Barebox can be configured to start usbgadget automatically by using global variables, instead of creating a boot script. This can be useful if autostart policy should be chosen at boot time from other driver or script. To get usbgadget autostart support barebox has to be compiled with CONFIG_USB_GADGET_AUTOSTART enabled.
1.17.5.1. USB Gadget autostart Options¶
- global.usbgadget.autostart
Boolean flag. If set to 1, usbgadget will be started automatically on boot and enable USB OTG mode. (Default 0).
- global.usbgadget.acm
Boolean flag. If set to 1, CDC ACM function will be created. See usbgadget - Create USB Gadget multifunction device -a. (Default 0).
- global.usbgadget.eem
Boolean flag. If set to 1, CDC EEM function will be created. See usbgadget - Create USB Gadget multifunction device -e and CDC EEM (Ethernet Emulation Model). (Default 0).
- global.system.partitions
Common function description for all of DFU, fastboot and USB mass storage gadgets. See Partition description above for the syntax. Both Fastboot and DFU partitions also have dedicated override variables for backwards-compatibility:
- global.usbgadget.dfu_function
Function description for DFU. See usbgadget - Create USB Gadget multifunction device -D [desc], and Partition description above for the syntax.
- global.fastboot.partitions
Function description for fastboot. See usbgadget - Create USB Gadget multifunction device -A [desc], and Partition description above for the syntax.
- global.fastboot.bbu
Export barebox update handlers. See usbgadget - Create USB Gadget multifunction device -b. (Default 0).