summaryrefslogtreecommitdiff
path: root/mobicore/MobiCoreDriverLib/ClientLib/Contrib/tee_client_api.cpp
blob: 3424ca7f0214a6069725f41add4ae82235d517c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
 * Copyright (c) 2013-2015 TRUSTONIC LIMITED
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "Mci/mcinq.h"          /* TA termination codes */

#undef LOG_TAG
#define LOG_TAG "TeeGpClient"
#include "log.h"
#include "common_client.h"
#include "tee_client_api.h"
#include "GpTci.h"      /* Needs stuff from tee_client_api.h or its includes */

//------------------------------------------------------------------------------
// Macros
#define _TEEC_GET_PARAM_TYPE(t, i) (((t) >> (4*i)) & 0xF)

//Parameter number
#define _TEEC_PARAMETER_NUMBER      4

/**
 * These error codes are still to be decided by GP and as we do not wish to
 * expose any part of the GP TAF as of yet, for now they will have to live here
 * until we decide what to do about them.
 */
#define TEEC_ERROR_TA_LOCKED        0xFFFF0257
#define TEEC_ERROR_SD_BLOCKED       0xFFFF0258
#define TEEC_ERROR_TARGET_KILLED    0xFFFF0259

static CommonClient& client = CommonClient::getInstance();

//------------------------------------------------------------------------------
// Local functions
static TEEC_Result _TEEC_UnwindOperation(
    TEEC_Session_IMP*    session,
    _TEEC_TCI*           tci,
    TEEC_Operation*      operation,
    bool                 copyValues,
    uint32_t*            returnOrigin);

//------------------------------------------------------------------------------
static void _libUuidToArray(
    const TEEC_UUID* uuid,
    uint8_t*         uuidArr) {
    uint8_t* pIdentifierCursor = (uint8_t*)uuid;
    /* offsets and syntax constants. See explanations above */
#ifdef S_BIG_ENDIAN
    uint32_t offsets = 0;
#else
    uint32_t offsets = 0xF1F1DF13;
#endif
    uint32_t i;

    for (i = 0; i < sizeof(TEEC_UUID); i++) {
        /* Two-digit hex number */
        uint8_t number;
        int32_t offset = ((int32_t)((offsets & 0xF) << 28)) >> 28;
        number = pIdentifierCursor[offset];
        offsets >>= 4;
        pIdentifierCursor++;

        uuidArr[i] = number;
    }
}

