hacc_tee_req.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/device.h>
  5. #include <linux/mm.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/fs.h>
  10. #include <linux/device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/types.h>
  14. #include "mobicore_driver_api.h"
  15. #include "tlcApisec.h"
  16. #include "sec_error.h"
  17. #include "hacc_tee.h"
  18. static const struct mc_uuid_t MC_UUID_HACC = { {
  19. 0x05, 0x11, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00,
  21. 0x00, 0x00, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0x00}
  23. };
  24. uint32_t deviceId = MC_DEVICE_ID_DEFAULT;
  25. struct mc_session_handle drSessionHandle;
  26. static dapc_tciMessage_t *pTci;
  27. /* static int open_count = 0; */
  28. /* DO NOT invoke this function unless you get HACC lock */
  29. int open_sdriver_connection(void)
  30. {
  31. enum mc_result mcRet = 0;
  32. int retryCnt = 0;
  33. do {
  34. /* Initialize session handle data */
  35. mcRet = mc_open_device(deviceId);
  36. if (MC_DRV_OK != mcRet) {
  37. retryCnt++;
  38. pr_debug("NWD HACC: mc_open_device failed: %d, retry count (%d)\n", mcRet,
  39. retryCnt);
  40. continue;
  41. }
  42. pr_debug("NWD HACC: mc_open_device success: %d\n", mcRet);
  43. /* Allocating WSM for DCI */
  44. mcRet =
  45. mc_malloc_wsm(deviceId, 0, sizeof(dapc_tciMessage_t), (uint8_t **) &pTci, 0);
  46. if (MC_DRV_OK != mcRet) {
  47. pr_debug("NWD HACC: mc_malloc_wsm failed: %d\n", mcRet);
  48. break;
  49. }
  50. /* Open session to the trustlet */
  51. memset(&drSessionHandle, 0, sizeof(drSessionHandle));
  52. drSessionHandle.device_id = deviceId;
  53. mcRet =
  54. mc_open_session(&drSessionHandle, &MC_UUID_HACC, (uint8_t *) pTci,
  55. (uint32_t) sizeof(dapc_tciMessage_t));
  56. if (MC_DRV_OK != mcRet) {
  57. pr_debug("NWD HACC: mc_open_session failed: %d\n", mcRet);
  58. break;
  59. }
  60. pr_debug("NWD HACC: mc_open_session success: %d\n", mcRet);
  61. break;
  62. } while (retryCnt < 30);
  63. if (MC_DRV_OK != mcRet)
  64. return -1;
  65. return 0;
  66. }
  67. /* DO NOT invoke this function unless you get HACC lock */
  68. int close_sdriver_connection(void)
  69. {
  70. enum mc_result mcRet = 0;
  71. do {
  72. /* Close session */
  73. mcRet = mc_close_session(&drSessionHandle);
  74. if (MC_DRV_OK != mcRet) {
  75. pr_debug("NWD HACC: mc_close_session failed: %d\n", mcRet);
  76. break;
  77. }
  78. pr_debug("NWD HACC: mc_close_session success: %d\n", mcRet);
  79. memset(&drSessionHandle, 0, sizeof(drSessionHandle));
  80. /* Free WSM for DCI */
  81. mcRet = mc_free_wsm(deviceId, (uint8_t *) pTci);
  82. if (MC_DRV_OK != mcRet) {
  83. pr_debug("NWD HACC: mc_free_wsm failed: %d\n", mcRet);
  84. break;
  85. }
  86. pTci = NULL;
  87. /* Close MobiCore device */
  88. mcRet = mc_close_device(deviceId);
  89. if (MC_DRV_OK != mcRet) {
  90. pr_debug("NWD HACC: mc_close_device failed: %d\n", mcRet);
  91. break;
  92. }
  93. pr_debug("NWD HACC: mc_close_device success: %d\n", mcRet);
  94. } while (false);
  95. if (MC_DRV_OK != mcRet)
  96. return -1;
  97. return 0;
  98. }
  99. /* DO NOT invoke this function unless you get HACC lock */
  100. int tee_secure_request(unsigned int user, unsigned char *data, unsigned int data_size,
  101. unsigned int direction, unsigned char *seed, unsigned int seed_size)
  102. {
  103. int ret = SEC_OK;
  104. struct mc_bulk_map dataMapInfo;
  105. struct mc_bulk_map seedMapInfo;
  106. char *databuf = NULL;
  107. char *seedbuf = NULL;
  108. enum mc_result mcRet = 0;
  109. /* allocate data buffer to be sent to TEE */
  110. databuf = vmalloc(data_size);
  111. if (databuf == NULL) {
  112. ret = ERR_HACC_ALLOCATE_BUFFER_FAIL;
  113. goto _allocate_data_buf_err;
  114. }
  115. memcpy(databuf, data, data_size);
  116. if (seed_size != 0) {
  117. /* allocate seed buffer to be sent to TEE */
  118. seedbuf = vmalloc(seed_size);
  119. if (seedbuf == NULL) {
  120. ret = ERR_HACC_ALLOCATE_BUFFER_FAIL;
  121. goto _allocate_seed_buf_err;
  122. }
  123. memcpy(seedbuf, seed, seed_size);
  124. }
  125. /* map TCI virtual address for data buffer */
  126. ret = mc_map(&drSessionHandle, databuf, data_size, &dataMapInfo);
  127. if (MC_DRV_OK != ret) {
  128. pr_debug("NWD HACC: mcMap failed of data buffer: %d", ret);
  129. ret = ERR_HACC_MCMAP_BUFFER_FAIL;
  130. goto _mcmap_data_fail;
  131. }
  132. pTci->data_addr = (uint32_t) dataMapInfo.secure_virt_addr;
  133. pTci->data_len = data_size;
  134. if (seed_size != 0) {
  135. /* map TCI virtual address for seed buffer */
  136. ret = mc_map(&drSessionHandle, seedbuf, seed_size, &seedMapInfo);
  137. if (MC_DRV_OK != ret) {
  138. pr_debug("NWD HACC: mcMap failed of seed buffer: %d", ret);
  139. ret = ERR_HACC_MCMAP_BUFFER_FAIL;
  140. goto _mcmap_seed_fail;
  141. }
  142. pTci->seed_addr = (uint32_t) seedMapInfo.secure_virt_addr;
  143. pTci->seed_len = seed_size;
  144. } else {
  145. pTci->seed_addr = 0;
  146. pTci->seed_len = 0;
  147. }
  148. /* set other TCI parameter */
  149. pTci->hacc_user = user;
  150. pTci->direction = direction;
  151. /* set TCI command */
  152. pTci->cmd.header.commandId = CMD_HACC_REQUEST;
  153. /* notify the trustlet */
  154. pr_debug("NWD HACC: prepare notify\n");
  155. mcRet = mc_notify(&drSessionHandle);
  156. if (MC_DRV_OK != mcRet) {
  157. pr_debug("NWD HACC IRQ fail: mc_notify returned: %d\n", mcRet);
  158. ret = ERR_HACC_NOTIFY_TO_TRUSTLET_FAIL;
  159. goto _notify_to_trustlet_fail;
  160. }
  161. /* wait for response from the trustlet */
  162. mcRet = mc_wait_notification(&drSessionHandle, /*MC_INFINITE_TIMEOUT */ 20000);
  163. if (MC_DRV_OK != mcRet) {
  164. pr_debug("NWD HACC IRQ fail: mc_wait_notification 20s timeout: %d\n", mcRet);
  165. ret = ERR_HACC_NOTIFY_FROM_TRUSTLET_FAIL;
  166. goto _notify_from_trustlet_fail;
  167. }
  168. if (pTci->result != 0) {
  169. pr_debug("NWD HACC Request Fail!!!!!!!!(ret:%d, err:%d)\n", pTci->result,
  170. pTci->rsp.header.returnCode);
  171. } else {
  172. pr_debug("NWD HACC Request Success!!!!!!!!\n");
  173. /* update result from secure buffer */
  174. memcpy(data, databuf, data_size);
  175. }
  176. _notify_from_trustlet_fail:
  177. _notify_to_trustlet_fail:
  178. if (seed_size != 0)
  179. mc_unmap(&drSessionHandle, seedbuf, &seedMapInfo);
  180. _mcmap_seed_fail:
  181. mc_unmap(&drSessionHandle, databuf, &dataMapInfo);
  182. _mcmap_data_fail:
  183. if (seed_size != 0)
  184. vfree(seedbuf);
  185. _allocate_seed_buf_err:
  186. vfree(databuf);
  187. _allocate_data_buf_err:
  188. return ret;
  189. }