blob: f53a1b63bea6c3a4a43e06e4ba2d3149a57fe898 [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>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010010#include <zynqmp_firmware.h>
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +053011#include <asm/arch/hardware.h>
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053012#include <asm/arch/sys_proto.h>
13#include <asm/io.h>
14
Vipul Kumar95a3f872018-07-11 15:18:28 +053015static int do_zynqmp_verify_secure(cmd_tbl_t *cmdtp, int flag, int argc,
16 char * const argv[])
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053017{
Vipul Kumar95a3f872018-07-11 15:18:28 +053018 u64 src_addr, addr;
19 u32 len, src_lo, src_hi;
20 u8 *key_ptr = NULL;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053021 int ret;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053022 u32 key_lo = 0;
23 u32 key_hi = 0;
24 u32 ret_payload[PAYLOAD_ARG_CNT];
Vipul Kumar95a3f872018-07-11 15:18:28 +053025
26 if (argc < 4)
27 return CMD_RET_USAGE;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053028
Vipul Kumar95a3f872018-07-11 15:18:28 +053029 src_addr = simple_strtoull(argv[2], NULL, 16);
30 len = simple_strtoul(argv[3], NULL, 16);
31
32 if (argc == 5)
33 key_ptr = (uint8_t *)(uintptr_t)simple_strtoull(argv[4],
34 NULL, 16);
35
36 if ((ulong)src_addr != ALIGN((ulong)src_addr,
37 CONFIG_SYS_CACHELINE_SIZE)) {
38 printf("Failed: source address not aligned:%lx\n",
39 (ulong)src_addr);
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053040 return -EINVAL;
41 }
42
Vipul Kumar95a3f872018-07-11 15:18:28 +053043 src_lo = lower_32_bits((ulong)src_addr);
44 src_hi = upper_32_bits((ulong)src_addr);
45 flush_dcache_range((ulong)src_addr, (ulong)(src_addr + len));
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +053046
47 if (key_ptr) {
48 key_lo = lower_32_bits((ulong)key_ptr);
49 key_hi = upper_32_bits((ulong)key_ptr);
50 flush_dcache_range((ulong)key_ptr,
51 (ulong)(key_ptr + KEY_PTR_LEN));
52 }
53
54 ret = invoke_smc(ZYNQMP_SIP_SVC_PM_SECURE_IMG_LOAD, src_lo, src_hi,
55 key_lo, key_hi, ret_payload);
56 if (ret) {
57 printf("Failed: secure op status:0x%x\n", ret);
58 } else {
59 addr = (u64)ret_payload[1] << 32 | ret_payload[2];
60 printf("Verified image at 0x%llx\n", addr);
61 env_set_hex("zynqmp_verified_img_addr", addr);
62 }
63
64 return ret;
65}
66
Vipul Kumar869ed8c2018-07-16 18:04:22 +053067static int do_zynqmp_mmio_read(cmd_tbl_t *cmdtp, int flag, int argc,
68 char * const argv[])
69{
70 u32 read_val, addr;
71 int ret;
72
73 if (argc != cmdtp->maxargs)
74 return CMD_RET_USAGE;
75
76 addr = simple_strtoul(argv[2], NULL, 16);
77
78 ret = zynqmp_mmio_read(addr, &read_val);
79 if (!ret)
80 printf("mmio read value at 0x%x = 0x%x\n",
81 addr, read_val);
82 else
83 printf("Failed: mmio read\n");
84
85 return ret;
86}
87
88static int do_zynqmp_mmio_write(cmd_tbl_t *cmdtp, int flag, int argc,
89 char * const argv[])
90{
91 u32 addr, mask, val;
92 int ret;
93
94 if (argc != cmdtp->maxargs)
95 return CMD_RET_USAGE;
96
97 addr = simple_strtoul(argv[2], NULL, 16);
98 mask = simple_strtoul(argv[3], NULL, 16);
99 val = simple_strtoul(argv[4], NULL, 16);
100
101 ret = zynqmp_mmio_write(addr, mask, val);
102 if (ret != 0)
103 printf("Failed: mmio write\n");
104
105 return ret;
106}
107
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530108#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
109static int do_zynqmp_tcm_init(cmd_tbl_t *cmdtp, int flag, int argc,
110 char * const argv[])
111{
112 u8 mode;
113
114 if (argc != cmdtp->maxargs)
115 return CMD_RET_USAGE;
116
117 mode = simple_strtoul(argv[2], NULL, 16);
118 if (mode != TCM_LOCK && mode != TCM_SPLIT) {
119 printf("Mode should be either 0(lock)/1(split)\n");
120 return CMD_RET_FAILURE;
121 }
122
123 dcache_disable();
124 tcm_init(mode);
125 dcache_enable();
126
127 return CMD_RET_SUCCESS;
128}
129#endif
130
Vipul Kumar95a3f872018-07-11 15:18:28 +0530131static cmd_tbl_t cmd_zynqmp_sub[] = {
132 U_BOOT_CMD_MKENT(secure, 5, 0, do_zynqmp_verify_secure, "", ""),
Vipul Kumar869ed8c2018-07-16 18:04:22 +0530133 U_BOOT_CMD_MKENT(mmio_read, 3, 0, do_zynqmp_mmio_read, "", ""),
134 U_BOOT_CMD_MKENT(mmio_write, 5, 0, do_zynqmp_mmio_write, "", ""),
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530135#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
136 U_BOOT_CMD_MKENT(tcminit, 3, 0, do_zynqmp_tcm_init, "", ""),
137#endif
Vipul Kumar95a3f872018-07-11 15:18:28 +0530138};
139
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530140/**
141 * do_zynqmp - Handle the "zynqmp" command-line command
142 * @cmdtp: Command data struct pointer
143 * @flag: Command flag
144 * @argc: Command-line argument count
145 * @argv: Array of command-line arguments
146 *
147 * Processes the zynqmp specific commands
148 *
149 * Return: return 0 on success and CMD_RET_USAGE incase of misuse and error
150 */
151static int do_zynqmp(cmd_tbl_t *cmdtp, int flag, int argc,
152 char *const argv[])
153{
Vipul Kumar95a3f872018-07-11 15:18:28 +0530154 cmd_tbl_t *c;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530155
Vipul Kumar95a3f872018-07-11 15:18:28 +0530156 if (argc < 2)
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530157 return CMD_RET_USAGE;
158
Vipul Kumar95a3f872018-07-11 15:18:28 +0530159 c = find_cmd_tbl(argv[1], &cmd_zynqmp_sub[0],
160 ARRAY_SIZE(cmd_zynqmp_sub));
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530161
Vipul Kumar95a3f872018-07-11 15:18:28 +0530162 if (c)
163 return c->cmd(c, flag, argc, argv);
164 else
165 return CMD_RET_USAGE;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530166}
167
168/***************************************************/
169#ifdef CONFIG_SYS_LONGHELP
170static char zynqmp_help_text[] =
171 "secure src len [key_addr] - verifies secure images of $len bytes\n"
172 " long at address $src. Optional key_addr\n"
173 " can be specified if user key needs to\n"
Vipul Kumar869ed8c2018-07-16 18:04:22 +0530174 " be used for decryption\n"
175 "zynqmp mmio_read address - read from address\n"
176 "zynqmp mmio_write address mask value - write value after masking to\n"
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530177 " address\n"
178#ifdef CONFIG_DEFINE_TCM_OCM_MMAP
Michal Simek3a5642d2018-11-21 07:49:18 +0100179 "zynqmp tcminit mode - Initialize the TCM with zeros. TCM needs to be\n"
180 " initialized before accessing to avoid ECC\n"
181 " errors. mode specifies in which mode TCM has\n"
182 " to be initialized. Supported modes will be\n"
183 " lock(0)/split(1)\n"
Siva Durga Prasad Paladugu48eaa0c2018-10-05 15:09:05 +0530184#endif
185 ;
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530186#endif
187
188U_BOOT_CMD(
189 zynqmp, 5, 1, do_zynqmp,
Vipul Kumar95a3f872018-07-11 15:18:28 +0530190 "ZynqMP sub-system",
Siva Durga Prasad Paladugub1acb652018-02-28 13:26:53 +0530191 zynqmp_help_text
192)