SD card formatting


HelenF

Very Active Member
Joined
Jun 22, 2013
Messages
615
Location
UK
So we have this note on the wiki about aligning partitions to 4MB:

http://pandorawiki.org/Formatting_SD_cards#Partitions

And more details:

http://wiki.laptop.org/go/How_to_Damage_a_FLASH_Storage_Device

The factory format is probably right, so what would be good ways to get information about it, either for making a similar one or to restore it later? The partition layout should be simple, but looks like the file system parameters might matter too.

One example card:

sudo fdisk -lu /dev/mmcblk1

Disk /dev/mmcblk1: 3965 MB, 3965190144 bytes
49 heads, 48 sectors/track, 3292 cylinders, total 7744512 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/mmcblk1p1 8192 7744511 3868160 b W95 FAT32
I think that means it has a partition which starts at 4MiB. Do SD cards always have partitions, or do some not? And a partition can't start at 0 because the partition layout information has to go at the beginning?
(cfdisk says "FATAL ERROR: Bad primary partition 0: Partition ends in the final partition cyl".)
 
Hmm, it's a while since I last formatted an SD card, and I wasn't aware of much of that stuff in your second link. All I usually do is observe where the partition starts and copy that start for my first partition.


All SD cards have partitons, usually just one at factory format. That's because they're emulating hard discs, and hard discs have partitions.


I should probably me more aware of block sizes, and the options to mkfs, but so far (touch wood) I've not had any trouble. I do tend to stick to Sandisk cards though, if that has any bearing.
 
I have one harddisk that I formatted without partition, so instead of mounting /dev/sda1, I mount /dev/sda

Linux can not automount it... have to do it manually and give even the type of the disk.

As for my SD cards, you have Id "b", both mine are "c". And of course, the number of blocks are not the same, as they get marked bad, you have less and less :)

The rest is the same, mine also start at 8KB.

Anyway, this here is the grub:

http://blog.silabs.com/2013/02/07/how-to-interface-an-sd-card-with-fat-file-system-using-spi/

For the lazy reader: You do not need the partition table nor MBR, but all devices expect it.

Most SD cards are sold pre-formatted with a FAT file system on top of an MBR partition scheme as per the above table. The popularity of this file system allows the card to be accessed on virtually any host device with an SD reader

...........

However most consumer products will expect MBR partitioning and FAT16/FAT32 filesystem.

Your SD card content is roughly laid out like this:

  • MBR and partition table
  • …maybe some sectors for logical partitions or reserved space…
  • Boot sector of a FAT16/FAT32 partition
  • …optionally some reserved sectors…
  • 1-2 copies of File Allocation Table (FAT)
  • Root directory
  • Other directories and data
  •  

From simple-fat-and-sd-tutorial: http://codeandlife.com/2012/04/02/simple-fat-and-sd-tutorial-part-1/

More about the MBR: http://en.wikipedia.org/wiki/Master_boot_record

As for making a copy. You can fill the SD disks with zero's, then use dd to copy the full disk to an image, and because it is mostly zero's it compresses very well.

If you are more a windows person, maybe this still works: http://www.winimage.com/winimage.htm
 
Last edited by a moderator:
Ah, I never clear the whole drive out when I repartition, just delete the old partitions and create new ones, so I guess I'm left with a MBR and FAT-style partition table. Then a gap, as per the original setup, then my various partitions with default params.
 
Thanks for the info!

As for making a copy. You can fill the SD disks with zero's, then use dd to copy the full disk to an image, and because it is mostly zero's it compresses very well.
Good idea. I can write some zero files with dd too.
The rest is the same, mine also start at 8KB.
I think it means 8192*512, i.e. 4MiB.
And of course, the number of blocks are not the same, as they get marked bad, you have less and less :)
Do blocks go bad often? That would corrupt files wouldn't it?
And if I backup an SD with dd, and then the number of good blocks goes down, will I be able to restore the backup?
 
And if I backup an SD with dd, and then the number of good blocks goes down, will I be able to restore the backup?
Depends on wether you back it up as a system of files and directories, or as a direct bit-for-bit copy, I imagine.
 
Back
Top