blob: 24389120f39a1fcb30febc0bcf8b6b24691a7751 [file] [log] [blame]
jaypit02ea3cd062018-10-05 12:22:38 +05301/** @file
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +05302 * Copyright (c) 2018-2020, Arm Limited or its affiliates. All rights reserved.
jaypit02ea3cd062018-10-05 12:22:38 +05303 * SPDX-License-Identifier : Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16**/
17
18#include "val_framework.h"
19#include "val_interfaces.h"
20#include "val_dispatcher.h"
21#include "val_peripherals.h"
22#include "pal_interfaces_ns.h"
23#include "val_target.h"
24
25extern val_api_t val_api;
26extern psa_api_t psa_api;
27
28/* globals */
29test_status_buffer_t g_status_buffer;
30
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +053031#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +053032/**
33 * @brief Connect to given sid
34 @param -sid : RoT service id
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +053035 @param -version : version of RoT service
jaypit02ea3cd062018-10-05 12:22:38 +053036 @param -handle - return connection handle
37 * @return val_status_t
38 */
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +053039val_status_t val_ipc_connect(uint32_t sid, uint32_t version, psa_handle_t *handle )
jaypit02ea3cd062018-10-05 12:22:38 +053040{
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +053041 *handle = psa_connect(sid, version);
jaypit02ea3cd062018-10-05 12:22:38 +053042
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +053043 if (*handle > 0)
44 return VAL_STATUS_SUCCESS;
jaypit02ea3cd062018-10-05 12:22:38 +053045
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +053046 return VAL_STATUS_CONNECTION_FAILED;
jaypit02ea3cd062018-10-05 12:22:38 +053047}
48
49/**
50 * @brief Call a connected Root of Trust Service.@n
51 * The caller must provide an array of ::psa_invec_t structures as the input payload.
52 *
53 * @param handle Handle for the connection.
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +053054 * @param type Request type
jaypit02ea3cd062018-10-05 12:22:38 +053055 * @param in_vec Array of psa_invec structures.
56 * @param in_len Number of psa_invec structures in in_vec.
57 * @param out_vec Array of psa_outvec structures for optional Root of Trust Service response.
58 * @param out_len Number of psa_outvec structures in out_vec.
59 * @return val_status_t
60 */
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +053061val_status_t val_ipc_call(psa_handle_t handle,
62 int32_t type,
63 const psa_invec *in_vec,
64 size_t in_len,
65 psa_outvec *out_vec,
66 size_t out_len)
jaypit02ea3cd062018-10-05 12:22:38 +053067{
68 psa_status_t call_status = PSA_SUCCESS;
69
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +053070 call_status = psa_call(handle, type, in_vec, in_len, out_vec, out_len);
jaypit02ea3cd062018-10-05 12:22:38 +053071
72 if (call_status != PSA_SUCCESS)
73 {
74 return VAL_STATUS_CALL_FAILED;
75 }
76
77 return VAL_STATUS_SUCCESS;
78}
79
80/**
81 * @brief Close a connection to a Root of Trust Service.
82 * Sends the PSA_IPC_DISCONNECT message to the Root of Trust Service so it can clean up resources.
83 *
84 * @param handle Handle for the connection.
85 * @return void
86 */
87void val_ipc_close(psa_handle_t handle)
88{
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +053089 psa_close(handle);
jaypit02ea3cd062018-10-05 12:22:38 +053090}
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +053091#endif
92
jaypit02ea3cd062018-10-05 12:22:38 +053093/**
94 @brief - This function executes given list of tests from non-secure sequentially
95 This covers non-secure to secure IPC API scenario
96 @param - test_num : Test_num
97 @param - tests_list : list of tests to be executed
98 @param - server_hs : Initiate a server handshake
99 @return - val_status_t
100**/
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530101val_status_t val_execute_non_secure_tests(uint32_t test_num, const client_test_t *tests_list,
jaypit02ea3cd062018-10-05 12:22:38 +0530102 bool_t server_hs)
103{
104 val_status_t status = VAL_STATUS_SUCCESS;
105 val_status_t test_status = VAL_STATUS_SUCCESS;
106 boot_t boot;
jaypit02ea3cd062018-10-05 12:22:38 +0530107 uint32_t i = 1;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530108#ifdef IPC
109 psa_handle_t handle;
jaypit02ea3cd062018-10-05 12:22:38 +0530110 test_info_t test_info;
111
112 test_info.test_num = test_num;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530113#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530114
115 status = val_get_boot_flag(&boot.state);
116 if (VAL_ERROR(status))
117 {
118 val_set_status(RESULT_FAIL(status));
119 return status;
120 }
121
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530122 if (boot.state == BOOT_NOT_EXPECTED || boot.state == BOOT_EXPECTED_REENTER_TEST
123 || boot.state == BOOT_EXPECTED_CONT_TEST_EXEC)
jaypit02ea3cd062018-10-05 12:22:38 +0530124 {
jaypit02ea3cd062018-10-05 12:22:38 +0530125 while (tests_list[i] != NULL)
126 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530127 /*
128 * Reboot have been expected by test in previous ns run,
129 * consider previous run pass and jump to second test function
130 * of the same test if available.
131 */
132 if ((boot.state == BOOT_EXPECTED_REENTER_TEST) && (i == 1))
133 {
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530134 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530135 i++;
136 continue;
137 }
138
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530139 if (boot.state != BOOT_EXPECTED_CONT_TEST_EXEC)
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530140 {
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530141 status = val_set_boot_flag(BOOT_NOT_EXPECTED);
142 if (VAL_ERROR(status))
143 {
144 return status;
145 }
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530146 }
147
148 if (i == 1)
149 val_print(PRINT_TEST,"[Info] Executing tests from non-secure\n", 0);
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530150#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +0530151 if (server_hs == TRUE)
152 {
153 /* Handshake with server tests */
154 test_info.block_num = i;
155 status = val_execute_secure_test_func(&handle, test_info,
156 SERVER_TEST_DISPATCHER_SID);
157 if (VAL_ERROR(status))
158 {
159 val_set_status(RESULT_FAIL(status));
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530160 val_print(PRINT_DEBUG, "[Check %d] START\n", i);
jaypit02ea3cd062018-10-05 12:22:38 +0530161 return status;
162 }
163 else
164 {
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530165 val_print(PRINT_DEBUG, "[Check %d] START\n", i);
jaypit02ea3cd062018-10-05 12:22:38 +0530166 }
167 }
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530168#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530169 /* Execute client tests */
Gowtham Siddarthb1cd50f2019-08-21 11:50:26 +0530170 test_status = tests_list[i](CALLER_NONSECURE);
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530171#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +0530172 if (server_hs == TRUE)
173 {
174 /* Retrive Server test status */
175 status = val_get_secure_test_result(&handle);
176 }
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530177#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530178 status = test_status ? test_status:status;
Gowtham Siddarth47223082019-01-17 09:59:50 +0530179 if (IS_TEST_SKIP(status))
180 {
181 val_set_status(status);
182 if (server_hs == TRUE)
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530183 val_print(PRINT_DEBUG, "[Check %d] SKIPPED\n", i);
Gowtham Siddarth47223082019-01-17 09:59:50 +0530184 return status;
185 }
186 else if (VAL_ERROR(status))
jaypit02ea3cd062018-10-05 12:22:38 +0530187 {
188 val_set_status(RESULT_FAIL(status));
jaypit02ac23b5b2018-11-02 13:10:19 +0530189 if (server_hs == TRUE)
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530190 val_print(PRINT_DEBUG, "[Check %d] FAILED\n", i);
Gowtham Siddarth47223082019-01-17 09:59:50 +0530191
jaypit02ea3cd062018-10-05 12:22:38 +0530192 return status;
193 }
194 else
195 {
jaypit02ac23b5b2018-11-02 13:10:19 +0530196 if (server_hs == TRUE)
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530197 val_print(PRINT_DEBUG, "[Check %d] PASSED\n", i);
jaypit02ea3cd062018-10-05 12:22:38 +0530198 }
Gowtham Siddarth47223082019-01-17 09:59:50 +0530199
jaypit02ea3cd062018-10-05 12:22:38 +0530200 i++;
201 }
202 }
203 else
204 {
205 /* If we are here means, we are in second run of this test */
206 status = VAL_STATUS_SUCCESS;
207 if (boot.state != BOOT_EXPECTED_S)
208 {
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530209 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530210 }
211 }
212 return status;
213}
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530214
215#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +0530216/**
217 @brief - This function is used to switch to client_partition.c
218 where client tests will be executed to cover secure to secure
219 IPC scenario.
220 @param - test_num : Test_num
221 @return - val_status_t
222**/
223val_status_t val_switch_to_secure_client(uint32_t test_num)
224{
225 val_status_t status = VAL_STATUS_SUCCESS;
226 boot_t boot;
227 psa_handle_t handle;
228 test_info_t test_info;
229
230 test_info.test_num = test_num;
231 test_info.block_num = 1;
232
233 status = val_get_boot_flag(&boot.state);
234 if (VAL_ERROR(status))
235 {
236 goto exit;
237 }
238
239 if (boot.state != BOOT_EXPECTED_S)
240 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530241 /*
242 * Reboot have been expected by test in previous s run,
243 * consider previous run pass and jump to second test function
244 * of the same test if available.
245 */
246 if (boot.state == BOOT_EXPECTED_REENTER_TEST)
247 {
248 test_info.block_num++;
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530249 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530250 }
251
jaypit02ea3cd062018-10-05 12:22:38 +0530252 status = val_set_boot_flag(BOOT_NOT_EXPECTED);
253 if (VAL_ERROR(status))
254 {
255 goto exit;
256 }
257
258 /* switch to secure client */
259 status = val_execute_secure_test_func(&handle, test_info, CLIENT_TEST_DISPATCHER_SID);
260 if (VAL_ERROR(status))
261 {
262 goto exit;
263 }
264
265 /* Retrive secure client test status */
266 status = val_get_secure_test_result(&handle);
Gowtham Siddarth47223082019-01-17 09:59:50 +0530267 if (IS_TEST_SKIP(status))
268 {
269 val_set_status(status);
270 return status;
271 }
jaypit02ea3cd062018-10-05 12:22:38 +0530272 if (VAL_ERROR(status))
273 {
274 goto exit;
275 }
276 return status;
277 }
278 else
279 {
280 /* If we are here means, we are in third run of this test */
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530281 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530282 return VAL_STATUS_SUCCESS;
283 }
284
285exit:
286 val_set_status(RESULT_FAIL(status));
287 return status;
288}
289
290/**
291 @brief - This function is used to handshake between:
292 - nonsecure client fn to server test fn
293 - secure client fn and server test fn
294 - nonsecure client fn to secure client test fn
295 @param - handle : handle returned while connecting given sid
296 @param - test_info : Test_num and block_num to be executed
297 @param - sid : RoT service to be connected. Partition dispatcher sid
298 @return - val_status_t
299**/
300val_status_t val_execute_secure_test_func(psa_handle_t *handle, test_info_t test_info, uint32_t sid)
301{
302 uint32_t test_data;
303 val_status_t status = VAL_STATUS_SUCCESS;
304 psa_status_t status_of_call = PSA_SUCCESS;
305
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530306 *handle = psa_connect(sid, 1);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530307 if (*handle > 0)
308 {
309 test_data = ((uint32_t)(test_info.test_num) |((uint32_t)(test_info.block_num) << BLOCK_NUM_POS)
310 | ((uint32_t)(TEST_EXECUTE_FUNC) << ACTION_POS));
311 psa_invec data[1] = {{&test_data, sizeof(test_data)}};
312
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530313 status_of_call = psa_call(*handle, 0, data, 1, NULL, 0);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530314 if (status_of_call != PSA_SUCCESS)
315 {
316 status = VAL_STATUS_CALL_FAILED;
317 val_print(PRINT_ERROR, "Call to dispatch SF failed. Status=%x\n", status_of_call);
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530318 psa_close(*handle);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530319 }
320 }
321 else
jaypit02ea3cd062018-10-05 12:22:38 +0530322 {
323 val_print(PRINT_ERROR, "Could not connect SID. Handle=%x\n", *handle);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530324 status = VAL_STATUS_CONNECTION_FAILED;
jaypit02ea3cd062018-10-05 12:22:38 +0530325 }
Gowtham Siddarth47223082019-01-17 09:59:50 +0530326
jaypit02ea3cd062018-10-05 12:22:38 +0530327 return status;
328}
329
330/**
331 @brief - This function is used to retrive the status of previously connected test function
332 using val_execute_secure_test_func
333 @param - handle : handle of server function. Handle of Partition dispatcher sid
334 @return - The status of test functions
335**/
336val_status_t val_get_secure_test_result(psa_handle_t *handle)
337{
338 uint32_t test_data;
339 val_status_t status = VAL_STATUS_SUCCESS;
340 psa_status_t status_of_call = PSA_SUCCESS;
341
342 test_data = (TEST_RETURN_RESULT << ACTION_POS);
343
344 psa_outvec resp = {&status, sizeof(status)};
345 psa_invec data[1] = {{&test_data, sizeof(test_data)}};
346
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530347 status_of_call = psa_call(*handle, 0, data, 1, &resp, 1);
jaypit02ea3cd062018-10-05 12:22:38 +0530348 if (status_of_call != PSA_SUCCESS)
349 {
350 status = VAL_STATUS_CALL_FAILED;
351 val_print(PRINT_ERROR, "Call to dispatch SF failed. Status=%x\n", status_of_call);
352 }
353
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530354 psa_close(*handle);
jaypit02ea3cd062018-10-05 12:22:38 +0530355 return status;
356}
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530357#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530358
359/**
360 @brief - Parses input status for a given test and
361 outputs appropriate information on the console
362 @return - Test state
363**/
364uint32_t val_report_status(void)
365{
366 uint32_t status, state;
367
368 status = val_get_status();
369
370 state = (status >> TEST_STATE_BIT) & TEST_STATE_MASK;
371 status = status & TEST_STATUS_MASK;
372
373 switch (state)
374 {
375 case TEST_START:
376 state = TEST_FAIL;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530377 val_print(PRINT_ALWAYS, "\nTEST RESULT: FAILED (Error Code=0x%x)\n",
jaypit02ea3cd062018-10-05 12:22:38 +0530378 VAL_STATUS_INIT_FAILED);
379 break;
380
381 case TEST_END:
382 state = TEST_PASS;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530383 val_print(PRINT_ALWAYS, "\nTEST RESULT: PASSED\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530384 break;
385
386 case TEST_FAIL:
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530387 val_print(PRINT_ALWAYS, "\nTEST RESULT: FAILED (Error Code=0x%x)\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530388 break;
389
390 case TEST_SKIP:
Gowtham Siddarth47223082019-01-17 09:59:50 +0530391 state = TEST_SKIP;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530392 val_print(PRINT_ALWAYS, "\nTEST RESULT: SKIPPED (Skip Code=0x%x)\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530393 break;
394
395 case TEST_PENDING:
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530396 val_print(PRINT_ALWAYS, "\nTEST RESULT: SIM ERROR (Error Code=0x%x)\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530397 break;
398
399 default:
400 state = TEST_FAIL;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530401 val_print(PRINT_ALWAYS, "\nTEST RESULT: FAILED(Error Code=0x%x)\n", VAL_STATUS_INVALID);
jaypit02ea3cd062018-10-05 12:22:38 +0530402 break;
403
404 }
405
406 val_print(PRINT_ALWAYS, "\n******************************************\n", 0);
407 return state;
408}
409
410/**
411 @brief - Records the state and status of test
412 @return - val_status_t
413**/
414val_status_t val_set_status(uint32_t status)
415{
416 g_status_buffer.state = ((status >> TEST_STATE_BIT) & TEST_STATE_MASK);
417 g_status_buffer.status = (status & TEST_STATUS_MASK);
418
419 return VAL_STATUS_SUCCESS;
420}
421
422/**
423 @brief - Updates the state and status for a given test
424 @return - test status
425**/
426uint32_t val_get_status(void)
427{
428 return ((g_status_buffer.state) << TEST_STATE_BIT) | (g_status_buffer.status);
429}
430
431/*
432 @brief - This function checks if the input status argument is an error.
433 On error, we print the checkpoint value and set the status.
434 @param - checkpoint : Test debug checkpoint
435 - val_status_t : Test status
436 @return - returns the input status back to the program.
437*/
438
439val_status_t val_err_check_set(uint32_t checkpoint, val_status_t status)
440{
441 if (VAL_ERROR(status))
442 {
443 val_print(PRINT_ERROR, "\tCheckpoint %d : ", checkpoint);
444 val_print(PRINT_ERROR, "Error Code=0x%x \n", status);
445 val_set_status(RESULT_FAIL(status));
446 }
447 else
448 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530449 status = (val_get_status() & TEST_STATUS_MASK);
jaypit02ea3cd062018-10-05 12:22:38 +0530450 if (VAL_ERROR(status))
451 {
452 val_print(PRINT_ERROR, "\tCheckpoint %d : ", checkpoint);
453 val_print(PRINT_ERROR, "Error Code=0x%x \n", status);
454 }
455 else
456 {
457 val_print(PRINT_DEBUG, "\tCheckpoint %d \n", checkpoint);
458 }
459 }
460 return status;
461}
462
463/**
464 @brief This API prints the test number, description and
465 sets the test state to TEST_START on successful execution.
466 @param test_num :unique number identifying this test
467 @param desc :brief description of the test
468 @param test_bitfield :Addition test info such as
469 - test isolation level requirement
470 - Watchdog timeout type
471 @return void
472**/
473
474void val_test_init(uint32_t test_num, char8_t *desc, uint32_t test_bitfield)
475{
476 val_status_t status = VAL_STATUS_SUCCESS;
jaypit02ea3cd062018-10-05 12:22:38 +0530477
478 /*global init*/
cherat013f028722019-02-18 14:50:37 +0530479 g_status_buffer.state = TEST_FAIL;
jaypit02ea3cd062018-10-05 12:22:38 +0530480 g_status_buffer.status = VAL_STATUS_INVALID;
481
482 val_print(PRINT_ALWAYS, "\nTEST: %d | DESCRIPTION: ", test_num);
483 val_print(PRINT_ALWAYS, desc, 0);
484
485 /* common skip logic */
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530486 if (PLATFORM_PSA_ISOLATION_LEVEL < GET_TEST_ISOLATION_LEVEL(test_bitfield))
jaypit02ea3cd062018-10-05 12:22:38 +0530487 {
488 val_set_status(RESULT_SKIP(VAL_STATUS_ISOLATION_LEVEL_NOT_SUPP));
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530489 val_print(PRINT_ALWAYS, "\tSkipping test. Required isolation level is not supported\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530490 return;
491 }
492
Vinay Kumar Kotegowder9982f902019-07-15 09:13:54 +0530493#ifdef WATCHDOG_AVAILABLE
jaypit02ea3cd062018-10-05 12:22:38 +0530494 /* Initialise watchdog */
495 status = val_wd_timer_init(GET_WD_TIMOUT_TYPE(test_bitfield));
496 if (VAL_ERROR(status))
497 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530498 val_print(PRINT_ERROR, "\tval_wd_timer_init failed Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530499 return;
500 }
501
502 /* Enable watchdog Timer */
503 status = val_wd_timer_enable();
504 if (VAL_ERROR(status))
505 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530506 val_print(PRINT_ERROR, "\tval_wd_timer_enable failed Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530507 return;
508 }
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530509#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530510
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530511 val_set_status(RESULT_START(status));
jaypit02ea3cd062018-10-05 12:22:38 +0530512 return;
513}
514
515/**
516 @brief This API sets the test state to TEST_END if test is successfuly passed.
517 @param none
518 @return none
519**/
520
521void val_test_exit(void)
522{
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530523 val_status_t status = VAL_STATUS_SUCCESS;
jaypit02ea3cd062018-10-05 12:22:38 +0530524
Vinay Kumar Kotegowder9982f902019-07-15 09:13:54 +0530525#ifdef WATCHDOG_AVAILABLE
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530526 status = val_wd_timer_disable();
527 if (VAL_ERROR(status))
528 {
529 val_print(PRINT_ERROR, "\tval_wd_timer_disable failed Error=0x%x\n", status);
530 val_set_status(RESULT_FAIL(status));
531 return;
532 }
533#endif
534
Gowtham Siddarth47223082019-01-17 09:59:50 +0530535 status = val_get_status();
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530536
jaypit02ea3cd062018-10-05 12:22:38 +0530537 /* return if test skipped or failed */
Gowtham Siddarth47223082019-01-17 09:59:50 +0530538 if (IS_TEST_FAIL(status) || IS_TEST_SKIP(status))
jaypit02ea3cd062018-10-05 12:22:38 +0530539 {
Gowtham Siddarth47223082019-01-17 09:59:50 +0530540 return;
jaypit02ea3cd062018-10-05 12:22:38 +0530541 }
Gowtham Siddarth47223082019-01-17 09:59:50 +0530542 else
543 {
544 val_set_status(RESULT_END(VAL_STATUS_SUCCESS));
545 }
jaypit02ea3cd062018-10-05 12:22:38 +0530546}
547
548/**
549 @brief - This function returns the test ID of the last test that was run
550 @param - test_id address
551 @return - val_status_t
552**/
553val_status_t val_get_last_run_test_id(test_id_t *test_id)
554{
555 val_status_t status;
556 test_count_t test_count;
557 boot_t boot;
558 int i = 0, intermediate_boot = 0;
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530559 boot_state_t boot_state[] = {BOOT_NOT_EXPECTED,
560 BOOT_EXPECTED_NS,
561 BOOT_EXPECTED_S,
562 BOOT_EXPECTED_BUT_FAILED,
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530563 BOOT_EXPECTED_REENTER_TEST,
564 BOOT_EXPECTED_CONT_TEST_EXEC
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530565 };
jaypit02ea3cd062018-10-05 12:22:38 +0530566
567 status = val_get_boot_flag(&boot.state);
568 if (VAL_ERROR(status))
569 {
570 return status;
571 }
572
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530573 val_print(PRINT_INFO, "\n\tboot.state=0x%x", boot.state);
574
jaypit02ea3cd062018-10-05 12:22:38 +0530575 for (i = 0; i < (sizeof(boot_state)/sizeof(boot_state[0])); i++)
576 {
577 if (boot.state == boot_state[i])
578 {
579 intermediate_boot = 1;
580 break;
581 }
582 }
583
584 if (!intermediate_boot)
585 {
586 /* First boot. Initiliase necessary data structure */
587 status = val_set_boot_flag(BOOT_UNKNOWN);
588 if (VAL_ERROR(status))
589 {
590 return status;
591 }
592
593 *test_id = VAL_INVALID_TEST_ID;
594 status = val_nvmem_write(VAL_NVMEM_OFFSET(NV_TEST_ID_PREVIOUS),
595 test_id, sizeof(test_id_t));
596 if (VAL_ERROR(status))
597 {
598 val_print(PRINT_ALWAYS, "\n\tNVMEM write error", 0);
599 return status;
600 }
601
602 test_count.pass_cnt = 0;
603 test_count.fail_cnt = 0;
604 test_count.skip_cnt = 0;
605 test_count.sim_error_cnt = 0;
606
607 status = val_nvmem_write(VAL_NVMEM_OFFSET(NV_TEST_CNT),
608 &test_count, sizeof(test_count_t));
609 if (VAL_ERROR(status))
610 {
611 val_print(PRINT_ERROR, "\n\tNVMEM write error", 0);
612 return status;
613 }
614 }
615
616 status = val_nvmem_read(VAL_NVMEM_OFFSET(NV_TEST_ID_PREVIOUS), test_id, sizeof(test_id_t));
617 if (VAL_ERROR(status))
618 {
619 val_print(PRINT_ERROR, "\n\tNVMEM read error", 0);
620 }
621
622 val_print(PRINT_INFO, "In val_get_last_run_test_id, test_id=%x\n", *test_id);
623 return status;
624}
625
626/**
627 @brief - This function sets the given boot.state value to corresponding
628 boot NVMEM location
629 @param - state: boot_state_t
630 @return - val_status_t
631**/
632val_status_t val_set_boot_flag(boot_state_t state)
633{
634 boot_t boot;
635 val_status_t status;
636
637 boot.state = state;
638 status = val_nvmem_write(VAL_NVMEM_OFFSET(NV_BOOT), &boot, sizeof(boot_t));
639 if (VAL_ERROR(status))
640 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530641 val_print(PRINT_ERROR, "\tval_nvmem_write failed. Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530642 return status;
643 }
644 return status;
645}
646
647/**
648 @brief - This function returns boot.state value available in boot NVMEM location
649 @param - state address
650 @return - val_status_t
651**/
652val_status_t val_get_boot_flag(boot_state_t *state)
653{
654 boot_t boot;
655 val_status_t status;
656
657 status = val_nvmem_read(VAL_NVMEM_OFFSET(NV_BOOT), &boot, sizeof(boot_t));
658 if (VAL_ERROR(status))
659 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530660 val_print(PRINT_ERROR, "\tval_nvmem_read failed. Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530661 return status;
662 }
663 *state = boot.state;
664 return status;
665}