Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 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> |
Patrick Delaunay | eb653ac | 2020-11-06 19:01:29 +0100 | [diff] [blame] | 9 | #include <log.h> |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 10 | #include <misc.h> |
Patrick Delaunay | 33a909a | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 11 | #include <asm/arch/bsec.h> |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 12 | #include <dm/device.h> |
| 13 | #include <dm/uclass.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 14 | #include <linux/printk.h> |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 15 | |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 16 | /* |
| 17 | * Closed device: OTP0 |
| 18 | * STM32MP15x: bit 6 of OPT0 |
| 19 | * STM32MP13x: 0b111111 = 0x3F for OTP_SECURED closed device |
| 20 | */ |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 21 | #define STM32_OTP_CLOSE_ID 0 |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 22 | #define STM32_OTP_STM32MP13x_CLOSE_MASK 0x3F |
| 23 | #define STM32_OTP_STM32MP15x_CLOSE_MASK BIT(6) |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 24 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 25 | /* PKH is the first element of the key list */ |
| 26 | #define STM32KEY_PKH 0 |
| 27 | |
| 28 | struct stm32key { |
| 29 | char *name; |
| 30 | char *desc; |
| 31 | u8 start; |
| 32 | u8 size; |
| 33 | }; |
| 34 | |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 35 | const struct stm32key stm32mp13_list[] = { |
| 36 | [STM32KEY_PKH] = { |
| 37 | .name = "PKHTH", |
| 38 | .desc = "Hash of the 8 ECC Public Keys Hashes Table (ECDSA is the authentication algorithm)", |
| 39 | .start = 24, |
| 40 | .size = 8, |
| 41 | }, |
| 42 | { |
| 43 | .name = "EDMK", |
| 44 | .desc = "Encryption/Decryption Master Key", |
| 45 | .start = 92, |
| 46 | .size = 4, |
| 47 | } |
| 48 | }; |
| 49 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 50 | const struct stm32key stm32mp15_list[] = { |
| 51 | [STM32KEY_PKH] = { |
| 52 | .name = "PKH", |
| 53 | .desc = "Hash of the ECC Public Key (ECDSA is the authentication algorithm)", |
| 54 | .start = 24, |
| 55 | .size = 8, |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | /* index of current selected key in stm32key list, 0 = PKH by default */ |
| 60 | static u8 stm32key_index; |
| 61 | |
| 62 | static u8 get_key_nb(void) |
| 63 | { |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 64 | if (IS_ENABLED(CONFIG_STM32MP13x)) |
| 65 | return ARRAY_SIZE(stm32mp13_list); |
| 66 | |
| 67 | if (IS_ENABLED(CONFIG_STM32MP15x)) |
| 68 | return ARRAY_SIZE(stm32mp15_list); |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static const struct stm32key *get_key(u8 index) |
| 72 | { |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 73 | if (IS_ENABLED(CONFIG_STM32MP13x)) |
| 74 | return &stm32mp13_list[index]; |
| 75 | |
| 76 | if (IS_ENABLED(CONFIG_STM32MP15x)) |
| 77 | return &stm32mp15_list[index]; |
| 78 | } |
| 79 | |
| 80 | static u32 get_otp_close_mask(void) |
| 81 | { |
| 82 | if (IS_ENABLED(CONFIG_STM32MP13x)) |
| 83 | return STM32_OTP_STM32MP13x_CLOSE_MASK; |
| 84 | |
| 85 | if (IS_ENABLED(CONFIG_STM32MP15x)) |
| 86 | return STM32_OTP_STM32MP15x_CLOSE_MASK; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 87 | } |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 88 | |
Patrick Delaunay | e00e1f3 | 2021-06-28 14:56:01 +0200 | [diff] [blame] | 89 | static int get_misc_dev(struct udevice **dev) |
| 90 | { |
| 91 | int ret; |
| 92 | |
| 93 | ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), dev); |
| 94 | if (ret) |
| 95 | log_err("Can't find stm32mp_bsec driver\n"); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 100 | static void read_key_value(const struct stm32key *key, u32 addr) |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 101 | { |
| 102 | int i; |
| 103 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 104 | for (i = 0; i < key->size; i++) { |
| 105 | printf("%s OTP %i: [%08x] %08x\n", key->name, key->start + i, |
| 106 | addr, __be32_to_cpu(*(u32 *)addr)); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 107 | addr += 4; |
| 108 | } |
| 109 | } |
| 110 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 111 | static int read_key_otp(struct udevice *dev, const struct stm32key *key, bool print, bool *locked) |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 112 | { |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 113 | int i, word, ret; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 114 | int nb_invalid = 0, nb_zero = 0, nb_lock = 0, nb_lock_err = 0; |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 115 | u32 val, lock; |
| 116 | bool status; |
| 117 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 118 | for (i = 0, word = key->start; i < key->size; i++, word++) { |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 119 | ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4); |
| 120 | if (ret != 4) |
| 121 | val = ~0x0; |
| 122 | ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4); |
| 123 | if (ret != 4) |
Patrick Delaunay | c6327ba | 2022-09-15 18:11:38 +0200 | [diff] [blame] | 124 | lock = BSEC_LOCK_ERROR; |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 125 | if (print) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 126 | printf("%s OTP %i: %08x lock : %08x\n", key->name, word, val, lock); |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 127 | if (val == ~0x0) |
| 128 | nb_invalid++; |
| 129 | else if (val == 0x0) |
| 130 | nb_zero++; |
Patrick Delaunay | c6327ba | 2022-09-15 18:11:38 +0200 | [diff] [blame] | 131 | if (lock & BSEC_LOCK_PERM) |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 132 | nb_lock++; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 133 | if (lock & BSEC_LOCK_ERROR) |
| 134 | nb_lock_err++; |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 137 | status = nb_lock_err || (nb_lock == key->size); |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 138 | if (locked) |
| 139 | *locked = status; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 140 | if (nb_lock_err && print) |
| 141 | printf("%s lock is invalid!\n", key->name); |
| 142 | else if (!status && print) |
| 143 | printf("%s is not locked!\n", key->name); |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 144 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 145 | if (nb_invalid == key->size) { |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 146 | if (print) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 147 | printf("%s is invalid!\n", key->name); |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 148 | return -EINVAL; |
| 149 | } |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 150 | if (nb_zero == key->size) { |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 151 | if (print) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 152 | printf("%s is free!\n", key->name); |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 153 | return -ENOENT; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 159 | static int read_close_status(struct udevice *dev, bool print, bool *closed) |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 160 | { |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 161 | int word, ret, result; |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 162 | u32 val, lock, mask; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 163 | bool status; |
| 164 | |
| 165 | result = 0; |
| 166 | word = STM32_OTP_CLOSE_ID; |
| 167 | ret = misc_read(dev, STM32_BSEC_OTP(word), &val, 4); |
| 168 | if (ret < 0) |
| 169 | result = ret; |
| 170 | if (ret != 4) |
| 171 | val = 0x0; |
| 172 | |
| 173 | ret = misc_read(dev, STM32_BSEC_LOCK(word), &lock, 4); |
| 174 | if (ret < 0) |
| 175 | result = ret; |
| 176 | if (ret != 4) |
| 177 | lock = BSEC_LOCK_ERROR; |
| 178 | |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 179 | mask = get_otp_close_mask(); |
| 180 | status = (val & mask) == mask; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 181 | if (closed) |
| 182 | *closed = status; |
| 183 | if (print) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 184 | printf("OTP %d: closed status: %d lock : %08x\n", word, status, lock); |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 185 | |
| 186 | return result; |
| 187 | } |
| 188 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 189 | static int fuse_key_value(struct udevice *dev, const struct stm32key *key, u32 addr, bool print) |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 190 | { |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 191 | u32 word, val; |
| 192 | int i, ret; |
| 193 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 194 | for (i = 0, word = key->start; i < key->size; i++, word++, addr += 4) { |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 195 | val = __be32_to_cpu(*(u32 *)addr); |
Patrick Delaunay | fe24090 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 196 | if (print) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 197 | printf("Fuse %s OTP %i : %08x\n", key->name, word, val); |
Patrick Delaunay | fe24090 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 198 | |
| 199 | ret = misc_write(dev, STM32_BSEC_OTP(word), &val, 4); |
| 200 | if (ret != 4) { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 201 | log_err("Fuse %s OTP %i failed\n", key->name, word); |
Patrick Delaunay | fe24090 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 202 | return ret; |
| 203 | } |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 204 | /* on success, lock the OTP for the key */ |
Patrick Delaunay | c6327ba | 2022-09-15 18:11:38 +0200 | [diff] [blame] | 205 | val = BSEC_LOCK_PERM; |
Patrick Delaunay | 3da2552 | 2021-06-28 14:56:00 +0200 | [diff] [blame] | 206 | ret = misc_write(dev, STM32_BSEC_LOCK(word), &val, 4); |
| 207 | if (ret != 4) { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 208 | log_err("Lock %s OTP %i failed\n", key->name, word); |
Patrick Delaunay | 3da2552 | 2021-06-28 14:56:00 +0200 | [diff] [blame] | 209 | return ret; |
| 210 | } |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 211 | } |
Patrick Delaunay | fe24090 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 212 | |
| 213 | return 0; |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int confirm_prog(void) |
| 217 | { |
| 218 | puts("Warning: Programming fuses is an irreversible operation!\n" |
| 219 | " This may brick your system.\n" |
| 220 | " Use this command only if you are sure of what you are doing!\n" |
| 221 | "\nReally perform this fuse programming? <y/N>\n"); |
| 222 | |
| 223 | if (confirm_yesno()) |
| 224 | return 1; |
| 225 | |
| 226 | puts("Fuse programming aborted\n"); |
| 227 | return 0; |
| 228 | } |
| 229 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 230 | static void display_key_info(const struct stm32key *key) |
| 231 | { |
| 232 | printf("%s : %s\n", key->name, key->desc); |
| 233 | printf("\tOTP%d..%d\n", key->start, key->start + key->size); |
| 234 | } |
| 235 | |
| 236 | static int do_stm32key_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 237 | { |
| 238 | int i; |
| 239 | |
| 240 | for (i = 0; i < get_key_nb(); i++) |
| 241 | display_key_info(get_key(i)); |
| 242 | |
| 243 | return CMD_RET_SUCCESS; |
| 244 | } |
| 245 | |
| 246 | static int do_stm32key_select(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 247 | { |
| 248 | const struct stm32key *key; |
| 249 | int i; |
| 250 | |
| 251 | if (argc == 1) { |
| 252 | printf("Selected key:\n"); |
| 253 | key = get_key(stm32key_index); |
| 254 | display_key_info(key); |
| 255 | return CMD_RET_SUCCESS; |
| 256 | } |
| 257 | |
| 258 | for (i = 0; i < get_key_nb(); i++) { |
| 259 | key = get_key(i); |
| 260 | if (!strcmp(key->name, argv[1])) { |
| 261 | printf("%s selected\n", key->name); |
| 262 | stm32key_index = i; |
| 263 | return CMD_RET_SUCCESS; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | printf("Unknown key %s\n", argv[1]); |
| 268 | |
| 269 | return CMD_RET_FAILURE; |
| 270 | } |
| 271 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 272 | static int do_stm32key_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 273 | { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 274 | const struct stm32key *key; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 275 | struct udevice *dev; |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 276 | u32 addr; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 277 | int ret, i; |
| 278 | int result; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 279 | |
| 280 | ret = get_misc_dev(&dev); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 281 | |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 282 | if (argc == 1) { |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 283 | if (ret) |
| 284 | return CMD_RET_FAILURE; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 285 | key = get_key(stm32key_index); |
| 286 | ret = read_key_otp(dev, key, true, NULL); |
| 287 | if (ret != -ENOENT) |
| 288 | return CMD_RET_FAILURE; |
| 289 | return CMD_RET_SUCCESS; |
| 290 | } |
| 291 | |
| 292 | if (!strcmp("-a", argv[1])) { |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 293 | if (ret) |
| 294 | return CMD_RET_FAILURE; |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 295 | result = CMD_RET_SUCCESS; |
| 296 | for (i = 0; i < get_key_nb(); i++) { |
| 297 | key = get_key(i); |
| 298 | ret = read_key_otp(dev, key, true, NULL); |
| 299 | if (ret != -ENOENT) |
| 300 | result = CMD_RET_FAILURE; |
| 301 | } |
| 302 | ret = read_close_status(dev, true, NULL); |
| 303 | if (ret) |
| 304 | result = CMD_RET_FAILURE; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 305 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 306 | return result; |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 307 | } |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 308 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 309 | addr = hextoul(argv[1], NULL); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 310 | if (!addr) |
| 311 | return CMD_RET_USAGE; |
| 312 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 313 | key = get_key(stm32key_index); |
| 314 | printf("Read %s at 0x%08x\n", key->name, addr); |
| 315 | read_key_value(key, addr); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 316 | |
| 317 | return CMD_RET_SUCCESS; |
| 318 | } |
| 319 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 320 | static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 321 | { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 322 | const struct stm32key *key = get_key(stm32key_index); |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 323 | struct udevice *dev; |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 324 | u32 addr; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 325 | int ret; |
| 326 | bool yes = false, lock; |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 327 | |
| 328 | if (argc < 2) |
| 329 | return CMD_RET_USAGE; |
| 330 | |
| 331 | if (argc == 3) { |
| 332 | if (strcmp(argv[1], "-y")) |
| 333 | return CMD_RET_USAGE; |
| 334 | yes = true; |
| 335 | } |
| 336 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 337 | addr = hextoul(argv[argc - 1], NULL); |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 338 | if (!addr) |
| 339 | return CMD_RET_USAGE; |
| 340 | |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 341 | ret = get_misc_dev(&dev); |
| 342 | if (ret) |
| 343 | return CMD_RET_FAILURE; |
| 344 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 345 | if (read_key_otp(dev, key, !yes, &lock) != -ENOENT) { |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 346 | printf("Error: can't fuse again the OTP\n"); |
| 347 | return CMD_RET_FAILURE; |
| 348 | } |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 349 | if (lock) { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 350 | printf("Error: %s is locked\n", key->name); |
Patrick Delaunay | d3551b8 | 2021-06-28 14:56:02 +0200 | [diff] [blame] | 351 | return CMD_RET_FAILURE; |
| 352 | } |
| 353 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 354 | if (!yes) { |
| 355 | printf("Writing %s with\n", key->name); |
| 356 | read_key_value(key, addr); |
| 357 | } |
| 358 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 359 | if (!yes && !confirm_prog()) |
| 360 | return CMD_RET_FAILURE; |
| 361 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 362 | if (fuse_key_value(dev, key, addr, !yes)) |
Patrick Delaunay | fe24090 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 363 | return CMD_RET_FAILURE; |
| 364 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 365 | printf("%s updated !\n", key->name); |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 366 | |
| 367 | return CMD_RET_SUCCESS; |
| 368 | } |
| 369 | |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 370 | static int do_stm32key_close(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 371 | { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 372 | const struct stm32key *key; |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 373 | bool yes, lock, closed; |
| 374 | struct udevice *dev; |
| 375 | u32 val; |
| 376 | int ret; |
| 377 | |
| 378 | yes = false; |
| 379 | if (argc == 2) { |
| 380 | if (strcmp(argv[1], "-y")) |
| 381 | return CMD_RET_USAGE; |
| 382 | yes = true; |
| 383 | } |
| 384 | |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 385 | ret = get_misc_dev(&dev); |
| 386 | if (ret) |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 387 | return CMD_RET_FAILURE; |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 388 | |
| 389 | if (read_close_status(dev, !yes, &closed)) |
| 390 | return CMD_RET_FAILURE; |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 391 | |
| 392 | if (closed) { |
| 393 | printf("Error: already closed!\n"); |
| 394 | return CMD_RET_FAILURE; |
| 395 | } |
| 396 | |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 397 | /* check PKH status before to close */ |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 398 | key = get_key(STM32KEY_PKH); |
| 399 | ret = read_key_otp(dev, key, !yes, &lock); |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 400 | if (ret) { |
| 401 | if (ret == -ENOENT) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 402 | printf("Error: %s not programmed!\n", key->name); |
Patrick Delaunay | 8921b3d | 2022-09-15 18:11:39 +0200 | [diff] [blame] | 403 | return CMD_RET_FAILURE; |
| 404 | } |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 405 | if (!lock) |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 406 | printf("Warning: %s not locked!\n", key->name); |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 407 | |
| 408 | if (!yes && !confirm_prog()) |
| 409 | return CMD_RET_FAILURE; |
| 410 | |
Patrick Delaunay | e83cef8 | 2022-09-15 18:11:41 +0200 | [diff] [blame] | 411 | val = get_otp_close_mask(); |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 412 | ret = misc_write(dev, STM32_BSEC_OTP(STM32_OTP_CLOSE_ID), &val, 4); |
| 413 | if (ret != 4) { |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 414 | printf("Error: can't update OTP %d\n", STM32_OTP_CLOSE_ID); |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 415 | return CMD_RET_FAILURE; |
| 416 | } |
| 417 | |
| 418 | printf("Device is closed !\n"); |
| 419 | |
| 420 | return CMD_RET_SUCCESS; |
| 421 | } |
| 422 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 423 | static char stm32key_help_text[] = |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 424 | "list : list the supported key with description\n" |
| 425 | "stm32key select [<key>] : Select the key identified by <key> or display the key used for read/fuse command\n" |
| 426 | "stm32key read [<addr> | -a ] : Read the curent key at <addr> or current / all (-a) key in OTP\n" |
| 427 | "stm32key fuse [-y] <addr> : Fuse the current key at addr in OTP\n" |
| 428 | "stm32key close [-y] : Close the device\n"; |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 429 | |
Patrick Delaunay | fd1f4c9 | 2022-09-15 18:11:40 +0200 | [diff] [blame] | 430 | U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Manage key on STM32", stm32key_help_text, |
| 431 | U_BOOT_SUBCMD_MKENT(list, 1, 0, do_stm32key_list), |
| 432 | U_BOOT_SUBCMD_MKENT(select, 2, 0, do_stm32key_select), |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 433 | U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read), |
Patrick Delaunay | 80cfc6c | 2021-06-28 14:56:03 +0200 | [diff] [blame] | 434 | U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse), |
| 435 | U_BOOT_SUBCMD_MKENT(close, 2, 0, do_stm32key_close)); |