2

I'm working on an embedded system project using altera cyclone 5 fpga-soc. On the real processor (ARM) I will use Linux and the script for FPGA communication will be implemented in Python 2.

I need a memory map to send and receive data to and from FPGA via /dev/mem. To develop the script i use a Lubuntu system on a virtual machine. Below is my code tested in PyCharm on Lubuntu VM:

def open(self):
    file = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
    self.map = mmap.mmap(file,
                         self.CONST_mapLength,
                         mmap.MAP_SHARED,
                         prot = mmap.PROT_READ | mmap.PROT_WRITE,
                         offset = self.CONST_offset)
    return

My problem is that i cant open \dev\mem. The script ended with this text:

file = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
OSError: [Errno 13] Permission denied: '/dev/mem'
Process finished with exit code 1

What is the problem and how do I fix it?

jww
  • 97,681
  • 90
  • 411
  • 885
Semaj
  • 21
  • 1
  • 1
  • 2
  • 1
    you have to be `root`. – Daniel Jul 20 '17 at 18:30
  • have you the right permission ? check it by opening or modifying a file manually, and then change this with chmod if necessary – PRMoureu Jul 20 '17 at 18:30
  • 2
    You shouldn't be changing permissions or ownership on anything in /dev as they will be kernel controlled. – Raman Sailopal Jul 20 '17 at 18:32
  • 2
    @PRMoureu that's bad advice, `/dev/mem` is not a normal file – Ofer Sadan Jul 20 '17 at 18:33
  • @OferSadan ok thanks, my bad...i let the comment as a not-to-do. – PRMoureu Jul 20 '17 at 18:37
  • Also see [/dev/mem access denied on raspberry pi](https://stackoverflow.com/q/24746412/608639), [No access to /dev/mem. Try running as root](https://stackoverflow.com/q/31084856/608639), [RuntimeError: No access to /dev/mem](https://stackoverflow.com/q/18389013/608639), [No access to /dev/mem. Try running as root on Raspberry Pi](https://stackoverflow.com/q/27864804/608639), etc – jww Jul 20 '17 at 19:59

2 Answers2

3

From my view,you do not have the right to access the file /dev/mem.

So before you open it, you have to get the right.

There are four ways to get it.

  1. Run your script as root, but it is dangerous, for the root can do anything in the system.

  2. Using sudo to run you application, such as sudo your_python.py.

  3. You could run this command sudo chmod 777 /dev/men, open the right to all users.

  4. Add you to the group kmem who own /dev/mem, with the command sudo usermod -g kmem yourID

whalemare
  • 1,107
  • 1
  • 13
  • 30
Forward
  • 855
  • 7
  • 12
0

I would be extremely careful with editing or even opening /dev/mem especially since you don't seem to be closing it... at all (by using close())

But anyway, if you want permission in linux to access anything in dev you should be the root user or a user with sudo privileges, so instead of accessing your program with:

python whatever.py

you should:

sudo python whatever.py

Assuming that your user is allowed to do that. If not, that means you really are a user without enough permissions on someone's system, and you should ask you system administrator for help

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62