LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Low level access to SD card (read and write specific blocks) (https://www.linuxquestions.org/questions/programming-9/low-level-access-to-sd-card-read-and-write-specific-blocks-4175526797/)

jcsistemas2001 11-29-2014 03:59 AM

Low level access to SD card (read and write specific blocks)
 
Hello All,

I'm trying to read/write specific locations of a SD card using low level functions (without file system, cache, etc...).

I have a custom SD card that can be lock/unlock using a custom command. To activate/execute these commands, the application need to read and write in specials locations (with special data).
Assuming this, I already develop an application in Windows and Linux with success. For Linux, while I use a SD Reader, I can implement some USB Mass Storage Class commands in userspace application (to avoid the whole file system).

Until now, everything is OK. However, If the SD card is connected directly (without the USB SD reader), the same approach cannot be applied.

For example, I'm using a Wandboard (iMX6 ARM with native SD support) and I get the same results (not success).

When I try to use mmc-utils, the call ioctl get "Connection timed out".

Using the following methods doesn't work:

- open/read/write functions (c/c++)
- open/read/write functions (c/c++) with O_DIRECT flag
- open/read/write functions (c/c++) with O_DIRECT flag and flush operations

Any suggestions will be appreciated.

Thanks

ttk 12-02-2014 11:18 AM

If you are not accessing the SD device via a USB interface, then using USB Mass Storage Class commands obviously cannot work, because there is no USB device involved.

Under Linux, the raw device is available as a pseudofile, for instance /dev/mmcblk0. Run "sudo dmesg | tail -n 30" after inserting the SD device to see the exact device name.

You should be able to open, read, and write this file to access raw device blocks (of the device, not its filesystems). You can also use the "dd" command to access specific blocks from the commandline, if you just want to try it quickly without programming. In either case you will likely need to be the root user.

Do you still get errors when accessing it this way? If so, what error?

sundialsvcs 12-02-2014 03:54 PM

And, for what should be obvious reason, superuser privileges will be needed to open the device.

jcsistemas2001 12-18-2014 03:07 AM

Dear ttk, sundialsvcs,
Thanks for the replies.
I able to open, read, and write the file "/dev/mmcblk0" to access raw device blocks, however, the custom SD memory not reply as expected. I suppose there is some timeout inside the SD controller that don't allow to use these functions (because the inherent kernel control, cache, etc...).
I get working WRITE and READ commands directly to the SD using the current driver and IOCTL calls.
Thanks,
Julio

wanhaiming 12-30-2014 11:39 AM

read/write specific locations of a SD card using low level functions
 
hi,Julio:
I have the same problem about read/write specific locations of a SD card using low level functions,so could you please give me more details about how to solve it,my email whmhyf@163.com,thanks.
Quote:

Originally Posted by jcsistemas2001 (Post 5286815)
Dear ttk, sundialsvcs,
Thanks for the replies.
I able to open, read, and write the file "/dev/mmcblk0" to access raw device blocks, however, the custom SD memory not reply as expected. I suppose there is some timeout inside the SD controller that don't allow to use these functions (because the inherent kernel control, cache, etc...).
I get working WRITE and READ commands directly to the SD using the current driver and IOCTL calls.
Thanks,
Julio


jcsistemas2001 01-03-2015 02:42 AM

Nihao haiming,

Below the function to read on specific address using IOCTL function:

Code:

void read_address_ioctl(int fd, void *buffer, int address){
        int ret = 0;
        struct mmc_ioc_cmd idata;
        printf("\n low level reading at address %x. ", address);
        memset(&idata, 0, sizeof(idata));
        memset(buffer, 0, sizeof(__u8) * 512);
        idata.write_flag = 0;
        idata.opcode = MMC_READ_SINGLE_BLOCK;
        idata.arg = address;
        idata.flags = MMC_DATA_READ;
        idata.blksz = 512;
        idata.blocks = 1;
        mmc_ioc_cmd_set_data(idata, buffer);

        ret = ioctl(fd, MMC_IOC_CMD, &idata);
        if (ret)
                perror("ioctl");
        else
                printf("succcess");
}

I suggest to verify the driver code to set the FLAGS properly (idata.flags) according with your needs.

Regards

Julio

Quote:

Originally Posted by wanhaiming (Post 5292742)
hi,Julio:
I have the same problem about read/write specific locations of a SD card using low level functions,so could you please give me more details about how to solve it,my email whmhyf@163.com,thanks.



All times are GMT -5. The time now is 09:31 PM.