blob: 632e9960b0b08d190be3d59ac54bfae4026b16c5 [file] [log] [blame]
Jens Wiklandera10b9832018-09-25 16:40:19 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Linaro Limited
4 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Jens Wiklandera10b9832018-09-25 16:40:19 +020010#include <dm/test.h>
11#include <sandboxtee.h>
12#include <tee.h>
13#include <test/ut.h>
14#include <tee/optee_ta_avb.h>
15
16static int open_session(struct udevice *dev, u32 *session)
17{
18 struct tee_open_session_arg arg;
19 const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
20 int rc;
21
22 memset(&arg, 0, sizeof(arg));
23 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
24 rc = tee_open_session(dev, &arg, 0, NULL);
25 if (rc)
26 return rc;
27 if (arg.ret)
28 return -EIO;
29 *session = arg.session;
30
31 return 0;
32}
33
34static int invoke_func(struct udevice *dev, u32 session)
35{
36 struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
37 struct tee_invoke_arg arg;
38
39 memset(&arg, 0, sizeof(arg));
40 arg.session = session;
41 arg.func = TA_AVB_CMD_READ_LOCK_STATE;
42
43 if (tee_invoke_func(dev, &arg, 1, &param) || arg.ret)
44 return -1;
45
46 return 0;
47}
48
49static int match(struct tee_version_data *vers, const void *data)
50{
51 return vers->gen_caps & TEE_GEN_CAP_GP;
52}
53
Jens Wiklander78c4b3e2018-10-29 11:41:35 +010054struct test_tee_vars {
55 struct tee_shm *reg_shm;
56 struct tee_shm *alloc_shm;
57};
58
59static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars)
Jens Wiklandera10b9832018-09-25 16:40:19 +020060{
61 struct tee_version_data vers;
62 struct udevice *dev;
63 struct sandbox_tee_state *state;
64 u32 session = 0;
65 int rc;
66 u8 data[128];
Jens Wiklandera10b9832018-09-25 16:40:19 +020067
68 dev = tee_find_device(NULL, match, NULL, &vers);
69 ut_assert(dev);
70 state = dev_get_priv(dev);
71 ut_assert(!state->session);
72
73 rc = open_session(dev, &session);
74 ut_assert(!rc);
75 ut_assert(session == state->session);
76
77 rc = invoke_func(dev, session);
78 ut_assert(!rc);
79
80 rc = tee_close_session(dev, session);
81 ut_assert(!rc);
82 ut_assert(!state->session);
83
84 ut_assert(!state->num_shms);
Jens Wiklander78c4b3e2018-10-29 11:41:35 +010085 rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm);
Jens Wiklandera10b9832018-09-25 16:40:19 +020086 ut_assert(!rc);
87 ut_assert(state->num_shms == 1);
88
Jens Wiklander78c4b3e2018-10-29 11:41:35 +010089 rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm);
Jens Wiklandera10b9832018-09-25 16:40:19 +020090 ut_assert(!rc);
91 ut_assert(state->num_shms == 2);
92
Jens Wiklander78c4b3e2018-10-29 11:41:35 +010093 ut_assert(tee_shm_is_registered(vars->reg_shm, dev));
94 ut_assert(tee_shm_is_registered(vars->alloc_shm, dev));
Jens Wiklandera10b9832018-09-25 16:40:19 +020095
Jens Wiklander78c4b3e2018-10-29 11:41:35 +010096 tee_shm_free(vars->reg_shm);
97 vars->reg_shm = NULL;
98 tee_shm_free(vars->alloc_shm);
99 vars->alloc_shm = NULL;
Jens Wiklandera10b9832018-09-25 16:40:19 +0200100 ut_assert(!state->num_shms);
101
102 return 0;
103}
104
Jens Wiklander78c4b3e2018-10-29 11:41:35 +0100105static int dm_test_tee(struct unit_test_state *uts)
106{
107 struct test_tee_vars vars = { NULL, NULL };
108 int rc = test_tee(uts, &vars);
109
110 /* In case test_tee() asserts these may still remain allocated */
111 tee_shm_free(vars.reg_shm);
112 tee_shm_free(vars.alloc_shm);
113
114 return rc;
115}
116
Jens Wiklandera10b9832018-09-25 16:40:19 +0200117DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);