xref: /optee_os/core/kernel/tee_misc.c (revision 5580c17c51ad1e391e7f8a3c5130d0034eba8bb4)
154e04708SPascal Brand /*
254e04708SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
354e04708SPascal Brand  * All rights reserved.
454e04708SPascal Brand  *
554e04708SPascal Brand  * Redistribution and use in source and binary forms, with or without
654e04708SPascal Brand  * modification, are permitted provided that the following conditions are met:
754e04708SPascal Brand  *
854e04708SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
954e04708SPascal Brand  * this list of conditions and the following disclaimer.
1054e04708SPascal Brand  *
1154e04708SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
1254e04708SPascal Brand  * this list of conditions and the following disclaimer in the documentation
1354e04708SPascal Brand  * and/or other materials provided with the distribution.
1454e04708SPascal Brand  *
1554e04708SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1654e04708SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1754e04708SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1854e04708SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1954e04708SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2054e04708SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2154e04708SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2254e04708SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2354e04708SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2454e04708SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2554e04708SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
2654e04708SPascal Brand  */
27*5580c17cSEtienne Carriere #include <stdio.h>
2854e04708SPascal Brand #include <kernel/tee_common.h>
2954e04708SPascal Brand #include <kernel/chip_services.h>
3054e04708SPascal Brand #include <kernel/tee_misc.h>
3154e04708SPascal Brand #include <mm/core_memprot.h>
3254e04708SPascal Brand #include <kernel/tee_common_otp.h>
334de4bebcSJens Wiklander #include <trace.h>
3454e04708SPascal Brand 
3554e04708SPascal Brand static uint8_t tee_b2hs_add_base(uint8_t in)
3654e04708SPascal Brand {
3754e04708SPascal Brand 	if (in > 9)
3854e04708SPascal Brand 		return in + 55;
3954e04708SPascal Brand 	else
4054e04708SPascal Brand 		return in + 48;
4154e04708SPascal Brand }
4254e04708SPascal Brand 
4354e04708SPascal Brand static int tee_hs2b_rem_base(uint8_t in, uint8_t *out)
4454e04708SPascal Brand {
4554e04708SPascal Brand 	if (in < 48 || in > 70 || (in > 57 && in < 65))
4654e04708SPascal Brand 		return -1;
4754e04708SPascal Brand 
4854e04708SPascal Brand 	if (in < 58)
4954e04708SPascal Brand 		*out = in - 48;
5054e04708SPascal Brand 	else
5154e04708SPascal Brand 		*out = in - 55;
5254e04708SPascal Brand 
5354e04708SPascal Brand 	return 0;
5454e04708SPascal Brand }
5554e04708SPascal Brand 
5654e04708SPascal Brand uint32_t tee_b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen)
5754e04708SPascal Brand {
5854e04708SPascal Brand 	uint32_t i = 0;
5954e04708SPascal Brand 
6054e04708SPascal Brand 	if (blen * 2 + 1 > hslen)
6154e04708SPascal Brand 		return 0;
6254e04708SPascal Brand 
6354e04708SPascal Brand 	for (; i < blen; i++) {
6454e04708SPascal Brand 		hs[i * 2 + 1] = tee_b2hs_add_base(b[i] & 0xf);
6554e04708SPascal Brand 		hs[i * 2] = tee_b2hs_add_base(b[i] >> 4);
6654e04708SPascal Brand 	}
6754e04708SPascal Brand 	hs[blen * 2] = 0;
6854e04708SPascal Brand 
6954e04708SPascal Brand 	return blen * 2;
7054e04708SPascal Brand }
7154e04708SPascal Brand 
7254e04708SPascal Brand uint32_t tee_hs2b(uint8_t *hs, uint8_t *b, uint32_t hslen, uint32_t blen)
7354e04708SPascal Brand {
7454e04708SPascal Brand 	uint32_t i = 0;
7554e04708SPascal Brand 	uint32_t len = TEE_HS2B_BBUF_SIZE(hslen);
7654e04708SPascal Brand 	uint8_t hi;
7754e04708SPascal Brand 	uint8_t lo;
7854e04708SPascal Brand 
7954e04708SPascal Brand 	if (len > blen)
8054e04708SPascal Brand 		return 0;
8154e04708SPascal Brand 
8254e04708SPascal Brand 	for (; i < len; i++) {
8354e04708SPascal Brand 		if (tee_hs2b_rem_base(hs[i * 2], &hi))
8454e04708SPascal Brand 			return 0;
8554e04708SPascal Brand 		if (tee_hs2b_rem_base(hs[i * 2 + 1], &lo))
8654e04708SPascal Brand 			return 0;
8754e04708SPascal Brand 		b[i] = (hi << 4) + lo;
8854e04708SPascal Brand 	}
8954e04708SPascal Brand 
9054e04708SPascal Brand 	return len;
9154e04708SPascal Brand }
92106d8aa6SPascal Brand 
93106d8aa6SPascal Brand static bool is_valid_conf_and_notnull_size(
94106d8aa6SPascal Brand 		vaddr_t b, size_t bl, vaddr_t a, size_t al)
95106d8aa6SPascal Brand {
96106d8aa6SPascal Brand 	/* invalid config return false */
97106d8aa6SPascal Brand 	if ((b + bl < b) || (a + al < a))
98106d8aa6SPascal Brand 		return false;
99106d8aa6SPascal Brand 	/* null sized areas are never inside / outside / overlap */
100106d8aa6SPascal Brand 	if (!bl || !al)
101106d8aa6SPascal Brand 		return false;
102106d8aa6SPascal Brand 	return true;
103106d8aa6SPascal Brand }
104106d8aa6SPascal Brand 
105106d8aa6SPascal Brand /* Returns true when buffer 'b' is fully contained in area 'a' */
106106d8aa6SPascal Brand bool _core_is_buffer_inside(vaddr_t b, size_t bl, vaddr_t a, size_t al)
107106d8aa6SPascal Brand {
108106d8aa6SPascal Brand 	/* invalid config or "null size" return false */
109106d8aa6SPascal Brand 	if (!is_valid_conf_and_notnull_size(b, bl, a, al))
110106d8aa6SPascal Brand 		return false;
111106d8aa6SPascal Brand 
112106d8aa6SPascal Brand 	if ((b >= a) && (b + bl <= a + al))
113106d8aa6SPascal Brand 		return true;
114106d8aa6SPascal Brand 	return false;
115106d8aa6SPascal Brand }
116106d8aa6SPascal Brand 
117106d8aa6SPascal Brand /* Returns true when buffer 'b' is fully contained in area 'a' */
118106d8aa6SPascal Brand bool _core_is_buffer_outside(vaddr_t b, size_t bl, vaddr_t a, size_t al)
119106d8aa6SPascal Brand {
120106d8aa6SPascal Brand 	/* invalid config or "null size" return false */
121106d8aa6SPascal Brand 	if (!is_valid_conf_and_notnull_size(b, bl, a, al))
122106d8aa6SPascal Brand 		return false;
123106d8aa6SPascal Brand 
124106d8aa6SPascal Brand 	if ((b + bl <= a) || (b >= a + al))
125106d8aa6SPascal Brand 		return true;
126106d8aa6SPascal Brand 	return false;
127106d8aa6SPascal Brand }
128106d8aa6SPascal Brand 
129106d8aa6SPascal Brand /* Returns true when buffer 'b' intersects area 'a' */
130106d8aa6SPascal Brand bool _core_is_buffer_intersect(vaddr_t b, size_t bl, vaddr_t a, size_t al)
131106d8aa6SPascal Brand {
132106d8aa6SPascal Brand 	/* invalid config or "null size" return false */
133106d8aa6SPascal Brand 	if (!is_valid_conf_and_notnull_size(b, bl, a, al))
134106d8aa6SPascal Brand 		return false;
135106d8aa6SPascal Brand 
136106d8aa6SPascal Brand 	if ((b + bl <= a) || (b >= a + al))
137106d8aa6SPascal Brand 		return false;
138106d8aa6SPascal Brand 	return true;
139106d8aa6SPascal Brand }
140*5580c17cSEtienne Carriere 
141*5580c17cSEtienne Carriere int uuid2str(char *dst, TEE_UUID *uuid)
142*5580c17cSEtienne Carriere {
143*5580c17cSEtienne Carriere 	if (dst == NULL)
144*5580c17cSEtienne Carriere 		return 0;
145*5580c17cSEtienne Carriere 
146*5580c17cSEtienne Carriere 	memset(dst, 0, TEE_UUID_STRING_LEN);
147*5580c17cSEtienne Carriere 	return snprintf(dst, TEE_UUID_STRING_LEN,
148*5580c17cSEtienne Carriere 			"%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
149*5580c17cSEtienne Carriere 			(unsigned int)uuid->timeLow,
150*5580c17cSEtienne Carriere 			(unsigned int)uuid->timeMid,
151*5580c17cSEtienne Carriere 			(unsigned int)uuid->timeHiAndVersion,
152*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[0],
153*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[1],
154*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[2],
155*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[3],
156*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[4],
157*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[5],
158*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[6],
159*5580c17cSEtienne Carriere 			(unsigned int)uuid->clockSeqAndNode[7]);
160*5580c17cSEtienne Carriere }
161