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