//------------------------------------------------------------------------------
static TEEC_Result _TEEC_SetupOperation(
    TEEC_Session_IMP*    session,
    _TEEC_TCI*           tci,
    TEEC_Operation*      operation,
    uint32_t*            returnOrigin) {
    uint32_t                    i;
    _TEEC_ParameterInternal*    imp;
    TEEC_Parameter*             ext;
    TEEC_Result                 teecResult = TEEC_SUCCESS;

    LOG_D(" %s()", __func__);

    tci->operation.isCancelled = false;
    tci->operation.paramTypes = 0;

    //operation can be NULL
    if (operation) {
        uint32_t n_buf = 0;
        operation->started = 1;
        // Buffers to map to SWd
        struct mc_ioctl_map map;
        map.sid = session->sessionId;
        // Operation parameters for the buffers above
        _TEEC_ParameterInternal* imps[_TEEC_PARAMETER_NUMBER] = { NULL };

        //This design allows a non-NULL buffer with a size of 0 bytes to allow trivial integration with any
        //implementations of the C library malloc, in which is valid to allocate a zero byte buffer and receive a non-
        //NULL pointer which may not be de-referenced in return.

        for (i = 0; i < _TEEC_PARAMETER_NUMBER && teecResult == TEEC_SUCCESS; i++) {
            uint8_t paramType = _TEEC_GET_PARAM_TYPE(operation->paramTypes, i);

            imp = &tci->operation.params[i];
            ext = &operation->params[i];

            switch (paramType) {
                case TEEC_VALUE_OUTPUT:
                    LOG_D("  cycle %d, TEEC_VALUE_OUTPUT", i);
                    break;
                case TEEC_NONE:
                    LOG_D("  cycle %d, TEEC_NONE", i);
                    break;
                case TEEC_VALUE_INPUT:
                case TEEC_VALUE_INOUT: {
                    LOG_D("  cycle %d, TEEC_VALUE_IN*", i);
                    imp->value.a = ext->value.a;
                    imp->value.b = ext->value.b;
                    break;
                }
                case TEEC_MEMREF_TEMP_INPUT:
                case TEEC_MEMREF_TEMP_OUTPUT:
                case TEEC_MEMREF_TEMP_INOUT: {
                    // A Temporary Memory Reference may be null, which can be used
                    // to denote a special case for the parameter. Output Memory
                    // References that are null are typically used to request the
                    // required output size.
                    LOG_D("  cycle %d, TEEC_MEMREF_TEMP_*", i);
                    if ((ext->tmpref.size) && (ext->tmpref.buffer)) {
                        map.bufs[n_buf].va = (uintptr_t)ext->tmpref.buffer;
                        map.bufs[n_buf].len = (uint32_t)ext->tmpref.size;
                        map.bufs[n_buf].flags = paramType & TEEC_MEM_INOUT;
                        imps[n_buf] = imp;
                        n_buf++;
                    } else {
                        LOG_D("  cycle %d, TEEC_TEMP_IN*  - zero pointer or size", i);
                    }
                    break;
                }
                case TEEC_MEMREF_WHOLE: {
                    LOG_D("  cycle %d, TEEC_MEMREF_WHOLE", i);
                    if (ext->memref.parent->size) {
                        map.bufs[n_buf].va = (uintptr_t)ext->memref.parent->buffer;
                        map.bufs[n_buf].len = (uint32_t)ext->memref.parent->size;
                        map.bufs[n_buf].flags = ext->memref.parent->flags & TEEC_MEM_INOUT;
                        imps[n_buf] = imp;
                        n_buf++;
                    }
                    /* We don't transmit that the mem ref is the whole shared mem */
                    /* Magic number 4 means that it is a mem ref */
                    paramType = (uint8_t)ext->memref.parent->flags | 4;
                    break;
                }
                case TEEC_MEMREF_PARTIAL_INPUT:
                case TEEC_MEMREF_PARTIAL_OUTPUT:
                case TEEC_MEMREF_PARTIAL_INOUT: {
                    LOG_D("  cycle %d, TEEC_MEMREF_PARTIAL_*", i);
                    //Check data flow consistency
                    if ((((ext->memref.parent->flags & TEEC_MEM_INOUT) == TEEC_MEM_INPUT) &&
                            (paramType == TEEC_MEMREF_PARTIAL_OUTPUT)) ||
                            (((ext->memref.parent->flags & TEEC_MEM_INOUT) == TEEC_MEM_OUTPUT) &&
                             (paramType == TEEC_MEMREF_PARTIAL_INPUT))) {
                        LOG_E("PARTIAL data flow inconsistency");
                        *returnOrigin = TEEC_ORIGIN_API;
                        teecResult = TEEC_ERROR_BAD_PARAMETERS;
                        break;
                    }
                    /* We don't transmit that the mem ref is partial */
                    paramType &= TEEC_MEMREF_TEMP_INOUT;

                    if (ext->memref.offset + ext->memref.size > ext->memref.parent->size) {
                        LOG_E("PARTIAL offset/size error");
                        *returnOrigin = TEEC_ORIGIN_API;
                        teecResult = TEEC_ERROR_BAD_PARAMETERS;
                        break;
                    }
                    if (ext->memref.size) {
                        map.bufs[n_buf].va = (uintptr_t)ext->memref.parent->buffer + ext->memref.offset;
                        map.bufs[n_buf].len = (uint32_t)ext->memref.size;
                        map.bufs[n_buf].flags = paramType & TEEC_MEM_INOUT;
                        imps[n_buf] = imp;
                        n_buf++;
                    }
                    break;
                }
                default:
                    LOG_E("cycle %d, default", i);
                    *returnOrigin = TEEC_ORIGIN_API;
                    teecResult = TEEC_ERROR_BAD_PARAMETERS;
                    break;
            }
            tci->operation.paramTypes |= (uint32_t)(paramType << i * 4);
        }

        if (n_buf > MC_MAP_MAX) {
            LOG_E("too many buffers: %s", strerror(errno));
            teecResult = TEEC_ERROR_EXCESS_DATA;
        }

        if ((teecResult == TEEC_SUCCESS) && (tci->operation.isCancelled)) {
            LOG_E("the operation has been cancelled in COMMS");
            *returnOrigin = TEEC_ORIGIN_COMMS;
            teecResult = TEEC_ERROR_CANCEL;
        }

        // Map buffers
        if ((teecResult == TEEC_SUCCESS) && (n_buf > 0)) {
            for (i = n_buf; i < MC_MAP_MAX; i++) {
                map.bufs[i].va = 0;
            }
            if (client.map(map) == 0) {
                for (i = 0; i < MC_MAP_MAX; i++) {
                    if (map.bufs[i].va) {
                        imps[i]->memref.sVirtualAddr = (uint32_t)map.bufs[i].sva;
                        imps[i]->memref.sVirtualLen = map.bufs[i].len;
                    }
                }
            } else {
                LOG_E("client map failed: %s", strerror(errno));
                *returnOrigin = TEEC_ORIGIN_COMMS;
                teecResult = TEEC_ERROR_GENERIC;
            }
        }

        if (teecResult != TEEC_SUCCESS) {
            uint32_t retOrigIgnored;
            _TEEC_UnwindOperation(session, tci, operation, false, &retOrigIgnored);
            //Zeroing out tci->operation
            memset(&tci->operation, 0, sizeof(tci->operation));
            return teecResult;
        }
    }

    //Copy version indicator field
    memcpy(tci->header, "TCIGP000", sizeof(tci->header));

    // Fill in invalid values for secure world to overwrite
    tci->returnStatus = TEEC_ERROR_BAD_STATE;

    // Signal completion of request writing
    tci->ready = 1;

    return teecResult;
}

