Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 10 | #include <dm/test.h> |
| 11 | #include <sandboxtee.h> |
| 12 | #include <tee.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <test/test.h> |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 14 | #include <test/ut.h> |
| 15 | #include <tee/optee_ta_avb.h> |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 16 | #include <tee/optee_ta_rpc_test.h> |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 17 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 18 | static int open_session(struct udevice *dev, u32 *session, |
| 19 | struct tee_optee_ta_uuid *uuid) |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 20 | { |
| 21 | struct tee_open_session_arg arg; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 22 | int rc; |
| 23 | |
| 24 | memset(&arg, 0, sizeof(arg)); |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 25 | tee_optee_ta_uuid_to_octets(arg.uuid, uuid); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 26 | rc = tee_open_session(dev, &arg, 0, NULL); |
| 27 | if (rc) |
| 28 | return rc; |
| 29 | if (arg.ret) |
| 30 | return -EIO; |
| 31 | *session = arg.session; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 36 | static int invoke_func_avb(struct udevice *dev, u32 session) |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 37 | { |
| 38 | struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT }; |
| 39 | struct tee_invoke_arg arg; |
| 40 | |
| 41 | memset(&arg, 0, sizeof(arg)); |
| 42 | arg.session = session; |
| 43 | arg.func = TA_AVB_CMD_READ_LOCK_STATE; |
| 44 | |
| 45 | if (tee_invoke_func(dev, &arg, 1, ¶m) || arg.ret) |
| 46 | return -1; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 51 | static int invoke_func_rpc_test(struct udevice *dev, u32 session, |
| 52 | u64 op, u64 busnum, u64 chip_addr, |
| 53 | u64 xfer_flags, u8 *buf, size_t buf_size) |
| 54 | { |
| 55 | struct tee_param param[2]; |
| 56 | struct tee_invoke_arg arg; |
| 57 | struct tee_shm *shm_buf; |
| 58 | int rc; |
| 59 | |
| 60 | memset(&arg, 0, sizeof(arg)); |
| 61 | arg.session = session; |
| 62 | arg.func = op; |
| 63 | |
| 64 | rc = tee_shm_alloc(dev, buf_size, |
| 65 | TEE_SHM_ALLOC, &shm_buf); |
| 66 | if (rc) |
| 67 | return rc; |
| 68 | |
| 69 | if (op == TA_RPC_TEST_CMD_I2C_WRITE) |
| 70 | memcpy(shm_buf->addr, buf, buf_size); |
| 71 | |
| 72 | memset(param, 0, sizeof(param)); |
| 73 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 74 | param[0].u.value.a = busnum; |
| 75 | param[0].u.value.b = chip_addr; |
| 76 | param[0].u.value.c = xfer_flags; |
| 77 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT; |
| 78 | param[1].u.memref.shm = shm_buf; |
| 79 | param[1].u.memref.size = buf_size; |
| 80 | |
| 81 | if (tee_invoke_func(dev, &arg, 2, param) || arg.ret) { |
| 82 | rc = -1; |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | if (op == TA_RPC_TEST_CMD_I2C_READ) |
| 87 | memcpy(buf, shm_buf->addr, buf_size); |
| 88 | out: |
| 89 | tee_shm_free(shm_buf); |
| 90 | return rc; |
| 91 | } |
| 92 | |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 93 | static int match(struct tee_version_data *vers, const void *data) |
| 94 | { |
| 95 | return vers->gen_caps & TEE_GEN_CAP_GP; |
| 96 | } |
| 97 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 98 | struct test_tee_vars { |
| 99 | struct tee_shm *reg_shm; |
| 100 | struct tee_shm *alloc_shm; |
| 101 | }; |
| 102 | |
| 103 | static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars) |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 104 | { |
| 105 | struct tee_version_data vers; |
| 106 | struct udevice *dev; |
| 107 | struct sandbox_tee_state *state; |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 108 | struct tee_optee_ta_uuid avb_uuid = TA_AVB_UUID; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 109 | u32 session = 0; |
| 110 | int rc; |
| 111 | u8 data[128]; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 112 | |
| 113 | dev = tee_find_device(NULL, match, NULL, &vers); |
| 114 | ut_assert(dev); |
| 115 | state = dev_get_priv(dev); |
| 116 | ut_assert(!state->session); |
| 117 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 118 | rc = open_session(dev, &session, &avb_uuid); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 119 | ut_assert(!rc); |
| 120 | ut_assert(session == state->session); |
| 121 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 122 | rc = invoke_func_avb(dev, session); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 123 | ut_assert(!rc); |
| 124 | |
| 125 | rc = tee_close_session(dev, session); |
| 126 | ut_assert(!rc); |
| 127 | ut_assert(!state->session); |
| 128 | |
| 129 | ut_assert(!state->num_shms); |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 130 | rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 131 | ut_assert(!rc); |
| 132 | ut_assert(state->num_shms == 1); |
| 133 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 134 | rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 135 | ut_assert(!rc); |
| 136 | ut_assert(state->num_shms == 2); |
| 137 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 138 | ut_assert(tee_shm_is_registered(vars->reg_shm, dev)); |
| 139 | ut_assert(tee_shm_is_registered(vars->alloc_shm, dev)); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 140 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 141 | tee_shm_free(vars->reg_shm); |
| 142 | vars->reg_shm = NULL; |
| 143 | tee_shm_free(vars->alloc_shm); |
| 144 | vars->alloc_shm = NULL; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 145 | ut_assert(!state->num_shms); |
| 146 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 147 | return rc; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 150 | #define I2C_BUF_SIZE 64 |
| 151 | |
| 152 | static int test_tee_rpc(struct unit_test_state *uts) |
| 153 | { |
| 154 | struct tee_version_data vers; |
| 155 | struct udevice *dev; |
| 156 | struct sandbox_tee_state *state; |
| 157 | struct tee_optee_ta_uuid rpc_test_uuid = TA_RPC_TEST_UUID; |
| 158 | u32 session = 0; |
| 159 | int rc; |
| 160 | |
| 161 | char *test_str = "Test string"; |
| 162 | u8 data[I2C_BUF_SIZE] = {0}; |
| 163 | u8 data_from_eeprom[I2C_BUF_SIZE] = {0}; |
| 164 | |
| 165 | /* Use sandbox I2C EEPROM emulation; bus: 0, chip: 0x2c */ |
| 166 | u64 bus = 0; |
| 167 | u64 chip = 0x2c; |
| 168 | u64 xfer_flags = 0; |
| 169 | |
| 170 | dev = tee_find_device(NULL, match, NULL, &vers); |
| 171 | ut_assert(dev); |
| 172 | state = dev_get_priv(dev); |
| 173 | ut_assert(!state->session); |
| 174 | |
| 175 | /* Test RPC call asking for I2C service */ |
| 176 | rc = open_session(dev, &session, &rpc_test_uuid); |
| 177 | ut_assert(!rc); |
| 178 | ut_assert(session == state->session); |
| 179 | |
| 180 | /* Write buffer */ |
| 181 | strncpy((char *)data, test_str, strlen(test_str)); |
| 182 | rc = invoke_func_rpc_test(dev, session, TA_RPC_TEST_CMD_I2C_WRITE, |
| 183 | bus, chip, xfer_flags, data, sizeof(data)); |
| 184 | ut_assert(!rc); |
| 185 | |
| 186 | /* Read buffer */ |
| 187 | rc = invoke_func_rpc_test(dev, session, TA_RPC_TEST_CMD_I2C_READ, |
| 188 | bus, chip, xfer_flags, data_from_eeprom, |
| 189 | sizeof(data_from_eeprom)); |
| 190 | ut_assert(!rc); |
| 191 | |
| 192 | /* Compare */ |
| 193 | ut_assert(!memcmp(data, data_from_eeprom, sizeof(data))); |
| 194 | |
| 195 | rc = tee_close_session(dev, session); |
| 196 | ut_assert(!rc); |
| 197 | ut_assert(!state->session); |
| 198 | |
| 199 | return rc; |
| 200 | } |
| 201 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 202 | static int dm_test_tee(struct unit_test_state *uts) |
| 203 | { |
| 204 | struct test_tee_vars vars = { NULL, NULL }; |
| 205 | int rc = test_tee(uts, &vars); |
| 206 | |
Igor Opaniuk | 40154e9 | 2021-01-25 14:28:43 +0200 | [diff] [blame] | 207 | if (rc) |
| 208 | goto out; |
| 209 | |
| 210 | if (IS_ENABLED(CONFIG_OPTEE_TA_RPC_TEST)) |
| 211 | rc = test_tee_rpc(uts); |
| 212 | out: |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 213 | /* In case test_tee() asserts these may still remain allocated */ |
| 214 | tee_shm_free(vars.reg_shm); |
| 215 | tee_shm_free(vars.alloc_shm); |
| 216 | |
| 217 | return rc; |
| 218 | } |
| 219 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 220 | DM_TEST(dm_test_tee, UT_TESTF_SCAN_FDT); |