11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 254e04708SPascal Brand /* 354e04708SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 454e04708SPascal Brand */ 55580c17cSEtienne Carriere #include <stdio.h> 654e04708SPascal Brand #include <kernel/tee_common.h> 754e04708SPascal Brand #include <kernel/chip_services.h> 854e04708SPascal Brand #include <kernel/tee_misc.h> 954e04708SPascal Brand #include <mm/core_memprot.h> 1054e04708SPascal Brand #include <kernel/tee_common_otp.h> 114de4bebcSJens Wiklander #include <trace.h> 1254e04708SPascal Brand 1354e04708SPascal Brand static uint8_t tee_b2hs_add_base(uint8_t in) 1454e04708SPascal Brand { 1554e04708SPascal Brand if (in > 9) 1654e04708SPascal Brand return in + 55; 1754e04708SPascal Brand else 1854e04708SPascal Brand return in + 48; 1954e04708SPascal Brand } 2054e04708SPascal Brand 2154e04708SPascal Brand static int tee_hs2b_rem_base(uint8_t in, uint8_t *out) 2254e04708SPascal Brand { 2354e04708SPascal Brand if (in < 48 || in > 70 || (in > 57 && in < 65)) 2454e04708SPascal Brand return -1; 2554e04708SPascal Brand 2654e04708SPascal Brand if (in < 58) 2754e04708SPascal Brand *out = in - 48; 2854e04708SPascal Brand else 2954e04708SPascal Brand *out = in - 55; 3054e04708SPascal Brand 3154e04708SPascal Brand return 0; 3254e04708SPascal Brand } 3354e04708SPascal Brand 3454e04708SPascal Brand uint32_t tee_b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen) 3554e04708SPascal Brand { 3654e04708SPascal Brand uint32_t i = 0; 3754e04708SPascal Brand 3854e04708SPascal Brand if (blen * 2 + 1 > hslen) 3954e04708SPascal Brand return 0; 4054e04708SPascal Brand 4154e04708SPascal Brand for (; i < blen; i++) { 4254e04708SPascal Brand hs[i * 2 + 1] = tee_b2hs_add_base(b[i] & 0xf); 4354e04708SPascal Brand hs[i * 2] = tee_b2hs_add_base(b[i] >> 4); 4454e04708SPascal Brand } 4554e04708SPascal Brand hs[blen * 2] = 0; 4654e04708SPascal Brand 4754e04708SPascal Brand return blen * 2; 4854e04708SPascal Brand } 4954e04708SPascal Brand 5054e04708SPascal Brand uint32_t tee_hs2b(uint8_t *hs, uint8_t *b, uint32_t hslen, uint32_t blen) 5154e04708SPascal Brand { 5254e04708SPascal Brand uint32_t i = 0; 5354e04708SPascal Brand uint32_t len = TEE_HS2B_BBUF_SIZE(hslen); 5454e04708SPascal Brand uint8_t hi; 5554e04708SPascal Brand uint8_t lo; 5654e04708SPascal Brand 5754e04708SPascal Brand if (len > blen) 5854e04708SPascal Brand return 0; 5954e04708SPascal Brand 6054e04708SPascal Brand for (; i < len; i++) { 6154e04708SPascal Brand if (tee_hs2b_rem_base(hs[i * 2], &hi)) 6254e04708SPascal Brand return 0; 6354e04708SPascal Brand if (tee_hs2b_rem_base(hs[i * 2 + 1], &lo)) 6454e04708SPascal Brand return 0; 6554e04708SPascal Brand b[i] = (hi << 4) + lo; 6654e04708SPascal Brand } 6754e04708SPascal Brand 6854e04708SPascal Brand return len; 6954e04708SPascal Brand } 70106d8aa6SPascal Brand 71*c3682b1cSJens Wiklander static bool is_valid_conf_and_notnull_size(paddr_t b, paddr_size_t bl, 72*c3682b1cSJens Wiklander paddr_t a, paddr_size_t al) 73106d8aa6SPascal Brand { 74106d8aa6SPascal Brand /* invalid config return false */ 75d87d5edeSPeng Fan if ((b - 1 + bl < b) || (a - 1 + al < a)) 76106d8aa6SPascal Brand return false; 77106d8aa6SPascal Brand /* null sized areas are never inside / outside / overlap */ 78106d8aa6SPascal Brand if (!bl || !al) 79106d8aa6SPascal Brand return false; 80106d8aa6SPascal Brand return true; 81106d8aa6SPascal Brand } 82106d8aa6SPascal Brand 83106d8aa6SPascal Brand /* Returns true when buffer 'b' is fully contained in area 'a' */ 84*c3682b1cSJens Wiklander bool core_is_buffer_inside(paddr_t b, paddr_size_t bl, 85*c3682b1cSJens Wiklander paddr_t a, paddr_size_t al) 86106d8aa6SPascal Brand { 87106d8aa6SPascal Brand /* invalid config or "null size" return false */ 88106d8aa6SPascal Brand if (!is_valid_conf_and_notnull_size(b, bl, a, al)) 89106d8aa6SPascal Brand return false; 90106d8aa6SPascal Brand 91d87d5edeSPeng Fan if ((b >= a) && (b - 1 + bl <= a - 1 + al)) 92106d8aa6SPascal Brand return true; 93106d8aa6SPascal Brand return false; 94106d8aa6SPascal Brand } 95106d8aa6SPascal Brand 96106d8aa6SPascal Brand /* Returns true when buffer 'b' is fully contained in area 'a' */ 97*c3682b1cSJens Wiklander bool core_is_buffer_outside(paddr_t b, paddr_size_t bl, 98*c3682b1cSJens Wiklander paddr_t a, paddr_size_t al) 99106d8aa6SPascal Brand { 100106d8aa6SPascal Brand /* invalid config or "null size" return false */ 101106d8aa6SPascal Brand if (!is_valid_conf_and_notnull_size(b, bl, a, al)) 102106d8aa6SPascal Brand return false; 103106d8aa6SPascal Brand 104385000b0SEtienne Carriere if ((b + bl - 1 < a) || (b > a + al - 1)) 105106d8aa6SPascal Brand return true; 106106d8aa6SPascal Brand return false; 107106d8aa6SPascal Brand } 108106d8aa6SPascal Brand 109106d8aa6SPascal Brand /* Returns true when buffer 'b' intersects area 'a' */ 110*c3682b1cSJens Wiklander bool core_is_buffer_intersect(paddr_t b, paddr_size_t bl, 111*c3682b1cSJens Wiklander paddr_t a, paddr_size_t al) 112106d8aa6SPascal Brand { 113106d8aa6SPascal Brand /* invalid config or "null size" return false */ 114106d8aa6SPascal Brand if (!is_valid_conf_and_notnull_size(b, bl, a, al)) 115106d8aa6SPascal Brand return false; 116106d8aa6SPascal Brand 117385000b0SEtienne Carriere if ((b + bl - 1 < a) || (b > a + al - 1)) 118106d8aa6SPascal Brand return false; 119106d8aa6SPascal Brand return true; 120106d8aa6SPascal Brand } 121