//------------------------------------------------------------------------------
static TEEC_Result _TEEC_UnwindOperation(
    TEEC_Session_IMP*    session,
    _TEEC_TCI*           tci,
    TEEC_Operation*      operation,
    bool                 copyValues,
    uint32_t*            returnOrigin) {
    uint32_t                    i;
    _TEEC_ParameterInternal*    imp;
    TEEC_Parameter*             ext;
    uint32_t                    n_buf = 0;

    //operation can be NULL
    if (operation == NULL) {
        return  TEEC_SUCCESS;
    }

    LOG_D(" %s()", __func__);

    operation->started = 2;
    // Buffers to unmap from SWd
    struct mc_ioctl_map map;
    map.sid = session->sessionId;
    // Some sanity checks
    if (tci->returnOrigin == 0 ||
            ((tci->returnOrigin != TEEC_ORIGIN_TRUSTED_APP) &&
             (tci->returnStatus != TEEC_SUCCESS))) {
        *returnOrigin = TEEC_ORIGIN_COMMS;
        return TEEC_ERROR_COMMUNICATION;
    }
    *returnOrigin = tci->returnOrigin;

    //Clear sVirtualLen to unMap further
    for (i = 0; i < _TEEC_PARAMETER_NUMBER; i++) {
        imp = &tci->operation.params[i];
        ext = &operation->params[i];

        switch (_TEEC_GET_PARAM_TYPE(operation->paramTypes, i)) {
            case TEEC_VALUE_INPUT:
                LOG_D("  cycle %d, TEEC_VALUE_INPUT", i);
                break;
            case TEEC_NONE:
                LOG_D("  cycle %d, TEEC_NONE", i);
                break;
            case TEEC_VALUE_OUTPUT:
            case TEEC_VALUE_INOUT: {
                LOG_D("  cycle %d, TEEC_VALUE_*OUT", i);
                if (copyValues) {
                    ext->value.a = imp->value.a;
                    ext->value.b = imp->value.b;
                }
                break;
            }
            case TEEC_MEMREF_TEMP_OUTPUT:
            case TEEC_MEMREF_TEMP_INPUT:
            case TEEC_MEMREF_TEMP_INOUT: {
                LOG_D("  cycle %d, TEEC_TEMP*", i);
                if ((copyValues) &&
                        (_TEEC_GET_PARAM_TYPE(operation->paramTypes, i) != TEEC_MEMREF_TEMP_INPUT)) {
                    ext->tmpref.size = imp->memref.outputSize;
                }
                if (imp->memref.sVirtualLen > 0) {
                    map.bufs[n_buf].va = (uintptr_t)ext->tmpref.buffer;
                    map.bufs[n_buf].sva = imp->memref.sVirtualAddr;
                    map.bufs[n_buf].len = imp->memref.sVirtualLen;
                    n_buf++;
                }
                break;
            }
            case TEEC_MEMREF_WHOLE: {
                LOG_D("  cycle %d, TEEC_MEMREF_WHOLE", i);
                if ((copyValues) && (ext->memref.parent->flags != TEEC_MEM_INPUT)) {
                    ext->memref.size = imp->memref.outputSize;
                }
                if (imp->memref.sVirtualLen > 0) {
                    map.bufs[n_buf].va = (uintptr_t)ext->memref.parent->buffer;
                    map.bufs[n_buf].sva = imp->memref.sVirtualAddr;
                    map.bufs[n_buf].len = imp->memref.sVirtualLen;
                    n_buf++;
                }
                break;
            }

            case TEEC_MEMREF_PARTIAL_OUTPUT:
            case TEEC_MEMREF_PARTIAL_INOUT:
            case TEEC_MEMREF_PARTIAL_INPUT: {
                LOG_D("  cycle %d, TEEC_MEMREF_PARTIAL*", i);
                if ((copyValues) &&
                        (_TEEC_GET_PARAM_TYPE(operation->paramTypes, i) != TEEC_MEMREF_PARTIAL_INPUT)) {
                    ext->memref.size = imp->memref.outputSize;
                }
                if (imp->memref.sVirtualLen > 0) {
                    map.bufs[n_buf].va = (uintptr_t)ext->memref.parent->buffer + ext->memref.offset;
                    map.bufs[n_buf].sva = imp->memref.sVirtualAddr;
                    map.bufs[n_buf].len = imp->memref.sVirtualLen;
                    n_buf++;
                }
                break;
            }
            default:
                LOG_E("cycle %d, bad parameter", i);
                break;
        }
    }

    if (n_buf > MC_MAP_MAX) {
        LOG_E("too many buffers: %s", strerror(errno));
        return TEEC_ERROR_EXCESS_DATA;
    }

    for (i = n_buf; i < MC_MAP_MAX; i++) {
        map.bufs[i].va = 0;
    }

    if (n_buf > 0) {
        // This function assumes that we cannot handle errors
        if (client.unmap(map) < 0) {
            LOG_E("client unmap failed: %s", strerror(errno));
        }
    }

    return tci->returnStatus;
}

