LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2014, 03:59 AM   #1
jcsistemas2001
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
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
 
Old 12-02-2014, 11:18 AM   #2
ttk
Member
 
Registered: May 2012
Location: Sebastopol, CA
Distribution: Slackware
Posts: 469
Blog Entries: 17

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
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?
 
Old 12-02-2014, 03:54 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,669

Rep: Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439
And, for what should be obvious reason, superuser privileges will be needed to open the device.
 
Old 12-18-2014, 03:07 AM   #4
jcsistemas2001
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thumbs up

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
 
Old 12-30-2014, 11:39 AM   #5
wanhaiming
LQ Newbie
 
Registered: Dec 2014
Posts: 1

Rep: Reputation: Disabled
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 View Post
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
 
Old 01-03-2015, 02:42 AM   #6
jcsistemas2001
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
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 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - Newbie 16 09-01-2010 09:07 AM
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - General 2 08-26-2010 03:32 PM
Low level access to USB device Recursion Linux - Hardware 5 12-01-2009 09:21 PM
Serial port Low Level access.... webquinty Linux - Newbie 2 05-07-2009 09:34 AM
low level hardware access herambshembekar General 3 03-25-2002 12:12 PM


All times are GMT -5. The time now is 12:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration