blob: c0d28a73e45dad55737a899d7592acaff5cb4693 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +05302/*
3 * (C) Copyright 2018 Xilinx, Inc.
4 * Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com>
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +05305 */
6
7#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060010#include <env.h>
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053011#include <malloc.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010012#include <zynqmp_firmware.h>
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +053013#include <asm/arch/hardware.h>
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053014#include <asm/arch/sys_proto.h>
15#include <asm/io.h>
16
Simon Glassed38aef2020-05-10 11:40:03 -060017static int do_zynqmp_verify_secure(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053019{
Vipul Kumar95a3f872018-07-11 15:18:28 +053020 u64 src_addr, addr;
21 u32 len, src_lo, src_hi;
22 u8 *key_ptr = NULL;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053023 int ret;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053024 u32 key_lo = 0;
25 u32 key_hi = 0;
26 u32 ret_payload[PAYLOAD_ARG_CNT];
Vipul Kumar95a3f872018-07-11 15:18:28 +053027
28 if (argc < 4)
29 return CMD_RET_USAGE;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053030
Vipul Kumar95a3f872018-07-11 15:18:28 +053031 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 Paladugub1acb652018-02-28 13:26:53 +053042 return -EINVAL;
43 }
44
Vipul Kumar95a3f872018-07-11 15:18:28 +053045 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 Paladugub1acb652018-02-28 13:26:53 +053048
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 Simek4c3de372019-10-04 15:35:45 +020056 ret = xilinx_pm_request(PM_SECURE_IMAGE, src_lo, src_hi,
57 key_lo, key_hi, ret_payload);
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053058 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 Glassed38aef2020-05-10 11:40:03 -060069static int do_zynqmp_mmio_read(struct cmd_tbl *cmdtp, int flag, int argc,
70 char *const argv[])
Vipul Kumar869ed8c2018-07-16 18:04:22 +053071{
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 Glassed38aef2020-05-10 11:40:03 -060090static int do_zynqmp_mmio_write(struct cmd_tbl *cmdtp, int flag, int argc,
91 char *const argv[])
Vipul Kumar869ed8c2018-07-16 18:04:22 +053092{
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 Paladugu48eaa0c2018-10-05 15:09:05 +0530110#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
Simon Glassed38aef2020-05-10 11:40:03 -0600111static int do_zynqmp_tcm_init(struct cmd_tbl *cmdtp, int flag, int argc,
112 char *const argv[])
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530113{
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 Simekb0e4b232020-04-27 11:26:31 +0200133static 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 Glassed38aef2020-05-10 11:40:03 -0600151static struct cmd_tbl cmd_zynqmp_sub[] = {
Vipul Kumar95a3f872018-07-11 15:18:28 +0530152 U_BOOT_CMD_MKENT(secure, 5, 0, do_zynqmp_verify_secure, "", ""),
Michal Simekb0e4b232020-04-27 11:26:31 +0200153 U_BOOT_CMD_MKENT(pmufw, 4, 0, do_zynqmp_pmufw, "", ""),
Vipul Kumar869ed8c2018-07-16 18:04:22 +0530154 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 Paladugu48eaa0c2018-10-05 15:09:05 +0530156#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
157 U_BOOT_CMD_MKENT(tcminit, 3, 0, do_zynqmp_tcm_init, "", ""),
158#endif
Vipul Kumar95a3f872018-07-11 15:18:28 +0530159};
160
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530161/**
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 Glassed38aef2020-05-10 11:40:03 -0600172static int do_zynqmp(struct cmd_tbl *cmdtp, int flag, int argc,
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530173 char *const argv[])
174{
Simon Glassed38aef2020-05-10 11:40:03 -0600175 struct cmd_tbl *c;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530176
Vipul Kumar95a3f872018-07-11 15:18:28 +0530177 if (argc < 2)
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530178 return CMD_RET_USAGE;
179
Vipul Kumar95a3f872018-07-11 15:18:28 +0530180 c = find_cmd_tbl(argv[1], &cmd_zynqmp_sub[0],
181 ARRAY_SIZE(cmd_zynqmp_sub));
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530182
Vipul Kumar95a3f872018-07-11 15:18:28 +0530183 if (c)
184 return c->cmd(c, flag, argc, argv);
185 else
186 return CMD_RET_USAGE;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530187}
188
189/***************************************************/
190#ifdef CONFIG_SYS_LONGHELP
191static 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 Kumar869ed8c2018-07-16 18:04:22 +0530195 " 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 Paladugu48eaa0c2018-10-05 15:09:05 +0530198 " address\n"
199#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
Michal Simek3a5642d2018-11-21 07:49:18 +0100200 "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 Paladugu48eaa0c2018-10-05 15:09:05 +0530205#endif
Michal Simekb0e4b232020-04-27 11:26:31 +0200206 "zynqmp pmufw address size - load PMU FW configuration object\n"
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530207 ;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530208#endif
209
210U_BOOT_CMD(
211 zynqmp, 5, 1, do_zynqmp,
Vipul Kumar95a3f872018-07-11 15:18:28 +0530212 "ZynqMP sub-system",
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530213 zynqmp_help_text
214)