//------------------------------------------------------------------------------
//TEEC_InitializeContext: TEEC_SUCCESS, Another error code from Table 4-2.
TEEC_Result TEEC_InitializeContext(
    const char*   name,
    TEEC_Context* context) {
    (void) name;
    LOG_D("== %s() ==============", __func__);

    if (context == NULL) {
        LOG_E("context is NULL");
        return TEEC_ERROR_BAD_PARAMETERS;
    }

    // For test purpose
    switch (context->imp.reserved) {
        case CommonClient::DRIVER:
            client.setOpenMode(CommonClient::DRIVER);
            break;
        case CommonClient::PROXY:
            client.setOpenMode(CommonClient::PROXY);
            break;
    }

    if (client.open()) {
        switch (errno) {
            case ENOENT:
                return TEEC_ERROR_COMMUNICATION;
            case EINVAL:
                return TEEC_ERROR_BAD_PARAMETERS;
            default:
                return TEEC_ERROR_GENERIC;
        }
    }

    return TEEC_SUCCESS;
}

//------------------------------------------------------------------------------
//TEEC_FinalizeContext: void

// The implementation of this function MUST NOT be able to fail: after this function returns the Client
// Application must be able to consider that the Context has been closed.

void TEEC_FinalizeContext(
    TEEC_Context* context) {
    LOG_D("== %s() ==============", __func__);

    // The parameter context MUST point to an initialized TEE Context.
    if (context == NULL) {
        LOG_E("context is NULL");
        return;
    }

    // The implementation of this function MUST NOT be able to fail: after this function returns the Client
    // Application must be able to consider that the Context has been closed.
    if (client.close()) {
        LOG_E("mcCloseDevice failed: %s", strerror(errno));
        /* continue even in case of error */;
    }
}

static void _TEEC_DeleteTci(TEEC_Session_IMP* session_imp) {
    if (session_imp->tci) {
        ::munmap(session_imp->tci, (size_t)sysconf(_SC_PAGESIZE));
        session_imp->tci = NULL;
    }
}

