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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions dist/tools/openocd/openocd.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#
# Unified OpenOCD script for RIOT
#
Expand Down Expand Up @@ -145,6 +145,43 @@ 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}"; }
}

# 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
#
Expand All @@ -159,6 +196,21 @@ do_flash() {
exit $RETVAL
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

# flash device
sh -c "${OPENOCD} \
${OPENOCD_ADAPTER_INIT} \
Expand All @@ -173,7 +225,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'
Expand Down