blob: ed7ba58c6475ad9a1b6961e90fd647ac66573bf8 [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 Glass313112a2019-08-01 09:46:46 -06008#include <env.h>
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +05309#include <malloc.h>
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +053010#include <asm/arch/hardware.h>
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053011#include <asm/arch/sys_proto.h>
12#include <asm/io.h>
13
Vipul Kumar95a3f872018-07-11 15:18:28 +053014static int do_zynqmp_verify_secure(cmd_tbl_t *cmdtp, int flag, int argc,
15 char * const argv[])
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053016{
Vipul Kumar95a3f872018-07-11 15:18:28 +053017 u64 src_addr, addr;
18 u32 len, src_lo, src_hi;
19 u8 *key_ptr = NULL;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053020 int ret;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053021 u32 key_lo = 0;
22 u32 key_hi = 0;
23 u32 ret_payload[PAYLOAD_ARG_CNT];
Vipul Kumar95a3f872018-07-11 15:18:28 +053024
25 if (argc < 4)
26 return CMD_RET_USAGE;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053027
Vipul Kumar95a3f872018-07-11 15:18:28 +053028 src_addr = simple_strtoull(argv[2], NULL, 16);
29 len = simple_strtoul(argv[3], NULL, 16);
30
31 if (argc == 5)
32 key_ptr = (uint8_t *)(uintptr_t)simple_strtoull(argv[4],
33 NULL, 16);
34
35 if ((ulong)src_addr != ALIGN((ulong)src_addr,
36 CONFIG_SYS_CACHELINE_SIZE)) {
37 printf("Failed: source address not aligned:%lx\n",
38 (ulong)src_addr);
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053039 return -EINVAL;
40 }
41
Vipul Kumar95a3f872018-07-11 15:18:28 +053042 src_lo = lower_32_bits((ulong)src_addr);
43 src_hi = upper_32_bits((ulong)src_addr);
44 flush_dcache_range((ulong)src_addr, (ulong)(src_addr + len));
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053045
46 if (key_ptr) {
47 key_lo = lower_32_bits((ulong)key_ptr);
48 key_hi = upper_32_bits((ulong)key_ptr);
49 flush_dcache_range((ulong)key_ptr,
50 (ulong)(key_ptr + KEY_PTR_LEN));
51 }
52
53 ret = invoke_smc(ZYNQMP_SIP_SVC_PM_SECURE_IMG_LOAD, src_lo, src_hi,
54 key_lo, key_hi, ret_payload);
55 if (ret) {
56 printf("Failed: secure op status:0x%x\n", ret);
57 } else {
58 addr = (u64)ret_payload[1] << 32 | ret_payload[2];
59 printf("Verified image at 0x%llx\n", addr);
60 env_set_hex("zynqmp_verified_img_addr", addr);
61 }
62
63 return ret;
64}
65
Vipul Kumar869ed8c2018-07-16 18:04:22 +053066static int do_zynqmp_mmio_read(cmd_tbl_t *cmdtp, int flag, int argc,
67 char * const argv[])
68{
69 u32 read_val, addr;
70 int ret;
71
72 if (argc != cmdtp->maxargs)
73 return CMD_RET_USAGE;
74
75 addr = simple_strtoul(argv[2], NULL, 16);
76
77 ret = zynqmp_mmio_read(addr, &read_val);
78 if (!ret)
79 printf("mmio read value at 0x%x = 0x%x\n",
80 addr, read_val);
81 else
82 printf("Failed: mmio read\n");
83
84 return ret;
85}
86
87static int do_zynqmp_mmio_write(cmd_tbl_t *cmdtp, int flag, int argc,
88 char * const argv[])
89{
90 u32 addr, mask, val;
91 int ret;
92
93 if (argc != cmdtp->maxargs)
94 return CMD_RET_USAGE;
95
96 addr = simple_strtoul(argv[2], NULL, 16);
97 mask = simple_strtoul(argv[3], NULL, 16);
98 val = simple_strtoul(argv[4], NULL, 16);
99
100 ret = zynqmp_mmio_write(addr, mask, val);
101 if (ret != 0)
102 printf("Failed: mmio write\n");
103
104 return ret;
105}
106
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530107#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
108static int do_zynqmp_tcm_init(cmd_tbl_t *cmdtp, int flag, int argc,
109 char * const argv[])
110{
111 u8 mode;
112
113 if (argc != cmdtp->maxargs)
114 return CMD_RET_USAGE;
115
116 mode = simple_strtoul(argv[2], NULL, 16);
117 if (mode != TCM_LOCK && mode != TCM_SPLIT) {
118 printf("Mode should be either 0(lock)/1(split)\n");
119 return CMD_RET_FAILURE;
120 }
121
122 dcache_disable();
123 tcm_init(mode);
124 dcache_enable();
125
126 return CMD_RET_SUCCESS;
127}
128#endif
129
Vipul Kumar95a3f872018-07-11 15:18:28 +0530130static cmd_tbl_t cmd_zynqmp_sub[] = {
131 U_BOOT_CMD_MKENT(secure, 5, 0, do_zynqmp_verify_secure, "", ""),
Vipul Kumar869ed8c2018-07-16 18:04:22 +0530132 U_BOOT_CMD_MKENT(mmio_read, 3, 0, do_zynqmp_mmio_read, "", ""),
133 U_BOOT_CMD_MKENT(mmio_write, 5, 0, do_zynqmp_mmio_write, "", ""),
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530134#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
135 U_BOOT_CMD_MKENT(tcminit, 3, 0, do_zynqmp_tcm_init, "", ""),
136#endif
Vipul Kumar95a3f872018-07-11 15:18:28 +0530137};
138
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530139/**
140 * do_zynqmp - Handle the "zynqmp" command-line command
141 * @cmdtp: Command data struct pointer
142 * @flag: Command flag
143 * @argc: Command-line argument count
144 * @argv: Array of command-line arguments
145 *
146 * Processes the zynqmp specific commands
147 *
148 * Return: return 0 on success and CMD_RET_USAGE incase of misuse and error
149 */
150static int do_zynqmp(cmd_tbl_t *cmdtp, int flag, int argc,
151 char *const argv[])
152{
Vipul Kumar95a3f872018-07-11 15:18:28 +0530153 cmd_tbl_t *c;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530154
Vipul Kumar95a3f872018-07-11 15:18:28 +0530155 if (argc < 2)
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530156 return CMD_RET_USAGE;
157
Vipul Kumar95a3f872018-07-11 15:18:28 +0530158 c = find_cmd_tbl(argv[1], &cmd_zynqmp_sub[0],
159 ARRAY_SIZE(cmd_zynqmp_sub));
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530160
Vipul Kumar95a3f872018-07-11 15:18:28 +0530161 if (c)
162 return c->cmd(c, flag, argc, argv);
163 else
164 return CMD_RET_USAGE;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530165}
166
167/***************************************************/
168#ifdef CONFIG_SYS_LONGHELP
169static char zynqmp_help_text[] =
170 "secure src len [key_addr] - verifies secure images of $len bytes\n"
171 " long at address $src. Optional key_addr\n"
172 " can be specified if user key needs to\n"
Vipul Kumar869ed8c2018-07-16 18:04:22 +0530173 " be used for decryption\n"
174 "zynqmp mmio_read address - read from address\n"
175 "zynqmp mmio_write address mask value - write value after masking to\n"
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530176 " address\n"
177#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
Michal Simek3a5642d2018-11-21 07:49:18 +0100178 "zynqmp tcminit mode - Initialize the TCM with zeros. TCM needs to be\n"
179 " initialized before accessing to avoid ECC\n"
180 " errors. mode specifies in which mode TCM has\n"
181 " to be initialized. Supported modes will be\n"
182 " lock(0)/split(1)\n"
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530183#endif
184 ;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530185#endif
186
187U_BOOT_CMD(
188 zynqmp, 5, 1, do_zynqmp,
Vipul Kumar95a3f872018-07-11 15:18:28 +0530189 "ZynqMP sub-system",
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530190 zynqmp_help_text
191)