Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, STMicroelectronics International N.V. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include "xtest_test.h" |
| 21 | #include "xtest_helpers.h" |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 22 | #include "tee_api_defines.h" |
| 23 | #include "tee_client_api.h" |
| 24 | |
| 25 | #define OFFSET0 0 |
| 26 | |
| 27 | #define PARAM_0 0 |
| 28 | #define PARAM_1 1 |
| 29 | #define PARAM_2 2 |
| 30 | #define PARAM_3 3 |
| 31 | |
| 32 | struct xtest_session { |
| 33 | ADBG_Case_t *c; |
| 34 | TEEC_Session session; |
| 35 | TEEC_Context context; |
| 36 | }; |
| 37 | |
| 38 | /* Compares two memories and checks if their length and content is the same */ |
| 39 | #define EXPECT_SHARED_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, shrm) \ |
| 40 | do { \ |
| 41 | if ((exp_buf) == NULL) { \ |
| 42 | ADBG_EXPECT((c), exp_blen, \ |
| 43 | (op)->params[(param_num)].memref.size); \ |
| 44 | } else { \ |
| 45 | ADBG_EXPECT_COMPARE_POINTER((c), (shrm), ==, \ |
| 46 | (op)->params[(param_num)].memref.parent); \ |
| 47 | ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \ |
| 48 | (shrm)->buffer, \ |
| 49 | (op)->params[(param_num)].memref.size); \ |
| 50 | } \ |
| 51 | } while (0) |
| 52 | |
| 53 | /* |
| 54 | * Compares the content of the memory cells in OP with the expected value |
| 55 | * contained. |
| 56 | */ |
| 57 | #define EXPECT_OP_TMP_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, buf) \ |
| 58 | do { \ |
| 59 | if ((exp_buf) == NULL) { \ |
| 60 | ADBG_EXPECT((c), exp_blen, \ |
| 61 | (op)->params[(param_num)].tmpref.size); \ |
| 62 | } else { \ |
| 63 | ADBG_EXPECT_COMPARE_POINTER((c), (buf), ==, \ |
| 64 | (op)->params[(param_num)].tmpref.buffer); \ |
| 65 | ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \ |
| 66 | (buf), \ |
| 67 | (op)->params[(param_num)].memref.size); \ |
| 68 | } \ |
| 69 | } while (0) |
| 70 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 71 | /* Registers the TEEC_SharedMemory to the TEE. */ |
| 72 | static TEEC_Result RegisterSharedMemory(TEEC_Context *ctx, |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 73 | TEEC_SharedMemory *shm, size_t size, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 74 | uint32_t flags) |
| 75 | { |
| 76 | shm->flags = flags; |
| 77 | shm->size = size; |
| 78 | return TEEC_RegisterSharedMemory(ctx, shm); |
| 79 | } |
| 80 | |
| 81 | /* Allocates shared memory inside of the TEE. */ |
| 82 | static TEEC_Result AllocateSharedMemory(TEEC_Context *ctx, |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 83 | TEEC_SharedMemory *shm, size_t size, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 84 | uint32_t flags) |
| 85 | { |
| 86 | shm->flags = flags; |
| 87 | shm->size = size; |
| 88 | return TEEC_AllocateSharedMemory(ctx, shm); |
| 89 | } |
| 90 | |
| 91 | static void CloseSession_null(struct xtest_session *cs) |
| 92 | { |
| 93 | Do_ADBG_BeginSubCase(cs->c, "CloseSession_null"); |
| 94 | { |
| 95 | /* In reality doesn't test anything. */ |
| 96 | TEEC_CloseSession(NULL); |
| 97 | } |
| 98 | Do_ADBG_EndSubCase(cs->c, "CloseSession_null"); |
| 99 | } |
| 100 | |
| 101 | static void Allocate_In(struct xtest_session *cs) |
| 102 | { |
| 103 | Do_ADBG_BeginSubCase(cs->c, "Allocate_In"); |
| 104 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 105 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 106 | size_t size = 1024; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 107 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 108 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 109 | TEEC_InitializeContext(_device, &cs->context))) |
| 110 | goto out; |
| 111 | |
| 112 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 113 | AllocateSharedMemory(&cs->context, &shm, size, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 114 | TEEC_MEM_INPUT))) |
| 115 | goto out_final; |
| 116 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 117 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 118 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 119 | TEEC_FinalizeContext(&cs->context); |
| 120 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 121 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 122 | Do_ADBG_EndSubCase(cs->c, "Allocate_In"); |
| 123 | } |
| 124 | |
| 125 | static void Allocate_out_of_memory(struct xtest_session *cs) |
| 126 | { |
| 127 | Do_ADBG_BeginSubCase(cs->c, "Allocate_out_of_memory"); |
| 128 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 129 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 130 | size_t SIZE_OVER_MEMORY_CAPACITY = SIZE_MAX; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 131 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 132 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 133 | TEEC_InitializeContext(_device, &cs->context))) |
| 134 | goto out; |
| 135 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 136 | ADBG_EXPECT_TEEC_RESULT(cs->c, TEEC_ERROR_OUT_OF_MEMORY, |
| 137 | AllocateSharedMemory(&cs->context, &shm, |
| 138 | SIZE_OVER_MEMORY_CAPACITY, |
| 139 | TEEC_MEM_INPUT)); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 140 | TEEC_FinalizeContext(&cs->context); |
| 141 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 142 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 143 | Do_ADBG_EndSubCase(cs->c, "Allocate_out_of_memory"); |
| 144 | } |
| 145 | |
| 146 | static void OpenSession_error_notExistingTA(struct xtest_session *cs) |
| 147 | { |
| 148 | Do_ADBG_BeginSubCase(cs->c, "OpenSession_error_notExistingTA"); |
| 149 | { |
| 150 | TEEC_UUID NONEXISTING_TA_UUID = { 0x534D1192, 0x6143, 0x234C, |
| 151 | { 0x47, 0x55, 0x53, 0x52, |
| 152 | 0x54, 0x4F, 0x4F, 0x59 } }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 153 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 154 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 155 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 156 | TEEC_InitializeContext(_device, &cs->context))) |
| 157 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 158 | |
| 159 | ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=, |
| 160 | TEEC_OpenSession(&cs->context, &cs->session, |
| 161 | &NONEXISTING_TA_UUID, |
| 162 | TEEC_LOGIN_PUBLIC, NULL, NULL, |
| 163 | &ret_orig)); |
| 164 | ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_ORIGIN_TRUSTED_APP, !=, |
| 165 | ret_orig); |
| 166 | TEEC_FinalizeContext(&cs->context); |
| 167 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 168 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 169 | Do_ADBG_EndSubCase(cs->c, "OpenSession_error_notExistingTA"); |
| 170 | } |
| 171 | |
| 172 | static void Allocate_InOut(struct xtest_session *cs) |
| 173 | { |
| 174 | Do_ADBG_BeginSubCase(cs->c, "Allocate_InOut"); |
| 175 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 176 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 177 | uint8_t val[] = { 54, 76, 98, 32 }; |
| 178 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 179 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 180 | TEEC_InitializeContext(_device, &cs->context))) |
| 181 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 182 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 183 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 184 | AllocateSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 185 | TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) |
| 186 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 187 | |
| 188 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 189 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 190 | TEEC_FinalizeContext(&cs->context); |
| 191 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 192 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 193 | Do_ADBG_EndSubCase(cs->c, "Allocate_InOut"); |
| 194 | } |
| 195 | |
| 196 | static void Register_In(struct xtest_session *cs) |
| 197 | { |
| 198 | Do_ADBG_BeginSubCase(cs->c, "Register_In"); |
| 199 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 200 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 201 | uint8_t val[] = { 32, 65, 43, 21, 98 }; |
| 202 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 203 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 204 | TEEC_InitializeContext(_device, &cs->context))) |
| 205 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 206 | |
| 207 | shm.buffer = val; |
| 208 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 209 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 210 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 211 | TEEC_MEM_INPUT))) |
| 212 | goto out_final; |
| 213 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 214 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 215 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 216 | TEEC_FinalizeContext(&cs->context); |
| 217 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 218 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 219 | Do_ADBG_EndSubCase(cs->c, "Register_In"); |
| 220 | } |
| 221 | |
| 222 | static void Register_notZeroLength_Out(struct xtest_session *cs) |
| 223 | { |
| 224 | Do_ADBG_BeginSubCase(cs->c, "Register_notZeroLength_Out"); |
| 225 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 226 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 227 | uint8_t val[] = { 56, 67, 78, 99 }; |
| 228 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 229 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 230 | TEEC_InitializeContext(_device, &cs->context))) |
| 231 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 232 | |
| 233 | shm.buffer = val; |
| 234 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 235 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 236 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
| 237 | TEEC_MEM_OUTPUT))) |
| 238 | goto out_final; |
| 239 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 240 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 241 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 242 | TEEC_FinalizeContext(&cs->context); |
| 243 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 244 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 245 | Do_ADBG_EndSubCase(cs->c, "Register_notZeroLength_Out"); |
| 246 | } |
| 247 | |
| 248 | static void Register_InOut(struct xtest_session *cs) |
| 249 | { |
| 250 | Do_ADBG_BeginSubCase(cs->c, "Register_InOut"); |
| 251 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 252 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 253 | uint8_t val[] = { 54, 76, 23, 98, 255, 23, 86 }; |
| 254 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 255 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 256 | TEEC_InitializeContext(_device, &cs->context))) |
| 257 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 258 | |
| 259 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 260 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 261 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 262 | TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) |
| 263 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 264 | |
| 265 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 266 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 267 | TEEC_FinalizeContext(&cs->context); |
| 268 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 269 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 270 | Do_ADBG_EndSubCase(cs->c, "Register_InOut"); |
| 271 | } |
| 272 | |
| 273 | static void Register_zeroLength_Out(struct xtest_session *cs) |
| 274 | { |
| 275 | Do_ADBG_BeginSubCase(cs->c, "Register_zeroLength_Out"); |
| 276 | { |
| 277 | uint8_t val[] = { 65, 76, 98, 32 }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 278 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 279 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 280 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 281 | TEEC_InitializeContext(_device, &cs->context))) |
| 282 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 283 | |
| 284 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 285 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 286 | RegisterSharedMemory(&cs->context, &shm, 0, |
| 287 | TEEC_MEM_OUTPUT))) |
| 288 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 289 | |
| 290 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 291 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 292 | TEEC_FinalizeContext(&cs->context); |
| 293 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 294 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 295 | Do_ADBG_EndSubCase(cs->c, "Register_zeroLength_Out"); |
| 296 | } |
| 297 | |
| 298 | static void Allocate_Out(struct xtest_session *cs) |
| 299 | { |
| 300 | Do_ADBG_BeginSubCase(cs->c, "Allocate_Out"); |
| 301 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 302 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 303 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 304 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 305 | TEEC_InitializeContext(_device, &cs->context))) |
| 306 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 307 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 308 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 309 | AllocateSharedMemory(&cs->context, &shm, 0, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 310 | TEEC_MEM_OUTPUT))) |
| 311 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 312 | |
| 313 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 314 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 315 | TEEC_FinalizeContext(&cs->context); |
| 316 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 317 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 318 | Do_ADBG_EndSubCase(cs->c, "Allocate_Out"); |
| 319 | } |
| 320 | |
| 321 | static void FinalizeContext_null(struct xtest_session *cs) |
| 322 | { |
| 323 | Do_ADBG_BeginSubCase(cs->c, "FinalizeContext_null"); |
| 324 | { |
| 325 | TEEC_FinalizeContext(NULL); |
| 326 | } |
| 327 | Do_ADBG_EndSubCase(cs->c, "FinalizeContext_null"); |
| 328 | } |
| 329 | |
| 330 | static void InitializeContext_NotExistingTEE(struct xtest_session *cs) |
| 331 | { |
| 332 | Do_ADBG_BeginSubCase(cs->c, "InitializeContext_NotExistingTEE"); |
| 333 | { |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 334 | if (!ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 335 | TEEC_InitializeContext("Invalid TEE name", |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 336 | &cs->context))) |
| 337 | TEEC_FinalizeContext(&cs->context); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 338 | } |
| 339 | Do_ADBG_EndSubCase(cs->c, "InitializeContext_NotExistingTEE"); |
| 340 | } |
| 341 | |
| 342 | static void AllocateThenRegister_SameMemory(struct xtest_session *cs) |
| 343 | { |
| 344 | Do_ADBG_BeginSubCase(cs->c, "AllocateThenRegister_SameMemory"); |
| 345 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 346 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 347 | size_t size_allocation = 32; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 348 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 349 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 350 | TEEC_InitializeContext(_device, &cs->context))) |
| 351 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 352 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 353 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 354 | AllocateSharedMemory(&cs->context, &shm, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 355 | size_allocation, TEEC_MEM_INPUT))) |
| 356 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 357 | |
| 358 | ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 359 | RegisterSharedMemory(&cs->context, &shm, |
| 360 | size_allocation, TEEC_MEM_INPUT)); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 361 | |
| 362 | TEEC_ReleaseSharedMemory(&shm); |
| 363 | out_final: |
| 364 | TEEC_FinalizeContext(&cs->context); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 365 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 366 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 367 | Do_ADBG_EndSubCase(cs->c, "AllocateThenRegister_SameMemory"); |
| 368 | } |
| 369 | |
| 370 | static void AllocateSameMemory_twice(struct xtest_session *cs) |
| 371 | { |
| 372 | Do_ADBG_BeginSubCase(cs->c, "AllocateSameMemory_twice"); |
| 373 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 374 | TEEC_SharedMemory shm = { }; |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 375 | size_t size_allocation = 32; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 376 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 377 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 378 | TEEC_InitializeContext(_device, &cs->context))) |
| 379 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 380 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 381 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 382 | AllocateSharedMemory(&cs->context, &shm, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 383 | size_allocation, TEEC_MEM_INPUT))) |
| 384 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 385 | |
| 386 | ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 387 | AllocateSharedMemory(&cs->context, &shm, |
| 388 | size_allocation, TEEC_MEM_INPUT)); |
| 389 | |
| 390 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 391 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 392 | TEEC_FinalizeContext(&cs->context); |
| 393 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 394 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 395 | Do_ADBG_EndSubCase(cs->c, "AllocateSameMemory_twice"); |
| 396 | } |
| 397 | |
| 398 | static void RegisterSameMemory_twice(struct xtest_session *cs) |
| 399 | { |
| 400 | Do_ADBG_BeginSubCase(cs->c, "RegisterSameMemory_twice"); |
| 401 | { |
| 402 | uint8_t val[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 403 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 404 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 405 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 406 | TEEC_InitializeContext(_device, &cs->context))) |
| 407 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 408 | |
| 409 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 410 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 411 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 412 | TEEC_MEM_INPUT))) |
| 413 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 414 | |
| 415 | ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 416 | RegisterSharedMemory(&cs->context, &shm, sizeof(val), |
| 417 | TEEC_MEM_INPUT)); |
| 418 | |
| 419 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 420 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 421 | TEEC_FinalizeContext(&cs->context); |
| 422 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 423 | out: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 424 | Do_ADBG_EndSubCase(cs->c, "RegisterSameMemory_twice"); |
| 425 | } |
| 426 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 427 | #ifndef MIN |
| 428 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 429 | #endif |
| 430 | |
| 431 | static void Allocate_sharedMemory_32k(struct xtest_session *cs) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 432 | { |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 433 | Do_ADBG_BeginSubCase(cs->c, "Allocate_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 434 | { |
Jerome Forissier | de0c443 | 2017-05-23 11:38:08 +0200 | [diff] [blame] | 435 | size_t size = MIN(32 * 1024, |
| 436 | TEEC_CONFIG_SHAREDMEM_MAX_SIZE); |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 437 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 438 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 439 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 440 | TEEC_InitializeContext(_device, &cs->context))) |
| 441 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 442 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 443 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 444 | AllocateSharedMemory(&cs->context, &shm, size, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 445 | TEEC_MEM_INPUT))) |
| 446 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 447 | |
| 448 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 449 | out_final: |
| 450 | TEEC_FinalizeContext(&cs->context); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 451 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 452 | out: |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 453 | Do_ADBG_EndSubCase(cs->c, "Allocate_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 456 | #define SHM_32K_SIZE MIN(32 * 1024, TEEC_CONFIG_SHAREDMEM_MAX_SIZE) |
| 457 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 458 | static void Register_sharedMemory_32k(struct xtest_session *cs) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 459 | { |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 460 | Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 461 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 462 | uint8_t val[SHM_32K_SIZE] = { }; |
| 463 | TEEC_SharedMemory shm = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 464 | |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 465 | if (!ADBG_EXPECT(cs->c, TEEC_SUCCESS, |
| 466 | TEEC_InitializeContext(_device, &cs->context))) |
| 467 | goto out; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 468 | |
| 469 | shm.buffer = val; |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 470 | if (!ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame^] | 471 | RegisterSharedMemory(&cs->context, &shm, SHM_32K_SIZE, |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 472 | TEEC_MEM_INPUT))) |
| 473 | goto out_final; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 474 | |
| 475 | TEEC_ReleaseSharedMemory(&shm); |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 476 | out_final: |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 477 | TEEC_FinalizeContext(&cs->context); |
| 478 | } |
Jens Wiklander | 72c5545 | 2016-10-27 20:44:35 +0200 | [diff] [blame] | 479 | out: |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 480 | Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_32k"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void xtest_teec_TEE(ADBG_Case_t *c) |
| 484 | { |
| 485 | struct xtest_session connection = { c }; |
| 486 | |
| 487 | CloseSession_null(&connection); |
| 488 | |
| 489 | Allocate_In(&connection); |
| 490 | |
| 491 | Allocate_out_of_memory(&connection); |
| 492 | |
| 493 | OpenSession_error_notExistingTA(&connection); |
| 494 | |
| 495 | Allocate_InOut(&connection); |
| 496 | |
| 497 | Register_In(&connection); |
| 498 | |
| 499 | Register_notZeroLength_Out(&connection); |
| 500 | |
| 501 | Register_InOut(&connection); |
| 502 | |
| 503 | Register_zeroLength_Out(&connection); |
| 504 | |
| 505 | Allocate_Out(&connection); |
| 506 | |
| 507 | FinalizeContext_null(&connection); |
| 508 | |
| 509 | InitializeContext_NotExistingTEE(&connection); |
| 510 | |
| 511 | AllocateThenRegister_SameMemory(&connection); |
| 512 | |
| 513 | AllocateSameMemory_twice(&connection); |
| 514 | |
| 515 | RegisterSameMemory_twice(&connection); |
| 516 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 517 | Allocate_sharedMemory_32k(&connection); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 518 | |
Jerome Forissier | cefbe02 | 2017-05-23 11:02:35 +0200 | [diff] [blame] | 519 | Register_sharedMemory_32k(&connection); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 522 | ADBG_CASE_DEFINE(regression, 5006, xtest_teec_TEE, |
Jens Wiklander | 25a57fe | 2016-12-26 21:46:24 +0100 | [diff] [blame] | 523 | "Tests for Global platform TEEC"); |