hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 * All rights reserved.
 */
#include <compiler.h>
#include <dlfcn.h>
#include <link.h>
#include <setjmp.h>
#include <stdint.h>
#include <string.h>
#include <ta_crypt.h>
#include <ta_os_test.h>
#include <tee_internal_api_extensions.h>
 
#include "os_test.h"
#include "test_float_subj.h"
#include "os_test_lib.h"
 
enum p_type {
   P_TYPE_BOOL,
   P_TYPE_INT,
   P_TYPE_UUID,
   P_TYPE_IDENTITY,
   P_TYPE_STRING,
   P_TYPE_BINARY_BLOCK,
};
 
struct p_attr {
   const char *str;
   enum p_type type;
   bool retrieved;
};
 
static TEE_Result check_returned_prop(
       int line __maybe_unused, char *prop_name __maybe_unused,
       TEE_Result return_res, TEE_Result expected_res,
       uint32_t return_len, uint32_t expected_len)
{
   if (return_res != expected_res) {
       EMSG("From line %d (property name=%s): return_res=0x%x  vs  expected_res=0x%x",
            line, (prop_name ? prop_name : "unknown"),
            (unsigned int)return_res, (unsigned int)expected_res);
       return TEE_ERROR_GENERIC;
   }
   if (return_len != expected_len) {
       EMSG("From line %d (property name=%s): return_len=%u  vs  expected_res=%u",
            line, (prop_name ? prop_name : "unknown"),
            return_len, expected_len);
       return TEE_ERROR_GENERIC;
   }
   return TEE_SUCCESS;
}
 
static TEE_Result check_binprop_ones(size_t size, char *bbuf, size_t bblen)
{
   char ones[4] = { 0xff, 0xff, 0xff, 0xff };
 
   if (size > 4 || bblen != size) {
       EMSG("Size error (size=%zu, bblen=%zu)", size, bblen);
       return TEE_ERROR_GENERIC;
   }
   if (strncmp(bbuf, ones, bblen)) {
       EMSG("Unexpected content");
       DHEXDUMP(bbuf, bblen);
       return TEE_ERROR_GENERIC;
   }
   return TEE_SUCCESS;
}
 
static TEE_Result get_binblock_property(TEE_PropSetHandle h,
                   char *nbuf __unused, char **bbuf, size_t *bblen)
{
   TEE_Result res = TEE_ERROR_GENERIC;
   uint32_t block_len = 0;
 
   *bbuf = NULL;
   *bblen = 0;
   res = TEE_GetPropertyAsBinaryBlock(h, NULL, *bbuf, &block_len);
 
   if (res == TEE_SUCCESS && !block_len)
       return TEE_SUCCESS;
 
   if (res != TEE_ERROR_SHORT_BUFFER) {
       EMSG("TEE_GetPropertyAsBinaryBlock() size query returned 0x%x",
            (unsigned int)res);
       return res ? res : TEE_ERROR_GENERIC;
   }
 
   *bbuf = TEE_Malloc(block_len, TEE_MALLOC_FILL_ZERO);
   if (!bbuf)
       return TEE_ERROR_OUT_OF_MEMORY;
 
   res = TEE_GetPropertyAsBinaryBlock(h, NULL, *bbuf, &block_len);
   if (res != TEE_SUCCESS)
       EMSG("TEE_GetPropertyAsBinaryBlock(\"%s\") returned 0x%x",
            nbuf, (unsigned int)res);
   else
       *bblen = block_len;
 
   return res;
}
 
