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> |
| 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, ¶m) || 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 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 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) |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 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]; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 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); |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 86 | rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 87 | ut_assert(!rc); |
| 88 | ut_assert(state->num_shms == 1); |
| 89 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 90 | rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 91 | ut_assert(!rc); |
| 92 | ut_assert(state->num_shms == 2); |
| 93 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 94 | ut_assert(tee_shm_is_registered(vars->reg_shm, dev)); |
| 95 | ut_assert(tee_shm_is_registered(vars->alloc_shm, dev)); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 96 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 97 | tee_shm_free(vars->reg_shm); |
| 98 | vars->reg_shm = NULL; |
| 99 | tee_shm_free(vars->alloc_shm); |
| 100 | vars->alloc_shm = NULL; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 101 | ut_assert(!state->num_shms); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 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 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 118 | DM_TEST(dm_test_tee, UT_TESTF_SCAN_FDT); |