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 <stdio.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stddef.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <limits.h> |
| 12 | #include <tee_plugin_method.h> |
| 13 | #include <test_supp_plugin.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | static TEEC_Result pass_values(unsigned int sub_cmd, void *data, |
| 17 | size_t data_len, size_t *out_len) |
| 18 | { |
| 19 | struct add_op { |
| 20 | uint32_t a; |
| 21 | uint32_t b; |
| 22 | } *values = NULL; |
| 23 | |
| 24 | values = (struct add_op *)data; |
| 25 | *out_len = sizeof(uint32_t); |
| 26 | |
| 27 | if (sub_cmd == '+') |
| 28 | values->a = values->a + values->b; |
| 29 | else |
| 30 | values->a = values->a - values->b; |
| 31 | |
| 32 | return TEEC_SUCCESS; |
| 33 | } |
| 34 | |
| 35 | static TEEC_Result write_test_arr(unsigned int sub_cmd, void *data, |
| 36 | size_t data_len, size_t *out_len) |
| 37 | { |
| 38 | (void)sub_cmd; |
| 39 | (void)out_len; |
| 40 | |
| 41 | int fd = -1; |
| 42 | int n = -1; |
| 43 | char template[] = "/tmp/testplugin-XXXXXX"; |
| 44 | char fname[PATH_MAX] = { 0 }; |
| 45 | |
| 46 | strcpy(fname, template); |
| 47 | |
| 48 | fd = mkstemp(fname); |
| 49 | if (fd < 0) |
| 50 | return TEEC_ERROR_GENERIC; |
| 51 | |
| 52 | n = write(fd, (const void *)data, data_len); |
| 53 | if (n < 0) { |
| 54 | close(fd); |
| 55 | return TEEC_ERROR_GENERIC; |
| 56 | } |
| 57 | |
| 58 | *out_len = sizeof(template); |
| 59 | memcpy(data, (const void *)fname, *out_len); |
| 60 | close(fd); |
| 61 | |
| 62 | return TEEC_SUCCESS; |
| 63 | } |
| 64 | |
| 65 | static TEEC_Result get_test_arr(unsigned int sub_cmd, void *data, |
| 66 | size_t data_len, size_t *out_len) |
| 67 | { |
| 68 | (void)sub_cmd; |
| 69 | char test_arr[] = "Array from plugin"; |
| 70 | size_t test_size = sizeof(test_arr); |
| 71 | |
| 72 | if (data_len < test_size) |
| 73 | return TEEC_ERROR_GENERIC; |
| 74 | |
| 75 | memcpy(data, test_arr, test_size); |
| 76 | *out_len = test_size; |
| 77 | |
| 78 | return TEEC_SUCCESS; |
| 79 | } |
| 80 | |
| 81 | static TEEC_Result test_plugin_invoke(unsigned int cmd, unsigned int sub_cmd, |
| 82 | void *data, size_t data_len, |
| 83 | size_t *out_len) |
| 84 | { |
| 85 | switch (cmd) { |
| 86 | case TEST_PLUGIN_CMD_PING: |
| 87 | return TEEC_SUCCESS; |
| 88 | case TEST_PLUGIN_CMD_PASS_VALUES: |
| 89 | return pass_values(sub_cmd, data, data_len, out_len); |
| 90 | case TEST_PLUGIN_CMD_WRITE_ARR: |
| 91 | return write_test_arr(sub_cmd, data, data_len, out_len); |
| 92 | case TEST_PLUGIN_CMD_GET_ARR: |
| 93 | return get_test_arr(sub_cmd, data, data_len, out_len); |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | return TEEC_ERROR_NOT_SUPPORTED; |
| 99 | } |
| 100 | |
| 101 | struct plugin_method plugin_method = { |
| 102 | .name = "test", |
| 103 | .uuid = TEST_PLUGIN_UUID, |
| 104 | .invoke = test_plugin_invoke, |
| 105 | }; |