• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI variable service via TEE
4  *
5  *  Copyright (C) 2022 Linaro
6  */
7 
8 #include <linux/efi.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/tee.h>
12 #include <linux/tee_drv.h>
13 #include <linux/ucs2_string.h>
14 #include "mm_communication.h"
15 
16 static struct efivars tee_efivars;
17 static struct efivar_operations tee_efivar_ops;
18 
19 static size_t max_buffer_size; /* comm + var + func + data */
20 static size_t max_payload_size; /* func + data */
21 
22 struct tee_stmm_efi_private {
23 	struct tee_context *ctx;
24 	u32 session;
25 	struct device *dev;
26 };
27 
28 static struct tee_stmm_efi_private pvt_data;
29 
30 /* UUID of the stmm PTA */
31 static const struct tee_client_device_id tee_stmm_efi_id_table[] = {
32 	{PTA_STMM_UUID},
33 	{}
34 };
35 
tee_ctx_match(struct tee_ioctl_version_data * ver,const void * data)36 static int tee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
37 {
38 	/* currently only OP-TEE is supported as a communication path */
39 	if (ver->impl_id == TEE_IMPL_ID_OPTEE)
40 		return 1;
41 	else
42 		return 0;
43 }
44 
45 /**
46  * tee_mm_communicate() - Pass a buffer to StandaloneMM running in TEE
47  *
48  * @comm_buf:		locally allocated communication buffer
49  * @dsize:		buffer size
50  * Return:		status code
51  */
tee_mm_communicate(void * comm_buf,size_t dsize)52 static efi_status_t tee_mm_communicate(void *comm_buf, size_t dsize)
53 {
54 	size_t buf_size;
55 	struct efi_mm_communicate_header *mm_hdr;
56 	struct tee_ioctl_invoke_arg arg;
57 	struct tee_param param[4];
58 	struct tee_shm *shm = NULL;
59 	int rc;
60 
61 	if (!comm_buf)
62 		return EFI_INVALID_PARAMETER;
63 
64 	mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
65 	buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
66 
67 	if (dsize != buf_size)
68 		return EFI_INVALID_PARAMETER;
69 
70 	shm = tee_shm_register_kernel_buf(pvt_data.ctx, comm_buf, buf_size);
71 	if (IS_ERR(shm)) {
72 		dev_err(pvt_data.dev, "Unable to register shared memory\n");
73 		return EFI_UNSUPPORTED;
74 	}
75 
76 	memset(&arg, 0, sizeof(arg));
77 	arg.func = PTA_STMM_CMD_COMMUNICATE;
78 	arg.session = pvt_data.session;
79 	arg.num_params = 4;
80 
81 	memset(param, 0, sizeof(param));
82 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
83 	param[0].u.memref.size = buf_size;
84 	param[0].u.memref.shm = shm;
85 	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
86 	param[2].attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
87 	param[3].attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
88 
89 	rc = tee_client_invoke_func(pvt_data.ctx, &arg, param);
90 	tee_shm_free(shm);
91 
92 	if (rc < 0 || arg.ret != 0) {
93 		dev_err(pvt_data.dev,
94 			"PTA_STMM_CMD_COMMUNICATE invoke error: 0x%x\n", arg.ret);
95 		return EFI_DEVICE_ERROR;
96 	}
97 
98 	switch (param[1].u.value.a) {
99 	case ARM_SVC_SPM_RET_SUCCESS:
100 		return EFI_SUCCESS;
101 
102 	case ARM_SVC_SPM_RET_INVALID_PARAMS:
103 		return EFI_INVALID_PARAMETER;
104 
105 	case ARM_SVC_SPM_RET_DENIED:
106 		return EFI_ACCESS_DENIED;
107 
108 	case ARM_SVC_SPM_RET_NO_MEMORY:
109 		return EFI_OUT_OF_RESOURCES;
110 
111 	default:
112 		return EFI_ACCESS_DENIED;
113 	}
114 }
115 
116 /**
117  * mm_communicate() - Adjust the communication buffer to StandAlonneMM and send
118  * it to TEE
119  *
120  * @comm_buf:		locally allocated communication buffer, buffer should
121  *			be enough big to have some headers and payload
122  * @payload_size:	payload size
123  * Return:		status code
124  */
mm_communicate(u8 * comm_buf,size_t payload_size)125 static efi_status_t mm_communicate(u8 *comm_buf, size_t payload_size)
126 {
127 	size_t dsize;
128 	efi_status_t ret;
129 	struct efi_mm_communicate_header *mm_hdr;
130 	struct smm_variable_communicate_header *var_hdr;
131 
132 	dsize = payload_size + MM_COMMUNICATE_HEADER_SIZE +
133 		MM_VARIABLE_COMMUNICATE_SIZE;
134 	mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
135 	var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
136 
137 	ret = tee_mm_communicate(comm_buf, dsize);
138 	if (ret != EFI_SUCCESS) {
139 		dev_err(pvt_data.dev, "%s failed!\n", __func__);
140 		return ret;
141 	}
142 
143 	return var_hdr->ret_status;
144 }
145 
146 #define COMM_BUF_SIZE(__payload_size)	(MM_COMMUNICATE_HEADER_SIZE + \
147 					 MM_VARIABLE_COMMUNICATE_SIZE + \
148 					 (__payload_size))
149 
150 /**
151  * setup_mm_hdr() -	Allocate a buffer for StandAloneMM and initialize the
152  *			header data.
153  *
154  * @dptr:		pointer address to store allocated buffer
155  * @payload_size:	payload size
156  * @func:		standAloneMM function number
157  * @ret:		EFI return code
158  * Return:		pointer to corresponding StandAloneMM function buffer or NULL
159  */
setup_mm_hdr(u8 ** dptr,size_t payload_size,size_t func,efi_status_t * ret)160 static void *setup_mm_hdr(u8 **dptr, size_t payload_size, size_t func,
161 			  efi_status_t *ret)
162 {
163 	const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
164 	struct efi_mm_communicate_header *mm_hdr;
165 	struct smm_variable_communicate_header *var_hdr;
166 	u8 *comm_buf;
167 
168 	/* In the init function we initialize max_buffer_size with
169 	 * get_max_payload(). So skip the test if max_buffer_size is initialized
170 	 * StandAloneMM will perform similar checks and drop the buffer if it's
171 	 * too long
172 	 */
173 	if (max_buffer_size &&
174 	    max_buffer_size < (MM_COMMUNICATE_HEADER_SIZE +
175 			       MM_VARIABLE_COMMUNICATE_SIZE + payload_size)) {
176 		*ret = EFI_INVALID_PARAMETER;
177 		return NULL;
178 	}
179 
180 	comm_buf = alloc_pages_exact(COMM_BUF_SIZE(payload_size),
181 				     GFP_KERNEL | __GFP_ZERO);
182 	if (!comm_buf) {
183 		*ret = EFI_OUT_OF_RESOURCES;
184 		return NULL;
185 	}
186 
187 	mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
188 	memcpy(&mm_hdr->header_guid, &mm_var_guid, sizeof(mm_hdr->header_guid));
189 	mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
190 
191 	var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
192 	var_hdr->function = func;
193 	if (dptr)
194 		*dptr = comm_buf;
195 	*ret = EFI_SUCCESS;
196 
197 	return var_hdr->data;
198 }
199 
200 /**
201  * get_max_payload() - Get variable payload size from StandAloneMM.
202  *
203  * @size:    size of the variable in storage
204  * Return:   status code
205  */
get_max_payload(size_t * size)206 static efi_status_t get_max_payload(size_t *size)
207 {
208 	struct smm_variable_payload_size *var_payload = NULL;
209 	size_t payload_size;
210 	u8 *comm_buf = NULL;
211 	efi_status_t ret;
212 
213 	if (!size)
214 		return EFI_INVALID_PARAMETER;
215 
216 	payload_size = sizeof(*var_payload);
217 	var_payload = setup_mm_hdr(&comm_buf, payload_size,
218 				   SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE,
219 				   &ret);
220 	if (!var_payload)
221 		return EFI_OUT_OF_RESOURCES;
222 
223 	ret = mm_communicate(comm_buf, payload_size);
224 	if (ret != EFI_SUCCESS)
225 		goto out;
226 
227 	/* Make sure the buffer is big enough for storing variables */
228 	if (var_payload->size < MM_VARIABLE_ACCESS_HEADER_SIZE + 0x20) {
229 		ret = EFI_DEVICE_ERROR;
230 		goto out;
231 	}
232 	*size = var_payload->size;
233 	/*
234 	 * There seems to be a bug in EDK2 miscalculating the boundaries and
235 	 * size checks, so deduct 2 more bytes to fulfill this requirement. Fix
236 	 * it up here to ensure backwards compatibility with older versions
237 	 * (cf. StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c.
238 	 * sizeof (EFI_MM_COMMUNICATE_HEADER) instead the size minus the
239 	 * flexible array member).
240 	 *
241 	 * size is guaranteed to be > 2 due to checks on the beginning.
242 	 */
243 	*size -= 2;
244 out:
245 	free_pages_exact(comm_buf, COMM_BUF_SIZE(payload_size));
246 	return ret;
247 }
248 
get_property_int(u16 * name,size_t name_size,const efi_guid_t * vendor,struct var_check_property * var_property)249 static efi_status_t get_property_int(u16 *name, size_t name_size,
250 				     const efi_guid_t *vendor,
251 				     struct var_check_property *var_property)
252 {
253 	struct smm_variable_var_check_property *smm_property;
254 	size_t payload_size;
255 	u8 *comm_buf = NULL;
256 	efi_status_t ret;
257 
258 	memset(var_property, 0, sizeof(*var_property));
259 	payload_size = sizeof(*smm_property) + name_size;
260 	if (payload_size > max_payload_size)
261 		return EFI_INVALID_PARAMETER;
262 
263 	smm_property = setup_mm_hdr(
264 		&comm_buf, payload_size,
265 		SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET, &ret);
266 	if (!smm_property)
267 		return EFI_OUT_OF_RESOURCES;
268 
269 	memcpy(&smm_property->guid, vendor, sizeof(smm_property->guid));
270 	smm_property->name_size = name_size;
271 	memcpy(smm_property->name, name, name_size);
272 
273 	ret = mm_communicate(comm_buf, payload_size);
274 	/*
275 	 * Currently only R/O property is supported in StMM.
276 	 * Variables that are not set to R/O will not set the property in StMM
277 	 * and the call will return EFI_NOT_FOUND. We are setting the
278 	 * properties to 0x0 so checking against that is enough for the
279 	 * EFI_NOT_FOUND case.
280 	 */
281 	if (ret == EFI_NOT_FOUND)
282 		ret = EFI_SUCCESS;
283 	if (ret != EFI_SUCCESS)
284 		goto out;
285 	memcpy(var_property, &smm_property->property, sizeof(*var_property));
286 
287 out:
288 	free_pages_exact(comm_buf, COMM_BUF_SIZE(payload_size));
289 	return ret;
290 }
291 
tee_get_variable(u16 * name,efi_guid_t * vendor,u32 * attributes,unsigned long * data_size,void * data)292 static efi_status_t tee_get_variable(u16 *name, efi_guid_t *vendor,
293 				     u32 *attributes, unsigned long *data_size,
294 				     void *data)
295 {
296 	struct var_check_property var_property;
297 	struct smm_variable_access *var_acc;
298 	size_t payload_size;
299 	size_t name_size;
300 	size_t tmp_dsize;
301 	u8 *comm_buf = NULL;
302 	efi_status_t ret;
303 
304 	if (!name || !vendor || !data_size)
305 		return EFI_INVALID_PARAMETER;
306 
307 	name_size = (ucs2_strnlen(name, EFI_VAR_NAME_LEN) + 1) * sizeof(u16);
308 	if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE)
309 		return EFI_INVALID_PARAMETER;
310 
311 	/* Trim output buffer size */
312 	tmp_dsize = *data_size;
313 	if (name_size + tmp_dsize >
314 	    max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
315 		tmp_dsize = max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE -
316 			    name_size;
317 	}
318 
319 	payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
320 	var_acc = setup_mm_hdr(&comm_buf, payload_size,
321 			       SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
322 	if (!var_acc)
323 		return EFI_OUT_OF_RESOURCES;
324 
325 	/* Fill in contents */
326 	memcpy(&var_acc->guid, vendor, sizeof(var_acc->guid));
327 	var_acc->data_size = tmp_dsize;
328 	var_acc->name_size = name_size;
329 	var_acc->attr = attributes ? *attributes : 0;
330 	memcpy(var_acc->name, name, name_size);
331 
332 	ret = mm_communicate(comm_buf, payload_size);
333 	if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL)
334 		/* Update with reported data size for trimmed case */
335 		*data_size = var_acc->data_size;
336 	if (ret != EFI_SUCCESS)
337 		goto out;
338 
339 	ret = get_property_int(name, name_size, vendor, &var_property);
340 	if (ret != EFI_SUCCESS)
341 		goto out;
342 
343 	if (attributes)
344 		*attributes = var_acc->attr;
345 
346 	if (!data) {
347 		ret = EFI_INVALID_PARAMETER;
348 		goto out;
349 	}
350 	memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
351 	       var_acc->data_size);
352 out:
353 	free_pages_exact(comm_buf, COMM_BUF_SIZE(payload_size));
354 	return ret;
355 }
356 
tee_get_next_variable(unsigned long * name_size,efi_char16_t * name,efi_guid_t * guid)357 static efi_status_t tee_get_next_variable(unsigned long *name_size,
358 					  efi_char16_t *name, efi_guid_t *guid)
359 {
360 	struct smm_variable_getnext *var_getnext;
361 	size_t payload_size;
362 	size_t out_name_size;
363 	size_t in_name_size;
364 	u8 *comm_buf = NULL;
365 	efi_status_t ret;
366 
367 	if (!name_size || !name || !guid)
368 		return EFI_INVALID_PARAMETER;
369 
370 	out_name_size = *name_size;
371 	in_name_size = (ucs2_strnlen(name, EFI_VAR_NAME_LEN) + 1) * sizeof(u16);
372 
373 	if (out_name_size < in_name_size)
374 		return EFI_INVALID_PARAMETER;
375 
376 	if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
377 		return EFI_INVALID_PARAMETER;
378 
379 	/* Trim output buffer size */
380 	if (out_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
381 		out_name_size =
382 			max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE;
383 
384 	payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
385 	var_getnext = setup_mm_hdr(&comm_buf, payload_size,
386 				   SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
387 				   &ret);
388 	if (!var_getnext)
389 		return EFI_OUT_OF_RESOURCES;
390 
391 	/* Fill in contents */
392 	memcpy(&var_getnext->guid, guid, sizeof(var_getnext->guid));
393 	var_getnext->name_size = out_name_size;
394 	memcpy(var_getnext->name, name, in_name_size);
395 	memset((u8 *)var_getnext->name + in_name_size, 0x0,
396 	       out_name_size - in_name_size);
397 
398 	ret = mm_communicate(comm_buf, payload_size);
399 	if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
400 		/* Update with reported data size for trimmed case */
401 		*name_size = var_getnext->name_size;
402 	}
403 	if (ret != EFI_SUCCESS)
404 		goto out;
405 
406 	memcpy(guid, &var_getnext->guid, sizeof(*guid));
407 	memcpy(name, var_getnext->name, var_getnext->name_size);
408 
409 out:
410 	free_pages_exact(comm_buf, COMM_BUF_SIZE(payload_size));
411 	return ret;
412 }
413 
tee_set_variable(efi_char16_t * name,efi_guid_t * vendor,u32 attributes,unsigned long data_size,void * data)414 static efi_status_t tee_set_variable(efi_char16_t *name, efi_guid_t *vendor,
415 				     u32 attributes, unsigned long data_size,
416 				     void *data)
417 {
418 	efi_status_t ret;
419 	struct var_check_property var_property;
420 	struct smm_variable_access *var_acc;
421 	size_t payload_size;
422 	size_t name_size;
423 	u8 *comm_buf = NULL;
424 
425 	if (!name || name[0] == 0 || !vendor)
426 		return EFI_INVALID_PARAMETER;
427 
428 	if (data_size > 0 && !data)
429 		return EFI_INVALID_PARAMETER;
430 
431 	/* Check payload size */
432 	name_size = (ucs2_strnlen(name, EFI_VAR_NAME_LEN) + 1) * sizeof(u16);
433 	payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
434 	if (payload_size > max_payload_size)
435 		return EFI_INVALID_PARAMETER;
436 
437 	/*
438 	 * Allocate the buffer early, before switching to RW (if needed)
439 	 * so we won't need to account for any failures in reading/setting
440 	 * the properties, if the allocation fails
441 	 */
442 	var_acc = setup_mm_hdr(&comm_buf, payload_size,
443 			       SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
444 	if (!var_acc)
445 		return EFI_OUT_OF_RESOURCES;
446 
447 	/*
448 	 * The API has the ability to override RO flags. If no RO check was
449 	 * requested switch the variable to RW for the duration of this call
450 	 */
451 	ret = get_property_int(name, name_size, vendor, &var_property);
452 	if (ret != EFI_SUCCESS) {
453 		dev_err(pvt_data.dev, "Getting variable property failed\n");
454 		goto out;
455 	}
456 
457 	if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) {
458 		ret = EFI_WRITE_PROTECTED;
459 		goto out;
460 	}
461 
462 	/* Fill in contents */
463 	memcpy(&var_acc->guid, vendor, sizeof(var_acc->guid));
464 	var_acc->data_size = data_size;
465 	var_acc->name_size = name_size;
466 	var_acc->attr = attributes;
467 	memcpy(var_acc->name, name, name_size);
468 	memcpy((u8 *)var_acc->name + name_size, data, data_size);
469 
470 	ret = mm_communicate(comm_buf, payload_size);
471 	dev_dbg(pvt_data.dev, "Set Variable %s %d %lx\n", __FILE__, __LINE__, ret);
472 out:
473 	free_pages_exact(comm_buf, COMM_BUF_SIZE(payload_size));
474 	return ret;
475 }
476 
tee_set_variable_nonblocking(efi_char16_t * name,efi_guid_t * vendor,u32 attributes,unsigned long data_size,void * data)477 static efi_status_t tee_set_variable_nonblocking(efi_char16_t *name,
478 						 efi_guid_t *vendor,
479 						 u32 attributes,
480 						 unsigned long data_size,
481 						 void *data)
482 {
483 	return EFI_UNSUPPORTED;
484 }
485 
tee_query_variable_info(u32 attributes,u64 * max_variable_storage_size,u64 * remain_variable_storage_size,u64 * max_variable_size)486 static efi_status_t tee_query_variable_info(u32 attributes,
487 					    u64 *max_variable_storage_size,
488 					    u64 *remain_variable_storage_size,
489 					    u64 *max_variable_size)
490 {
491 	struct smm_variable_query_info *mm_query_info;
492 	size_t payload_size;
493 	efi_status_t ret;
494 	u8 *comm_buf;
495 
496 	payload_size = sizeof(*mm_query_info);
497 	mm_query_info = setup_mm_hdr(&comm_buf, payload_size,
498 				SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
499 				&ret);
500 	if (!mm_query_info)
501 		return EFI_OUT_OF_RESOURCES;
502 
503 	mm_query_info->attr = attributes;
504 	ret = mm_communicate(comm_buf, payload_size);
505 	if (ret != EFI_SUCCESS)
506 		goto out;
507 	*max_variable_storage_size = mm_query_info->max_variable_storage;
508 	*remain_variable_storage_size =
509 		mm_query_info->remaining_variable_storage;
510 	*max_variable_size = mm_query_info->max_variable_size;
511 
512 out:
513 	free_pages_exact(comm_buf, COMM_BUF_SIZE(payload_size));
514 	return ret;
515 }
516 
tee_stmm_efi_close_context(void * data)517 static void tee_stmm_efi_close_context(void *data)
518 {
519 	tee_client_close_context(pvt_data.ctx);
520 }
521 
tee_stmm_efi_close_session(void * data)522 static void tee_stmm_efi_close_session(void *data)
523 {
524 	tee_client_close_session(pvt_data.ctx, pvt_data.session);
525 }
526 
tee_stmm_restore_efivars_generic_ops(void)527 static void tee_stmm_restore_efivars_generic_ops(void)
528 {
529 	efivars_unregister(&tee_efivars);
530 	efivars_generic_ops_register();
531 }
532 
tee_stmm_efi_probe(struct device * dev)533 static int tee_stmm_efi_probe(struct device *dev)
534 {
535 	struct tee_ioctl_open_session_arg sess_arg;
536 	efi_status_t ret;
537 	int rc;
538 
539 	pvt_data.ctx = tee_client_open_context(NULL, tee_ctx_match, NULL, NULL);
540 	if (IS_ERR(pvt_data.ctx))
541 		return -ENODEV;
542 
543 	rc = devm_add_action_or_reset(dev, tee_stmm_efi_close_context, NULL);
544 	if (rc)
545 		return rc;
546 
547 	/* Open session with StMM PTA */
548 	memset(&sess_arg, 0, sizeof(sess_arg));
549 	export_uuid(sess_arg.uuid, &tee_stmm_efi_id_table[0].uuid);
550 	rc = tee_client_open_session(pvt_data.ctx, &sess_arg, NULL);
551 	if ((rc < 0) || (sess_arg.ret != 0)) {
552 		dev_err(dev, "tee_client_open_session failed, err: %x\n",
553 			sess_arg.ret);
554 		return -EINVAL;
555 	}
556 	pvt_data.session = sess_arg.session;
557 	pvt_data.dev = dev;
558 	rc = devm_add_action_or_reset(dev, tee_stmm_efi_close_session, NULL);
559 	if (rc)
560 		return rc;
561 
562 	ret = get_max_payload(&max_payload_size);
563 	if (ret != EFI_SUCCESS)
564 		return -EIO;
565 
566 	max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
567 			  MM_VARIABLE_COMMUNICATE_SIZE +
568 			  max_payload_size;
569 
570 	tee_efivar_ops.get_variable		= tee_get_variable;
571 	tee_efivar_ops.get_next_variable	= tee_get_next_variable;
572 	tee_efivar_ops.set_variable		= tee_set_variable;
573 	tee_efivar_ops.set_variable_nonblocking	= tee_set_variable_nonblocking;
574 	tee_efivar_ops.query_variable_store	= efi_query_variable_store;
575 	tee_efivar_ops.query_variable_info	= tee_query_variable_info;
576 
577 	efivars_generic_ops_unregister();
578 	pr_info("Using TEE-based EFI runtime variable services\n");
579 	efivars_register(&tee_efivars, &tee_efivar_ops);
580 
581 	return 0;
582 }
583 
tee_stmm_efi_remove(struct device * dev)584 static int tee_stmm_efi_remove(struct device *dev)
585 {
586 	tee_stmm_restore_efivars_generic_ops();
587 
588 	return 0;
589 }
590 
591 MODULE_DEVICE_TABLE(tee, tee_stmm_efi_id_table);
592 
593 static struct tee_client_driver tee_stmm_efi_driver = {
594 	.id_table	= tee_stmm_efi_id_table,
595 	.driver		= {
596 		.name		= "tee-stmm-efi",
597 		.bus		= &tee_bus_type,
598 		.probe		= tee_stmm_efi_probe,
599 		.remove		= tee_stmm_efi_remove,
600 	},
601 };
602 
tee_stmm_efi_mod_init(void)603 static int __init tee_stmm_efi_mod_init(void)
604 {
605 	return driver_register(&tee_stmm_efi_driver.driver);
606 }
607 
tee_stmm_efi_mod_exit(void)608 static void __exit tee_stmm_efi_mod_exit(void)
609 {
610 	driver_unregister(&tee_stmm_efi_driver.driver);
611 }
612 
613 module_init(tee_stmm_efi_mod_init);
614 module_exit(tee_stmm_efi_mod_exit);
615 
616 MODULE_LICENSE("GPL");
617 MODULE_AUTHOR("Ilias Apalodimas <ilias.apalodimas@linaro.org>");
618 MODULE_AUTHOR("Masahisa Kojima <masahisa.kojima@linaro.org>");
619 MODULE_DESCRIPTION("TEE based EFI runtime variable service driver");
620