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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 133 | static struct cmd_tbl cmd_zynqmp_sub[] = { |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 134 | U_BOOT_CMD_MKENT(secure, 5, 0, do_zynqmp_verify_secure, "", ""), |
Vipul Kumar | 869ed8c | 2018-07-16 18:04:22 +0530 | [diff] [blame] | 135 | U_BOOT_CMD_MKENT(mmio_read, 3, 0, do_zynqmp_mmio_read, "", ""), |
| 136 | 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] | 137 | #ifdef CONFIG_DEFINE_TCM_OCM_MMAP |
| 138 | U_BOOT_CMD_MKENT(tcminit, 3, 0, do_zynqmp_tcm_init, "", ""), |
| 139 | #endif |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 140 | }; |
| 141 | |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 142 | /** |
| 143 | * do_zynqmp - Handle the "zynqmp" command-line command |
| 144 | * @cmdtp: Command data struct pointer |
| 145 | * @flag: Command flag |
| 146 | * @argc: Command-line argument count |
| 147 | * @argv: Array of command-line arguments |
| 148 | * |
| 149 | * Processes the zynqmp specific commands |
| 150 | * |
| 151 | * Return: return 0 on success and CMD_RET_USAGE incase of misuse and error |
| 152 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 153 | 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] | 154 | char *const argv[]) |
| 155 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 156 | struct cmd_tbl *c; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 157 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 158 | if (argc < 2) |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 159 | return CMD_RET_USAGE; |
| 160 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 161 | c = find_cmd_tbl(argv[1], &cmd_zynqmp_sub[0], |
| 162 | ARRAY_SIZE(cmd_zynqmp_sub)); |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 163 | |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 164 | if (c) |
| 165 | return c->cmd(c, flag, argc, argv); |
| 166 | else |
| 167 | return CMD_RET_USAGE; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /***************************************************/ |
| 171 | #ifdef CONFIG_SYS_LONGHELP |
| 172 | static char zynqmp_help_text[] = |
| 173 | "secure src len [key_addr] - verifies secure images of $len bytes\n" |
| 174 | " long at address $src. Optional key_addr\n" |
| 175 | " can be specified if user key needs to\n" |
Vipul Kumar | 869ed8c | 2018-07-16 18:04:22 +0530 | [diff] [blame] | 176 | " be used for decryption\n" |
| 177 | "zynqmp mmio_read address - read from address\n" |
| 178 | "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] | 179 | " address\n" |
| 180 | #ifdef CONFIG_DEFINE_TCM_OCM_MMAP |
Michal Simek | 3a5642d | 2018-11-21 07:49:18 +0100 | [diff] [blame] | 181 | "zynqmp tcminit mode - Initialize the TCM with zeros. TCM needs to be\n" |
| 182 | " initialized before accessing to avoid ECC\n" |
| 183 | " errors. mode specifies in which mode TCM has\n" |
| 184 | " to be initialized. Supported modes will be\n" |
| 185 | " lock(0)/split(1)\n" |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 186 | #endif |
| 187 | ; |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 188 | #endif |
| 189 | |
| 190 | U_BOOT_CMD( |
| 191 | zynqmp, 5, 1, do_zynqmp, |
Vipul Kumar | 95a3f87 | 2018-07-11 15:18:28 +0530 | [diff] [blame] | 192 | "ZynqMP sub-system", |
Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 193 | zynqmp_help_text |
| 194 | ) |