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 | |
| 14 | #define STM32_OTP_HASH_KEY_START 24 |
| 15 | #define STM32_OTP_HASH_KEY_SIZE 8 |
| 16 | |
Patrick Delaunay | bd0233a | 2021-06-28 14:56:01 +0200 | [diff] [blame^] | 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 | |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 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 | |
Patrick Delaunay | c80a0e4 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 39 | static int fuse_hash_value(u32 addr, bool print) |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 40 | { |
| 41 | struct udevice *dev; |
| 42 | u32 word, val; |
| 43 | int i, ret; |
| 44 | |
Patrick Delaunay | bd0233a | 2021-06-28 14:56:01 +0200 | [diff] [blame^] | 45 | ret = get_misc_dev(&dev); |
| 46 | if (ret) |
Patrick Delaunay | c80a0e4 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 47 | return ret; |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 48 | |
Patrick Delaunay | cd8bfb3 | 2021-06-28 14:56:00 +0200 | [diff] [blame] | 49 | for (i = 0, word = STM32_OTP_HASH_KEY_START; |
| 50 | i < STM32_OTP_HASH_KEY_SIZE; |
| 51 | i++, word++, addr += 4) { |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 52 | val = __be32_to_cpu(*(u32 *)addr); |
Patrick Delaunay | c80a0e4 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 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 | } |
Patrick Delaunay | cd8bfb3 | 2021-06-28 14:56:00 +0200 | [diff] [blame] | 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 | } |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 68 | } |
Patrick Delaunay | c80a0e4 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 69 | |
| 70 | return 0; |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 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 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | u32 addr; |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 90 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 91 | if (argc == 1) |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 92 | return CMD_RET_USAGE; |
| 93 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 94 | addr = simple_strtoul(argv[1], NULL, 16); |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 95 | if (!addr) |
| 96 | return CMD_RET_USAGE; |
| 97 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 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; |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 107 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 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; |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 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 | |
Patrick Delaunay | c80a0e4 | 2021-06-28 14:55:59 +0200 | [diff] [blame] | 124 | if (fuse_hash_value(addr, !yes)) |
| 125 | return CMD_RET_FAILURE; |
| 126 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 127 | printf("Hash key updated !\n"); |
| 128 | |
Patrick Delaunay | 109d13f | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 129 | return CMD_RET_SUCCESS; |
| 130 | } |
| 131 | |
Patrick Delaunay | 8b5fe51 | 2021-06-28 14:55:58 +0200 | [diff] [blame] | 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)); |