Write protect issue on SD cards (not the lock switch)

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Write protect issue on SD cards (not the lock switch)

Post by DavidCooper »

I've been using flash drives and SD cards for my OS for a long time now, loading my OS from them, modifying the OS, and then writing it back to them using the BIOS to do all the writes. Until recently it's been fine, but all of a sudden I've found both the SD cards I'm using keep becoming write protected after saving to them. This began to happen not when modifying my OS though, but merely by saving ordinary text files, so it isn't any change to my OS that's caused the issue. I suspect it's something about the nature of SD cards themselves.

I can get the SD cards back to normal again just by putting them in a Pentax camera and taking a photo, but it's annoying having to do that as I then have to reboot before the BIOS accepts them again (because it doesn't like an SD card to be taken out and put back in). So, what I'm wondering is, has anyone else had this problem, and is there anything I can do (via the BIOS) to remove write protection from the SD card after each write to it.

(The problem may go away once my OS can use its own USB code to write to SD cards without going through the BIOS, so this isn't a major issue. I'm just hoping there might be a quick fix to make life easier in the meantime.)
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Write protect issue on SD cards (not the lock switch)

Post by Brynet-Inc »

The physical lock switch on SD cards is essentially the same as the lock switch on floppy disks, it requires support from the device it's inserted into and can be ignored.

http://www.seanet.com/~karllunt/sdlocker.html
http://hackaday.com/2014/01/18/the-tini ... rd-locker/

SD cards have a pretty extensive feature-set (MMC), one interesting feature is the ability to lock writes temporarily until unlocked or even lock permanently. There's a 32-bit ARM microcontroller inside even microSD cards, and it's running a complex firmware that handles communication with the host.

Unfortunately with a USB card reader you can't do any of that, you need a real SD controller.. some laptops do have PCI host controllers though.

http://www.bunniestudios.com/blog/?p=3554
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
embryo

Re: Write protect issue on SD cards (not the lock switch)

Post by embryo »

DavidCooper wrote:I can get the SD cards back to normal again just by putting them in a Pentax camera and taking a photo
May be it's the file system that was changed and allows BIOS code to work with the card? In my practice incomplete partition table was a real cause of a problem like described above. It can be any data structure on the media that can be changed properly by the Pentax and not properly by your code.

It is possible to get raw bytes rigth from the media start and up to a few megabytes, after your system makes the card inoperable. Next, after the Pentax makes the cart working, it is possible to get the raw bytes againt and to compare them with previous version.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Write protect issue on SD cards (not the lock switch)

Post by DavidCooper »

Brynet-Inc wrote:The physical lock switch on SD cards is essentially the same as the lock switch on floppy disks, it requires support from the device it's inserted into and can be ignored.

http://www.seanet.com/~karllunt/sdlocker.html
http://hackaday.com/2014/01/18/the-tini ... rd-locker/

SD cards have a pretty extensive feature-set (MMC), one interesting feature is the ability to lock writes temporarily until unlocked or even lock permanently. There's a 32-bit ARM microcontroller inside even microSD cards, and it's running a complex firmware that handles communication with the host.

Unfortunately with a USB card reader you can't do any of that, you need a real SD controller.. some laptops do have PCI host controllers though.

http://www.bunniestudios.com/blog/?p=3554
Thanks for the links and other info. (I look forward to communicating directly with cards some day and risking write protecting them permanently - I didn't know that hazard was available.)

So, it's a control register that's responsible for changing it to write protected and it's something the BIOS has started doing by mistake. It's a puzzle though as to why it's started doing it now after so long, starting just a couple of weeks ago with one card, and then yesterday with the second card. It's just possible that I'm doing something different, but if so it's not through any modifications to my code (because it's happened at a time when I haven't been changing it; all I'm doing is using my OS to load in text files from my own partition, to edit them, and then to write them back to my own partition, though not always on the same drive), but must be something I'm doing as a user of my OS. I'll have to try a few experiments to see if multiple reads are necessary before a write in order to prevent it from ending up write-protected after the first set of writes.


embryo wrote:
DavidCooper wrote:I can get the SD cards back to normal again just by putting them in a Pentax camera and taking a photo
May be it's the file system that was changed and allows BIOS code to work with the card? In my practice incomplete partition table was a real cause of a problem like described above. It can be any data structure on the media that can be changed properly by the Pentax and not properly by your code.

It is possible to get raw bytes right from the media start and up to a few megabytes, after your system makes the card inoperable. Next, after the Pentax makes the card working, it is possible to get the raw bytes againt and to compare them with previous version.
I was hoping it would be something of that kind that's easy to access, but I'm not writing to the FAT partition at all, or even reading it, so the only changes made there are the ones made by the camera. The camera must be changing a bit in a control register to enable itself to write to the card again, and the BIOS must be modifying it too for some reason, but in the wrong direction. I have to try to find a way to stop it doing that. I know that it used to write to the cards without setting it, so hopefully I can find a way to get it back to doing that.

Anyway, it's a problem that's unlikely to affect many people as it's so unusual to use the BIOS to write to SD cards. If I find an easy way to fix it, I'll post about it here just on the off chance that it helps someone else. The idea fix though is to write USB drivers and escape from the BIOS. Some day...
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Write protect issue on SD cards (not the lock switch)

Post by Brynet-Inc »

DavidCooper wrote:So, it's a control register that's responsible for changing it to write protected and it's something the BIOS has started doing by mistake.
As I said, for USB SD card readers.. the BIOS never issues commands to the SD card directly, it only sees a USB mass storage device. The firmware in your USB SD card reader is probably responsible for this behaviour.. perhaps it sets the write protect bit on some error condition, maybe a misaligned electrical connection caused an errant bit flip, or.. :-)

It might be time to buy a new SD card reader, or, maybe find a laptop with a PCI controller. There's the older "SD Host Controller Standard Simplified Specification", but a Realtek chipset has become increasingly popular which needs another driver.

An external USB reader might be better though, some BIOS's can't boot from those types of controllers.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Write protect issue on SD cards (not the lock switch)

Post by DavidCooper »

I don't actually know how the SD card reader is wired into the machine - I didn't notice it when going through the PCI devices (when I was experimenting with Intel HD audio), but it may have been there in a form I didn't recognise. As a result, I assumed it must be wired up via USB, but that assumption may well be wrong. If it turns out that it's there as a PCI device, I should be able to write to that control register fairly easily, but I'll have to be careful as the bit to set permanent write-protect has been cunningly put in the same control register. I'm using expensive high-capacity SD cards, so I don't want to make mistakes.

[Edit on 2nd June: the second card's worse than the first - I can't write to it with the BIOS at all now, even though the camera can use it. I'll just give up on using SD cards for now and hope the problem doesn't start affecting flash drives too next.]
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Post Reply