I have the following udev rules to mount the first partition of the sd card to /mnt/sdcard.
KERNEL=="mmcblk0p1", SUBSYSTEMS=="mmc", ATTRS{name}=="?*", ATTRS{serial}=="?*", ENV{ID_NAME}="$attr{name}", ENV{ID_SERIAL}="$attr{serial}", SYMLINK+="sdcard", RUN+="/usr/bin/mount_sdcard"
KERNEL=="mmcblk0", ACTION=="remove", RUN+="/usr/bin/unmount_sdcard"
My /usr/bin/mount_sdcard
executable is :
#!/bin/sh
# log event
logger -t mount_sdcard -p user.warn "New SD Card detected"
# mount to /mnt/sdcard
mount_result=`mount $DEVNAME /mnt/sdcard 2>&1`
# On errors, send error to log
echo $mount_result | logger -t mount_sdcard -p user.error
if [ "x$mount_result" = "x" ]
then
# print filesystem type
stat -f /mnt/sdcard | grep Type | cut -d: -f4 | logger -t mount_sdcard -p user.warn
# print space left on device
df -h /dev/sdcard | logger -t mount_sdcard -p user.warn
fi
This code is working correctly and the partition is mounted read write (rw) when a sd card is inserted.
But if the sd card is already present at boot, the partition is mounted read only (ro).
In this case, I cannot mount the partition read write without removing and reinserting the sd card manually.
I tried to unmount and then to mount again. I tried to use the remount option: mount -o remount,rw /dev/mmcblk0p1
which seems to work but the partition is still marked as ro when running the mount
command:
/dev/mmcblk0p1 on /mnt/sdcard type ext4 (ro,relatime,data=ordered)
Update:
The problem is more precise: This is on custom hardware where the WP (write protect) pin on the ARM processor is wired to an output of the processor.
At boot, this output set the sdcard controller in read only mode and after the init this output is inverted to allow to write to the sd card. The problem is that the kernel will try to read this WP pin only at boot and when a card is inserted.
==> at boot the kernel sd card controller set the card as ro:
kernel: [ 1.723728] mmc0: new high speed SD card at address 59b4
kernel: [ 1.738262] mmcblk0: mmc0:59b4 USD 1.87 GiB (ro)
And after the WP pin changes and the card is removed/replugged, the kernel sd card controller will set the card as rw:
kernel: [ 527.931457] mmc0: new high speed SD card at address 59b4
kernel: [ 527.943988] mmcblk0: mmc0:59b4 USD 1.87 GiB
My question changes: how to force the kernel to read the WP pin again without removing the sd card ?
I was able to read the WP pin again by resetting the controller for this card with these commands:
First get the controller:
$ readlink /sys/block/mmcblk0
../devices/soc0/soc/2100000.aips-bus/2194000.usdhc/mmc_host/mmc0/mmc0:59b4/block
Then unbind and bind the card:
$ echo 2194000.usdhc > /sys/bus/platform/drivers/sdhci-esdhc-imx/unbind
$ echo 2194000.usdhc > /sys/bus/platform/drivers/sdhci-esdhc-imx/bind