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