0001
0002
0003
0004
0005
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007
0008 #include <linux/kernel.h>
0009 #include <linux/slab.h>
0010 #include <linux/tee_drv.h>
0011 #include <linux/uuid.h>
0012 #include "optee_private.h"
0013
0014 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
0015 {
0016 if (ver->impl_id == TEE_IMPL_ID_OPTEE)
0017 return 1;
0018 else
0019 return 0;
0020 }
0021
0022 static int get_devices(struct tee_context *ctx, u32 session,
0023 struct tee_shm *device_shm, u32 *shm_size,
0024 u32 func)
0025 {
0026 int ret = 0;
0027 struct tee_ioctl_invoke_arg inv_arg;
0028 struct tee_param param[4];
0029
0030 memset(&inv_arg, 0, sizeof(inv_arg));
0031 memset(¶m, 0, sizeof(param));
0032
0033 inv_arg.func = func;
0034 inv_arg.session = session;
0035 inv_arg.num_params = 4;
0036
0037
0038 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
0039 param[0].u.memref.shm = device_shm;
0040 param[0].u.memref.size = *shm_size;
0041 param[0].u.memref.shm_offs = 0;
0042
0043 ret = tee_client_invoke_func(ctx, &inv_arg, param);
0044 if ((ret < 0) || ((inv_arg.ret != TEEC_SUCCESS) &&
0045 (inv_arg.ret != TEEC_ERROR_SHORT_BUFFER))) {
0046 pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n",
0047 inv_arg.ret);
0048 return -EINVAL;
0049 }
0050
0051 *shm_size = param[0].u.memref.size;
0052
0053 return 0;
0054 }
0055
0056 static void optee_release_device(struct device *dev)
0057 {
0058 struct tee_client_device *optee_device = to_tee_client_device(dev);
0059
0060 kfree(optee_device);
0061 }
0062
0063 static int optee_register_device(const uuid_t *device_uuid)
0064 {
0065 struct tee_client_device *optee_device = NULL;
0066 int rc;
0067
0068 optee_device = kzalloc(sizeof(*optee_device), GFP_KERNEL);
0069 if (!optee_device)
0070 return -ENOMEM;
0071
0072 optee_device->dev.bus = &tee_bus_type;
0073 optee_device->dev.release = optee_release_device;
0074 if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) {
0075 kfree(optee_device);
0076 return -ENOMEM;
0077 }
0078 uuid_copy(&optee_device->id.uuid, device_uuid);
0079
0080 rc = device_register(&optee_device->dev);
0081 if (rc) {
0082 pr_err("device registration failed, err: %d\n", rc);
0083 kfree(optee_device);
0084 }
0085
0086 return rc;
0087 }
0088
0089 static int __optee_enumerate_devices(u32 func)
0090 {
0091 const uuid_t pta_uuid =
0092 UUID_INIT(0x7011a688, 0xddde, 0x4053,
0093 0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8);
0094 struct tee_ioctl_open_session_arg sess_arg;
0095 struct tee_shm *device_shm = NULL;
0096 const uuid_t *device_uuid = NULL;
0097 struct tee_context *ctx = NULL;
0098 u32 shm_size = 0, idx, num_devices = 0;
0099 int rc;
0100
0101 memset(&sess_arg, 0, sizeof(sess_arg));
0102
0103
0104 ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
0105 if (IS_ERR(ctx))
0106 return -ENODEV;
0107
0108
0109 export_uuid(sess_arg.uuid, &pta_uuid);
0110 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
0111 sess_arg.num_params = 0;
0112
0113 rc = tee_client_open_session(ctx, &sess_arg, NULL);
0114 if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) {
0115
0116 rc = 0;
0117 goto out_ctx;
0118 }
0119
0120 rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func);
0121 if (rc < 0 || !shm_size)
0122 goto out_sess;
0123
0124 device_shm = tee_shm_alloc_kernel_buf(ctx, shm_size);
0125 if (IS_ERR(device_shm)) {
0126 pr_err("tee_shm_alloc_kernel_buf failed\n");
0127 rc = PTR_ERR(device_shm);
0128 goto out_sess;
0129 }
0130
0131 rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func);
0132 if (rc < 0)
0133 goto out_shm;
0134
0135 device_uuid = tee_shm_get_va(device_shm, 0);
0136 if (IS_ERR(device_uuid)) {
0137 pr_err("tee_shm_get_va failed\n");
0138 rc = PTR_ERR(device_uuid);
0139 goto out_shm;
0140 }
0141
0142 num_devices = shm_size / sizeof(uuid_t);
0143
0144 for (idx = 0; idx < num_devices; idx++) {
0145 rc = optee_register_device(&device_uuid[idx]);
0146 if (rc)
0147 goto out_shm;
0148 }
0149
0150 out_shm:
0151 tee_shm_free(device_shm);
0152 out_sess:
0153 tee_client_close_session(ctx, sess_arg.session);
0154 out_ctx:
0155 tee_client_close_context(ctx);
0156
0157 return rc;
0158 }
0159
0160 int optee_enumerate_devices(u32 func)
0161 {
0162 return __optee_enumerate_devices(func);
0163 }
0164
0165 static int __optee_unregister_device(struct device *dev, void *data)
0166 {
0167 if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta")))
0168 device_unregister(dev);
0169
0170 return 0;
0171 }
0172
0173 void optee_unregister_devices(void)
0174 {
0175 bus_for_each_dev(&tee_bus_type, NULL, NULL,
0176 __optee_unregister_device);
0177 }