stlink icon indicating copy to clipboard operation
stlink copied to clipboard

[feature] reading and writing flash address with CLI

Open Hoel opened this issue 5 years ago • 11 comments

Hello, is there a way to read and write some flash addresses directly with the CLI without having to flash or read a whole binary file? I want to check few addresses on the flash and also overwrite few others, my application uses the binary directly (with CLI) as it is a javafx application. Thank you.

Hoel avatar Mar 14 '20 13:03 Hoel

Please describe more precisely what you intend to do. What system and hardware do you use?

Nightwalker-87 avatar Mar 14 '20 15:03 Nightwalker-87

I use STLink V2-1, on STM32F1xx device in this case. What i want to do is to write and read few bytes at different addresses of the flash, without having to read the whole firmware and write it back. The purpose of this is to update some data in place, without having to read or write the whole thing.

Hoel avatar Mar 14 '20 15:03 Hoel

Is there anybody who is able to contribute on this issue?

Nightwalker-87 avatar Mar 20 '20 14:03 Nightwalker-87

Some facts may need a stress.

  1. The internal flash is erased once a page(page size ranging from 1KB to at least 4KB). Any non-0xff bytes cannot be rewritten reliably. For F1XX, if I got it right, even 0xff bytes cannot be rewritten without an erase. That is, writing some random bytes causes the whole page to be erased.
  2. Considering this feature, a partial update may not reduce erase cycles without intentionally design that places all variable data inside a limited number of pages.
  3. For an intentional design, manually specifying the start address and using a partial binary file is as effective as stlink on erase cycles and would reduce a read cycle that is a must if the diff process is done by stlink.

chenguokai avatar Apr 12 '20 08:04 chenguokai

What is the current state of this?

Nightwalker-87 avatar Jun 24 '20 22:06 Nightwalker-87

hello, Regarding this issue i did us a workaround, a base firmware is stored in the application, some chunks are copied and patched when the target is programmed, st-flash is then used to flash these small chunks on the target. This is not as elegant as it could be but it is fast since the chunks are small and there is no need to read the entire firmware from each target, so for this specific use case it is fine.

Hoel avatar Jun 25 '20 08:06 Hoel

@Hoel This could be helpful indeed. Can you submit a PR so we can have a look at the implementation?

Nightwalker-87 avatar Jun 28 '20 00:06 Nightwalker-87

I think this could be very useful, at least on ST's command line tool for Windows I'm using this for a couple of years now. Example using this feature:

C:\> ST-LINK_CLI.EXE -c UR SWD freq=4000 -w32 0x080FF800 0x131D3FFC

For st-flash it could look as following:

$ st-flash write <variable-type> <variable> <address>

Example:

$ st-flash write -w32 0x131D3FFC 0x080FF800

Edit: to partially solve this problem, on linux I crafted a small binary file by hand and flashed that. Example:

$ echo -n $'\x5E' > secrets.bin
$ echo -n $'\x6F' >> secrets.bin
$ echo -n $'\xB8' >> secrets.bin
$ echo -n $'\x13' >> secrets.bin
$ st-flash write secrets.bin 0x080FF800

note: that you can't flash '0' value bytes this way

geoffrey-vl avatar Mar 26 '21 13:03 geoffrey-vl

@geoffrey-vl Can you contribute on this topic regarding an implementation?

Nightwalker-87 avatar May 21 '21 13:05 Nightwalker-87

To be honest I have a couple of other project I'm also working on, so don't count on it. But for starters I'd like to discuss how the command would look like. Can it be as described in https://github.com/stlink-org/stlink/issues/875#issuecomment-808199643 ?

geoffrey-vl avatar May 21 '21 13:05 geoffrey-vl

I'd consider this a useful feature. Who can help out on this topic? Does anybody have an idea how to implement this?

Nightwalker-87 avatar Aug 31 '22 19:08 Nightwalker-87

Closing this feature request due to inactivity.

Nightwalker-87 avatar Sep 04 '23 22:09 Nightwalker-87