linux usb驱动——Gadget编译生产

参考 http://linux-sunxi.org/USB_Gadget/Mass_storage

make kernel_menuconfig

Device Drivers ---> USB support ---> USB Gadget Support ---> <M> Mass Storage Gadget

在src/linux-3.14/drivers/usb/gadget下生成g_mass_storage.ko

加载:

Creating a sparse file

Use "dd" command's "count" and "seek" options to create a 1GB file that will not use storage until it is used:

# dd if=/dev/zero of=/mass_storage bs=1M seek=1024 count=0
# ls -l /mass_storage 
-rw-r--r-- 1 root root 1073741824 Feb 15 16:40 /mass_storage
# du -hs /mass_storage 
0       /mass_storage

Partitioning and formatting sparse file

To be recognized by most Operating Systems, create a single FAT type partition and format it as DOS filesystem using Linux loop device driver.

Create a single partition

# cat <<EOT | sfdisk --in-order -L -uM /mass_storage 
,,c
EOT

Find partition offset

# fdisk -lu /mass_storage 

Disk /mass_storage: 1073 MB, 1073741824 bytes
139 heads, 8 sectors/track, 1885 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

	Device Boot      Start         End      Blocks   Id  System
/mass_storage1               1     2097151     1048575+   c  W95 FAT32 (LBA)

First partition starts at first sector: offset = 1 * 512 = 512 bytes

Set up loop device

# losetup -o512 /dev/loop0 /mass_storage
# losetup -a
/dev/loop0: [b302]:14867 (/mass_storage), offset 512

Format device

# apt-get install dosfstools
# mkdosfs /dev/loop0

 

Test device

# mount -t vfat /dev/loop0 /mnt/
# df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0     1022M  4.0K 1022M   1% /mnt
# mount | grep mnt
/dev/loop0 on /mnt type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=remount-ro)

Release device

# umount  /mnt/
# losetup -d /dev/loop0 
# losetup -a

As we can see below, 1GB sparse file is only using 2.1MB storage (size of filesystem metadata):

# du -sh /mass_storage 
2.1M    /mass_storage
# ls -lh /mass_storage 
-rw-r--r-- 1 root root 1.0G Feb 15 16:54 /mass_storage

 

posted @ 2018-03-02 17:59  平分秋色  阅读(669)  评论(0编辑  收藏  举报