1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include <unistd.h>
13 #include <sys/mman.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include "tst_common.h"
17 #include "test_log.h"
18 #include "tests_phys_map.h"
19 #include "tst_common.h"
20 
21 #ifdef CC_IOT
22 #include "tests_hw_access_iot.h"
23 #else
24 #include "tests_hw_access.h"
25 #endif
26 
27 #ifdef CC_SUPPORT_FIPS
28 #define CC_REE_BASE  0x80000000 // same as shared/hw/ree_include/dx_reg_base_host.h DX_BASE_CC
29 #define CC_REE_AREA_LEN  0x100000
30 #define DX_HOST_GPR0_REG_OFFSET     0xA70UL   // taken from ree_include/dx_host.h
31 
testSetReeFipsError(uint32_t reeError,CCFipsState_t expfipsState)32 int testSetReeFipsError(uint32_t  reeError, CCFipsState_t expfipsState)
33 {
34     uint32_t rc;
35     CCFipsState_t fipsState;
36     uint32_t maxRetries = 20;
37 
38     rc = TestMapRee();
39     if (rc != 0) {
40         return 1;
41     }
42     WRITE_REE_REG(DX_HOST_GPR0_REG_OFFSET, (unsigned int)(reeError));
43 
44     TestMunMapRee();
45 
46     TEST_PRINTF("waiting for fips stat to be %d\n", expfipsState);
47     do {
48         rc = CC_FipsStateGet(&fipsState, NULL);
49         if (rc != 0) {
50             return 1;
51         }
52         usleep(100); // wait 100 milisecond
53         TEST_PRINTF("fips stat is %d\n", fipsState);
54     } while ((fipsState != expfipsState) && (maxRetries-- > 0));
55     if ((maxRetries == 0) && (fipsState != expfipsState)) {
56         return 1;
57     }
58     TEST_PRINTF("doen OK\n");
59     return 0;
60 }
61 #endif
62 
63 /**
64  * The function copies src buffer to dst with reversed bytes order.
65  *
66  * Note: Overlapping is not allowed, besides reversing of the buffer in place.
67  *
68  * @param dst
69  * @param src
70  * @param sizeInBytes
71  */
TST_MemCpyReversed(void * dst_ptr,void * src_ptr,unsigned int sizeInBytes)72 int TST_MemCpyReversed( void* dst_ptr, void* src_ptr, unsigned int sizeInBytes)
73 {
74     unsigned int i;
75     unsigned char *dst, *src;
76 
77     src = (unsigned char *)src_ptr;
78     dst = (unsigned char *)dst_ptr;
79 
80     if (((dst < src) && (dst+sizeInBytes > src)) ||
81         ((src < dst) && (src+sizeInBytes > dst)))
82         return -1;
83 
84     if (dst == src) {
85         unsigned char tmp;
86         for (i=0; i<sizeInBytes/2; i++) {
87             tmp = dst[sizeInBytes-i-1];
88             dst[sizeInBytes-i-1] = src[i];
89             src[i] = tmp;
90         }
91     } else {
92         for (i=0; i<sizeInBytes; i++) {
93             dst[i] = src[sizeInBytes-i-1];
94         }
95     }
96 
97     return 0;
98 }
99 
100 
101 
102 #ifdef CC_IOT
103 
Test_LibInit(void * params)104  void* Test_LibInit(void *params ){
105     uint32_t rc;
106     uint8_t             *pRc =NULL;
107     LibInitArgs* threadArgs = (LibInitArgs*)params;
108 
109     pRc = (uint8_t *)malloc(sizeof(uint8_t));
110     rc = CC_LibInit(threadArgs->rndContext_ptr, threadArgs->rndWorkBuff_ptr);
111     *pRc =rc;
112     return((void *)pRc);
113 
114 }
tests_CC_libInit(CCRndContext_t * rndContext_ptr,CCRndWorkBuff_t * rndWorkBuff_ptr,unsigned long * stackAddress)115 int tests_CC_libInit(CCRndContext_t* rndContext_ptr, CCRndWorkBuff_t * rndWorkBuff_ptr, unsigned long* stackAddress){
116     int rc = 0;
117     void *res;
118     pthread_t threadId;
119     pthread_attr_t threadAttr;
120     LibInitArgs threadArgs;
121     threadArgs.rndContext_ptr=rndContext_ptr;
122     threadArgs.rndWorkBuff_ptr=rndWorkBuff_ptr;
123     rc = pthread_attr_init(&threadAttr);
124 
125     if (rc != 0) {
126         return rc;
127     }
128 
129     rc = pthread_attr_setstack(&threadAttr,  stackAddress, THREAD_STACK_SIZE);
130     if (rc != 0) {
131         return rc;
132     }
133 
134     rc = pthread_create(&threadId, &threadAttr, Test_LibInit, (void *)&threadArgs);
135 
136     rc = pthread_join(threadId, (void**)&res);
137     if (rc != 0) {
138         return rc;
139     }
140 
141     rc = pthread_attr_destroy(&threadAttr);
142 
143     return rc;
144 }
145 
146 #endif
147