How to read physical memory

Hello, could you help me? I need utility or part of code which read part of physical memory in non kernel mode. I wrote this : #include #include #include #include #include

int main() { FILE* fd = fopen("/dev/mem","r"); unsigned int i; unsigned int base; char c[4]; if (fd == 0) printf("Error while opening /dev/mem\n"); base = 0x40d00010;

fseek(fd, SEEK_SET, base); for (i = 0; i < 16*4; i+=4) { fread(&c,4,1,fd); printf("addr[%2X]=0x%.2X %.2X %.2X %.2X\n",base+i,c[0],c[1],c[2],c[3]); } return 0; }

But i cannot read /dev/mem on my device because /dev/mem doesn't work (cat /dev/mem cause segmentation fault). Is there any other possibility how do these. (ioctl)

Thank you

Tomas

Reply to
Tomas Kratochvil
Loading thread data ...

You're on the right way, however you have to mmap() the memory area you want to read from user space after /dev/mem is opened.

Marco Cavallini ============================================== Koan s.a.s. - Software Engineering (x86 and ARM) Linux and WinCE solutions for Embedded and Real-Time Software Klinux : the embedded distribution for industrial applications - Microsoft Windows Embedded Partner - Intel PCA Developer Network member Via Pascoli, 3 - 24121 Bergamo - ITALIA Tel. (++39) 035-255.235 - Fax (++39) 178-223.9748

formatting link
-
formatting link
==============================================

Reply to
Marco Cavallini

Thank you for your help. I rewrite the code. This is new version.

#include #include #include #include #include #include

int main() { unsigned int base, basepage, baseoff; char* mem; int fd;

if ((fd = open("/dev/mem",O_RDWR)) == 0) { printf("Error while opening /dev/mem\n"); return -1; } printf("pagesize = %d\n",getpagesize()); base = 0x40d00010;

baseoff = base % getpagesize(); basepage = base - baseoff; printf("basepage=%.8X, baseoff = %.8X, destaddr = %.8X\n", basepage, baseoff,basepage+baseoff); if ((mem = (char*) mmap(0,getpagesize(),PROT_READ, MAP_SHARED, fd, basepage)) == (caddr_t) -1) { printf("Error while maping memory\n"); return -2; } printf("addr[%.8X]=0x%.2X 0x%.2X 0x%.2X

0x%.2X\n",basepage+baseoff+i,mem[baseoff],mem[baseoff+1],mem[baseoff+2],mem[baseoff+3]); return 0; }

On my desktop Linux (Intel Celereon) seems to works fine (get data from memory, result of last printf is "addr[40d00010]=0x$FF $FF $FF $FF"), but on target platform (ARM/Linux) instead of data from memory return last printf "bus error". No "addr[0x40d00010]=0x bla bla" only "bus error".

Is there any problem with portation of Linux on this custom device (mmap dosen't work) or code is bad or anything else???

Thanx for any help

Tomas Kratochvil

Reply to
Tomas Kratochvil

.snip.

Well done Tomas !

I should need more info about the target system and the error log to understand what is happening. However you have to veri9fy mmap() return value and ensure that the memoy location you want to read is not already mapped in $YOURLINUX/drivers/mtd/maps/...

Marco Cavallini ============================================== Koan s.a.s. - Software Engineering (x86 and ARM) Linux and WinCE solutions for Embedded and Real-Time Software Klinux : the embedded distribution for industrial applications - Microsoft Windows Embedded Partner - Intel PCA Developer Network member Via Pascoli, 3 - 24121 Bergamo - ITALIA Tel. (++39) 035-255.235 - Fax (++39) 178-223.9748

formatting link
-
formatting link
==============================================

Reply to
Marco Cavallini

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.