I'm currently working on a 32-bit architecture Xilinx SoC-FPGA board. A small C-program has to write some data into a specific physical memory region starting at the address 0x80000000
. To access the physical memory, /dev/mem
is used. The program opens the device file and sets the position indicator to the required address (on this system the smallest addressable unit of memory is one byte).
As an example, let's assume that I want to write the word 0xAB
to the memory address 0x80000000
:
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE *fp;
char word[1] = {0xAB};
unsigned long v_offset= 0x80000000;
fp = fopen("/dev/mem", "r+b");
if (fp == NULL) {
// error handling
} else {
fseek(fp, v_offset, SEEK_SET); // set position indicator
fwrite(word , sizeof(char), sizeof(word), fp);
fclose(fp);
}
return 0;
}
The program above cannot work: int fseek(FILE *stream, long offset, int whence)
(man page) expects a signed long for the offset parameter. Therefore 0x7FFFFFFF=0b01111111111111111111111111111111
is the last value for v_offset
that will not cause the parameter offset
to overflow.
Edit: The memory region starting at 0x80000000
is accessible and I can read and write data via devmem2.
User contributions licensed under CC BY-SA 3.0