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