|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Microsoft Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <err.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include <tee_client_api.h> |
| 33 | +#include <tee_client_api_extensions.h> |
| 34 | + |
| 35 | +#include <rpc.h> |
| 36 | + |
| 37 | +static TEEC_Result rpc_callback(int func_id, TEEC_Parameter *parameters, |
| 38 | + void *context) |
| 39 | +{ |
| 40 | + struct ta_rpc_func_params *par; |
| 41 | + const char *msg = "wonderful world!"; |
| 42 | + const char *full_msg = "What a wonderful world!"; |
| 43 | + |
| 44 | + printf("Have RPC: %i\n", func_id); |
| 45 | + |
| 46 | + switch (func_id) |
| 47 | + { |
| 48 | + case TA_RPC_FUNC_NO_PARAMS: |
| 49 | + break; |
| 50 | + case TA_RPC_FUNC_PARAMS_IN: |
| 51 | + printf("First param: 0x%x, 0x%x.\n", parameters[0].value.a, |
| 52 | + parameters[0].value.b); |
| 53 | + printf("Fourth param: %s\n", |
| 54 | + (char *)parameters[3].tmpref.buffer); |
| 55 | + break; |
| 56 | + case TA_RPC_FUNC_PARAMS_INOUT: |
| 57 | + par = (struct ta_rpc_func_params *)parameters[2].tmpref.buffer; |
| 58 | + |
| 59 | + printf("First param: 0x%x, 0x%x.\n", parameters[0].value.a, |
| 60 | + parameters[0].value.b); |
| 61 | + printf("Second param: 0x%x, 0x%x.\n", parameters[1].value.a, |
| 62 | + parameters[1].value.b); |
| 63 | + printf("Third param: %s\n", par->second); |
| 64 | + printf("Fourth param: %s\n", |
| 65 | + (char *)parameters[3].tmpref.buffer); |
| 66 | + |
| 67 | + parameters[1].value.a = 0x3; |
| 68 | + parameters[1].value.b = 0xA; |
| 69 | + |
| 70 | + memcpy(par->second, msg, strlen(msg) + 1); |
| 71 | + break; |
| 72 | + case TA_RPC_FUNC_PARAMS_OUT: |
| 73 | + parameters[0].value.a = 0x4; |
| 74 | + parameters[0].value.b = 0xB; |
| 75 | + |
| 76 | + parameters[3].tmpref.size = strlen(full_msg) + 1; |
| 77 | + memcpy(parameters[3].tmpref.buffer, full_msg, |
| 78 | + parameters[3].tmpref.size); |
| 79 | + break; |
| 80 | + default: |
| 81 | + TEEC_ERROR_BAD_PARAMETERS; |
| 82 | + } |
| 83 | + |
| 84 | + return TEEC_SUCCESS; |
| 85 | +} |
| 86 | + |
| 87 | +int main(void) |
| 88 | +{ |
| 89 | + TEEC_Result res; |
| 90 | + TEEC_Context ctx; |
| 91 | + TEEC_Session sess; |
| 92 | + TEEC_Operation op; |
| 93 | + TEEC_UUID uuid = TA_RPC_UUID; |
| 94 | + uint32_t err_origin; |
| 95 | + |
| 96 | + res = TEEC_InitializeContext(NULL, &ctx); |
| 97 | + if (res != TEEC_SUCCESS) |
| 98 | + errx(1, "TEEC_InitializeContext failed with code 0x%x", res); |
| 99 | + |
| 100 | + res = TEEC_OpenSessionEx(&ctx, &sess, &uuid, |
| 101 | + TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin, |
| 102 | + rpc_callback, &sess); |
| 103 | + if (res != TEEC_SUCCESS) |
| 104 | + errx(1, "TEEC_OpenSessionEx failed with code 0x%x origin 0x%x", |
| 105 | + res, err_origin); |
| 106 | + |
| 107 | + /* |
| 108 | + * First Call |
| 109 | + */ |
| 110 | + |
| 111 | + memset(&op, 0, sizeof(op)); |
| 112 | + op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 113 | + TEEC_NONE); |
| 114 | + |
| 115 | + printf("Invoking TA to perform RPC back to host\n"); |
| 116 | + res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_NO_PARAMS, &op, |
| 117 | + &err_origin); |
| 118 | + if (res != TEEC_SUCCESS) |
| 119 | + errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x", |
| 120 | + res, err_origin); |
| 121 | + printf("Performed call\n"); |
| 122 | + |
| 123 | + /* |
| 124 | + * Second Call |
| 125 | + */ |
| 126 | + |
| 127 | + printf("Invoking TA to perform RPC back to host\n"); |
| 128 | + res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_PARAM_IN, &op, |
| 129 | + &err_origin); |
| 130 | + if (res != TEEC_SUCCESS) |
| 131 | + errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x", |
| 132 | + res, err_origin); |
| 133 | + printf("Performed call\n"); |
| 134 | + |
| 135 | + /* |
| 136 | + * Third Call |
| 137 | + */ |
| 138 | + |
| 139 | + printf("Invoking TA to perform RPC back to host\n"); |
| 140 | + res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_PARAM_INOUT, &op, |
| 141 | + &err_origin); |
| 142 | + if (res != TEEC_SUCCESS) |
| 143 | + errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x", |
| 144 | + res, err_origin); |
| 145 | + printf("Performed call\n"); |
| 146 | + |
| 147 | + /* |
| 148 | + * Fourth Call |
| 149 | + */ |
| 150 | + |
| 151 | + printf("Invoking TA to perform RPC back to host\n"); |
| 152 | + res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_PARAM_OUT, &op, |
| 153 | + &err_origin); |
| 154 | + if (res != TEEC_SUCCESS) |
| 155 | + errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x", |
| 156 | + res, err_origin); |
| 157 | + printf("Performed call\n"); |
| 158 | + |
| 159 | + TEEC_CloseSession(&sess); |
| 160 | + TEEC_FinalizeContext(&ctx); |
| 161 | + |
| 162 | + return 0; |
| 163 | +} |
0 commit comments