device.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Linaro Ltd.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/tee_core.h>
  9. #include <linux/uuid.h>
  10. #include "optee_private.h"
  11. static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
  12. {
  13. if (ver->impl_id == TEE_IMPL_ID_OPTEE)
  14. return 1;
  15. else
  16. return 0;
  17. }
  18. static int get_devices(struct tee_context *ctx, u32 session,
  19. struct tee_shm *device_shm, u32 *shm_size,
  20. u32 func)
  21. {
  22. int ret = 0;
  23. struct tee_ioctl_invoke_arg inv_arg;
  24. struct tee_param param[4];
  25. memset(&inv_arg, 0, sizeof(inv_arg));
  26. memset(&param, 0, sizeof(param));
  27. inv_arg.func = func;
  28. inv_arg.session = session;
  29. inv_arg.num_params = 4;
  30. /* Fill invoke cmd params */
  31. param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
  32. param[0].u.memref.shm = device_shm;
  33. param[0].u.memref.size = *shm_size;
  34. param[0].u.memref.shm_offs = 0;
  35. ret = tee_client_invoke_func(ctx, &inv_arg, param);
  36. if ((ret < 0) || ((inv_arg.ret != TEEC_SUCCESS) &&
  37. (inv_arg.ret != TEEC_ERROR_SHORT_BUFFER))) {
  38. /*
  39. * TEE_ERROR_STORAGE_NOT_AVAILABLE is returned when getting
  40. * the list of device TAs that depends on RPMB but a usable
  41. * RPMB device isn't found.
  42. */
  43. if (inv_arg.ret == TEE_ERROR_STORAGE_NOT_AVAILABLE)
  44. return -ENODEV;
  45. pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n",
  46. inv_arg.ret);
  47. return -EINVAL;
  48. }
  49. *shm_size = param[0].u.memref.size;
  50. return 0;
  51. }
  52. static void optee_release_device(struct device *dev)
  53. {
  54. struct tee_client_device *optee_device = to_tee_client_device(dev);
  55. kfree(optee_device);
  56. }
  57. static ssize_t need_supplicant_show(struct device *dev,
  58. struct device_attribute *attr,
  59. char *buf)
  60. {
  61. return 0;
  62. }
  63. static DEVICE_ATTR_RO(need_supplicant);
  64. static int optee_register_device(const uuid_t *device_uuid, u32 func)
  65. {
  66. struct tee_client_device *optee_device = NULL;
  67. int rc;
  68. optee_device = kzalloc_obj(*optee_device);
  69. if (!optee_device)
  70. return -ENOMEM;
  71. optee_device->dev.bus = &tee_bus_type;
  72. optee_device->dev.release = optee_release_device;
  73. if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) {
  74. kfree(optee_device);
  75. return -ENOMEM;
  76. }
  77. uuid_copy(&optee_device->id.uuid, device_uuid);
  78. rc = device_register(&optee_device->dev);
  79. if (rc) {
  80. pr_err("device registration failed, err: %d\n", rc);
  81. put_device(&optee_device->dev);
  82. return rc;
  83. }
  84. if (func == PTA_CMD_GET_DEVICES_SUPP)
  85. device_create_file(&optee_device->dev,
  86. &dev_attr_need_supplicant);
  87. return 0;
  88. }
  89. static int __optee_enumerate_devices(u32 func)
  90. {
  91. const uuid_t pta_uuid =
  92. UUID_INIT(0x7011a688, 0xddde, 0x4053,
  93. 0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8);
  94. struct tee_ioctl_open_session_arg sess_arg;
  95. struct tee_shm *device_shm = NULL;
  96. const uuid_t *device_uuid = NULL;
  97. struct tee_context *ctx = NULL;
  98. u32 shm_size = 0, idx, num_devices = 0;
  99. int rc;
  100. memset(&sess_arg, 0, sizeof(sess_arg));
  101. /* Open context with OP-TEE driver */
  102. ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
  103. if (IS_ERR(ctx))
  104. return -ENODEV;
  105. /* Open session with device enumeration pseudo TA */
  106. export_uuid(sess_arg.uuid, &pta_uuid);
  107. sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
  108. sess_arg.num_params = 0;
  109. rc = tee_client_open_session(ctx, &sess_arg, NULL);
  110. if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) {
  111. /* Device enumeration pseudo TA not found */
  112. rc = 0;
  113. goto out_ctx;
  114. }
  115. rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func);
  116. if (rc < 0 || !shm_size)
  117. goto out_sess;
  118. device_shm = tee_shm_alloc_kernel_buf(ctx, shm_size);
  119. if (IS_ERR(device_shm)) {
  120. pr_err("tee_shm_alloc_kernel_buf failed\n");
  121. rc = PTR_ERR(device_shm);
  122. goto out_sess;
  123. }
  124. rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func);
  125. if (rc < 0)
  126. goto out_shm;
  127. device_uuid = tee_shm_get_va(device_shm, 0);
  128. if (IS_ERR(device_uuid)) {
  129. pr_err("tee_shm_get_va failed\n");
  130. rc = PTR_ERR(device_uuid);
  131. goto out_shm;
  132. }
  133. num_devices = shm_size / sizeof(uuid_t);
  134. for (idx = 0; idx < num_devices; idx++) {
  135. rc = optee_register_device(&device_uuid[idx], func);
  136. if (rc)
  137. goto out_shm;
  138. }
  139. out_shm:
  140. tee_shm_free(device_shm);
  141. out_sess:
  142. tee_client_close_session(ctx, sess_arg.session);
  143. out_ctx:
  144. tee_client_close_context(ctx);
  145. return rc;
  146. }
  147. int optee_enumerate_devices(u32 func)
  148. {
  149. return __optee_enumerate_devices(func);
  150. }
  151. static int __optee_unregister_device(struct device *dev, void *data)
  152. {
  153. if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta")))
  154. device_unregister(dev);
  155. return 0;
  156. }
  157. void optee_unregister_devices(void)
  158. {
  159. bus_for_each_dev(&tee_bus_type, NULL, NULL,
  160. __optee_unregister_device);
  161. }