blob: d40f13d291559f9d4781b53cd42ceae3095d64e0 [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>
Simon Glass336d4612020-02-03 07:36:16 -07008#include <malloc.h>
Jens Wiklandera6ab4242018-09-25 16:40:19 +02009#include <dm/test.h>
10#include <sandboxtee.h>
11#include <tee.h>
12#include <test/ut.h>
13#include <tee/optee_ta_avb.h>
14
15static int open_session(struct udevice *dev, u32 *session)
16{
17 struct tee_open_session_arg arg;
18 const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
19 int rc;
20
21 memset(&arg, 0, sizeof(arg));
22 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
23 rc = tee_open_session(dev, &arg, 0, NULL);
24 if (rc)
25 return rc;
26 if (arg.ret)
27 return -EIO;
28 *session = arg.session;
29
30 return 0;
31}
32
33static int invoke_func(struct udevice *dev, u32 session)
34{
35 struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
36 struct tee_invoke_arg arg;
37
38 memset(&arg, 0, sizeof(arg));
39 arg.session = session;
40 arg.func = TA_AVB_CMD_READ_LOCK_STATE;
41
42 if (tee_invoke_func(dev, &arg, 1, &param) || arg.ret)
43 return -1;
44
45 return 0;
46}
47
48static int match(struct tee_version_data *vers, const void *data)
49{
50 return vers->gen_caps & TEE_GEN_CAP_GP;
51}
52
Jens Wiklanderc091dc72018-10-29 11:41:35 +010053struct test_tee_vars {
54 struct tee_shm *reg_shm;
55 struct tee_shm *alloc_shm;
56};
57
58static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars)
Jens Wiklandera6ab4242018-09-25 16:40:19 +020059{
60 struct tee_version_data vers;
61 struct udevice *dev;
62 struct sandbox_tee_state *state;
63 u32 session = 0;
64 int rc;
65 u8 data[128];
Jens Wiklandera6ab4242018-09-25 16:40:19 +020066
67 dev = tee_find_device(NULL, match, NULL, &vers);
68 ut_assert(dev);
69 state = dev_get_priv(dev);
70 ut_assert(!state->session);
71
72 rc = open_session(dev, &session);
73 ut_assert(!rc);
74 ut_assert(session == state->session);
75
76 rc = invoke_func(dev, session);
77 ut_assert(!rc);
78
79 rc = tee_close_session(dev, session);
80 ut_assert(!rc);
81 ut_assert(!state->session);
82
83 ut_assert(!state->num_shms);
Jens Wiklanderc091dc72018-10-29 11:41:35 +010084 rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm);
Jens Wiklandera6ab4242018-09-25 16:40:19 +020085 ut_assert(!rc);
86 ut_assert(state->num_shms == 1);
87
Jens Wiklanderc091dc72018-10-29 11:41:35 +010088 rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm);
Jens Wiklandera6ab4242018-09-25 16:40:19 +020089 ut_assert(!rc);
90 ut_assert(state->num_shms == 2);
91
Jens Wiklanderc091dc72018-10-29 11:41:35 +010092 ut_assert(tee_shm_is_registered(vars->reg_shm, dev));
93 ut_assert(tee_shm_is_registered(vars->alloc_shm, dev));
Jens Wiklandera6ab4242018-09-25 16:40:19 +020094
Jens Wiklanderc091dc72018-10-29 11:41:35 +010095 tee_shm_free(vars->reg_shm);
96 vars->reg_shm = NULL;
97 tee_shm_free(vars->alloc_shm);
98 vars->alloc_shm = NULL;
Jens Wiklandera6ab4242018-09-25 16:40:19 +020099 ut_assert(!state->num_shms);
100
101 return 0;
102}
103
Jens Wiklanderc091dc72018-10-29 11:41:35 +0100104static int dm_test_tee(struct unit_test_state *uts)
105{
106 struct test_tee_vars vars = { NULL, NULL };
107 int rc = test_tee(uts, &vars);
108
109 /* In case test_tee() asserts these may still remain allocated */
110 tee_shm_free(vars.reg_shm);
111 tee_shm_free(vars.alloc_shm);
112
113 return rc;
114}
115
Jens Wiklandera6ab4242018-09-25 16:40:19 +0200116DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);