• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2253
  • Last Modified:

Zero out flash memory

I work with embedded systems.  Normally, when I need to wipe a hard drive clean, I use the Linux badblocks command to zero out the drive.  badblocks -svw -b 512 -t 0x00 /dev/<device> does the trick by doing a destructive bad sector test using zeroes as the test pattern.  I find that method to be faster than dd if=/dev/zero of=/dev/<device> bs=512, plus you get a progress indicator.

However, this method does not work on flash type media.  I have tried USB sticks and most recently, SD cards.  I wind up getting I/O errors about 3/4 of the way through, and the end of the card (as viewed in a hex editor) is not zeroed out at all (although the beginning is).  This happens with different media from different brands.  The failure is not always in the same spot, but they all fail.

SanDisk media in particular reacts very badly to this procedure.  Once you get the I/O errors, it is simply a matter of making a filesystem on it to use it again in Linux.  However, no Windows machine is ever able to work with the device ever again (not that I care that much about Windows, but it's odd).

Anwyay, my questions is this:  Is there some kind of booger in the way the Linux kernel is working, or can you not do block level operations on flash media like that?

How would you do a low-level zero of all the data on a flash card?
0
cuziyq
Asked:
cuziyq
  • 4
  • 4
1 Solution
 
rindiCommented:
What happens if you format it to fat32 within Linux?
0
 
cuziyqAuthor Commented:
For SanDisk media specifically, Linux will read the device just fine.  However, even with a FAT16 or FAT32 format, Windows cannot use it after it's been through the Linux badblocks scan.  Different types of media exhibit different behavior.  For example, with a 4GB SD card, Windows sees the device and even gives it a drive letter.  But trying to open it or get properties on it hangs Explorer to the point where Task Manager won't even kill it.  If I plug it into a SD-IDE adapter, Windows recognizes it as a fixed disk, but does not see that it is formatted.  When you try to format it with Disk Administrator, it grinds on it for about 30 seconds and then gives a very generic device error.

With a 2GB USB flash drive, Windows detects the stick in the port and knows what it is, but claims that it can't load the driver for it.  In both situations, Linux sees and uses the thing just fine.

So FYI, don't ever do a dd or badblocks command on SanDisk flash media if you ever want to use it in Windows ;-)

Anyway, I don't really care if it works in Windows or not.  I just don't understand why I can't do a wipe the same way I would with a hard drive.
0
 
rindiCommented:
And if you del all partitions, can you format it within windoze then?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cuziyqAuthor Commented:
Not with SanDisk.  I have tried it with Kingston, Patriot, and A-Data flash memory.  They all work fine.  But once a SanDisk has been run through the wringers like that, Windows will never be able to work with it no matter what I've tried.
0
 
rindiCommented:
Strange.
0
 
cuziyqAuthor Commented:
I've found the answer.  Flash memory cells have a finite number of write cycles before the cell becomes "worn out" and unusable.  Flash devices have a non-addressable "slack" region that the controller uses to dynamically reallocate blocks so that wear is leveled out across the entire memory space, thus increasing its lifespan.

The oddity here is that Linux can see this slack space and thinks it is an addressable region when in fact it is not.  When it tries to write to that area, the flash controller barfs and now you have a bad low level format (which confuses Windows, but not Linux).

The answer, in case anyone is wondering, is to use a tool like WinHex, which can get raw access to physical devices in Windows, and zero it out that way.  Since Windows can't see that slack area, writing zeroes to the space that is addressable causes the flash controller to do its wear-leveling thing and properly deallocate the slack area, making the low-level format valid again.

I can't say if this is universal behavior, but it worked on the two SD cards I've tried it on.  It effectively revived otherwise dead cards.
0
 
cuziyqAuthor Commented:
Also FYI, in case anyone is interested . . . ALL flash memory devices employ a wear-leveling algorithm like this (with the exception if cheap Chinese counterfeits from eBay, which explains why their quality is so poor).  Therefore, it is physically impossible to securely wipe a flash memory device.  If it had sensitive information you wish to get rid of, the only option is to destroy the card.
0
 
rindiCommented:
Don't delete, PAQ it. The asker provided useful info about flash memory and answered it himself.
0
 
Vee_ModCommented:
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now