/dev/mmcsd0 Operation not permitted

From: orbit <ordinarybit_at_proton.me>
Date: Wed, 27 Mar 2024 07:46:50 UTC
Hi,

I'm currently running FreeBSD 14.0-RELEASE in Raspberry Pi 3 installed in a SanDisk 32GB microSD card (/dev/mmcsd0) and I've observed that it gets an error of "Operation not permitted" when using O_RDWR or O_WRONLY flags in the open() function which I use it in my little C program (open_device.c).

root@sandisk:~ # cat open_device.c
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
int error = 0; char *devfile = "/dev/mmcsd0";
int fd = open(devfile, O_RDWR);

if (fd == -1) {
printf("Error opening %s device file in read/write!\n", devfile);
err(error, NULL, NULL);
exit(1);
} else {
printf("Device file %s opened sucessfully!\n", devfile);
}
return 0;
}

Here's the error.

root@sandisk:~ # ./open_device
Error opening /dev/mmcsd0 device file in read/write!
open_device2: Operation not permitted

With O_RDONLY flag there's no problem.

I also installed mmc-utils (https://www.freshports.org/sysutils/mmc-utils/) and the same error encountered.

root@sandisk:~ # /usr/local/sbin/mmc gen_cmd read /dev/mmcsd0
device open failed: Operation not permitted

I downloaded mmc-utils source code and compile .

root@sandisk:~/mmc-utils-master # ./mmc gen_cmd read /dev/mmcsd0
device open failed: Operation not permitted

Digging up the mmc_cmds.c source in do_general_cmd_read() routine, having the flag O_RDWR in the line dev_fd = open(device, O_RDWR) will get the "device open failed: Operation not permitted" error.

int do_general_cmd_read(int nargs, char **argv)
{
int dev_fd;
char *device;
char *endptr;
__u8 buf[512];
__u32 arg = 0x01;
int ret = -EINVAL, i;
struct mmc_ioc_cmd idata;

if (nargs != 2 && nargs != 3) {
fprintf(stderr, "Usage: gen_cmd read </path/to/mmcblkX> [arg]\n");
exit(1);
}

device = argv[1];
dev_fd = open(device, O_RDWR);
if (dev_fd < 0) {
perror("device open failed");
exit(1);
} ...
}

But changing it to dev_fd = open(device, O_RDONLY) is successful as you can see below.

root@sandisk:~/mmc-utils-master # ./mmc gen_cmd read /dev/mmcs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