|
| 1 | +/**************************************************************************** |
| 2 | + * apps/examples/optee_gp/optee_gp_main.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | +#include <nuttx/tee.h> |
| 29 | +#include <tee_client_api.h> |
| 30 | +#include <teec_trace.h> |
| 31 | +#include <uuid.h> |
| 32 | + |
| 33 | +/**************************************************************************** |
| 34 | + * Pre-processor definitions |
| 35 | + ****************************************************************************/ |
| 36 | + |
| 37 | +/* This UUID is taken from the OP-TEE OS built-in pseudo TA: |
| 38 | + * https://github.com/OP-TEE/optee_os/blob/4.6.0/ |
| 39 | + * lib/libutee/include/pta_device.h |
| 40 | + */ |
| 41 | +#define PTA_DEVICE_ENUM_UUID \ |
| 42 | + { \ |
| 43 | + 0x7011a688, 0xddde, 0x4053, \ |
| 44 | + { \ |
| 45 | + 0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8 \ |
| 46 | + } \ |
| 47 | + } |
| 48 | + |
| 49 | +#define PTA_CMD_GET_DEVICES 0x0 |
| 50 | + |
| 51 | +/**************************************************************************** |
| 52 | + * Public Functions |
| 53 | + ****************************************************************************/ |
| 54 | + |
| 55 | +/**************************************************************************** |
| 56 | + * optee_gp_main |
| 57 | + ****************************************************************************/ |
| 58 | + |
| 59 | +int main(int argc, FAR char *argv[]) |
| 60 | +{ |
| 61 | + TEEC_Result res; |
| 62 | + TEEC_Context ctx; |
| 63 | + TEEC_Session sess; |
| 64 | + TEEC_Operation op; |
| 65 | + TEEC_UUID uuid = PTA_DEVICE_ENUM_UUID; |
| 66 | + void *buf; |
| 67 | + TEEC_SharedMemory io_shm; |
| 68 | + uint32_t err_origin; |
| 69 | + unsigned int count; |
| 70 | + const uuid_t *raw_ta_uuid; |
| 71 | + uuid_t ta_uuid; |
| 72 | + char *ta_uuid_s; |
| 73 | + |
| 74 | + res = TEEC_InitializeContext(NULL, &ctx); |
| 75 | + if (res != TEEC_SUCCESS) |
| 76 | + { |
| 77 | + EMSG("TEEC_InitializeContext failed with code 0x%08x\n", res); |
| 78 | + goto exit; |
| 79 | + } |
| 80 | + |
| 81 | + memset(&op, 0, sizeof(op)); |
| 82 | + |
| 83 | + /* Open a session with the devices pseudo TA */ |
| 84 | + |
| 85 | + res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 86 | + &op, &err_origin); |
| 87 | + if (res != TEEC_SUCCESS) |
| 88 | + { |
| 89 | + EMSG("TEEC_Opensession failed with code 0x%08x origin 0x%08x", res, |
| 90 | + err_origin); |
| 91 | + goto exit_with_ctx; |
| 92 | + } |
| 93 | + |
| 94 | + /* Invoke command with NULL buffer to get required size */ |
| 95 | + |
| 96 | + op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, |
| 97 | + TEEC_NONE, TEEC_NONE); |
| 98 | + op.params[0].tmpref.buffer = NULL; |
| 99 | + op.params[0].tmpref.size = 0; |
| 100 | + |
| 101 | + res = TEEC_InvokeCommand(&sess, PTA_CMD_GET_DEVICES, &op, &err_origin); |
| 102 | + if (err_origin != TEEC_ORIGIN_TRUSTED_APP || |
| 103 | + res != TEEC_ERROR_SHORT_BUFFER) |
| 104 | + { |
| 105 | + EMSG("TEEC_InvokeCommand failed: code 0x%08x origin 0x%08x", |
| 106 | + res, err_origin); |
| 107 | + goto exit_with_session; |
| 108 | + } |
| 109 | + |
| 110 | + /* Invoke command using temporary memory */ |
| 111 | + |
| 112 | + op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, |
| 113 | + TEEC_NONE, TEEC_NONE); |
| 114 | + |
| 115 | + op.params[0].tmpref.buffer = buf = malloc(op.params[0].tmpref.size); |
| 116 | + if (!op.params[0].tmpref.buffer) |
| 117 | + { |
| 118 | + EMSG("Failed to allocate %zu bytes of memory to share with TEE", |
| 119 | + op.params[0].tmpref.size); |
| 120 | + goto exit_with_session; |
| 121 | + } |
| 122 | + |
| 123 | + res = TEEC_InvokeCommand(&sess, PTA_CMD_GET_DEVICES, &op, &err_origin); |
| 124 | + if (res != TEEC_SUCCESS) |
| 125 | + { |
| 126 | + EMSG("TEEC_InvokeCommand failed: code 0x%08x origin 0x%08x", |
| 127 | + res, err_origin); |
| 128 | + goto exit_with_buf; |
| 129 | + } |
| 130 | + |
| 131 | + /* Invoke command using pre-allocated, pre-registered memory */ |
| 132 | + |
| 133 | + io_shm.size = op.params[0].tmpref.size; |
| 134 | + io_shm.flags = TEEC_MEM_OUTPUT; |
| 135 | + res = TEEC_AllocateSharedMemory(&ctx, &io_shm); |
| 136 | + if (res != TEEC_SUCCESS) |
| 137 | + { |
| 138 | + EMSG("TEEC_AllocateSharedMemory failed: code 0x%08x", res); |
| 139 | + goto exit_with_buf; |
| 140 | + } |
| 141 | + |
| 142 | + op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, TEEC_NONE, TEEC_NONE, |
| 143 | + TEEC_NONE); |
| 144 | + op.params[0].memref.parent = &io_shm; |
| 145 | + |
| 146 | + res = TEEC_InvokeCommand(&sess, PTA_CMD_GET_DEVICES, &op, &err_origin); |
| 147 | + if (res != TEEC_SUCCESS) |
| 148 | + { |
| 149 | + EMSG("TEEC_InvokeCommand failed: code 0x%08x origin 0x%08x", |
| 150 | + res, err_origin); |
| 151 | + goto exit_with_shm; |
| 152 | + } |
| 153 | + |
| 154 | + /* Sanity check that both outputs are the same */ |
| 155 | + |
| 156 | + if (memcmp(buf, io_shm.buffer, io_shm.size)) |
| 157 | + { |
| 158 | + EMSG("Different results with temp vs registered memory"); |
| 159 | + goto exit_with_shm; |
| 160 | + } |
| 161 | + |
| 162 | + /* Print results to stdout */ |
| 163 | + |
| 164 | + IMSG("Available devices:"); |
| 165 | + |
| 166 | + count = io_shm.size / sizeof(uuid_t); |
| 167 | + raw_ta_uuid = (uuid_t *)io_shm.buffer; |
| 168 | + |
| 169 | + while (count--) |
| 170 | + { |
| 171 | + uuid_dec_be(raw_ta_uuid, &ta_uuid); |
| 172 | + uuid_to_string(&ta_uuid, &ta_uuid_s, NULL); |
| 173 | + |
| 174 | + IMSG(" %s", ta_uuid_s); |
| 175 | + |
| 176 | + free(ta_uuid_s); |
| 177 | + raw_ta_uuid++; |
| 178 | + } |
| 179 | + |
| 180 | +exit_with_shm: |
| 181 | + TEEC_ReleaseSharedMemory(&io_shm); |
| 182 | +exit_with_buf: |
| 183 | + free(buf); |
| 184 | +exit_with_session: |
| 185 | + TEEC_CloseSession(&sess); |
| 186 | +exit_with_ctx: |
| 187 | + TEEC_FinalizeContext(&ctx); |
| 188 | +exit: |
| 189 | + return res; |
| 190 | +} |
0 commit comments