blob: dc6a0b70aa6f0d92a3e5d66e2bb17a43cb8ef1a3 [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
104 default:
105 return TFM_PLAT_ERR_UNSUPPORTED;
106 }
107}
108
Michel Jaouend0fd8d92021-10-14 09:22:41 +0200109#if defined(OTP_WRITEABLE)
Raef Coles148b9472021-06-18 08:48:17 +0100110static enum tfm_plat_err_t read_from_input(enum tfm_otp_element_id_t id,
111 uint32_t offset, size_t in_len,
112 const uint8_t *in)
113{
114 enum tfm_plat_err_t err = TFM_PLAT_ERR_SUCCESS;
115 size_t value_size;
116 size_t copy_size;
117 uint8_t buffer[in_len];
118 size_t idx;
119
120 err = tfm_plat_otp_get_size(id, &value_size);
121 if (err != TFM_PLAT_ERR_SUCCESS) {
122 return err;
123 }
124
125 copy_size = in_len < value_size ? in_len : value_size;
126
127 err = read_otp_nv_counters_flash(offset, buffer, copy_size);
128 if (err != TFM_PLAT_ERR_SUCCESS) {
129 return err;
130 }
131
132 for (idx = 0; idx < copy_size; idx++) {
133 if ((buffer[idx] | in[idx]) != in[idx]) {
134 return TFM_PLAT_ERR_INVALID_INPUT;
135 }
136
137 buffer[idx] |= in[idx];
138 }
139
140 err = write_otp_nv_counters_flash(offset, buffer, copy_size);
141 if (err != TFM_PLAT_ERR_SUCCESS) {
142 return err;
143 }
144
145 return TFM_PLAT_ERR_SUCCESS;
146}
147
148enum tfm_plat_err_t tfm_plat_otp_write(enum tfm_otp_element_id_t id,
149 size_t in_len, const uint8_t *in)
150{
151 switch (id) {
152 case PLAT_OTP_ID_HUK:
153 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, huk), in_len, in);
154 case PLAT_OTP_ID_IAK:
155 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak), in_len, in);
156 case PLAT_OTP_ID_IAK_LEN:
157 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_len), in_len, in);
158 case PLAT_OTP_ID_IAK_TYPE:
159 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_type), in_len, in);
160 case PLAT_OTP_ID_IAK_ID:
161 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, iak_id), in_len, in);
162
163 case PLAT_OTP_ID_BOOT_SEED:
164 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, boot_seed), in_len, in);
165 case PLAT_OTP_ID_LCS:
166 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, lcs), in_len, in);
167 case PLAT_OTP_ID_IMPLEMENTATION_ID:
168 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, implementation_id), in_len, in);
169 case PLAT_OTP_ID_HW_VERSION:
170 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, hw_version), in_len, in);
171 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
172 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, verification_service_url), in_len, in);
173 case PLAT_OTP_ID_PROFILE_DEFINITION:
174 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, profile_definition), in_len, in);
175
176#ifdef BL2
177 case PLAT_OTP_ID_BL2_ROTPK_0:
178 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_0), in_len, in);
179 case PLAT_OTP_ID_BL2_ROTPK_1:
180 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_1), in_len, in);
181 case PLAT_OTP_ID_BL2_ROTPK_2:
182 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_rotpk_2), in_len, in);
183
184 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
185 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_0), in_len, in);
186 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
187 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_1), in_len, in);
188 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
189 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl2_nv_counter_2), in_len, in);
190#endif /* Bl2 */
191
192#ifdef BL1
193 case PLAT_OTP_ID_BL1_ROTPK_0:
194 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_rotpk_0), in_len, in);
195
196 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
197 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, bl1_nv_counter_0), in_len, in);
198#endif /* BL1 */
199
200 case PLAT_OTP_ID_ENTROPY_SEED:
201 return read_from_input(id, offsetof(struct flash_otp_nv_counters_region_t, entropy_seed), in_len, in);
202
203 default:
204 return TFM_PLAT_ERR_UNSUPPORTED;
205 }
206}
Michel Jaouend0fd8d92021-10-14 09:22:41 +0200207#else
208enum tfm_plat_err_t tfm_plat_otp_write(enum tfm_otp_element_id_t id,
209 size_t in_len, const uint8_t *in)
210{
211 (void)id;
212 (void)in_len;
213 (void)in;
214 return TFM_PLAT_ERR_UNSUPPORTED;
215}
216#endif
Raef Coles148b9472021-06-18 08:48:17 +0100217
218enum tfm_plat_err_t tfm_plat_otp_get_size(enum tfm_otp_element_id_t id,
219 size_t *size)
220{
221 switch (id) {
222 case PLAT_OTP_ID_HUK:
223 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->huk);
224 break;
225 case PLAT_OTP_ID_IAK:
226 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak);
227 break;
228 case PLAT_OTP_ID_IAK_LEN:
229 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_len);
230 break;
231 case PLAT_OTP_ID_IAK_TYPE:
232 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_type);
233 break;
234 case PLAT_OTP_ID_IAK_ID:
235 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->iak_id);
236 break;
237
238 case PLAT_OTP_ID_BOOT_SEED:
239 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->boot_seed);
240 break;
241 case PLAT_OTP_ID_LCS:
242 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->lcs);
243 break;
244 case PLAT_OTP_ID_IMPLEMENTATION_ID:
245 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->implementation_id);
246 break;
247 case PLAT_OTP_ID_HW_VERSION:
248 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->hw_version);
249 break;
250 case PLAT_OTP_ID_VERIFICATION_SERVICE_URL:
251 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->verification_service_url);
252 break;
253 case PLAT_OTP_ID_PROFILE_DEFINITION:
254 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->profile_definition);
255 break;
256
257#ifdef BL2
258 case PLAT_OTP_ID_BL2_ROTPK_0:
259 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_0);
260 break;
261 case PLAT_OTP_ID_BL2_ROTPK_1:
262 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_1);
263 break;
264 case PLAT_OTP_ID_BL2_ROTPK_2:
265 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_rotpk_2);
266 break;
267
268 case PLAT_OTP_ID_NV_COUNTER_BL2_0:
269 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_0);
270 break;
271 case PLAT_OTP_ID_NV_COUNTER_BL2_1:
272 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_1);
273 break;
274 case PLAT_OTP_ID_NV_COUNTER_BL2_2:
275 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl2_nv_counter_2);
276 break;
277#endif /* BL2 */
278
279#ifdef BL1
280 case PLAT_OTP_ID_BL1_ROTPK_0:
281 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl1_rotpk_0);
282 break;
283
284 case PLAT_OTP_ID_NV_COUNTER_BL1_0:
285 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->bl1_nv_counter_0);
286 break;
287#endif /* BL1 */
288
289 case PLAT_OTP_ID_ENTROPY_SEED:
290 *size = sizeof(((struct flash_otp_nv_counters_region_t*)0)->entropy_seed);
291 break;
292
293 default:
294 return TFM_PLAT_ERR_UNSUPPORTED;
295 }
296
297 return TFM_PLAT_ERR_SUCCESS;
298}