static TEE_Result print_properties(TEE_PropSetHandle h,
                  TEE_PropSetHandle prop_set,
                  struct p_attr *p_attrs, size_t num_p_attrs)
{
TEE_Result res = TEE_ERROR_GENERIC;
size_t n = 0;
 
TEE_StartPropertyEnumerator(h, prop_set);
 
while (true) {
   char nbuf[256] = { };
   char nbuf_small[256] = { };
   char vbuf[256] = { };
   char vbuf2[256] = { };
   uint32_t nblen = sizeof(nbuf);
   uint32_t nblen_small = 0;
   uint32_t vblen = sizeof(vbuf);
   uint32_t vblen2 = sizeof(vbuf2);
   char *bbuf = NULL;
   size_t bblen = 0;
 
   res = TEE_GetPropertyName(h, nbuf, &nblen);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_GetPropertyName returned 0x%x\n",
            (unsigned int)res);
       return res;
   }
   if (nblen != strlen(nbuf) + 1) {
       EMSG("Name has wrong size: %u vs %zu", nblen, strlen(nbuf) + 1);
       return TEE_ERROR_GENERIC;
   }
 
 
   /* Get the property name with a very small buffer */
   nblen_small = 2;
   res = TEE_GetPropertyName(h, nbuf_small, &nblen_small);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_ERROR_SHORT_BUFFER,
                 nblen_small, nblen);
   if (res != TEE_SUCCESS)
       return res;
 
   /* Get the property name with almost the correct buffer */
   nblen_small = nblen - 1;
   res = TEE_GetPropertyName(h, nbuf_small, &nblen_small);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_ERROR_SHORT_BUFFER,
                 nblen_small, nblen);
   if (res != TEE_SUCCESS)
       return res;
 
   /* Get the property name with the exact buffer length */
   nblen_small = nblen;
   res = TEE_GetPropertyName(h, nbuf_small, &nblen_small);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS,
                 nblen_small, nblen);
   if (res != TEE_SUCCESS)
       return res;
 
   /* Get the property value */
   res = TEE_GetPropertyAsString(h, NULL, vbuf, &vblen);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS,
                 vblen, strlen(vbuf) + 1);
   if (res != TEE_SUCCESS)
       return res;
 
   res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS,
                 vblen2, strlen(vbuf2) + 1);
   if (res != TEE_SUCCESS)
       return res;
 
   if (strcmp(vbuf, vbuf2) != 0) {
       EMSG("String of \"%s\" differs\n", nbuf);
       return TEE_ERROR_GENERIC;
   }
 
   /* Get the property with a very small buffer */
   if (vblen > 1) {
       vblen2 = 1;
       res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
       res = check_returned_prop(__LINE__, nbuf, res,
                     TEE_ERROR_SHORT_BUFFER,
                     vblen2, vblen);
       if (res != TEE_SUCCESS)
           return res;
   }
 
   /* Get the property with almost the correct buffer */
   vblen2 = vblen - 1;
   res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_ERROR_SHORT_BUFFER,
                 vblen2, vblen);
   if (res != TEE_SUCCESS)
       return res;
 
   /* Get the property name with the exact buffer length */
   vblen2 = vblen;
   res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
   res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS, vblen2, vblen);
   if (res != TEE_SUCCESS)
       return res;
 
   /* check specific myprop.hello property, which is larger than 80 */
   if (!strcmp("myprop.hello", nbuf) &&
       vblen2 != 1 + strlen("hello property, larger than 80 characters, so that it checks that it is not truncated by anything in the source code which may be wrong")) {
       EMSG("TEE_GetPropertyAsString(\"%s\") is truncated - returned \"%s\"\n",
            nbuf, vbuf);
       return TEE_ERROR_GENERIC;
   }
 
   DMSG("Found \"%s\" value \"%s\"\n", nbuf, vbuf);
 
   for (n = 0; n < num_p_attrs; n++) {
       if (strcmp(nbuf, p_attrs[n].str) != 0)
           continue;
 
       if (p_attrs[n].retrieved) {
           EMSG("Value \"%s\" already retrieved\n",
                p_attrs[n].str);
           return TEE_ERROR_GENERIC;
       }
       p_attrs[n].retrieved = true;
 
       switch (p_attrs[n].type) {
       case P_TYPE_BOOL:
           {
               bool v = false;
 
               res =
                   TEE_GetPropertyAsBool(h, NULL, &v);
               if (res != TEE_SUCCESS) {
                   EMSG(
                   "TEE_GetPropertyAsBool(\"%s\") returned 0x%x\n",
                   nbuf, (unsigned int)res);
                   return res;
               }
           }
           break;
 
       case P_TYPE_INT:
           {
               uint32_t v = 0;
 
               res = TEE_GetPropertyAsU32(h, NULL, &v);
               if (res != TEE_SUCCESS) {
                   EMSG(
                   "TEE_GetPropertyAsU32(\"%s\") returned 0x%x\n",
                   nbuf, (unsigned int)res);
                   return res;
               }
           }
           break;
 
       case P_TYPE_UUID:
           {
               TEE_UUID v = { };
 
               res =
                   TEE_GetPropertyAsUUID(h, NULL, &v);
               if (res != TEE_SUCCESS) {
                   EMSG(
                   "TEE_GetPropertyAsUUID(\"%s\") returned 0x%x\n",
                   nbuf, (unsigned int)res);
                   return res;
               }
           }
           break;
 
       case P_TYPE_IDENTITY:
           {
               TEE_Identity v = { };
 
               res =
                   TEE_GetPropertyAsIdentity(h, NULL,
                                 &v);
               if (res != TEE_SUCCESS) {
                   EMSG(
                   "TEE_GetPropertyAsIdentity(\"%s\") returned 0x%x\n",
                   nbuf, (unsigned int)res);
                   return res;
               }
           }
           break;
 
       case P_TYPE_STRING:
           /* Already read as string */
           break;
 
       case P_TYPE_BINARY_BLOCK:
           res = get_binblock_property(h, nbuf, &bbuf, &bblen);
           if (res)
               return res;
 
           if (!strcmp("myprop.binaryblock", nbuf)) {
               const char exp_bin_value[] = "Hello world!";
 
               if (bblen != strlen(exp_bin_value) ||
                   TEE_MemCompare(exp_bin_value, bbuf,
                          bblen)) {
                   EMSG("Binary buffer of \"%s\" differs from \"%s\"",
                        nbuf, exp_bin_value);
                   EMSG("Got \"%s\"", bbuf);
                   return TEE_ERROR_GENERIC;
               }
           } else if (!strcmp("myprop.binaryblock.1byte-ones",
                      nbuf)) {
               res = check_binprop_ones(1, bbuf, bblen);
               if (res)
                   return res;
           } else if (!strcmp("myprop.binaryblock.2byte-ones",
                      nbuf)) {
               res = check_binprop_ones(2, bbuf, bblen);
               if (res)
                   return res;
           } else if (!strcmp("myprop.binaryblock.3byte-ones",
                      nbuf)) {
               res = check_binprop_ones(3, bbuf, bblen);
               if (res)
                   return res;
           } else if (!strcmp("myprop.binaryblock.4byte-ones",
                      nbuf)) {
               res = check_binprop_ones(4, bbuf, bblen);
               if (res)
                   return res;
           } else if (!strcmp("myprop.binaryblock.empty1", nbuf) ||
                  !strcmp("myprop.binaryblock.empty2", nbuf) ||
                  !strcmp("myprop.binaryblock.empty3", nbuf)) {
               if (bblen) {
                   EMSG("Property \"%s\": %zu byte(s)",
                        nbuf, bblen);
                   return TEE_ERROR_GENERIC;
               }
           } else {
               EMSG("Unexpected property \"%s\"", nbuf);
               TEE_Panic(0);
           }
 
           TEE_Free(bbuf);
           break;
 
       default:
           EMSG("Unknown type (%d) for \"%s\"\n",
                p_attrs[n].type, p_attrs[n].str);
           return TEE_ERROR_GENERIC;
       }
   }
 
   res = TEE_GetNextProperty(h);
   if (res != TEE_SUCCESS) {
       if (res == TEE_ERROR_ITEM_NOT_FOUND)
           return TEE_SUCCESS;
       return res;
   }
}
}
 
static TEE_Result test_malloc(void)
{
   void *p = TEE_Malloc(4, 0);
 
   if (p == NULL) {
       EMSG("TEE_Malloc failed\n");
       return TEE_ERROR_OUT_OF_MEMORY;
   }
   TEE_Free(p);
   TEE_Free(NULL);
 
   return TEE_SUCCESS;
}
 
