blob: fec9551fb27cc38af45a1a41ddefac6447c3cce8 [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 Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Jens Wiklandera6ab4242018-09-25 16:40:19 +020010#include <dm/test.h>
11#include <sandboxtee.h>
12#include <tee.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060013#include <test/test.h>
Jens Wiklandera6ab4242018-09-25 16:40:19 +020014#include <test/ut.h>
15#include <tee/optee_ta_avb.h>
16
17static int open_session(struct udevice *dev, u32 *session)
18{
19 struct tee_open_session_arg arg;
20 const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
21 int rc;
22
23 memset(&arg, 0, sizeof(arg));
24 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
25 rc = tee_open_session(dev, &arg, 0, NULL);
26 if (rc)
27 return rc;
28 if (arg.ret)
29 return -EIO;
30 *session = arg.session;
31
32 return 0;
33}
34
35static int invoke_func(struct udevice *dev, u32 session)
36{
37 struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
38 struct tee_invoke_arg arg;
39
40 memset(&arg, 0, sizeof(arg));
41 arg.session = session;
42 arg.func = TA_AVB_CMD_READ_LOCK_STATE;
43
44 if (tee_invoke_func(dev, &arg, 1, &param) || arg.ret)
45 return -1;
46
47 return 0;
48}
49
50static int match(struct tee_version_data *vers, const void *data)
51{
52 return vers->gen_caps & TEE_GEN_CAP_GP;
53}
54
Jens Wiklanderc091dc72018-10-29 11:41:35 +010055struct test_tee_vars {
56 struct tee_shm *reg_shm;
57 struct tee_shm *alloc_shm;
58};
59
60static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars)
Jens Wiklandera6ab4242018-09-25 16:40:19 +020061{
62 struct tee_version_data vers;
63 struct udevice *dev;
64 struct sandbox_tee_state *state;
65 u32 session = 0;
66 int rc;
67 u8 data[128];
Jens Wiklandera6ab4242018-09-25 16:40:19 +020068
69 dev = tee_find_device(NULL, match, NULL, &vers);
70 ut_assert(dev);
71 state = dev_get_priv(dev);
72 ut_assert(!state->session);
73
74 rc = open_session(dev, &session);
75 ut_assert(!rc);
76 ut_assert(session == state->session);
77
78 rc = invoke_func(dev, session);
79 ut_assert(!rc);
80
81 rc = tee_close_session(dev, session);
82 ut_assert(!rc);
83 ut_assert(!state->session);
84
85 ut_assert(!state->num_shms);
Jens Wiklanderc091dc72018-10-29 11:41:35 +010086 rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm);
Jens Wiklandera6ab4242018-09-25 16:40:19 +020087 ut_assert(!rc);
88 ut_assert(state->num_shms == 1);
89
Jens Wiklanderc091dc72018-10-29 11:41:35 +010090 rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm);
Jens Wiklandera6ab4242018-09-25 16:40:19 +020091 ut_assert(!rc);
92 ut_assert(state->num_shms == 2);
93
Jens Wiklanderc091dc72018-10-29 11:41:35 +010094 ut_assert(tee_shm_is_registered(vars->reg_shm, dev));
95 ut_assert(tee_shm_is_registered(vars->alloc_shm, dev));
Jens Wiklandera6ab4242018-09-25 16:40:19 +020096
Jens Wiklanderc091dc72018-10-29 11:41:35 +010097 tee_shm_free(vars->reg_shm);
98 vars->reg_shm = NULL;
99 tee_shm_free(vars->alloc_shm);
100 vars->alloc_shm = NULL;
Jens Wiklandera6ab4242018-09-25 16:40:19 +0200101 ut_assert(!state->num_shms);
102
103 return 0;
104}
105
Jens Wiklanderc091dc72018-10-29 11:41:35 +0100106static int dm_test_tee(struct unit_test_state *uts)
107{
108 struct test_tee_vars vars = { NULL, NULL };
109 int rc = test_tee(uts, &vars);
110
111 /* In case test_tee() asserts these may still remain allocated */
112 tee_shm_free(vars.reg_shm);
113 tee_shm_free(vars.alloc_shm);
114
115 return rc;
116}
117
Jens Wiklandera6ab4242018-09-25 16:40:19 +0200118DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);