static void _TEEC_CloseSession(TEEC_Session_IMP* session_imp) {
    if (client.closeSession(session_imp->sessionId)) {
        LOG_E("%s failed: %s", __func__, strerror(errno));
    }
    session_imp->active = false;
}

//------------------------------------------------------------------------------
static TEEC_Result _TEEC_CallTA(
    TEEC_Session_IMP*    session,
    TEEC_Operation*      operation,
    uint32_t*            returnOrigin) {
    TEEC_Result     teecRes;
    TEEC_Result     teecError = TEEC_SUCCESS;
    int             ret = 0;

    LOG_D(" %s()", __func__);

    // Phase 1: start the operation and wait for the result
    _TEEC_TCI* tci = static_cast<_TEEC_TCI*>(session->tci);
    teecRes = _TEEC_SetupOperation(session, tci, operation, returnOrigin);
    if (teecRes != TEEC_SUCCESS ) {
        LOG_E("_TEEC_SetupOperation failed (%08x)", teecRes);
        return teecRes;
    }

    // Signal the Trusted App
    ret = client.notify(session->sessionId);
    if (ret) {
        LOG_E("Notify failed: %s", strerror(errno));
        teecError = TEEC_ERROR_COMMUNICATION;
    } else {
        // -------------------------------------------------------------
        // Wait for the Trusted App response
        struct mc_ioctl_wait wait;
        wait.sid = session->sessionId;
        wait.timeout = -1;
        ret = client.waitNotification(wait);
        if (ret) {
            teecError = TEEC_ERROR_COMMUNICATION;
            if (errno == ECOMM) {
                struct mc_ioctl_geterr err;
                err.sid = session->sessionId;

                ret = client.getError(err);
                switch (err.value) {
                    case TA_EXIT_CODE_FINISHED:
                        // We may get here if the TA_OpenSessionEntryPoint returns an error and TA goes fast through DestroyEntryPoint and exits the TA.
                        teecError = TEEC_SUCCESS;
                        break;
                   /* case ERR_SESSION_KILLED:
                        teecError = TEEC_ERROR_TARGET_KILLED;
                        break;
                    */
                    case ERR_INVALID_SID:
                    case ERR_SID_NOT_ACTIVE:
                        LOG_E("mcWaitNotification failed: %s", strerror(errno));
                        LOG_E("mcGetSessionErrorCode returned %d", err.value);
                        break;
                    default:
                        LOG_E("Target is DEAD");
                        *returnOrigin = TEEC_ORIGIN_TEE;
                        teecError = TEEC_ERROR_TARGET_DEAD;
                        break;
                }
            }
        }
    }
    // Phase 2: Return values and cleanup
    // unmap memory and copy values if no error
    teecRes = _TEEC_UnwindOperation(session, tci, operation,
                                    (teecError == TEEC_SUCCESS), returnOrigin);
    if (teecRes != TEEC_SUCCESS ) {
        LOG_E("_TEEC_UnwindOperation (%08x)", teecRes);
        /* continue even in case of error */;
    }

    // Cleanup
    if (teecError != TEEC_SUCCESS) {
        if (teecError == TEEC_ERROR_COMMUNICATION) {
            *returnOrigin = TEEC_ORIGIN_COMMS;
        }
        // Previous interactions failed, either TA is dead or communication error
        _TEEC_CloseSession(session);
        _TEEC_DeleteTci(session);
    }
    return teecError;
}

