stm32mp: cmd_stm32key: lock of PKH OTP after fuse
[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 void read_hash_value(u32 addr)
18 {
19         int i;
20
21         for (i = 0; i < STM32_OTP_HASH_KEY_SIZE; i++) {
22                 printf("OTP value %i: %x\n", STM32_OTP_HASH_KEY_START + i,
23                        __be32_to_cpu(*(u32 *)addr));
24                 addr += 4;
25         }
26 }
27
28 static int fuse_hash_value(u32 addr, bool print)
29 {
30         struct udevice *dev;
31         u32 word, val;
32         int i, ret;
33
34         ret = uclass_get_device_by_driver(UCLASS_MISC,
35                                           DM_DRIVER_GET(stm32mp_bsec),
36                                           &dev);
37         if (ret) {
38                 log_err("Can't find stm32mp_bsec driver\n");
39                 return ret;
40         }
41
42         for (i = 0, word = STM32_OTP_HASH_KEY_START;
43              i < STM32_OTP_HASH_KEY_SIZE;
44              i++, word++, addr += 4) {
45                 val = __be32_to_cpu(*(u32 *)addr);
46                 if (print)
47                         printf("Fuse OTP %i : %x\n", word, val);
48
49                 ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4);
50                 if (ret != 4) {
51                         log_err("Fuse OTP %i failed\n", word);
52                         return ret;
53                 }
54                 /* on success, lock the OTP for HASH key */
55                 val = 1;
56                 ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4);
57                 if (ret != 4) {
58                         log_err("Lock OTP %i failed\n", word);
59                         return ret;
60                 }
61         }
62
63         return 0;
64 }
65
66 static int confirm_prog(void)
67 {
68         puts("Warning: Programming fuses is an irreversible operation!\n"
69                         "         This may brick your system.\n"
70                         "         Use this command only if you are sure of what you are doing!\n"
71                         "\nReally perform this fuse programming? <y/N>\n");
72
73         if (confirm_yesno())
74                 return 1;
75
76         puts("Fuse programming aborted\n");
77         return 0;
78 }
79
80 static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
81 {
82         u32 addr;
83
84         if (argc == 1)
85                 return CMD_RET_USAGE;
86
87         addr = simple_strtoul(argv[1], NULL, 16);
88         if (!addr)
89                 return CMD_RET_USAGE;
90
91         read_hash_value(addr);
92
93         return CMD_RET_SUCCESS;
94 }
95
96 static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
97 {
98         u32 addr;
99         bool yes = false;
100
101         if (argc < 2)
102                 return CMD_RET_USAGE;
103
104         if (argc == 3) {
105                 if (strcmp(argv[1], "-y"))
106                         return CMD_RET_USAGE;
107                 yes = true;
108         }
109
110         addr = simple_strtoul(argv[argc - 1], NULL, 16);
111         if (!addr)
112                 return CMD_RET_USAGE;
113
114         if (!yes && !confirm_prog())
115                 return CMD_RET_FAILURE;
116
117         if (fuse_hash_value(addr, !yes))
118                 return CMD_RET_FAILURE;
119
120         printf("Hash key updated !\n");
121
122         return CMD_RET_SUCCESS;
123 }
124
125 static char stm32key_help_text[] =
126         "read <addr>: Read the hash stored at addr in memory\n"
127         "stm32key fuse [-y] <addr> : Fuse hash stored at addr in OTP\n";
128
129 U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Fuse ST Hash key", stm32key_help_text,
130         U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read),
131         U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse));