summaryrefstree
path: root/kernel/drivers/misc/tzdev/teec/misc.c
blob: 84c104b49f427d4d2b4d1a8da60c1024e1f6ca4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) 2012-2019, Samsung Electronics Co., Ltd.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/anon_inodes.h>
#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/file.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/types.h>

#include <tzdev/tee_client_api.h>

#include "misc.h"
#include "core/iwsock.h"
#include "core/kthread_pool.h"
#include "core/log.h"
#include "core/sysdep.h"

#define MAX_CONNECTION_ATTEMPTS 20
#define CONNECTION_ATTEMPT_TIMEOUT 100

int tzdev_teec_connect(struct sock_desc *socket, char *name,
				uint32_t *result, uint32_t *origin)
{
	int ret;
	unsigned int attempt;

	*origin = TEEC_ORIGIN_COMMS;
	*result = TEEC_SUCCESS;

	for (attempt = 0; attempt < MAX_CONNECTION_ATTEMPTS; attempt++) {
		ret = tz_iwsock_connect(socket, name, 0);
		if (ret == 0)
			return ret;

		log_info(tzdev_teec, "Failed to connect to %s socket, error = %d, retrying...\n",
				name, ret);

		msleep(CONNECTION_ATTEMPT_TIMEOUT);
	}

	log_error(tzdev_teec, "Failed to connect to %s socket after %d retries\n",
			name, MAX_CONNECTION_ATTEMPTS);

	*result = tzdev_teec_error_to_tee_error(ret);

	return ret;
}

void tzdev_teec_disconnect(struct sock_desc *socket)
{
	tz_iwsock_release(socket);
}

int tzdev_teec_send(struct sock_desc *socket, void *data, uint32_t size, int flags,
		uint32_t *result, uint32_t *origin)
{
	int ret;

	*origin = TEEC_ORIGIN_COMMS;
	*result = TEEC_SUCCESS;

	ret = tz_iwsock_write(socket, data, size, flags);
	if (ret >= 0 && ret != size)
		ret = -EMSGSIZE;
	if (ret < 0)
		*result = tzdev_teec_error_to_tee_error(ret);

	return ret;
}

int tzdev_teec_recv(struct sock_desc *socket, void *data, uint32_t size, int flags,
		uint32_t *result, uint32_t *origin)
{
	int ret;

	*origin = TEEC_ORIGIN_COMMS;
	*result = TEEC_SUCCESS;

	ret = tz_iwsock_read(socket, data, size, flags);
	if (ret > 0 && ret != size)
		ret = -EMSGSIZE;
	if (!ret)
		ret = -ECONNRESET;
	if (ret < 0)
		*result = tzdev_teec_error_to_tee_error(ret);

	return ret;
}

int tzdev_teec_send_then_recv(struct sock_desc *socket,
		void *send_data, uint32_t send_size, int send_flags,
		void *recv_data, uint32_t recv_size, int recv_flags,
		uint32_t *result, uint32_t *origin)
{
	int ret;

	ret = tzdev_teec_send(socket, send_data, send_size, send_flags,
			result, origin);
	if (ret < 0)
		return ret;

	ret = tzdev_teec_recv(socket, recv_data, recv_size, recv_flags,
			result, origin);
	if (ret < 0)
		return ret;

	return 0;
}

int tzdev_teec_check_reply(struct cmd_reply *reply, uint32_t cmd, uint32_t serial,
		uint32_t *result, uint32_t *origin)
{
	int ret;

	*origin = TEEC_ORIGIN_COMMS;
	*result = TEEC_SUCCESS;

	if (reply->base.cmd != cmd) {
		log_error(tzdev_teec, "Received wrong reply = %u, expected = %u\n",
				reply->base.cmd, cmd);
		ret = -ECOMM;
		goto out;
	}

	if (reply->base.serial != serial) {
		log_error(tzdev_teec, "Received wrong reply serial = %u, expected = %u\n",
				reply->base.serial, serial);
		ret = -ECOMM;
		goto out;
	}

	return 0;

out:
	*result = tzdev_teec_error_to_tee_error(ret);

	return ret;
}

uint32_t tzdev_teec_error_to_tee_error(int error)
{
	switch (error) {
	case -EINVAL:
		return TEEC_ERROR_BAD_PARAMETERS;
	case -ENOMEM:
		return TEEC_ERROR_OUT_OF_MEMORY;
	case -EPIPE:
	case -ENOTCONN:
	case -ECONNREFUSED:
	case -EMSGSIZE:
	case -ECOMM:
		return TEEC_ERROR_COMMUNICATION;
	case -ECONNRESET:
		return TEEC_ERROR_TARGET_DEAD;
	case -ECANCELED:
		return TEEC_ERROR_CANCEL;
	default:
		log_error(tzdev_teec, "Unknown error code = %d\n", error);
		return TEEC_ERROR_GENERIC;
	}
}

void tzdev_teec_fixup_origin(uint32_t result, uint32_t *origin)
{
	if (result == TEEC_ERROR_TARGET_DEAD)
		*origin = TEEC_ORIGIN_TEE;
}