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 */ 2754e04708SPascal Brand #include <kernel/tee_common.h> 2854e04708SPascal Brand #include <kernel/chip_services.h> 2954e04708SPascal Brand #include <kernel/tee_misc.h> 3054e04708SPascal Brand #include <mm/core_memprot.h> 3154e04708SPascal Brand #include <kernel/tee_common_otp.h> 32*4de4bebcSJens Wiklander #include <trace.h> 3354e04708SPascal Brand 3454e04708SPascal Brand static uint8_t tee_b2hs_add_base(uint8_t in) 3554e04708SPascal Brand { 3654e04708SPascal Brand if (in > 9) 3754e04708SPascal Brand return in + 55; 3854e04708SPascal Brand else 3954e04708SPascal Brand return in + 48; 4054e04708SPascal Brand } 4154e04708SPascal Brand 4254e04708SPascal Brand static int tee_hs2b_rem_base(uint8_t in, uint8_t *out) 4354e04708SPascal Brand { 4454e04708SPascal Brand if (in < 48 || in > 70 || (in > 57 && in < 65)) 4554e04708SPascal Brand return -1; 4654e04708SPascal Brand 4754e04708SPascal Brand if (in < 58) 4854e04708SPascal Brand *out = in - 48; 4954e04708SPascal Brand else 5054e04708SPascal Brand *out = in - 55; 5154e04708SPascal Brand 5254e04708SPascal Brand return 0; 5354e04708SPascal Brand } 5454e04708SPascal Brand 5554e04708SPascal Brand uint32_t tee_b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen) 5654e04708SPascal Brand { 5754e04708SPascal Brand uint32_t i = 0; 5854e04708SPascal Brand 5954e04708SPascal Brand if (blen * 2 + 1 > hslen) 6054e04708SPascal Brand return 0; 6154e04708SPascal Brand 6254e04708SPascal Brand for (; i < blen; i++) { 6354e04708SPascal Brand hs[i * 2 + 1] = tee_b2hs_add_base(b[i] & 0xf); 6454e04708SPascal Brand hs[i * 2] = tee_b2hs_add_base(b[i] >> 4); 6554e04708SPascal Brand } 6654e04708SPascal Brand hs[blen * 2] = 0; 6754e04708SPascal Brand 6854e04708SPascal Brand return blen * 2; 6954e04708SPascal Brand } 7054e04708SPascal Brand 7154e04708SPascal Brand uint32_t tee_hs2b(uint8_t *hs, uint8_t *b, uint32_t hslen, uint32_t blen) 7254e04708SPascal Brand { 7354e04708SPascal Brand uint32_t i = 0; 7454e04708SPascal Brand uint32_t len = TEE_HS2B_BBUF_SIZE(hslen); 7554e04708SPascal Brand uint8_t hi; 7654e04708SPascal Brand uint8_t lo; 7754e04708SPascal Brand 7854e04708SPascal Brand if (len > blen) 7954e04708SPascal Brand return 0; 8054e04708SPascal Brand 8154e04708SPascal Brand for (; i < len; i++) { 8254e04708SPascal Brand if (tee_hs2b_rem_base(hs[i * 2], &hi)) 8354e04708SPascal Brand return 0; 8454e04708SPascal Brand if (tee_hs2b_rem_base(hs[i * 2 + 1], &lo)) 8554e04708SPascal Brand return 0; 8654e04708SPascal Brand b[i] = (hi << 4) + lo; 8754e04708SPascal Brand } 8854e04708SPascal Brand 8954e04708SPascal Brand return len; 9054e04708SPascal Brand } 91106d8aa6SPascal Brand 92106d8aa6SPascal Brand static bool is_valid_conf_and_notnull_size( 93106d8aa6SPascal Brand vaddr_t b, size_t bl, vaddr_t a, size_t al) 94106d8aa6SPascal Brand { 95106d8aa6SPascal Brand /* invalid config return false */ 96106d8aa6SPascal Brand if ((b + bl < b) || (a + al < a)) 97106d8aa6SPascal Brand return false; 98106d8aa6SPascal Brand /* null sized areas are never inside / outside / overlap */ 99106d8aa6SPascal Brand if (!bl || !al) 100106d8aa6SPascal Brand return false; 101106d8aa6SPascal Brand return true; 102106d8aa6SPascal Brand } 103106d8aa6SPascal Brand 104106d8aa6SPascal Brand /* Returns true when buffer 'b' is fully contained in area 'a' */ 105106d8aa6SPascal Brand bool _core_is_buffer_inside(vaddr_t b, size_t bl, vaddr_t a, size_t al) 106106d8aa6SPascal Brand { 107106d8aa6SPascal Brand /* invalid config or "null size" return false */ 108106d8aa6SPascal Brand if (!is_valid_conf_and_notnull_size(b, bl, a, al)) 109106d8aa6SPascal Brand return false; 110106d8aa6SPascal Brand 111106d8aa6SPascal Brand if ((b >= a) && (b + bl <= a + al)) 112106d8aa6SPascal Brand return true; 113106d8aa6SPascal Brand return false; 114106d8aa6SPascal Brand } 115106d8aa6SPascal Brand 116106d8aa6SPascal Brand /* Returns true when buffer 'b' is fully contained in area 'a' */ 117106d8aa6SPascal Brand bool _core_is_buffer_outside(vaddr_t b, size_t bl, vaddr_t a, size_t al) 118106d8aa6SPascal Brand { 119106d8aa6SPascal Brand /* invalid config or "null size" return false */ 120106d8aa6SPascal Brand if (!is_valid_conf_and_notnull_size(b, bl, a, al)) 121106d8aa6SPascal Brand return false; 122106d8aa6SPascal Brand 123106d8aa6SPascal Brand if ((b + bl <= a) || (b >= a + al)) 124106d8aa6SPascal Brand return true; 125106d8aa6SPascal Brand return false; 126106d8aa6SPascal Brand } 127106d8aa6SPascal Brand 128106d8aa6SPascal Brand /* Returns true when buffer 'b' intersects area 'a' */ 129106d8aa6SPascal Brand bool _core_is_buffer_intersect(vaddr_t b, size_t bl, vaddr_t a, size_t al) 130106d8aa6SPascal Brand { 131106d8aa6SPascal Brand /* invalid config or "null size" return false */ 132106d8aa6SPascal Brand if (!is_valid_conf_and_notnull_size(b, bl, a, al)) 133106d8aa6SPascal Brand return false; 134106d8aa6SPascal Brand 135106d8aa6SPascal Brand if ((b + bl <= a) || (b >= a + al)) 136106d8aa6SPascal Brand return false; 137106d8aa6SPascal Brand return true; 138106d8aa6SPascal Brand } 139