Platform: stm32l5xx: Create a dedicated stm_tool.py

Since tfm relies on mcuboot upstream, the imgtool stm specific
function used for computing the stm32l5xx board programming
settings is not available on mcuboot upstream. stm_tool.py
implements the computing of board programming settings.

Change-Id: I49059d543806dab198bd18e85be18cf057b82b89
Signed-off-by: Michel Jaouen <michel.jaouen@st.com>
diff --git a/platform/ext/target/stm/stm32l5xx/boards/scripts/postbuild.sh b/platform/ext/target/stm/stm32l5xx/boards/scripts/postbuild.sh
index 01d39bf..605fd00 100755
--- a/platform/ext/target/stm/stm32l5xx/boards/scripts/postbuild.sh
+++ b/platform/ext/target/stm/stm32l5xx/boards/scripts/postbuild.sh
@@ -25,7 +25,7 @@
 basedir=$projectdir
 echo preprocess bl2 file
 arm-none-eabi-gcc -E -P -xc -I$projectdir -o$preprocess_bl2_file $bl2_file_to_preprocess
-imgtool=$basedir"/scripts/imgtool.py"
+stm_tool=$basedir"/scripts/stm_tool.py"
 #determine/check python version command
 cmd="python3"
 $cmd --version  &> /dev/null
@@ -45,7 +45,7 @@
   fi
 fi
 
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b secbootadd0 -m  RE_BL2_BOOT_ADDRESS  -d 0x80  -s 0 "$regressionsh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b secbootadd0 -m  RE_BL2_BOOT_ADDRESS  -d 0x80  -s 0 "$regressionsh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -54,7 +54,7 @@
 exit 1
 fi
 
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b boot -m  RE_BL2_PERSO_ADDRESS -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b boot -m  RE_BL2_PERSO_ADDRESS -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -63,7 +63,7 @@
 exit 1
 fi
 
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b slot0 -m  RE_IMAGE_FLASH_ADDRESS_SECURE -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot0 -m  RE_IMAGE_FLASH_ADDRESS_SECURE -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -73,7 +73,7 @@
 fi
 
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b slot1 -m  RE_IMAGE_FLASH_ADDRESS_NON_SECURE -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot1 -m  RE_IMAGE_FLASH_ADDRESS_NON_SECURE -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -82,7 +82,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b slot2 -m  RE_IMAGE_FLASH_SECURE_UPDATE -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot2 -m  RE_IMAGE_FLASH_SECURE_UPDATE -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -91,7 +91,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b slot3 -m  RE_IMAGE_FLASH_NON_SECURE_UPDATE -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot3 -m  RE_IMAGE_FLASH_NON_SECURE_UPDATE -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -100,7 +100,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b scratch -m  RE_IMAGE_FLASH_SCRATCH -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b scratch -m  RE_IMAGE_FLASH_SCRATCH -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -109,7 +109,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b nvcounter -m  RE_IMAGE_FLASH_NV_COUNTERS -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b nvcounter -m  RE_IMAGE_FLASH_NV_COUNTERS -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -118,7 +118,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b sst -m  RE_IMAGE_FLASH_NV_PS -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b sst -m  RE_IMAGE_FLASH_NV_PS -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -127,7 +127,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b its -m  RE_IMAGE_FLASH_NV_ITS -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b its -m  RE_IMAGE_FLASH_NV_ITS -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
@@ -136,7 +136,7 @@
 exit 1
 fi
 echo $updatebat" updated"
-command=$cmd" "$imgtool" flash --layout "$preprocess_bl2_file" -b unused -m  RE_IMAGE_FLASH_UNUSED -s 0 "$updatesh
+command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b unused -m  RE_IMAGE_FLASH_UNUSED -s 0 "$updatesh
 $command  >> $projectdir"/output.txt"
 ret=$?
 if [ $ret != 0 ]; then
