Saturday, June 3, 2017

SD Card Corruption - even in the Raspberry Pi.

SD Cards are used everywhere, and they aren't taken care of the way USB drives are.  People are reminded with USB drives and told when they are safe to be removed, but the same handling applies to SD Cards.  The problem is SD Cards are being "yanked out" electrically, even if they remain in their slots and are not physically being removed, and the software developers never read the SD Card hardware manual on how to handle them!

SD Cards will most likely get corrupted when they are being written to, and the power goes off.   Detailed specs on the cards specifies something like, the power to the card must be maintained for 4 seconds when last written too.  This is because there is actually a micro controller in the SD card that must write, erase flash pages and do house keeping during and after the card is written to, and it needs power to complete that correctly.

What happens is if a sector is being written to when power fails is the WHOLE SD Card page (with usually many sectors) with that sector your writing too will get corrupted.   Can look like all 0s with random 1s or the other way round. This is because there is a circuits that sets the 1 and 0 thresholds on the flash page that is not set correctly if power fails.  The way sectors are allocated in a file system will mean it may not be obvious that damaged sectors belong to the same page.  You just get a corrupted file system, directory or even just a file randomly.

This corruption can happen in digital cameras when you take a picture and the battery fails during the save process. Damaging  more than just the picture you just took.

Same for your own embedded project, like an SD Card with an Arduino.

In embedded Linux boards like Beagle Boards and Raspberry Pi's it occurs as the OS is ALWAYS writing log files, and so has a good chance of doing that when you need to pull the power plug.  The only way to really stop that is issue the shutdown command so the MCU and system halts before you remove power.  Really needs a battery backed psu, as well.
That command is:  sudo shutdown -P now Only when the SD Card access LED stops flashing is it safe to remove power.

Note that the way sectors are allocated to pages means that write protecting the boot drive is meaningless.

This is the same reason if you put a SSD drive in your computer, you want the one with the fancy battery backup feature too. That keeps it powered so that it can shut down properly, even if the main power fails.

One industrial Linux MCU board supplier solves the always writing logs problem of Linux by  having battery backed ram on their boards for the system logs. That means the system flash is under no threat of corruption when power is removed from the log writing.  Of course if your application is writing to flash and the power can fail, you need a process to call shutdown and know that it has halted before power fails.

We can be contacted at Art and Technology





No comments: