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