1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4  */
5 
6 #include <common.h>
7 #include <command.h>
8 #include <console.h>
9 #include <log.h>
10 #include <misc.h>
11 #include <asm/arch/bsec.h>
12 #include <dm/device.h>
13 #include <dm/uclass.h>
14 
15 /*
16  * Closed device: OTP0
17  * STM32MP15x: bit 6 of OPT0
18  * STM32MP13x: 0b111111 = 0x3F for OTP_SECURED closed device
19  */
20 #define STM32_OTP_CLOSE_ID		0
21 #define STM32_OTP_STM32MP13x_CLOSE_MASK	0x3F
22 #define STM32_OTP_STM32MP15x_CLOSE_MASK	BIT(6)
23 
24 /* PKH is the first element of the key list */
25 #define STM32KEY_PKH 0
26 
27 struct stm32key {
28 	char *name;
29 	char *desc;
30 	u8 start;
31 	u8 size;
32 };
33 
34 const struct stm32key stm32mp13_list[] = {
35 	[STM32KEY_PKH] = {
36 		.name = "PKHTH",
37 		.desc = "Hash of the 8 ECC Public Keys Hashes Table (ECDSA is the authentication algorithm)",
38 		.start = 24,
39 		.size = 8,
40 	},
41 	{
42 		.name = "EDMK",
43 		.desc = "Encryption/Decryption Master Key",
44 		.start = 92,
45 		.size = 4,
46 	}
47 };
48 
49 const struct stm32key stm32mp15_list[] = {
50 	[STM32KEY_PKH] = {
51 		.name = "PKH",
52 		.desc = "Hash of the ECC Public Key (ECDSA is the authentication algorithm)",
53 		.start = 24,
54 		.size = 8,
55 	}
56 };
57 
58 /* index of current selected key in stm32key list, 0 = PKH by default */
59 static u8 stm32key_index;
60 
get_key_nb(void)61 static u8 get_key_nb(void)
62 {
63 	if (IS_ENABLED(CONFIG_STM32MP13x))
64 		return ARRAY_SIZE(stm32mp13_list);
65 
66 	if (IS_ENABLED(CONFIG_STM32MP15x))
67 		return ARRAY_SIZE(stm32mp15_list);
68 }
69 
get_key(u8 index)70 static const struct stm32key *get_key(u8 index)
71 {
72 	if (IS_ENABLED(CONFIG_STM32MP13x))
73 		return &stm32mp13_list[index];
74 
75 	if (IS_ENABLED(CONFIG_STM32MP15x))
76 		return &stm32mp15_list[index];
77 }
78 
get_otp_close_mask(void)79 static u32 get_otp_close_mask(void)
80 {
81 	if (IS_ENABLED(CONFIG_STM32MP13x))
82 		return STM32_OTP_STM32MP13x_CLOSE_MASK;
83 
84 	if (IS_ENABLED(CONFIG_STM32MP15x))
85 		return STM32_OTP_STM32MP15x_CLOSE_MASK;
86 }
87 
get_misc_dev(struct udevice ** dev)88 static int get_misc_dev(struct udevice **dev)
89 {
90 	int ret;
91 
92 	ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev);
93 	if (ret)
94 		log_err("Can't find stm32mp_bsec driver\n");
95 
96 	return ret;
97 }
98 
read_key_value(const struct stm32key * key,u32 addr)99 static void read_key_value(const struct stm32key *key, u32 addr)
100 {
101 	int i;
102 
103 	for (i = 0; i < key->size; i++) {
104 		printf("%s OTP %i: [%08x] %08x\n", key->name, key->start + i,
105 		       addr, __be32_to_cpu(*(u32 *)addr));
106 		addr += 4;
107 	}
108 }
109 
read_key_otp(struct udevice * dev,const struct stm32key * key,bool print,bool * locked)110 static int read_key_otp(struct udevice *dev, const struct stm32key *key, bool print, bool *locked)
111 {
112 	int i, word, ret;
113 	int nb_invalid = 0, nb_zero = 0, nb_lock = 0, nb_lock_err = 0;
114 	u32 val, lock;
115 	bool status;
116 
117 	for (i = 0, word = key->start; i < key->size; i++, word++) {
118 		ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
119 		if (ret != 4)
120 			val = ~0x0;
121 		ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
122 		if (ret != 4)
123 			lock = BSEC_LOCK_ERROR;
124 		if (print)
125 			printf("%s OTP %i: %08x lock : %08x\n", key->name, word, val, lock);
126 		if (val == ~0x0)
127 			nb_invalid++;
128 		else if (val == 0x0)
129 			nb_zero++;
130 		if (lock & BSEC_LOCK_PERM)
131 			nb_lock++;
132 		if (lock & BSEC_LOCK_ERROR)
133 			nb_lock_err++;
134 	}
135 
136 	status = nb_lock_err || (nb_lock == key->size);
137 	if (locked)
138 		*locked = status;
139 	if (nb_lock_err && print)
140 		printf("%s lock is invalid!\n", key->name);
141 	else if (!status && print)
142 		printf("%s is not locked!\n", key->name);
143 
144 	if (nb_invalid == key->size) {
145 		if (print)
146 			printf("%s is invalid!\n", key->name);
147 		return -EINVAL;
148 	}
149 	if (nb_zero == key->size) {
150 		if (print)
151 			printf("%s is free!\n", key->name);
152 		return -ENOENT;
153 	}
154 
155 	return 0;
156 }
157 
read_close_status(struct udevice * dev,bool print,bool * closed)158 static int read_close_status(struct udevice *dev, bool print, bool *closed)
159 {
160 	int word, ret, result;
161 	u32 val, lock, mask;
162 	bool status;
163 
164 	result = 0;
165 	word = STM32_OTP_CLOSE_ID;
166 	ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
167 	if (ret < 0)
168 		result = ret;
169 	if (ret != 4)
170 		val = 0x0;
171 
172 	ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
173 	if (ret < 0)
174 		result = ret;
175 	if (ret != 4)
176 		lock = BSEC_LOCK_ERROR;
177 
178 	mask = get_otp_close_mask();
179 	status = (val & mask) == mask;
180 	if (closed)
181 		*closed = status;
182 	if (print)
183 		printf("OTP %d: closed status: %d lock : %08x\n", word, status, lock);
184 
185 	return result;
186 }
187 
fuse_key_value(struct udevice * dev,const struct stm32key * key,u32 addr,bool print)188 static int fuse_key_value(struct udevice *dev, const struct stm32key *key, u32 addr, bool print)
189 {
190 	u32 word, val;
191 	int i, ret;
192 
193 	for (i = 0, word = key->start; i < key->size; i++, word++, addr += 4) {
194 		val = __be32_to_cpu(*(u32 *)addr);
195 		if (print)
196 			printf("Fuse %s OTP %i : %08x\n", key->name, word, val);
197 
198 		ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
199 		if (ret != 4) {
200 			log_err("Fuse %s OTP %i failed\n", key->name, word);
201 			return ret;
202 		}
203 		/* on success, lock the OTP for the key */
204 		val = BSEC_LOCK_PERM;
205 		ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
206 		if (ret != 4) {
207 			log_err("Lock %s OTP %i failed\n", key->name, word);
208 			return ret;
209 		}
210 	}
211 
212 	return 0;
213 }
214 
confirm_prog(void)215 static int confirm_prog(void)
216 {
217 	puts("Warning: Programming fuses is an irreversible operation!\n"
218 			"         This may brick your system.\n"
219 			"         Use this command only if you are sure of what you are doing!\n"
220 			"\nReally perform this fuse programming? <y/N>\n");
221 
222 	if (confirm_yesno())
223 		return 1;
224 
225 	puts("Fuse programming aborted\n");
226 	return 0;
227 }
228 
display_key_info(const struct stm32key * key)229 static void display_key_info(const struct stm32key *key)
230 {
231 	printf("%s : %s\n", key->name, key->desc);
232 	printf("\tOTP%d..%d\n", key->start, key->start + key->size);
233 }
234 
do_stm32key_list(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])235 static int do_stm32key_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
236 {
237 	int i;
238 
239 	for (i = 0; i < get_key_nb(); i++)
240 		display_key_info(get_key(i));
241 
242 	return CMD_RET_SUCCESS;
243 }
244 
do_stm32key_select(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])245 static int do_stm32key_select(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
246 {
247 	const struct stm32key *key;
248 	int i;
249 
250 	if (argc == 1) {
251 		printf("Selected key:\n");
252 		key = get_key(stm32key_index);
253 		display_key_info(key);
254 		return CMD_RET_SUCCESS;
255 	}
256 
257 	for (i = 0; i < get_key_nb(); i++) {
258 		key = get_key(i);
259 		if (!strcmp(key->name, argv[1])) {
260 			printf("%s selected\n", key->name);
261 			stm32key_index = i;
262 			return CMD_RET_SUCCESS;
263 		}
264 	}
265 
266 	printf("Unknown key %s\n", argv[1]);
267 
268 	return CMD_RET_FAILURE;
269 }
270 
do_stm32key_read(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])271 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
272 {
273 	const struct stm32key *key;
274 	struct udevice *dev;
275 	u32 addr;
276 	int ret, i;
277 	int result;
278 
279 	ret = get_misc_dev(&dev);
280 
281 	if (argc == 1) {
282 		if (ret)
283 			return CMD_RET_FAILURE;
284 		key = get_key(stm32key_index);
285 		ret = read_key_otp(dev, key, true, NULL);
286 		if (ret != -ENOENT)
287 			return CMD_RET_FAILURE;
288 		return CMD_RET_SUCCESS;
289 	}
290 
291 	if (!strcmp("-a", argv[1])) {
292 		if (ret)
293 			return CMD_RET_FAILURE;
294 		result = CMD_RET_SUCCESS;
295 		for (i = 0; i < get_key_nb(); i++) {
296 			key = get_key(i);
297 			ret = read_key_otp(dev, key, true, NULL);
298 			if (ret != -ENOENT)
299 				result = CMD_RET_FAILURE;
300 		}
301 		ret = read_close_status(dev, true, NULL);
302 		if (ret)
303 			result = CMD_RET_FAILURE;
304 
305 		return result;
306 	}
307 
308 	addr = hextoul(argv[1], NULL);
309 	if (!addr)
310 		return CMD_RET_USAGE;
311 
312 	key = get_key(stm32key_index);
313 	printf("Read %s at 0x%08x\n", key->name, addr);
314 	read_key_value(key, addr);
315 
316 	return CMD_RET_SUCCESS;
317 }
318 
do_stm32key_fuse(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])319 static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
320 {
321 	const struct stm32key *key = get_key(stm32key_index);
322 	struct udevice *dev;
323 	u32 addr;
324 	int ret;
325 	bool yes = false, lock;
326 
327 	if (argc < 2)
328 		return CMD_RET_USAGE;
329 
330 	if (argc == 3) {
331 		if (strcmp(argv[1], "-y"))
332 			return CMD_RET_USAGE;
333 		yes = true;
334 	}
335 
336 	addr = hextoul(argv[argc - 1], NULL);
337 	if (!addr)
338 		return CMD_RET_USAGE;
339 
340 	ret = get_misc_dev(&dev);
341 	if (ret)
342 		return CMD_RET_FAILURE;
343 
344 	if (read_key_otp(dev, key, !yes, &lock) != -ENOENT) {
345 		printf("Error: can't fuse again the OTP\n");
346 		return CMD_RET_FAILURE;
347 	}
348 	if (lock) {
349 		printf("Error: %s is locked\n", key->name);
350 		return CMD_RET_FAILURE;
351 	}
352 
353 	if (!yes) {
354 		printf("Writing %s with\n", key->name);
355 		read_key_value(key, addr);
356 	}
357 
358 	if (!yes && !confirm_prog())
359 		return CMD_RET_FAILURE;
360 
361 	if (fuse_key_value(dev, key, addr, !yes))
362 		return CMD_RET_FAILURE;
363 
364 	printf("%s updated !\n", key->name);
365 
366 	return CMD_RET_SUCCESS;
367 }
368 
do_stm32key_close(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])369 static int do_stm32key_close(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
370 {
371 	const struct stm32key *key;
372 	bool yes, lock, closed;
373 	struct udevice *dev;
374 	u32 val;
375 	int ret;
376 
377 	yes = false;
378 	if (argc == 2) {
379 		if (strcmp(argv[1], "-y"))
380 			return CMD_RET_USAGE;
381 		yes = true;
382 	}
383 
384 	ret = get_misc_dev(&dev);
385 	if (ret)
386 		return CMD_RET_FAILURE;
387 
388 	if (read_close_status(dev, !yes, &closed))
389 		return CMD_RET_FAILURE;
390 
391 	if (closed) {
392 		printf("Error: already closed!\n");
393 		return CMD_RET_FAILURE;
394 	}
395 
396 	/* check PKH status before to close */
397 	key = get_key(STM32KEY_PKH);
398 	ret = read_key_otp(dev, key, !yes, &lock);
399 	if (ret) {
400 		if (ret == -ENOENT)
401 			printf("Error: %s not programmed!\n", key->name);
402 		return CMD_RET_FAILURE;
403 	}
404 	if (!lock)
405 		printf("Warning: %s not locked!\n", key->name);
406 
407 	if (!yes && !confirm_prog())
408 		return CMD_RET_FAILURE;
409 
410 	val = get_otp_close_mask();
411 	ret = misc_write(dev, STM32_BSEC_OTP(STM32_OTP_CLOSE_ID), &val, 4);
412 	if (ret != 4) {
413 		printf("Error: can't update OTP %d\n", STM32_OTP_CLOSE_ID);
414 		return CMD_RET_FAILURE;
415 	}
416 
417 	printf("Device is closed !\n");
418 
419 	return CMD_RET_SUCCESS;
420 }
421 
422 static char stm32key_help_text[] =
423 	"list : list the supported key with description\n"
424 	"stm32key select [<key>] : Select the key identified by <key> or display the key used for read/fuse command\n"
425 	"stm32key read [<addr> | -a ] : Read the curent key at <addr> or current / all (-a) key in OTP\n"
426 	"stm32key fuse [-y] <addr> : Fuse the current key at addr in OTP\n"
427 	"stm32key close [-y] : Close the device\n";
428 
429 U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Manage key on STM32", stm32key_help_text,
430 	U_BOOT_SUBCMD_MKENT(list, 1, 0, do_stm32key_list),
431 	U_BOOT_SUBCMD_MKENT(select, 2, 0, do_stm32key_select),
432 	U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read),
433 	U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse),
434 	U_BOOT_SUBCMD_MKENT(close, 2, 0, do_stm32key_close));
435