blob: c4fe0165a74602421eec680c1a4a290aa8fbe3ec [file] [log] [blame]
David Brazdilee5e25d2020-01-24 14:17:45 +00001#!/usr/bin/env python3
Andrew Scull18834872018-10-12 11:48:09 +01002#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
Andrew Walbrane959ec12020-06-17 15:01:09 +01005# Use of this source code is governed by a BSD-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01008
David Brazdil2df24082019-09-05 11:55:08 +01009"""Script which drives invocation of tests and parsing their output to produce
10a results report.
Andrew Scullbc7189d2018-08-14 09:35:13 +010011"""
12
13from __future__ import print_function
14
Andrew Scull3b62f2b2018-08-21 14:26:12 +010015import xml.etree.ElementTree as ET
16
Andrew Scullbc7189d2018-08-14 09:35:13 +010017import argparse
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010018from abc import ABC, abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +010019import collections
Andrew Scull04502e42018-09-03 14:54:52 +010020import datetime
David Brazdil4f9cf9a2020-02-06 17:34:44 +000021import importlib
Andrew Scullbc7189d2018-08-14 09:35:13 +010022import json
23import os
24import re
25import subprocess
26import sys
Andrew Walbran42bf2842020-06-05 18:50:19 +010027import time
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010028import fdt
Andrew Scullbc7189d2018-08-14 09:35:13 +010029
Andrew Scull845fc9b2019-04-03 12:44:26 +010030HFTEST_LOG_PREFIX = "[hftest] "
31HFTEST_LOG_FAILURE_PREFIX = "Failure:"
32HFTEST_LOG_FINISHED = "FINISHED"
33
David Brazdil17e76652020-01-29 14:44:19 +000034HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]"
35HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]"
36
David Brazdil2df24082019-09-05 11:55:08 +010037HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
38 os.path.abspath(__file__))))
David Brazdil5715f042019-08-27 11:11:51 +010039DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py")
David Brazdil2df24082019-09-05 11:55:08 +010040FVP_BINARY = os.path.join(
41 os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMv8A_pkg", "models",
Olivier Depreze4153042020-10-02 15:24:59 +020042 "Linux64_GCC-6.4", "FVP_Base_RevC-2xAEMv8A")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010043HF_PREBUILTS = os.path.join(HF_ROOT, "prebuilts")
44FVP_PREBUILTS_TFA_TRUSTY_ROOT = os.path.join(
45 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", "fvp")
David Brazdil2df24082019-09-05 11:55:08 +010046FVP_PREBUILT_DTS = os.path.join(
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010047 FVP_PREBUILTS_TFA_TRUSTY_ROOT, "fvp-base-gicv3-psci-1t.dts")
48
49FVP_PREBUILT_TFA_ROOT = os.path.join(
50 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a", "fvp")
Andrew Scull845fc9b2019-04-03 12:44:26 +010051
David Brazdil2df24082019-09-05 11:55:08 +010052def read_file(path):
53 with open(path, "r") as f:
54 return f.read()
Andrew Scull845fc9b2019-04-03 12:44:26 +010055
David Brazdil2df24082019-09-05 11:55:08 +010056def write_file(path, to_write, append=False):
57 with open(path, "a" if append else "w") as f:
58 f.write(to_write)
59
60def append_file(path, to_write):
61 write_file(path, to_write, append=True)
62
63def join_if_not_None(*args):
64 return " ".join(filter(lambda x: x, args))
65
J-Alves8cc7dbb2021-04-16 10:38:48 +010066DT = collections.namedtuple("DT", ["dts", "dtb"])
67
David Brazdil2df24082019-09-05 11:55:08 +010068class ArtifactsManager:
69 """Class which manages folder with test artifacts."""
70
71 def __init__(self, log_dir):
72 self.created_files = []
73 self.log_dir = log_dir
74
75 # Create directory.
Andrew Scull845fc9b2019-04-03 12:44:26 +010076 try:
David Brazdil2df24082019-09-05 11:55:08 +010077 os.makedirs(self.log_dir)
78 except OSError:
79 if not os.path.isdir(self.log_dir):
80 raise
81 print("Logs saved under", log_dir)
82
83 # Create files expected by the Sponge test result parser.
84 self.sponge_log_path = self.create_file("sponge_log", ".log")
85 self.sponge_xml_path = self.create_file("sponge_log", ".xml")
86
David Brazdil623b6812019-09-09 11:41:08 +010087 def gen_file_path(self, basename, extension):
88 """Generate path to a file in the log directory."""
89 return os.path.join(self.log_dir, basename + extension)
90
David Brazdil2df24082019-09-05 11:55:08 +010091 def create_file(self, basename, extension):
92 """Create and touch a new file in the log folder. Ensure that no other
93 file of the same name was created by this instance of ArtifactsManager.
94 """
95 # Determine the path of the file.
David Brazdil623b6812019-09-09 11:41:08 +010096 path = self.gen_file_path(basename, extension)
David Brazdil2df24082019-09-05 11:55:08 +010097
98 # Check that the path is unique.
99 assert(path not in self.created_files)
100 self.created_files += [ path ]
101
102 # Touch file.
103 with open(path, "w") as f:
104 pass
105
106 return path
Andrew Scullbc7189d2018-08-14 09:35:13 +0100107
David Brazdil623b6812019-09-09 11:41:08 +0100108 def get_file(self, basename, extension):
109 """Return path to a file in the log folder. Assert that it was created
110 by this instance of ArtifactsManager."""
111 path = self.gen_file_path(basename, extension)
112 assert(path in self.created_files)
113 return path
114
Andrew Scullbc7189d2018-08-14 09:35:13 +0100115
David Brazdil2df24082019-09-05 11:55:08 +0100116# Tuple holding the arguments common to all driver constructors.
117# This is to avoid having to pass arguments from subclasses to superclasses.
118DriverArgs = collections.namedtuple("DriverArgs", [
119 "artifacts",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100120 "hypervisor",
121 "spmc",
David Brazdil2df24082019-09-05 11:55:08 +0100122 "initrd",
123 "vm_args",
David Brazdil17e76652020-01-29 14:44:19 +0000124 "cpu",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100125 "partitions"
David Brazdil2df24082019-09-05 11:55:08 +0100126 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100127
Andrew Walbran98656252019-03-14 14:52:29 +0000128
David Brazdil2df24082019-09-05 11:55:08 +0100129# State shared between the common Driver class and its subclasses during
130# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100131class DriverRunState:
132 def __init__(self, log_path):
133 self.log_path = log_path
134 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000135
David Brazdil7325eaf2019-09-27 13:04:51 +0100136 def set_ret_code(self, ret_code):
137 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000138
David Brazdil0dbb41f2019-09-09 18:03:35 +0100139class DriverRunException(Exception):
140 """Exception thrown if subprocess invoked by a driver returned non-zero
141 status code. Used to fast-exit from a driver command sequence."""
142 pass
143
144
David Brazdil2df24082019-09-05 11:55:08 +0100145class Driver:
146 """Parent class of drivers for all testable platforms."""
147
148 def __init__(self, args):
149 self.args = args
150
David Brazdil623b6812019-09-09 11:41:08 +0100151 def get_run_log(self, run_name):
152 """Return path to the main log of a given test run."""
153 return self.args.artifacts.get_file(run_name, ".log")
154
David Brazdil2df24082019-09-05 11:55:08 +0100155 def start_run(self, run_name):
156 """Hook called by Driver subclasses before they invoke the target
157 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100158 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100159
Andrew Walbranf636b842020-01-10 11:46:12 +0000160 def exec_logged(self, run_state, exec_args, cwd=None):
David Brazdil2df24082019-09-05 11:55:08 +0100161 """Run a subprocess on behalf of a Driver subclass and append its
162 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100163 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100164 with open(run_state.log_path, "a") as f:
165 f.write("$ {}\r\n".format(" ".join(exec_args)))
166 f.flush()
Andrew Walbranf636b842020-01-10 11:46:12 +0000167 ret_code = subprocess.call(exec_args, stdout=f, stderr=f, cwd=cwd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100168 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100169 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100170 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100171
David Brazdil0dbb41f2019-09-09 18:03:35 +0100172 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100173 """Hook called by Driver subclasses after they finished running the
174 target platform. `ret_code` argument is the return code of the main
175 command run by the driver. A corresponding log message is printed."""
176 # Decode return code and add a message to the log.
177 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100178 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100179 f.write("\r\n{}{} timed out\r\n".format(
180 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100181 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100182 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100183 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
184 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100185
186 # Append log of this run to full test log.
187 log_content = read_file(run_state.log_path)
188 append_file(
189 self.args.artifacts.sponge_log_path,
190 log_content + "\r\n\r\n")
191 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000192
193
David Brazdil2df24082019-09-05 11:55:08 +0100194class QemuDriver(Driver):
195 """Driver which runs tests in QEMU."""
196
Andrew Walbranf636b842020-01-10 11:46:12 +0000197 def __init__(self, args, qemu_wd, tfa):
David Brazdil2df24082019-09-05 11:55:08 +0100198 Driver.__init__(self, args)
Andrew Walbranf636b842020-01-10 11:46:12 +0000199 self.qemu_wd = qemu_wd
200 self.tfa = tfa
David Brazdil2df24082019-09-05 11:55:08 +0100201
David Brazdila2358d42020-01-27 18:51:38 +0000202 def gen_exec_args(self, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100203 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100204 time_limit = "120s" if is_long_running else "10s"
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000205 # If no CPU configuration is selected, then test against the maximum
206 # configuration, "max", supported by QEMU.
207 cpu = self.args.cpu or "max"
David Brazdil2df24082019-09-05 11:55:08 +0100208 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100209 "timeout", "--foreground", time_limit,
Andrew Walbranf636b842020-01-10 11:46:12 +0000210 os.path.abspath("prebuilts/linux-x64/qemu/qemu-system-aarch64"),
Andrew Walbrana081a292020-01-23 10:08:42 +0000211 "-machine", "virt,virtualization=on,gic-version=3",
Andrew Walbranf636b842020-01-10 11:46:12 +0000212 "-cpu", cpu, "-smp", "4", "-m", "1G",
David Brazdil2df24082019-09-05 11:55:08 +0100213 "-nographic", "-nodefaults", "-serial", "stdio",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100214 "-d", "unimp", "-kernel", os.path.abspath(self.args.hypervisor),
David Brazdil2df24082019-09-05 11:55:08 +0100215 ]
216
Andrew Walbranf636b842020-01-10 11:46:12 +0000217 if self.tfa:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100218 bl1_path = os.path.join(
219 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty",
220 "qemu", "bl1.bin")
Andrew Walbranf636b842020-01-10 11:46:12 +0000221 exec_args += ["-bios",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100222 os.path.abspath(bl1_path),
223 "-machine", "secure=on", "-semihosting-config",
Andrew Walbranf636b842020-01-10 11:46:12 +0000224 "enable,target=native"]
225
David Brazdil2df24082019-09-05 11:55:08 +0100226 if self.args.initrd:
Andrew Walbranf636b842020-01-10 11:46:12 +0000227 exec_args += ["-initrd", os.path.abspath(self.args.initrd)]
David Brazdil2df24082019-09-05 11:55:08 +0100228
229 vm_args = join_if_not_None(self.args.vm_args, test_args)
230 if vm_args:
231 exec_args += ["-append", vm_args]
232
233 return exec_args
234
David Brazdil3cc24aa2019-09-27 10:24:41 +0100235 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100236 """Run test given by `test_args` in QEMU."""
237 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100238
239 try:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100240 # Execute test in QEMU..
David Brazdila2358d42020-01-27 18:51:38 +0000241 exec_args = self.gen_exec_args(test_args, is_long_running)
Andrew Walbranf636b842020-01-10 11:46:12 +0000242 self.exec_logged(run_state, exec_args,
243 cwd=self.qemu_wd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100244 except DriverRunException:
245 pass
246
247 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100248
David Brazdil94fd1e92020-02-03 16:45:20 +0000249 def finish(self):
250 """Clean up after running tests."""
251 pass
252
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100253class FvpDriver(Driver, ABC):
254 """Base class for driver which runs tests in Arm FVP emulator."""
David Brazdil2df24082019-09-05 11:55:08 +0100255
256 def __init__(self, args):
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000257 if args.cpu:
258 raise ValueError("FVP emulator does not support the --cpu option.")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100259 super().__init__(args)
David Brazdil2df24082019-09-05 11:55:08 +0100260
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100261 @property
262 @abstractmethod
263 def CPU_START_ADDRESS(self):
264 pass
David Brazdil2df24082019-09-05 11:55:08 +0100265
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100266 @property
267 @abstractmethod
268 def DTB_ADDRESS(self):
269 pass
270
271 @property
272 @abstractmethod
273 def FVP_PREBUILT_BL31(self):
274 pass
275
276 @property
277 @abstractmethod
278 def KERNEL_ADDRESS(self):
279 pass
280
281 def create_dt(self, run_name : str):
282 """Create DT related files, and return respective paths in a tuple
283 (dts,dtb)"""
J-Alves8cc7dbb2021-04-16 10:38:48 +0100284 return DT(self.args.artifacts.create_file(run_name, ".dts"),
285 self.args.artifacts.create_file(run_name, ".dtb"))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100286
J-Alves8cc7dbb2021-04-16 10:38:48 +0100287 def compile_dt(self, run_state, dt : DT):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100288 """Compile DT calling dtc."""
289 dtc_args = [
J-Alves8cc7dbb2021-04-16 10:38:48 +0100290 DTC_SCRIPT, "compile", "-i", dt.dts, "-o", dt.dtb,
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100291 ]
292 self.exec_logged(run_state, dtc_args)
293
294 def create_uart_log(self, run_name : str, file_name : str):
295 """Create uart log file, and return path"""
296 return self.args.artifacts.create_file(run_name, file_name)
297
298 def get_img_and_ldadd(self, partitions : dict):
299 ret = []
300 for i, p in enumerate(partitions):
301 with open(p["dts"], "r") as dts:
302 manifest = fdt.parse_dts(dts.read())
303 load_address = manifest.get_property("load_address",
304 f"/hypervisor/vm{str(i+1)}").value
305 ret.append((p["img"], load_address))
306 return ret
307
308 def get_manifests_from_json(self, partitions : list):
309 manifests = ""
310 if partitions is not None:
311 for p in partitions:
312 manifests += read_file(p["dts"])
313 return manifests
314
315 @abstractmethod
J-Alves8cc7dbb2021-04-16 10:38:48 +0100316 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100317 """Abstract method to generate dts file. This specific to the use case
318 so should be implemented within derived driver"""
319 pass
320
321 @abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +0100322 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100323 self, is_long_running, uart0_log_path, uart1_log_path, dt):
David Brazdil2df24082019-09-05 11:55:08 +0100324 """Generate command line arguments for FVP."""
Andrew Walbranee5418e2019-11-27 17:43:05 +0000325 time_limit = "80s" if is_long_running else "40s"
David Brazdil2df24082019-09-05 11:55:08 +0100326 fvp_args = [
Andrew Walbranee5418e2019-11-27 17:43:05 +0000327 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100328 FVP_BINARY,
J-Alves10446d82021-04-26 11:52:57 +0100329 "-C", "pci.pci_smmuv3.mmu.SMMU_AIDR=2",
330 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR0=0x0046123B",
331 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR1=0x00600002",
332 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR3=0x1714",
333 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR5=0xFFFF0472",
334 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR1=0xA0000002",
335 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR2=0",
336 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR3=0",
David Brazdil2df24082019-09-05 11:55:08 +0100337 "-C", "pctl.startup=0.0.0.0",
338 "-C", "bp.secure_memory=0",
339 "-C", "cluster0.NUM_CORES=4",
340 "-C", "cluster1.NUM_CORES=4",
341 "-C", "cache_state_modelled=0",
342 "-C", "bp.vis.disable_visualisation=true",
343 "-C", "bp.vis.rate_limit-enable=false",
344 "-C", "bp.terminal_0.start_telnet=false",
345 "-C", "bp.terminal_1.start_telnet=false",
346 "-C", "bp.terminal_2.start_telnet=false",
347 "-C", "bp.terminal_3.start_telnet=false",
348 "-C", "bp.pl011_uart0.untimed_fifos=1",
349 "-C", "bp.pl011_uart0.unbuffered_output=1",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100350 "-C", f"bp.pl011_uart0.out_file={uart0_log_path}",
351 "-C", f"bp.pl011_uart1.out_file={uart1_log_path}",
352 "-C", f"cluster0.cpu0.RVBAR={self.CPU_START_ADDRESS}",
353 "-C", f"cluster0.cpu1.RVBAR={self.CPU_START_ADDRESS}",
354 "-C", f"cluster0.cpu2.RVBAR={self.CPU_START_ADDRESS}",
355 "-C", f"cluster0.cpu3.RVBAR={self.CPU_START_ADDRESS}",
356 "-C", f"cluster1.cpu0.RVBAR={self.CPU_START_ADDRESS}",
357 "-C", f"cluster1.cpu1.RVBAR={self.CPU_START_ADDRESS}",
358 "-C", f"cluster1.cpu2.RVBAR={self.CPU_START_ADDRESS}",
359 "-C", f"cluster1.cpu3.RVBAR={self.CPU_START_ADDRESS}",
360 "--data",
361 f"cluster0.cpu0={self.FVP_PREBUILT_BL31}@{self.CPU_START_ADDRESS}",
David Brazdil2df24082019-09-05 11:55:08 +0100362 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
363 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
364 ]
David Brazdil2df24082019-09-05 11:55:08 +0100365 return fvp_args
366
David Brazdil3cc24aa2019-09-27 10:24:41 +0100367 def run(self, run_name, test_args, is_long_running):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100368 """ Run test """
David Brazdil2df24082019-09-05 11:55:08 +0100369 run_state = self.start_run(run_name)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100370 dt = self.create_dt(run_name)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100371 uart0_log_path = self.create_uart_log(run_name, ".uart0.log")
372 uart1_log_path = self.create_uart_log(run_name, ".uart1.log")
David Brazdil2df24082019-09-05 11:55:08 +0100373
David Brazdil0dbb41f2019-09-09 18:03:35 +0100374 try:
J-Alves8cc7dbb2021-04-16 10:38:48 +0100375 self.gen_dts(dt, test_args)
376 self.compile_dt(run_state, dt)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100377 fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path,
J-Alves8cc7dbb2021-04-16 10:38:48 +0100378 uart1_log_path, dt)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100379 self.exec_logged(run_state, fvp_args)
380 except DriverRunException:
381 pass
David Brazdil2df24082019-09-05 11:55:08 +0100382
383 # Append UART0 output to main log.
384 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100385 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100386
David Brazdil94fd1e92020-02-03 16:45:20 +0000387 def finish(self):
388 """Clean up after running tests."""
389 pass
390
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100391class FvpDriverHypervisor(FvpDriver):
392 """
393 Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor
394 """
395 INITRD_START= 0x84000000
Olivier Depreza516f482021-04-30 18:47:59 +0200396 INITRD_END = 0x86000000 #Default value, however may change if initrd in args
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100397
398 def __init__(self, args):
399 self.vms_in_partitions_json = args.partitions and args.partitions["VMs"]
400 super().__init__(args)
401
402 @property
403 def CPU_START_ADDRESS(self):
404 return "0x04020000"
405
406 @property
407 def DTB_ADDRESS(self):
408 return "0x82000000"
409
410 @property
411 def FVP_PREBUILT_BL31(self):
412 return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin")
413
414 @property
415 def KERNEL_ADDRESS(self):
416 return "0x80000000"
417
J-Alves8cc7dbb2021-04-16 10:38:48 +0100418 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100419 """Create a DeviceTree source which will be compiled into a DTB and
420 passed to FVP for a test run."""
421
422 vm_args = join_if_not_None(self.args.vm_args, test_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100423 write_file(dt.dts, read_file(FVP_PREBUILT_DTS))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100424
425 # Write the vm arguments to the partition manifest
426 to_append = f"""
427/ {{
428 chosen {{
429 bootargs = "{vm_args}";
430 stdout-path = "serial0:115200n8";
431 linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>;
432 linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>;
433 }};
434}};"""
435 if self.vms_in_partitions_json:
436 to_append += self.get_manifests_from_json(self.args.partitions["VMs"])
437
J-Alves8cc7dbb2021-04-16 10:38:48 +0100438 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100439
440 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100441 self, is_long_running, uart0_log_path, uart1_log_path, dt, call_super = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100442 """Generate command line arguments for FVP."""
J-Alves8cc7dbb2021-04-16 10:38:48 +0100443 common_args = (is_long_running, uart0_log_path, uart1_log_path, dt)
444 fvp_args = super().gen_fvp_args(*common_args) if call_super else []
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100445
J-Alves8cc7dbb2021-04-16 10:38:48 +0100446 fvp_args += [
447 "--data", f"cluster0.cpu0={dt.dtb}@{self.DTB_ADDRESS}",
448 "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.KERNEL_ADDRESS}",
449 ]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100450
451 if self.vms_in_partitions_json:
452 img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"])
453 for img, ldadd in img_ldadd:
454 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
455
456 if self.args.initrd:
457 fvp_args += [
458 "--data",
459 f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}"
460 ]
461 return fvp_args
462
463class FvpDriverSPMC(FvpDriver):
464 """
465 Driver which runs tests in Arm FVP emulator, with hafnium as SPMC
466 """
467 FVP_PREBUILT_SECURE_DTS = os.path.join(
468 HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts")
469 HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds")
470
471 def __init__(self, args):
472 if args.partitions is None or args.partitions["SPs"] is None:
J-Alves10446d82021-04-26 11:52:57 +0100473 raise Exception("Need to provide SPs in partitions_json")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100474 super().__init__(args)
475
476 @property
477 def CPU_START_ADDRESS(self):
478 return "0x04010000"
479
480 @property
481 def DTB_ADDRESS(self):
482 return "0x0403f000"
483
484 @property
485 def FVP_PREBUILT_BL31(self):
486 return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")
487
488 @property
489 def KERNEL_ADDRESS(self):
490 return "0x6000000"
491
J-Alves8cc7dbb2021-04-16 10:38:48 +0100492 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100493 """Create a DeviceTree source which will be compiled into a DTB and
494 passed to FVP for a test run."""
495 to_append = self.get_manifests_from_json(self.args.partitions["SPs"])
J-Alves8cc7dbb2021-04-16 10:38:48 +0100496 write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS))
497 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100498
499 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100500 self, is_long_running, uart0_log_path, uart1_log_path, dt,
501 call_super = True, secure_hfctrl = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100502 """Generate command line arguments for FVP."""
J-Alves8cc7dbb2021-04-16 10:38:48 +0100503 common_args = (is_long_running, uart0_log_path, uart1_log_path, dt.dtb)
504 fvp_args = super().gen_fvp_args(*common_args) if call_super else []
505
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100506 fvp_args += [
J-Alves8cc7dbb2021-04-16 10:38:48 +0100507 "--data", f"cluster0.cpu0={dt.dtb}@{self.DTB_ADDRESS}",
508 "--data", f"cluster0.cpu0={self.args.spmc}@{self.KERNEL_ADDRESS}",
509 ]
510
511 if secure_hfctrl:
512 fvp_args += [
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100513 "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}",
514 "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"",
J-Alvesb5a63992021-04-30 10:23:19 +0100515 "-C", "cluster0.has_arm_v8-5=1",
516 "-C", "cluster1.has_arm_v8-5=1",
517 "-C", "cluster0.has_branch_target_exception=1",
518 "-C", "cluster1.has_branch_target_exception=1",
519 "-C", "cluster0.restriction_on_speculative_execution=2",
520 "-C", "cluster1.restriction_on_speculative_execution=2",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100521 ]
J-Alves8cc7dbb2021-04-16 10:38:48 +0100522
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100523 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
524 for img, ldadd in img_ldadd:
525 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
526
527 return fvp_args
528
529 def run(self, run_name, test_args, is_long_running):
530 with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f:
531 vm_args = join_if_not_None(self.args.vm_args, test_args)
532 f.write(f"{vm_args}\n")
533 return super().run(run_name, test_args, is_long_running)
534
535 def finish(self):
536 """Clean up after running tests."""
537 os.remove(FvpDriverSPMC.HFTEST_CMD_FILE)
David Brazdil2df24082019-09-05 11:55:08 +0100538
David Brazdil17e76652020-01-29 14:44:19 +0000539class SerialDriver(Driver):
540 """Driver which communicates with a device over the serial port."""
541
David Brazdil9d4ed962020-02-06 17:23:48 +0000542 def __init__(self, args, tty_file, baudrate, init_wait):
David Brazdil17e76652020-01-29 14:44:19 +0000543 Driver.__init__(self, args)
David Brazdil9d4ed962020-02-06 17:23:48 +0000544 self.tty_file = tty_file
545 self.baudrate = baudrate
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000546 self.pyserial = importlib.import_module("serial")
David Brazdild8013f92020-02-03 16:40:25 +0000547
David Brazdil9d4ed962020-02-06 17:23:48 +0000548 if init_wait:
David Brazdild8013f92020-02-03 16:40:25 +0000549 input("Press ENTER and then reset the device...")
David Brazdil17e76652020-01-29 14:44:19 +0000550
David Brazdil9d4ed962020-02-06 17:23:48 +0000551 def connect(self):
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000552 return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10)
David Brazdil9d4ed962020-02-06 17:23:48 +0000553
David Brazdil17e76652020-01-29 14:44:19 +0000554 def run(self, run_name, test_args, is_long_running):
555 """Communicate `test_args` to the device over the serial port."""
556 run_state = self.start_run(run_name)
557
David Brazdil9d4ed962020-02-06 17:23:48 +0000558 with self.connect() as ser:
David Brazdil17e76652020-01-29 14:44:19 +0000559 with open(run_state.log_path, "a") as f:
560 while True:
561 # Read one line from the serial port.
562 line = ser.readline().decode('utf-8')
563 if len(line) == 0:
564 # Timeout
565 run_state.set_ret_code(124)
566 input("Timeout. " +
567 "Press ENTER and then reset the device...")
568 break
569 # Write the line to the log file.
570 f.write(line)
571 if HFTEST_CTRL_GET_COMMAND_LINE in line:
572 # Device is waiting for `test_args`.
573 ser.write(test_args.encode('ascii'))
574 ser.write(b'\r')
575 elif HFTEST_CTRL_FINISHED in line:
576 # Device has finished running this test and will reboot.
577 break
578 return self.finish_run(run_state)
579
David Brazdil94fd1e92020-02-03 16:45:20 +0000580 def finish(self):
581 """Clean up after running tests."""
David Brazdil9d4ed962020-02-06 17:23:48 +0000582 with self.connect() as ser:
David Brazdil94fd1e92020-02-03 16:45:20 +0000583 while True:
584 line = ser.readline().decode('utf-8')
585 if len(line) == 0:
586 input("Timeout. Press ENTER and then reset the device...")
587 elif HFTEST_CTRL_GET_COMMAND_LINE in line:
588 # Device is waiting for a command. Instruct it to exit
589 # the test environment.
590 ser.write("exit".encode('ascii'))
591 ser.write(b'\r')
592 break
593
David Brazdil2df24082019-09-05 11:55:08 +0100594# Tuple used to return information about the results of running a set of tests.
595TestRunnerResult = collections.namedtuple("TestRunnerResult", [
596 "tests_run",
597 "tests_failed",
Andrew Walbranf9463922020-06-05 16:44:42 +0100598 "tests_skipped",
David Brazdil2df24082019-09-05 11:55:08 +0100599 ])
600
601
602class TestRunner:
603 """Class which communicates with a test platform to obtain a list of
604 available tests and driving their execution."""
605
J-Alves8cc7dbb2021-04-16 10:38:48 +0100606 def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100607 skip_long_running_tests, force_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100608 self.artifacts = artifacts
609 self.driver = driver
J-Alves8cc7dbb2021-04-16 10:38:48 +0100610 self.test_set_up = test_set_up
David Brazdil3cc24aa2019-09-27 10:24:41 +0100611 self.skip_long_running_tests = skip_long_running_tests
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100612 self.force_long_running = force_long_running
David Brazdil2df24082019-09-05 11:55:08 +0100613
614 self.suite_re = re.compile(suite_regex or ".*")
615 self.test_re = re.compile(test_regex or ".*")
616
617 def extract_hftest_lines(self, raw):
618 """Extract hftest-specific lines from a raw output from an invocation
619 of the test platform."""
620 lines = []
J-Alves07be7bb2021-04-13 11:09:12 +0100621 lines_to_process = raw.splitlines()
622
623 try:
624 # If logs have logs of more than one VM, the loop below to extract
625 # lines won't work. Thus, extracting between starting and ending
626 # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED.
627 hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1
628 hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED)
629 except ValueError:
630 hftest_start = 0
631 hftest_end = len(lines_to_process)
632
633 lines_to_process = lines_to_process[hftest_start : hftest_end]
634
635 for line in lines_to_process:
J-Alves3dbb8562020-12-01 10:45:37 +0000636 match = re.search(f"^VM \d+: ", line)
637 if match is not None:
638 line = line[match.end():]
David Brazdil2df24082019-09-05 11:55:08 +0100639 if line.startswith(HFTEST_LOG_PREFIX):
640 lines.append(line[len(HFTEST_LOG_PREFIX):])
641 return lines
642
643 def get_test_json(self):
644 """Invoke the test platform and request a JSON of available test and
645 test suites."""
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100646 out = self.driver.run("json", "json", self.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100647 hf_out = "\n".join(self.extract_hftest_lines(out))
648 try:
649 return json.loads(hf_out)
650 except ValueError as e:
651 print(out)
652 raise e
653
654 def collect_results(self, fn, it, xml_node):
655 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
656 Insert "tests" and "failures" nodes to `xml_node`."""
657 tests_run = 0
658 tests_failed = 0
Andrew Walbranf9463922020-06-05 16:44:42 +0100659 tests_skipped = 0
Andrew Walbran42bf2842020-06-05 18:50:19 +0100660 start_time = time.perf_counter()
David Brazdil2df24082019-09-05 11:55:08 +0100661 for i in it:
662 sub_result = fn(i)
663 assert(sub_result.tests_run >= sub_result.tests_failed)
664 tests_run += sub_result.tests_run
665 tests_failed += sub_result.tests_failed
Andrew Walbranf9463922020-06-05 16:44:42 +0100666 tests_skipped += sub_result.tests_skipped
Andrew Walbran42bf2842020-06-05 18:50:19 +0100667 elapsed_time = time.perf_counter() - start_time
David Brazdil2df24082019-09-05 11:55:08 +0100668
Andrew Walbranf9463922020-06-05 16:44:42 +0100669 xml_node.set("tests", str(tests_run + tests_skipped))
David Brazdil2df24082019-09-05 11:55:08 +0100670 xml_node.set("failures", str(tests_failed))
Andrew Walbranf9463922020-06-05 16:44:42 +0100671 xml_node.set("skipped", str(tests_skipped))
Andrew Walbran42bf2842020-06-05 18:50:19 +0100672 xml_node.set("time", str(elapsed_time))
Andrew Walbranf9463922020-06-05 16:44:42 +0100673 return TestRunnerResult(tests_run, tests_failed, tests_skipped)
David Brazdil2df24082019-09-05 11:55:08 +0100674
675 def is_passed_test(self, test_out):
676 """Parse the output of a test and return True if it passed."""
677 return \
678 len(test_out) > 0 and \
679 test_out[-1] == HFTEST_LOG_FINISHED and \
680 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
681
Andrew Walbranf9463922020-06-05 16:44:42 +0100682 def get_failure_message(self, test_out):
683 """Parse the output of a test and return the message of the first
684 assertion failure."""
685 for i, line in enumerate(test_out):
686 if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out):
687 # The assertion message is on the line after the 'Failure:'
688 return test_out[i + 1].strip()
689
690 return None
691
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000692 def get_log_name(self, suite, test):
693 """Returns a string with a generated log name for the test."""
694 log_name = ""
695
696 cpu = self.driver.args.cpu
697 if cpu:
698 log_name += cpu + "."
699
700 log_name += suite["name"] + "." + test["name"]
701
702 return log_name
703
David Brazdil2df24082019-09-05 11:55:08 +0100704 def run_test(self, suite, test, suite_xml):
705 """Invoke the test platform and request to run a given `test` in given
706 `suite`. Create a new XML node with results under `suite_xml`.
707 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100708 if not self.test_re.match(test["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100709 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100710
711 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100712 test_xml.set("name", test["name"])
713 test_xml.set("classname", suite["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100714
715 if self.skip_long_running_tests and test["is_long_running"]:
716 print(" SKIP", test["name"])
717 test_xml.set("status", "notrun")
718 skipped_xml = ET.SubElement(test_xml, "skipped")
719 skipped_xml.set("message", "Long running")
720 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1)
721
722 print(" RUN", test["name"])
723 log_name = self.get_log_name(suite, test)
724
David Brazdil2df24082019-09-05 11:55:08 +0100725 test_xml.set("status", "run")
726
Andrew Walbran42bf2842020-06-05 18:50:19 +0100727 start_time = time.perf_counter()
Andrew Walbranf9463922020-06-05 16:44:42 +0100728 out = self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100729 log_name, "run {} {}".format(suite["name"], test["name"]),
Andrew Walbranf9463922020-06-05 16:44:42 +0100730 test["is_long_running"] or self.force_long_running)
731 hftest_out = self.extract_hftest_lines(out)
Andrew Walbran42bf2842020-06-05 18:50:19 +0100732 elapsed_time = time.perf_counter() - start_time
733
734 test_xml.set("time", str(elapsed_time))
David Brazdil2df24082019-09-05 11:55:08 +0100735
Andrew Walbranf9463922020-06-05 16:44:42 +0100736 system_out_xml = ET.SubElement(test_xml, "system-out")
737 system_out_xml.text = out
738
739 if self.is_passed_test(hftest_out):
David Brazdil2df24082019-09-05 11:55:08 +0100740 print(" PASS")
Andrew Walbranf9463922020-06-05 16:44:42 +0100741 return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100742 else:
David Brazdil623b6812019-09-09 11:41:08 +0100743 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100744 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Walbranf9463922020-06-05 16:44:42 +0100745 failure_message = self.get_failure_message(hftest_out) or "Test failed"
746 failure_xml.set("message", failure_message)
747 failure_xml.text = '\n'.join(hftest_out)
748 return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100749
750 def run_suite(self, suite, xml):
751 """Invoke the test platform and request to run all matching tests in
752 `suite`. Create new XML nodes with results under `xml`.
753 Suite skipped if it does not match the regex given to constructor."""
754 if not self.suite_re.match(suite["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100755 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100756
757 print(" SUITE", suite["name"])
758 suite_xml = ET.SubElement(xml, "testsuite")
759 suite_xml.set("name", suite["name"])
Andrew Walbran16ae62e2020-06-05 18:27:46 +0100760 properties_xml = ET.SubElement(suite_xml, "properties")
761
762 property_xml = ET.SubElement(properties_xml, "property")
763 property_xml.set("name", "driver")
764 property_xml.set("value", type(self.driver).__name__)
765
766 if self.driver.args.cpu:
767 property_xml = ET.SubElement(properties_xml, "property")
768 property_xml.set("name", "cpu")
769 property_xml.set("value", self.driver.args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100770
771 return self.collect_results(
772 lambda test: self.run_test(suite, test, suite_xml),
773 suite["tests"],
774 suite_xml)
775
776 def run_tests(self):
777 """Run all suites and tests matching regexes given to constructor.
778 Write results to sponge log XML. Return the number of run and failed
779 tests."""
780
781 test_spec = self.get_test_json()
782 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
783
784 xml = ET.Element("testsuites")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100785 xml.set("name", self.test_set_up)
David Brazdil2df24082019-09-05 11:55:08 +0100786 xml.set("timestamp", timestamp)
787
788 result = self.collect_results(
789 lambda suite: self.run_suite(suite, xml),
790 test_spec["suites"],
791 xml)
792
793 # Write XML to file.
David Brazdilee5e25d2020-01-24 14:17:45 +0000794 ET.ElementTree(xml).write(self.artifacts.sponge_xml_path,
795 encoding='utf-8', xml_declaration=True)
David Brazdil2df24082019-09-05 11:55:08 +0100796
797 if result.tests_failed > 0:
798 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
799 "tests failed")
800 elif result.tests_run > 0:
801 print(" PASS: all", result.tests_run, "tests passed")
802
David Brazdil94fd1e92020-02-03 16:45:20 +0000803 # Let the driver clean up.
804 self.driver.finish()
805
David Brazdil2df24082019-09-05 11:55:08 +0100806 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100807
Andrew Scullbc7189d2018-08-14 09:35:13 +0100808def Main():
809 parser = argparse.ArgumentParser()
J-Alves8cc7dbb2021-04-16 10:38:48 +0100810 parser.add_argument("--hypervisor")
811 parser.add_argument("--spmc")
Andrew Scull23e93a82018-10-26 14:56:04 +0100812 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100813 parser.add_argument("--out_initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100814 parser.add_argument("--out_partitions")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000815 parser.add_argument("--initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100816 parser.add_argument("--partitions_json")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100817 parser.add_argument("--suite")
818 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000819 parser.add_argument("--vm_args")
David Brazdil17e76652020-01-29 14:44:19 +0000820 parser.add_argument("--driver", default="qemu")
821 parser.add_argument("--serial-dev", default="/dev/ttyUSB0")
822 parser.add_argument("--serial-baudrate", type=int, default=115200)
David Brazdild8013f92020-02-03 16:40:25 +0000823 parser.add_argument("--serial-no-init-wait", action="store_true")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100824 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100825 parser.add_argument("--force-long-running", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000826 parser.add_argument("--cpu",
827 help="Selects the CPU configuration for the run environment.")
Andrew Walbranf636b842020-01-10 11:46:12 +0000828 parser.add_argument("--tfa", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100829 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100830
J-Alves8cc7dbb2021-04-16 10:38:48 +0100831 # Create class which will manage all test artifacts.
832 if args.hypervisor and args.spmc:
833 test_set_up = "hypervisor_and_spmc"
834 elif args.hypervisor:
835 test_set_up = "hypervisor"
836 elif args.spmc:
837 test_set_up = "spmc"
838 else:
839 raise Exception("No Hafnium image provided!\n")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100840
J-Alves8cc7dbb2021-04-16 10:38:48 +0100841 initrd = None
842 if args.hypervisor and args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100843 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
844 initrd = os.path.join(initrd_dir, "initrd.img")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100845 test_set_up += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000846 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100847
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100848 partitions = None
849 if args.driver == "fvp" and args.partitions_json is not None:
850 partitions_dir = os.path.join(args.out_partitions, "obj", args.partitions_json)
851 partitions = json.load(open(partitions_dir, "r"))
852
David Brazdil2df24082019-09-05 11:55:08 +0100853 # Create class which will manage all test artifacts.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100854 log_dir = os.path.join(args.log, test_set_up)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100855 artifacts = ArtifactsManager(log_dir)
David Brazdil2df24082019-09-05 11:55:08 +0100856
857 # Create a driver for the platform we want to test on.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100858 driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd,
859 vm_args, args.cpu, partitions)
David Brazdil17e76652020-01-29 14:44:19 +0000860
J-Alves8cc7dbb2021-04-16 10:38:48 +0100861 if args.spmc:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100862 if args.driver != "fvp":
863 raise Exception("Secure tests can only run with fvp driver")
864 driver = FvpDriverSPMC(driver_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100865 elif args.hypervisor:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100866 if args.driver == "qemu":
J-Alves8cc7dbb2021-04-16 10:38:48 +0100867 out = os.path.dirname(args.hypervisor)
868 driver = QemuDriver(driver_args, out, args.tfa)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100869 elif args.driver == "fvp":
870 driver = FvpDriverHypervisor(driver_args)
871 elif args.driver == "serial":
872 driver = SerialDriver(driver_args, args.serial_dev,
873 args.serial_baudrate, not args.serial_no_init_wait)
874 else:
875 raise Exception("Unknown driver name: {}".format(args.driver))
J-Alves8cc7dbb2021-04-16 10:38:48 +0100876 else:
877 raise Exception("No Hafnium image provided!\n")
David Brazdil2df24082019-09-05 11:55:08 +0100878
879 # Create class which will drive test execution.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100880 runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100881 args.skip_long_running_tests, args.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100882
883 # Run tests.
884 runner_result = runner.run_tests()
885
886 # Print error message if no tests were run as this is probably unexpected.
887 # Return suitable error code.
888 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100889 print("Error: no tests match")
890 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100891 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100892 return 1
893 else:
David Brazdil2df24082019-09-05 11:55:08 +0100894 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100895
896if __name__ == "__main__":
897 sys.exit(Main())