1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
11 #include <dm/device.h>
12 #include <dm/uclass.h>
14 #define STM32_OTP_HASH_KEY_START 24
15 #define STM32_OTP_HASH_KEY_SIZE 8
17 static void read_hash_value(u32 addr)
21 for (i = 0; i < STM32_OTP_HASH_KEY_SIZE; i++) {
22 printf("OTP value %i: %x\n", STM32_OTP_HASH_KEY_START + i,
23 __be32_to_cpu(*(u32 *)addr));
28 static int fuse_hash_value(u32 addr, bool print)
34 ret = uclass_get_device_by_driver(UCLASS_MISC,
35 DM_DRIVER_GET(stm32mp_bsec),
38 log_err("Can't find stm32mp_bsec driver\n");
42 for (i = 0, word = STM32_OTP_HASH_KEY_START;
43 i < STM32_OTP_HASH_KEY_SIZE;
44 i++, word++, addr += 4) {
45 val = __be32_to_cpu(*(u32 *)addr);
47 printf("Fuse OTP %i : %x\n", word, val);
49 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
51 log_err("Fuse OTP %i failed\n", word);
54 /* on success, lock the OTP for HASH key */
56 ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
58 log_err("Lock OTP %i failed\n", word);
66 static int confirm_prog(void)
68 puts("Warning: Programming fuses is an irreversible operation!\n"
69 " This may brick your system.\n"
70 " Use this command only if you are sure of what you are doing!\n"
71 "\nReally perform this fuse programming? <y/N>\n");
76 puts("Fuse programming aborted\n");
80 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
87 addr = simple_strtoul(argv[1], NULL, 16);
91 read_hash_value(addr);
93 return CMD_RET_SUCCESS;
96 static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
102 return CMD_RET_USAGE;
105 if (strcmp(argv[1], "-y"))
106 return CMD_RET_USAGE;
110 addr = simple_strtoul(argv[argc - 1], NULL, 16);
112 return CMD_RET_USAGE;
114 if (!yes && !confirm_prog())
115 return CMD_RET_FAILURE;
117 if (fuse_hash_value(addr, !yes))
118 return CMD_RET_FAILURE;
120 printf("Hash key updated !\n");
122 return CMD_RET_SUCCESS;
125 static char stm32key_help_text[] =
126 "read <addr>: Read the hash stored at addr in memory\n"
127 "stm32key fuse [-y] <addr> : Fuse hash stored at addr in OTP\n";
129 U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Fuse ST Hash key", stm32key_help_text,
130 U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read),
131 U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse));