SD card recovery using an Arduino
One of my SD card “died” the other day. More specifically, my laptop wouldn’t read it anymore, and neither would any other machines or cameras I tried. It did react (looking at the dmesg output), but would only throw errors, and not recognize any partitons on the card. So I decided to try and read it using an Arduino.
All software mentioned here is either included in your Arduino IDE (the CardInfo sketch), or available on Github. Feel free to check out some of my other stuff there as well.
Attention: Dangerous half knowledge ahead
SD cards have two modes in which they can be accessed. One is the
SDIO SD bus mode*, which is used by your average card reader, your camera, etc. It’s fast, and somewhat complicated. The other mode is SPI mode. It’s much slower, but it’s fairly simple. That’s what you use if you connect your SD card to a microcontroller such as an arduino.
I used a Playduino One Arduino clone (Duemilanove compatible), that I had lying around, and a SD card shield I got from ElectroDragon. Just plugged them together, and ready is your own card reader hardware. I tried it with an Arduino Leonard clone as well, but that did not work. I guess that might be due to a different SPI pin setup, but I didn’t investigate further, and instead just went straight for the Playduino.
I took 3 steps in the recovery process: Grabing the card information using
The SD card library in the Arduino IDE contains an example called CardInfo. It connects to the card, and gets information such as size, filesystem type, and a list of files on the card.
That worked, so I kept going.
The sketch sd_recovery_files.ino tries to iterate over the filesystem, and outputs all files in a HEX encoded format over the serial port. If you save the output to a file on your machine, you can then parse the file using the supplied python script parse_files.py.
This surprisingly worked for a number of files, but the openNextFile() function doesn’t seem to work all that well, so not all files were copied.
The sketch sd_recovery_raw.ino reads the SD card block by block, and outputs it in a HEX encoded format over the serial port. You can parse tduemillanovehe output back into a binary file using the supplied parse_raw.py python script.
This may just take forever, and I have not been successful in using that file, but I have yet to actually copy the entire SD card that way (might take a few days for the 4GB card I have).
I tried sending binary data directly over the serial port, which would have made the entire process much quicker, but that did not work at all.
So even if no “normal” device reads an SD card anymore, all is not lost. Apparently, the
SDIO SD bus interface* can jump the sharks, without breaking the SPI interface completely. I still managed to get some of my data back, even though the stuff I was interested most in getting back seems gone. I could try to do a full backup of the card using my Arduino card reader, but that would take forever (I estimated about 4 days), since I’m transferring at least twice the necessary amount of data (thanks to the HEX encoding) over a 115200 baud serial line. Now my pictures weren’t that important.
Again, if you want to try it for yourself, grab an SD card shield and an Arduino, grab the firmware from my GitHub Repository, and give it a shot. Feel free to make adjustments to the code, I’d be glad to try it out.