static TEE_Result test_properties(void)
{
   TEE_Result res = TEE_ERROR_GENERIC;
   TEE_PropSetHandle h = TEE_HANDLE_NULL;
   struct p_attr p_attrs[] = {
       {"gpd.ta.appID", P_TYPE_UUID},
       {"gpd.ta.singleInstance", P_TYPE_BOOL},
       {"gpd.ta.multiSession", P_TYPE_BOOL},
       {"gpd.ta.instanceKeepAlive", P_TYPE_BOOL},
       {"gpd.ta.dataSize", P_TYPE_INT},
       {"gpd.ta.stackSize", P_TYPE_INT},
       {"gpd.ta.version", P_TYPE_STRING},
       {"gpd.ta.description", P_TYPE_STRING},
       {"gpd.client.identity", P_TYPE_IDENTITY},
       {"gpd.tee.apiversion", P_TYPE_STRING},
       {"gpd.tee.description", P_TYPE_STRING},
       {"gpd.tee.deviceID", P_TYPE_UUID},
       {"gpd.tee.systemTime.protectionLevel", P_TYPE_INT},
       {"gpd.tee.TAPersistentTime.protectionLevel", P_TYPE_INT},
       {"gpd.tee.arith.maxBigIntSize", P_TYPE_INT},
       {"gpd.tee.cryptography.ecc", P_TYPE_BOOL},
       {"gpd.tee.trustedStorage.antiRollback.protectionLevel", P_TYPE_INT},
       {"gpd.tee.trustedos.implementation.version", P_TYPE_STRING},
       {"gpd.tee.trustedos.implementation.binaryversion", P_TYPE_INT},
       {"gpd.tee.trustedos.manufacturer", P_TYPE_STRING},
       {"gpd.tee.firmware.implementation.version", P_TYPE_STRING},
       {"gpd.tee.firmware.implementation.binaryversion", P_TYPE_INT},
       {"gpd.tee.firmware.manufacturer", P_TYPE_STRING},
       {"myprop.true", P_TYPE_BOOL},
       {"myprop.42", P_TYPE_INT},
       {"myprop.123", P_TYPE_UUID},
       {"myprop.1234", P_TYPE_IDENTITY},
       {"myprop.hello", P_TYPE_STRING},
       {"myprop.binaryblock", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.1byte-ones", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.2byte-ones", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.3byte-ones", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.4byte-ones", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.empty1", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.empty2", P_TYPE_BINARY_BLOCK},
       {"myprop.binaryblock.empty3", P_TYPE_BINARY_BLOCK},
   };
   const size_t num_p_attrs = sizeof(p_attrs) / sizeof(p_attrs[0]);
   size_t n = 0;
 
   res = TEE_AllocatePropertyEnumerator(&h);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_AllocatePropertyEnumerator: returned 0x%x\n",
            (unsigned int)res);
       return TEE_ERROR_GENERIC;
   }
 
   printf("Getting properties for current TA\n");
   res = print_properties(h, TEE_PROPSET_CURRENT_TA, p_attrs, num_p_attrs);
   if (res != TEE_SUCCESS)
       goto cleanup_return;
 
   printf("Getting properties for current client\n");
   res = print_properties(h, TEE_PROPSET_CURRENT_CLIENT, p_attrs,
                  num_p_attrs);
   if (res != TEE_SUCCESS)
       goto cleanup_return;
 
   printf("Getting properties for implementation\n");
   res = print_properties(h, TEE_PROPSET_TEE_IMPLEMENTATION, p_attrs,
                  num_p_attrs);
   if (res != TEE_SUCCESS)
       goto cleanup_return;
 
   for (n = 0; n < num_p_attrs; n++) {
       if (!p_attrs[n].retrieved) {
           EMSG("\"%s\" not retrieved\n", p_attrs[n].str);
           res = TEE_ERROR_GENERIC;
           goto cleanup_return;
       }
   }
 
cleanup_return:
   TEE_FreePropertyEnumerator(h);
   return res;
}
 
static TEE_Result test_mem_access_right(uint32_t param_types,
                   TEE_Param params[4])
{
   static const TEE_UUID test_uuid = TA_OS_TEST_UUID;
   TEE_Result res = TEE_ERROR_GENERIC;
   uint32_t ret_orig = 0;
   uint32_t l_pts = 0;
   TEE_Param l_params[4] = { };
   uint8_t buf[32] = { };
   TEE_TASessionHandle sess = TEE_HANDLE_NULL;
   TEE_UUID *uuid = NULL;
 
   if (param_types !=
       TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT, 0, 0, 0))
       return TEE_ERROR_GENERIC;
 
   /* test access rights on memref parameter */
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
                     TEE_MEMORY_ACCESS_ANY_OWNER,
                     params[0].memref.buffer,
                     params[0].memref.size);
   if (res != TEE_SUCCESS)
       return res;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
                     params[0].memref.buffer,
                     params[0].memref.size);
   if (res != TEE_ERROR_ACCESS_DENIED)
       return TEE_ERROR_GENERIC;
 
   /* test access rights on private read-only and read-write memory */
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
                     (void *)&test_uuid, sizeof(test_uuid));
   if (res != TEE_SUCCESS)
       return res;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_WRITE,
                     (void *)&test_uuid, sizeof(test_uuid));
   if (res == TEE_SUCCESS)
       return TEE_ERROR_GENERIC;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
                     TEE_MEMORY_ACCESS_WRITE,
                     &ret_orig, sizeof(ret_orig));
   if (res != TEE_SUCCESS)
       return res;
 
   uuid = TEE_Malloc(sizeof(*uuid), 0);
   if (!uuid)
       return TEE_ERROR_OUT_OF_MEMORY;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
                     TEE_MEMORY_ACCESS_WRITE,
                     uuid, sizeof(*uuid));
   TEE_Free(uuid);
   if (res != TEE_SUCCESS)
       return res;
 
   /* test access rights on invalid memory (at least lower 256kB) */
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
                     NULL, 1);
   if (res == TEE_SUCCESS)
       return TEE_ERROR_GENERIC;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
                     (void*)(256 * 1024), 1);
   if (res == TEE_SUCCESS)
       return TEE_ERROR_GENERIC;
 
   res = TEE_OpenTASession(&test_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
               &sess, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("test_mem_access_right: TEE_OpenTASession failed\n");
       goto cleanup_return;
   }
 
   l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
               TEE_PARAM_TYPE_MEMREF_INPUT, 0, 0);
   l_params[0].memref.buffer = buf;
   l_params[0].memref.size = sizeof(buf);
   l_params[1].memref.buffer = NULL;
   l_params[1].memref.size = 0;
   res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
                 TA_OS_TEST_CMD_PARAMS_ACCESS,
                 l_pts, l_params, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("test_mem_access_right: TEE_InvokeTACommand failed\n");
       goto cleanup_return;
   }
 
