blob: 886c52794f2fd104831fcec3427bee902fe87ca6 [file] [log] [blame]
Patrick Delaunayf4cb5d62019-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 Delaunayeb653ac2020-11-06 19:01:29 +01009#include <log.h>
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020010#include <misc.h>
11#include <dm/device.h>
12#include <dm/uclass.h>
13
14#define STM32_OTP_HASH_KEY_START 24
15#define STM32_OTP_HASH_KEY_SIZE 8
16
Patrick Delaunaye00e1f32021-06-28 14:56:01 +020017static int get_misc_dev(struct udevice **dev)
18{
19 int ret;
20
21 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev);
22 if (ret)
23 log_err("Can't find stm32mp_bsec driver\n");
24
25 return ret;
26}
27
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020028static void read_hash_value(u32 addr)
29{
30 int i;
31
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));
35 addr += 4;
36 }
37}
38
Patrick Delaunayfe240902021-06-28 14:55:59 +020039static int fuse_hash_value(u32 addr, bool print)
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020040{
41 struct udevice *dev;
42 u32 word, val;
43 int i, ret;
44
Patrick Delaunaye00e1f32021-06-28 14:56:01 +020045 ret = get_misc_dev(&dev);
46 if (ret)
Patrick Delaunayfe240902021-06-28 14:55:59 +020047 return ret;
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020048
Patrick Delaunay3da25522021-06-28 14:56:00 +020049 for (i = 0, word = STM32_OTP_HASH_KEY_START;
50 i < STM32_OTP_HASH_KEY_SIZE;
51 i++, word++, addr += 4) {
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020052 val = __be32_to_cpu(*(u32 *)addr);
Patrick Delaunayfe240902021-06-28 14:55:59 +020053 if (print)
54 printf("Fuse OTP %i : %x\n", word, val);
55
56 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
57 if (ret != 4) {
58 log_err("Fuse OTP %i failed\n", word);
59 return ret;
60 }
Patrick Delaunay3da25522021-06-28 14:56:00 +020061 /* on success, lock the OTP for HASH key */
62 val = 1;
63 ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
64 if (ret != 4) {
65 log_err("Lock OTP %i failed\n", word);
66 return ret;
67 }
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020068 }
Patrick Delaunayfe240902021-06-28 14:55:59 +020069
70 return 0;
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020071}
72
73static int confirm_prog(void)
74{
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");
79
80 if (confirm_yesno())
81 return 1;
82
83 puts("Fuse programming aborted\n");
84 return 0;
85}
86
Patrick Delaunaybc78d5f2021-06-28 14:55:58 +020087static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020088{
89 u32 addr;
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020090
Patrick Delaunaybc78d5f2021-06-28 14:55:58 +020091 if (argc == 1)
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020092 return CMD_RET_USAGE;
93
Patrick Delaunaybc78d5f2021-06-28 14:55:58 +020094 addr = simple_strtoul(argv[1], NULL, 16);
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020095 if (!addr)
96 return CMD_RET_USAGE;
97
Patrick Delaunaybc78d5f2021-06-28 14:55:58 +020098 read_hash_value(addr);
Patrick Delaunayf4cb5d62019-07-05 17:20:17 +020099
100 return CMD_RET_SUCCESS;
101}
102
Patrick Delaunaybc78d5f2021-06-28 14:55:58 +0200103static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
104{
105 u32 addr;
106 bool yes = false;
107
108 if (argc < 2)
109 return CMD_RET_USAGE;
110
111 if (argc == 3) {
112 if (strcmp(argv[1], "-y"))
113 return CMD_RET_USAGE;
114 yes = true;
115 }
116
117 addr = simple_strtoul(argv[argc - 1], NULL, 16);
118 if (!addr)
119 return CMD_RET_USAGE;
120
121 if (!yes && !confirm_prog())
122 return CMD_RET_FAILURE;
123
Patrick Delaunayfe240902021-06-28 14:55:59 +0200124 if (fuse_hash_value(addr, !yes))
125 return CMD_RET_FAILURE;
126
Patrick Delaunaybc78d5f2021-06-28 14:55:58 +0200127 printf("Hash key updated !\n");
128
129 return CMD_RET_SUCCESS;
130}
131
132static 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";
135
136U_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));