Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openocd.sh: allow flashing binary files without configuration #9787

Merged
merged 6 commits into from
Sep 4, 2018

Conversation

cladmi
Copy link
Contributor

@cladmi cladmi commented Aug 16, 2018

Contribution description

This adds support for flashing binary files using openocd and not configuring anything.

When flashing binaries, openocd needs the absolute address for flashing.
There was already IMAGE_OFFSET but it required configuration for flashing by default for boards that do not start at 0x0, like iotlab-m3.

The rom base address is now queried from openocd and IMAGE_OFFSET is added to that.

  • API change to IMAGE_OFFSET with binary files as it should now be an offset to the base address
    • This makes it more compatible with edbg behavior too, which as an --offset variable that takes into account the base address.
  • FIX verify image that is not using IMAGE_OFFSET when it should
  • Use openocd flash list to extract the first bank address
    • I did not make this bank number configurable for now. It could be addressed later if necessary.

API change

This is an API change but would still consider it minor as it only affects openocd when flashing binary files and was also not working as IMAGE_OFFSET was not set for verify_image. So changing an API of something that did not work.

Testing

I tested with iotlab-m3 but using any board using openocd should do. Even better if the board start address is not zero, and if it has with multiple rom banks.

For easier eye debugging, openocd can be run with sh -xc instead of sh -c here:

sh -c "${OPENOCD} \

Flash an elf with offset

make -C examples/hello-world BOARD=iotlab-m3 flash IMAGE_OFFSET=0x1000
# With debug output there is the correct address
-c 'flash write_image erase "/RIOT/examples/hello-world/bin/iotlab-m3/hello-world.elf" 0x1000 '

# In master it generated
# Error: checksum mismatch - attempting binary compare
# diff 0 address 0x08000000. Was 0x7f instead of 0x00
# diff 1 address 0x08000001. Was 0x45 instead of 0x02
# diff 2 address 0x08000002. Was 0x4c instead of 0x00

Flash a simple binary without configuration

make -C examples/hello-world BOARD=iotlab-m3 IMAGE_FILE=bin/iotlab-m3/hello-world.bin binfile flash-only
# In master it generated
# Warn : no flash bank found for address 0            
# wrote 0 bytes from file bin/iotlab-m3/hello-world.bin in 0.001894s (0.000 KiB/s)

Flash a binary with offset

make -C examples/hello-world BOARD=iotlab-m3 IMAGE_FILE=bin/iotlab-m3/hello-world.bin binfile flash-only IMAGE_OFFSET=0x100
# With debug output there is the correct address
-c 'flash write_image erase "bin/iotlab-m3/hello-world.bin" 0x8000100 '

Issues/PRs references

This is part of the OTA implementation required features in #9342

