blob: 582bc3e5aa072246ec23c70e979a1afad173e9e3 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001From 58358f79d9f8abbdc8bcfc7d08bd0c7c4c90ec84 Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
3Date: Tue, 16 Nov 2021 12:36:27 +0000
Patrick Williams92b42cb2022-09-03 06:53:57 -05004Subject: [PATCH 07/24] arm_ffa: introducing armffa command
Brad Bishopbec4ebc2022-08-03 09:55:16 -04005
6A new armffa command is provided as an example of how to use
7the FF-A helper functions to communicate with secure world.
8
9The armffa command allows to query secure partitions data from
10the secure world and exchanging messages with the partitions.
11
12Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
13Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
14---
15 MAINTAINERS | 1 +
16 cmd/Kconfig | 10 ++
17 cmd/Makefile | 2 +
18 cmd/armffa.c | 266 +++++++++++++++++++++++++++++++++++++++++++++++++++
19 4 files changed, 279 insertions(+)
20 create mode 100644 cmd/armffa.c
21
22diff --git a/MAINTAINERS b/MAINTAINERS
Patrick Williams92b42cb2022-09-03 06:53:57 -050023index d29d7e040764..32fc267fcf13 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040024--- a/MAINTAINERS
25+++ b/MAINTAINERS
Patrick Williams92b42cb2022-09-03 06:53:57 -050026@@ -247,6 +247,7 @@ F: include/configs/turris_*.h
Brad Bishopbec4ebc2022-08-03 09:55:16 -040027 ARM FF-A
28 M: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
29 S: Maintained
30+F: cmd/armffa.c
31 F: drivers/arm-ffa/
32 F: include/arm_ffa.h
33 F: include/arm_ffa_helper.h
34diff --git a/cmd/Kconfig b/cmd/Kconfig
Patrick Williams92b42cb2022-09-03 06:53:57 -050035index ba2f321ae989..090e668125d5 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040036--- a/cmd/Kconfig
37+++ b/cmd/Kconfig
Patrick Williams92b42cb2022-09-03 06:53:57 -050038@@ -873,6 +873,16 @@ endmenu
Brad Bishopbec4ebc2022-08-03 09:55:16 -040039
40 menu "Device access commands"
41
42+config CMD_ARMFFA
43+ bool "Arm FF-A test command"
44+ depends on ARM_FFA_TRANSPORT
45+ help
46+ Provides a test command for the Arm FF-A driver
47+ supported options:
48+ - Listing the partition(s) info
49+ - Sending a data pattern to the specified partition
50+ - Displaying the arm_ffa device info
51+
52 config CMD_ARMFLASH
53 #depends on FLASH_CFI_DRIVER
54 bool "armflash"
55diff --git a/cmd/Makefile b/cmd/Makefile
Patrick Williams92b42cb2022-09-03 06:53:57 -050056index 5e43a1e022e8..e40f52f1e416 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040057--- a/cmd/Makefile
58+++ b/cmd/Makefile
59@@ -12,6 +12,8 @@ obj-y += panic.o
60 obj-y += version.o
61
62 # command
63+
64+obj-$(CONFIG_CMD_ARMFFA) += armffa.o
65 obj-$(CONFIG_CMD_ACPI) += acpi.o
66 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
67 obj-$(CONFIG_CMD_AES) += aes.o
68diff --git a/cmd/armffa.c b/cmd/armffa.c
69new file mode 100644
70index 000000000000..71a6ebb656d1
71--- /dev/null
72+++ b/cmd/armffa.c
73@@ -0,0 +1,266 @@
74+// SPDX-License-Identifier: GPL-2.0+
75+/*
76+ * (C) Copyright 2021 ARM Limited
77+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
78+ */
79+
80+#include <arm_ffa_helper.h>
81+#include <asm/io.h>
82+#include <common.h>
83+#include <command.h>
84+#include <dm.h>
85+#include <mapmem.h>
86+#include <stdlib.h>
87+
88+/**
89+ * do_ffa_get_singular_partition_info - implementation of the getpart subcommand
90+ * @cmdtp: Command Table
91+ * @flag: flags
92+ * @argc: number of arguments
93+ * @argv: arguments
94+ *
95+ * This function queries the secure partition information which the UUID is provided
96+ * as an argument. The function uses the arm_ffa driver helper function
97+ * to retrieve the data.
98+ * The input UUID string is expected to be in big endian format.
99+ *
100+ * Return:
101+ *
102+ * CMD_RET_SUCCESS: on success, otherwise failure
103+ */
104+static int do_ffa_get_singular_partition_info(struct cmd_tbl *cmdtp, int flag, int argc,
105+ char *const argv[])
106+{
107+ struct ffa_interface_data func_data = {0};
108+ u32 count = 0;
109+ int ret;
110+ union ffa_partition_uuid service_uuid = {0};
111+ struct ffa_partition_info *parts_info;
112+ u32 info_idx;
113+
114+ if (argc != 1)
115+ return -EINVAL;
116+
117+ if (ffa_uuid_str_to_bin(argv[0], (unsigned char *)&service_uuid)) {
118+ ffa_err("Invalid UUID");
119+ return -EINVAL;
120+ }
121+
122+ /*
123+ * get from the driver the count of the SPs matching the UUID
124+ */
125+ func_data.data0_size = sizeof(service_uuid);
126+ func_data.data0 = &service_uuid;
127+ func_data.data1_size = sizeof(count);
128+ func_data.data1 = &count;
129+
130+ ret = ffa_helper_get_partitions_info(&func_data);
131+ if (ret != FFA_ERR_STAT_SUCCESS) {
132+ ffa_err("Failure in querying partitions count (error code: %d)", ret);
133+ return ret;
134+ }
135+
136+ if (!count) {
137+ ffa_info("No secure partition found");
138+ return ret;
139+ }
140+
141+ /*
142+ * pre-allocate a buffer to be filled by the driver
143+ * with ffa_partition_info structs
144+ */
145+
146+ parts_info = calloc(count, sizeof(struct ffa_partition_info));
147+ if (!parts_info)
148+ return -EINVAL;
149+
150+ ffa_info("Pre-allocating %d partition(s) info structures", count);
151+
152+ func_data.data1_size = count * sizeof(struct ffa_partition_info);
153+ func_data.data1 = parts_info;
154+
155+ /*
156+ * ask the driver to fill the buffer with the SPs info
157+ */
158+ ret = ffa_helper_get_partitions_info(&func_data);
159+ if (ret != FFA_ERR_STAT_SUCCESS) {
160+ ffa_err("Failure in querying partition(s) info (error code: %d)", ret);
161+ free(parts_info);
162+ return ret;
163+ }
164+
165+ /*
166+ * SPs found , show the partition information
167+ */
168+ for (info_idx = 0; info_idx < count ; info_idx++) {
169+ ffa_info("Partition: id = 0x%x , exec_ctxt 0x%x , properties 0x%x",
170+ parts_info[info_idx].id,
171+ parts_info[info_idx].exec_ctxt,
172+ parts_info[info_idx].properties);
173+ }
174+
175+ free(parts_info);
176+
177+ return 0;
178+}
179+
180+/**
181+ * do_ffa_msg_send_direct_req - implementation of the ping subcommand
182+ * @cmdtp: Command Table
183+ * @flag: flags
184+ * @argc: number of arguments
185+ * @argv: arguments
186+ *
187+ * This function sends data to the secure partition which the ID is provided
188+ * as an argument. The function uses the arm_ffa driver helper function
189+ * to send data.
190+ *
191+ * Return:
192+ *
193+ * CMD_RET_SUCCESS: on success, otherwise failure
194+ */
195+int do_ffa_msg_send_direct_req(struct cmd_tbl *cmdtp, int flag, int argc,
196+ char *const argv[])
197+{
198+ struct ffa_interface_data func_data = {0};
199+ struct ffa_send_direct_data msg = {0};
200+ u32 pattern = 0xaabbccd0;
201+ u16 part_id;
202+ int ret;
203+
204+ if (argc != 1)
205+ return -EINVAL;
206+
207+ errno = 0;
208+ part_id = strtoul(argv[0], NULL, 16);
209+
210+ if (errno) {
211+ ffa_err("Invalid partition ID");
212+ return -EINVAL;
213+ }
214+
215+ /*
216+ * telling the driver which partition to use
217+ */
218+ func_data.data0_size = sizeof(part_id);
219+ func_data.data0 = &part_id;
220+
221+ /*
222+ * filling the message data
223+ */
224+ msg.a3 = ++pattern;
225+ msg.a4 = ++pattern;
226+ msg.a5 = ++pattern;
227+ msg.a6 = ++pattern;
228+ msg.a7 = ++pattern;
229+ func_data.data1_size = sizeof(msg);
230+ func_data.data1 = &msg;
231+
232+ ret = ffa_helper_msg_send_direct_req(&func_data);
233+ if (ret == FFA_ERR_STAT_SUCCESS) {
234+ u8 cnt;
235+
236+ ffa_info("SP response:\n[LSB]");
237+ for (cnt = 0;
238+ cnt < sizeof(struct ffa_send_direct_data) / sizeof(u32);
239+ cnt++)
240+ ffa_info("0x%x", ((u32 *)&msg)[cnt]);
241+ } else {
242+ ffa_err("Sending direct request error (%d)", ret);
243+ }
244+
245+ return ret;
246+}
247+
248+/**
249+ *do_ffa_dev_list - implementation of the devlist subcommand
250+ * @cmdtp: [in] Command Table
251+ * @flag: flags
252+ * @argc: number of arguments
253+ * @argv: arguments
254+ *
255+ * This function queries the devices belonging to the UCLASS_FFA
256+ * class. Currently, one device is expected to show up: the arm_ffa device
257+ *
258+ * Return:
259+ *
260+ * CMD_RET_SUCCESS: on success, otherwise failure
261+ */
262+int do_ffa_dev_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
263+{
264+ struct udevice *dev = NULL;
265+ int i, ret;
266+
267+ ffa_info("arm_ffa uclass entries:");
268+
269+ for (i = 0, ret = uclass_first_device(UCLASS_FFA, &dev);
270+ dev;
271+ ret = uclass_next_device(&dev), i++) {
272+ if (ret)
273+ break;
274+
275+ ffa_info("entry %d - instance %08x, ops %08x, plat %08x",
276+ i,
277+ (u32)map_to_sysmem(dev),
278+ (u32)map_to_sysmem(dev->driver->ops),
279+ (u32)map_to_sysmem(dev_get_plat(dev)));
280+ }
281+
282+ return cmd_process_error(cmdtp, ret);
283+}
284+
285+static struct cmd_tbl armffa_commands[] = {
286+ U_BOOT_CMD_MKENT(getpart, 1, 1, do_ffa_get_singular_partition_info, "", ""),
287+ U_BOOT_CMD_MKENT(ping, 1, 1, do_ffa_msg_send_direct_req, "", ""),
288+ U_BOOT_CMD_MKENT(devlist, 0, 1, do_ffa_dev_list, "", ""),
289+};
290+
291+/**
292+ * do_armffa - the armffa command main function
293+ * @cmdtp: Command Table
294+ * @flag: flags
295+ * @argc: number of arguments
296+ * @argv: arguments
297+ *
298+ * This function identifies which armffa subcommand to run.
299+ * Then, it makes sure the arm_ffa device is probed and
300+ * ready for use.
301+ * Then, it runs the subcommand.
302+ *
303+ * Return:
304+ *
305+ * CMD_RET_SUCCESS: on success, otherwise failure
306+ */
307+static int do_armffa(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
308+{
309+ struct cmd_tbl *armffa_cmd;
310+ int ret;
311+
312+ if (argc < 2)
313+ return CMD_RET_USAGE;
314+
315+ armffa_cmd = find_cmd_tbl(argv[1], armffa_commands, ARRAY_SIZE(armffa_commands));
316+
317+ argc -= 2;
318+ argv += 2;
319+
320+ if (!armffa_cmd || argc > armffa_cmd->maxargs)
321+ return CMD_RET_USAGE;
322+
323+ ret = ffa_helper_init_device();
324+ if (ret != FFA_ERR_STAT_SUCCESS)
325+ return cmd_process_error(cmdtp, ret);
326+
327+ ret = armffa_cmd->cmd(armffa_cmd, flag, argc, argv);
328+
329+ return cmd_process_error(armffa_cmd, ret);
330+}
331+
332+U_BOOT_CMD(armffa, 4, 1, do_armffa,
333+ "Arm FF-A operations test command",
334+ "getpart <partition UUID>\n"
335+ " - lists the partition(s) info\n"
336+ "ping <partition ID>\n"
337+ " - sends a data pattern to the specified partition\n"
338+ "devlist\n"
339+ " - displays the arm_ffa device info\n");
340--
Patrick Williams92b42cb2022-09-03 06:53:57 -05003412.37.1
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400342