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