Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2018 Xilinx, Inc. |
| 4 | * Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com> |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 10 | #include <env.h> |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 11 | #include <malloc.h> |
Ibai Erkiaga | c8a3efa | 2019-09-27 11:37:01 +0100 | [diff] [blame] | 12 | #include <zynqmp_firmware.h> |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 13 | #include <asm/arch/hardware.h> |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 14 | #include <asm/arch/sys_proto.h> |
| 15 | #include <asm/io.h> |
| 16 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 17 | static int do_zynqmp_verify_secure(struct cmd_tbl *cmdtp, int flag, int argc, |
| 18 | char *const argv[]) |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 19 | { |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 20 | u64 src_addr, addr; |
| 21 | u32 len, src_lo, src_hi; |
| 22 | u8 *key_ptr = NULL; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 23 | int ret; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 24 | u32 key_lo = 0; |
| 25 | u32 key_hi = 0; |
| 26 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 27 | |
| 28 | if (argc < 4) |
| 29 | return CMD_RET_USAGE; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 30 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 31 | src_addr = simple_strtoull(argv[2], NULL, 16); |
| 32 | len = simple_strtoul(argv[3], NULL, 16); |
| 33 | |
| 34 | if (argc == 5) |
| 35 | key_ptr = (uint8_t *)(uintptr_t)simple_strtoull(argv[4], |
| 36 | NULL, 16); |
| 37 | |
| 38 | if ((ulong)src_addr != ALIGN((ulong)src_addr, |
| 39 | CONFIG_SYS_CACHELINE_SIZE)) { |
| 40 | printf("Failed: source address not aligned:%lx\n", |
| 41 | (ulong)src_addr); |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 42 | return -EINVAL; |
| 43 | } |
| 44 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 45 | src_lo = lower_32_bits((ulong)src_addr); |
| 46 | src_hi = upper_32_bits((ulong)src_addr); |
| 47 | flush_dcache_range((ulong)src_addr, (ulong)(src_addr + len)); |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 48 | |
| 49 | if (key_ptr) { |
| 50 | key_lo = lower_32_bits((ulong)key_ptr); |
| 51 | key_hi = upper_32_bits((ulong)key_ptr); |
| 52 | flush_dcache_range((ulong)key_ptr, |
| 53 | (ulong)(key_ptr + KEY_PTR_LEN)); |
| 54 | } |
| 55 | |
Michal Simek | 4c3de37 | 2019-10-04 15:35:45 +0200 | [diff] [blame] | 56 | ret = xilinx_pm_request(PM_SECURE_IMAGE, src_lo, src_hi, |
| 57 | key_lo, key_hi, ret_payload); |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 58 | if (ret) { |
| 59 | printf("Failed: secure op status:0x%x\n", ret); |
| 60 | } else { |
| 61 | addr = (u64)ret_payload[1] << 32 | ret_payload[2]; |
| 62 | printf("Verified image at 0x%llx\n", addr); |
| 63 | env_set_hex("zynqmp_verified_img_addr", addr); |
| 64 | } |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 69 | static int do_zynqmp_mmio_read(struct cmd_tbl *cmdtp, int flag, int argc, |
| 70 | char *const argv[]) |
Vipul Kumar | 869ed8c | 2018-07-16 18:04:22 +0530 | [diff] [blame] | 71 | { |
| 72 | u32 read_val, addr; |
| 73 | int ret; |
| 74 | |
| 75 | if (argc != cmdtp->maxargs) |
| 76 | return CMD_RET_USAGE; |
| 77 | |
| 78 | addr = simple_strtoul(argv[2], NULL, 16); |
| 79 | |
| 80 | ret = zynqmp_mmio_read(addr, &read_val); |
| 81 | if (!ret) |
| 82 | printf("mmio read value at 0x%x = 0x%x\n", |
| 83 | addr, read_val); |
| 84 | else |
| 85 | printf("Failed: mmio read\n"); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 90 | static int do_zynqmp_mmio_write(struct cmd_tbl *cmdtp, int flag, int argc, |
| 91 | char *const argv[]) |
Vipul Kumar | 869ed8c | 2018-07-16 18:04:22 +0530 | [diff] [blame] | 92 | { |
| 93 | u32 addr, mask, val; |
| 94 | int ret; |
| 95 | |
| 96 | if (argc != cmdtp->maxargs) |
| 97 | return CMD_RET_USAGE; |
| 98 | |
| 99 | addr = simple_strtoul(argv[2], NULL, 16); |
| 100 | mask = simple_strtoul(argv[3], NULL, 16); |
| 101 | val = simple_strtoul(argv[4], NULL, 16); |
| 102 | |
| 103 | ret = zynqmp_mmio_write(addr, mask, val); |
| 104 | if (ret != 0) |
| 105 | printf("Failed: mmio write\n"); |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 110 | #ifdef CONFIG_DEFINE_TCM_OCM_MMAP |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 111 | static int do_zynqmp_tcm_init(struct cmd_tbl *cmdtp, int flag, int argc, |
| 112 | char *const argv[]) |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 113 | { |
| 114 | u8 mode; |
| 115 | |
| 116 | if (argc != cmdtp->maxargs) |
| 117 | return CMD_RET_USAGE; |
| 118 | |
| 119 | mode = simple_strtoul(argv[2], NULL, 16); |
| 120 | if (mode != TCM_LOCK && mode != TCM_SPLIT) { |
| 121 | printf("Mode should be either 0(lock)/1(split)\n"); |
| 122 | return CMD_RET_FAILURE; |
| 123 | } |
| 124 | |
| 125 | dcache_disable(); |
| 126 | tcm_init(mode); |
| 127 | dcache_enable(); |
| 128 | |
| 129 | return CMD_RET_SUCCESS; |
| 130 | } |
| 131 | #endif |
| 132 | |
Michal Simek | b0e4b23 | 2020-04-27 11:26:31 +0200 | [diff] [blame] | 133 | static int do_zynqmp_pmufw(struct cmd_tbl *cmdtp, int flag, int argc, |
| 134 | char * const argv[]) |
| 135 | { |
| 136 | u32 addr, size; |
| 137 | |
| 138 | if (argc != cmdtp->maxargs) |
| 139 | return CMD_RET_USAGE; |
| 140 | |
| 141 | addr = simple_strtoul(argv[2], NULL, 16); |
| 142 | size = simple_strtoul(argv[3], NULL, 16); |
| 143 | flush_dcache_range((ulong)addr, (ulong)(addr + size)); |
| 144 | |
| 145 | zynqmp_pmufw_load_config_object((const void *)(uintptr_t)addr, |
| 146 | (size_t)size); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 151 | static struct cmd_tbl cmd_zynqmp_sub[] = { |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 152 | U_BOOT_CMD_MKENT(secure, 5, 0, do_zynqmp_verify_secure, "", ""), |
Michal Simek | b0e4b23 | 2020-04-27 11:26:31 +0200 | [diff] [blame] | 153 | U_BOOT_CMD_MKENT(pmufw, 4, 0, do_zynqmp_pmufw, "", ""), |
Vipul Kumar | 869ed8c | 2018-07-16 18:04:22 +0530 | [diff] [blame] | 154 | U_BOOT_CMD_MKENT(mmio_read, 3, 0, do_zynqmp_mmio_read, "", ""), |
| 155 | U_BOOT_CMD_MKENT(mmio_write, 5, 0, do_zynqmp_mmio_write, "", ""), |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 156 | #ifdef CONFIG_DEFINE_TCM_OCM_MMAP |
| 157 | U_BOOT_CMD_MKENT(tcminit, 3, 0, do_zynqmp_tcm_init, "", ""), |
| 158 | #endif |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 159 | }; |
| 160 | |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 161 | /** |
| 162 | * do_zynqmp - Handle the "zynqmp" command-line command |
| 163 | * @cmdtp: Command data struct pointer |
| 164 | * @flag: Command flag |
| 165 | * @argc: Command-line argument count |
| 166 | * @argv: Array of command-line arguments |
| 167 | * |
| 168 | * Processes the zynqmp specific commands |
| 169 | * |
| 170 | * Return: return 0 on success and CMD_RET_USAGE incase of misuse and error |
| 171 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 172 | static int do_zynqmp(struct cmd_tbl *cmdtp, int flag, int argc, |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 173 | char *const argv[]) |
| 174 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 175 | struct cmd_tbl *c; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 176 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 177 | if (argc < 2) |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 178 | return CMD_RET_USAGE; |
| 179 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 180 | c = find_cmd_tbl(argv[1], &cmd_zynqmp_sub[0], |
| 181 | ARRAY_SIZE(cmd_zynqmp_sub)); |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 182 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 183 | if (c) |
| 184 | return c->cmd(c, flag, argc, argv); |
| 185 | else |
| 186 | return CMD_RET_USAGE; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /***************************************************/ |
| 190 | #ifdef CONFIG_SYS_LONGHELP |
| 191 | static char zynqmp_help_text[] = |
| 192 | "secure src len [key_addr] - verifies secure images of $len bytes\n" |
| 193 | " long at address $src. Optional key_addr\n" |
| 194 | " can be specified if user key needs to\n" |
Vipul Kumar | 869ed8c | 2018-07-16 18:04:22 +0530 | [diff] [blame] | 195 | " be used for decryption\n" |
| 196 | "zynqmp mmio_read address - read from address\n" |
| 197 | "zynqmp mmio_write address mask value - write value after masking to\n" |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 198 | " address\n" |
| 199 | #ifdef CONFIG_DEFINE_TCM_OCM_MMAP |
Michal Simek | 3a5642d | 2018-11-21 07:49:18 +0100 | [diff] [blame] | 200 | "zynqmp tcminit mode - Initialize the TCM with zeros. TCM needs to be\n" |
| 201 | " initialized before accessing to avoid ECC\n" |
| 202 | " errors. mode specifies in which mode TCM has\n" |
| 203 | " to be initialized. Supported modes will be\n" |
| 204 | " lock(0)/split(1)\n" |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 205 | #endif |
Michal Simek | b0e4b23 | 2020-04-27 11:26:31 +0200 | [diff] [blame] | 206 | "zynqmp pmufw address size - load PMU FW configuration object\n" |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 207 | ; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 208 | #endif |
| 209 | |
| 210 | U_BOOT_CMD( |
| 211 | zynqmp, 5, 1, do_zynqmp, |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 212 | "ZynqMP sub-system", |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 213 | zynqmp_help_text |
| 214 | ) |