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> |
| 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 void 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, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 35 | DM_DRIVER_GET(stm32mp_bsec), |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 36 | &dev); |
| 37 | if (ret) { |
Patrick Delaunay | eb653ac | 2020-11-06 19:01:29 +0100 | [diff] [blame] | 38 | log_err("Can't find stm32mp_bsec driver\n"); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | |
| 42 | for (i = 0; i < STM32_OTP_HASH_KEY_SIZE; i++) { |
| 43 | if (print) |
| 44 | printf("Fuse OTP %i : %x\n", |
| 45 | STM32_OTP_HASH_KEY_START + i, |
| 46 | __be32_to_cpu(*(u32 *)addr)); |
| 47 | |
| 48 | word = STM32_OTP_HASH_KEY_START + i; |
| 49 | val = __be32_to_cpu(*(u32 *)addr); |
| 50 | misc_write(dev, STM32_BSEC_OTP(word), &val, 4); |
| 51 | |
| 52 | addr += 4; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static int confirm_prog(void) |
| 57 | { |
| 58 | puts("Warning: Programming fuses is an irreversible operation!\n" |
| 59 | " This may brick your system.\n" |
| 60 | " Use this command only if you are sure of what you are doing!\n" |
| 61 | "\nReally perform this fuse programming? <y/N>\n"); |
| 62 | |
| 63 | if (confirm_yesno()) |
| 64 | return 1; |
| 65 | |
| 66 | puts("Fuse programming aborted\n"); |
| 67 | return 0; |
| 68 | } |
| 69 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame^] | 70 | 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] | 71 | { |
| 72 | u32 addr; |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 73 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame^] | 74 | if (argc == 1) |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 75 | return CMD_RET_USAGE; |
| 76 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame^] | 77 | addr = simple_strtoul(argv[1], NULL, 16); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 78 | if (!addr) |
| 79 | return CMD_RET_USAGE; |
| 80 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame^] | 81 | read_hash_value(addr); |
Patrick Delaunay | f4cb5d6 | 2019-07-05 17:20:17 +0200 | [diff] [blame] | 82 | |
| 83 | return CMD_RET_SUCCESS; |
| 84 | } |
| 85 | |
Patrick Delaunay | bc78d5f | 2021-06-28 14:55:58 +0200 | [diff] [blame^] | 86 | static int do_stm32key_fuse(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 87 | { |
| 88 | u32 addr; |
| 89 | bool yes = false; |
| 90 | |
| 91 | if (argc < 2) |
| 92 | return CMD_RET_USAGE; |
| 93 | |
| 94 | if (argc == 3) { |
| 95 | if (strcmp(argv[1], "-y")) |
| 96 | return CMD_RET_USAGE; |
| 97 | yes = true; |
| 98 | } |
| 99 | |
| 100 | addr = simple_strtoul(argv[argc - 1], NULL, 16); |
| 101 | if (!addr) |
| 102 | return CMD_RET_USAGE; |
| 103 | |
| 104 | if (!yes && !confirm_prog()) |
| 105 | return CMD_RET_FAILURE; |
| 106 | |
| 107 | fuse_hash_value(addr, !yes); |
| 108 | printf("Hash key updated !\n"); |
| 109 | |
| 110 | return CMD_RET_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | static char stm32key_help_text[] = |
| 114 | "read <addr>: Read the hash stored at addr in memory\n" |
| 115 | "stm32key fuse [-y] <addr> : Fuse hash stored at addr in OTP\n"; |
| 116 | |
| 117 | U_BOOT_CMD_WITH_SUBCMDS(stm32key, "Fuse ST Hash key", stm32key_help_text, |
| 118 | U_BOOT_SUBCMD_MKENT(read, 2, 0, do_stm32key_read), |
| 119 | U_BOOT_SUBCMD_MKENT(fuse, 3, 0, do_stm32key_fuse)); |