C programming question

Greg Franks rgfranks-cmaem7PIVQT44Nm34jS7GywD8/FfD2ys at public.gmane.org
Tue Dec 23 03:19:03 UTC 2003


>>>>> "John" == John Wildberger <wildberger-iRg7kjdsKiH3fQ9qLvQP4Q at public.gmane.org> writes:
    John> Could I ask you to expand on your two words a little bit by
    John> using the info in the man page in a concrete way, e.g show
    John> me how to access physical memory at address 0x200 to 0x205.
    John> Thanks, John

With all due respect, are you really sure that you know what you are
doing and or what you want?

You must be root to run this.
*** Writing to /dev/mem will probably crash and or corrupt your system. ***

You can use mmap too.  


/* mem.c	-- Greg Franks Mon Dec 22 2003
 *
 * $Log$
 */

#ifndef lint
static char *rcsid = "$Header$";
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

main()
{
    int fd = open( "/dev/mem", O_RDONLY );
    if ( fd < 0 ) {
	perror( "Cannot open /dev/mem: " );
	exit( 1 );
    } else {
	off_t offset = lseek( fd,  0x10, SEEK_SET );
	if ( offset == (off_t)-1 ) {
	    perror( "Seek: "  );
	    exit( 1 );
	} else {
	    long x;
	    ssize_t size = read( fd, &x, 4 );
	    if ( size == -1 ) {
		perror( "Read: " );
		exit( 1 );
	    }
	    fprintf( stderr, "Offset 10 is %x\n", x );
	}
    }
    exit ( 0 );
}
	
% sudo ./a.out 
Offset 10 is f000ef6f


For the lazy, try:

% sudo od -x -N 100 /dev/mem
0000000 0001 0000 ef6f f000 e2c3 f000 ef6f f000
0000020 ef6f f000 ff54 f000 8008 f000 ef6f f000
0000040 fea5 f000 e987 f000 ef6f f000 ef6f f000
0000060 ef6f f000 ef6f f000 ef57 f000 ef6f f000
0000100 10fa c000 f84d f000 f841 f000 aa9c f000
0000120 e739 f000 f859 f000 e82e f000 efd2 f000
0000140 e7a4 f000


-- 
   __@               Greg Franks              <|       _~@ __O 
 _`\<,_         Ottawa, Ontario, Canada        |O\   -^\<;^\<, 
(*)/ (*)                                       (*)--(*)%---/(*)
          "Where do you want to go today?"   Outside.  
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list