Linux Mass Storage kernel module g_mass_storage use with FAT

0

I am currently configuring a portable (embedded linux based) device which becomes a Mass storage provider once plugged into a computer. The mass storage media must be compatible with Windows and Mac, therefore i choose FAT as the filesystem (file allocation table).

The FAT is produced with mkdosfs

Im now facing the following issues when connected to a computer:

Windows: - want's to always repair the drive, If I allow it to do so it will come back with no errors found....

Mac: - The drive is mounted stays a little while mounted and usable simply to vanish again after a while. This ends up in a regular attempt to remount just to unmount again. Further I see the following line with dmesg

USBMSC Identifier (non-unique) 0x00000000 0xXXXX 0x300 0x100, 2

If I however plug into a Windows machine and format the exposed drive with FAT (FAT 32) both the Win and Mac boxes are happy with the FAT system found on the device. This implies to me that mkdosfs is not capable of producing a valid FAT although knowning that it is a vfat that it produces...

Had anyone similar issues getting this cross OS compatibility to work? I cannot believe that I am the only one facing this issue.

Here my setup routine for the g_mass_storage kernel module:

Kernel:

modprobe libcomposite
cd /sys/kernel/config/usb_gadget

mkdir -p usbdisk

cd usbdisk

echo 0xXXXX > idVendor
echo 0x0300 > idProduct
echo 0x0100 > bcdDevice
echo 0x0200 > bcdUSB


mkdir -p strings/0x409

echo "42" > strings/0x409/serialnumber
echo "Spam and Eggs Inc." > strings/0x409/manufacturer
echo "My awesome Product" > strings/0x409/product


mkdir -p functions/mass_storage.usb0

echo 0 > functions/mass_storage.usb0/stall
echo 0 > functions/mass_storage.usb0/lun.0/cdrom
echo 0 > functions/mass_storage.usb0/lun.0/ro
echo 0 > functions/mass_storage.usb0/lun.0/nofua
echo "/dev/mmcblk0p1" > functions/mass_storage.usb0/lun.0/file


mkdir -p configs/c.1/strings/0x409


echo "My Mass Storage" > configs/c.1/strings/0x409/configuration
echo 500 > configs/c.1/MaxPower
ln -s functions/mass_storage.usb0 configs/c.1

echo ci_hdrc.0 > UDC

Formatting:

I have tried the following scenarios:

with a backing file, fdisk and mkdosfs http://www.linux-usb.org/gadget/file_storage.html

In the latest version I use a block device (SD Card) partition and format it with

mkdosfs -n LABEL -F 32 /dev/mmcblk0p1
linux
macos
linux-kernel
fat
usb-mass-storage

1 Answer

0

I was able to have a working script on Windows 10 and Windows XP (Yes I know I am working on old system !) by using fdisk and gdisk. gdisk is only working for Windows 10 which is using GPT partition table. For Windows XP, you have to use fdisk because this system is not GPT compatible. Only use old MBR stuff.

In this example, I need a 1 Gb partition.

So for Windows 10:

# create 1GB empty file
dd if=/dev/zero of=/data/mass_storage bs=1M seek=1024 count=0
sync

# create partition with fdisk
(
echo o
echo y
echo n
echo 1
echo ""
echo ""
echo 0700
echo p
echo w
echo y
) | gdisk /data/mass_storage
sync

# compute automatically offset for losetup
offset=`gdisk -l /data/mass_storage  | tail -n 1 | awk '{ print $2}'`
offset=$((offset*512))

# format mass storage and mount
losetup -o$offset /dev/loop0 /data/mass_storage
mkfs.msdos -n DRAGON /dev/loop0
modprobe g_mass_storage file=/data/mass_storage

If someone need this also for Windows XP..

# create 1GB empty file
dd if=/dev/zero of=/data/mass_storage bs=1M seek=1024 count=0
sync

# create partition with fdisk
(
echo o
echo n
echo p
echo 1
echo ""
echo ""
echo t
echo b
echo w
) | fdisk -u /data/mass_storage
sync

# format mass storage and mount
offset=`fdisk -lu /data/mass_storage | tail -n 1 | awk '{ print $4 }'`
offset=$((offset*512))
losetup -o$offset /dev/loop0 /data/mass_storage
mkfs.msdos -n DRAGON /dev/loop0
sync
answered on Stack Overflow Sep 22, 2020 by ArthurLambert • edited Sep 22, 2020 by ArthurLambert

User contributions licensed under CC BY-SA 3.0