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> |
| 13 | #include <test/ut.h> |
| 14 | #include <tee/optee_ta_avb.h> |
| 15 | |
| 16 | static int open_session(struct udevice *dev, u32 *session) |
| 17 | { |
| 18 | struct tee_open_session_arg arg; |
| 19 | const struct tee_optee_ta_uuid uuid = TA_AVB_UUID; |
| 20 | int rc; |
| 21 | |
| 22 | memset(&arg, 0, sizeof(arg)); |
| 23 | tee_optee_ta_uuid_to_octets(arg.uuid, &uuid); |
| 24 | rc = tee_open_session(dev, &arg, 0, NULL); |
| 25 | if (rc) |
| 26 | return rc; |
| 27 | if (arg.ret) |
| 28 | return -EIO; |
| 29 | *session = arg.session; |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static int invoke_func(struct udevice *dev, u32 session) |
| 35 | { |
| 36 | struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT }; |
| 37 | struct tee_invoke_arg arg; |
| 38 | |
| 39 | memset(&arg, 0, sizeof(arg)); |
| 40 | arg.session = session; |
| 41 | arg.func = TA_AVB_CMD_READ_LOCK_STATE; |
| 42 | |
| 43 | if (tee_invoke_func(dev, &arg, 1, ¶m) || arg.ret) |
| 44 | return -1; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int match(struct tee_version_data *vers, const void *data) |
| 50 | { |
| 51 | return vers->gen_caps & TEE_GEN_CAP_GP; |
| 52 | } |
| 53 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 54 | struct test_tee_vars { |
| 55 | struct tee_shm *reg_shm; |
| 56 | struct tee_shm *alloc_shm; |
| 57 | }; |
| 58 | |
| 59 | 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] | 60 | { |
| 61 | struct tee_version_data vers; |
| 62 | struct udevice *dev; |
| 63 | struct sandbox_tee_state *state; |
| 64 | u32 session = 0; |
| 65 | int rc; |
| 66 | u8 data[128]; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 67 | |
| 68 | dev = tee_find_device(NULL, match, NULL, &vers); |
| 69 | ut_assert(dev); |
| 70 | state = dev_get_priv(dev); |
| 71 | ut_assert(!state->session); |
| 72 | |
| 73 | rc = open_session(dev, &session); |
| 74 | ut_assert(!rc); |
| 75 | ut_assert(session == state->session); |
| 76 | |
| 77 | rc = invoke_func(dev, session); |
| 78 | ut_assert(!rc); |
| 79 | |
| 80 | rc = tee_close_session(dev, session); |
| 81 | ut_assert(!rc); |
| 82 | ut_assert(!state->session); |
| 83 | |
| 84 | ut_assert(!state->num_shms); |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 85 | rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 86 | ut_assert(!rc); |
| 87 | ut_assert(state->num_shms == 1); |
| 88 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 89 | rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 90 | ut_assert(!rc); |
| 91 | ut_assert(state->num_shms == 2); |
| 92 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 93 | ut_assert(tee_shm_is_registered(vars->reg_shm, dev)); |
| 94 | ut_assert(tee_shm_is_registered(vars->alloc_shm, dev)); |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 95 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 96 | tee_shm_free(vars->reg_shm); |
| 97 | vars->reg_shm = NULL; |
| 98 | tee_shm_free(vars->alloc_shm); |
| 99 | vars->alloc_shm = NULL; |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 100 | ut_assert(!state->num_shms); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Jens Wiklander | 78c4b3e | 2018-10-29 11:41:35 +0100 | [diff] [blame] | 105 | static int dm_test_tee(struct unit_test_state *uts) |
| 106 | { |
| 107 | struct test_tee_vars vars = { NULL, NULL }; |
| 108 | int rc = test_tee(uts, &vars); |
| 109 | |
| 110 | /* In case test_tee() asserts these may still remain allocated */ |
| 111 | tee_shm_free(vars.reg_shm); |
| 112 | tee_shm_free(vars.alloc_shm); |
| 113 | |
| 114 | return rc; |
| 115 | } |
| 116 | |
Jens Wiklander | a10b983 | 2018-09-25 16:40:19 +0200 | [diff] [blame] | 117 | DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT); |