cleanup_return:
   TEE_CloseTASession(sess);
   return res;
}
 
static TEE_Result test_time(void)
{
   TEE_Result res = TEE_ERROR_GENERIC;
   TEE_Time t = { };
   TEE_Time sys_t = { };
   static const TEE_Time null_time = { 0, 0 };
   static const TEE_Time wrap_time = { UINT32_MAX, 999 };
 
   TEE_GetSystemTime(&sys_t);
   printf("system time %u.%03u\n", (unsigned int)sys_t.seconds,
          (unsigned int)sys_t.millis);
 
   TEE_GetREETime(&t);
   printf("REE time %u.%03u\n", (unsigned int)t.seconds,
          (unsigned int)t.millis);
 
   res = TEE_GetTAPersistentTime(&t);
   switch (res) {
   case TEE_SUCCESS:
       printf("Stored TA time %u.%03u\n", (unsigned int)t.seconds,
              (unsigned int)t.millis);
       break;
   case TEE_ERROR_OVERFLOW:
       EMSG("Stored TA time overflowed %u.%03u\n",
            (unsigned int)t.seconds, (unsigned int)t.millis);
       break;
   case TEE_ERROR_TIME_NOT_SET:
       EMSG("TA time not stored\n");
       break;
   case TEE_ERROR_TIME_NEEDS_RESET:
       EMSG("TA time needs reset\n");
       break;
   default:
       return res;
   }
 
   res = TEE_SetTAPersistentTime(&null_time);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_SetTAPersistentTime: failed\n");
       return res;
   }
 
   res = TEE_GetTAPersistentTime(&t);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_GetTAPersistentTime null: failed\n");
       return res;
   }
   printf("TA time %u.%03u\n", (unsigned int)t.seconds,
          (unsigned int)t.millis);
   /*
    * The time between TEE_SetTAPersistentTime() and
    * TEE_GetTAPersistentTime() should be much less than 1 second, in fact
    * it's not even a millisecond.
    */
   if (t.seconds > 1 || t.millis >= 1000) {
       EMSG("Unexpected stored TA time %u.%03u\n",
            (unsigned int)t.seconds, (unsigned int)t.millis);
       return TEE_ERROR_BAD_STATE;
   }
 
   res = TEE_SetTAPersistentTime(&wrap_time);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_SetTAPersistentTime wrap: failed\n");
       return res;
   }
 
   res = TEE_Wait(1000);
   if (res != TEE_SUCCESS)
       EMSG("TEE_Wait wrap: failed\n");
 
   res = TEE_GetTAPersistentTime(&t);
   if (res != TEE_ERROR_OVERFLOW) {
       EMSG("TEE_GetTAPersistentTime: failed\n");
       return TEE_ERROR_BAD_STATE;
   }
   printf("TA time %u.%03u\n", (unsigned int)t.seconds,
          (unsigned int)t.millis);
 
   if (t.seconds > sys_t.seconds) {
       EMSG("Unexpected wrapped time %u.%03u (sys_t %u.%03u)\n",
            (unsigned int)t.seconds, (unsigned int)t.millis,
            (unsigned int)sys_t.seconds, (unsigned int)sys_t.millis);
       return TEE_ERROR_BAD_STATE;
   }
 
   return TEE_SUCCESS;
}
 
#ifdef CFG_TA_FLOAT_SUPPORT
static bool my_dcmpeq(double v1, double v2, double prec)
{
   return v1 > (v2 - prec) && v1 < (v2 + prec);
}
 