//------------------------------------------------------------------------------
//TEEC_OpenSession: if the returnOrigin is different from TEEC_ORIGIN_TRUSTED_APP, an error code from Table 4-2
// If the returnOrigin is equal to TEEC_ORIGIN_TRUSTED_APP, a return code defined by the
//protocol between the Client Application and the Trusted Application.
TEEC_Result TEEC_OpenSession (
    TEEC_Context*        context,
    TEEC_Session*        session,
    const TEEC_UUID*     destination,
    uint32_t             connectionMethod,
    const void*          connectionData,
    TEEC_Operation*      operation,
    uint32_t*            returnOrigin) {
    TEEC_Result         teecRes;
    uint32_t            returnOrigin_local = TEEC_ORIGIN_API;
    struct mc_uuid_t    tauuid;
    int                 ret = 0;

    LOG_D("== %s() ==============", __func__);
    // -------------------------------------------------------------
    //The parameter context MUST point to an initialized TEE Context.
    if (context == NULL) {
        LOG_E("context is NULL");
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_API;
        }
        return TEEC_ERROR_BAD_PARAMETERS;
    }

    if (session == NULL) {
        LOG_E("session is NULL");
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_API;
        }
        return TEEC_ERROR_BAD_PARAMETERS;
    }

    if ((connectionMethod != TEEC_LOGIN_PUBLIC) &&
            (connectionMethod != TEEC_LOGIN_USER) &&
            (connectionMethod != TEEC_LOGIN_GROUP) &&
            (connectionMethod != TEEC_LOGIN_APPLICATION) &&
            (connectionMethod != TEEC_LOGIN_USER_APPLICATION) &&
            (connectionMethod != TEEC_LOGIN_GROUP_APPLICATION)) {
        LOG_E("connectionMethod not supported");
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_API;
        }
        return TEEC_ERROR_NOT_IMPLEMENTED;
    }

    if ((TEEC_LOGIN_GROUP == connectionMethod) ||
            (TEEC_LOGIN_GROUP_APPLICATION == connectionMethod)) {
        if (NULL == connectionData) {
            LOG_E("connectionData is NULL");
            if (returnOrigin) {
                *returnOrigin = TEEC_ORIGIN_API;
            }
            return TEEC_ERROR_BAD_PARAMETERS;
        }
    }

    // -------------------------------------------------------------
    session->imp.active = false;

    _libUuidToArray(destination, tauuid.value);

    if (operation) {
        operation->imp.session = &session->imp;
    }

    //Allocate a 4kB page with mmap, zero it out, and set session->imp.tci to its address.
    session->imp.tci = NULL;
    void* bulkBuf = (void*)mmap(0, (size_t)sysconf(_SC_PAGESIZE),
                                PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (bulkBuf == MAP_FAILED) {
        LOG_E("mmap failed on tci buffer allocation");
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_API;
        }
        return TEEC_ERROR_OUT_OF_MEMORY;
    }

    session->imp.tci = bulkBuf;
    memset(session->imp.tci, 0, (size_t)sysconf(_SC_PAGESIZE));

    pthread_mutex_init(&session->imp.mutex_tci, NULL);
    pthread_mutex_lock(&session->imp.mutex_tci);

    //Fill the TCI buffer session.tci with the destination UUID.
    _TEEC_TCI* tci = static_cast<_TEEC_TCI*>(session->imp.tci);
    memcpy(&tci->destination, destination, sizeof(tci->destination));
    // -------------------------------------------------------------
    struct mc_ioctl_open_session sess;
    sess.sid = 0;
    sess.tci = (uintptr_t)tci;
    sess.tcilen = sizeof(_TEEC_TCI);
    sess.uuid = tauuid;
    sess.is_gp_uuid = 1;
    sess.identity.login_type = static_cast<mc_login_type>(connectionMethod);
    sess.identity.pid = 0;
    if (connectionData) {
        memcpy(&sess.identity.login_data, connectionData,
               sizeof(sess.identity.login_data));
    }

    ret = client.openSession(sess);
    if (ret) {
        LOG_E("%s failed: %s", __func__, strerror(errno));
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_COMMS;
        }
        switch (errno) {
            case ENOENT:
                teecRes = TEEC_ERROR_ITEM_NOT_FOUND;
                break;
            case EACCES:
                teecRes = TEEC_ERROR_ACCESS_DENIED;
                break;
            case EINVAL:
                teecRes = TEEC_ERROR_NOT_IMPLEMENTED;
                break;
            case ECONNREFUSED:
                teecRes = TEEC_ERROR_SD_BLOCKED;
                break;
            case ECONNABORTED:
                teecRes = TEEC_ERROR_TA_LOCKED;
                break;
            case ECONNRESET:
                teecRes = TEEC_ERROR_TARGET_KILLED;
                break;
            case EBUSY:
                teecRes = TEEC_ERROR_BUSY;
                break;
            default:
                teecRes = TEEC_ERROR_GENERIC;
        }
        goto error;
    }

    session->imp.context = context->imp;
    session->imp.sessionId = sess.sid;
    session->imp.active = true;
    LOG_I(" created session ID %x", session->imp.sessionId);

    // Let TA go through entry points
    LOG_D(" let TA go through entry points");
    tci->operation.type = _TA_OPERATION_OPEN_SESSION;
    teecRes = _TEEC_CallTA(&session->imp, operation, &returnOrigin_local);

    // Check for error on communication level
    if (teecRes != TEEC_SUCCESS ) {
        LOG_E("_TEEC_CallTA failed(%08x)", teecRes);
        // Nothing to do here because _TEEC_CallTA closes broken sessions
        if (returnOrigin) {
            *returnOrigin = returnOrigin_local;
        }
        goto error;
    }
    LOG_D(" no errors in com layer");

    // Check for error from TA
    if (returnOrigin) {
        *returnOrigin = tci->returnOrigin;
    }
    teecRes = tci->returnStatus;
    if (teecRes != TEEC_SUCCESS ) {
        LOG_E("TA OpenSession EP failed(%08x)", teecRes);
        goto error;
    }

    LOG_D(" %s() = TEEC_SUCCESS ", __func__);
    pthread_mutex_unlock(&session->imp.mutex_tci);

    if (returnOrigin) {
        *returnOrigin = TEEC_ORIGIN_TRUSTED_APP;
    }
    return TEEC_SUCCESS;

    // -------------------------------------------------------------
