1# Copyright (c) 2020, Linaro. All rights reserved.
2# Copyright (c) 2020, Arm Limited. All rights reserved.
3# Copyright 2020-2023 NXP. All rights reserved.
4# SPDX-License-Identifier: BSD-3-Clause
5
6import os
7import platform
8
9# Move to the TFM build folder
10os.chdir('../../../../../../build/bin')
11
12# Parse flash base addresses of secure and non secure images stored in CMakeCache.txt
13if platform.system() == 'Windows':
14    BL2_S_IMAGE_START =  os.popen('findstr BL2_S_IMAGE_START:STRING= ..\CMakeCache.txt').read().split('=')[1].rstrip()
15    BL2_NS_IMAGE_START = os.popen('findstr BL2_NS_IMAGE_START:STRING= ..\CMakeCache.txt').read().split('=')[1].rstrip()
16
17else:
18    BL2_S_IMAGE_START =  os.popen('grep "BL2_S_IMAGE_START:STRING=" ../CMakeCache.txt').read().split('=')[1].rstrip()
19    BL2_NS_IMAGE_START = os.popen('grep "BL2_NS_IMAGE_START:STRING=" ../CMakeCache.txt').read().split('=')[1].rstrip()
20
21# Define JLink configuration script file
22FILE = "flash.jlink"
23
24# Remove previous flash.jlink script file
25if os.path.isfile(FILE):
26    if platform.system() == 'Windows':
27        os.system('del /f /q ' + FILE)
28    else:
29        os.system('rm -rf ' + FILE)
30
31# Write the JLink configuration into flash.jlink script
32os.system('echo r >> ' + FILE)                                                      # reset the target
33os.system('echo erase  >> ' + FILE)                                                 # erase the flash memory
34os.system('echo loadfile bl2.hex >> ' + FILE)                                       # flash the bootloader file into target
35os.system('echo loadfile tfm_s_ns_signed.bin ' + BL2_S_IMAGE_START + ' >> ' + FILE) # flash the signed image into target
36os.system('echo r >> ' + FILE)                                                      # reset the target
37os.system('echo go >> ' + FILE)                                                     # run the program
38os.system('echo exit >> ' + FILE)                                                   # exit the JLinkCommander
39
40# Upload the configuration from flash.jlink script into the target device
41if platform.system() == 'Windows':
42    os.system('JLink -device lpc55s69 -if swd -speed 2000 -autoconnect 1 -commanderscript ' + FILE)
43
44else:
45    os.system('JLinkExe -device lpc55s69 -if swd -speed 2000 -autoconnect 1 -commanderscript ' +FILE)
46