1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
10 #include <asm/arch/bsec.h>
11 #include <dm/device.h>
12 #include <dm/uclass.h>
13 #include <linux/printk.h>
17 * STM32MP15x: bit 6 of OPT0
18 * STM32MP13x: 0b111111 = 0x3F for OTP_SECURED closed device
20 #define STM32_OTP_CLOSE_ID 0
21 #define STM32_OTP_STM32MP13X_CLOSE_MASK 0x3F
22 #define STM32_OTP_STM32MP15X_CLOSE_MASK BIT(6)
24 /* PKH is the first element of the key list */
25 #define STM32KEY_PKH 0
34 const struct stm32key stm32mp13_list[] = {
37 .desc = "Hash of the 8 ECC Public Keys Hashes Table (ECDSA is the authentication algorithm)",
43 .desc = "Encryption/Decryption Master Key",
49 const struct stm32key stm32mp15_list[] = {
52 .desc = "Hash of the ECC Public Key (ECDSA is the authentication algorithm)",
58 /* index of current selected key in stm32key list, 0 = PKH by default */
59 static u8 stm32key_index;
61 static u8 get_key_nb(void)
63 if (IS_ENABLED(CONFIG_STM32MP13X))
64 return ARRAY_SIZE(stm32mp13_list);
66 if (IS_ENABLED(CONFIG_STM32MP15X))
67 return ARRAY_SIZE(stm32mp15_list);
70 static const struct stm32key *get_key(u8 index)
72 if (IS_ENABLED(CONFIG_STM32MP13X))
73 return &stm32mp13_list[index];
75 if (IS_ENABLED(CONFIG_STM32MP15X))
76 return &stm32mp15_list[index];
79 static u32 get_otp_close_mask(void)
81 if (IS_ENABLED(CONFIG_STM32MP13X))
82 return STM32_OTP_STM32MP13X_CLOSE_MASK;
84 if (IS_ENABLED(CONFIG_STM32MP15X))
85 return STM32_OTP_STM32MP15X_CLOSE_MASK;
88 static int get_misc_dev(struct udevice **dev)
92 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev);
94 log_err("Can't find stm32mp_bsec driver\n");
99 static void read_key_value(const struct stm32key *key, u32 addr)
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));
110 static int read_key_otp(struct udevice *dev, const struct stm32key *key, bool print, bool *locked)
113 int nb_invalid = 0, nb_zero = 0, nb_lock = 0, nb_lock_err = 0;
117 for (i = 0, word = key->start; i < key->size; i++, word++) {
118 ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
121 ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
123 lock = BSEC_LOCK_ERROR;
125 printf("%s OTP %i: %08x lock : %08x\n", key->name, word, val, lock);
130 if (lock & BSEC_LOCK_PERM)
132 if (lock & BSEC_LOCK_ERROR)
136 status = nb_lock_err || (nb_lock == key->size);
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);
144 if (nb_invalid == key->size) {
146 printf("%s is invalid!\n", key->name);
149 if (nb_zero == key->size) {
151 printf("%s is free!\n", key->name);
158 static int read_close_status(struct udevice *dev, bool print, bool *closed)
160 int word, ret, result;
165 word = STM32_OTP_CLOSE_ID;
166 ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
172 ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
176 lock = BSEC_LOCK_ERROR;
178 mask = get_otp_close_mask();
179 status = (val & mask) == mask;
183 printf("OTP %d: closed status: %d lock : %08x\n", word, status, lock);
188 static int fuse_key_value(struct udevice *dev, const struct stm32key *key, u32 addr, bool print)
193 for (i = 0, word = key->start; i < key->size; i++, word++, addr += 4) {
194 val = __be32_to_cpu(*(u32 *)addr);
196 printf("Fuse %s OTP %i : %08x\n", key->name, word, val);
198 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
200 log_err("Fuse %s OTP %i failed\n", key->name, word);
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);
207 log_err("Lock %s OTP %i failed\n", key->name, word);
215 static int confirm_prog(void)
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");
225 puts("Fuse programming aborted\n");
229 static void display_key_info(const struct stm32key *key)
231 printf("%s : %s\n", key->name, key->desc);
232 printf("\tOTP%d..%d\n", key->start, key->start + key->size);
235 static int do_stm32key_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
239 for (i = 0; i < get_key_nb(); i++)
240 display_key_info(get_key(i));
242 return CMD_RET_SUCCESS;
245 static int do_stm32key_select(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
247 const struct stm32key *key;
251 printf("Selected key:\n");
252 key = get_key(stm32key_index);
253 display_key_info(key);
254 return CMD_RET_SUCCESS;
257 for (i = 0; i < get_key_nb(); i++) {
259 if (!strcmp(key->name, argv[1])) {
260 printf("%s selected\n", key->name);
262 return CMD_RET_SUCCESS;
266 printf("Unknown key %s\n", argv[1]);
268 return CMD_RET_FAILURE;
271 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
273 const struct stm32key *key;
279 ret = get_misc_dev(&dev);
283 return CMD_RET_FAILURE;
284 key = get_key(stm32key_index);
285 ret = read_key_otp(dev, key, true, NULL);
287 return CMD_RET_FAILURE;
288 return CMD_RET_SUCCESS;
291 if (!strcmp("-a", argv[1])) {
293 return CMD_RET_FAILURE;
294 result = CMD_RET_SUCCESS;
295 for (i = 0; i < get_key_nb(); i++) {
297 ret = read_key_otp(dev, key, true, NULL);
299 result = CMD_RET_FAILURE;
301 ret = read_close_status(dev, true, NULL);
303 result = CMD_RET_FAILURE;
308 addr = hextoul(argv[1], NULL);
310 return CMD_RET_USAGE;
312 key = get_key(stm32key_index);
313 printf("Read %s at 0x%08x\n", key->name, addr);
314 read_key_value(key, addr);
316 return CMD_RET_SUCCESS;
319 static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
321 const struct stm32key *key = get_key(stm32key_index);
325 bool yes = false, lock;
328 return CMD_RET_USAGE;
331 if (strcmp(argv[1], "-y"))
332 return CMD_RET_USAGE;
336 addr = hextoul(argv[argc - 1], NULL);
338 return CMD_RET_USAGE;
340 ret = get_misc_dev(&dev);
342 return CMD_RET_FAILURE;
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;
349 printf("Error: %s is locked\n", key->name);
350 return CMD_RET_FAILURE;
354 printf("Writing %s with\n", key->name);
355 read_key_value(key, addr);
358 if (!yes && !confirm_prog())
359 return CMD_RET_FAILURE;
361 if (fuse_key_value(dev, key, addr, !yes))
362 return CMD_RET_FAILURE;
364 printf("%s updated !\n", key->name);
366 return CMD_RET_SUCCESS;
369 static int do_stm32key_close(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
371 const struct stm32key *key;
372 bool yes, lock, closed;
379 if (strcmp(argv[1], "-y"))
380 return CMD_RET_USAGE;
384 ret = get_misc_dev(&dev);
386 return CMD_RET_FAILURE;
388 if (read_close_status(dev, !yes, &closed))
389 return CMD_RET_FAILURE;
392 printf("Error: already closed!\n");
393 return CMD_RET_FAILURE;
396 /* check PKH status before to close */
397 key = get_key(STM32KEY_PKH);
398 ret = read_key_otp(dev, key, !yes, &lock);
401 printf("Error: %s not programmed!\n", key->name);
402 return CMD_RET_FAILURE;
405 printf("Warning: %s not locked!\n", key->name);
407 if (!yes && !confirm_prog())
408 return CMD_RET_FAILURE;
410 val = get_otp_close_mask();
411 ret = misc_write(dev, STM32_BSEC_OTP(STM32_OTP_CLOSE_ID), &val, 4);
413 printf("Error: can't update OTP %d\n", STM32_OTP_CLOSE_ID);
414 return CMD_RET_FAILURE;
417 printf("Device is closed !\n");
419 return CMD_RET_SUCCESS;
422 U_BOOT_LONGHELP(stm32key,
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");
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));