error:
    if (session->imp.active) {
        // After notifying us, TA went to Destry EP, so close session now
        _TEEC_CloseSession(&session->imp);
    }

    pthread_mutex_unlock(&session->imp.mutex_tci);
    pthread_mutex_destroy(&session->imp.mutex_tci);
    _TEEC_DeleteTci(&session->imp);

    LOG_D(" %s() = 0x%x", __func__, teecRes);
    return teecRes;
}

//------------------------------------------------------------------------------
TEEC_Result TEEC_InvokeCommand(
    TEEC_Session*     session,
    uint32_t          commandID,
    TEEC_Operation*   operation,
    uint32_t*         returnOrigin) {
    TEEC_Result teecRes;
    uint32_t returnOrigin_local = TEEC_ORIGIN_API;

    LOG_D("== %s() ==============", __func__);

    // -------------------------------------------------------------
    if (session == NULL) {
        LOG_E("session is NULL");
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_API;
        }
        return TEEC_ERROR_BAD_PARAMETERS;
    }

    if (!session->imp.active) {
        LOG_E("session is inactive");
        if (returnOrigin) {
            *returnOrigin = TEEC_ORIGIN_API;
        }
        return TEEC_ERROR_BAD_STATE;
    }
    // -------------------------------------------------------------
    if (operation) {
        operation->imp.session = &session->imp;
    }

    pthread_mutex_lock(&session->imp.mutex_tci);

    // Call TA
    _TEEC_TCI* tci = static_cast<_TEEC_TCI*>(session->imp.tci);
    tci->operation.commandId = commandID;
    tci->operation.type = _TA_OPERATION_INVOKE_COMMAND;
    teecRes = _TEEC_CallTA(&session->imp, operation, &returnOrigin_local);
    if (teecRes != TEEC_SUCCESS ) {
        LOG_E("_TEEC_CallTA failed(%08x)", teecRes);
        if (returnOrigin) {
            *returnOrigin = returnOrigin_local;
        }
    } else {
        if (returnOrigin) {
            *returnOrigin = tci->returnOrigin;
        }
        teecRes = tci->returnStatus;
    }

    pthread_mutex_unlock(&session->imp.mutex_tci);
    LOG_D(" %s() = 0x%x", __func__, teecRes);
    return teecRes;
}

//------------------------------------------------------------------------------
void TEEC_CloseSession(TEEC_Session* session) {
    TEEC_Result     teecRes = TEEC_SUCCESS;
    uint32_t        returnOrigin;

    LOG_D("== %s() ==============", __func__);

    // -------------------------------------------------------------
    //The Implementation MUST do nothing if the session parameter is NULL.
    if (session == NULL) {
        LOG_E("session is NULL");
        return;
    }

    // -------------------------------------------------------------
    if (session->imp.active) {
        // Let TA go through CloseSession and Destroy entry points
        LOG_D(" let TA go through close entry points");
        pthread_mutex_lock(&session->imp.mutex_tci);
        _TEEC_TCI* tci = static_cast<_TEEC_TCI*>(session->imp.tci);
        tci->operation.type = _TA_OPERATION_CLOSE_SESSION;
        teecRes = _TEEC_CallTA(&session->imp, NULL, &returnOrigin);
        if (teecRes != TEEC_SUCCESS ) {
            /* continue even in case of error */;
            LOG_E("_TEEC_CallTA failed(%08x)", teecRes);
        }

        if (session->imp.active) {
            _TEEC_CloseSession(&session->imp);
        }
        pthread_mutex_unlock(&session->imp.mutex_tci);
    }

    pthread_mutex_destroy(&session->imp.mutex_tci);
    _TEEC_DeleteTci(&session->imp);

    LOG_D(" %s() = 0x%x", __func__, teecRes);
}

