blob: 0f1f26986f21309d2611d99f4841dd962ccaa1bc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Siva Durga Prasad Paladuguc436bf92018-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 Paladuguc436bf92018-02-28 13:26:53 +05305 */
6
7#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -06008#include <command.h>
Simon Glass9edefc22019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060010#include <env.h>
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +053011#include <malloc.h>
Ibai Erkiaga009ab7b2019-09-27 11:37:01 +010012#include <zynqmp_firmware.h>
Siva Durga Prasad Paladugu5860bc12018-10-05 15:09:05 +053013#include <asm/arch/hardware.h>
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +053014#include <asm/arch/sys_proto.h>
15#include <asm/io.h>
16
Simon Glass09140112020-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 Paladuguc436bf92018-02-28 13:26:53 +053019{
Vipul Kumar8c258e62018-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 Paladuguc436bf92018-02-28 13:26:53 +053023 int ret;
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +053024 u32 key_lo = 0;
25 u32 key_hi = 0;
26 u32 ret_payload[PAYLOAD_ARG_CNT];
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +053027
Vipul Kumar8c258e62018-07-11 15:18:28 +053028 if (argc < 4)
29 return CMD_RET_USAGE;
30
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 Paladuguc436bf92018-02-28 13:26:53 +053042 return -EINVAL;
43 }
44
Vipul Kumar8c258e62018-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 Paladuguc436bf92018-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 Simek40361952019-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 Paladuguc436bf92018-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 Glass09140112020-05-10 11:40:03 -060069static int do_zynqmp_mmio_read(struct cmd_tbl *cmdtp, int flag, int argc,
70 char *const argv[])
Vipul Kumar0358cee2018-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 Glass09140112020-05-10 11:40:03 -060090static int do_zynqmp_mmio_write(struct cmd_tbl *cmdtp, int flag, int argc,
91 char *const argv[])
Vipul Kumar0358cee2018-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 Paladugu5860bc12018-10-05 15:09:05 +0530110#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
Simon Glass09140112020-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 Paladugu5860bc12018-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
Simon Glass09140112020-05-10 11:40:03 -0600133static struct cmd_tbl cmd_zynqmp_sub[] = {
Vipul Kumar8c258e62018-07-11 15:18:28 +0530134 U_BOOT_CMD_MKENT(secure, 5, 0, do_zynqmp_verify_secure, "", ""),
Vipul Kumar0358cee2018-07-16 18:04:22 +0530135 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 Paladugu5860bc12018-10-05 15:09:05 +0530137#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
138 U_BOOT_CMD_MKENT(tcminit, 3, 0, do_zynqmp_tcm_init, "", ""),
139#endif
Vipul Kumar8c258e62018-07-11 15:18:28 +0530140};
141
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530142/**
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 Glass09140112020-05-10 11:40:03 -0600153static int do_zynqmp(struct cmd_tbl *cmdtp, int flag, int argc,
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530154 char *const argv[])
155{
Simon Glass09140112020-05-10 11:40:03 -0600156 struct cmd_tbl *c;
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530157
Vipul Kumar8c258e62018-07-11 15:18:28 +0530158 if (argc < 2)
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530159 return CMD_RET_USAGE;
160
Vipul Kumar8c258e62018-07-11 15:18:28 +0530161 c = find_cmd_tbl(argv[1], &cmd_zynqmp_sub[0],
162 ARRAY_SIZE(cmd_zynqmp_sub));
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530163
Vipul Kumar8c258e62018-07-11 15:18:28 +0530164 if (c)
165 return c->cmd(c, flag, argc, argv);
166 else
167 return CMD_RET_USAGE;
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530168}
169
170/***************************************************/
171#ifdef CONFIG_SYS_LONGHELP
172static 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 Kumar0358cee2018-07-16 18:04:22 +0530176 " 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 Paladugu5860bc12018-10-05 15:09:05 +0530179 " address\n"
180#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
Michal Simek4c848442018-11-21 07:49:18 +0100181 "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 Paladugu5860bc12018-10-05 15:09:05 +0530186#endif
187 ;
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530188#endif
189
190U_BOOT_CMD(
191 zynqmp, 5, 1, do_zynqmp,
Vipul Kumar8c258e62018-07-11 15:18:28 +0530192 "ZynqMP sub-system",
Siva Durga Prasad Paladuguc436bf92018-02-28 13:26:53 +0530193 zynqmp_help_text
194)