Read a specific memory address via /dev/mem from the command line
Asked Answered
S

2

10

For context, programming a driver to interact with an FPGA IP core on an embedded Linux (Yocto: krogoth) on a Xilinx board.

For debugging purposes I would like to read out specific memory addresses from physical memory. /dev/mem looks promising. I wanted to ask how I can read out the value of a specific physical memory address from the command line. I was hoping for something along the lines of cat /dev/mem 0x2000000 to read the byte at 0x2000000.

Stilliform answered 8/12, 2017 at 13:11 Comment(3)
Search the web for the source code of J.D.Bakker's devmem2 utility, and build it. It might even already be built in your Yocto RFS.Shippen
Thanks for pointing me to devmem2. Found it as a recipe layers.openembedded.org/layerindex/recipe/1069. So I'd just add that to my local.conf file and compile yocto againStilliform
@Shippen What would be the correct procedure to adding my own comprehensive answer if the provided ones only partially cover my use case?Stilliform
M
14

Usually you should already have devmem tool installed in your Linux image:

$ devmem 0x2000000

If you don't however, you can go to Busybox menu and tweak it to make sure it gets compiled and installed:

$ bitbake busybox -c menuconfig

(search for devmem)

Multifoliate answered 9/12, 2017 at 18:59 Comment(1)
At the time of posting the question I did not distinguish the types of memory, physical, io and registers. I posted a more comprehensive question + answer here that answers my use caseStilliform
B
10

Hexdump is often installed in embedded systems. Then you can do

hexdump -C --skip 0x2000000 /dev/mem | head

in order to read more than a single word, and see it decoded in various ways. (The busybox hexdump is a little more limited, but still very useful.)

Between answered 13/12, 2018 at 14:16 Comment(6)
Note that busybox hexdump may not support --skip, but has -s instead.Electrum
AFAIK, with hexdump we can only read flash memory. It cannot read I/O peripheral registers.Peerage
@Peerage Hexdump can only read files. To it, /dev/mem looks like a file. The beauty (or one of them) of unix-like operating systems is that most things can be represented as files. If you have something (anything, really), that is presented by the OS as a file, hexdump will be able to read it.Between
Neither devmem nor hexdump installed on the Red Pitaya Ubuntu 16 embedded Linux I am using. Tried dd piping to xxd but could not get it to skip to 0x4020_0000 where my registers are located. xxd /dev/mem could have worked if I had the patience to wait 1/2 day for it to scroll to 0x4020_0000Ideograph
I get the error hexdump: /dev/mem: Operation not permitted, even when run in a root shell. Is there some kernel setting that disables access to /dev/mem, just like SysRq combos can be disabled?Consideration
@Consideration > hexdump: /dev/mem: Operation not permitted -- Yes - apparently you can set CONFIG_STRICT_DEVMEM to restrict root access to /dev/mem. (There's apparently an IO_STRICT_DEVMEM, which is independent. If that's set to 'n', you should have access to PCI etc, but I haven't tried it.)Between

© 2022 - 2024 — McMap. All rights reserved.