diff --git a/boards/common/frdm/Makefile.include b/boards/common/frdm/Makefile.include index b12569e9c4..873e931acf 100644 --- a/boards/common/frdm/Makefile.include +++ b/boards/common/frdm/Makefile.include @@ -32,7 +32,7 @@ endif export OPENOCD_CONFIG ?= $(RIOTBOARD)/common/frdm/dist/openocd-$(CPU_FAMILY).cfg # Check the flash configuration field before flashing -export PRE_FLASH_CHECK_SCRIPT = $(RIOTCPU)/$(CPU)/dist/check-fcfield-elf.sh +export PRE_FLASH_CHECK_SCRIPT = $(RIOTCPU)/$(CPU)/dist/check-fcfield.sh # setup serial terminal include $(RIOTMAKE)/tools/serial.inc.mk diff --git a/boards/mulle/Makefile.include b/boards/mulle/Makefile.include index d0c6e24957..276f2fc3c8 100644 --- a/boards/mulle/Makefile.include +++ b/boards/mulle/Makefile.include @@ -56,7 +56,7 @@ endif export OPENOCD_PRE_VERIFY_CMDS += \ -c 'load_image $(RIOTCPU)/$(CPU)/dist/wdog-disable.bin 0x20000000 bin' \ -c 'resume 0x20000000' -export PRE_FLASH_CHECK_SCRIPT = $(RIOTCPU)/$(CPU)/dist/check-fcfield-elf.sh +export PRE_FLASH_CHECK_SCRIPT = $(RIOTCPU)/$(CPU)/dist/check-fcfield.sh FLASHDEPS += $(RIOTCPU)/$(CPU)/dist/wdog-disable.bin diff --git a/boards/pba-d-01-kw2x/Makefile.include b/boards/pba-d-01-kw2x/Makefile.include index 6d9fd09d3a..99cfb993fa 100644 --- a/boards/pba-d-01-kw2x/Makefile.include +++ b/boards/pba-d-01-kw2x/Makefile.include @@ -20,7 +20,7 @@ export OPENOCD_PRE_VERIFY_CMDS += \ -c 'load_image $(RIOTCPU)/$(CPU)/dist/wdog-disable.bin 0x20000000 bin' \ -c 'resume 0x20000000' export OPENOCD_EXTRA_INIT -export PRE_FLASH_CHECK_SCRIPT = $(RIOTCPU)/$(CPU)/dist/check-fcfield-elf.sh +export PRE_FLASH_CHECK_SCRIPT = $(RIOTCPU)/$(CPU)/dist/check-fcfield.sh export DEBUG_ADAPTER ?= dap diff --git a/cpu/kinetis/dist/check-fcfield-elf.sh b/cpu/kinetis/dist/check-fcfield-elf.sh deleted file mode 100755 index 6afa1dcf7e..0000000000 --- a/cpu/kinetis/dist/check-fcfield-elf.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# -# Anti brick check script for Freescale Kinetis MCUs. -# -# This script is supposed to be called from RIOTs -# unified OpenOCD script (dist/tools/openocd/openocd.sh). -# -# @author Jonas Remmert -# @author Johann Fischer - -ELFFILE=$1 -ACTUAL_FCFIELD=$(arm-none-eabi-objdump -j.fcfield -s "${ELFFILE}" | awk '/^ 0400 / {print $2 $3 $4 $5}') -# Allow any FOPT flags configuration (".." in the pattern) -EXPECTED_FCFIELD="^fffffffffffffffffffffffffe..ffff$" - -if ! printf '%s' "${ACTUAL_FCFIELD}" | grep -q "${EXPECTED_FCFIELD}" ; then - echo "Danger of bricking the device during flash!" - echo "Flash configuration field of ${ELFFILE}:" - arm-none-eabi-objdump -j.fcfield -s "${ELFFILE}" - echo "Abort flash procedure!" - exit 1 -fi -echo "${ELFFILE} is fine." -exit 0 diff --git a/cpu/kinetis/dist/check-fcfield-hex.sh b/cpu/kinetis/dist/check-fcfield-hex.sh deleted file mode 100755 index 4b2a0a27d5..0000000000 --- a/cpu/kinetis/dist/check-fcfield-hex.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/sh -# -# Flash configuration field check script for Freescale Kinetis MCUs. -# -# This script is supposed to be called from RIOTs -# unified OpenOCD script (dist/tools/openocd/openocd.sh). -# -# Syntax: check-fcfield-hex.sh $HEXFILE -# -# @author Jonas Remmert -# @author Johann Fischer -# @author Joakim Nohlgård - -if [ $# -ne 1 ]; then - echo "Usage: $0 HEXFILE" - echo "Checks the flash configuration field protection bits to avoid flashing a locked image to a Kinetis MCU (protection against accidental bricking)." - exit 2 -fi - -HEXFILE="$1" -FCFIELD_START='0x400' -FCFIELD_END='0x410' -FCFIELD_AWK_REGEX='^ 0400 ' - -ACTUAL_FCFIELD=$(arm-none-eabi-objdump --start-address=${FCFIELD_START} --stop-address=${FCFIELD_END} ${HEXFILE} -s | awk -F' ' "/${FCFIELD_AWK_REGEX}/ { print \$2 \$3 \$4 \$5; }") -# Allow any FOPT flags configuration (".." in the pattern) -EXPECTED_FCFIELD="^fffffffffffffffffffffffffe..ffff$" - -if ! printf '%s' "${ACTUAL_FCFIELD}" | grep -q "${EXPECTED_FCFIELD}"; then - echo "Danger of bricking the device during flash!" - echo "Flash configuration field of ${HEXFILE}:" - arm-none-eabi-objdump --start-address=${FCFIELD_START} --stop-address=${FCFIELD_END} ${HEXFILE} -s - echo "Abort flash procedure!" - exit 1 -fi -echo "${HEXFILE} is not locked." -exit 0 diff --git a/cpu/kinetis/dist/check-fcfield.sh b/cpu/kinetis/dist/check-fcfield.sh new file mode 100755 index 0000000000..eae3f51b76 --- /dev/null +++ b/cpu/kinetis/dist/check-fcfield.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# +# Flash configuration field check script for Freescale Kinetis MCUs. +# +# This script is supposed to be called from RIOTs +# unified OpenOCD script (dist/tools/openocd/openocd.sh). +# +# Syntax: check-fcfield.sh $FLASHFILE +# +# @author Jonas Remmert +# @author Johann Fischer +# @author Joakim Nohlgård +# @author Francisco Molina + +# Elf or Hex file to flash +FLASHFILE="$1" + +# FCFIELD goes from 0x400-0x40f +FCFIELD_START='0x400' +FCFIELD_END='0x410' +FCFIELD_AWK_REGEX='^ 0400 ' + +# The following fcfield places the flash in a unsecure state which is the default +# FCF code for Freescale Kinetis MCUs, see /cpu/kinetis/fcfield.c. +EXPECTED_FCFIELD="^fffffffffffffffffffffffffe..ffff$" + +get_fc_field() +{ + if [ ${1##*.} = elf ]; then + arm-none-eabi-objdump -j.fcfield -s "${1}" + elif [ ${1##*.} = hex ]; then + arm-none-eabi-objdump --start-address=${FCFIELD_START} --stop-address=${FCFIELD_END} ${1} -s + else + echo "Unkown file extension: ${1##*.}" + exit 1 + fi +} + +filter_fc_field() +{ + get_fc_field "${1}" | awk -F' ' "/${2}/ { print \$2 \$3 \$4 \$5; }" +} + +if [ $# -ne 1 ]; then + echo "Usage: $0 FLASHFILE" + echo "Checks the flash configuration field protection bits to avoid flashing a locked image to a Kinetis MCU (protection against accidental bricking)." + exit 2 +fi + +if ! filter_fc_field ${FLASHFILE} ${FCFIELD_AWK_REGEX} | grep -q "${EXPECTED_FCFIELD}"; then + echo "Danger of bricking the device during flash!" + echo "Flash configuration field of ${FLASHFILE}:" + get_fc_field ${FLASHFILE} + echo "Abort flash procedure!" + exit 1 +fi +echo "${FLASHFILE} is not locked." +exit 0