static bool my_fcmpeq(float v1, float v2, float prec)
{
   return v1 > (v2 - prec) && v1 < (v2 + prec);
}
 
 
static TEE_Result test_float(void)
{
#define VAL1        2.6
#define VAL1_INT    2
#define VAL2        5.3
#define DPREC        0.000000000000001
#define FPREC        0.000001
#define EXPECT(expr) do { \
       if (!(expr)) { \
           EMSG("Expression %s failed", #expr); \
           return TEE_ERROR_GENERIC; \
       } \
   } while (0)
 
   IMSG("Testing floating point operations");
 
   EXPECT(my_dcmpeq(test_float_dadd(VAL1, VAL2), VAL1 + VAL2, DPREC));
   EXPECT(my_dcmpeq(test_float_ddiv(VAL1, VAL2), VAL1 / VAL2, DPREC));
   EXPECT(my_dcmpeq(test_float_dmul(VAL1, VAL2), VAL1 * VAL2, DPREC));
   EXPECT(my_dcmpeq(test_float_drsub(VAL1, VAL2), VAL2 - VAL1, DPREC));
   EXPECT(my_dcmpeq(test_float_dsub(VAL1, VAL2), VAL1 - VAL2, DPREC));
 
   EXPECT(test_float_dcmpeq(VAL1, VAL1) == 1);
   EXPECT(test_float_dcmplt(VAL1, VAL2) == 1);
   EXPECT(test_float_dcmple(VAL1, VAL1) == 1);
   EXPECT(test_float_dcmpge(VAL1, VAL1) == 1);
   EXPECT(test_float_dcmpgt(VAL2, VAL1) == 1);
 
   EXPECT(my_fcmpeq(test_float_fadd(VAL1, VAL2), VAL1 + VAL2, FPREC));
   EXPECT(my_fcmpeq(test_float_fdiv(VAL1, VAL2), VAL1 / VAL2, FPREC));
   EXPECT(my_fcmpeq(test_float_fmul(VAL1, VAL2), VAL1 * VAL2, FPREC));
   EXPECT(my_fcmpeq(test_float_frsub(VAL1, VAL2), VAL2 - VAL1, FPREC));
   EXPECT(my_fcmpeq(test_float_fsub(VAL1, VAL2), VAL1 - VAL2, FPREC));
 
   EXPECT(test_float_fcmpeq(VAL1, VAL1) == 1);
   EXPECT(test_float_fcmplt(VAL1, VAL2) == 1);
   EXPECT(test_float_fcmple(VAL1, VAL1) == 1);
   EXPECT(test_float_fcmpge(VAL1, VAL1) == 1);
   EXPECT(test_float_fcmpgt(VAL2, VAL1) == 1);
 
   EXPECT(test_float_d2iz(VAL1) == VAL1_INT);
   EXPECT(test_float_d2uiz(VAL1) == VAL1_INT);
   EXPECT(test_float_d2lz(VAL1) == VAL1_INT);
   EXPECT(test_float_d2ulz(VAL1) == VAL1_INT);
 
   EXPECT(test_float_f2iz(VAL1) == VAL1_INT);
   EXPECT(test_float_f2uiz(VAL1) == VAL1_INT);
   EXPECT(test_float_f2lz(VAL1) == VAL1_INT);
   EXPECT(test_float_f2ulz(VAL1) == VAL1_INT);
 
   EXPECT(my_fcmpeq(test_float_d2f(VAL1), VAL1, FPREC));
   EXPECT(my_dcmpeq(test_float_f2d(VAL1), VAL1, FPREC));
 
   EXPECT(my_dcmpeq(test_float_i2d(VAL1_INT), VAL1_INT, DPREC));
   EXPECT(my_dcmpeq(test_float_ui2d(VAL1_INT), VAL1_INT, DPREC));
   EXPECT(my_dcmpeq(test_float_l2d(VAL1_INT), VAL1_INT, DPREC));
   EXPECT(my_dcmpeq(test_float_ul2d(VAL1_INT), VAL1_INT, DPREC));
 
   EXPECT(my_fcmpeq(test_float_i2f(VAL1_INT), VAL1_INT, FPREC));
   EXPECT(my_fcmpeq(test_float_ui2f(VAL1_INT), VAL1_INT, FPREC));
   EXPECT(my_fcmpeq(test_float_l2f(VAL1_INT), VAL1_INT, FPREC));
   EXPECT(my_fcmpeq(test_float_ul2f(VAL1_INT), VAL1_INT, FPREC));
   return TEE_SUCCESS;
}
#else /*CFG_TA_FLOAT_SUPPORT*/
static TEE_Result test_float(void)
{
   IMSG("Floating point disabled");
   return TEE_SUCCESS;
}
#endif /*CFG_TA_FLOAT_SUPPORT*/
 
#if defined(CFG_TA_BGET_TEST)
/* From libutils */
int bget_main_test(void *(*malloc_func)(size_t), void (*free_func)(void *));
 
static void *malloc_wrapper(size_t size)
{
   return tee_map_zi(size, 0);
}
 
static void free_wrapper(void *ptr __unused)
{
}
 
static TEE_Result test_bget(void)
{
   DMSG("Testing bget");
   if (bget_main_test(malloc_wrapper, free_wrapper)) {
       EMSG("bget_main_test failed");
       return TEE_ERROR_GENERIC;
   }
   DMSG("Bget OK");
   return TEE_SUCCESS;
}
#else
static TEE_Result test_bget(void)
{
   IMSG("Bget test disabled");
   return TEE_SUCCESS;
}
#endif
 
 
static __noinline __noreturn void call_longjmp(jmp_buf env)
{
   DMSG("Calling longjmp");
   longjmp(env, 1);
   EMSG("error: longjmp returned to calling function");
}
 
static TEE_Result test_setjmp(void)
{
   jmp_buf env = { };
 
   if (setjmp(env)) {
       IMSG("Returned via longjmp");
       return TEE_SUCCESS;
   } else {
       call_longjmp(env);
       return TEE_ERROR_GENERIC;
   }
}
 
TEE_Result ta_entry_basic(uint32_t param_types, TEE_Param params[4])
{
   TEE_Result res = TEE_ERROR_GENERIC;
 
   printf("ta_entry_basic: enter\n");
 
   res = test_malloc();
   if (res != TEE_SUCCESS)
       return res;
 
   res = test_properties();
   if (res != TEE_SUCCESS)
       return res;
 
   res = test_mem_access_right(param_types, params);
   if (res != TEE_SUCCESS)
       return res;
 
   res = test_time();
   if (res != TEE_SUCCESS)
       return res;
 
   res = test_float();
   if (res != TEE_SUCCESS)
       return res;
 
   res = test_setjmp();
   if (res != TEE_SUCCESS)
       return res;
 
   res = test_bget();
   if (res != TEE_SUCCESS)
       return res;
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_panic(uint32_t param_types, TEE_Param params[4])
{
   volatile bool mytrue = true;
   (void)param_types;
   (void)params;
 
   printf("ta_entry_panic: enter\n");
   /*
    * Somewhat clumsy way of avoiding compile errors if TEE_Panic() has
    * the __noreturn attribute.
    */
   if (mytrue)
       TEE_Panic(0xbeef);
 
   /*
    * Should not be reached, but if it is the testsuite can detect that
    * TEE_Panic() returned instead of panicking the TA.
    */
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_client_with_timeout(uint32_t param_types,
                   TEE_Param params[4])
{
   static const TEE_UUID os_test_uuid = TA_OS_TEST_UUID;
   TEE_Result res = TEE_ERROR_GENERIC;
   TEE_TASessionHandle sess = TEE_HANDLE_NULL;
   uint32_t ret_orig = 0;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE)) {
       EMSG("ta_entry_client_with_timeout: bad parameters\n");
       return TEE_ERROR_BAD_PARAMETERS;
   }
 
   res = TEE_OpenTASession(&os_test_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
               &sess, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG(
       "ta_entry_client_with_timeout: TEE_OpenTASession failed\n");
       return res;
   }
 
   res =
       TEE_InvokeTACommand(sess, params[0].value.a / 2,
               TA_OS_TEST_CMD_WAIT, param_types, params,
               &ret_orig);
 
   if (ret_orig != TEE_ORIGIN_TRUSTED_APP || res != TEE_ERROR_CANCEL) {
       EMSG("ta_entry_client_with_timeout: TEE_InvokeTACommand: "
            "res 0x%x ret_orig 0x%x\n", (unsigned int)res,
            (unsigned int)ret_orig);
       res = TEE_ERROR_GENERIC;
   } else
       res = TEE_SUCCESS;
 
   TEE_CloseTASession(sess);
   return res;
 
}
 
