From 5bb9babfe0c7280fab04989919fbbb7d786d8649 Mon Sep 17 00:00:00 2001 From: cladmi Date: Mon, 3 Sep 2018 15:58:23 +0200 Subject: [PATCH 1/6] openocd.sh: update to `bash` Update to bash to have `local` variables and `=~` regex matching. Will be used in upcoming commits. --- dist/tools/openocd/openocd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index 6af7165c42c1..20fba6a45341 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Unified OpenOCD script for RIOT # From 09f0930f594ace1c6c7738b5aec358ab469407a1 Mon Sep 17 00:00:00 2001 From: cladmi Date: Wed, 29 Aug 2018 16:45:15 +0200 Subject: [PATCH 2/6] openocd.sh: add a debug message when flashing with an offset --- dist/tools/openocd/openocd.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index 20fba6a45341..4dd58c13bfff 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -159,6 +159,11 @@ do_flash() { exit $RETVAL fi fi + + if [ "${IMAGE_OFFSET}" != "0" ]; then + echo "Flashing with IMAGE_OFFSET: ${IMAGE_OFFSET}" + fi + # flash device sh -c "${OPENOCD} \ ${OPENOCD_ADAPTER_INIT} \ From 60aac4a75b86824cf2678adacf71f2ab852d71b0 Mon Sep 17 00:00:00 2001 From: cladmi Date: Thu, 16 Aug 2018 15:42:03 +0200 Subject: [PATCH 3/6] openocd.sh: fix verify_image with IMAGE_OFFSET 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. --- dist/tools/openocd/openocd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index 4dd58c13bfff..f4a0e628297b 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -178,7 +178,7 @@ do_flash() { ${OPENOCD_PRE_FLASH_CMDS} \ -c 'flash write_image erase \"${IMAGE_FILE}\" ${IMAGE_OFFSET} ${IMAGE_TYPE}' \ ${OPENOCD_PRE_VERIFY_CMDS} \ - -c 'verify_image \"${IMAGE_FILE}\"' \ + -c 'verify_image \"${IMAGE_FILE}\" ${IMAGE_OFFSET}' \ -c 'reset run' \ -c 'shutdown'" && echo 'Done flashing' From 5748d6a75faa9f2efd203a2d3646eef3e0e6d779 Mon Sep 17 00:00:00 2001 From: cladmi Date: Thu, 16 Aug 2018 15:14:39 +0200 Subject: [PATCH 4/6] openocd.sh: add a _is_binfile function 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. --- dist/tools/openocd/openocd.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index f4a0e628297b..7ea7d665699d 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -145,6 +145,20 @@ test_imagefile() { fi } +_has_bin_extension() { + # The regex need to be without quotes + local firmware=$1 + [[ "${firmware}" =~ ^.*\.bin$ ]] +} + +# Return 0 if given file should be considered a binary +_is_binfile() { + local firmware="$1" + local firmware_type="$2" + [[ "${firmware_type}" = "bin" ]] || { \ + [[ -z "${firmware_type}" ]] && _has_bin_extension "${firmware}"; } +} + # # now comes the actual actions # From 36b51467ddb3709b5d4ee13be47d64b33d8f750e Mon Sep 17 00:00:00 2001 From: cladmi Date: Thu, 16 Aug 2018 15:17:18 +0200 Subject: [PATCH 5/6] openocd.sh: add functions to access board flash address 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. --- dist/tools/openocd/openocd.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index 7ea7d665699d..94ac2695fac7 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -159,6 +159,29 @@ _is_binfile() { [[ -z "${firmware_type}" ]] && _has_bin_extension "${firmware}"; } } +# Outputs bank info on different lines without the '{}' +_flash_list() { + # Openocd output for 'flash list' is + # .... + # {name nrf51 base 0 size 0 bus_width 1 chip_width 1} {name nrf51 base 268439552 size 0 bus_width 1 chip_width 1} + # .... + sh -c "${OPENOCD} \ + ${OPENOCD_ADAPTER_INIT} \ + -f '${OPENOCD_CONFIG}' \ + -c 'flash list' \ + -c 'shutdown'" 2>&1 | sed -n '/^{.*}$/ {s/\} /\}\n/g;s/[{}]//g;p}' +} + +# Print flash address for 'bank_num' num defaults to 1 +# _flash_address [bank_num:1] +_flash_address() { + bank_num=${1:-1} + + # extract 'base' value and print as hexadecimal + # name nrf51 base 268439552 size 0 bus_width 1 chip_width 1 + _flash_list | awk "NR==${bank_num}"'{printf "0x%08x\n", $4}' +} + # # now comes the actual actions # From bc7e53f3d463e0bed68052346ab04a40004d640e Mon Sep 17 00:00:00 2001 From: cladmi Date: Thu, 16 Aug 2018 15:28:17 +0200 Subject: [PATCH 6/6] openocd.sh: include base address in IMAGE_OFFSET for binfiles 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. --- dist/tools/openocd/openocd.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index 94ac2695fac7..a4c72dd38696 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -197,6 +197,16 @@ do_flash() { fi fi + # In case of binary file, IMAGE_OFFSET should include the flash base address + # This allows flashing normal binary files without env configuration + if _is_binfile "${IMAGE_FILE}" "${IMAGE_TYPE}"; then + # hardwritten to use the first bank + FLASH_ADDR=$(_flash_address 1) + echo "Binfile detected, adding ROM base address: ${FLASH_ADDR}" + IMAGE_TYPE=bin + IMAGE_OFFSET=$(printf "0x%08x\n" "$((${IMAGE_OFFSET} + ${FLASH_ADDR}))") + fi + if [ "${IMAGE_OFFSET}" != "0" ]; then echo "Flashing with IMAGE_OFFSET: ${IMAGE_OFFSET}" fi