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