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 28 #include <kernel/panic.h> 29 #include <kernel/tee_time.h> 30 #include <string.h> 31 #include <stdlib.h> 32 #include <trace.h> 33 #include <utee_defines.h> 34 35 struct tee_ta_time_offs { 36 TEE_UUID uuid; 37 TEE_Time offs; 38 bool positive; 39 }; 40 41 static struct tee_ta_time_offs *tee_time_offs; 42 static size_t tee_time_num_offs; 43 44 static TEE_Result tee_time_ta_get_offs(const TEE_UUID *uuid, 45 const TEE_Time **offs, bool *positive) 46 { 47 size_t n; 48 49 for (n = 0; n < tee_time_num_offs; n++) { 50 if (memcmp(uuid, &tee_time_offs[n].uuid, sizeof(TEE_UUID)) 51 == 0) { 52 *offs = &tee_time_offs[n].offs; 53 *positive = tee_time_offs[n].positive; 54 return TEE_SUCCESS; 55 } 56 } 57 return TEE_ERROR_TIME_NOT_SET; 58 } 59 60 static TEE_Result tee_time_ta_set_offs(const TEE_UUID *uuid, 61 const TEE_Time *offs, bool positive) 62 { 63 size_t n; 64 struct tee_ta_time_offs *o; 65 66 for (n = 0; n < tee_time_num_offs; n++) { 67 if (memcmp(uuid, &tee_time_offs[n].uuid, sizeof(TEE_UUID)) 68 == 0) { 69 tee_time_offs[n].offs = *offs; 70 tee_time_offs[n].positive = positive; 71 return TEE_SUCCESS; 72 } 73 } 74 75 n = tee_time_num_offs + 1; 76 o = realloc(tee_time_offs, n * sizeof(struct tee_ta_time_offs)); 77 if (!o) 78 return TEE_ERROR_OUT_OF_MEMORY; 79 tee_time_offs = o; 80 tee_time_offs[tee_time_num_offs].uuid = *uuid; 81 tee_time_offs[tee_time_num_offs].offs = *offs; 82 tee_time_offs[tee_time_num_offs].positive = positive; 83 tee_time_num_offs = n; 84 return TEE_SUCCESS; 85 } 86 87 TEE_Result tee_time_get_ta_time(const TEE_UUID *uuid, TEE_Time *time) 88 { 89 TEE_Result res; 90 const TEE_Time *offs; 91 bool positive; 92 TEE_Time t; 93 TEE_Time t2; 94 95 res = tee_time_ta_get_offs(uuid, &offs, &positive); 96 if (res != TEE_SUCCESS) 97 return res; 98 99 res = tee_time_get_sys_time(&t); 100 if (res != TEE_SUCCESS) 101 return res; 102 103 if (positive) { 104 TEE_TIME_ADD(t, *offs, t2); 105 106 /* Detect wrapping, the wrapped time should be returned. */ 107 if (TEE_TIME_LT(t2, t)) 108 res = TEE_ERROR_OVERFLOW; 109 } else { 110 TEE_TIME_SUB(t, *offs, t2); 111 112 /* Detect wrapping, the wrapped time should be returned. */ 113 if (TEE_TIME_LE(t, t2)) 114 res = TEE_ERROR_OVERFLOW; 115 } 116 *time = t2; 117 118 return res; 119 } 120 121 TEE_Result tee_time_set_ta_time(const TEE_UUID *uuid, const TEE_Time *time) 122 { 123 TEE_Result res; 124 TEE_Time offs; 125 TEE_Time t; 126 127 /* Check that time is normalized. */ 128 if (time->millis >= TEE_TIME_MILLIS_BASE) 129 return TEE_ERROR_BAD_PARAMETERS; 130 131 res = tee_time_get_sys_time(&t); 132 if (res != TEE_SUCCESS) 133 return res; 134 135 if (TEE_TIME_LT(t, *time)) { 136 TEE_TIME_SUB(*time, t, offs); 137 return tee_time_ta_set_offs(uuid, &offs, true); 138 } else { 139 TEE_TIME_SUB(t, *time, offs); 140 return tee_time_ta_set_offs(uuid, &offs, false); 141 } 142 } 143 144 void tee_time_busy_wait(uint32_t milliseconds_delay) 145 { 146 TEE_Time curr; 147 TEE_Time delta; 148 TEE_Time end; 149 150 if (tee_time_get_sys_time(&curr) != TEE_SUCCESS) 151 panic(); 152 delta.seconds = milliseconds_delay / 1000; 153 delta.millis = milliseconds_delay % 1000; 154 TEE_TIME_ADD(curr, delta, end); 155 156 while (TEE_TIME_LT(curr, end)) 157 if (tee_time_get_sys_time(&curr) != TEE_SUCCESS) 158 panic(); 159 } 160