TEE_Result ta_entry_client(uint32_t param_types, TEE_Param params[4])
{
   static const TEE_UUID crypt_uuid = TA_CRYPT_UUID;
   TEE_Result res = TEE_ERROR_GENERIC;
   uint32_t l_pts = 0;
   TEE_Param l_params[4] = { };
   TEE_TASessionHandle sess = TEE_HANDLE_NULL;
   uint32_t ret_orig = 0;
   static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
   static const uint8_t sha256_out[] = {
       0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
       0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
       0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
       0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
   };
   uint8_t out[32] = { 0 };
   void *in = NULL;
 
   (void)param_types;
   (void)params;
 
   printf("ta_entry_client: enter\n");
 
   in = TEE_Malloc(sizeof(sha256_in), 0);
   if (in == NULL)
       return TEE_ERROR_OUT_OF_MEMORY;
   TEE_MemMove(in, sha256_in, sizeof(sha256_in));
 
   res = TEE_OpenTASession(&crypt_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
               &sess, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("ta_entry_client: TEE_OpenTASession failed\n");
       goto cleanup_return;
   }
 
   l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
               TEE_PARAM_TYPE_MEMREF_OUTPUT, 0, 0);
   l_params[0].memref.buffer = in;
   l_params[0].memref.size = sizeof(sha256_in);
   l_params[1].memref.buffer = out;
   l_params[1].memref.size = sizeof(out);
 
   res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
                 TA_CRYPT_CMD_SHA256, l_pts, l_params,
                 &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("ta_entry_client: TEE_InvokeTACommand failed\n");
       goto cleanup_return;
   }
 
   if (TEE_MemCompare(sha256_out, out, sizeof(sha256_out)) != 0) {
       EMSG("ta_entry_client: out parameter failed\n");
       res = TEE_ERROR_GENERIC;
       goto cleanup_return;
   }
 
cleanup_return:
   TEE_Free(in);
   TEE_CloseTASession(sess);
   return res;
}
 
TEE_Result ta_entry_params_access_rights(uint32_t param_types, TEE_Param params[4])
{
   TEE_Result res = TEE_ERROR_GENERIC;
 
   if (param_types !=
       TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
               TEE_PARAM_TYPE_MEMREF_INPUT, 0, 0))
       return TEE_ERROR_GENERIC;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
                     TEE_MEMORY_ACCESS_ANY_OWNER,
                     params[0].memref.buffer,
                     params[0].memref.size);
   if (res != TEE_SUCCESS)
       return res;
 
   res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
                     params[0].memref.buffer,
                     params[0].memref.size);
   if (res != TEE_ERROR_ACCESS_DENIED)
       return TEE_ERROR_GENERIC;
   if (params[1].memref.buffer || params[1].memref.size)
       return TEE_ERROR_BAD_PARAMETERS;
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_wait(uint32_t param_types, TEE_Param params[4])
{
   TEE_Result res = TEE_SUCCESS;
   (void)param_types;
 
   printf("ta_entry_wait: waiting %d\n", (unsigned int)params[0].value.a);
   /* Wait */
   res = TEE_Wait(params[0].value.a);
 
   return res;
}
 
static void undef_instr(void)
{
#if defined(ARM64)
   __asm__(".word 0x0");
#elif defined(ARM32)
   __asm__(".word 0xe7ffffff");
#else
#error "Unsupported architecture"
#endif
}
 
TEE_Result ta_entry_bad_mem_access(uint32_t param_types, TEE_Param params[4])
{
   long int stack = 0;
   long int stack_addr = (long int)&stack;
   void (*volatile null_fn_ptr)(void) = NULL;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, 0, 0, 0) &&
       param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
                      TEE_PARAM_TYPE_MEMREF_INOUT, 0, 0))
       return TEE_ERROR_GENERIC;
 
   switch (params[0].value.a) {
   case 1:
       *((volatile uint32_t *)0) = 0;
       break;
   case 2:
       *((uint32_t *)(stack_addr + 0x40000000)) = 0;
       break;
   case 3:
       null_fn_ptr();
       break;
   case 4:
       ((void (*)(void))(stack_addr + 0x40000000)) ();
       break;
   case 5:
       undef_instr();
       break;
   default:
       break;
   }
 
   return TEE_SUCCESS;
}
 
static void incr_values(size_t bufsize, uint8_t *a, uint8_t *b, uint8_t *c)
{
   size_t i = 0;
 
   for (i = 0; i < bufsize; i++) {
       a[i]++; b[i]++; c[i]++;
   }
}
 
