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