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