xref: /optee_os/core/tee/tee_time_generic.c (revision bc420748bfc44a9e09000a3966fc59e9e0219df4)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <string.h>
29 #include <stdlib.h>
30 #include <trace.h>
31 #include <utee_defines.h>
32 #include <kernel/tee_time.h>
33 
34 struct tee_ta_time_offs {
35 	TEE_UUID uuid;
36 	TEE_Time offs;
37 	bool positive;
38 };
39 
40 static struct tee_ta_time_offs *tee_time_offs;
41 static size_t tee_time_num_offs;
42 
43 static TEE_Result tee_time_ta_get_offs(const TEE_UUID *uuid,
44 				       const TEE_Time **offs, bool *positive)
45 {
46 	size_t n;
47 
48 	for (n = 0; n < tee_time_num_offs; n++) {
49 		if (memcmp(uuid, &tee_time_offs[n].uuid, sizeof(TEE_UUID))
50 				== 0) {
51 			*offs = &tee_time_offs[n].offs;
52 			*positive = tee_time_offs[n].positive;
53 			return TEE_SUCCESS;
54 		}
55 	}
56 	return TEE_ERROR_TIME_NOT_SET;
57 }
58 
59 static TEE_Result tee_time_ta_set_offs(const TEE_UUID *uuid,
60 				       const TEE_Time *offs, bool positive)
61 {
62 	size_t n;
63 	struct tee_ta_time_offs *o;
64 
65 	for (n = 0; n < tee_time_num_offs; n++) {
66 		if (memcmp(uuid, &tee_time_offs[n].uuid, sizeof(TEE_UUID))
67 				== 0) {
68 			tee_time_offs[n].offs = *offs;
69 			tee_time_offs[n].positive = positive;
70 			return TEE_SUCCESS;
71 		}
72 	}
73 
74 	n = tee_time_num_offs + 1;
75 	o = malloc(n * sizeof(struct tee_ta_time_offs));
76 	if (o == NULL)
77 		return TEE_ERROR_OUT_OF_MEMORY;
78 	memcpy(o, tee_time_offs,
79 	       tee_time_num_offs * sizeof(struct tee_ta_time_offs));
80 	free(tee_time_offs);
81 	tee_time_offs = o;
82 	tee_time_offs[tee_time_num_offs].uuid = *uuid;
83 	tee_time_offs[tee_time_num_offs].offs = *offs;
84 	tee_time_offs[tee_time_num_offs].positive = positive;
85 	tee_time_num_offs = n;
86 	return TEE_SUCCESS;
87 }
88 
89 TEE_Result tee_time_get_ta_time(const TEE_UUID *uuid, TEE_Time *time)
90 {
91 	TEE_Result res;
92 	const TEE_Time *offs;
93 	bool positive;
94 	TEE_Time t;
95 	TEE_Time t2;
96 
97 	res = tee_time_ta_get_offs(uuid, &offs, &positive);
98 	if (res != TEE_SUCCESS)
99 		return res;
100 
101 	res = tee_time_get_sys_time(&t);
102 	if (res != TEE_SUCCESS)
103 		return res;
104 
105 	if (positive) {
106 		TEE_TIME_ADD(t, *offs, t2);
107 
108 		/* Detect wrapping, the wrapped time should be returned. */
109 		if (TEE_TIME_LT(t2, t))
110 			res = TEE_ERROR_OVERFLOW;
111 	} else {
112 		TEE_TIME_SUB(t, *offs, t2);
113 
114 		/* Detect wrapping, the wrapped time should be returned. */
115 		if (TEE_TIME_LE(t, t2))
116 			res = TEE_ERROR_OVERFLOW;
117 	}
118 	*time = t2;
119 
120 	return res;
121 }
122 
123 TEE_Result tee_time_set_ta_time(const TEE_UUID *uuid, const TEE_Time *time)
124 {
125 	TEE_Result res;
126 	TEE_Time offs;
127 	TEE_Time t;
128 
129 	/* Check that time is normalized. */
130 	if (time->millis >= TEE_TIME_MILLIS_BASE)
131 		return TEE_ERROR_BAD_PARAMETERS;
132 
133 	res = tee_time_get_sys_time(&t);
134 	if (res != TEE_SUCCESS)
135 		return res;
136 
137 	if (TEE_TIME_LT(t, *time)) {
138 		TEE_TIME_SUB(*time, t, offs);
139 		return tee_time_ta_set_offs(uuid, &offs, true);
140 	} else {
141 		TEE_TIME_SUB(t, *time, offs);
142 		return tee_time_ta_set_offs(uuid, &offs, false);
143 	}
144 }
145