Fabio D'Urso fabiodurso@hotmail.it [nuttx]
2018-01-14 22:45:21 UTC
Hello,
I am trying to expose a static FAT disk image (stored in ROMFS) to a PC
as a USB disk.
I have found what seems to be a copy-and-paste issue in
drivers/loop/losetup.c: it tries to open the image in RW mode first and
then RW again for the second attempt, despite the comment saying that
the second attempt is in RO mode. As a result, ROMFS files cannot be
used with losetup ("losetup: ioctl failed: EACCES").
With the attached fix (nuttx-losetup.patch), I can successfully run:
 losetup -r /dev/loop0 /etc/usb-disk.img
 msconn
and Linux sees the USB disk:
 scsi host7: usb-storage 3-1:1.0
 scsi 7:0:0:0: Direct-Access    NuttX   Mass Storage    0101 PQ: 0
ANSI: 2
 sd 7:0:0:0: Attached scsi generic sg4 type 0
 sd 7:0:0:0: [sdd] 100 512-byte logical blocks: (51.2 kB/50.0 KiB)
 sd 7:0:0:0: [sdd] Write Protect is off
 sd 7:0:0:0: [sdd] Mode Sense: 0f 00 00 00
 sd 7:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't
support DPO or FUA
 sdd:
 sd 7:0:0:0: [sdd] Attached SCSI disk
However, the disk is detected as writable (and I/O errors occur if you
try to edit its contents). I noticed that NuttX's usbmsc_bindlun
(drivers/usbdev/usbmsc.c) is smart and tries to guess whether the disk
should be presented as writable or not, according to the presence of the
write() method in the storage device's file operations table.
Unfortunately, this heuristic fails in this case, because the loop
device driver does have write() support for the general case of RW loop
devices.
I have solved this problem by adding Kconfig parameters to make it
possible to have msconn force the readonly flag. I am attaching this
second patch (apps-kconfig.patch) as well. With both patches applied and
the new Kconfig option set (it is unset by default - same behavior as
before), I have tested that everything works as intended:
 sd 7:0:0:0: [sdd] Write Protect is on
 sd 7:0:0:0: [sdd] Mode Sense: 0f 00 80 00
 sd 7:0:0:0: [sdd] Write cache: disabled, read cache: enabled, doesn't
support DPO or FUA
Best regards,
Fabio D'Urso
I am trying to expose a static FAT disk image (stored in ROMFS) to a PC
as a USB disk.
I have found what seems to be a copy-and-paste issue in
drivers/loop/losetup.c: it tries to open the image in RW mode first and
then RW again for the second attempt, despite the comment saying that
the second attempt is in RO mode. As a result, ROMFS files cannot be
used with losetup ("losetup: ioctl failed: EACCES").
With the attached fix (nuttx-losetup.patch), I can successfully run:
 losetup -r /dev/loop0 /etc/usb-disk.img
 msconn
and Linux sees the USB disk:
 scsi host7: usb-storage 3-1:1.0
 scsi 7:0:0:0: Direct-Access    NuttX   Mass Storage    0101 PQ: 0
ANSI: 2
 sd 7:0:0:0: Attached scsi generic sg4 type 0
 sd 7:0:0:0: [sdd] 100 512-byte logical blocks: (51.2 kB/50.0 KiB)
 sd 7:0:0:0: [sdd] Write Protect is off
 sd 7:0:0:0: [sdd] Mode Sense: 0f 00 00 00
 sd 7:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't
support DPO or FUA
 sdd:
 sd 7:0:0:0: [sdd] Attached SCSI disk
However, the disk is detected as writable (and I/O errors occur if you
try to edit its contents). I noticed that NuttX's usbmsc_bindlun
(drivers/usbdev/usbmsc.c) is smart and tries to guess whether the disk
should be presented as writable or not, according to the presence of the
write() method in the storage device's file operations table.
Unfortunately, this heuristic fails in this case, because the loop
device driver does have write() support for the general case of RW loop
devices.
I have solved this problem by adding Kconfig parameters to make it
possible to have msconn force the readonly flag. I am attaching this
second patch (apps-kconfig.patch) as well. With both patches applied and
the new Kconfig option set (it is unset by default - same behavior as
before), I have tested that everything works as intended:
 sd 7:0:0:0: [sdd] Write Protect is on
 sd 7:0:0:0: [sdd] Mode Sense: 0f 00 80 00
 sd 7:0:0:0: [sdd] Write cache: disabled, read cache: enabled, doesn't
support DPO or FUA
Best regards,
Fabio D'Urso