xref: /optee_os/core/kernel/tee_misc.c (revision d1d226a5264ce5654695edc656ef759fc48f675f)
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 #include <kernel/tee_common.h>
28 #include <kernel/chip_services.h>
29 #include <kernel/tee_misc.h>
30 #include <mm/core_memprot.h>
31 #include <kernel/tee_common_otp.h>
32 #include <trace.h>
33 
34 static uint8_t tee_b2hs_add_base(uint8_t in)
35 {
36 	if (in > 9)
37 		return in + 55;
38 	else
39 		return in + 48;
40 }
41 
42 static int tee_hs2b_rem_base(uint8_t in, uint8_t *out)
43 {
44 	if (in < 48 || in > 70 || (in > 57 && in < 65))
45 		return -1;
46 
47 	if (in < 58)
48 		*out = in - 48;
49 	else
50 		*out = in - 55;
51 
52 	return 0;
53 }
54 
55 uint32_t tee_b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen)
56 {
57 	uint32_t i = 0;
58 
59 	if (blen * 2 + 1 > hslen)
60 		return 0;
61 
62 	for (; i < blen; i++) {
63 		hs[i * 2 + 1] = tee_b2hs_add_base(b[i] & 0xf);
64 		hs[i * 2] = tee_b2hs_add_base(b[i] >> 4);
65 	}
66 	hs[blen * 2] = 0;
67 
68 	return blen * 2;
69 }
70 
71 uint32_t tee_hs2b(uint8_t *hs, uint8_t *b, uint32_t hslen, uint32_t blen)
72 {
73 	uint32_t i = 0;
74 	uint32_t len = TEE_HS2B_BBUF_SIZE(hslen);
75 	uint8_t hi;
76 	uint8_t lo;
77 
78 	if (len > blen)
79 		return 0;
80 
81 	for (; i < len; i++) {
82 		if (tee_hs2b_rem_base(hs[i * 2], &hi))
83 			return 0;
84 		if (tee_hs2b_rem_base(hs[i * 2 + 1], &lo))
85 			return 0;
86 		b[i] = (hi << 4) + lo;
87 	}
88 
89 	return len;
90 }
91 
92 static bool is_valid_conf_and_notnull_size(
93 		vaddr_t b, size_t bl, vaddr_t a, size_t al)
94 {
95 	/* invalid config return false */
96 	if ((b + bl < b) || (a + al < a))
97 		return false;
98 	/* null sized areas are never inside / outside / overlap */
99 	if (!bl || !al)
100 		return false;
101 	return true;
102 }
103 
104 /* Returns true when buffer 'b' is fully contained in area 'a' */
105 bool _core_is_buffer_inside(vaddr_t b, size_t bl, vaddr_t a, size_t al)
106 {
107 	/* invalid config or "null size" return false */
108 	if (!is_valid_conf_and_notnull_size(b, bl, a, al))
109 		return false;
110 
111 	if ((b >= a) && (b + bl <= a + al))
112 		return true;
113 	return false;
114 }
115 
116 /* Returns true when buffer 'b' is fully contained in area 'a' */
117 bool _core_is_buffer_outside(vaddr_t b, size_t bl, vaddr_t a, size_t al)
118 {
119 	/* invalid config or "null size" return false */
120 	if (!is_valid_conf_and_notnull_size(b, bl, a, al))
121 		return false;
122 
123 	if ((b + bl <= a) || (b >= a + al))
124 		return true;
125 	return false;
126 }
127 
128 /* Returns true when buffer 'b' intersects area 'a' */
129 bool _core_is_buffer_intersect(vaddr_t b, size_t bl, vaddr_t a, size_t al)
130 {
131 	/* invalid config or "null size" return false */
132 	if (!is_valid_conf_and_notnull_size(b, bl, a, al))
133 		return false;
134 
135 	if ((b + bl <= a) || (b >= a + al))
136 		return false;
137 	return true;
138 }
139