Re: /dev/mmcsd0 Operation not permitted

From: orbit <ordinarybit_at_proton.me>
Date: Wed, 27 Mar 2024 09:12:33 UTC
On Wednesday, 27 March 2024 at 16:52, titus <titus@edc.ro> wrote:

> sysctl kern.geom.debugflags=16
> see man 4 geom

Hi titus,

With your sysctl knob above, changing the value from default 0 to 16, the open() O_RDWR and O_WRONLY flags issues solves it.

root@sandisk:~ # sysctl kern.geom.debugflags
kern.geom.debugflags: 0
root@sandisk:~ # sysctl kern.geom.debugflags=16
kern.geom.debugflags: 0 -> 16
root@sandisk:~ # sysctl kern.geom.debugflags
kern.geom.debugflags: 16

root@sandisk:~ # ./open_device
Device file /dev/mmcsd0 opened sucessfully!

root@sandisk:~ # /usr/local/sbin/mmc gen_cmd read /dev/mmcsd0
Data:
44 53 32 31 30 34 31 35 1 1 1 1f 0 0 5 1
1 0 39 3 0 0 0 1 0 0 a9 0 1 2 1 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 53 61 6e 44 69 73 6b 20 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 43 31 66 33 30 70 20 20 20 20 20
20 3 53 44 53 41 30 38 47 80 57 67 5e f1 1 54
f5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Thanks a lot for the help!

BR,
orbit

>> On Mar 27, 2024, at 10:33 AM, orbit <ordinarybit@proton.me> wrote:
>>
>> Here's what I can see.
>>
>> root@sandisk:~ # sysctl kern.securelevel
>> kern.securelevel: -1
>>
>> What should be the value? Thanks!
>>
>> On Wednesday, 27 March 2024 at 15:50, titus <titus@edc.ro> wrote:
>>
>>> do you have kern.securelevel != -1 ?
>>>
>>>> On Mar 27, 2024, at 9:46 AM, orbit <ordinarybit@proton.me> wrote:
>>>>
>>>> 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/mmcsd0
>>>> Data:
>>>> 44 53 32 31 30 34 31 35 1 1 1 1f 0 0 5 1
>>>> 1 0 39 3 0 0 0 1 0 0 a9 0 1 2 1 2
>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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