main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2016, Linaro Limited
  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. #include <err.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. /* OP-TEE TEE client API (built by optee_client) */
  31. #include <tee_client_api.h>
  32. /* For the UUID (found in the TA's h-file(s)) */
  33. #include <hello_world_ta.h>
  34. int main(void)
  35. {
  36. TEEC_Result res;
  37. TEEC_Context ctx;
  38. TEEC_Session sess;
  39. TEEC_Operation op;
  40. TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
  41. uint32_t err_origin;
  42. /* Initialize a context connecting us to the TEE */
  43. res = TEEC_InitializeContext(NULL, &ctx);
  44. if (res != TEEC_SUCCESS)
  45. errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
  46. /*
  47. * Open a session to the "hello world" TA, the TA will print "hello
  48. * world!" in the log when the session is created.
  49. */
  50. res = TEEC_OpenSession(&ctx, &sess, &uuid,
  51. TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
  52. if (res != TEEC_SUCCESS)
  53. errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
  54. res, err_origin);
  55. /*
  56. * Execute a function in the TA by invoking it, in this case
  57. * we're incrementing a number.
  58. *
  59. * The value of command ID part and how the parameters are
  60. * interpreted is part of the interface provided by the TA.
  61. */
  62. /* Clear the TEEC_Operation struct */
  63. memset(&op, 0, sizeof(op));
  64. /*
  65. * Prepare the argument. Pass a value in the first parameter,
  66. * the remaining three parameters are unused.
  67. */
  68. op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,
  69. TEEC_NONE, TEEC_NONE);
  70. op.params[0].value.a = 42;
  71. /*
  72. * TA_HELLO_WORLD_CMD_INC_VALUE is the actual function in the TA to be
  73. * called.
  74. */
  75. printf("Invoking TA to increment %d\n", op.params[0].value.a);
  76. res = TEEC_InvokeCommand(&sess, TA_HELLO_WORLD_CMD_INC_VALUE, &op,
  77. &err_origin);
  78. if (res != TEEC_SUCCESS)
  79. errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
  80. res, err_origin);
  81. printf("TA incremented value to %d\n", op.params[0].value.a);
  82. /*
  83. * We're done with the TA, close the session and
  84. * destroy the context.
  85. *
  86. * The TA will print "Goodbye!" in the log when the
  87. * session is closed.
  88. */
  89. TEEC_CloseSession(&sess);
  90. TEEC_FinalizeContext(&ctx);
  91. return 0;
  92. }