//------------------------------------------------------------------------------
TEEC_Result TEEC_RegisterSharedMemory(
    TEEC_Context*      context,
    TEEC_SharedMemory* sharedMem) {
    LOG_D("== %s() ==============", __func__);

    //The parameter context MUST point to an initialized TEE Context.
    if (context == NULL) {
        LOG_E("context is NULL");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    //The parameter sharedMem MUST point to the Shared Memory structure defining
    //the memory region to register.
    if (sharedMem == NULL) {
        LOG_E("sharedMem is NULL");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    //The buffer field MUST point to the memory region to be shared, and MUST not be NULL.
    if (sharedMem->buffer == NULL) {
        LOG_E("sharedMem->buffer is NULL");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    if ((static_cast<int>(sharedMem->flags) & ~TEEC_MEM_INOUT)) {
        LOG_E("sharedMem->flags is incorrect");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    if (sharedMem->flags == 0) {
        LOG_E("sharedMem->flags is incorrect");
        return TEEC_ERROR_BAD_PARAMETERS;
    }

    sharedMem->imp.implementation_allocated = false;
    return TEEC_SUCCESS;
}

//------------------------------------------------------------------------------
TEEC_Result TEEC_AllocateSharedMemory(
    TEEC_Context*      context,
    TEEC_SharedMemory* sharedMem) {
    //No connection to "context"?
    LOG_D("== %s() ==============", __func__);

    //The parameter context MUST point to an initialized TEE Context.
    if (context == NULL) {
        LOG_E("context is NULL");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    //The parameter sharedMem MUST point to the Shared Memory structure defining
    //the memory region to register.
    if (sharedMem == NULL) {
        LOG_E("sharedMem is NULL");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    if (static_cast<int>(sharedMem->flags) & ~TEEC_MEM_INOUT) {
        LOG_E("sharedMem->flags is incorrect");
        return TEEC_ERROR_BAD_PARAMETERS;
    }
    if (sharedMem->flags == 0) {
        LOG_E("sharedMem->flags is incorrect");
        return TEEC_ERROR_BAD_PARAMETERS;
    }

    sharedMem->buffer = malloc(sharedMem->size);
    if (sharedMem->buffer == NULL) {
        LOG_E("malloc failed");
        return TEEC_ERROR_OUT_OF_MEMORY;
    }
    sharedMem->imp.implementation_allocated = true;

    return TEEC_SUCCESS;
}

//------------------------------------------------------------------------------
void TEEC_ReleaseSharedMemory (
    TEEC_SharedMemory* sharedMem) {
    //No connection to "context"?
    LOG_D("== %s() ==============", __func__);

    //The Implementation MUST do nothing if the sharedMem parameter is NULL
    if (sharedMem == NULL) {
        LOG_E("sharedMem is NULL");
        return;
    }

    //For a memory buffer allocated using TEEC_AllocateSharedMemory the Implementation
    //MUST free the underlying memory
    if (sharedMem->imp.implementation_allocated) {
        if (sharedMem->buffer) {
            free(sharedMem->buffer);
            sharedMem->buffer = NULL;
            sharedMem->size = 0;
        }
    }
}

//------------------------------------------------------------------------------
void TEEC_RequestCancellation(
    TEEC_Operation* operation) {
    LOG_D("== %s() ==============", __func__);

    while (operation->started == 0);

    LOG_D("while(operation->started ==0) passed");

    if (operation->started > 1) {
        LOG_D("The operation has finished");
        return;
    }

    TEEC_Session_IMP* session = operation->imp.session;
    operation->started = 2;

    if (!session->active)  {
        LOG_D("Corresponding session is not active");
        return;
    }

    if (client.gpRequestCancellation(session->sessionId)) {
        LOG_ERRNO("gpRequestCancellation");
    }
}

//------------------------------------------------------------------------------