hello_world_ta.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <tee_internal_api.h>
  28. #include <tee_internal_api_extensions.h>
  29. #include <hello_world_ta.h>
  30. /*
  31. * Called when the instance of the TA is created. This is the first call in
  32. * the TA.
  33. */
  34. TEE_Result TA_CreateEntryPoint(void)
  35. {
  36. DMSG("has been called");
  37. return TEE_SUCCESS;
  38. }
  39. /*
  40. * Called when the instance of the TA is destroyed if the TA has not
  41. * crashed or panicked. This is the last call in the TA.
  42. */
  43. void TA_DestroyEntryPoint(void)
  44. {
  45. DMSG("has been called");
  46. }
  47. /*
  48. * Called when a new session is opened to the TA. *sess_ctx can be updated
  49. * with a value to be able to identify this session in subsequent calls to the
  50. * TA. In this function you will normally do the global initialization for the
  51. * TA.
  52. */
  53. TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
  54. TEE_Param __maybe_unused params[4],
  55. void __maybe_unused **sess_ctx)
  56. {
  57. uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
  58. TEE_PARAM_TYPE_NONE,
  59. TEE_PARAM_TYPE_NONE,
  60. TEE_PARAM_TYPE_NONE);
  61. DMSG("has been called");
  62. if (param_types != exp_param_types)
  63. return TEE_ERROR_BAD_PARAMETERS;
  64. /* Unused parameters */
  65. (void)&params;
  66. (void)&sess_ctx;
  67. /*
  68. * The DMSG() macro is non-standard, TEE Internal API doesn't
  69. * specify any means to logging from a TA.
  70. */
  71. IMSG("Hello World!\n");
  72. /* If return value != TEE_SUCCESS the session will not be created. */
  73. return TEE_SUCCESS;
  74. }
  75. /*
  76. * Called when a session is closed, sess_ctx hold the value that was
  77. * assigned by TA_OpenSessionEntryPoint().
  78. */
  79. void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
  80. {
  81. (void)&sess_ctx; /* Unused parameter */
  82. IMSG("Goodbye!\n");
  83. }
  84. static TEE_Result inc_value(uint32_t param_types,
  85. TEE_Param params[4])
  86. {
  87. uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
  88. TEE_PARAM_TYPE_NONE,
  89. TEE_PARAM_TYPE_NONE,
  90. TEE_PARAM_TYPE_NONE);
  91. DMSG("has been called");
  92. if (param_types != exp_param_types)
  93. return TEE_ERROR_BAD_PARAMETERS;
  94. IMSG("Got value: %u from NW", params[0].value.a);
  95. params[0].value.a++;
  96. IMSG("Increase value to: %u", params[0].value.a);
  97. return TEE_SUCCESS;
  98. }
  99. static TEE_Result dec_value(uint32_t param_types,
  100. TEE_Param params[4])
  101. {
  102. uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
  103. TEE_PARAM_TYPE_NONE,
  104. TEE_PARAM_TYPE_NONE,
  105. TEE_PARAM_TYPE_NONE);
  106. DMSG("has been called");
  107. if (param_types != exp_param_types)
  108. return TEE_ERROR_BAD_PARAMETERS;
  109. IMSG("Got value: %u from NW", params[0].value.a);
  110. params[0].value.a--;
  111. IMSG("Decrease value to: %u", params[0].value.a);
  112. return TEE_SUCCESS;
  113. }
  114. /*
  115. * Called when a TA is invoked. sess_ctx hold that value that was
  116. * assigned by TA_OpenSessionEntryPoint(). The rest of the paramters
  117. * comes from normal world.
  118. */
  119. TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx,
  120. uint32_t cmd_id,
  121. uint32_t param_types, TEE_Param params[4])
  122. {
  123. (void)&sess_ctx; /* Unused parameter */
  124. switch (cmd_id) {
  125. case TA_HELLO_WORLD_CMD_INC_VALUE:
  126. return inc_value(param_types, params);
  127. case TA_HELLO_WORLD_CMD_DEC_VALUE:
  128. return dec_value(param_types, params);
  129. default:
  130. return TEE_ERROR_BAD_PARAMETERS;
  131. }
  132. }