blob: 4eac56082db622560bbd71214624d41f35adb443 [file] [log] [blame]
Patrick Delaunay109d13f2019-07-05 17:20:17 +02001// 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>
Patrick Delaunayba779402020-11-06 19:01:29 +01009#include <log.h>
Patrick Delaunay109d13f2019-07-05 17:20:17 +020010#include <misc.h>
11#include <dm/device.h>
12#include <dm/uclass.h>
13
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020014/* Closed device : bit 6 of OPT0*/
15#define STM32_OTP_CLOSE_ID 0
16#define STM32_OTP_CLOSE_MASK BIT(6)
17
Patrick Delaunay424876f2022-09-15 18:11:40 +020018/* PKH is the first element of the key list */
19#define STM32KEY_PKH 0
20
21struct stm32key {
22 char *name;
23 char *desc;
24 u8 start;
25 u8 size;
26};
27
28const struct stm32key stm32mp15_list[] = {
29 [STM32KEY_PKH] = {
30 .name = "PKH",
31 .desc = "Hash of the ECC Public Key (ECDSA is the authentication algorithm)",
32 .start = 24,
33 .size = 8,
34 }
35};
36
37/* index of current selected key in stm32key list, 0 = PKH by default */
38static u8 stm32key_index;
39
40static u8 get_key_nb(void)
41{
42 return ARRAY_SIZE(stm32mp15_list);
43}
44
45static const struct stm32key *get_key(u8 index)
46{
47 return &stm32mp15_list[index];
48}
Patrick Delaunay109d13f2019-07-05 17:20:17 +020049
Patrick Delaunay212eafc2022-09-15 18:11:38 +020050#define BSEC_LOCK_ERROR (-1)
51#define BSEC_LOCK_PERM BIT(0)
52
Patrick Delaunaybd0233a2021-06-28 14:56:01 +020053static int get_misc_dev(struct udevice **dev)
54{
55 int ret;
56
57 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev);
58 if (ret)
59 log_err("Can't find stm32mp_bsec driver\n");
60
61 return ret;
62}
63
Patrick Delaunay424876f2022-09-15 18:11:40 +020064static void read_key_value(const struct stm32key *key, u32 addr)
Patrick Delaunay109d13f2019-07-05 17:20:17 +020065{
66 int i;
67
Patrick Delaunay424876f2022-09-15 18:11:40 +020068 for (i = 0; i < key->size; i++) {
69 printf("%s OTP %i: [%08x] %08x\n", key->name, key->start + i,
70 addr, __be32_to_cpu(*(u32 *)addr));
Patrick Delaunay109d13f2019-07-05 17:20:17 +020071 addr += 4;
72 }
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020073}
74
Patrick Delaunay424876f2022-09-15 18:11:40 +020075static int read_key_otp(struct udevice *dev, const struct stm32key *key, bool print, bool *locked)
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020076{
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020077 int i, word, ret;
Patrick Delaunay424876f2022-09-15 18:11:40 +020078 int nb_invalid = 0, nb_zero = 0, nb_lock = 0, nb_lock_err = 0;
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020079 u32 val, lock;
80 bool status;
81
Patrick Delaunay424876f2022-09-15 18:11:40 +020082 for (i = 0, word = key->start; i < key->size; i++, word++) {
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020083 ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
84 if (ret != 4)
85 val = ~0x0;
86 ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
87 if (ret != 4)
Patrick Delaunay212eafc2022-09-15 18:11:38 +020088 lock = BSEC_LOCK_ERROR;
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020089 if (print)
Patrick Delaunay424876f2022-09-15 18:11:40 +020090 printf("%s OTP %i: %08x lock : %08x\n", key->name, word, val, lock);
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020091 if (val == ~0x0)
92 nb_invalid++;
93 else if (val == 0x0)
94 nb_zero++;
Patrick Delaunay212eafc2022-09-15 18:11:38 +020095 if (lock & BSEC_LOCK_PERM)
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020096 nb_lock++;
Patrick Delaunay424876f2022-09-15 18:11:40 +020097 if (lock & BSEC_LOCK_ERROR)
98 nb_lock_err++;
Patrick Delaunay7b094ea2021-06-28 14:56:02 +020099 }
100
Patrick Delaunay424876f2022-09-15 18:11:40 +0200101 status = nb_lock_err || (nb_lock == key->size);
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200102 if (locked)
103 *locked = status;
Patrick Delaunay424876f2022-09-15 18:11:40 +0200104 if (nb_lock_err && print)
105 printf("%s lock is invalid!\n", key->name);
106 else if (!status && print)
107 printf("%s is not locked!\n", key->name);
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200108
Patrick Delaunay424876f2022-09-15 18:11:40 +0200109 if (nb_invalid == key->size) {
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200110 if (print)
Patrick Delaunay424876f2022-09-15 18:11:40 +0200111 printf("%s is invalid!\n", key->name);
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200112 return -EINVAL;
113 }
Patrick Delaunay424876f2022-09-15 18:11:40 +0200114 if (nb_zero == key->size) {
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200115 if (print)
Patrick Delaunay424876f2022-09-15 18:11:40 +0200116 printf("%s is free!\n", key->name);
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200117 return -ENOENT;
118 }
119
120 return 0;
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200121}
122
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200123static int read_close_status(struct udevice *dev, bool print, bool *closed)
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200124{
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200125 int word, ret, result;
126 u32 val, lock;
127 bool status;
128
129 result = 0;
130 word = STM32_OTP_CLOSE_ID;
131 ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4);
132 if (ret < 0)
133 result = ret;
134 if (ret != 4)
135 val = 0x0;
136
137 ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4);
138 if (ret < 0)
139 result = ret;
140 if (ret != 4)
141 lock = BSEC_LOCK_ERROR;
142
143 status = (val & STM32_OTP_CLOSE_MASK) == STM32_OTP_CLOSE_MASK;
144 if (closed)
145 *closed = status;
146 if (print)
Patrick Delaunay424876f2022-09-15 18:11:40 +0200147 printf("OTP %d: closed status: %d lock : %08x\n", word, status, lock);
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200148
149 return result;
150}
151
Patrick Delaunay424876f2022-09-15 18:11:40 +0200152static int fuse_key_value(struct udevice *dev, const struct stm32key *key, u32 addr, bool print)
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200153{
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200154 u32 word, val;
155 int i, ret;
156
Patrick Delaunay424876f2022-09-15 18:11:40 +0200157 for (i = 0, word = key->start; i < key->size; i++, word++, addr += 4) {
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200158 val = __be32_to_cpu(*(u32 *)addr);
Patrick Delaunayc80a0e42021-06-28 14:55:59 +0200159 if (print)
Patrick Delaunay424876f2022-09-15 18:11:40 +0200160 printf("Fuse %s OTP %i : %08x\n", key->name, word, val);
Patrick Delaunayc80a0e42021-06-28 14:55:59 +0200161
162 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
163 if (ret != 4) {
Patrick Delaunay424876f2022-09-15 18:11:40 +0200164 log_err("Fuse %s OTP %i failed\n", key->name, word);
Patrick Delaunayc80a0e42021-06-28 14:55:59 +0200165 return ret;
166 }
Patrick Delaunay424876f2022-09-15 18:11:40 +0200167 /* on success, lock the OTP for the key */
Patrick Delaunay212eafc2022-09-15 18:11:38 +0200168 val = BSEC_LOCK_PERM;
Patrick Delaunaycd8bfb32021-06-28 14:56:00 +0200169 ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
170 if (ret != 4) {
Patrick Delaunay424876f2022-09-15 18:11:40 +0200171 log_err("Lock %s OTP %i failed\n", key->name, word);
Patrick Delaunaycd8bfb32021-06-28 14:56:00 +0200172 return ret;
173 }
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200174 }
Patrick Delaunayc80a0e42021-06-28 14:55:59 +0200175
176 return 0;
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200177}
178
179static int confirm_prog(void)
180{
181 puts("Warning: Programming fuses is an irreversible operation!\n"
182 " This may brick your system.\n"
183 " Use this command only if you are sure of what you are doing!\n"
184 "\nReally perform this fuse programming? <y/N>\n");
185
186 if (confirm_yesno())
187 return 1;
188
189 puts("Fuse programming aborted\n");
190 return 0;
191}
192
Patrick Delaunay424876f2022-09-15 18:11:40 +0200193static void display_key_info(const struct stm32key *key)
194{
195 printf("%s : %s\n", key->name, key->desc);
196 printf("\tOTP%d..%d\n", key->start, key->start + key->size);
197}
198
199static int do_stm32key_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
200{
201 int i;
202
203 for (i = 0; i < get_key_nb(); i++)
204 display_key_info(get_key(i));
205
206 return CMD_RET_SUCCESS;
207}
208
209static int do_stm32key_select(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
210{
211 const struct stm32key *key;
212 int i;
213
214 if (argc == 1) {
215 printf("Selected key:\n");
216 key = get_key(stm32key_index);
217 display_key_info(key);
218 return CMD_RET_SUCCESS;
219 }
220
221 for (i = 0; i < get_key_nb(); i++) {
222 key = get_key(i);
223 if (!strcmp(key->name, argv[1])) {
224 printf("%s selected\n", key->name);
225 stm32key_index = i;
226 return CMD_RET_SUCCESS;
227 }
228 }
229
230 printf("Unknown key %s\n", argv[1]);
231
232 return CMD_RET_FAILURE;
233}
234
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200235static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200236{
Patrick Delaunay424876f2022-09-15 18:11:40 +0200237 const struct stm32key *key;
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200238 struct udevice *dev;
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200239 u32 addr;
Patrick Delaunay424876f2022-09-15 18:11:40 +0200240 int ret, i;
241 int result;
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200242
243 ret = get_misc_dev(&dev);
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200244
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200245 if (argc == 1) {
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200246 if (ret)
247 return CMD_RET_FAILURE;
Patrick Delaunay424876f2022-09-15 18:11:40 +0200248 key = get_key(stm32key_index);
249 ret = read_key_otp(dev, key, true, NULL);
250 if (ret != -ENOENT)
251 return CMD_RET_FAILURE;
252 return CMD_RET_SUCCESS;
253 }
254
255 if (!strcmp("-a", argv[1])) {
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200256 if (ret)
257 return CMD_RET_FAILURE;
Patrick Delaunay424876f2022-09-15 18:11:40 +0200258 result = CMD_RET_SUCCESS;
259 for (i = 0; i < get_key_nb(); i++) {
260 key = get_key(i);
261 ret = read_key_otp(dev, key, true, NULL);
262 if (ret != -ENOENT)
263 result = CMD_RET_FAILURE;
264 }
265 ret = read_close_status(dev, true, NULL);
266 if (ret)
267 result = CMD_RET_FAILURE;
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200268
Patrick Delaunay424876f2022-09-15 18:11:40 +0200269 return result;
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200270 }
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200271
Simon Glass3ff49ec2021-07-24 09:03:29 -0600272 addr = hextoul(argv[1], NULL);
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200273 if (!addr)
274 return CMD_RET_USAGE;
275
Patrick Delaunay424876f2022-09-15 18:11:40 +0200276 key = get_key(stm32key_index);
277 printf("Read %s at 0x%08x\n", key->name, addr);
278 read_key_value(key, addr);
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200279
280 return CMD_RET_SUCCESS;
281}
282
283static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
284{
Patrick Delaunay424876f2022-09-15 18:11:40 +0200285 const struct stm32key *key = get_key(stm32key_index);
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200286 struct udevice *dev;
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200287 u32 addr;
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200288 int ret;
289 bool yes = false, lock;
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200290
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200291 if (argc < 2)
292 return CMD_RET_USAGE;
293
294 if (argc == 3) {
295 if (strcmp(argv[1], "-y"))
296 return CMD_RET_USAGE;
297 yes = true;
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200298 }
299
Simon Glass3ff49ec2021-07-24 09:03:29 -0600300 addr = hextoul(argv[argc - 1], NULL);
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200301 if (!addr)
302 return CMD_RET_USAGE;
303
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200304 ret = get_misc_dev(&dev);
305 if (ret)
306 return CMD_RET_FAILURE;
307
Patrick Delaunay424876f2022-09-15 18:11:40 +0200308 if (read_key_otp(dev, key, !yes, &lock) != -ENOENT) {
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200309 printf("Error: can't fuse again the OTP\n");
310 return CMD_RET_FAILURE;
311 }
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200312 if (lock) {
Patrick Delaunay424876f2022-09-15 18:11:40 +0200313 printf("Error: %s is locked\n", key->name);
Patrick Delaunay7b094ea2021-06-28 14:56:02 +0200314 return CMD_RET_FAILURE;
315 }
316
Patrick Delaunay424876f2022-09-15 18:11:40 +0200317 if (!yes) {
318 printf("Writing %s with\n", key->name);
319 read_key_value(key, addr);
320 }
321
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200322 if (!yes && !confirm_prog())
323 return CMD_RET_FAILURE;
324
Patrick Delaunay424876f2022-09-15 18:11:40 +0200325 if (fuse_key_value(dev, key, addr, !yes))
Patrick Delaunayc80a0e42021-06-28 14:55:59 +0200326 return CMD_RET_FAILURE;
327
Patrick Delaunay424876f2022-09-15 18:11:40 +0200328 printf("%s updated !\n", key->name);
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200329
Patrick Delaunay109d13f2019-07-05 17:20:17 +0200330 return CMD_RET_SUCCESS;
331}
332
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200333static int do_stm32key_close(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
334{
Patrick Delaunay424876f2022-09-15 18:11:40 +0200335 const struct stm32key *key;
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200336 bool yes, lock, closed;
337 struct udevice *dev;
338 u32 val;
339 int ret;
340
341 yes = false;
342 if (argc == 2) {
343 if (strcmp(argv[1], "-y"))
344 return CMD_RET_USAGE;
345 yes = true;
346 }
347
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200348 ret = get_misc_dev(&dev);
349 if (ret)
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200350 return CMD_RET_FAILURE;
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200351
352 if (read_close_status(dev, !yes, &closed))
353 return CMD_RET_FAILURE;
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200354
355 if (closed) {
356 printf("Error: already closed!\n");
357 return CMD_RET_FAILURE;
358 }
359
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200360 /* check PKH status before to close */
Patrick Delaunay424876f2022-09-15 18:11:40 +0200361 key = get_key(STM32KEY_PKH);
362 ret = read_key_otp(dev, key, !yes, &lock);
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200363 if (ret) {
364 if (ret == -ENOENT)
Patrick Delaunay424876f2022-09-15 18:11:40 +0200365 printf("Error: %s not programmed!\n", key->name);
Patrick Delaunay9b7c4d32022-09-15 18:11:39 +0200366 return CMD_RET_FAILURE;
367 }
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200368 if (!lock)
Patrick Delaunay424876f2022-09-15 18:11:40 +0200369 printf("Warning: %s not locked!\n", key->name);
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200370
371 if (!yes && !confirm_prog())
372 return CMD_RET_FAILURE;
373
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200374 val = STM32_OTP_CLOSE_MASK;
375 ret = misc_write(dev, STM32_BSEC_OTP(STM32_OTP_CLOSE_ID), &val, 4);
376 if (ret != 4) {
Patrick Delaunay424876f2022-09-15 18:11:40 +0200377 printf("Error: can't update OTP %d\n", STM32_OTP_CLOSE_ID);
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200378 return CMD_RET_FAILURE;
379 }
380
381 printf("Device is closed !\n");
382
383 return CMD_RET_SUCCESS;
384}
385
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200386static char stm32key_help_text[] =
Patrick Delaunay424876f2022-09-15 18:11:40 +0200387 "list : list the supported key with description\n"
388 "stm32key select [<key>] : Select the key identified by <key> or display the key used for read/fuse command\n"
389 "stm32key read [<addr> | -a ] : Read the curent key at <addr> or current / all (-a) key in OTP\n"
390 "stm32key fuse [-y] <addr> : Fuse the current key at addr in OTP\n"
391 "stm32key close [-y] : Close the device\n";
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200392
Patrick Delaunay424876f2022-09-15 18:11:40 +0200393U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Manage key on STM32", stm32key_help_text,
394 U_BOOT_SUBCMD_MKENT(list, 1, 0, do_stm32key_list),
395 U_BOOT_SUBCMD_MKENT(select, 2, 0, do_stm32key_select),
Patrick Delaunay8b5fe512021-06-28 14:55:58 +0200396 U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read),
Patrick Delaunayf4a9b4b2021-06-28 14:56:03 +0200397 U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse),
398 U_BOOT_SUBCMD_MKENT(close, 2, 0, do_stm32key_close));