On occasion compact flash cards can get corrupted. I think the most common case is you forget to ``unmount'' them and then insert another card. Certain operating system then happily writes the old FAT table onto the new card. This happens in XP/2K/NT, as well as Linux. The net effect is you need to reformat the card, losing all of your images. This is a bummer, so here is a quick routine that will recover *most* of your image data.

On a Unix system (Linux, Mac OS X, etc), issue the command:

dd if=/dev/disks1 of=image.img bs=512

Make sure that the /dev/? input device is the RAW disk, not the partition (which may be corrupted). You want a snap shot of the entire disk. The above device is for OS X, but a Linux device is something like /dev/hdb

Next, grab this source code and compile it for your operating system. Then execute the command:

saveimg image.img

You will then get a collection of files save_img_001.jpg, save_img_002.jpg, ... Some of these files will be ok, some will be corrupt. To determine which, you can use your favorite image editor, or download the netpbm tools (or certain gimp installations have these as well). Using the program "jpegtran" you can process the images from the command line. jpegtran will identify good from bad.

How does this work? Usually, you use an almost blank compact flash card in your digital camera when you begin taking pictures. With the FAT file system this means that files are stored sequentially onto the raw device. What the saveimg program does is assume the input file is a collection of JPEG files that have been cat'ed together (and aligned on 512 byte boundaries -- like a physical disk). It then slices up the input file by looking for the header bytes of a JPEG image. When it finds those it starts a new output file.

A couple of notes: the first output file "save_img_001.jpg" is always garbage. The first file is the partition table and FAT tables and other misc. Second thing to note is that if your compact flash card was *NOT* blank prior to inserting it into your camera, then your milage may very substantially. However, using this simple hack I've been able to recover a totally messed up compact flash card (one with a corrupted FAT table) and get back about 90% of the images in perfect form. YMMV.

Some updated notes....

  • Users report that it works on Windows if you install the cygwin tools. The raw devices are named like if=//./D
  • Seriously bad compact flash cards might require the conv=noerror flag on the dd command line. You will see it spew out a bunch of I/O errors, but hey it worked for at least one user out there.
  • After you have recovered your image files it's always nice to be able to use the compact flash card again. Your milage may seriously vary on this. One of my cameras doesn't seem to care that the card is corrupt and will happily reformat it correctly. Another just gives up. If your camera is in the latter catagory you probably ought to repartition /reformat the card on your desktop -- which may be difficult on some operating systems. Eitherway, I do not suggest you use the card with the existing corrupt file system. By the way, if you are good enough to plan ahead, then you could have taken a snapshot of the card with dd when you first bought it. Then if your OS is too brain dead to format it you could just dd the snapshot back onto the raw device -- of course, I'm never planning that far ahead myself ;-)