openocd: Refactor openocd.sh script#7685
Conversation
|
In particular, the default image file is changed from .hex to .elf. I don't know of any boards that do any special magic translations of the elf file when creating the hex. Flashing should work correctly for any board which have fully working GDB debugging through OpenOCD, because that means that the symbols are loading properly from the ELF file. |
|
I like this PR! It comes in the moment I'm actually working on a solution to force all (openocd-flashed) boards to flash bin (easier to distribute for updates). My current solution is: do_flash_bin() {
test_config
test_binfile
if [ -n "${PRE_FLASH_CHECK_SCRIPT}" ]; then
sh -c "${PRE_FLASH_CHECK_SCRIPT} '${HEXFILE}'"
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "pre-flash checks failed, status=$RETVAL"
exit $RETVAL
fi
fi
# flash device
sh -c "${OPENOCD} -f '${OPENOCD_CONFIG}' \
${OPENOCD_EXTRA_INIT} \
-c 'tcl_port 0' \
-c 'telnet_port 0' \
-c 'gdb_port 0' \
-c 'init' \
-c 'targets' \
-c 'reset halt' \
${OPENOCD_PRE_FLASH_CMDS} \
-c 'program \"${BINFILE}\" \"${FLASH_ADDR}\"' \
-c 'reset halt' \
${OPENOCD_PRE_VERIFY_CMDS} \
-c 'verify_image \"${BINFILE}\" \"${FLASH_ADDR}\" bin' \
-c 'reset run' \
-c 'shutdown'" &&
echo 'Done flashing'
}Which can be also merged to the HEX or ELF solutions, except for the command, which is So, in summary, I like the work but I don't see a strong reason to flash always ELF, however I do agree that we don't need to flash HEX for openocd flashing. |
|
It should be easy to refactor this in a follow up to support setting the base address that you need for bin flashing. My main motivation for this was to make it possible to run ddd automatically and to prepare for an upcoming separation of board configuration from debug interface adapter configuration, which will be useful to reduce code duplication in the repo, as well as being able to use any freestanding jtag/swd adapter for programming any board (especially useful for custom boards without integrated debug adapters) |
|
@kYc0o btw, you should not need to switch to the program command in openocd, it should be enough to put your flash base address as an extra argument after the file name to |
Ok I see, but then I need to provide also the type, which is not harmful... thanks for the hint! So, In short, can we use this PR for the default debugging interface but maybe we use bin for the flash command? Are these two necessarily together? I think flashing bin and debugging with ELF is possible and would ease other procedures like bin file distribution. |
| # Default TCL port, set to 0 to disable | ||
| : ${TCL_PORT:=6333} | ||
| # Default path to OpenOCD configuration file | ||
| : ${OPENOCD_CONFIG:=${RIOTBOARD}/${BOARD}/dist/openocd.cfg} |
There was a problem hiding this comment.
So here it shouldn't point to a generic configuration depending on the debugger?
There was a problem hiding this comment.
Oops, sorry, I think this change would come in #7686 (still I don't see it there either)
There was a problem hiding this comment.
In #7686, The config file is the configuration for the board, it should contain the CPU definitions and anything related to getting the board into a state ready to receive a new image. So basically everything except the jtag interface adapter definition, which is handled by the new $OPENOCD_IFACE_INIT (in #7686, not here)
|
I don't like the idea of using bin as the default. Bin is just a raw memory dump with no extra information. Also since it's a raw dump it doesn't support memory holes. It is way easier to mess up a flash unintentionally when you don't have any header information for the image.
In a future PR:
|
It seems perfect to me, I didn't considered having a mess in the resulting binary since the goal is to generate it exactly in the same way as the HEX are generated (using objcopy), so for me it seemed legit. However I prefer you solution since it avoids any mistake if a different binary is loaded.
I'd like to do that job. |
f21b890 to
f9eb678
Compare
|
@kYc0o |
|
Perfect, will test very soon. |
f9eb678 to
de2e59f
Compare
| # Image file used for flashing. Must be in a format that OpenOCD can handle (ELF, | ||
| # Intel hex, S19, or raw binary) | ||
| # Default is to use $ELFFILE | ||
| : ${IMAGE_FILE:=${ELFFILE}} |
There was a problem hiding this comment.
:= means optional? Can it be overridden?
There was a problem hiding this comment.
The construct : ${varname:=default_value} is a way to specify defaults for missing values. The colon operator is a no-op command in sh, and the := means that the variable will be updated with the value given if it is empty.
Another option is to write it as
varname = ${varname:default_value}
There was a problem hiding this comment.
OK, I'm not familiar with sh, so thanks a lot from the explanation! My ACK remains.
kYc0o
left a comment
There was a problem hiding this comment.
ACK. Tested on several boards with several debuggers (OpenSDA, CMSIS-DAP, FT2232, ST-Link, JLink).
|
Please squash, I think this is ready. |
|
Will rebase tomorrow |
de2e59f to
365918a
Compare
|
squashed |
- Merge flash and flash-elf commands since they were identical except
for the file name of the image
- Split GDB command from DBG environment variable to allow more easily
configure front-ends for GDB via environment variables.
- Remove verbose tests of empty variables and replace by `: ${VAR:=default}`
- Remove passed command line arguments to sub-functions, they were
unused in the functions anyway.
- Remove TUI variable, use `export DBG_EXTRA_FLAGS=-tui` to get the same
result.
Fixes problems with the watchdog interfering on Kinetis K devices when USE_OLD_OPENOCD=0.
365918a to
887ddce
Compare
|
Amended a small documentation error (ELFFILE vs. IMAGE_FILE in the docs for the flash command) |
| ${OPENOCD_PRE_FLASH_CMDS} \ | ||
| -c 'flash write_image erase \"${HEXFILE}\"' \ | ||
| -c 'reset halt' \ | ||
| -c 'flash write_image erase \"${IMAGE_FILE}\" ${IMAGE_OFFSET} ${IMAGE_TYPE}' \ |
There was a problem hiding this comment.
A last nitpick... to avoid errors, shouldn't ${IMAGE_TYPE} be deduced from the ${IMAGE_FILE} value? because then this can allow to set ELFFILE bin. Though I think is not a blocker for merging as is.
There was a problem hiding this comment.
Since we default to having IMAGE_TYPE empty, we defer image type deduction to OpenOCD by default. If a user does export IMAGE_TYPE=bin and IMAGE_FILE=myfile.elf, this is obviously going to cause a bricked board (sounds worse than it is, simply re-flashing with an empty IMAGE_TYPE will unbrick it).
In my opinion we can't protect the user from every possible configuration mistake they do, we should only make sure that the default configuration always works and that it is easy use the tool.
Can we leave any kind of image type deduction as a possible follow up and let others propose changes?
I don't know how we would do it in a good way other than using tools like file or readelf..
There was a problem hiding this comment.
Yes I agree that's maybe too much overhead to avoid this kind of errors.
|
Let's go for #7686 |
|
Thanks @kYc0o |
flashandflash-elfcommands since they were identical except for the file name of the image: ${VAR:=default}export DBG_EXTRA_FLAGS=-tuito get the same result.write_imageandverifycommands, fixes an issue with the watchdog interfering with the checksum computation on Kinetis K devices.Tested with samr21-xpro and frdm-k22f.