-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_psa_target.py
451 lines (372 loc) · 11.4 KB
/
test_psa_target.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python3
"""
Copyright (c) 2020-2021 ARM Limited. All rights reserved.
SPDX-License-Identifier: Apache-2.0
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.
"""
import os
from os.path import join, relpath, normpath
import argparse
import sys
import signal
import logging
import json
import shutil
from psa_builder import *
logging.basicConfig(
level=logging.INFO,
format="[Test-Target] %(asctime)s: %(message)s.",
datefmt="%H:%M:%S",
)
def _get_baud_rate():
"""
return baud rate from mbed_app.json
"""
with open("mbed_app.json", "r") as json_file:
json_object = json.load(json_file)
json_file.close()
return json_object["target_overrides"]["*"]["platform.stdio-baud-rate"]
def _set_json_param(regression, compliance, sync):
"""
Set json parameter required for Mbed OS build
:param regression: set regression build
:param compliance: set compliance build
:param sync: set waiting for sync from Greentea host
"""
with open("mbed_app.json", "r") as json_file:
json_object = json.load(json_file)
json_file.close()
json_object["config"]["regression-test"] = regression
json_object["config"]["psa-compliance-test"] = compliance
json_object["config"]["wait-for-sync"] = sync
with open("mbed_app.json", "w") as json_file:
json.dump(json_object, json_file, indent=4)
json_file.close()
def _build_mbed_os(args):
"""
Build Mbed OS
:param args: Command-line arguments
"""
build_tool = "mbed" if args.cli == 1 else "mbedtools"
cmd = [
build_tool,
"compile",
"-m",
args.mcu,
"-t",
TC_DICT.get(args.toolchain),
]
retcode = run_cmd_output_realtime(cmd, ROOT)
if retcode:
logging.critical("Unable to build Mbed OS target - %s", args.mcu)
sys.exit(1)
def _build_tfm(args, config, suite=None):
"""
Build TF-M regression test
:param args: Command-line arguments
:param config: Config type
:param suite: Test suite for PSA compliance
"""
cmd = [
# On Windows, python3 interpreter can be python.exe instead of python3.exe.
"python3" if shutil.which("python3") is not None else "python",
"build_tfm.py",
"-m",
args.mcu,
"-t",
args.toolchain,
"-c",
config,
]
if config in SUPPORTED_TFM_PSA_CONFIGS:
cmd.append("-s")
cmd.append(suite)
if args.clean:
cmd.append("--clean")
if args.skip_clone:
cmd.append("--skip-clone")
retcode = run_cmd_output_realtime(cmd, ROOT)
if retcode:
logging.critical("Unable to build TF-M for target - %s", args.mcu)
sys.exit(1)
def _erase_flash_storage(args, suite):
"""
Creates a target specific binary which has its ITS erased
:param args: Command-line arguments
:param suite: Test suite
:return: return binary name generated
"""
if args.cli == 1:
mbed_os_dir = join(
ROOT, "BUILD", args.mcu, TC_DICT.get(args.toolchain)
)
else:
mbed_os_dir = join(
ROOT,
"cmake_build",
args.mcu,
"develop",
TC_DICT.get(args.toolchain),
)
cmd = []
binary_name = "mbed-os-tf-m-regression-tests-reset-flash-{}.hex".format(
suite
)
if args.mcu == "ARM_MUSCA_B1":
cmd = [
"srec_cat",
"mbed-os-tf-m-regression-tests.bin",
"-Binary",
"-offset",
"0xA000000",
"-fill",
"0xFF",
"0xA1F0000",
"0XA1FC000",
"-o",
binary_name,
"-Intel",
]
if args.mcu == "ARM_MUSCA_S1":
# Note: The erase range is different from https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git/tree/platform/ext/target/musca_s1/partition/flash_layout.h?h=TF-Mv1.2.0#n29
# ARM_MUSCA_S1's DAPLink only permits 64K-aligned flashing for compatibility with QSPI even though MRAM (which we use) has a 4K granularity.
cmd = [
"srec_cat",
"mbed-os-tf-m-regression-tests.bin",
"-Binary",
"-offset",
"0xA000000",
"-fill",
"0xFF",
"0xA1E0000",
"0xA1F0000",
"-o",
binary_name,
"-Intel",
]
if args.mcu == "NU_M2354":
# Note: Dummy the script for erasing flash storage because drag-n-drop flash
# invokes Mass Erase which will erase the whole flash.
cmd = [
"srec_cat",
"mbed-os-tf-m-regression-tests.bin",
"-Binary",
"-offset",
"0x0",
"-o",
binary_name,
"-Intel",
]
retcode = run_cmd_output_realtime(cmd, mbed_os_dir)
if retcode:
logging.critical(
"Unable to create a binary with ITS erased for target %s, suite %s",
args.mcu,
suite,
)
sys.exit(1)
return binary_name
def _execute_test():
"""
Execute greentea runs test as specified in test_spec.json
"""
if not os.path.isfile("test_spec.json"):
logging.critical(
"test_spec.json is not found, please build the tests first"
)
sys.exit(1)
cmd = ["mbedgt", "--polling-timeout", "600", "-V"]
run_cmd_output_realtime(cmd, os.getcwd())
def _init_test_spec(args):
"""
initialize test specification
:param args: Command-line arguments
:return: test specification dictionary with initial contents
"""
target = args.mcu
toolchain = TC_DICT.get(args.toolchain)
test_group = _get_test_group(args)
baud_rate = _get_baud_rate()
return {
"builds": {
test_group: {
"platform": target,
"toolchain": toolchain,
"base_path": ".",
"baud_rate": baud_rate,
"tests": {},
}
}
}
def _get_test_group(args):
"""
make a unique test group name
:param args: Command-line arguments
:return: return test group name
"""
target = args.mcu
toolchain = TC_DICT.get(args.toolchain)
baud_rate = _get_baud_rate()
return "{}-{}-{}".format(target, toolchain, baud_rate)
def _get_test_spec(args, suite, binary_name):
"""
return test specification for the suite name
:param args: Command-line arguments
:param suite: test suite
:param binary_name: name of the binary
:return: return test spec dictionary for the suite name
"""
target = args.mcu
toolchain = TC_DICT.get(args.toolchain)
log_path = join("test", "logs", target, "{}.log".format(suite))
if args.cli == 1:
image_path = join("BUILD", target, toolchain, binary_name)
else:
image_path = join(
"cmake_build", target, "develop", toolchain, binary_name
)
return {
"binaries": [
{
"binary_type": "bootable",
"path": normpath(image_path),
"compare_log": log_path,
}
]
}
def _build_regression_test(args, test_spec):
"""
Build TF-M regression test for the target
:param args: Command-line arguments
"""
logging.info("Build TF-M regression tests for %s", args.mcu)
suite = "REGRESSION"
_set_json_param(1, 0, 0 if args.no_sync else 1)
# build stuff
_build_tfm(args, "RegressionIPC")
_build_mbed_os(args)
binary_name = _erase_flash_storage(args, suite)
# update the test_spec
test_group = _get_test_group(args)
test_spec["builds"][test_group]["tests"][suite.lower()] = _get_test_spec(
args, suite, binary_name
)
def _build_compliance_test(args, test_spec):
"""
Build PSA Compliance test for the target
:param args: Command-line arguments
"""
_set_json_param(0, 1, 0 if args.no_sync else 1)
test_group = _get_test_group(args)
for suite in PSA_SUITE_CHOICES:
logging.info("Build PSA Compliance - %s suite for %s", suite, args.mcu)
_build_tfm(args, "PsaApiTestIPC", suite)
_build_mbed_os(args)
binary_name = _erase_flash_storage(args, suite)
test_spec["builds"][test_group]["tests"][
suite.lower()
] = _get_test_spec(args, suite, binary_name)
def _get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"-m",
"--mcu",
help="Build for the given MCU",
choices=get_tfm_regression_targets(),
default=None,
)
parser.add_argument(
"-t",
"--toolchain",
help="Build for the given toolchain",
default=None,
choices=["ARMCLANG", "GNUARM"],
)
parser.add_argument(
"-b",
"--build",
help="Build the target only",
action="store_true",
)
parser.add_argument(
"-r",
"--run",
help="Run on the target only",
action="store_true",
)
parser.add_argument(
"--no-sync",
help="Tests start without waiting for sync from Greentea host",
action="store_true",
)
parser.add_argument(
"-l",
"--list",
help="Print supported TF-M secure targets",
action="store_true",
)
parser.add_argument(
"--clean",
help="Clean the cloned dependencies",
action="store_true",
default=False,
)
parser.add_argument(
"--skip-clone",
help="Skip cloning/checkout of TF-M dependencies",
action="store_true",
default=False,
)
parser.add_argument(
"--cli",
help="Build with the specified version of Mbed CLI",
type=int,
default=2,
choices=[1, 2],
)
return parser
def _main():
"""
Build and run Regression, PSA compliance for suported targets
"""
signal.signal(signal.SIGINT, exit_gracefully)
parser = _get_parser()
args = parser.parse_args()
if args.list:
logging.info(
"Supported TF-M regression and PSA compliance targets are: {}".format(
", ".join([t for t in get_tfm_regression_targets()])
)
)
return
logging.info("Target - %s", args.mcu)
build = args.build
run = args.run
# Default to build and run
if not args.build and not args.run:
build = True
run = True
if build:
test_spec = _init_test_spec(args)
_build_regression_test(args, test_spec)
# M2354 hasn't supported PSA compliance test yet.
if args.mcu != "NU_M2354":
_build_compliance_test(args, test_spec)
with open("test_spec.json", "w") as f:
f.write(json.dumps(test_spec, indent=2))
logging.info("Target built succesfully - %s", args.mcu)
if run:
_execute_test()
if __name__ == "__main__":
if are_dependencies_installed() != 0:
sys.exit(1)
else:
_main()