SD Card Write protection

Most SD cards have a physical write protection switch. This switch is read by the reader
instead of being enforced by the card. Of course, many readers ignore this signal and will
happily write to a write protected card. Furthermore, Micro SD cards do not have a mechanical switch.

It is relatively unknown that SD/MMC cards also have an electronic write protection system.
Every card has two programmable flags, one for temporary write protection and
one to lock the card forever. Writing these flags is not supported by most SD host devices.
To remedy this, this page presents a program that allows a Linux host to configure the protection register.

Usage of the program is simple:
./sdtool [device] [command] where device is the mmc block device, for example /dev/mmcblk0 and command is
one of the following:
statusShow current write protection setting.
unlockDisable write protection.
lockEnable temporary write protection.
permlockEnable permanent write protection (untested).
resetSend go_idle_state command to all cards.

You can download the source code of the tool here.

Note that you must be able to access the MMC device directly. Some SD card readers connect over USB and export a Mass
Storage Device (ex. /dev/sda), which cannot be used. My Dell Vostro 1310 laptop shows the card to the operating system as
a MMC device. Most embedded boards, such as the Raspberry Pi, do the same. Therefore, I have compiled a static binary
that you can use on your Raspberry Pi and most other Linux based ARM embedded systems.
You can download it here.

As a side effect, you can also use it to make the Raspberry Pi boot volume read-only as an extra precaution against corruption.
Do this only if you mount your root filesystem read-only. In the future I will publish instructions and software that can be
used to automatically make hard-shutdown proof Raspberry Pi OS images (using buildroot).

Run the following command on your Raspberry Pi to make it read-only:
wget -O /tmp/sdtool;chmod +x /tmp/sdtool;/tmp/sdtool /dev/mmcblk0 lock

The license for the program and static binary can be found here.