Reference Http://linux-sunxi.org/USB_Gadget/Mass_storage
Make Kernel_menuconfig
Device Drivers---> USB support---> USB Gadget support---> <M> Mass Storage Gadget
Generate G_mass_storage.ko under Src/linux-3.14/drivers/usb/gadget
Load:
Creating a sparse file
Use "dd" command ' s "Count" and "seek" options to create a 1GB file that won't use storage until it's 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 1 6:40/mass_storage# du-hs/mass_storage 0 /mass_storage
partitioning and formatting sparse file
To is 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,, Ceot
Find Partition Offset
# fdisk-lu/mass_storage disk/mass_storage:1073 MB, 1073741824 bytes139 heads, 8 sectors/track, 1885 cylinders, Total 2 097152 sectorsunits = sectors of 1 * = bytessector size (logical/physical): bytes/512 bytesi/o Size (minimum /optimal): bytes/512 bytesdisk identifier:0x00000000device Boot Start End Blocks Id system/ Mass_storage1 1 2097151 1048575+ C W95 FAT32 (LBA)
First partition starts at first sector:offset = 1 * = + 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/mntfilesystem Size used Avail use% mounted on/dev/loop0 1022M 4.0 K 1022M 1%/mnt# Mount | grep mnt/dev/loop0 on/mnt type VFAT (Rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocha Rset=ascii,shortname=mixed,errors=remount-ro)
Release Device
# umount /mnt/# losetup-d/dev/loop0 # losetup-a
As we can see below, 1GB sparse file was 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 16:54/mass_storage
Linux USB driver--gadget compiled production