stm32mp: cmd_stm32key: add get_misc_dev function
[platform/kernel/u-boot.git] / arch / arm / mach-stm32mp / cmd_stm32key.c
1 // 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>
9 #include <log.h>
10 #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
17 static 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
28 static 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
39 static int fuse_hash_value(u32 addr, bool print)
40 {
41         struct udevice *dev;
42         u32 word, val;
43         int i, ret;
44
45         ret = get_misc_dev(&dev);
46         if (ret)
47                 return ret;
48
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);
53                 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                 }
61                 /* 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                 }
68         }
69
70         return 0;
71 }
72
73 static 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
87 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
88 {
89         u32 addr;
90
91         if (argc == 1)
92                 return CMD_RET_USAGE;
93
94         addr = simple_strtoul(argv[1], NULL, 16);
95         if (!addr)
96                 return CMD_RET_USAGE;
97
98         read_hash_value(addr);
99
100         return CMD_RET_SUCCESS;
101 }
102
103 static 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
124         if (fuse_hash_value(addr, !yes))
125                 return CMD_RET_FAILURE;
126
127         printf("Hash key updated !\n");
128
129         return CMD_RET_SUCCESS;
130 }
131
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";
135
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));