1
\$\begingroup\$

I come from a software background, but feel this is a hardware/electronics question.

Looking for a way to hardware-write-protect on flash media, I bought an SDHC card (with a tiny switch labelled "lock") and a cheap USB card reader. I wanted to create a 'read-only' device that I can be certain will not be modified - without having to consider the software on the hosts to which I connect it.

Inserted directly into my Dell laptop, the card can only be written when unlocked... I assumed this meant that the write protect is implemented by the SD card... When I put the SD card into the USB reader, however, the same laptop allows me to write to the locked card. I've now read this question, and this confirmation that I was wrong to assume that the 'lock' switch would provide hardware write protect. I now know that the 'lock' is not a feature of the card in isolation - and that standard 'locked' depends upon OS drivers (which I don't want to be forced to trust.)

I would like to know: What would be a straightforward, simple, reliable, neat and cheap way to implement a hardware write protect switch for flash media? Ideally, I would like to access this media over USB. I assume that I will need a device that can intercept the blocks of data exchanged over the serial interface to either USB or the SD card?

\$\endgroup\$
  • \$\begingroup\$ I think this question is somewhat off-topic here, but I’ve seen someone who had an USB thumb drive with a RO/RW switch. \$\endgroup\$ – user2233709 Mar 31 '17 at 20:32
2
\$\begingroup\$

You can mark the SDCard as read only or temporary write protect. You can also hide storage or password protect for write.
If you don't want to modify code/drivers then you could just build an external unit to quickly write protect your SDCards.
See here for one implementation that uses an external ATTiny85 micro as man-in-the-middle.

You should read the simplified specs

You will find general discussion in Physical Layer 5.0 You can permanently and irrevocably mark a storage area as Read Only.

4.3.6 Write Protect Management
Three write protect methods are supported in the SD Memory Card as follows:
- Mechanical write protect switch (Host responsibility only)
- Card internal write protect (Card's responsibility)
- Password protection card lock operation.

Password Card Lock looks like it would be the best bet for you.

4.3.7 Card Lock/Unlock Operation
The password protection feature enables the host to lock a card while providing a password, which later will be used for unlocking the card. The password and its size are kept in a 128-bit PWD and 8-bit PWD_LEN registers, respectively. These registers are non-volatile so that a power cycle will not erase them.

Be aware (from Ali Chen's discourse) that not all SDCards you buy have all features of the spec implemented. YMMV depending on what you buy. If you wanted to be thorough, I'd drop an email to Sandisk or others and ask if they support this feature.

Look at these two projects:

  1. http://www.seanet.com/~karllunt/sdlocker.html This shows you how to set PERM_WRITE_PROTECT ....no way back...permanently write protected.
  2. http://www.seanet.com/~karllunt/sdlocker2.html This shows you how to set the TMP_Write_Protect bit and the Password protection.

Samsung definitely support PERM_WRITE_PROTECT as shown in their spec for the CSD register. It correctly shows it as (W)1 ...a write once value.

From these you should be able to find a solution that meets your needs.

\$\endgroup\$
  • \$\begingroup\$ Jack, Isn't it true that all internal card Write Protection is "optional" in the specifications, so no manufacturers actually have it implemented in mass products? The Samsung specs show this PERM_WRITE_PROTECT as "unused". If you could post any SW tools to turn the PERM on, I will withdraw my "dissertation". \$\endgroup\$ – Ale..chenski Mar 31 '17 at 22:24
  • \$\begingroup\$ @AliChen. Sandisk certainly support it. In fact they have a service problem in it occasionally getting set for users....effectively bricking their SDCards. The only option is to send the SDCard back to Sandisk for replacement. I agree that there are suppliers who don't support it. But the OP can certainly find those that do. \$\endgroup\$ – Jack Creasey Mar 31 '17 at 22:38
  • \$\begingroup\$ Jack, I found this topic of 5 years ago: superuser.com/a/354497/620011 Toshiba doesn't implement the WP management. I am sure many other follow. So you need to modify your answer accordingly, with SanDisk, and point to a tool that can do this. Obviously this is nonj-standard procedure, and many USB card readers simply don't support corresponding SD commands. \$\endgroup\$ – Ale..chenski Mar 31 '17 at 22:41
  • \$\begingroup\$ Ali, we are now at Rev 5.0 of the spec. And yes, as I said, some manufacturers don't support features ....some do. This guy (seanet.com/~karllunt/sdlocker.html) showed the viability several years ago. All the OP has to do is get a card that supports PERM_WRITE_PROTECT and he's good to go ...even if he has to build a small tool to set the bit in CSD. I've not seen any toolsets for MAC/Windows/Linux, but certainly ChibiOS has code to support the features: chibios.sourceforge.net/docs3/hal/group___m_m_c_s_d.html ...there is also code on the TI MSP430 for doing this. \$\endgroup\$ – Jack Creasey Mar 31 '17 at 22:58
  • \$\begingroup\$ Jack, even at Rev.5, the main section 4.3.6 states that "Internal Write Protection" is optional. I don't understand your reluctance to improve your answer, to expand that not all SD cards have this feature. There supposed to be another nice feature, FORCE_ERASE with removal of permanent write protection, so the flash can be still re-used. Where is it? The SD_locker guy uses TMP_WRITE_PROTECT, which is implemented everywhere, to LOCK and UNLOCK, which is not what OP is asking. \$\endgroup\$ – Ale..chenski Mar 31 '17 at 23:10
1
\$\begingroup\$

The simplest answer is to find a different USB card reader, one that properly implements the locking.

It sounds like you might view USB card readers as simple pass-through devices. They are not -- the USB protocol is completely different from the SD card protocols. So there is already a microcontroller continuously intercepting the blocks of data. You just need to find a better one that also checks the lock bit.

\$\endgroup\$
  • \$\begingroup\$ Perhaps - though my understanding of the answer to Melab's question was that the OS drivers implement the write-protect. If the USB card reader microcontroller were to implement the write-protect, that would work. How can I establish if the microcontroller itself, not the OS driver, is actually implementing the write-protect functionality? \$\endgroup\$ – aSteve Mar 31 '17 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.