1 /*
2  * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "mbedtls/build_info.h"
8 
9 #if defined(MBEDTLS_PLATFORM_C)
10 #include "mbedtls/platform.h"
11 #else
12 #include <stdio.h>
13 #define mbedtls_printf printf
14 #define mbedtls_calloc calloc
15 #define mbedtls_free   free
16 #endif
17 
18 #include "mbedtls/platform.h"
19 
20 #include <pthread.h>
21 #include "tests_phys_map.h"
22 #include "tests_hw_access_iot.h"
23 #include "tst_common.h"
24 #include "cc_lib.h"
25 
Test_LibInit(void * params)26 void* Test_LibInit(void *params ){
27    uint32_t rc;
28    CClibRetCode_t *pRc =NULL;
29    LibInitArgs* threadArgs = (LibInitArgs*)params;
30 
31    pRc = (CClibRetCode_t *)malloc(sizeof(CClibRetCode_t));
32    if( pRc == NULL){
33        return((void *)pRc);
34    }
35    rc = CC_LibInit(threadArgs->rndContext_ptr, threadArgs->rndWorkBuff_ptr);
36    *pRc =rc;
37 
38    return((void *)pRc);
39 }
40 
tests_CC_libInit(CCRndContext_t * rndContext_ptr,CCRndWorkBuff_t * rndWorkBuff_ptr,unsigned long * stackAddress)41 int tests_CC_libInit(CCRndContext_t* rndContext_ptr, CCRndWorkBuff_t * rndWorkBuff_ptr, unsigned long* stackAddress){
42     uint32_t rc = 0;
43     void *res;
44     pthread_t threadId;
45     pthread_attr_t threadAttr;
46     LibInitArgs threadArgs;
47 
48     threadArgs.rndContext_ptr=rndContext_ptr;
49     threadArgs.rndWorkBuff_ptr=rndWorkBuff_ptr;
50 
51     rc = pthread_attr_init(&threadAttr);
52     mbedtls_printf( "\n'pthread_attr_init' result = %d \n", rc );
53     if (rc != 0) {
54         goto EndThread_2;
55     }
56 
57     rc = pthread_attr_setstack(&threadAttr,  stackAddress, THREAD_STACK_SIZE);
58     mbedtls_printf( "\n'pthread_attr_setstack' result = %d \n", rc );
59     if (rc != 0) {
60         goto EndThread;
61     }
62 
63     rc = pthread_create(&threadId, &threadAttr, Test_LibInit, (void *)&threadArgs);
64     mbedtls_printf( "\n'pthread_create' result = %d \n", rc );
65     if (rc != 0) {
66         goto EndThread;
67     }
68     rc = pthread_join(threadId, (void**)&res);
69     mbedtls_printf( "\n'pthread_join' result = %d \n", rc );
70     if (rc != 0 || res == NULL) {
71         goto EndThread;
72     }
73 
74     rc = *((int *)*&res);
75     mbedtls_printf( "\n'res' result = %d \n", rc );
76 
77 EndThread:
78     pthread_attr_destroy(&threadAttr);
79     free(res);
80 EndThread_2:
81     return rc;
82 }
83 
84 //uint32_t platform_start(void)
mbedtls_platform_setup(mbedtls_platform_context * ctx)85 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
86 {
87    uint32_t rc = 0;
88    zynqPlatTestContext_t *pPlatTestCtx = NULL;
89 
90    rc = initPlatform();
91 
92    mbedtls_printf( "\n'initPlatform' result = %d \n", rc );
93 
94    if (rc != 0){
95        return rc;
96    }
97 
98    /* set pointer to user context */
99    pPlatTestCtx = (zynqPlatTestContext_t *)ctx;
100 
101    pPlatTestCtx->rndContext_ptr = (CCRndContext_t *)g_testHwRndCtxBaseAddr;
102    pPlatTestCtx->rndWorkBuff_ptr = (CCRndWorkBuff_t *)(g_testHwRndWorkBuffBaseAddr);
103    pPlatTestCtx->rndContext_ptr->rndState = (void *)(g_testHwRndCtxBaseAddr+sizeof(CCRndContext_t));
104    pPlatTestCtx->rndContext_ptr->entropyCtx = (void *)(g_testHwRndCtxBaseAddr+sizeof(CCRndContext_t)+sizeof(CCRndState_t));
105    rc = tests_CC_libInit(pPlatTestCtx->rndContext_ptr, pPlatTestCtx->rndWorkBuff_ptr, (unsigned long*)g_testHwUserStackBaseAddr);
106 
107    mbedtls_printf( "\n'tests_CC_libInit' result = %d \n", rc );
108 
109    return rc;
110 }
111 
112 //void platform_stop(void)
mbedtls_platform_teardown(mbedtls_platform_context * ctx)113 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
114 {
115     zynqPlatTestContext_t *pPlatTestCtx = NULL;
116 
117     pPlatTestCtx = (zynqPlatTestContext_t *)ctx;
118 
119     CC_LibFini(pPlatTestCtx->rndContext_ptr);
120     freePlatform();
121 }
122 
123 
124 
125 
126 
127 
128 
129