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 int get_misc_dev(struct udevice **dev)
21 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev);
23 log_err("Can't find stm32mp_bsec driver\n");
28 static void read_hash_value(u32 addr)
32 for (i = 0; i < STM32_OTP_HASH_KEY_SIZE; i++) {
33 printf("OTP value %i: %x\n", STM32_OTP_HASH_KEY_START + i,
34 __be32_to_cpu(*(u32 *)addr));
39 static int fuse_hash_value(u32 addr, bool print)
45 ret = get_misc_dev(&dev);
49 for (i = 0, word = STM32_OTP_HASH_KEY_START;
50 i < STM32_OTP_HASH_KEY_SIZE;
51 i++, word++, addr += 4) {
52 val = __be32_to_cpu(*(u32 *)addr);
54 printf("Fuse OTP %i : %x\n", word, val);
56 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
58 log_err("Fuse OTP %i failed\n", word);
61 /* on success, lock the OTP for HASH key */
63 ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
65 log_err("Lock OTP %i failed\n", word);
73 static int confirm_prog(void)
75 puts("Warning: Programming fuses is an irreversible operation!\n"
76 " This may brick your system.\n"
77 " Use this command only if you are sure of what you are doing!\n"
78 "\nReally perform this fuse programming? <y/N>\n");
83 puts("Fuse programming aborted\n");
87 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
94 addr = simple_strtoul(argv[1], NULL, 16);
98 read_hash_value(addr);
100 return CMD_RET_SUCCESS;
103 static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
109 return CMD_RET_USAGE;
112 if (strcmp(argv[1], "-y"))
113 return CMD_RET_USAGE;
117 addr = simple_strtoul(argv[argc - 1], NULL, 16);
119 return CMD_RET_USAGE;
121 if (!yes && !confirm_prog())
122 return CMD_RET_FAILURE;
124 if (fuse_hash_value(addr, !yes))
125 return CMD_RET_FAILURE;
127 printf("Hash key updated !\n");
129 return CMD_RET_SUCCESS;
132 static char stm32key_help_text[] =
133 "read <addr>: Read the hash stored at addr in memory\n"
134 "stm32key fuse [-y] <addr> : Fuse hash stored at addr in OTP\n";
136 U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Fuse ST Hash key", stm32key_help_text,
137 U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read),
138 U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse));