个人笔记:编译aarch64平台的gt,自动创建usb gadget设备

aarch64-linux-gcc -print-file-name=libc.so

/opt/FriendlyARM/toolchain/6.4-aarch64/aarch64-cortexa53-linux-gnu/sysroot/为交叉编译工具链路径

sudo su
vim /root/.bashrc
添加到最后一行
export PATH=/opt/FriendlyARM/toolchain/6.4-aarch64/bin/:$PATH
source /root/.bashrc
git clone https://github.com/hyperrealm/libconfig.git
cd libconfig/
mkdir build
cd build/
export CC=aarch64-linux-gcc
export CXX=aarch64-linux-g++
sudo apt install cmake
cmake ..
make
sudo make DESTDIR=/home/gt/usr/ install
git clone https://github.com/linux-usb-gadgets/libusbgx.git
cd libusbgx/
sudo apt install autoconf libtool libconfig-dev
autoreconf -i
./configure --host=aarch64-linux \
--prefix=/home/gt/usr/ \
--exec-prefix=/home/gt/usr/ \
LIBCONFIG_CFLAGS="-I/home/gt/usr/usr/local/include" \
LIBCONFIG_LIBS="-L/home/gt/usr/usr/local/lib -lconfig"
make
sudo su
make install
git clone https://github.com/linux-usb-gadgets/gt.git
cd gt/source/
vim CMakeLists.txt
注释掉
#pkg_check_modules(pkgs REQUIRED ${PKG_MODULES})
#ADD_SUBDIRECTORY(manpages)
把
INSTALL(TARGETS gt RUNTIME)
修改为
INSTALL(TARGETS gt RUNTIME DESTINATION /home/gt/_install)
mkdir build
cd build/
export CC=aarch64-linux-gcc
export CXX=aarch64-linux-g++
cmake -DCMAKE_RUNTIME_PREFIX=/ \
-DEXTRA_CFLAGS="-I/home/gt/usr/include -I/home/gt/usr/usr/local/include -L/home/gt/usr/lib -lusbgx -L/home/gt/usr/usr/local/lib -lconfig" ..
make
sudo make install
sudo cp /usr/local/etc/gt/gt.conf rootfs/usr/local/etc/gt/gt.conf

把gt需要的libconfig libusbgx的so文件复制到根文件系统的lib64目录下

ums.scheme

attrs :
{
    bcdUSB = 0x200;
    idVendor = 0x1d6b;
    idProduct = 0x0104;
    bcdDevice = 0x0100;
};
strings = (
        {
                lang = 0x409;
                serialnumber = "deadbeef001";
                manufacturer = "Example Corp";
                product = "USB Mass Storage";
        }
);
functions :
{
    mass_storage_usb0 :
    {
        instance = "usb0";
        type = "mass_storage";
		attrs :
		{
			stall = true;
			luns = (
			{
				file = "/root/usb.img";
				removable = false;
				ro = false;
			}
			);
		};
    };
    hid_usb0 :
    {
        instance = "usb0";
        type = "hid";
		attrs :
		{
			protocol = 1;
			subclass = 1;
			report_length = 8;
			report_desc = (0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xc0);
		};
    };
};
configs = (
    {
        id = 1;
        name = "c";
        attrs :
        {
            bMaxPower = 120;
        };
        functions = (
            {
                name = "mass_storage.usb0";
                function = "mass_storage_usb0";
            },
            {
                name = "hid.usb0";
                function = "hid_usb0";
            } );
    } );
dd if=/dev/zero of=/path/to/usb.img bs=1M count=64
mkfs.vfat /path/to/usb.img
sudo cp gt/examples/systemd/99-udc.rules ./rootfs/usr/lib/udev/rules.d/99-udc.rules
sudo cp gt/examples/systemd/gt.target ./rootfs/usr/lib/systemd/system/gt.target
sudo cp gt/examples/systemd/gt@.service ./rootfs/usr/lib/systemd/system/gt@.service

把gt@.service文件中的ExecStart=/bin/gt load /root/%i.scheme %i中的scheme文件路径改为你自己存放路径

#  SPDX-License-Identifier: MIT or LGPL-2.1+
#
# Copyright 2019 Collabora Ltd
#
[Unit]
Description=Load USB gadget scheme
Requires=sys-kernel-config.mount
After=sys-kernel-config.mount

[Service]
ExecStart=/bin/gt load /root/%i.scheme %i
RemainAfterExit=yes
ExecStop=/bin/gt rm -rf %i
Type=simple

[Install]
WantedBy=gt.target
systemctl enable gt@ums.service

按下a健

echo -ne '\x00\x00\x04\x00\x00\x00\x00\x00' > /dev/hidg0
echo -ne '\x00\x00\x00\x00\x00\x00\x00\x00' > /dev/hidg0 #停止

手动创建

gt load /root/ums.scheme ums

手动创建脚本

cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1

# 设置 USB 设备描述符
echo 0x1d6b > idVendor  # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # 1.0
echo 0x0200 > bcdUSB    # USB 2.0

# 创建字符串描述符
mkdir -p strings/0x409
echo "deadbeef001" > strings/0x409/serialnumber
echo "Example Corp" > strings/0x409/manufacturer
echo "USB Mass Storage" > strings/0x409/product

# 创建配置
mkdir -p configs/c.1/strings/0x409
echo "Config 1" > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPower

# 创建 Mass Storage 功能
mkdir -p functions/mass_storage.0
echo 1 > functions/mass_storage.0/stall
echo /path/to/usb.img > functions/mass_storage.0/lun.0/file
echo 0 > functions/mass_storage.0/lun.0/removable
echo 0 > functions/mass_storage.0/lun.0/ro

# 将 Mass Storage 绑定到配置
ln -s functions/mass_storage.0 configs/c.1/

# 绑定 UDC 驱动
ls /sys/class/udc > UDC
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值