简体   繁体   中英

SD card: how to force the kernel to read the WP pin again without removing the sdcard

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM