TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test_ps_nv_counters.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "test_ps_nv_counters.h"
9 
10 #include <limits.h>
12 #include "ps_utils.h"
13 
14 #define DISABLE_INCREMENT 0
15 #define ENABLE_INCREMENT 1
16 
17 #define TOTAL_PS_NV_COUNTERS 3
18 #define INIT_NV_COUNTERS_VALUE 42
19 
20 static uint8_t nv_increment_status = ENABLE_INCREMENT;
21 static uint32_t test_nv_counters[TOTAL_PS_NV_COUNTERS] = {
25  };
26 
27 static uint32_t get_nv_counter_position(enum tfm_nv_counter_t counter_id)
28 {
29  switch (counter_id) {
31  return 0;
33  return 1;
35  return 2;
36  default:
37  return TOTAL_PS_NV_COUNTERS;
38  }
39 }
40 
41 psa_status_t ps_read_nv_counter(enum tfm_nv_counter_t counter_id,
42  uint32_t *val)
43 {
44  uint32_t nv_pos;
45 
46  nv_pos = get_nv_counter_position(counter_id);
47  if (nv_pos >= TOTAL_PS_NV_COUNTERS) {
49  }
50 
51  /* Reads counter value */
52  *val = test_nv_counters[nv_pos];
53 
54  return PSA_SUCCESS;
55 }
56 
57 psa_status_t ps_increment_nv_counter(enum tfm_nv_counter_t counter_id)
58 {
59  uint32_t nv_pos;
60 
61  if (nv_increment_status == DISABLE_INCREMENT) {
63  }
64 
65  nv_pos = get_nv_counter_position(counter_id);
66  if (nv_pos >= TOTAL_PS_NV_COUNTERS) {
68  }
69 
70  if (test_nv_counters[nv_pos] == UINT32_MAX) {
72  }
73 
74  /* Increments counter value */
75  test_nv_counters[nv_pos]++;
76 
77  return PSA_SUCCESS;
78 }
79 
80 /* Implementation of PS NV counter interfaces defined by
81  * test_ps_nv_counters.h
82  */
84 {
85  nv_increment_status = DISABLE_INCREMENT;
86 }
87 
89 {
90  nv_increment_status = ENABLE_INCREMENT;
91 }
92 
93 psa_status_t test_ps_read_nv_counter(enum tfm_nv_counter_t counter_id,
94  uint32_t *val)
95 {
96  return ps_read_nv_counter(counter_id, val);
97 }
98 
99 psa_status_t test_ps_increment_nv_counter(enum tfm_nv_counter_t counter_id)
100 {
101  return ps_increment_nv_counter(counter_id);
102 }
103 
104 psa_status_t test_ps_decrement_nv_counter(enum tfm_nv_counter_t counter_id)
105 {
106  uint32_t nv_pos;
107 
108  nv_pos = get_nv_counter_position(counter_id);
109  if (nv_pos >= TOTAL_PS_NV_COUNTERS) {
111  }
112 
113  if (test_nv_counters[nv_pos] == 0) {
115  }
116 
117  /* Decrements counter value */
118  test_nv_counters[nv_pos]--;
119 
120  return PSA_SUCCESS;
121 }
122 
123 psa_status_t test_ps_set_nv_counter(enum tfm_nv_counter_t counter_id,
124  uint32_t value)
125 {
126  uint32_t nv_pos;
127 
128  nv_pos = get_nv_counter_position(counter_id);
129  if (nv_pos >= TOTAL_PS_NV_COUNTERS) {
131  }
132 
133  /* Sets counter value */
134  test_nv_counters[nv_pos] = value;
135 
136  return PSA_SUCCESS;
137 }
psa_status_t ps_read_nv_counter(enum tfm_nv_counter_t counter_id, uint32_t *val)
Reads the given non-volatile (NV) counter.
#define PSA_SUCCESS
Definition: crypto_values.h:35
psa_status_t ps_increment_nv_counter(enum tfm_nv_counter_t counter_id)
Increments the given non-volatile (NV) counter.
#define INIT_NV_COUNTERS_VALUE
#define TFM_PS_NV_COUNTER_1
#define PSA_ERROR_GENERIC_ERROR
Definition: crypto_values.h:43
#define TFM_PS_NV_COUNTER_3
psa_status_t test_ps_set_nv_counter(enum tfm_nv_counter_t counter_id, uint32_t value)
Sets a new value into the given non-volatile (NV) counter.
psa_status_t test_ps_decrement_nv_counter(enum tfm_nv_counter_t counter_id)
Decrements the given non-volatile (NV) counter.
psa_status_t test_ps_read_nv_counter(enum tfm_nv_counter_t counter_id, uint32_t *val)
Reads the given non-volatile (NV) counter.
psa_status_t test_ps_increment_nv_counter(enum tfm_nv_counter_t counter_id)
Increments the given non-volatile (NV) counter.
#define ENABLE_INCREMENT
void test_ps_enable_increment_nv_counter(void)
Enables PS increment nv counter function to work normally.
void test_ps_disable_increment_nv_counter(void)
Disables PS increment nv counter function to force PSA_ERROR_GENERIC_ERROR return value as an indicat...
#define TFM_PS_NV_COUNTER_2
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:43
#define DISABLE_INCREMENT
#define TOTAL_PS_NV_COUNTERS