Testing a new SD card under linux

SD cards are a common target for product forgery. Many cards on the market (be it eBay or retailers) are rebranded, have fake capacity or are simply bad ones initially removed from production that are resold afterwards by third parties. Bunnie has an excellent article on fake SD cards and how to test them. For more info on SD cards, check out this flash card survey.

The card I bought, a Sandisk 16GB class 4 Micro SDHC memory card, including adapter

I recently bought an SD card from Sandisk, one of the biggest target for forgeries. It was labeled “Sandisk 16GB class 4 Micro SDHC memory card”. I bought it on eBay, it was coming from China, and it was cheap (16GB for 8€ including shipping). Of course, I wanted to make sure that I got a good one, so I ran a couple of tests. (BTW, I guess the best up-front-protection against fakes is not to buy the cheapest offer on the market.)

Here’s what I did under Ubuntu Linux for testing my card. I ran two tests: one for the performance using flashbench, and another one to do a full-write test in order to reveal capacity problems. My laptop only has a USB card reader, so that I cannot read the manufacturer and other info of the card. Not the best for testing, but well.

Attention: the example below writes to /dev/sdb, which is the device for the card in my case. In your case, it might well be a disk. Make sure that you use the proper device (often something like /dev/mmcblk1 if you have a real card reader).

Running flashbench:

$ ./test_sd_card.sh /dev/sdb
4MiB    10.9M/s
2MiB    4.06M/s
1MiB    4.05M/s
512KiB  2.73M/s
256KiB  1.99M/s
128KiB  924K/s  
64KiB   508K/s  
32KiB   282K/s  
16KiB   194K/s  
8KiB    761K/s  
4KiB    959K/s

So, looking at the flashbench wiki this seems to be fairly reasonable for a class 4 card.

Next, I did the full-write test, to see if the card was actually had the promised capacity. Obviously, you need a bit of space on your disk for this. We’re creating a random data file, write it to the SD card, read it back, and check for differences.

Create a file with the required capacity, filled with random data. We’re writing blocks of 1024 bytes, so adjust the count to the capacity required (16GB in my case).

dd if=/dev/urandom of=rnd_init_data bs=1024 count=16000000

Now, we write the data to the SD card.

$ dd if=rnd_init_data of=/dev/sdb bs=1024 count=16000000
15558145+0 records in
15558144+0 records out
15931539456 bytes (16 GB) copied, 536.286 s, 29.7 MB/s

You can see, we didn’t manage to write all the bytes to the card. This is expected, as we don’t know the capacity of the card down to the last byte. As long as the capacity reported by dd (16 GB) is close to the expected result, we’re happy.

Now, we copy the bytes written to a new file, which we use for later comparison. Notice here the count, which we adapted to the bytes written to the card (15931539456 / 1024 = 15558144). I am sure this can be done easier using tools like truncate, but I didn’t want to start messing with programs that I don’t know by heart.

$ dd if=rnd_init_data of=rnd_written_data bs=1024 count=15558144
$ rm -f rnd_init_data

Read back the data from the card:

$ dd if=/dev/sdb of=rnd_read_data bs=1024 count=15558144

Finally, we’re comparing the two data files:

$ md5sum rnd*
dfed784abed6662926eb01f7fb5359ca  rnd_written_data
dfed784abed6662926eb01f7fb5359ca  rnd_read_data

Et voilà, my card is genuine (or at least does neither have abysmal performance nor fake capacity). Yay, I’ve been lucky!

Don’t forget to clean up (remove files, reformat card):

$ rm -f rnd_written_data rnd_read_data
$ fdisk /dev/sdb    # keys: o, n, p, 1, <accept defaults 2 times>, t, c, w
$ mkfs.vfat -F 32 /dev/sdb1

(The fdisk line creates a single primary FAT32 partition on the card.)

6 thoughts on “Testing a new SD card under linux

  1. Very useful, I’ve been doing some tests with badblocks looking for I/O errors within a standar usb card, there’s no such tool yet for Linux I guess… thanks

  2. You can get the actual size of the device, in bytes, with the command `blockdev –getsize64 /dev/sdc`. And you are right about truncate: just `truncate -s ` will make the given file of the given size, in bytes.

  3. Actually, these cards are getting so big that I didn’t have the room to create a 64Gb random file on my drive. And they’re not getting any smaller.

    You don’t have to create the intermediate data file with random data if you use:

    dd if=/dev/urandom bs=1024 count=62367744 | tee DEVICEFILE(like /dev/sde) | md5sum
    dd if=DEVICEFILE bs=1024 count=62367744 | md5sum

    You have to get the count from the reported size of the device divided by the blocksize you use.

    Also, for convenience, the gnome-disk-utility will report the size of the device and will test the speed.

    Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *