blob: 9e7df32f5af40c417b7bc3e7210c5579ea0e2dcd [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 Kotegowder59a21912020-06-14 19:51:54 +0530113#else
114 (void)test_num;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530115#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530116
117 status = val_get_boot_flag(&boot.state);
118 if (VAL_ERROR(status))
119 {
120 val_set_status(RESULT_FAIL(status));
121 return status;
122 }
123
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530124 if (boot.state == BOOT_NOT_EXPECTED || boot.state == BOOT_EXPECTED_REENTER_TEST
125 || boot.state == BOOT_EXPECTED_CONT_TEST_EXEC)
jaypit02ea3cd062018-10-05 12:22:38 +0530126 {
jaypit02ea3cd062018-10-05 12:22:38 +0530127 while (tests_list[i] != NULL)
128 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530129 /*
130 * Reboot have been expected by test in previous ns run,
131 * consider previous run pass and jump to second test function
132 * of the same test if available.
133 */
134 if ((boot.state == BOOT_EXPECTED_REENTER_TEST) && (i == 1))
135 {
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530136 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530137 i++;
138 continue;
139 }
140
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530141 if (boot.state != BOOT_EXPECTED_CONT_TEST_EXEC)
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530142 {
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530143 status = val_set_boot_flag(BOOT_NOT_EXPECTED);
144 if (VAL_ERROR(status))
145 {
146 return status;
147 }
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530148 }
149
150 if (i == 1)
151 val_print(PRINT_TEST,"[Info] Executing tests from non-secure\n", 0);
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530152#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +0530153 if (server_hs == TRUE)
154 {
155 /* Handshake with server tests */
156 test_info.block_num = i;
157 status = val_execute_secure_test_func(&handle, test_info,
158 SERVER_TEST_DISPATCHER_SID);
159 if (VAL_ERROR(status))
160 {
161 val_set_status(RESULT_FAIL(status));
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530162 val_print(PRINT_DEBUG, "[Check %d] START\n", i);
jaypit02ea3cd062018-10-05 12:22:38 +0530163 return status;
164 }
165 else
166 {
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530167 val_print(PRINT_DEBUG, "[Check %d] START\n", i);
jaypit02ea3cd062018-10-05 12:22:38 +0530168 }
169 }
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530170#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530171 /* Execute client tests */
Gowtham Siddarthb1cd50f2019-08-21 11:50:26 +0530172 test_status = tests_list[i](CALLER_NONSECURE);
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530173#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +0530174 if (server_hs == TRUE)
175 {
176 /* Retrive Server test status */
177 status = val_get_secure_test_result(&handle);
178 }
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530179#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530180 status = test_status ? test_status:status;
Gowtham Siddarth47223082019-01-17 09:59:50 +0530181 if (IS_TEST_SKIP(status))
182 {
183 val_set_status(status);
184 if (server_hs == TRUE)
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530185 val_print(PRINT_DEBUG, "[Check %d] SKIPPED\n", i);
Gowtham Siddarth47223082019-01-17 09:59:50 +0530186 return status;
187 }
188 else if (VAL_ERROR(status))
jaypit02ea3cd062018-10-05 12:22:38 +0530189 {
190 val_set_status(RESULT_FAIL(status));
jaypit02ac23b5b2018-11-02 13:10:19 +0530191 if (server_hs == TRUE)
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530192 val_print(PRINT_DEBUG, "[Check %d] FAILED\n", i);
Gowtham Siddarth47223082019-01-17 09:59:50 +0530193
jaypit02ea3cd062018-10-05 12:22:38 +0530194 return status;
195 }
196 else
197 {
jaypit02ac23b5b2018-11-02 13:10:19 +0530198 if (server_hs == TRUE)
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530199 val_print(PRINT_DEBUG, "[Check %d] PASSED\n", i);
jaypit02ea3cd062018-10-05 12:22:38 +0530200 }
Gowtham Siddarth47223082019-01-17 09:59:50 +0530201
jaypit02ea3cd062018-10-05 12:22:38 +0530202 i++;
203 }
204 }
205 else
206 {
207 /* If we are here means, we are in second run of this test */
208 status = VAL_STATUS_SUCCESS;
209 if (boot.state != BOOT_EXPECTED_S)
210 {
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530211 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530212 }
213 }
214 return status;
215}
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530216
217#ifdef IPC
jaypit02ea3cd062018-10-05 12:22:38 +0530218/**
219 @brief - This function is used to switch to client_partition.c
220 where client tests will be executed to cover secure to secure
221 IPC scenario.
222 @param - test_num : Test_num
223 @return - val_status_t
224**/
225val_status_t val_switch_to_secure_client(uint32_t test_num)
226{
227 val_status_t status = VAL_STATUS_SUCCESS;
228 boot_t boot;
229 psa_handle_t handle;
230 test_info_t test_info;
231
232 test_info.test_num = test_num;
233 test_info.block_num = 1;
234
235 status = val_get_boot_flag(&boot.state);
236 if (VAL_ERROR(status))
237 {
238 goto exit;
239 }
240
241 if (boot.state != BOOT_EXPECTED_S)
242 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530243 /*
244 * Reboot have been expected by test in previous s run,
245 * consider previous run pass and jump to second test function
246 * of the same test if available.
247 */
248 if (boot.state == BOOT_EXPECTED_REENTER_TEST)
249 {
250 test_info.block_num++;
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530251 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530252 }
253
jaypit02ea3cd062018-10-05 12:22:38 +0530254 status = val_set_boot_flag(BOOT_NOT_EXPECTED);
255 if (VAL_ERROR(status))
256 {
257 goto exit;
258 }
259
260 /* switch to secure client */
261 status = val_execute_secure_test_func(&handle, test_info, CLIENT_TEST_DISPATCHER_SID);
262 if (VAL_ERROR(status))
263 {
264 goto exit;
265 }
266
267 /* Retrive secure client test status */
268 status = val_get_secure_test_result(&handle);
Gowtham Siddarth47223082019-01-17 09:59:50 +0530269 if (IS_TEST_SKIP(status))
270 {
271 val_set_status(status);
272 return status;
273 }
jaypit02ea3cd062018-10-05 12:22:38 +0530274 if (VAL_ERROR(status))
275 {
276 goto exit;
277 }
278 return status;
279 }
280 else
281 {
282 /* If we are here means, we are in third run of this test */
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530283 val_print(PRINT_DEBUG, "[Check 1] PASSED\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530284 return VAL_STATUS_SUCCESS;
285 }
286
287exit:
288 val_set_status(RESULT_FAIL(status));
289 return status;
290}
291
292/**
293 @brief - This function is used to handshake between:
294 - nonsecure client fn to server test fn
295 - secure client fn and server test fn
296 - nonsecure client fn to secure client test fn
297 @param - handle : handle returned while connecting given sid
298 @param - test_info : Test_num and block_num to be executed
299 @param - sid : RoT service to be connected. Partition dispatcher sid
300 @return - val_status_t
301**/
302val_status_t val_execute_secure_test_func(psa_handle_t *handle, test_info_t test_info, uint32_t sid)
303{
304 uint32_t test_data;
305 val_status_t status = VAL_STATUS_SUCCESS;
306 psa_status_t status_of_call = PSA_SUCCESS;
307
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530308 *handle = psa_connect(sid, 1);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530309 if (*handle > 0)
310 {
311 test_data = ((uint32_t)(test_info.test_num) |((uint32_t)(test_info.block_num) << BLOCK_NUM_POS)
312 | ((uint32_t)(TEST_EXECUTE_FUNC) << ACTION_POS));
313 psa_invec data[1] = {{&test_data, sizeof(test_data)}};
314
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530315 status_of_call = psa_call(*handle, 0, data, 1, NULL, 0);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530316 if (status_of_call != PSA_SUCCESS)
317 {
318 status = VAL_STATUS_CALL_FAILED;
319 val_print(PRINT_ERROR, "Call to dispatch SF failed. Status=%x\n", status_of_call);
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530320 psa_close(*handle);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530321 }
322 }
323 else
jaypit02ea3cd062018-10-05 12:22:38 +0530324 {
325 val_print(PRINT_ERROR, "Could not connect SID. Handle=%x\n", *handle);
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530326 status = VAL_STATUS_CONNECTION_FAILED;
jaypit02ea3cd062018-10-05 12:22:38 +0530327 }
Gowtham Siddarth47223082019-01-17 09:59:50 +0530328
jaypit02ea3cd062018-10-05 12:22:38 +0530329 return status;
330}
331
332/**
333 @brief - This function is used to retrive the status of previously connected test function
334 using val_execute_secure_test_func
335 @param - handle : handle of server function. Handle of Partition dispatcher sid
336 @return - The status of test functions
337**/
338val_status_t val_get_secure_test_result(psa_handle_t *handle)
339{
340 uint32_t test_data;
341 val_status_t status = VAL_STATUS_SUCCESS;
342 psa_status_t status_of_call = PSA_SUCCESS;
343
344 test_data = (TEST_RETURN_RESULT << ACTION_POS);
345
346 psa_outvec resp = {&status, sizeof(status)};
347 psa_invec data[1] = {{&test_data, sizeof(test_data)}};
348
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530349 status_of_call = psa_call(*handle, 0, data, 1, &resp, 1);
jaypit02ea3cd062018-10-05 12:22:38 +0530350 if (status_of_call != PSA_SUCCESS)
351 {
352 status = VAL_STATUS_CALL_FAILED;
353 val_print(PRINT_ERROR, "Call to dispatch SF failed. Status=%x\n", status_of_call);
354 }
355
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530356 psa_close(*handle);
jaypit02ea3cd062018-10-05 12:22:38 +0530357 return status;
358}
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530359#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530360
361/**
362 @brief - Parses input status for a given test and
363 outputs appropriate information on the console
364 @return - Test state
365**/
366uint32_t val_report_status(void)
367{
368 uint32_t status, state;
369
370 status = val_get_status();
371
372 state = (status >> TEST_STATE_BIT) & TEST_STATE_MASK;
373 status = status & TEST_STATUS_MASK;
374
375 switch (state)
376 {
377 case TEST_START:
378 state = TEST_FAIL;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530379 val_print(PRINT_ALWAYS, "\nTEST RESULT: FAILED (Error Code=0x%x)\n",
jaypit02ea3cd062018-10-05 12:22:38 +0530380 VAL_STATUS_INIT_FAILED);
381 break;
382
383 case TEST_END:
384 state = TEST_PASS;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530385 val_print(PRINT_ALWAYS, "\nTEST RESULT: PASSED\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530386 break;
387
388 case TEST_FAIL:
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530389 val_print(PRINT_ALWAYS, "\nTEST RESULT: FAILED (Error Code=0x%x)\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530390 break;
391
392 case TEST_SKIP:
Gowtham Siddarth47223082019-01-17 09:59:50 +0530393 state = TEST_SKIP;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530394 val_print(PRINT_ALWAYS, "\nTEST RESULT: SKIPPED (Skip Code=0x%x)\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530395 break;
396
397 case TEST_PENDING:
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530398 val_print(PRINT_ALWAYS, "\nTEST RESULT: SIM ERROR (Error Code=0x%x)\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530399 break;
400
401 default:
402 state = TEST_FAIL;
Vinay Kumar Kotegowder18fcd402020-04-27 17:38:41 +0530403 val_print(PRINT_ALWAYS, "\nTEST RESULT: FAILED(Error Code=0x%x)\n", VAL_STATUS_INVALID);
jaypit02ea3cd062018-10-05 12:22:38 +0530404 break;
405
406 }
407
408 val_print(PRINT_ALWAYS, "\n******************************************\n", 0);
409 return state;
410}
411
412/**
413 @brief - Records the state and status of test
414 @return - val_status_t
415**/
416val_status_t val_set_status(uint32_t status)
417{
418 g_status_buffer.state = ((status >> TEST_STATE_BIT) & TEST_STATE_MASK);
419 g_status_buffer.status = (status & TEST_STATUS_MASK);
420
421 return VAL_STATUS_SUCCESS;
422}
423
424/**
425 @brief - Updates the state and status for a given test
426 @return - test status
427**/
428uint32_t val_get_status(void)
429{
430 return ((g_status_buffer.state) << TEST_STATE_BIT) | (g_status_buffer.status);
431}
432
433/*
434 @brief - This function checks if the input status argument is an error.
435 On error, we print the checkpoint value and set the status.
436 @param - checkpoint : Test debug checkpoint
437 - val_status_t : Test status
438 @return - returns the input status back to the program.
439*/
440
441val_status_t val_err_check_set(uint32_t checkpoint, val_status_t status)
442{
443 if (VAL_ERROR(status))
444 {
445 val_print(PRINT_ERROR, "\tCheckpoint %d : ", checkpoint);
446 val_print(PRINT_ERROR, "Error Code=0x%x \n", status);
447 val_set_status(RESULT_FAIL(status));
448 }
449 else
450 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530451 status = (val_get_status() & TEST_STATUS_MASK);
jaypit02ea3cd062018-10-05 12:22:38 +0530452 if (VAL_ERROR(status))
453 {
454 val_print(PRINT_ERROR, "\tCheckpoint %d : ", checkpoint);
455 val_print(PRINT_ERROR, "Error Code=0x%x \n", status);
456 }
457 else
458 {
459 val_print(PRINT_DEBUG, "\tCheckpoint %d \n", checkpoint);
460 }
461 }
462 return status;
463}
464
465/**
466 @brief This API prints the test number, description and
467 sets the test state to TEST_START on successful execution.
468 @param test_num :unique number identifying this test
469 @param desc :brief description of the test
470 @param test_bitfield :Addition test info such as
471 - test isolation level requirement
472 - Watchdog timeout type
473 @return void
474**/
475
476void val_test_init(uint32_t test_num, char8_t *desc, uint32_t test_bitfield)
477{
478 val_status_t status = VAL_STATUS_SUCCESS;
jaypit02ea3cd062018-10-05 12:22:38 +0530479
480 /*global init*/
cherat013f028722019-02-18 14:50:37 +0530481 g_status_buffer.state = TEST_FAIL;
jaypit02ea3cd062018-10-05 12:22:38 +0530482 g_status_buffer.status = VAL_STATUS_INVALID;
483
484 val_print(PRINT_ALWAYS, "\nTEST: %d | DESCRIPTION: ", test_num);
485 val_print(PRINT_ALWAYS, desc, 0);
486
487 /* common skip logic */
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530488 if (PLATFORM_PSA_ISOLATION_LEVEL < GET_TEST_ISOLATION_LEVEL(test_bitfield))
jaypit02ea3cd062018-10-05 12:22:38 +0530489 {
490 val_set_status(RESULT_SKIP(VAL_STATUS_ISOLATION_LEVEL_NOT_SUPP));
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530491 val_print(PRINT_ALWAYS, "\tSkipping test. Required isolation level is not supported\n", 0);
jaypit02ea3cd062018-10-05 12:22:38 +0530492 return;
493 }
494
Vinay Kumar Kotegowder9982f902019-07-15 09:13:54 +0530495#ifdef WATCHDOG_AVAILABLE
jaypit02ea3cd062018-10-05 12:22:38 +0530496 /* Initialise watchdog */
497 status = val_wd_timer_init(GET_WD_TIMOUT_TYPE(test_bitfield));
498 if (VAL_ERROR(status))
499 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530500 val_print(PRINT_ERROR, "\tval_wd_timer_init failed Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530501 return;
502 }
503
504 /* Enable watchdog Timer */
505 status = val_wd_timer_enable();
506 if (VAL_ERROR(status))
507 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530508 val_print(PRINT_ERROR, "\tval_wd_timer_enable failed Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530509 return;
510 }
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530511#endif
jaypit02ea3cd062018-10-05 12:22:38 +0530512
Jaykumar Pitambarbhai Patelccf5bf22019-12-06 11:58:32 +0530513 val_set_status(RESULT_START(status));
jaypit02ea3cd062018-10-05 12:22:38 +0530514 return;
515}
516
517/**
518 @brief This API sets the test state to TEST_END if test is successfuly passed.
519 @param none
520 @return none
521**/
522
523void val_test_exit(void)
524{
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530525 val_status_t status = VAL_STATUS_SUCCESS;
jaypit02ea3cd062018-10-05 12:22:38 +0530526
Vinay Kumar Kotegowder9982f902019-07-15 09:13:54 +0530527#ifdef WATCHDOG_AVAILABLE
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530528 status = val_wd_timer_disable();
529 if (VAL_ERROR(status))
530 {
531 val_print(PRINT_ERROR, "\tval_wd_timer_disable failed Error=0x%x\n", status);
532 val_set_status(RESULT_FAIL(status));
533 return;
534 }
535#endif
536
Gowtham Siddarth47223082019-01-17 09:59:50 +0530537 status = val_get_status();
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530538
jaypit02ea3cd062018-10-05 12:22:38 +0530539 /* return if test skipped or failed */
Gowtham Siddarth47223082019-01-17 09:59:50 +0530540 if (IS_TEST_FAIL(status) || IS_TEST_SKIP(status))
jaypit02ea3cd062018-10-05 12:22:38 +0530541 {
Gowtham Siddarth47223082019-01-17 09:59:50 +0530542 return;
jaypit02ea3cd062018-10-05 12:22:38 +0530543 }
Gowtham Siddarth47223082019-01-17 09:59:50 +0530544 else
545 {
546 val_set_status(RESULT_END(VAL_STATUS_SUCCESS));
547 }
jaypit02ea3cd062018-10-05 12:22:38 +0530548}
549
550/**
551 @brief - This function returns the test ID of the last test that was run
552 @param - test_id address
553 @return - val_status_t
554**/
555val_status_t val_get_last_run_test_id(test_id_t *test_id)
556{
557 val_status_t status;
558 test_count_t test_count;
559 boot_t boot;
560 int i = 0, intermediate_boot = 0;
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530561 boot_state_t boot_state[] = {BOOT_NOT_EXPECTED,
562 BOOT_EXPECTED_NS,
563 BOOT_EXPECTED_S,
564 BOOT_EXPECTED_BUT_FAILED,
Gowtham Siddarthb8926262019-08-05 12:59:35 +0530565 BOOT_EXPECTED_REENTER_TEST,
566 BOOT_EXPECTED_CONT_TEST_EXEC
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530567 };
jaypit02ea3cd062018-10-05 12:22:38 +0530568
569 status = val_get_boot_flag(&boot.state);
570 if (VAL_ERROR(status))
571 {
572 return status;
573 }
574
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530575 val_print(PRINT_INFO, "\n\tboot.state=0x%x", boot.state);
576
Vinay Kumar Kotegowder59a21912020-06-14 19:51:54 +0530577 for (i = 0; i < (int)(sizeof(boot_state)/sizeof(boot_state[0])); i++)
jaypit02ea3cd062018-10-05 12:22:38 +0530578 {
579 if (boot.state == boot_state[i])
580 {
581 intermediate_boot = 1;
582 break;
583 }
584 }
585
586 if (!intermediate_boot)
587 {
588 /* First boot. Initiliase necessary data structure */
589 status = val_set_boot_flag(BOOT_UNKNOWN);
590 if (VAL_ERROR(status))
591 {
592 return status;
593 }
594
595 *test_id = VAL_INVALID_TEST_ID;
596 status = val_nvmem_write(VAL_NVMEM_OFFSET(NV_TEST_ID_PREVIOUS),
597 test_id, sizeof(test_id_t));
598 if (VAL_ERROR(status))
599 {
600 val_print(PRINT_ALWAYS, "\n\tNVMEM write error", 0);
601 return status;
602 }
603
604 test_count.pass_cnt = 0;
605 test_count.fail_cnt = 0;
606 test_count.skip_cnt = 0;
607 test_count.sim_error_cnt = 0;
608
609 status = val_nvmem_write(VAL_NVMEM_OFFSET(NV_TEST_CNT),
610 &test_count, sizeof(test_count_t));
611 if (VAL_ERROR(status))
612 {
613 val_print(PRINT_ERROR, "\n\tNVMEM write error", 0);
614 return status;
615 }
616 }
617
618 status = val_nvmem_read(VAL_NVMEM_OFFSET(NV_TEST_ID_PREVIOUS), test_id, sizeof(test_id_t));
619 if (VAL_ERROR(status))
620 {
621 val_print(PRINT_ERROR, "\n\tNVMEM read error", 0);
622 }
623
624 val_print(PRINT_INFO, "In val_get_last_run_test_id, test_id=%x\n", *test_id);
625 return status;
626}
627
628/**
629 @brief - This function sets the given boot.state value to corresponding
630 boot NVMEM location
631 @param - state: boot_state_t
632 @return - val_status_t
633**/
634val_status_t val_set_boot_flag(boot_state_t state)
635{
636 boot_t boot;
637 val_status_t status;
638
639 boot.state = state;
640 status = val_nvmem_write(VAL_NVMEM_OFFSET(NV_BOOT), &boot, sizeof(boot_t));
641 if (VAL_ERROR(status))
642 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530643 val_print(PRINT_ERROR, "\tval_nvmem_write failed. Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530644 return status;
645 }
646 return status;
647}
648
649/**
650 @brief - This function returns boot.state value available in boot NVMEM location
651 @param - state address
652 @return - val_status_t
653**/
654val_status_t val_get_boot_flag(boot_state_t *state)
655{
656 boot_t boot;
657 val_status_t status;
658
659 status = val_nvmem_read(VAL_NVMEM_OFFSET(NV_BOOT), &boot, sizeof(boot_t));
660 if (VAL_ERROR(status))
661 {
Jaykumar Pitambarbhai Patelf97bc882019-06-03 11:57:48 +0530662 val_print(PRINT_ERROR, "\tval_nvmem_read failed. Error=0x%x\n", status);
jaypit02ea3cd062018-10-05 12:22:38 +0530663 return status;
664 }
665 *state = boot.state;
666 return status;
667}