3

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 ?

leszek.hanusz
  • 4,418
  • 2
  • 34
  • 53
  • Have you tried, mount_result=`mount $DEVNAME /mnt/sdcard -o rw 2>&1` – Rolf of Saxony Mar 18 '16 at 09:43
  • try unloading then reloading the relevant modules, your controller might get "re-detected" and reinitialized. However it seems that you have found a solution already. – Rolf Apr 18 '18 at 23:27

2 Answers2

2

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
leszek.hanusz
  • 4,418
  • 2
  • 34
  • 53
1

A few things I would try:

  • run fsck /dev/mmcblk0p1. The failure to mount a partition in rw mode is often a sign of unclean filesystem state. It's probably not the case here, since you can mount it correclty by replugging the card, but better be safe.

  • try to mount /dev/mmcblk0p1 via /etc/fstab to see if that works. I understand it won't be a definitive solution, but at least you'd isolate issues with mount from issues with udev.

  • compare dmesg output concerning the SD card at boot time with dmesg output when you replug the card afterwards. It seems that the controller fails initialize properly at boot time, but the issue disappears later on. Finding out what that intermittent issue is might solve it for you.

Dmitry Grigoryev
  • 2,826
  • 1
  • 22
  • 51
  • 1
    Thanks, the fsck and fstab points did not work. But the problem is found: the WP pin changes after boot. Now I need to know how to force the reload of the sdcard by the kernel without unplugging the sd card. – leszek.hanusz Mar 21 '16 at 16:22
  • @leszek.hanusz Hey, I'm glad you solved your issue. And thanks for sharing your solution too. Hopefully the upvotes will refund your bounty soon enough! – Dmitry Grigoryev Mar 21 '16 at 16:52