1#!/usr/bin/env bash
2#******************************************************************************
3#  * @attention
4#  *
5#  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
6#  * All rights reserved.</center></h2>
7#  * <h2><center>&copy; Copyright (c) 2021 Nordic Semiconductor ASA.
8#  * </center></h2>
9#  *
10#  *
11#  * This software component is licensed by ST under BSD 3-Clause license,
12#  * the "License"; You may not use this file except in compliance with the
13#  * License. You may obtain a copy of the License at:
14#  *                        opensource.org/licenses/BSD-3-Clause
15#  *
16#  ******************************************************************************
17# arg1 is optional, it fixes compiler full path if present
18# Absolute path to this script
19SCRIPT=$(readlink -f $0)
20# Absolute path this script
21projectdir=`dirname $SCRIPT`
22source $projectdir/preprocess.sh
23# Compiler full name & path is passed as argument
24compiler_full_path=
25if [ $# -eq 1 ]; then
26    compiler_full_path=$1
27fi
28# the file to preprocess is generated and present outside of install dir
29bl2_file_to_preprocess=$projectdir/image_macros_to_preprocess_bl2.c
30preprocess_bl2_file=$projectdir/image_macros_preprocessed_bl2.c
31regressionsh=$projectdir/regression.sh
32updatesh=$projectdir/TFM_UPDATE.sh
33bin2hexsh=$projectdir/TFM_BIN2HEX.sh
34basedir=$projectdir
35echo preprocess bl2 file
36preprocess $projectdir $bl2_file_to_preprocess $preprocess_bl2_file $compiler_full_path
37stm_tool=$basedir"/scripts/stm_tool.py"
38#determine/check python version command
39cmd="python3"
40$cmd --version  &> /dev/null
41ret=$?
42if [ $ret != 0 ]; then
43  cmd="python"
44  $cmd --version  &> /dev/null
45  ret=$?
46  if [ $ret != 0 ]; then
47    echo "This script requires python 3.0 or greater"
48    exit 1
49  fi
50  ver=$(python -V 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/')
51  if [ "$ver" -lt "30" ]; then
52    echo "This script requires python 3.0 or greater"
53    exit 1
54  fi
55fi
56
57command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b secbootadd0 -m  RE_BL2_BOOT_ADDRESS  -d 0x80  -s 0 "$regressionsh
58$command  >> $projectdir"/output.txt"
59ret=$?
60if [ $ret != 0 ]; then
61echo "postbuild.sh failed"
62echo $command
63exit 1
64fi
65
66command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b boot -m  RE_BL2_BIN_ADDRESS -s 0 "$updatesh
67$command  >> $projectdir"/output.txt"
68ret=$?
69if [ $ret != 0 ]; then
70echo "postbuild.sh failed"
71echo $command
72exit 1
73fi
74
75command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot0 -m  RE_IMAGE_FLASH_ADDRESS_SECURE -s 0 "$updatesh
76$command  >> $projectdir"/output.txt"
77ret=$?
78if [ $ret != 0 ]; then
79echo "postbuild.sh failed"
80echo $command
81exit 1
82fi
83
84echo $updatebat" updated"
85command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot1 -m  RE_IMAGE_FLASH_ADDRESS_NON_SECURE -s 0 "$updatesh
86$command  >> $projectdir"/output.txt"
87ret=$?
88if [ $ret != 0 ]; then
89echo "postbuild.sh failed"
90echo $command
91exit 1
92fi
93echo $updatebat" updated"
94command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot2 -m  RE_IMAGE_FLASH_SECURE_UPDATE -s 0 "$updatesh
95$command  >> $projectdir"/output.txt"
96ret=$?
97if [ $ret != 0 ]; then
98echo "postbuild.sh failed"
99echo $command
100exit 1
101fi
102echo $updatebat" updated"
103command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot3 -m  RE_IMAGE_FLASH_NON_SECURE_UPDATE -s 0 "$updatesh
104$command  >> $projectdir"/output.txt"
105ret=$?
106if [ $ret != 0 ]; then
107echo "postbuild.sh failed"
108echo $command
109exit 1
110fi
111echo $updatebat" updated"
112command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b scratch -m  RE_IMAGE_FLASH_SCRATCH -s 0 "$updatesh
113$command  >> $projectdir"/output.txt"
114ret=$?
115if [ $ret != 0 ]; then
116echo "postbuild.sh failed"
117echo $command
118exit 1
119fi
120echo $updatebat" updated"
121command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b nvcounter -m  RE_IMAGE_FLASH_NV_COUNTERS -s 0 "$updatesh
122$command  >> $projectdir"/output.txt"
123ret=$?
124if [ $ret != 0 ]; then
125echo "postbuild.sh failed"
126echo $command
127exit 1
128fi
129command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b nvmcnt -m  RE_BL2_NVMCNT_ADDRESS -s 0 "$updatesh
130$command  >> $projectdir"/output.txt"
131ret=$?
132if [ $ret != 0 ]; then
133echo "postbuild.sh failed"
134echo $command
135exit 1
136fi
137command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b prov -m  RE_BL2_PROVISION_ADDRESS -s 0 "$updatesh
138$command  >> $projectdir"/output.txt"
139ret=$?
140if [ $ret != 0 ]; then
141echo "postbuild.sh failed"
142echo $command
143exit 1
144fi
145echo $updatebat" updated"
146command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b sst -m  RE_IMAGE_FLASH_NV_PS -s 0 "$updatesh
147$command  >> $projectdir"/output.txt"
148ret=$?
149if [ $ret != 0 ]; then
150echo "postbuild.sh failed"
151echo $command
152exit 1
153fi
154echo $updatebat" updated"
155command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b its -m  RE_IMAGE_FLASH_NV_ITS -s 0 "$updatesh
156$command  >> $projectdir"/output.txt"
157ret=$?
158if [ $ret != 0 ]; then
159echo "postbuild.sh failed"
160echo $command
161exit 1
162fi
163echo $updatebat" updated"
164command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b unused -m  RE_IMAGE_FLASH_UNUSED -s 0 "$updatesh
165$command  >> $projectdir"/output.txt"
166ret=$?
167if [ $ret != 0 ]; then
168echo "postbuild.sh failed"
169echo $command
170exit 1
171fi
172echo $updatebat" updated"
173command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b encrypted -m  RE_IMAGE_ENCRYPTED -s 0 "$updatesh
174$command  >> $projectdir"/output.txt"
175ret=$?
176if [ $ret != 0 ]; then
177echo "postbuild.sh failed"
178echo $command
179exit 1
180fi
181
182command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b boot -m  RE_BL2_BIN_ADDRESS -s 0 "$bin2hexsh
183$command  >> $projectdir"/output.txt"
184ret=$?
185if [ $ret != 0 ]; then
186echo "postbuild.sh failed"
187echo $command
188exit 1
189fi
190
191
192command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot0 -m  RE_IMAGE_FLASH_ADDRESS_SECURE -s 0 "$bin2hexsh
193$command  >> $projectdir"/output.txt"
194ret=$?
195if [ $ret != 0 ]; then
196echo "postbuild.sh failed"
197echo $command
198exit 1
199fi
200
201echo $updatebat" updated"
202command=$cmd" "$stm_tool" flash --layout "$preprocess_bl2_file" -b slot1 -m  RE_IMAGE_FLASH_ADDRESS_NON_SECURE -s 0 "$bin2hexsh
203$command  >> $projectdir"/output.txt"
204ret=$?
205if [ $ret != 0 ]; then
206echo "postbuild.sh failed"
207echo $command
208exit 1
209fi
210