Howto: Sd Card Recovery


tsh

Active Member
Joined
Dec 19, 2008
Messages
775
Location
Cambridge
Website
Visit site
Yay. Just recovered my SD card which got Panda-mangled. This was a 16GB card, formatted FAT32, which lost it's partition table (and either the names were cached, or it really did gain the LABEL of my other which is EXT2).

Having little to loose, I re-created the partition table using ubutu FDISK, and copied some stuff from other cards. This is only a good idea if no partitions are recognized on the card. This is also a very risky procedure - it can trash your desktop if you type wrong even by a single character.

In preparation, record your card partition data BEFORE it gets mangled, somewhere which will be recoverable.
Code:
desktop:~$ sudo fdisk -u -l /dev/sdc

Disk /dev/sdc: 15.9 GB, 15931539456 bytes
32 heads, 32 sectors/track, 30387 cylinders, total 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xca832577

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            8192    31116287    15554048    b  W95 FAT32

The /dev/sd<xx> needs to be for the card, on my pandora, I get this (copying also to USB stick)
Code:
sudo fdisk -u -l | tee /media/sda1/disks.txt
Disk /dev/mmcblk0: 3941 MB, 3941597184 bytes
86 heads, 21 sectors/track, 4262 cylinders, total 7698432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            8192     7698431     3845120    b  W95 FAT32

Disk /dev/mmcblk1: 16.0 GB, 16039018496 bytes
255 heads, 63 sectors/track, 1949 cylinders, total 31326208 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Disk identifier: 0x00025d6d

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk1p1            8192    31326207    15659008   83  Linux

Disk /dev/sda: 1010 MB, 1010827264 bytes
228 heads, 41 sectors/track, 211 cylinders, total 1974272 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Disk identifier: 0x000560ee

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1     1974271      987135+   c  W95 FAT32 (LBA)
Partition 1 has different physical/logical endings:
     phys=(122, 227, 41) logical=(211, 44, 40)

So we're ready to start. With a strong coffee...
Code:
desktop:~$ sudo fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/sdc: 15.9 GB, 15931539456 bytes
32 heads, 32 sectors/track, 30387 cylinders, total 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xca832577

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):
This is the important bit. No partitions listed, so this is the correct how-to for your problem. We need to create a partition table which exactly matches the one which used to exist on the disk. Assuming you don't know what that was, we need to guess. Because of the tendency for files to be written in certain sizes, and the physical design of flash, there is usually a gap between the partition table (which must be the very first entry) and the start of the partitions. The same applies to formatting a SSD. Based on the other cards I have, the primary partition tends to start at sector 8192. I tried the default offered by fdisk first, 2048, but my partition was not detected correctly (garbage might look nearly right, but only accept perfection in this game - I once re-partitioned a HDD with nearly the right number of sectors, and small files were OK). Writing the partition table alone should not affect the data on the disk - it is just the first of a series of jump tables to find your files.
Code:
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-31116287, default 2048): 8192
Last sector, +sectors or +size{K,M,G} (8192-31116287, default 31116287): 
Using default value 31116287

Command (m for help): p

Disk /dev/sdc: 15.9 GB, 15931539456 bytes
32 heads, 32 sectors/track, 30387 cylinders, total 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xca832577

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            8192    31116287    15554048   83  Linux

Command (m for help):
So we have a partition, but it defaults to Linux. We need to change this to the MS Dos type, listed in the earlier data we captured. See how my USB stick is type c. it should be safe to trial-and-error this.
Code:
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (W95 FAT32)

Command (m for help): p

Disk /dev/sdc: 15.9 GB, 15931539456 bytes
32 heads, 32 sectors/track, 30387 cylinders, total 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xca832577

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            8192    31116287    15554048    b  W95 FAT32

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
Note that until you enter the W command, it is safe - nothing has been written. After doing this, it might be good to read a few files from the disk and cofirm it seems OK - and only after that is it safe to let loose a disk checking tool.

Only use this HowTo when there is nothing else to loose. Please try not to trash your desktop - even if it was booted from a liveCD, it is not safe. Fdisk is like running across the highway blindfold. Usually it works fine.
 
Top