Using USB With QEMU

Xilinx QEMU supports USB XHCI in the host-only mode. For Versal Adaptive SoC, we have a USB2.0 controller. For Zynq UltraScale+ MPSoC, we have two USB3.0 controllers. You can plug virtual USB devices or real host USB devices.
We will talk about how to use the USB host mode in the Versal Adaptive SoC and ZCU102 device. 



USB on Versal

# create a dummy usb image of size 16 MB on the host machine using fallocate or qemu-img(availble in the qemu build directory if QEMU was built from source):
fallocate -l 16M versal_usb.img
# qemu-img create versal_usb.img 16M
mkfs -t ext4 versal_usb.img
 
# Boot QEMU using petalinux commands:
petalinux-boot --qemu --prebuilt 3
 
# Login to prompt using:
username: root
password: root
 
# Enter QEMU monitor by using:
Ctrl+A + c.
# In monitor add following command to attach the driver:
drive_add 0 if=none,file=versal_usb.img,id=stick,format=raw
 
# You should 'OK' being printed on screen for sucessfull drive add.
 
# To attach the USB device:
device_add usb-storage,bus=usb2@USB2_0_XHCI.0,port=1,id=usb_dev1,drive=stick

# Optional: Enter below command to see usb attached with QEMU or not:
info usb
# It should print this: Device 0.0, Port 1, Speed 5000 Mb/s, Product QEMU USB MSD, ID: usb_dev1

# to find the mount path for usb, enter command:
df -h

# Above command should print something like this:
#/dev/sda                 14.5M    140.0K     13.2M   1% /run/media/sda

# Add a text file to mounted path using:
echo "Hello from QEMU" >/run/media/sda/test.txt

# Sync using:
sync
 
# Exit the QEMU using:
Ctrl + A + x
 
# On your host machine, create a directory:
mkdir test_mount
 
# On your host machine, mount the usb.img to this path using:
sudo mount versal_usb.img test_mount/
 
# On your host machine, check the text we added in this image:
cat test_mount/test.txt

USB on ZynqMP

# create a dummy usb image of size 16 M on the host machine using fallocate or qemu-img(availble in the qemu build directory if QEMU was built from source):
fallocate -l 16M zynqmp_usb.img
# qemu-img create zynqmp_usb.img 16M
mkfs -t ext4 zynqmp_usb.img
 
# Boot QEMU using petalinux or any other way. Example for Petalinux:
petalinux-boot --qemu --prebuilt 3
 
# Login to prompt using:
username: root
password: root
 
# Enter QEMU monitor by using:
Ctrl+A + c.
# In monitor add following command to attach the driver. Please add correct path for zynqmp_usb.img:
drive_add 0 if=none,file=zynqmp_usb.img,id=stick
 
# You should 'OK' being printed on screen for sucessfull drive add.
 
# To attach the USB device:
device_add usb-storage,bus=usb3@0xFE200000.0,port=1,id=usb_dev1,drive=stick

# Optional: Enter below command to see usb attached with QEMU or not:
info usb
# Above should print: Device 0.1, Port 1, Speed 5000 Mb/s, Product QEMU USB MSD, ID: usb_dev1
 
#Exit monitor mode by using:
Ctrl + A + c
 
# to find the mount path for usb, enter command:
df -h

# Above command should print something like this:
# /dev/sda                 14.5M    140.0K     13.2M   1% /run/media/sda

# Add a text file to mounted path using:
echo "Hello from QEMU" >/run/media/sda/test.txt

# Sync using:
sync
 
# Exit the QEMU using:
Ctrl + A + x
 
# On your host machine, create a directory:
mkdir test_mount
 
# On your host machine, mount the usb.img to this path using:
sudo mount zynqmp_usb.img test_mount/
 
# On your host machine, check the text we added in this image:
cat test_mount/test.txt
 
# It should print the following:
Hello from QEMU

For more use cases for USB on QEMU, please check the USB emulation link.


© Copyright 2019 - 2022 Xilinx Inc. Privacy Policy