2

I wpuld like to write to hardware registers on OMAP3530 in Linux userspace. Please, find below code. Mmap returns error:

Memory map failed. error -1

mmap: Permission denied

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

#define BASE_ADDRESS 0x48050000

int main()
{
        int mem;
        unsigned int *value;
        void *ptr;

        /* Open /dev/mem */
        if ((mem = open ("/dev/mem", O_RDONLY | O_SYNC)) == -1)
                fprintf(stderr, "Cannot open /dev/mem\n"), exit(1);

        ptr = mmap (0, 8192, PROT_READ|PROT_WRITE, MAP_SHARED, mem, BASE_ADDRESS);

        if(ptr == (void *) -1) {
                printf("Memory map failed. error %i\n", ptr);
                perror("mmap");
                }

        value = (unsigned int*)(ptr + 0x4BC+((1-1)*0x90)+(0*0x04));
        *value = 479*799*3-4;
        printf("value = %i", value);

        value = (unsigned int*)(ptr + 0x4C4+((-1)*0x90));
        *value = 1;
        printf("value = %i", value);

        value = (unsigned int*)(ptr + 0x4C8+((-1)*0x90));
        *value = 1;
        printf("value = %i", value);
        return 0;
}
user1016711
  • 139
  • 1
  • 4
  • 12

1 Answers1

2

The documentation for mmap() says (emphasis mine):

The mmap() function shall fail if:

EACCES

The fildes argument is not open for read, regardless of the protection specified, or fildes is not open for write and PROT_WRITE was specified for a MAP_SHARED type mapping.

Since you specify PROT_WRITE, the file should be open for write. Your code only opens it for read.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479