USB - 通过configfs配置Linux USB Gadget
Linux USB gadget configured through configfs
Overview
USB Linux 小工具是一种具有 UDC(USB 设备控制器)的设备,可连接到 USB 主机,以扩展其附加功能,如串行端口或大容量存储功能。
A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can be connected to a USB Host to extend it with additional functions like a serial port or a mass storage capability.
主机将gadget视为一组配置,每个配置都包含若干接口,从gadget的角度看,这些接口被称为功能,每个功能代表一个串行连接或 SCSI 磁盘等。
A gadget is seen by its host as a set of configurations, each of which contains a number of interfaces which, from the gadget's perspective, are known as functions, each function representing e.g. a serial connection or a SCSI disk.
Linux 为gadget提供了许多功能。
Linux provides a number of functions for gadgets to use.
创建gadget意味着要决定有哪些配置,以及每个配置将提供哪些功能。
Creating a gadget means deciding what configurations there will be and which functions each configuration will provide.
Configfs(请参阅 Configfs - 用户空间驱动的内核对象配置)可以很好地将上述决定告知内核。本文档将介绍如何做到这一点。 (Configfs - Userspace-driven Kernel Object Configuration — The Linux Kernel documentation)
Configfs (please see Configfs - Userspace-driven Kernel Object Configuration) lends itself nicely for the purpose of telling the kernel about the above mentioned decision. This document is about how to do it.
Configfs(请参阅 Configfs - 用户空间驱动的内核对象配置)可以很好地将上述决定告知内核。本文档将介绍如何做到这一点。
本文还将介绍如何将 configfs 集成到gadget中。
It also describes how configfs integration into gadget is designed.
Requirements
为使其正常工作,configfs 必须可用,因此 .config 中的 CONFIGFS_FS 必须为 "y "或 "m"。目前,USB_LIBCOMPOSITE 选择 CONFIGFS_FS。
In order for this to work configfs must be available, so CONFIGFS_FS must be 'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.
Usage
描述通过 configfs 提供第一个功能的原始帖子可在此处查看:
The original post describing the first function made available through configfs can be seen here:
[PATCH 30/30] usb/gadget: the start of the configfs interface — Linux USB
$ modprobe libcomposite
$ mount none $CONFIGFS_HOME -t configfs
其中 CONFIGFS_HOME变量 是 configfs 的挂载点 (一个文件夹路径)。
where CONFIGFS_HOME is the mount point for configfs.
1. Creating the gadgets [创建Gadget]
必须为每个要创建的小工具创建相应的目录:
For each gadget to be created its corresponding directory must be created:
$ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>
e.g.:
$ mkdir $CONFIGFS_HOME/usb_gadget/g1
...
...
...
$ cd $CONFIGFS_HOME/usb_gadget/g1
每个小工具都需要指定供应商 ID <VID> 和产品 ID <PID>:
Each gadget needs to have its vendor id <VID> and product id <PID> specified:
$ echo <VID> > idVendor
$ echo <PID> > idProduct
当执行上面第一行语句后,在此gadget目录中,就会自动创建出一系列文件及文件夹。
Gadget还需要序列号、制造商和产品字符串。为了存放这些字符串,必须为每种语言创建一个字符串子目录,如:
A gadget also needs its serial number, manufacturer and product strings. In order to have a place to store them, a strings subdirectory must be created for each language, e.g.:
$ mkdir strings/0x409 # 0x409表示英文
然后就可以指定字符串了:
Then the strings can be specified:
$ echo <serial number> > strings/0x409/serialnumber
$ echo <manufacturer> > strings/0x409/manufacturer
$ echo <product> > strings/0x409/product
此外,还可以在语言目录中创建自定义字符串描述符目录,并将字符串文本写入字符串目录中的 "s "属性:
Further custom string descriptors can be created as directories within the language's directory, with the string text being written to the "s" attribute within the string's directory:
$ mkdir strings/0x409/xu.0
$ echo <string text> > strings/0x409/xu.0/s
在函数驱动程序支持的情况下,函数可以允许这些自定义字符串描述符的符号链接,以便将这些字符串与类描述符关联起来。
Where function drivers support it, functions may allow symlinks to these custom string descriptors to associate those strings with class descriptors.
2. Creating the configurations [创建配置]
每个gadget都将包含多个配置,必须创建相应的目录:
Each gadget will consist of a number of configurations, their corresponding directories must be created:
$ mkdir configs/<name>.<number>
其中 <name> 可以是任何在文件系统中合法的字符串,<number> 是配置的编号,例如
where <name> can be any string which is legal in a filesystem and the <number> is the configuration's number, e.g.:
$ mkdir configs/c.1
...
...
...
每个配置还需要其字符串,因此必须为每种语言创建一个子目录,例如
Each configuration also needs its strings, so a subdirectory must be created for each language, e.g.:
$ mkdir configs/c.1/strings/0x409
然后就可以指定配置字符串了:
Then the configuration string can be specified:
$ echo <configuration> > configs/c.1/strings/0x409/configuration
也可为配置设置某些属性,例如:
Some attributes can also be set for a configuration, e.g.:
$ echo 120 > configs/c.1/MaxPower
3. Creating the functions [创建功能]
gadget将提供一些功能,必须为每个功能创建相应的目录:
The gadget will provide some functions, for each function its corresponding directory must be created:
$ mkdir functions/<name>.<instance name>
其中 <name> 是可使用函数名中的一个,而实例名则是文件系统中允许的任意字符串,例如
where <name> corresponds to one of allowed function names and instance name is an arbitrary string allowed in a filesystem, e.g.:
$ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()
...
...
...
每个函数都提供其特定的属性集,具有只读或读写访问权限。在适用的情况下,需要对它们进行适当的写入。更多信息请参阅 Documentation/ABI/testing/configfs-usb-gadget。
Each function provides its specific set of attributes, with either read-only or read-write access. Where applicable they need to be written to as appropriate. Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.
4. Associating the functions with their configurations [将功能和配置进行关联]
此时会创建许多gadget,每个gadget都有指定的配置和可用的功能。剩下的工作就是指定哪个功能在哪个配置中可用(同一个功能可以在多个配置中使用)。这可以通过创建符号链接来实现:
At this moment a number of gadgets is created, each of which has a number of configurations specified and a number of functions available. What remains is specifying which function is available in which configuration (the same function can be used in multiple configurations). This is achieved with creating symbolic links:
$ ln -s functions/<name>.<instance name> configs/<name>.<number>
e.g.:
$ ln -s functions/ncm.usb0 configs/c.1
...
...
...
5. Enabling the gadget [使能Gadget]
上述所有步骤都是为了将配置和功能组成一个gadget。
目录结构示例如下:
All the above steps serve the purpose of composing the gadget of configurations and functions.
An example directory structure might look like this:
.
./strings
./strings/0x409
./strings/0x409/serialnumber
./strings/0x409/product
./strings/0x409/manufacturer
./configs
./configs/c.1
./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0
./configs/c.1/strings
./configs/c.1/strings/0x409
./configs/c.1/strings/0x409/configuration
./configs/c.1/bmAttributes
./configs/c.1/MaxPower
./functions
./functions/ncm.usb0
./functions/ncm.usb0/ifname
./functions/ncm.usb0/qmult
./functions/ncm.usb0/host_addr
./functions/ncm.usb0/dev_addr
./UDC
./bcdUSB
./bcdDevice
./idProduct
./idVendor
./bMaxPacketSize0
./bDeviceProtocol
./bDeviceSubClass
./bDeviceClass
需要最后使能这个Gadget,以便 USB 主机能对其进行枚举。
为了启用Gadget,必须将其绑定到 UDC(USB 设备控制器)上:
Such a gadget must be finally enabled so that the USB host can enumerate it.
In order to enable the gadget it must be bound to a UDC (USB Device Controller):
$ echo <udc name> > UDC
其中 <udc 名称> 是在 /sys/class/udc/* 中找到的名称之一,例如
where <udc name> is one of those found in /sys/class/udc/* e.g.:
$ echo s3c-hsotg > UDC
6. Disabling the gadge [停止Gadget]
$ echo "" > UDC
7. Cleaning up [移除]
从配置中删除功能:
Remove functions from configurations:
$ rm configs/<config name>.<number>/<function>
其中 <config name>.<number> 指定配置,<function> 是指向从配置中移除的函数的符号链接,例如
where <config name>.<number> specify the configuration and <function> is a symlink to a function being removed from the configuration, e.g.:
$ rm configs/c.1/ncm.usb0
...
...
...
删除配置中的字符串目录:
Remove strings directories in configurations:
$ rmdir configs/<config name>.<number>/strings/<lang>
e.g.:
$ rmdir configs/c.1/strings/0x409
...
...
...
and remove the configurations:
$ rmdir configs/<config name>.<number>
e.g.:
rmdir configs/c.1
...
...
...
移除函数(但函数模块不会被卸载):
Remove functions (function modules are not unloaded, though):
$ rmdir functions/<name>.<instance name>
e.g.:
$ rmdir functions/ncm.usb0
...
...
...
Remove strings directories in the gadget:
$ rmdir strings/<lang>
e.g.:
$ rmdir strings/0x409
and finally remove the gadget:
$ cd ..
$ rmdir <gadget name>
e.g.:
$ rmdir g1
参考:
Linux USB gadget configured through configfs — The Linux Kernel documentation
相关文章:
USB - 通过configfs配置Linux USB Gadget
Linux USB gadget configured through configfs Overview USB Linux 小工具是一种具有 UDC(USB 设备控制器)的设备,可连接到 USB 主机,以扩展其附加功能,如串行端口或大容量存储功能。 A USB Linux Gadget is a device…...
迷宫与陷阱(蓝桥杯)
文章目录 迷宫与陷阱问题描述bfs解题思路代码 迷宫与陷阱 问题描述 小明在玩一款迷宫游戏,在游戏中他要控制自己的角色离开一间由 N x N 个格子组成的2D迷宫。 小明的起始位置在左上角,他需要到达右下角的格子才能离开迷宫,每一步…...
Temple of Doom靶场nodejs获取shellss-manager漏洞tcpdump提权
下载链接: Temple of Doom: 1 ~ VulnHub 下载完成后直接在vxbox中导入即可,网络链接模式根据自身情况而定(我采用的桥接模式) 正文: 先用nmap进行扫描靶机ip nmap -sn 192.168.1.1/24 对192.168.1.5进行端口探测&a…...
day03_mysql_课后练习 - 参考答案
文章目录 day03_mysql_课后练习mysql练习题第1题第2题第3题第4题第5题 day03_mysql_课后练习 mysql练习题 第1题 案例: 1、创建一个数据库:day03_test01_school 2、创建如下表格 表1 Department表的定义 字段名字段描述数据类型主键外键非空唯一D…...
creator-webview与Android交互
title: creator-webview与Android交互 categories: Cocos2dx tags: [cocos2dx, creator, webview, 交互] date: 2024-03-23 13:17:20 comments: false mathjax: true toc: true creator-webview与Android交互 前篇 Android:你要的WebView与 JS 交互方式 都在这里了…...
22.WEB渗透测试-BurpSuite(一)
免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动! 内容参考于: 易锦网校会员专享课 上一个内容:21.WEB渗透测试-HTTP协议(下)-CSDN博客 工具的使用需要先搭建靶场…...
前端性能优化:防抖与节流
一、防抖和节流主要是干什么的 防抖和节流主要用于控制函数执行的频率,通过限制函数的触发次数,避免函数被过度调用而引发的性能问题或产生不必要的副作用。 二、防抖 防抖是什么: 1、对于在事件被触发 n 秒后再执行的回调 --> 延迟执行 2、如果…...
Copilot 编程助手的介绍及使用
介绍 Copilot 是2021年由 GitHub 与 OpenAI 合作研发的一款编程助手,同时也是全球首款使用OpenAI Codex模型(GPT-3后代)打造的大规模生成式AI开发工具。 Copilot 底层模型目前经过了数十亿行公开代码的训练,与大多数代码辅助工具…...
数据库专题(oracle基础和进阶)
前言 本专题主要记录自己最近学的数据库,有兴趣一起补习的可以一起看看,有补充和不足之处请多多指出。希望专题可以给自己还有读者带去一点点提高。 数据库基本概念 本模块有参考:数据库基本概念-CSDN博客 数据库管理系统是一个由互相关联的…...
web蓝桥杯2022省赛真题:水果拼盘
代码及注释: /* TODO:待补充代码 */ #pond {display: flex; //flex布局flex-direction: column; //主轴方向从上到下flex-wrap: wrap; //子元素换行 } 知识点: flex弹性布局 父元素:diasplay: flex; flex-d…...
Web核心
目录 Web核心HTTP概念:协议特点:请求数据格式响应数据格式 Tomcat简介基本使用配置部署项目IDEA中创建 Maven Web 项目 IDEA使用Tomcat Servlet简介快速入门执行流程生命周期体系结构Servlet urlPattern配置一个Servlet,可以配置多个 urlPatt…...
iOS应用审核问题解决方案及优化方法 ✨
摘要 本文将针对iOS应用提交审核时可能遇到的问题,如“你必须在Xcode中添加com.apple.developer.game-center密钥”,以及突然间提交送审报错情况进行探讨。通过大量查询资料和尝试,结合案例分析,提供了解决方案和优化方法…...
java post、get请求第三方https接口
java post、get请求第三方https接口 前段时间做项目新加功能由于要对接其它系统,请求系统接口传输数据。写完后发现我写的这个方法和网上现有的例子有点不太一样,可能是因为我做的项目是政务网的原因,但我想正常的即便是互联网的系统请求方式…...
【C语言】鸡兔同笼,鸡和兔共 100 只,共 284 只脚,求鸡和兔的个数。
鸡兔同笼,鸡和兔共 100 只,共 284 只脚,求鸡和兔的个数。 int main() {for (int i 0; ; i){if (2 * i 4 * (100 - i) 284){printf("鸡的数量:%d,兔子的数量:%d", i, 100 - i);break;} } }这里直接算出题…...
沪漂8年回郑州三年如何走上创业之路
大家好,我是大牛,目前人在郑州。 现在标签是: 创业者🚗🐸 (注册有自己的公司,主要是为了自己的产品和接外包项目)独立开发者👨🏻💻 (有自己的小项目)数字游民&…...
MySQL数据库—事务与存储类型
一、事务: 1.事务的概念: 事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这组数据库命令要么都执行,要么都不执行。事务是一个不…...
蓝桥杯刷题8
1. 世纪末的星期 import java.util.Calendar; public class Main {public static void main(String[] args) {Calendar calendar Calendar.getInstance();for(int year 1999;year<100000;year100){calendar.set(Calendar.YEAR,year);calendar.set(Calendar.MONTH,11);cale…...
Java中的String字符串练习
目录 Java中的String字符串练习 01-用户登录 02-遍历字符串并统计字符个数 03-字符串拼接 04-字符串反转 注意点 05-金额转化(简单) 代码解释: 06-手机号屏蔽 07-身份证号码查看 易错点: 08-敏感词替换 01-用户登录 package com.xiaonan.exercise06;import java.u…...
基于JavaWeb SSM mybatis 学生信息管理系统设计和实现以及文档报告
基于JavaWeb SSM mybatis 学生信息管理系统设计和实现以及文档报告 博主介绍:多年java开发经验,专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 …...
二进制源码部署mysql8.0.35
二进制部署mysql8.0.35 创建mysql用户 [rootzyq ~]#: useradd -r -s /sbin/nologin -M mysql [rootzyq ~]#: id mysql uid990(mysql) gid990(mysql) groups990(mysql)上传mysql文件 [rootzyq ~]#: ls anaconda-ks.cfg mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz解压 [roo…...
铭豹扩展坞 USB转网口 突然无法识别解决方法
当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...
挑战杯推荐项目
“人工智能”创意赛 - 智能艺术创作助手:借助大模型技术,开发能根据用户输入的主题、风格等要求,生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用,帮助艺术家和创意爱好者激发创意、提高创作效率。 - 个性化梦境…...
R语言AI模型部署方案:精准离线运行详解
R语言AI模型部署方案:精准离线运行详解 一、项目概述 本文将构建一个完整的R语言AI部署解决方案,实现鸢尾花分类模型的训练、保存、离线部署和预测功能。核心特点: 100%离线运行能力自包含环境依赖生产级错误处理跨平台兼容性模型版本管理# 文件结构说明 Iris_AI_Deployme…...
渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止
<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet: https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...
相机从app启动流程
一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...
【单片机期末】单片机系统设计
主要内容:系统状态机,系统时基,系统需求分析,系统构建,系统状态流图 一、题目要求 二、绘制系统状态流图 题目:根据上述描述绘制系统状态流图,注明状态转移条件及方向。 三、利用定时器产生时…...
【JavaSE】绘图与事件入门学习笔记
-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角,以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置为垂直方向,距离坐标原点y个像素。 坐标体系-像素 …...
力扣-35.搜索插入位置
题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...
如何更改默认 Crontab 编辑器 ?
在 Linux 领域中,crontab 是您可能经常遇到的一个术语。这个实用程序在类 unix 操作系统上可用,用于调度在预定义时间和间隔自动执行的任务。这对管理员和高级用户非常有益,允许他们自动执行各种系统任务。 编辑 Crontab 文件通常使用文本编…...
【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制
使用 NginxLua 实现基于 IP 的访问频率限制 在高并发场景下,限制某个 IP 的访问频率是非常重要的,可以有效防止恶意攻击或错误配置导致的服务宕机。以下是一个详细的实现方案,使用 Nginx 和 Lua 脚本结合 Redis 来实现基于 IP 的访问频率限制…...
