0

I am working on an embedded application running Debian GNU/Linux 10 (buster) and I want to have direct access (i.e. read and write) to the content available at specific memory addresses in an SD Card.

For the sake of example, suppose that the SD card is visible through the path /dev/mmcblk0p1/.

By "direct access" I mean that I do not want to read or write files to the SD Card, but instead to read/write byte values to offsets from the start memory address of the SD Card, regardless of the file system (e.g. FAT32, NFTS, etc.) to which it is defined.

First of all, are there means to do so? If this is the case, how can I do it?

At the moment, I manage to open the SD Card as a file and get its size (correct value is observed in a debuger), but I do not know how to obtain the pointer (address) to the beginning of the flash memory.

Here it goes what I have so far:

#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <unistd.h>
#include <cstdint> 

int main(){
    uint64_t size;
    int fd = open("/dev/mmcblk0p1", O_RDWR);
    ioctl(fd, BLKGETSIZE64, &size);

    int f_closed = close(fd);

    return 0;
}
tuscasp
  • 49
  • 5
  • 1
    `How to read/write to specific memory addresses` Just open the device file, seek and write. `First of all, are there means to do so?` Yes. `If this is the case, how can I do it?` [Here is a great list of C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), any one of them offers an introduction to basic input/output operations in C++. `Code snippets are welcomed.` If you want someone to do something for you, see a freelancing site where you offer money for others work. What research did you do? What have you tried? Where exactly are you stuck? – KamilCuk May 12 '21 at 14:17
  • I have updated the question to say exactly what I am missing at the moment, besides describing my current status on the research. – tuscasp May 12 '21 at 14:56
  • 1
    Note that this has nothing to do with C++ but everything with the operating system (in your case, Linux). – Peter - Reinstate Monica May 12 '21 at 14:58
  • `I do not know how to obtain the pointer (address) to the beginning of the flash memory.` `fd` is your "pointer". Writing to `fd` writes to specific physical position. Research [file position indiicator](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseek.html) in the context of file descriptors. But in C++ you would use C++ - no need to use POSIX API. – KamilCuk May 12 '21 at 14:59
  • `p1` is a pointer to a partition. SD card itself never may have such a device node. – 0andriy May 12 '21 at 16:18
  • On top of that, SD card is a microcomputer, which won’t allow you to do what you want (there are hidden debug modes, but it’s completely different story). – 0andriy May 12 '21 at 16:20
  • SD card does not have *"memory addresses"*. It's a block device, and the blocks (aka sectors) are addressed by Logical Block Address, aka LBA. If you use an OS (& virtual memory) capability such as memory mapping that provides byte access/addressability, you should still be cognizant that it is a block device. – sawdust May 12 '21 at 21:20

1 Answers1

3

You can use mmap:

#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <unistd.h>
#include <cstdint>
#include <sys/mman.h>

int main(){
    uint64_t size;
    int fd = open("/dev/mmcblk0p1", O_RDWR);
    ioctl(fd, BLKGETSIZE64, &size);

    void* data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (data == MAP_FAILED)
        return 1;
    ((char*)data)[0x2137] = 42;

    int f_closed = close(fd);

    return 0;
}

You can find more info in man mmap.

enedil
  • 1,605
  • 4
  • 18
  • 34
  • Basically read my all comments against the posting. @KamilCuk, you too. – 0andriy May 12 '21 at 16:21
  • @0andriy isn't it the same with hard drive? You can open device /dev/sda, write something there. This data still passes through kernel driver and later through drive controller. Does it matter that there is some abstraction in between? (unless SD cards meaningfully differ from hard drives in that matter). – enedil May 12 '21 at 17:46
  • Yes it’s the same with hard drivers. – 0andriy May 12 '21 at 20:47