1#===============================================================================
2# Copyright (c) 2001-2019, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#===============================================================================
7
8#!/usr/bin/env bash
9PROJ_ROOT=$(pwd)
10MBEDTLS_ROOT_DIR=$PROJ_ROOT/mbedtls
11HOST_PROJ_ROOT=$PROJ_ROOT/host
12MBEDTLS_ALT_API=$PROJ_ROOT/shared/include/mbedtls
13MBEDTLS_PAL_INCDIRS+="$PROJ_ROOT/shared/include $PROJ_ROOT/shared/hw/include $PROJ_ROOT/shared/include/pal $PROJ_ROOT/shared/include/proj/cc3x $PROJ_ROOT/host/src/tests/common"
14MBEDTLS_CFLAGS=" -DUSE_MBEDTLS_CRYPTOCELL -I$MBEDTLS_ALT_API -I$PROJ_ROOT/host/include -I$PROJ_ROOT/shared/include/crypto_api/cc3x/ -I$PROJ_ROOT/shared/include/crypto_api/"
15#=========================== BUILD MBEDTLS LIBRARIES ==============================
16
17
18CORTEX=$ARM_CPU
19
20if [ "$CROSS_COMPILE" == "armcc" ]; then
21	TEE_OS=freertos
22elif [ "$CROSS_COMPILE" == "arm-xilinx-linux-gnueabi-" ]; then
23	TEE_OS=linux
24elif [ "$CROSS_COMPILE" == "arm-none-eabi-" ]; then
25	if [ "$CORTEX" == "cortex-m33" ] || [ "$CORTEX" == "cortex-m3" ]; then
26		TEE_OS=freertos
27	else
28		TEE_OS=no_os
29	fi
30elif [ "$CROSS_COMPILE" == "armclang" ]; then
31	TEE_OS=freertos
32fi
33
34# Set mbedtls_cflags according to the target's operating system (TEE_OS)
35if [ "$TEE_OS" == "freertos" ]; then
36    MBEDTLS_LDFLAGS+="-L$PROJ_ROOT/host/lib/libcc_312.a "
37    MBEDTLS_CFLAGS+=" -DMBEDTLS_CONFIG_FILE='<config-cc312-mps2-freertos.h>' "
38    MBEDTLS_CFLAGS+=" -I$KERNEL_DIR/OS/FreeRTOS/Source/include/"
39    MBEDTLS_PAL_INCDIRS+=" $PROJ_ROOT/shared/include/pal/freertos/"
40
41    if [ "$CROSS_COMPILE" == "armcc" ]; then
42        MBEDTLS_CFLAGS+=" -DARMCM3 "
43        MBEDTLS_CFLAGS+=" -I$KERNEL_DIR/OS/FreeRTOS/Source/portable/ARMCC/ARM_CM3/ "
44        MBEDTLS_CFLAGS+=" --cpu=cortex-m3 "
45    elif [ "$CROSS_COMPILE" == "arm-none-eabi-" ]; then
46        if [ "$CORTEX" == "cortex-m3" ]; then
47            MBEDTLS_CFLAGS +=" -mcpu=cortex-m3 "
48            MBEDTLS_CFLAGS+=" -DARMCM3 "
49            MBEDTLS_CFLAGS+=" -I$KERNEL_DIR/OS/FreeRTOS/Source/portable/GCC/ARM_CM3/ "
50        elif [ "$CORTEX" == "cortex-m33" ]; then
51            MBEDTLS_CFLAGS+=" -march=armv8-m.main "
52            MBEDTLS_CFLAGS+=" -mcmse "
53            MBEDTLS_CFLAGS+=" -DSSE_200 "
54            MBEDTLS_CFLAGS+=" -I$KERNEL_DIR/OS/FreeRTOS/Source/portable/GCC/ARM_CM33/ "
55        fi
56    elif [ "$CROSS_COMPILE" == "armclang" ]; then
57        if [ "$CORTEX" == "cortex-m3" ]; then
58            MBEDTLS_CFLAGS+=" --target=arm-arm-none-eabi -mcpu=cortex-m3 "
59            MBEDTLS_CFLAGS+=" -mlittle-endian -xc "
60            MBEDTLS_CFLAGS+=" -DARMCM3 "
61            MBEDTLS_CFLAGS+=" -I$KERNEL_DIR/OS/FreeRTOS/Source/portable/ARMCLANG/ARM_CM3/ "
62        elif [ "$CORTEX" == "cortex-m33" ]; then
63            MBEDTLS_CFLAGS+=" --target=arm-arm-none-eabi -march=armv8-m.main "
64            MBEDTLS_CFLAGS+=" -mlittle-endian -xc "
65            MBEDTLS_CFLAGS+=" -mcmse "
66            MBEDTLS_CFLAGS+=" -mfpu=none "
67            MBEDTLS_CFLAGS+=" -DSSE_200 "
68            MBEDTLS_CFLAGS+=" -I$KERNEL_DIR/OS/FreeRTOS/Source/portable/ARMCLANG/ARM_CM33/ "
69        fi
70    fi
71fi
72
73if [ "$TEE_OS" == "no_os" ]; then
74    MBEDTLS_CFLAGS+=" -DMBEDTLS_CONFIG_FILE='<config-cc312-mps2-no-os.h>' "
75    MBEDTLS_PAL_INCDIRS+=" $PROJ_ROOT/shared/include/pal/no_os/"
76fi
77
78if [ "$TEE_OS" == "linux" ]; then
79    MBEDTLS_CFLAGS+=" -DMBEDTLS_CONFIG_FILE='<config-cc312.h>' "
80fi
81
82if [ "$CROSS_COMPILE" == "armcc" ]; then
83    export CC="armcc"
84    export AR="armar"
85    MBEDTLS_CFLAGS+=" --thumb "
86
87    # Suppresses license management warning
88    MBEDTLS_CFLAGS+=" --diag_suppress 9931 "
89
90    MBEDTLS_LDFLAGS+="-L$PROJ_ROOT/host/lib/libcc_312.a "
91    MBEDTLS_TESTS_SRCFILES+="$PROJ_ROOT/host/src/tests/common/tests_hw_access_freertos.c $PROJ_ROOT/host/src/tests/common/tests_phys_map_freertos.c"
92
93	if [ -d $MBEDTLS_ROOT_DIR ]; then
94        #surpress warning levels in library makefile as arm-compiler-5 armcc does not seem to supoort it.
95	    sed -i '/WARNING_CFLAGS /d' $MBEDTLS_ROOT_DIR/library/Makefile
96
97	    #change debug flag -g3 to -g to comply with arm-compiler-5 armcc flags
98	    sed -i 's|g3|g| g ' $MBEDTLS_ROOT_DIR/library/Makefile
99    fi
100elif [ "$CROSS_COMPILE" == "arm-xilinx-linux-gnueabi-" ]; then
101
102    export CC="arm-xilinx-linux-gnueabi-gcc"
103    export AR="arm-xilinx-linux-gnueabi-ar"
104
105    MBEDTLS_TESTS_LDFLAGS+="-L../../host/lib -lpal_linux -lcc_312 -lpthread "
106    # as we cannot change mbedtls Makefile, we add these libs before mbedtls* in order to pass link
107    export LOCAL_LDFLAGS+="-L$PROJ_ROOT/host/lib -L$PROJ_ROOT/mbedtls/library -Wl,--start-group -lcc_312 "
108    export LOCAL_LDFLAGS+="-lmbedtls "
109    export LOCAL_LDFLAGS+="-lmbedx509 "
110    export LOCAL_LDFLAGS+="-lmbedcrypto "
111    export LOCAL_LDFLAGS+="-Wl,--end-group "
112
113    MBEDTLS_PAL_INCDIRS+=" $PROJ_ROOT/shared/include/pal/cc_linux/"
114    MBEDTLS_TESTS_SRCFILES+="$PROJ_ROOT/host/src/tests/common/tests_hw_access_iot.c $PROJ_ROOT/host/src/tests/common/tests_phys_map.c"
115
116    # make sure mbedTLS library makefile is untouched (from above section)
117    if [ -d $MBEDTLS_ROOT_DIR ];
118    then
119        cd $MBEDTLS_ROOT_DIR
120        git checkout -q -- $MBEDTLS_ROOT_DIR/library/Makefile
121    fi
122elif [ "$CROSS_COMPILE" == "arm-none-eabi-" ]; then
123    export CC="arm-none-eabi-gcc"
124    export AR="arm-none-eabi-ar"
125    MBEDTLS_CFLAGS+=" -mthumb "
126    MBEDTLS_PAL_INCDIRS+=" $MBEDTLS_ROOT_DIR/include/mbedtls"
127elif [ "$CROSS_COMPILE" == "armclang" ]; then
128    export CC="armclang"
129    export AR="armar"
130    MBEDTLS_CFLAGS+=" -mthumb "
131    MBEDTLS_CFLAGS+=" -Wno-license-management "
132    MBEDTLS_PAL_INCDIRS+=" $MBEDTLS_ROOT_DIR/include/mbedtls "
133else
134    export CC="cc"
135    export AR="ar"
136fi
137
138MBEDTLS_CFLAGS+=-I${MBEDTLS_PAL_INCDIRS// / -I}
139
140if [ "$DEBUG" == "" ]; then
141    MBEDTLS_CFLAGS+=" -O2"
142fi
143
144function mbedtls_git
145{
146    cd $PROJ_ROOT
147    if [ ! -d $MBEDTLS_ROOT_DIR ];
148    then
149        echo "cloning mbedtls into $MBEDTLS_ROOT_DIR"
150        git clone -b "mbedtls-2.16.2"  https://github.com/ARMmbed/mbedtls.git $MBEDTLS_ROOT_DIR
151    else
152        echo "rebase mbedtls $MBEDTLS_ROOT_DIR"
153        cd $MBEDTLS_ROOT_DIR
154        git pull --rebase
155    fi
156}
157
158
159function mbedtls_build_lib
160{
161    cd $MBEDTLS_ROOT_DIR
162    echo CFLAGS=$MBEDTLS_CFLAGS
163    CFLAGS=$MBEDTLS_CFLAGS make -j8 lib
164
165    if [[ $? -ne 0 ]]; then
166        exit 1;
167    fi
168
169    if [[ "$CROSS_COMPILE" == "armcc" ]] || [[ "$CROSS_COMPILE" == "arm-none-eabi-" ]] || [[ "$CROSS_COMPILE" == "armclang" ]]; then
170        sync
171        cd $MBEDTLS_ROOT_DIR
172        cp `find library/ -name *.a` $KERNEL_DIR/lib/
173    fi
174}
175
176#============================== CLEAN MBEDTLS =================================
177function mbedtls_clean
178{
179    cd $PROJ_ROOT/mbedtls
180    make clean
181}
182if [ "$#" == "0" ];
183then
184    echo "Usage: $0 clone|lib|all"
185    exit 1
186fi
187
188MODE=$1
189
190echo Running prepare_mbedtls.sh with $MODE
191if [ "$MODE" == "clone" ] || [ "$MODE" == "all" ]
192then
193    mbedtls_git
194fi
195
196if [ "$MODE" == "lib" ] || [ "$MODE" == "all" ]
197then
198    mbedtls_build_lib
199fi
200
201if [ "$MODE" == "clean" ]
202then
203    mbedtls_clean
204fi
205