blob: df630f425527fd43e0602268d08fcdbf35412b09 [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
Raef Coles8018e6b2021-11-17 10:55:01 +0000128 if (in_len > value_size) {
129 return TFM_PLAT_ERR_INVALID_INPUT;
130 }
Raef Coles148b9472021-06-18 08:48:17 +0100131
Raef Coles8018e6b2021-11-17 10:55:01 +0000132 err = read_otp_nv_counters_flash(offset, buffer, in_len);
Raef Coles148b9472021-06-18 08:48:17 +0100133 if (err != TFM_PLAT_ERR_SUCCESS) {
134 return err;
135 }
136
Raef Coles8018e6b2021-11-17 10:55:01 +0000137 for (idx = 0; idx < in_len; idx++) {
Raef Coles148b9472021-06-18 08:48:17 +0100138 if ((buffer[idx] | in[idx]) != in[idx]) {
139 return TFM_PLAT_ERR_INVALID_INPUT;
140 }
141
142 buffer[idx] |= in[idx];
143 }
144
Raef Coles8018e6b2021-11-17 10:55:01 +0000145 err = write_otp_nv_counters_flash(offset, buffer, in_len);
Raef Coles148b9472021-06-18 08:48:17 +0100146 if (err != TFM_PLAT_ERR_SUCCESS) {
147 return err;
148 }
149
150 return TFM_PLAT_ERR_SUCCESS;
151}
152
153enum tfm_plat_err_t tfm_plat_otp_write(enum tfm_otp_element_id_t id,
154 size_t in_len, const uint8_t *in)
155{
156 switch (id) {
157 case PLAT_OTP_ID_HUK:
158 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, huk), in_len, in);
159 case PLAT_OTP_ID_IAK:
160 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak), in_len, in);
161 case PLAT_OTP_ID_IAK_LEN:
162 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_len), in_len, in);
163 case PLAT_OTP_ID_IAK_TYPE:
164 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_type), in_len, in);
165 case PLAT_OTP_ID_IAK_ID:
166 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_id), in_len, in);
167
168 case PLAT_OTP_ID_BOOT_SEED:
169 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, boot_seed), in_len, in);
170 case PLAT_OTP_ID_LCS:
171 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, lcs), in_len, in);
172 case PLAT_OTP_ID_IMPLEMENTATION_ID:
173 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, implementation_id), in_len, in);
174 case PLAT_OTP_ID_HW_VERSION:
175 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, hw_version), in_len, in);
176 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
177 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, verification_service_url), in_len, in);
178 case PLAT_OTP_ID_PROFILE_DEFINITION:
179 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, profile_definition), in_len, in);
180
181#ifdef BL2
182 case PLAT_OTP_ID_BL2_ROTPK_0:
183 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_0), in_len, in);
184 case PLAT_OTP_ID_BL2_ROTPK_1:
185 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_1), in_len, in);
186 case PLAT_OTP_ID_BL2_ROTPK_2:
187 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_2), in_len, in);
188
189 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
190 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_0), in_len, in);
191 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
192 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_1), in_len, in);
193 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
194 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_2), in_len, in);
195#endif /* Bl2 */
196
197#ifdef BL1
198 case PLAT_OTP_ID_BL1_ROTPK_0:
199 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_rotpk_0), in_len, in);
200
201 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
202 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_nv_counter_0), in_len, in);
203#endif /* BL1 */
204
205 case PLAT_OTP_ID_ENTROPY_SEED:
206 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, entropy_seed), in_len, in);
207
Satish Kumara5312fd2021-10-26 07:08:57 +0100208 case PLAT_OTP_ID_SECURE_DEBUG_PK:
209 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, secure_debug_pk), in_len, in);
210
Raef Coles148b9472021-06-18 08:48:17 +0100211 default:
212 return TFM_PLAT_ERR_UNSUPPORTED;
213 }
214}
Michel Jaouend0fd8d92021-10-14 09:22:41 +0200215#else
216enum tfm_plat_err_t tfm_plat_otp_write(enum tfm_otp_element_id_t id,
217 size_t in_len, const uint8_t *in)
218{
219 (void)id;
220 (void)in_len;
221 (void)in;
222 return TFM_PLAT_ERR_UNSUPPORTED;
223}
224#endif
Raef Coles148b9472021-06-18 08:48:17 +0100225
226enum tfm_plat_err_t tfm_plat_otp_get_size(enum tfm_otp_element_id_t id,
227 size_t *size)
228{
229 switch (id) {
230 case PLAT_OTP_ID_HUK:
231 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->huk);
232 break;
233 case PLAT_OTP_ID_IAK:
234 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak);
235 break;
236 case PLAT_OTP_ID_IAK_LEN:
237 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_len);
238 break;
239 case PLAT_OTP_ID_IAK_TYPE:
240 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_type);
241 break;
242 case PLAT_OTP_ID_IAK_ID:
243 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_id);
244 break;
245
246 case PLAT_OTP_ID_BOOT_SEED:
247 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->boot_seed);
248 break;
249 case PLAT_OTP_ID_LCS:
250 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->lcs);
251 break;
252 case PLAT_OTP_ID_IMPLEMENTATION_ID:
253 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->implementation_id);
254 break;
255 case PLAT_OTP_ID_HW_VERSION:
256 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->hw_version);
257 break;
258 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
259 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->verification_service_url);
260 break;
261 case PLAT_OTP_ID_PROFILE_DEFINITION:
262 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->profile_definition);
263 break;
264
265#ifdef BL2
266 case PLAT_OTP_ID_BL2_ROTPK_0:
267 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_0);
268 break;
269 case PLAT_OTP_ID_BL2_ROTPK_1:
270 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_1);
271 break;
272 case PLAT_OTP_ID_BL2_ROTPK_2:
273 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_2);
274 break;
275
276 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
277 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_0);
278 break;
279 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
280 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_1);
281 break;
282 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
283 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_2);
284 break;
285#endif /* BL2 */
286
287#ifdef BL1
288 case PLAT_OTP_ID_BL1_ROTPK_0:
289 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl1_rotpk_0);
290 break;
291
292 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
293 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl1_nv_counter_0);
294 break;
295#endif /* BL1 */
296
297 case PLAT_OTP_ID_ENTROPY_SEED:
298 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->entropy_seed);
299 break;
300
Satish Kumara5312fd2021-10-26 07:08:57 +0100301 case PLAT_OTP_ID_SECURE_DEBUG_PK:
302 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->secure_debug_pk);
303 break;
304
Raef Coles148b9472021-06-18 08:48:17 +0100305 default:
306 return TFM_PLAT_ERR_UNSUPPORTED;
307 }
308
309 return TFM_PLAT_ERR_SUCCESS;
310}