blob: 22f05a4219783c8a7826b1b0750e49509c177e26 [file] [log] [blame]
Jens Wiklandera6ab4242018-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>
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
14static 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
32static 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
47static int match(struct tee_version_data *vers, const void *data)
48{
49 return vers->gen_caps & TEE_GEN_CAP_GP;
50}
51
Jens Wiklanderc091dc72018-10-29 11:41:35 +010052struct test_tee_vars {
53 struct tee_shm *reg_shm;
54 struct tee_shm *alloc_shm;
55};
56
57static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars)
Jens Wiklandera6ab4242018-09-25 16:40:19 +020058{
59 struct tee_version_data vers;
60 struct udevice *dev;
61 struct sandbox_tee_state *state;
62 u32 session = 0;
63 int rc;
64 u8 data[128];
Jens Wiklandera6ab4242018-09-25 16:40:19 +020065
66 dev = tee_find_device(NULL, match, NULL, &vers);
67 ut_assert(dev);
68 state = dev_get_priv(dev);
69 ut_assert(!state->session);
70
71 rc = open_session(dev, &session);
72 ut_assert(!rc);
73 ut_assert(session == state->session);
74
75 rc = invoke_func(dev, session);
76 ut_assert(!rc);
77
78 rc = tee_close_session(dev, session);
79 ut_assert(!rc);
80 ut_assert(!state->session);
81
82 ut_assert(!state->num_shms);
Jens Wiklanderc091dc72018-10-29 11:41:35 +010083 rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm);
Jens Wiklandera6ab4242018-09-25 16:40:19 +020084 ut_assert(!rc);
85 ut_assert(state->num_shms == 1);
86
Jens Wiklanderc091dc72018-10-29 11:41:35 +010087 rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm);
Jens Wiklandera6ab4242018-09-25 16:40:19 +020088 ut_assert(!rc);
89 ut_assert(state->num_shms == 2);
90
Jens Wiklanderc091dc72018-10-29 11:41:35 +010091 ut_assert(tee_shm_is_registered(vars->reg_shm, dev));
92 ut_assert(tee_shm_is_registered(vars->alloc_shm, dev));
Jens Wiklandera6ab4242018-09-25 16:40:19 +020093
Jens Wiklanderc091dc72018-10-29 11:41:35 +010094 tee_shm_free(vars->reg_shm);
95 vars->reg_shm = NULL;
96 tee_shm_free(vars->alloc_shm);
97 vars->alloc_shm = NULL;
Jens Wiklandera6ab4242018-09-25 16:40:19 +020098 ut_assert(!state->num_shms);
99
100 return 0;
101}
102
Jens Wiklanderc091dc72018-10-29 11:41:35 +0100103static int dm_test_tee(struct unit_test_state *uts)
104{
105 struct test_tee_vars vars = { NULL, NULL };
106 int rc = test_tee(uts, &vars);
107
108 /* In case test_tee() asserts these may still remain allocated */
109 tee_shm_free(vars.reg_shm);
110 tee_shm_free(vars.alloc_shm);
111
112 return rc;
113}
114
Jens Wiklandera6ab4242018-09-25 16:40:19 +0200115DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);