@cladmi cladmi added Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Area: tools Area: Supplementary tools labels Aug 16, 2018
@cladmi cladmi requested review from kYc0o, jnohlgard and aabadie and removed request for kYc0o August 16, 2018 14:13
@cladmi cladmi added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Aug 16, 2018
@cladmi cladmi mentioned this pull request Aug 16, 2018
@kYc0o kYc0o self-assigned this Aug 17, 2018
# This allows flashing normal binary files without env configuration
if _is_binfile ${IMAGE_FILE}; then
# hardwritten to use the first bank
FLASH_ADDR=$(_flash_address 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you call bank here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the term from openocd documentation: http://www.openocd.org/doc/html/Flash-Commands.html and here http://repo.or.cz/openocd.git/blob_plain/HEAD:/tcl/target/stm32f1x.cfg (flash bank).

From what I understood, it is a contiguous area of ROM.

By setting a 1 here, I assume that, if the board has multiple memory banks, the first one is used.

@kYc0o
Copy link
Contributor

kYc0o commented Aug 21, 2018

I reviewed the code and it makes sense to me. However I want to test it... Would it be possible to add a flash-bin target to use it? This means that we need systematically a bin file generated, but it can come as a dependency of the flash-bin target, so it's only generated when this recipe is used.

@cladmi
Copy link
Contributor Author

cladmi commented Aug 21, 2018

To have a generic flash-bin it would require #8838
I could have a target for openocd but it can also be tested without.

You can test with the examples in the PR description:

make -C examples/hello-world BOARD=iotlab-m3 IMAGE_FILE=bin/iotlab-m3/hello-world.bin binfile flash-only
make -C examples/hello-world BOARD=iotlab-m3 IMAGE_FILE=bin/iotlab-m3/hello-world.bin binfile flash-only IMAGE_OFFSET=0x100

I am also thinking about adding a test for a two step flashing but would currently require specific handling for edbg and openocd.

@kYc0o
Copy link
Contributor

kYc0o commented Aug 24, 2018

I've tested it successfully with your instructions, thanks!

However, I find that it would be useful to warn while flashing that openocd is writing with an offset, something like writing file_name with offset 0x100 for instance.

The file name is also different from what is today on master. It gives the full path while flashbin gives only the relative path. Can we normalise this into showing the full path?

@cladmi
Copy link
Contributor Author

cladmi commented Aug 24, 2018

It is because I gave a relative file in the command line. This gives the full path:

make -C examples/hello-world BOARD=iotlab-m3 IMAGE_FILE=${PWD}/bin/iotlab-m3/hello-world.bin binfile flash-only

I did not added the offset flashing, it was already in master.

# This is an optional offset to the base address that can be used to flash an
# image in a different location than it is linked at. This feature can be useful
# when flashing images for firmware swapping/remapping boot loaders.
# Default offset is 0, meaning the image will be flashed at the address that it
# was linked at.
: ${IMAGE_OFFSET:=0}

I only fixed that the 'verify' was not using the offset.

What I did, is that now if you provide a binary file, I add the ROM_BASE_ADDR.
I could add an info for this.

@kYc0o
Copy link
Contributor

kYc0o commented Aug 26, 2018

it is because I gave a relative file in the command line. This gives the full path:

That's what I meant with adding a new target flash-bin which depends on binfile to generate the file and it can be passed directly with its full path. Maybe that's easier isn't it?

I did not added the offset flashing, it was already in master.

That was not my question, sorry for not being clear. What I mean that I'd like to know if the flash script is flashing using an offset printing in the script something like Flashing at address 0x... which can only happen when flashing binfiles.

What I did, is that now if you provide a binary file, I add the ROM_BASE_ADDR.
I could add an info for this.

I think by printing the direct flashing address this is included.

Copy link
Contributor

@kYc0o kYc0o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the changes I'm asking I think it's better to wait for FLASHFILE so everything would fit better.

ACK.

@kYc0o
Copy link
Contributor

kYc0o commented Aug 28, 2018

@cladmi ok to merge?

@cladmi
Copy link
Contributor Author

cladmi commented Aug 28, 2018

There is not conflicts so I would say yes.

@cladmi
Copy link
Contributor Author

cladmi commented Aug 28, 2018

The test for a binary file is wrong. I tried flashing a text based binary so was not recognized as binary.

I could replace it by testing the extension and an env variable to force it. Any preference on this ?

After patching it, I tested in tests/periph_flashpage:

make BOARD=iotlab-m3 IMAGE_FILE=${PWD}/bin/iotlab-m3/tests_periph_flashpage.bin binfile flash-only
make BOARD=iotlab-m3 IMAGE_FILE=hello_riot.bin IMAGE_OFFSET=0x7f800 flash-only  QUIET=0
make BOARD=iotlab-m3 term
dump 255
> dump 255                                                                                                                                                                                                        
2018-08-28 18:25:22,259 - INFO #  dump 255                                                                                                                                                                        
2018-08-28 18:25:22,259 - INFO # Flash page 255 at address 0x807f800                                                                                                                                              
2018-08-28 18:25:22,260 - INFO # 0x48 0x65 0x6c 0x6c 0x6f 0x20 0x57 0x6f 0x72 0x6c 0x64 0x20 0x52 0x49 0x4f 0x54                                                                                                  
2018-08-28 18:25:22,261 - INFO #   H    e    l    l    o         W    o    r    l    d         R    I    O    T                                                                                                   
2018-08-28 18:25:22,262 - INFO # 0x21 0x21 0x21 0x21 0x0a 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff                                                                                                  
2018-08-28 18:25:22,262 - INFO #   !    !    !    !    ??   ??   ??   ??   ??   ??   ??   ??   ??   ??   ??   ??                                                                                                  
2018-08-28 18:25:22,263 - INFO # 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 

@cladmi
Copy link
Contributor Author

cladmi commented Aug 28, 2018

I cannot block my own PR…

Copy link
Contributor

@kYc0o kYc0o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot block my own PR…

UnACK.

@cladmi
Copy link
Contributor Author

cladmi commented Aug 29, 2018

I added debug output for IMAGE_OFFSET and the FLASH_ADDR used for binary files.

I changed the test to check the file extension and also use IMAGE_TYPE existing variable to force it.

IMAGE_TYPE:

  • Not a binfile
    • IMAGE_FILE=abc.bin IMAGE_TYPE=elf
    • IMAGE_FILE=abc.elf
  • binfile:
    • IMAGE_FILE=abc.bin
    • IMAGE_FILE=abc IMAGE_TYPE=bin

The periph_flashpage test also works.

@cladmi
Copy link
Contributor Author

cladmi commented Sep 3, 2018

@kYc0o for your expr not found problem mentioned IRL, I will change the script to use bash #! /bin/bash as a pre-dependency and rebase on it.
I will then update my commits accordingly with fixups too as I currently limited myself to /bin/sh features.

@cladmi cladmi force-pushed the pr/openocd/flashbin branch from 0355c24 to 1f32986 Compare September 3, 2018 14:02
@cladmi
Copy link
Contributor Author

cladmi commented Sep 3, 2018

I redid the periph_flashpage test and the binfile detection:

touch hello
touch hello.bin
touch hello_notabin

# Binfile detected
make flash-only BOARD=iotlab-m3 IMAGE_FILE=hello IMAGE_TYPE=bin 2>&1 | grep -m 1 'Binfile detected'
# Binfile detected, adding ROM base address: 0x08000000
make flash-only BOARD=iotlab-m3 IMAGE_FILE=hello.bin 2>&1 | grep -m 1 'Binfile detected'
# Binfile detected, adding ROM base address: 0x08000000

# Not binfile
make flash-only BOARD=iotlab-m3 IMAGE_FILE=hello 2>&1 | grep -m 1 'Binfile detected'
make flash-only BOARD=iotlab-m3 IMAGE_FILE=hello_notabin 2>&1 | grep -m 1 'Binfile detected'
make flash-only BOARD=iotlab-m3 IMAGE_FILE=hello.bin IMAGE_TYPE=elf 2>&1 | grep -m 1 'Binfile detected'

@kYc0o can you retry with mac ?

@kYc0o
Copy link
Contributor

kYc0o commented Sep 3, 2018

Tested with success on a nucleo-l152re on OSX. Squash and re-ACK.

Update to bash to have `local` variables and `=~` regex matching.
Will be used in upcoming commits.
When flashing with an IMAGE_OFFSET, it should also be passed to
verify_image. It is handling the base address in the image too.

This works with both elf files and binaries with the base address added.
Returns 0 if it is true.

The test is based on the file extension, but also use the IMAGE_TYPE variable
to force setting to binary.
This allows getting the ROM base address.

It may not be available in the build system directly so better extract it from
openocd. Also openocd is board specific and this address is cpu specific
so would have definition order issue in the build system.
Add the rom base address to the flash address when flashing binaries.
This allows flashing binaries with the default openocd configuration.

It is an API change to IMAGE_OFFSET with binary files as it should now
only be an offset to the base address.

Force openocd type to '.bin' in case we want to flash hex/elf objects or
files not automatically recognized as bin.
@cladmi cladmi force-pushed the pr/openocd/flashbin branch from 7272d8f to bc7e53f Compare September 4, 2018 11:54
@cladmi
Copy link
Contributor Author

cladmi commented Sep 4, 2018

Rebased with fix included to print as %08x to be coherent with flash_addr.

diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh
index 04faaaa67..a4c72dd38 100755
--- a/dist/tools/openocd/openocd.sh
+++ b/dist/tools/openocd/openocd.sh
@@ -204,7 +204,7 @@ do_flash() {
         FLASH_ADDR=$(_flash_address 1)
         echo "Binfile detected, adding ROM base address: ${FLASH_ADDR}"
         IMAGE_TYPE=bin
-        IMAGE_OFFSET=$(printf "0x%x\n" "$((${IMAGE_OFFSET} + ${FLASH_ADDR}))")
+        IMAGE_OFFSET=$(printf "0x%08x\n" "$((${IMAGE_OFFSET} + ${FLASH_ADDR}))")
     fi

     if [ "${IMAGE_OFFSET}" != "0" ]; then

@kYc0o
Copy link
Contributor

kYc0o commented Sep 4, 2018

That's ok, just a beautifier. Go!

@kYc0o kYc0o merged commit 7a40aae into RIOT-OS:master Sep 4, 2018
@cladmi cladmi deleted the pr/openocd/flashbin branch September 5, 2018 10:24
@cladmi cladmi added this to the Release 2018.10 milestone Nov 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants