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 /* Closed device : bit 6 of OPT0*/
15 #define STM32_OTP_CLOSE_ID 0
16 #define STM32_OTP_CLOSE_MASK BIT(6)
18 /* HASH of key: 8 OTPs, starting with OTP24) */
19 #define STM32_OTP_HASH_KEY_START 24
20 #define STM32_OTP_HASH_KEY_SIZE 8
22 static int get_misc_dev(struct udevice **dev)
26 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev);
28 log_err("Can't find stm32mp_bsec driver\n");
33 static void read_hash_value(u32 addr)
37 printf("Read KEY at 0x%x\n", addr);
38 for (i = 0; i < STM32_OTP_HASH_KEY_SIZE; i++) {
39 printf("OTP value %i: %x\n", STM32_OTP_HASH_KEY_START + i,
40 __be32_to_cpu(*(u32 *)addr));
45 static int read_hash_otp(bool print, bool *locked, bool *closed)
49 int nb_invalid = 0, nb_zero = 0, nb_lock = 0;
53 ret = get_misc_dev(&dev);
57 for (i = 0, word = STM32_OTP_HASH_KEY_START; i < STM32_OTP_HASH_KEY_SIZE; i++, word++) {
58 ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
61 ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
65 printf("OTP HASH %i: %x lock : %d\n", word, val, lock);
74 word = STM32_OTP_CLOSE_ID;
75 ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
78 ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
82 status = (val & STM32_OTP_CLOSE_MASK) == STM32_OTP_CLOSE_MASK;
86 printf("OTP %d: closed status: %d lock : %d\n", word, status, lock);
88 status = (nb_lock == STM32_OTP_HASH_KEY_SIZE);
92 printf("Hash of key is not locked!\n");
94 if (nb_invalid == STM32_OTP_HASH_KEY_SIZE) {
96 printf("Hash of key is invalid!\n");
99 if (nb_zero == STM32_OTP_HASH_KEY_SIZE) {
101 printf("Hash of key is free!\n");
108 static int fuse_hash_value(u32 addr, bool print)
114 ret = get_misc_dev(&dev);
118 for (i = 0, word = STM32_OTP_HASH_KEY_START;
119 i < STM32_OTP_HASH_KEY_SIZE;
120 i++, word++, addr += 4) {
121 val = __be32_to_cpu(*(u32 *)addr);
123 printf("Fuse OTP %i : %x\n", word, val);
125 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
127 log_err("Fuse OTP %i failed\n", word);
130 /* on success, lock the OTP for HASH key */
132 ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
134 log_err("Lock OTP %i failed\n", word);
142 static int confirm_prog(void)
144 puts("Warning: Programming fuses is an irreversible operation!\n"
145 " This may brick your system.\n"
146 " Use this command only if you are sure of what you are doing!\n"
147 "\nReally perform this fuse programming? <y/N>\n");
152 puts("Fuse programming aborted\n");
156 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
161 read_hash_otp(true, NULL, NULL);
162 return CMD_RET_SUCCESS;
165 addr = hextoul(argv[1], NULL);
167 return CMD_RET_USAGE;
169 read_hash_value(addr);
171 return CMD_RET_SUCCESS;
174 static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
177 bool yes = false, lock, closed;
180 return CMD_RET_USAGE;
183 if (strcmp(argv[1], "-y"))
184 return CMD_RET_USAGE;
188 addr = hextoul(argv[argc - 1], NULL);
190 return CMD_RET_USAGE;
192 if (read_hash_otp(!yes, &lock, &closed) != -ENOENT) {
193 printf("Error: can't fuse again the OTP\n");
194 return CMD_RET_FAILURE;
197 if (lock || closed) {
198 printf("Error: invalid OTP configuration (lock=%d, closed=%d)\n", lock, closed);
199 return CMD_RET_FAILURE;
202 if (!yes && !confirm_prog())
203 return CMD_RET_FAILURE;
205 if (fuse_hash_value(addr, !yes))
206 return CMD_RET_FAILURE;
208 printf("Hash key updated !\n");
210 return CMD_RET_SUCCESS;
213 static int do_stm32key_close(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
215 bool yes, lock, closed;
222 if (strcmp(argv[1], "-y"))
223 return CMD_RET_USAGE;
227 ret = read_hash_otp(!yes, &lock, &closed);
230 printf("Error: OTP not programmed!\n");
231 return CMD_RET_FAILURE;
235 printf("Error: already closed!\n");
236 return CMD_RET_FAILURE;
240 printf("Warning: OTP not locked!\n");
242 if (!yes && !confirm_prog())
243 return CMD_RET_FAILURE;
245 ret = get_misc_dev(&dev);
247 return CMD_RET_FAILURE;
249 val = STM32_OTP_CLOSE_MASK;
250 ret = misc_write(dev, STM32_BSEC_OTP(STM32_OTP_CLOSE_ID), &val, 4);
252 printf("Error: can't update OTP\n");
253 return CMD_RET_FAILURE;
256 printf("Device is closed !\n");
258 return CMD_RET_SUCCESS;
261 static char stm32key_help_text[] =
262 "read [<addr>]: Read the hash stored at addr in memory or in OTP\n"
263 "stm32key fuse [-y] <addr> : Fuse hash stored at addr in OTP\n"
264 "stm32key close [-y] : Close the device, the hash stored in OTP\n";
266 U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Fuse ST Hash key", stm32key_help_text,
267 U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read),
268 U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse),
269 U_BOOT_SUBCMD_MKENT(close, 2, 0, do_stm32key_close));