xref: /openbmc/u-boot/test/dm/tee.c (revision 9949ee87)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Linaro Limited
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <dm/test.h>
9 #include <sandboxtee.h>
10 #include <tee.h>
11 #include <test/ut.h>
12 #include <tee/optee_ta_avb.h>
13 
14 static int open_session(struct udevice *dev, u32 *session)
15 {
16 	struct tee_open_session_arg arg;
17 	const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
18 	int rc;
19 
20 	memset(&arg, 0, sizeof(arg));
21 	tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
22 	rc = tee_open_session(dev, &arg, 0, NULL);
23 	if (rc)
24 		return rc;
25 	if (arg.ret)
26 		return -EIO;
27 	*session = arg.session;
28 
29 	return 0;
30 }
31 
32 static int invoke_func(struct udevice *dev, u32 session)
33 {
34 	struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
35 	struct tee_invoke_arg arg;
36 
37 	memset(&arg, 0, sizeof(arg));
38 	arg.session = session;
39 	arg.func = TA_AVB_CMD_READ_LOCK_STATE;
40 
41 	if (tee_invoke_func(dev, &arg, 1, &param) || arg.ret)
42 		return -1;
43 
44 	return 0;
45 }
46 
47 static int match(struct tee_version_data *vers, const void *data)
48 {
49 	return vers->gen_caps & TEE_GEN_CAP_GP;
50 }
51 
52 static int dm_test_tee(struct unit_test_state *uts)
53 {
54 	struct tee_version_data vers;
55 	struct udevice *dev;
56 	struct sandbox_tee_state *state;
57 	u32 session = 0;
58 	int rc;
59 	u8 data[128];
60 	struct tee_shm *reg_shm;
61 	struct tee_shm *alloc_shm;
62 
63 	dev = tee_find_device(NULL, match, NULL, &vers);
64 	ut_assert(dev);
65 	state = dev_get_priv(dev);
66 	ut_assert(!state->session);
67 
68 	rc = open_session(dev, &session);
69 	ut_assert(!rc);
70 	ut_assert(session == state->session);
71 
72 	rc = invoke_func(dev, session);
73 	ut_assert(!rc);
74 
75 	rc = tee_close_session(dev, session);
76 	ut_assert(!rc);
77 	ut_assert(!state->session);
78 
79 	ut_assert(!state->num_shms);
80 	rc = tee_shm_register(dev, data, sizeof(data), 0, &reg_shm);
81 	ut_assert(!rc);
82 	ut_assert(state->num_shms == 1);
83 
84 	rc = tee_shm_alloc(dev, 256, 0, &alloc_shm);
85 	ut_assert(!rc);
86 	ut_assert(state->num_shms == 2);
87 
88 	ut_assert(tee_shm_is_registered(reg_shm, dev));
89 	ut_assert(tee_shm_is_registered(alloc_shm, dev));
90 
91 	tee_shm_free(reg_shm);
92 	tee_shm_free(alloc_shm);
93 	ut_assert(!state->num_shms);
94 
95 	return 0;
96 }
97 
98 DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);
99