#define TA2TA_BUF_SIZE        (2 * 1024)
TEE_Result ta_entry_ta2ta_memref(uint32_t param_types, TEE_Param params[4])
{
   static const TEE_UUID test_uuid = TA_OS_TEST_UUID;
   TEE_TASessionHandle sess = TEE_HANDLE_NULL;
   TEE_Param l_params[4] = { };
   uint8_t in[TA2TA_BUF_SIZE] = { };
   uint8_t inout[TA2TA_BUF_SIZE] = { };
   uint8_t out[TA2TA_BUF_SIZE] = { };
   TEE_Result res = TEE_ERROR_GENERIC;
   uint32_t ret_orig = 0;
   uint32_t l_pts = 0;
   size_t i = 0;
 
   (void)params;
 
   if (param_types != TEE_PARAM_TYPES(0, 0, 0, 0))
       return TEE_ERROR_GENERIC;
 
   res = TEE_OpenTASession(&test_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
               &sess, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_OpenTASession failed");
       goto cleanup_return;
   }
 
   l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
               TEE_PARAM_TYPE_MEMREF_INOUT,
               TEE_PARAM_TYPE_MEMREF_OUTPUT, 0);
   l_params[0].memref.buffer = in;
   l_params[0].memref.size = TA2TA_BUF_SIZE;
   l_params[1].memref.buffer = inout;
   l_params[1].memref.size = TA2TA_BUF_SIZE;
   l_params[2].memref.buffer = out;
   l_params[2].memref.size = TA2TA_BUF_SIZE;
 
   /* Initialize buffers */
   for (i = 0; i < TA2TA_BUF_SIZE; i++) {
       in[i] = 5;
       inout[i] = 10;
       out[i] = 0;
   }
 
   /*
    * TA will compute: out = ++inout + in
    * Expected values after this step: in: 5, inout: 11, out: 16
    */
   res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
                 TA_OS_TEST_CMD_TA2TA_MEMREF_MIX,
                 l_pts, l_params, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_InvokeTACommand failed");
       goto cleanup_return;
   }
 
   /*
    * Increment all values by one.
    * Expected values after this step: in: 6, inout: 12, out: 17
    */
   incr_values(TA2TA_BUF_SIZE, in, inout, out);
 
   /*
    * TA will compute: out = ++inout + in
    * Expected values after this step: in: 6, inout: 13, out: 19
    */
   res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
                 TA_OS_TEST_CMD_TA2TA_MEMREF_MIX,
                 l_pts, l_params, &ret_orig);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_InvokeTACommand failed");
       goto cleanup_return;
   }
 
   /* Check the actual values */
   for (i = 0; i < TA2TA_BUF_SIZE; i++) {
       if (in[i] != 6 || inout[i] != 13 || out[i] != 19) {
           EMSG("Unexpected value in buffer(s)");
           DHEXDUMP(in, TA2TA_BUF_SIZE);
           DHEXDUMP(inout, TA2TA_BUF_SIZE);
           DHEXDUMP(out, TA2TA_BUF_SIZE);
           return TEE_ERROR_GENERIC;
       }
   }
 
cleanup_return:
   TEE_CloseTASession(sess);
   return res;
}
 
