Tuesday, January 24, 2012

Reading Physical Mapped Memory using /dev/mem


FILES

/dev/mem Provides access to the computer's physical memory. /dev/kmem Provides access to the virtual address space of the operating system kernel, excluding memory that is associated with an I/O device. /dev/allkmem Provides access to the virtual address space of the operating system kernel, including memory that is associated with an I/O device. 
 
http://www.linuxjournal.com/magazine/anthony-lineberry-devmem-rootkits?page=0,0 
 
http://tldp.org/LDP/khg/HyperNews/get/devices/fake.html 
 http://www.plugcomputer.org/plugforum/index.php?PHPSESSID=pelnql865kdm19vfl3bgvr14f7&topic=104.0

source :
            http://forum.kernelnewbies.org/read.php?13,2316,2316

Example : Blink the Led's from the user space through the /dev/mem

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

#define LED_ADDR 0x80840020

extern int errno;

int main()
{
        int i;
        unsigned char *leds;
        unsigned char val;

        int fd = open("/dev/mem",O_RDWR|O_SYNC);
        if(fd < 0)
        {
                printf("Can't open /dev/mem\n"winking smiley;
                return 1;
        }
        leds = (unsigned char *) mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x80840000);
        if(leds == NULL)
        {
                printf("Can't mmap\n"winking smiley;
                return 1;
        }
        else
                printf("leds=%x\n",leds);

        for(i = 0; i < 256; i++)
        {
                val = i % 4;
                leds[0x20] = val;

                sleep(1);
        }

        return 0;
}






2 comments:

  1. Hi Naveen,
    Kindly suggest me a solution, when i run a code to access /dev/mem it shows a message "can not open /dev/mem", what should i do?

    ReplyDelete