xref: /optee_os/core/tee/tee_obj.c (revision 61ea19fdc0549f4a2e07c91c7a79f1dc2dae13b6)
1b0104773SPascal Brand /*
2b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3b0104773SPascal Brand  * All rights reserved.
4b0104773SPascal Brand  *
5b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7b0104773SPascal Brand  *
8b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10b0104773SPascal Brand  *
11b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13b0104773SPascal Brand  * and/or other materials provided with the distribution.
14b0104773SPascal Brand  *
15b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26b0104773SPascal Brand  */
27b0104773SPascal Brand 
28b0104773SPascal Brand #include <tee/tee_obj.h>
29b0104773SPascal Brand 
30b0104773SPascal Brand #include <stdlib.h>
31b0104773SPascal Brand #include <tee_api_defines.h>
32b0104773SPascal Brand #include <mm/tee_mmu.h>
33b0104773SPascal Brand #include <tee/tee_fs.h>
34b0104773SPascal Brand #include <tee/tee_pobj.h>
354de4bebcSJens Wiklander #include <trace.h>
36b0104773SPascal Brand 
37b0104773SPascal Brand void tee_obj_add(struct tee_ta_ctx *ctx, struct tee_obj *o)
38b0104773SPascal Brand {
39b0104773SPascal Brand 	TAILQ_INSERT_TAIL(&ctx->objects, o, link);
40b0104773SPascal Brand }
41b0104773SPascal Brand 
42b0104773SPascal Brand TEE_Result tee_obj_get(struct tee_ta_ctx *ctx, uint32_t obj_id,
43b0104773SPascal Brand 		       struct tee_obj **obj)
44b0104773SPascal Brand {
45b0104773SPascal Brand 	struct tee_obj *o;
46b0104773SPascal Brand 
47b0104773SPascal Brand 	TAILQ_FOREACH(o, &ctx->objects, link) {
48*61ea19fdSJens Wiklander 		if (obj_id == (vaddr_t)o) {
49b0104773SPascal Brand 			*obj = o;
50b0104773SPascal Brand 			return TEE_SUCCESS;
51b0104773SPascal Brand 		}
52b0104773SPascal Brand 	}
53b0104773SPascal Brand 	return TEE_ERROR_BAD_PARAMETERS;
54b0104773SPascal Brand }
55b0104773SPascal Brand 
56b0104773SPascal Brand void tee_obj_close(struct tee_ta_ctx *ctx, struct tee_obj *o)
57b0104773SPascal Brand {
58b0104773SPascal Brand 	TAILQ_REMOVE(&ctx->objects, o, link);
59b0104773SPascal Brand 
60c0346845SJens Wiklander 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) && o->fd >= 0) {
611fd927ebSJames Kung 		tee_file_ops.close(o->fd);
62b0104773SPascal Brand 		tee_pobj_release(o->pobj);
63b0104773SPascal Brand 	}
64b0104773SPascal Brand 
6551835057SJerome Forissier 	if (o->cleanup)
6651835057SJerome Forissier 		o->cleanup(o->data, true);
67b0104773SPascal Brand 	free(o->data);
68b0104773SPascal Brand 	free(o);
69b0104773SPascal Brand }
70b0104773SPascal Brand 
71b0104773SPascal Brand void tee_obj_close_all(struct tee_ta_ctx *ctx)
72b0104773SPascal Brand {
73b0104773SPascal Brand 	struct tee_obj_head *objects = &ctx->objects;
74b0104773SPascal Brand 
75b0104773SPascal Brand 	while (!TAILQ_EMPTY(objects))
76b0104773SPascal Brand 		tee_obj_close(ctx, TAILQ_FIRST(objects));
77b0104773SPascal Brand }
78