1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2024, Advanced Micro Devices, Inc.
4  */
5 #include <command.h>
6 #include <errno.h>
7 #include <tee.h>
8 #include <vsprintf.h>
9 #include <linux/string.h>
10 
11 #define TA_HELLO_WORLD_CMD_INC_VALUE 0
12 /* This needs to match the UUID of the Hello World TA. */
13 #define TA_HELLO_WORLD_UUID \
14 	{ 0x8aaaf200, 0x2450, 0x11e4, \
15 	{ 0xab, 0xe2, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} }
16 
hello_world_ta(unsigned int value)17 static int hello_world_ta(unsigned int value)
18 {
19 	const struct tee_optee_ta_uuid uuid = TA_HELLO_WORLD_UUID;
20 	struct tee_open_session_arg session_arg;
21 	struct udevice *tee = NULL;
22 	struct tee_invoke_arg arg;
23 	struct tee_param param[2];
24 	int rc;
25 
26 	tee = tee_find_device(tee, NULL, NULL, NULL);
27 	if (!tee)
28 		return -ENODEV;
29 
30 	memset(&session_arg, 0, sizeof(session_arg));
31 	tee_optee_ta_uuid_to_octets(session_arg.uuid, &uuid);
32 	rc = tee_open_session(tee, &session_arg, 0, NULL);
33 	if (rc) {
34 		printf("tee_open_session(): failed(%d)\n", rc);
35 		return rc;
36 	}
37 
38 	arg.func = TA_HELLO_WORLD_CMD_INC_VALUE;
39 	arg.session = session_arg.session;
40 
41 	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
42 	param[0].u.value.a = value;
43 
44 	printf("Value before: 0x%x\n", (int)param[0].u.value.a);
45 	printf("Calling TA\n");
46 	tee_invoke_func(tee, &arg, 1, param);
47 
48 	printf("Value after: 0x%x\n", (int)param[0].u.value.a);
49 	return tee_close_session(tee, session_arg.session);
50 }
51 
do_optee_hello_world_ta(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])52 static int do_optee_hello_world_ta(struct cmd_tbl *cmdtp, int flag, int argc,
53 				   char * const argv[])
54 {
55 	int ret, value = 0;
56 
57 	if (argc > 1)
58 		value = hextoul(argv[1], NULL);
59 
60 	ret = hello_world_ta(value);
61 	if (ret)
62 		return CMD_RET_FAILURE;
63 
64 	return CMD_RET_SUCCESS;
65 }
66 
67 U_BOOT_LONGHELP(optee,
68 	"hello [<value>]   Invoke the OP-TEE 'Hello World' TA\n");
69 
70 U_BOOT_CMD_WITH_SUBCMDS(optee, "OP-TEE commands", optee_help_text,
71 	U_BOOT_SUBCMD_MKENT(hello, 2, 1, do_optee_hello_world_ta));
72