diff --git a/platform/ext/target/stm/stm32l5xx/boards/scripts/stm_tool.py b/platform/ext/target/stm/stm32l5xx/boards/scripts/stm_tool.py
new file mode 100644
index 0000000..6295ed8
--- /dev/null
+++ b/platform/ext/target/stm/stm32l5xx/boards/scripts/stm_tool.py
@@ -0,0 +1,87 @@
+#! /usr/bin/env python3
+#
+# Copyright 2017 Linaro Limited
+# Copyright (c) 2018-2019, Arm Limited.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import print_function
+import os
+import re
+import argparse
+
+import sys
+import macro_parser
+import fileinput
+
+
+def do_flash(args):
+    image_value_re = re.compile(r"^\s*"+args.macro+"\s*=\s*(.*)")
+    value = macro_parser.evaluate_macro(args.layout, image_value_re, 0, 1,
+                                        True)
+    if args.setting == 1:
+        begin_line="set "+args.begin
+    else:
+        begin_line=args.begin
+
+    for line in fileinput.input(args.infile, inplace=True):
+        if line.startswith(begin_line):
+            if args.division:
+                value = int(value/int(args.division))
+            if args.phexa == 0:
+                line = begin_line+"="+str(value)+"\n"
+            else:
+                line = begin_line+"="+hex(value)+"\n"
+        sys.stdout.write(line)
+
+subcmds = {
+        'flash': do_flash, }
+
+
+def intparse(text):
+    """Parse a command line argument as an integer.
+
+    Accepts 0x and other prefixes to allow other bases to be used."""
+    return int(text, 0)
+
+def args():
+    parser = argparse.ArgumentParser()
+    subs = parser.add_subparsers(help='subcommand help', dest='subcmd')
+
+
+    flash = subs.add_parser('flash', help='modify flash script')
+    flash.add_argument("infile")
+    flash.add_argument('-l', '--layout', required=True,
+                      help='Location of the file that contains preprocessed macros')
+    flash.add_argument('-m', '--macro', required =True,
+                      help='macro symbol string to grep in preprocessed file')
+    flash.add_argument('-b', '--begin', required=True,
+                      help='begin of line to replace ')
+    flash.add_argument('-s', '--setting',type=intparse,required=False,default=0,
+                      help='search for window batch set variable')
+    flash.add_argument('-d', '--division',
+                       required=False,type=intparse,default=0,
+                      help='search for window batch set variable')
+    flash.add_argument('-p', '--phexa',
+                       required=False,type=intparse,default=1,
+                      help='print value in hexa')
+
+    args = parser.parse_args()
+    if args.subcmd is None:
+        print('Must specify a subcommand', file=sys.stderr)
+        sys.exit(1)
+
+    subcmds[args.subcmd](args)
+
+if __name__ == '__main__':
+    args()
diff --git a/platform/ext/target/stm/stm32l5xx/nucleo_l552ze_q.cmake b/platform/ext/target/stm/stm32l5xx/nucleo_l552ze_q.cmake
index d501d9d..a8f2daf 100644
--- a/platform/ext/target/stm/stm32l5xx/nucleo_l552ze_q.cmake
+++ b/platform/ext/target/stm/stm32l5xx/nucleo_l552ze_q.cmake
@@ -133,16 +133,9 @@
 endif()
 #install files reuqired for flashing and performing regression
 install(FILES
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool.py
+        ${PLATFORM_DIR}/target/stm/stm32l5xx/boards/scripts/stm_tool.py
         ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/macro_parser.py
         DESTINATION ./scripts/ )
-install(FILES
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/image.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/boot_record.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/keys.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/version.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/__init__.py
-        DESTINATION ./scripts/imgtool_lib )
 install(FILES ${PLATFORM_DIR}/target/stm/stm32l5xx/boards/stm32l562e_dk/flash_layout.h
         ${PLATFORM_DIR}/target/stm/stm32l5xx/boards/stm32l562e_dk/region_defs.h
         DESTINATION ./ )
diff --git a/platform/ext/target/stm/stm32l5xx/stm32l562e_dk.cmake b/platform/ext/target/stm/stm32l5xx/stm32l562e_dk.cmake
index eb069c4..7856e81 100644
--- a/platform/ext/target/stm/stm32l5xx/stm32l562e_dk.cmake
+++ b/platform/ext/target/stm/stm32l5xx/stm32l562e_dk.cmake
@@ -130,18 +130,11 @@
   message(WARNING "OVERWRITE ONLY upgrade strategy is preferred on target '${TARGET_PLATFORM}'. Your choice was overriden.")
   set(MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY")
 endif()
-#install files reuqired for flashing and performing regression
+#install files required for flashing and performing regression
 install(FILES
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool.py
+        ${PLATFORM_DIR}/target/stm/stm32l5xx/boards/scripts/stm_tool.py
         ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/macro_parser.py
         DESTINATION ./scripts/ )
-install(FILES
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/image.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/boot_record.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/keys.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/version.py
-        ${PLATFORM_DIR}/../../bl2/ext/mcuboot/scripts/imgtool_lib/__init__.py
-        DESTINATION ./scripts/imgtool_lib )
 install(FILES ${PLATFORM_DIR}/target/stm/stm32l5xx/boards/stm32l562e_dk/flash_layout.h
         ${PLATFORM_DIR}/target/stm/stm32l5xx/boards/stm32l562e_dk/region_defs.h
         DESTINATION ./ )