Xinyu Zhang | 3ea91b9 | 2021-09-22 14:54:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "tfm_ns_client_ext.h" |
| 9 | #include "tfm_nsid_manager.h" |
| 10 | |
| 11 | /* Max number of threads could be customised by RTOS */ |
| 12 | #ifndef THREAD_NUM_MAX |
| 13 | #define THREAD_NUM_MAX 10 |
| 14 | #endif |
| 15 | |
| 16 | /* Map table of token and NSIDs */ |
| 17 | struct nsid_token_pair { |
| 18 | uint32_t token; |
| 19 | int32_t nsid; |
| 20 | }; |
| 21 | |
| 22 | static struct nsid_token_pair test_ns_token_table[THREAD_NUM_MAX]; |
| 23 | |
| 24 | uint8_t nsid_mgr_init(void) |
| 25 | { |
| 26 | uint32_t i; |
| 27 | |
| 28 | for (i = 0; i < THREAD_NUM_MAX; i++) { |
| 29 | test_ns_token_table[i].token = TFM_NS_CLIENT_INVALID_TOKEN; |
| 30 | test_ns_token_table[i].nsid = TFM_INVALID_NSID_MIN; |
| 31 | } |
| 32 | |
| 33 | return NSID_MGR_ERR_SUCCESS; |
| 34 | } |
| 35 | |
| 36 | uint8_t nsid_mgr_add_entry(int32_t nsid, uint32_t token) |
| 37 | { |
| 38 | uint32_t i; |
| 39 | |
| 40 | if (nsid >= TFM_INVALID_NSID_MIN) { |
| 41 | return NSID_MGR_ERR_INVALID_NSID; |
| 42 | } |
| 43 | |
| 44 | if (token == TFM_NS_CLIENT_INVALID_TOKEN) { |
| 45 | return NSID_MGR_ERR_INVALID_TOKEN; |
| 46 | } |
| 47 | |
| 48 | for (i = 0; i < THREAD_NUM_MAX; i++) { |
| 49 | if (test_ns_token_table[i].token == TFM_NS_CLIENT_INVALID_TOKEN) { |
| 50 | test_ns_token_table[i].token = token; |
| 51 | test_ns_token_table[i].nsid = nsid; |
| 52 | return NSID_MGR_ERR_SUCCESS; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* No free entry for new token, return error */ |
| 57 | return NSID_MGR_ERR_NO_FREE_ENTRY; |
| 58 | } |
| 59 | |
| 60 | uint8_t nsid_mgr_remove_entry(uint32_t token) |
| 61 | { |
| 62 | uint32_t i; |
| 63 | |
| 64 | if (token == TFM_NS_CLIENT_INVALID_TOKEN) { |
| 65 | return NSID_MGR_ERR_INVALID_TOKEN; |
| 66 | } |
| 67 | |
| 68 | for (i = 0; i < THREAD_NUM_MAX; i++) { |
| 69 | if (test_ns_token_table[i].token == token) { |
| 70 | test_ns_token_table[i].token = TFM_NS_CLIENT_INVALID_TOKEN; |
| 71 | test_ns_token_table[i].nsid = TFM_INVALID_NSID_MIN; |
| 72 | return NSID_MGR_ERR_SUCCESS; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* Token not found in the table, return error */ |
| 77 | return NSID_MGR_ERR_INVALID_TOKEN; |
| 78 | } |
| 79 | |
| 80 | int32_t nsid_mgr_query_nsid(uint32_t token) |
| 81 | { |
| 82 | uint32_t i; |
| 83 | |
| 84 | /* Return invalid NSID if token is invalid */ |
| 85 | if (token == TFM_NS_CLIENT_INVALID_TOKEN) { |
| 86 | return TFM_INVALID_NSID_MIN; |
| 87 | } |
| 88 | |
| 89 | for (i = 0; i < THREAD_NUM_MAX; i++) { |
| 90 | if (test_ns_token_table[i].token == token) { |
| 91 | return test_ns_token_table[i].nsid; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* Token not found in the table, return invalid NSID */ |
| 96 | return TFM_INVALID_NSID_MIN; |
| 97 | } |