test: log: Give tests names instead of numbers
[platform/kernel/u-boot.git] / test / dm / tee.c
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 <log.h>
9 #include <malloc.h>
10 #include <dm/test.h>
11 #include <sandboxtee.h>
12 #include <tee.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15 #include <tee/optee_ta_avb.h>
16
17 static 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
35 static 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
50 static int match(struct tee_version_data *vers, const void *data)
51 {
52         return vers->gen_caps & TEE_GEN_CAP_GP;
53 }
54
55 struct test_tee_vars {
56         struct tee_shm *reg_shm;
57         struct tee_shm *alloc_shm;
58 };
59
60 static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars)
61 {
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];
68
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);
86         rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm);
87         ut_assert(!rc);
88         ut_assert(state->num_shms == 1);
89
90         rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm);
91         ut_assert(!rc);
92         ut_assert(state->num_shms == 2);
93
94         ut_assert(tee_shm_is_registered(vars->reg_shm, dev));
95         ut_assert(tee_shm_is_registered(vars->alloc_shm, dev));
96
97         tee_shm_free(vars->reg_shm);
98         vars->reg_shm = NULL;
99         tee_shm_free(vars->alloc_shm);
100         vars->alloc_shm = NULL;
101         ut_assert(!state->num_shms);
102
103         return 0;
104 }
105
106 static 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
118 DM_TEST(dm_test_tee, UT_TESTF_SCAN_FDT);