blob: 56ffce7e8f2b8941842acc2a90e4262240ac7107 [file] [log] [blame]
Raef Coles148b9472021-06-18 08:48:17 +01001/*
2 * Copyright (c) 2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8/* NOTE: For the security of the protected storage system, the bootloader
9 * rollback protection, and the protection of cryptographic material it is
10 * CRITICAL to use a internal (in-die) persistent memory for the implementation
11 * of the OTP_NV_COUNTERS flash area (see flash_otp_nv_layout.c).
12 */
13
14#include "tfm_plat_otp.h"
15
16#include "flash_layout.h"
17#include "flash_otp_nv_counters_backend.h"
18#include <string.h>
19#include <stddef.h>
20
21enum tfm_plat_err_t tfm_plat_otp_init(void)
22{
23 return init_otp_nv_counters_flash();
24}
25
26static enum tfm_plat_err_t write_to_output(enum tfm_otp_element_id_t id,
27 uint32_t offset, size_t out_len,
28 uint8_t *out)
29{
30 enum tfm_plat_err_t err = TFM_PLAT_ERR_SUCCESS;
31 size_t value_size;
32 size_t copy_size;
33
34 err = tfm_plat_otp_get_size(id, &value_size);
35 if (err != TFM_PLAT_ERR_SUCCESS) {
36 return err;
37 }
38
39 copy_size = out_len < value_size ? out_len : value_size;
40
41 err = read_otp_nv_counters_flash(offset, out, copy_size);
42 if (err != TFM_PLAT_ERR_SUCCESS) {
43 return err;
44 }
45
46 return TFM_PLAT_ERR_SUCCESS;
47}
48
49enum tfm_plat_err_t tfm_plat_otp_read(enum tfm_otp_element_id_t id,
50 size_t out_len, uint8_t *out)
51{
52 switch (id) {
53 case PLAT_OTP_ID_HUK:
54 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, huk), out_len, out);
55 case PLAT_OTP_ID_IAK:
56 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, iak), out_len, out);
57 case PLAT_OTP_ID_IAK_LEN:
58 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, iak_len), out_len, out);
59 case PLAT_OTP_ID_IAK_TYPE:
60 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, iak_type), out_len, out);
61 case PLAT_OTP_ID_IAK_ID:
62 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, iak_id), out_len, out);
63
64 case PLAT_OTP_ID_BOOT_SEED:
65 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, boot_seed), out_len, out);
66 case PLAT_OTP_ID_LCS:
67 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, lcs), out_len, out);
68 case PLAT_OTP_ID_IMPLEMENTATION_ID:
69 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, implementation_id), out_len, out);
70 case PLAT_OTP_ID_HW_VERSION:
71 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, hw_version), out_len, out);
72 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
73 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, verification_service_url), out_len, out);
74 case PLAT_OTP_ID_PROFILE_DEFINITION:
75 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, profile_definition), out_len, out);
76
77#ifdef BL2
78 case PLAT_OTP_ID_BL2_ROTPK_0:
79 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_0), out_len, out);
80 case PLAT_OTP_ID_BL2_ROTPK_1:
81 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_1), out_len, out);
82 case PLAT_OTP_ID_BL2_ROTPK_2:
83 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_2), out_len, out);
84
85 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
86 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_0), out_len, out);
87 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
88 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_1), out_len, out);
89 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
90 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_2), out_len, out);
91#endif /* BL2 */
92
93#ifdef BL1
94 case PLAT_OTP_ID_BL1_ROTPK_0:
95 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_rotpk_0), out_len, out);
96
97 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
98 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_nv_counter_0), out_len, out);
99#endif /* BL1 */
100
101 case PLAT_OTP_ID_ENTROPY_SEED:
102 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, entropy_seed), out_len, out);
103
Satish Kumara5312fd2021-10-26 07:08:57 +0100104 case PLAT_OTP_ID_SECURE_DEBUG_PK:
105 return write_to_output(id, offsetof(struct flash_otp_nv_counters_region_t, secure_debug_pk), out_len, out);
106
Raef Coles148b9472021-06-18 08:48:17 +0100107 default:
108 return TFM_PLAT_ERR_UNSUPPORTED;
109 }
110}
111
Michel Jaouend0fd8d92021-10-14 09:22:41 +0200112#if defined(OTP_WRITEABLE)
Raef Coles148b9472021-06-18 08:48:17 +0100113static enum tfm_plat_err_t read_from_input(enum tfm_otp_element_id_t id,
114 uint32_t offset, size_t in_len,
115 const uint8_t *in)
116{
117 enum tfm_plat_err_t err = TFM_PLAT_ERR_SUCCESS;
118 size_t value_size;
119 size_t copy_size;
120 uint8_t buffer[in_len];
121 size_t idx;
122
123 err = tfm_plat_otp_get_size(id, &value_size);
124 if (err != TFM_PLAT_ERR_SUCCESS) {
125 return err;
126 }
127
128 copy_size = in_len < value_size ? in_len : value_size;
129
130 err = read_otp_nv_counters_flash(offset, buffer, copy_size);
131 if (err != TFM_PLAT_ERR_SUCCESS) {
132 return err;
133 }
134
135 for (idx = 0; idx < copy_size; idx++) {
136 if ((buffer[idx] | in[idx]) != in[idx]) {
137 return TFM_PLAT_ERR_INVALID_INPUT;
138 }
139
140 buffer[idx] |= in[idx];
141 }
142
143 err = write_otp_nv_counters_flash(offset, buffer, copy_size);
144 if (err != TFM_PLAT_ERR_SUCCESS) {
145 return err;
146 }
147
148 return TFM_PLAT_ERR_SUCCESS;
149}
150
151enum tfm_plat_err_t tfm_plat_otp_write(enum tfm_otp_element_id_t id,
152 size_t in_len, const uint8_t *in)
153{
154 switch (id) {
155 case PLAT_OTP_ID_HUK:
156 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, huk), in_len, in);
157 case PLAT_OTP_ID_IAK:
158 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak), in_len, in);
159 case PLAT_OTP_ID_IAK_LEN:
160 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_len), in_len, in);
161 case PLAT_OTP_ID_IAK_TYPE:
162 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_type), in_len, in);
163 case PLAT_OTP_ID_IAK_ID:
164 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_id), in_len, in);
165
166 case PLAT_OTP_ID_BOOT_SEED:
167 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, boot_seed), in_len, in);
168 case PLAT_OTP_ID_LCS:
169 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, lcs), in_len, in);
170 case PLAT_OTP_ID_IMPLEMENTATION_ID:
171 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, implementation_id), in_len, in);
172 case PLAT_OTP_ID_HW_VERSION:
173 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, hw_version), in_len, in);
174 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
175 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, verification_service_url), in_len, in);
176 case PLAT_OTP_ID_PROFILE_DEFINITION:
177 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, profile_definition), in_len, in);
178
179#ifdef BL2
180 case PLAT_OTP_ID_BL2_ROTPK_0:
181 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_0), in_len, in);
182 case PLAT_OTP_ID_BL2_ROTPK_1:
183 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_1), in_len, in);
184 case PLAT_OTP_ID_BL2_ROTPK_2:
185 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_2), in_len, in);
186
187 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
188 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_0), in_len, in);
189 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
190 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_1), in_len, in);
191 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
192 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_2), in_len, in);
193#endif /* Bl2 */
194
195#ifdef BL1
196 case PLAT_OTP_ID_BL1_ROTPK_0:
197 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_rotpk_0), in_len, in);
198
199 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
200 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_nv_counter_0), in_len, in);
201#endif /* BL1 */
202
203 case PLAT_OTP_ID_ENTROPY_SEED:
204 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, entropy_seed), in_len, in);
205
Satish Kumara5312fd2021-10-26 07:08:57 +0100206 case PLAT_OTP_ID_SECURE_DEBUG_PK:
207 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, secure_debug_pk), in_len, in);
208
Raef Coles148b9472021-06-18 08:48:17 +0100209 default:
210 return TFM_PLAT_ERR_UNSUPPORTED;
211 }
212}
Michel Jaouend0fd8d92021-10-14 09:22:41 +0200213#else
214enum tfm_plat_err_t tfm_plat_otp_write(enum tfm_otp_element_id_t id,
215 size_t in_len, const uint8_t *in)
216{
217 (void)id;
218 (void)in_len;
219 (void)in;
220 return TFM_PLAT_ERR_UNSUPPORTED;
221}
222#endif
Raef Coles148b9472021-06-18 08:48:17 +0100223
224enum tfm_plat_err_t tfm_plat_otp_get_size(enum tfm_otp_element_id_t id,
225 size_t *size)
226{
227 switch (id) {
228 case PLAT_OTP_ID_HUK:
229 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->huk);
230 break;
231 case PLAT_OTP_ID_IAK:
232 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak);
233 break;
234 case PLAT_OTP_ID_IAK_LEN:
235 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_len);
236 break;
237 case PLAT_OTP_ID_IAK_TYPE:
238 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_type);
239 break;
240 case PLAT_OTP_ID_IAK_ID:
241 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_id);
242 break;
243
244 case PLAT_OTP_ID_BOOT_SEED:
245 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->boot_seed);
246 break;
247 case PLAT_OTP_ID_LCS:
248 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->lcs);
249 break;
250 case PLAT_OTP_ID_IMPLEMENTATION_ID:
251 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->implementation_id);
252 break;
253 case PLAT_OTP_ID_HW_VERSION:
254 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->hw_version);
255 break;
256 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
257 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->verification_service_url);
258 break;
259 case PLAT_OTP_ID_PROFILE_DEFINITION:
260 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->profile_definition);
261 break;
262
263#ifdef BL2
264 case PLAT_OTP_ID_BL2_ROTPK_0:
265 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_0);
266 break;
267 case PLAT_OTP_ID_BL2_ROTPK_1:
268 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_1);
269 break;
270 case PLAT_OTP_ID_BL2_ROTPK_2:
271 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_2);
272 break;
273
274 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
275 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_0);
276 break;
277 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
278 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_1);
279 break;
280 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
281 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_2);
282 break;
283#endif /* BL2 */
284
285#ifdef BL1
286 case PLAT_OTP_ID_BL1_ROTPK_0:
287 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl1_rotpk_0);
288 break;
289
290 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
291 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl1_nv_counter_0);
292 break;
293#endif /* BL1 */
294
295 case PLAT_OTP_ID_ENTROPY_SEED:
296 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->entropy_seed);
297 break;
298
Satish Kumara5312fd2021-10-26 07:08:57 +0100299 case PLAT_OTP_ID_SECURE_DEBUG_PK:
300 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->secure_debug_pk);
301 break;
302
Raef Coles148b9472021-06-18 08:48:17 +0100303 default:
304 return TFM_PLAT_ERR_UNSUPPORTED;
305 }
306
307 return TFM_PLAT_ERR_SUCCESS;
308}