TEE_Result ta_entry_ta2ta_memref_mix(uint32_t param_types, TEE_Param params[4])
{
   uint8_t *in = NULL;
   uint8_t *inout = NULL;
   uint8_t *out = NULL;
   size_t bufsize = 0;
   size_t i = 0;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                      TEE_PARAM_TYPE_MEMREF_INOUT,
                      TEE_PARAM_TYPE_MEMREF_OUTPUT, 0))
       return TEE_ERROR_GENERIC;
 
   bufsize = params[0].memref.size;
   if (params[1].memref.size != bufsize ||
       params[2].memref.size != bufsize)
       return TEE_ERROR_GENERIC;
 
   in = params[0].memref.buffer;
   inout = params[1].memref.buffer;
   out = params[2].memref.buffer;
 
   for (i = 0; i < bufsize; i++)
       out[i] = ++inout[i] + in[i];
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_params(uint32_t param_types, TEE_Param params[4])
{
   size_t n = 0;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                      TEE_PARAM_TYPE_MEMREF_INPUT,
                      TEE_PARAM_TYPE_MEMREF_OUTPUT,
                      TEE_PARAM_TYPE_MEMREF_OUTPUT))
       return TEE_ERROR_BAD_PARAMETERS;
 
   for (n = 0; n < TEE_NUM_PARAMS; n++)
       if (!params[n].memref.buffer || !params[n].memref.size)
           return TEE_ERROR_BAD_PARAMETERS;
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_null_memref(uint32_t param_types, TEE_Param params[4])
{
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                      TEE_PARAM_TYPE_MEMREF_INPUT,
                      TEE_PARAM_TYPE_MEMREF_OUTPUT,
                      TEE_PARAM_TYPE_MEMREF_OUTPUT))
       return TEE_ERROR_BAD_PARAMETERS;
 
   /*
    * Tests how client can provide null or non-null memref parameters
    * param[0] expected as a 0 byte input mapped memeref.
    * param[1] expected as a 0 byte input not-mapped memeref.
    * param[2] expected as a 0 byte output mapped memeref.
    * param[3] expected as a 0 byte output not-mapped memeref.
    */
   if (!params[0].memref.buffer || params[0].memref.size ||
       params[1].memref.buffer || params[1].memref.size ||
       !params[2].memref.buffer || params[2].memref.size ||
       params[3].memref.buffer || params[3].memref.size)
       return TEE_ERROR_BAD_PARAMETERS;
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_call_lib(uint32_t param_types,
                TEE_Param params[4] __unused)
{
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   if (os_test_shlib_add(1, 2) != 3)
       return TEE_ERROR_GENERIC;
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_call_lib_panic(uint32_t param_types,
                  TEE_Param params[4] __unused)
{
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   os_test_shlib_panic();
 
   return TEE_ERROR_GENERIC;
}
 
TEE_Result ta_entry_call_lib_dl(uint32_t param_types __maybe_unused,
               TEE_Param params[4] __unused)
{
   int (*add_func)(int a, int b) = NULL;
   TEE_Result res = TEE_ERROR_GENERIC;
   void *handle = NULL;
   void *hnull = NULL;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   handle = dlopen("b3091a65-9751-4784-abf7-0298a7cc35ba",
           RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
   if (!handle)
       return TEE_ERROR_GENERIC;
 
   add_func = dlsym(handle, "os_test_shlib_dl_add");
   if (!add_func)
       goto err;
   if (add_func(3, 4) != 7)
       goto err;
 
   hnull = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
   if (!hnull)
       goto err;
 
   add_func = dlsym(hnull, "os_test_shlib_dl_add");
   if (!add_func)
       goto err;
   if (add_func(5, 6) != 11)
       goto err;
 
   res = TEE_SUCCESS;
   dlclose(hnull);
err:
   dlclose(handle);
   return res;
}
 
TEE_Result ta_entry_call_lib_dl_panic(uint32_t param_types __maybe_unused,
                     TEE_Param params[4] __unused)
{
   int (*panic_func)(void) = NULL;
   void *handle = NULL;
   TEE_Result res = TEE_ERROR_GENERIC;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   handle = dlopen("b3091a65-9751-4784-abf7-0298a7cc35ba",
           RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
   if (!handle)
       return res;
 
   panic_func = dlsym(handle, "os_test_shlib_dl_panic");
   if (!panic_func)
       goto err;
   panic_func();
   return TEE_ERROR_GENERIC;
err:
   dlclose(handle);
   return res;
}
 
/* ELF initialization/finalization test */
 
volatile int os_test_global;
 
static void __attribute__((constructor)) os_test_init(void)
{
   os_test_global *= 10;
   os_test_global += 1;
   DMSG("os_test_global=%d", os_test_global);
}
 
TEE_Result ta_entry_get_global_var(uint32_t param_types, TEE_Param params[4])
{
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   params[0].value.a = os_test_global;
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_client_identity(uint32_t param_types, TEE_Param params[4])
{
   TEE_Result res = TEE_ERROR_GENERIC;
   TEE_Identity identity = { };
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
                      TEE_PARAM_TYPE_MEMREF_OUTPUT,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   if (params[1].memref.size < sizeof(TEE_UUID)) {
       params[1].memref.size = sizeof(TEE_UUID);
       return TEE_ERROR_SHORT_BUFFER;
   }
 
   res = TEE_GetPropertyAsIdentity(TEE_PROPSET_CURRENT_CLIENT,
                   "gpd.client.identity", &identity);
   if (res != TEE_SUCCESS) {
       EMSG("TEE_GetPropertyAsIdentity: returned %#"PRIx32, res);
       return res;
   }
 
   params[0].value.a = identity.login;
   memcpy(params[1].memref.buffer, &identity.uuid, sizeof(TEE_UUID));
   params[1].memref.size = sizeof(TEE_UUID);
 
   return res;
}
 
#if defined(WITH_TLS_TESTS)
__thread int os_test_tls_a;
__thread int os_test_tls_b = 42;
 
TEE_Result ta_entry_tls_test_main(void)
{
   if (os_test_tls_a != 0) {
       EMSG("os_test_tls_a=%d, expected 0", os_test_tls_a);
       return TEE_ERROR_GENERIC;
   }
   if (os_test_tls_b != 42) {
       EMSG("os_test_tls_b=%d, expected 42", os_test_tls_b);
       return TEE_ERROR_GENERIC;
   }
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_tls_test_shlib(void)
{
   if (os_test_shlib_tls_a != 0) {
       EMSG("os_test_shlib_tls_a=%d, expected 0", os_test_shlib_tls_a);
       return TEE_ERROR_GENERIC;
   }
   if (os_test_shlib_tls_b != 123) {
       EMSG("os_test_shlib_tls_b=%d, expected 123",
            os_test_shlib_tls_b);
       return TEE_ERROR_GENERIC;
   }
 
   return TEE_SUCCESS;
}
#else
TEE_Result ta_entry_tls_test_main(void)
{
   return TEE_ERROR_NOT_SUPPORTED;
}
 
TEE_Result ta_entry_tls_test_shlib(void)
{
   return TEE_ERROR_NOT_SUPPORTED;
}
#endif
 
static int iterate_hdr_cb(struct dl_phdr_info *info __maybe_unused,
             size_t size __unused, void *data)
{
   int *count = data;
 
   (*count)++;
   IMSG("ELF module index: %d", *count);
   IMSG(" dlpi_addr=%p", (void *)info->dlpi_addr);
   IMSG(" dlpi_name='%s'", info->dlpi_name);
   IMSG(" dlpi_phdr=%p", (void *)info->dlpi_phdr);
   IMSG(" dlpi_phnum=%hu", info->dlpi_phnum);
   IMSG(" dlpi_adds=%llu", info->dlpi_adds);
   IMSG(" dlpi_subs=%llu", info->dlpi_subs);
   IMSG(" dlpi_tls_modid=%zu", info->dlpi_tls_modid);
   IMSG(" dlpi_tls_data=%p", info->dlpi_tls_data);
 
   return 123;
}
 
static TEE_Result expect_dl_count_ge(size_t exp_count)
{
   int st = 0;
   size_t count = 0;
 
   st = dl_iterate_phdr(iterate_hdr_cb, (void *)&count);
   if (st != 123) {
       /*
        * dl_iterate_phdr() should return the last value returned by
        * the callback
        */
       EMSG("Expected return value 123, got %d", st);
       return TEE_ERROR_GENERIC;
   }
   if (count < exp_count) {
       /*
        * Expect >= and not == since there could be more shared
        * libraries (for instance, CFG_ULIBS_SHARED=y)
        */
       EMSG("Expected count > %zu, got: %zu", exp_count, count);
       return TEE_ERROR_GENERIC;
   }
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_dl_phdr(void)
{
   return expect_dl_count_ge(2);
}
 
TEE_Result ta_entry_dl_phdr_dl(void)
{
   TEE_Result res = TEE_ERROR_GENERIC;
   void *handle = NULL;
 
   handle = dlopen("b3091a65-9751-4784-abf7-0298a7cc35ba",
           RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
   if (!handle)
       return TEE_ERROR_GENERIC;
 
   res = expect_dl_count_ge(3);
   dlclose(handle);
 
   return res;
}