Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame^] | 1 | //SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (c) 2021 Open Mobile Platform LLC |
| 4 | */ |
| 5 | |
| 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <ta_supp_plugin.h> |
| 11 | #include <tee_internal_api.h> |
| 12 | #include <tee_internal_api_extensions.h> |
| 13 | #include <test_supp_plugin.h> |
| 14 | #include <trace.h> |
| 15 | |
| 16 | static const TEE_UUID uuid = TEST_PLUGIN_UUID; |
| 17 | |
| 18 | TEE_Result TA_CreateEntryPoint(void) |
| 19 | { |
| 20 | return TEE_SUCCESS; |
| 21 | } |
| 22 | |
| 23 | void TA_DestroyEntryPoint(void) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes __unused, |
| 28 | TEE_Param pParams[4] __unused, |
| 29 | void **ppSessionContext __unused) |
| 30 | { |
| 31 | /* check the plugin was loaded */ |
| 32 | return tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_PING, 0, NULL, 0, |
| 33 | NULL); |
| 34 | } |
| 35 | |
| 36 | void TA_CloseSessionEntryPoint(void *pSessionContext __unused) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | static TEE_Result pass_values(uint32_t param_types, |
| 41 | TEE_Param params[TEE_NUM_PARAMS]) |
| 42 | { |
| 43 | uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT, |
| 44 | TEE_PARAM_TYPE_VALUE_INPUT, |
| 45 | TEE_PARAM_TYPE_NONE, |
| 46 | TEE_PARAM_TYPE_NONE); |
| 47 | TEE_Result res = TEE_ERROR_GENERIC; |
| 48 | size_t outlen = 0; |
| 49 | struct add_op { |
| 50 | uint32_t a; |
| 51 | uint32_t b; |
| 52 | } values = { 0 }; |
| 53 | |
| 54 | if (exp_pt != param_types) |
| 55 | return TEE_ERROR_BAD_PARAMETERS; |
| 56 | |
| 57 | values.a = params[0].value.a; |
| 58 | values.b = params[0].value.b; |
| 59 | |
| 60 | res = tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_PASS_VALUES, |
| 61 | params[1].value.a, (void *)&values, |
| 62 | sizeof(struct add_op), &outlen); |
| 63 | params[0].value.a = values.a; |
| 64 | |
| 65 | return res; |
| 66 | } |
| 67 | |
| 68 | static TEE_Result write_array(uint32_t param_types, |
| 69 | TEE_Param params[TEE_NUM_PARAMS]) |
| 70 | { |
| 71 | uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT, |
| 72 | TEE_PARAM_TYPE_MEMREF_OUTPUT, |
| 73 | TEE_PARAM_TYPE_NONE, |
| 74 | TEE_PARAM_TYPE_NONE); |
| 75 | TEE_Result res = TEE_ERROR_GENERIC; |
| 76 | size_t outlen = 0; |
| 77 | char *buf = NULL; |
| 78 | |
| 79 | if (exp_pt != param_types) |
| 80 | return TEE_ERROR_BAD_PARAMETERS; |
| 81 | |
| 82 | buf = malloc(params[0].memref.size); |
| 83 | if (!buf) |
| 84 | return TEE_ERROR_OUT_OF_MEMORY; |
| 85 | |
| 86 | /* |
| 87 | * Plugins use a same buffer for input and output data. |
| 88 | * Make copy of input data to avoid erasing of it by the output. |
| 89 | * |
| 90 | * Output data contain file name the input data will be stored. |
| 91 | */ |
| 92 | memcpy(buf, params[0].memref.buffer, params[0].memref.size); |
| 93 | res = tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_WRITE_ARR, 0, buf, |
| 94 | params[0].memref.size, &outlen); |
| 95 | memcpy(params[1].memref.buffer, buf, outlen); |
| 96 | free(buf); |
| 97 | |
| 98 | return res; |
| 99 | } |
| 100 | |
| 101 | static TEE_Result get_array(uint32_t param_types, |
| 102 | TEE_Param params[TEE_NUM_PARAMS]) |
| 103 | { |
| 104 | uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_OUTPUT, |
| 105 | TEE_PARAM_TYPE_VALUE_OUTPUT, |
| 106 | TEE_PARAM_TYPE_NONE, |
| 107 | TEE_PARAM_TYPE_NONE); |
| 108 | TEE_Result res = TEE_ERROR_GENERIC; |
| 109 | size_t outlen = 0; |
| 110 | |
| 111 | if (exp_pt != param_types) |
| 112 | return TEE_ERROR_BAD_PARAMETERS; |
| 113 | |
| 114 | res = tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_GET_ARR, 0, |
| 115 | params[0].memref.buffer, |
| 116 | params[0].memref.size, |
| 117 | &outlen); |
| 118 | params[1].value.a = (uint32_t)outlen; |
| 119 | |
| 120 | return res; |
| 121 | } |
| 122 | |
| 123 | static TEE_Result bad_input_uuid(uint32_t param_types __unused, |
| 124 | TEE_Param params[TEE_NUM_PARAMS] __unused) |
| 125 | { |
| 126 | char data[32] = { }; |
| 127 | size_t inlen = sizeof(data); |
| 128 | |
| 129 | return tee_invoke_supp_plugin(NULL, 0, 0, data, inlen, NULL); |
| 130 | } |
| 131 | |
| 132 | static TEE_Result bad_input_data(uint32_t param_types __unused, |
| 133 | TEE_Param params[TEE_NUM_PARAMS] __unused) |
| 134 | { |
| 135 | return tee_invoke_supp_plugin(&uuid, 0, 0, NULL, 10, NULL); |
| 136 | } |
| 137 | |
| 138 | static TEE_Result bad_input_inlen(uint32_t param_types __unused, |
| 139 | TEE_Param params[TEE_NUM_PARAMS] __unused) |
| 140 | { |
| 141 | char data[32] = { }; |
| 142 | |
| 143 | return tee_invoke_supp_plugin(&uuid, 0, 0, data, 0, NULL); |
| 144 | } |
| 145 | |
| 146 | static TEE_Result call_unknown_plugin(uint32_t param_types __unused, |
| 147 | TEE_Param params[TEE_NUM_PARAMS] |
| 148 | __unused) |
| 149 | { |
| 150 | const TEE_UUID nulluuid = { }; |
| 151 | char data[32] = { }; |
| 152 | size_t inlen = sizeof(data); |
| 153 | |
| 154 | return tee_invoke_supp_plugin(&nulluuid, 0, 0, data, inlen, NULL); |
| 155 | } |
| 156 | |
| 157 | TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext __unused, |
| 158 | uint32_t nCommandID, |
| 159 | uint32_t nParamTypes, |
| 160 | TEE_Param pParams[4]) |
| 161 | { |
| 162 | switch (nCommandID) { |
| 163 | case TA_SUPP_PLUGIN_CMD_PASS_VALUES: |
| 164 | return pass_values(nParamTypes, pParams); |
| 165 | case TA_SUPP_PLUGIN_CMD_WRITE_ARR: |
| 166 | return write_array(nParamTypes, pParams); |
| 167 | case TA_SUPP_PLUGIN_CMD_GET_ARR: |
| 168 | return get_array(nParamTypes, pParams); |
| 169 | case TA_SUPP_PLUGIN_CMD_BAD_UUID: |
| 170 | return bad_input_uuid(nParamTypes, pParams); |
| 171 | case TA_SUPP_PLUGIN_CMD_BAD_IN_DATA: |
| 172 | return bad_input_data(nParamTypes, pParams); |
| 173 | case TA_SUPP_PLUGIN_CMD_BAD_IN_LEN: |
| 174 | return bad_input_inlen(nParamTypes, pParams); |
| 175 | case TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID: |
| 176 | return call_unknown_plugin(nParamTypes, pParams); |
| 177 | default: |
| 178 | return TEE_ERROR_NOT_SUPPORTED; |
| 179 | } |
| 180 | } |