none
How to make mass storage as write protected? RRS feed

  • Question

  • Hi all,

    I am working on wince 6.0 R3. I have a SD card on my device and I have enabled wince mass storage client driver to expose the SD card as a mass storage device to the host PC and it is working fine. 

    Now my requirement is to expose the SD card as a read only mass storage device to host PC.

    can any body help on this?

    Thanks

    Subbarao.

    Thursday, December 23, 2010 7:44 AM

Answers

  • If you just want to make the SD card appear as read only to the USB host as opposed to write protecting the SD card on the device itself, you can clone the USB Function Mass Storage Client Driver into your BSP and modify the SCSI2DAD library from WINCE600\PUBLIC\COMMON\OAK\DRIVERS\USBFN\CLASS\STORAGE\EMULATION\SCSI2\BLOCK so that it won't allow the SCSI Write command.  I've never acually tried to do this, but I'd recommend these changes to BLOCK.CPP:

    • Change STORE_IsCommandSupported() to return FALSE for the SCSI_WRITE10 command by setting fResult = FALSE.
    • ProcessScsiWrite10() to always return EXECUTE_FAIL
    • I also would change the ProcessScsiModeSense10() and ProcessScsiModeSense6() to set the Write Protect (WP) bit in the Mode Parameter Header.  There appears to be a possible bug in ProcessScsiModeSense6().  For MODE SENSE(10), the WP bit is Bit 7 of Byte 3 of the header, and ProcessScsiModeSense10() seems to be doing this correctly.  However, for the MODE SENSE(6) command, the WP bit is Bit 7 of Byte 2 of the header, but ProcessScsiModeSense6() in the version of BLOCK.CPP I have appears to be incorrectly using the MODE SENSE(10) Parameter Header, placing the WP bit in Byte 3 instead of Byte 2.  I believe the code should be:
      // mode data length
      pbData[0] = 6; // length should be just 1 byte
      // medium type, 9.3.3
      pbData[1] = 0x00; // default medium type
      // device-specific parameter, 9.3.3
      pbData[2] 0x00; // bit 7 = WP (write protected)
    
    

    Tom Gensel PTG Systems, LLC
    http://www.ptgsystems.com
    • Proposed as answer by Vinoth[MCTS] Monday, January 3, 2011 1:05 PM
    • Marked as answer by Subbarao. PNV Tuesday, January 4, 2011 4:23 PM
    Saturday, December 25, 2010 11:35 PM

