| /* |
| * Copyright (c) 2019 The Fuchsia Authors |
| * |
| * SPDX-License-Identifier: BSD-3-Clause |
| */ |
| |
| #include <tee/ta_helper.h> |
| |
| /* Log TA client API error codes. */ |
| #define LOCAL_DEBUG 1 |
| |
| #ifdef LOCAL_DEBUG |
| #define TA_HELPER_DEBUG(...) fprintf(stderr, __VA_ARGS__) |
| #else |
| #define TA_HELPER_DEBUG(...) |
| #endif |
| |
| int ta_open(const struct tee_optee_ta_uuid *uuid, |
| struct ta_context *out_context) |
| { |
| struct udevice *tee; |
| struct tee_open_session_arg arg = { 0 }; |
| int rc; |
| |
| if (!uuid || !out_context) { |
| return -EINVAL; |
| } |
| |
| tee = tee_find_device(NULL, NULL, NULL, NULL); |
| if (!tee) { |
| TA_HELPER_DEBUG("Error: No TEE device not found.\n"); |
| return -ENODEV; |
| } |
| |
| tee_optee_ta_uuid_to_octets(arg.uuid, uuid); |
| rc = tee_open_session(tee, &arg, 0, NULL); |
| if (rc || arg.ret) { |
| TA_HELPER_DEBUG("Error: Failed to open TA: %s.\n", |
| tee_explain_error(rc, arg.ret, arg.ret_origin)); |
| return -EBUSY; |
| } |
| |
| out_context->tee = tee; |
| out_context->session = arg.session; |
| |
| return 0; |
| } |
| |
| void ta_close(struct ta_context *context) |
| { |
| if (context && context->tee) { |
| tee_close_session(context->tee, context->session); |
| } |
| } |
| |
| int ta_invoke(const struct ta_context *context, uint32_t func, |
| uint32_t num_params, struct tee_param *params) |
| { |
| struct tee_invoke_arg arg = { 0 }; |
| int rc; |
| |
| if (!context || !context->tee) { |
| return -EINVAL; |
| } |
| |
| arg.func = func; |
| arg.session = context->session; |
| |
| rc = tee_invoke_func(context->tee, &arg, num_params, params); |
| if (rc || arg.ret) { |
| TA_HELPER_DEBUG( |
| "Error: failed to invoke command 0x%x: %s.\n", func, |
| tee_explain_error(rc, arg.ret, arg.ret_origin)); |
| return -EBUSY; |
| } |
| |
| return 0; |
| } |
| |
| int ta_call(const struct tee_optee_ta_uuid *uuid, uint32_t func, |
| uint32_t num_params, struct tee_param *params) |
| { |
| int rc; |
| struct ta_context context = { 0 }; |
| |
| rc = ta_open(uuid, &context); |
| if (rc) { |
| return rc; |
| } |
| |
| rc = ta_invoke(&context, func, num_params, params); |
| |
| ta_close(&context); |
| |
| return rc; |
| } |