How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address?
I was able to read a physical memory address (for example, 0x400) using this line:

    dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 ))

But I get an error:

dd: 'standard input': cannot skip to specified offset

when I try to write using this method

  function T_WRITE2 {
    printf $1 | dd of=/dev/mem count=4 bs=1 skip=$(( 0x400 ))
  }

Any suggestion?

Hello rabrant,

Sorry for the delayed response but I've been trying to think about this to find a good reason to do it. I would always recommend against it. It's not something I have done, and it's nothing something I would every suggest. Do you have a specific need for using a specific memory address? The OS will stop you if your process does not 'own' the location. Trying to write to /dev/mem would be extremely dangerous even if it was permitted.

Each process is allocated memory by the OS as it is required and the start location & length vary each time a process is called. If your process is swapped, then when recalled it will be extremely unlikely to be in the same physical location. Are you trying to refer to a memory location of your process or perhaps adjust some element of the hardware perhaps?

It's the sort of thing that I would say "No" many times and if you insist, I would have a very long think of any possible reason you might want to and then say "No" again. Can yoiu enlighten us to your need? There is almost certainly a better way.

Kind regards,
Robin

1 Like