All replies

  • On 12/23/2010 8:44 AM, Subbarao. PNV wrote:
    > Hi all,
    >
    > I am working on wince 6.0 R3. I have a SD card on my device and I have
    > enabled wince mass storage client driver to expose the SD card as a mass
    > storage device to the host PC and it is working fine.
    >
    > Now my requirement is to expose the SD card as a read only mass storage
    > device to host PC.
     
    SD write protection just signals a pin and it's up to the SW layers to
    read and support it...
    You may implement read-only behaviour by modifying the sd memory driver
    or by implementing a filesystem-filter.
    In the first way your wp mechanism will block also low-level access to
    SD, the second one could be used also on other storage devices.
     
     

    Valter Minute
    Windows Embedded MVP
    http://geekswithblogs.net/WindowsEmbeddedCookbook
    Thursday, December 23, 2010 11:47 AM
  • If you just want to make the SD card appear as read only to the USB host as opposed to write protecting the SD card on the device itself, you can clone the USB Function Mass Storage Client Driver into your BSP and modify the SCSI2DAD library from WINCE600\PUBLIC\COMMON\OAK\DRIVERS\USBFN\CLASS\STORAGE\EMULATION\SCSI2\BLOCK so that it won't allow the SCSI Write command.  I've never acually tried to do this, but I'd recommend these changes to BLOCK.CPP:

    • Change STORE_IsCommandSupported() to return FALSE for the SCSI_WRITE10 command by setting fResult = FALSE.
    • ProcessScsiWrite10() to always return EXECUTE_FAIL
    • I also would change the ProcessScsiModeSense10() and ProcessScsiModeSense6() to set the Write Protect (WP) bit in the Mode Parameter Header.  There appears to be a possible bug in ProcessScsiModeSense6().  For MODE SENSE(10), the WP bit is Bit 7 of Byte 3 of the header, and ProcessScsiModeSense10() seems to be doing this correctly.  However, for the MODE SENSE(6) command, the WP bit is Bit 7 of Byte 2 of the header, but ProcessScsiModeSense6() in the version of BLOCK.CPP I have appears to be incorrectly using the MODE SENSE(10) Parameter Header, placing the WP bit in Byte 3 instead of Byte 2.  I believe the code should be:
      // mode data length
      pbData[0] = 6; // length should be just 1 byte
      // medium type, 9.3.3
      pbData[1] = 0x00; // default medium type
      // device-specific parameter, 9.3.3
      pbData[2] 0x00; // bit 7 = WP (write protected)
    
    

    Tom Gensel PTG Systems, LLC
    http://www.ptgsystems.com
    • Proposed as answer by Vinoth[MCTS] Monday, January 3, 2011 1:05 PM
    • Marked as answer by Subbarao. PNV Tuesday, January 4, 2011 4:23 PM
    Saturday, December 25, 2010 11:35 PM
  •  

    Hi Gensel, 

    Thanks for your reply. 

    I have modified the function ProcessScsiModeSense6() as you said in your reply and it is working fine. I didn’t modify the functions STORE_IsCommandSupprted() and ProcessScsiWrite10(), since write protect is working fine with the modification in ProcessScsiModeSense6() function only.

    Should I modify STORE_IsCommandSupprted() and ProcessScsiWrite10() functions also. 

    Now my requirement is to change the write protect of the mass storage dynamically. 

    By default Mass storage device write protect bit is set “1” and after enumeration of the device host is sending "SENSE MODE 6" command and device replied with write protect bit as 1. In this case Mass storage device detected as “write protected” and I am unable to copy files from host PC to the mass storage device. 

    Now I am changing the write protect bit as “0” (to disable the write protect), after I did this, some times host PC is sending SENSE MODE 6 command, then device replied with write protect bit as 0. Now mass storage write protect is disabled and able to copy the files into mass storage. But some times SENSE MODE 6 command is not being sent by the host.

    Do you have any idea when the host will send SENSE MODE 6 command?

    Thanks,

    Subbarao.

     

    Tuesday, January 4, 2011 4:21 PM
  • The choice of whether to make further modification is yours since it sounds like it's working to your satisfaction.  If it were me, however, I would go ahead and make the other changes, because the Write Protect is a software-only mechanism, so a hacked driver on the host could probably circumvent that if it is the only thing preventing writes.  By failing the write directly if the WP is enabled, you help insure a hacked host driver can't do this.

    Unfortunately I'm not a SCSI-expert by any means, so I'm not at all familiar with when the host does or doesn't issue a SENSE MODE command.  This too seems to be another good reason to fail the Write command if the WP bit starts disabled and then transitions to enabled but the host doesn't detect it with a Mode Sense.


    Tom Gensel PTG Systems, LLC
    http://www.ptgsystems.com
    Thursday, January 6, 2011 6:47 PM
  •  

    Hi Gensel,  

    Thanks for your reply.  

    I have modified the function ProcessScsiModeSense6() as you said in your reply and it is working fine. I didn’t modify the functions STORE_IsCommandSupprted() and ProcessScsiWrite10(), since write protect is working fine with the modification in ProcessScsiModeSense6() function only.

    Should I modify STORE_IsCommandSupprted() and ProcessScsiWrite10() functions also.  

    Now my requirement is to change the write protect of the mass storage dynamically.  

    By default Mass storage device write protect bit is set “1” and after enumeration of the device host is sending "SENSE MODE 6" command and device replied with write protect bit as 1. In this case Mass storage device detected as “write protected” and I am unable to copy files from host PC to the mass storage device.  

    Now I am changing the write protect bit as “0” (to disable the write protect), after I did this, some times host PC is sending SENSE MODE 6 command, then device replied with write protect bit as 0. Now mass storage write protect is disabled and able to copy the files into mass storage. But some times SENSE MODE 6 command is not being sent by the host.

    Do you have any idea when the host will send SENSE MODE 6 command?

    Thanks,

    Subbarao.

     


    Hi Subbarao,

    I am working on Mass storage device...as you said in this above message to make write protect the device...we have to set the scsi sense command(6) bit 7 of 2 byte...so the value is 0x80....am i correct?

    you can see this book for usage of SCSI commands in mass storage device.

    USB Mass Storage Designing and Programming
    Devices and Embedded Hosts
    Jan Axelson

    Thanks

    arul

     

    Saturday, January 29, 2011 6:27 AM
  • Hi Arul,

    You are right, we have to set the 7th bit of 2nd byte in ProcessScsiModeSense6 function. It will work.

    I will check the book suggested by you. Thanks for your reference.

     

    Thanks

    Subbarao

    Thursday, February 3, 2011 4:10 PM
  • Hi Gensel,

    I need an help USB mass storage Removal.

    I am working Windows CE 6.0. I want to enable and disable USB Mass storage dynamically.

    Can you please guide me, how to do the above?

     

    Thanks for your help.

    Laxmi

     

    Monday, January 23, 2012 7:59 AM
  • Hi,
     
    You can dynamically switch between the USB clients.See the following blog post for the same.
     
     
    If you want only USB Mass storage enable and disable,you can simply remove the RNDIS client driver from your OS image and switch to that client when you want to disable the USB Mass storage.
     
    Monday, January 23, 2012 12:40 PM