Skip to content

Instantly share code, notes, and snippets.

@psiie
Created July 8, 2017 21:41
Show Gist options
  • Save psiie/9677f89776ab17f35cdf39209ad0b79f to your computer and use it in GitHub Desktop.
Save psiie/9677f89776ab17f35cdf39209ad0b79f to your computer and use it in GitHub Desktop.
Test Fake SD Cards using DD and PV.
if [ ! $1 ] || [ ! $2 ]
then
echo "---------------------------------------"
echo "WARNING. Will write to drive"
echo ""
echo "Command Syntax: ./fake-sd-check DRIVE GB-SIZE"
echo " example: ./fake-sd-check /dev/disk3 32"
echo "---------------------------------------"
exit
fi
echo "----- Darkenvy's Fake SD Card Tester ------"
echo "\nThe test may fail as we do not know the exact size (to the bit) of the SD card. This is expected. Just be sure, that when the process finishes, the report shows a transfer of around the card size.\n"
COUNT=`expr 1024 \* 1024 \* $2`
PVCOUNT=`expr 1024 \* 1024 \* 1024 \* $2`
sudo dd if=/dev/zero bs=1k count=$COUNT | pv -brpets $PVCOUNT | sudo dd of=$1
@quozl
Copy link

quozl commented Jul 9, 2017

Thanks, that's interesting.

You can find the exact size to the bit using /proc/partitions and multiplying by 1024. Linux won't write beyond that size.

Your pv will finish early, while it waits for the second dd to finish; you can make that less of an annoyance by adding oflag=dsync to the second dd.

Not all fakes will be detected by this method. It will only detect the fakes where the size reported by the flash translation layer grossly exceeds the physical flash array. That's the easiest fake to make, using card firmware reprogramming, so it is the most common fake. It also happens when a production run is made with incorrect settings; normally those cards are thrown away, but they can be stolen or redirected and become what we see as fakes.

Some flash translation layer firmware detects the zero block write and links the logical block to a single common block of zeros. On those cards, this method won't detect any problem.

To detect these and other fakes, you might use badblocks with the -t random option. There's also the -n option to make the test non-destructive.

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment