#===========================================================================
#  Copyright (c) 2011-2016 QUALCOMM Incorporated.
#  All Rights Reserved.
#  Qualcomm Confidential and Proprietary
#===========================================================================
#
#
# GENERAL DESCRIPTION
#    build script
#                      EDIT HISTORY FOR FILE
#
#  This section contains comments describing changes made to the module.
#  Notice that changes are listed in reverse chronological order.
#
# when       who     what, where, why
# --------   ---     ---------------------------------------------------------
#
#===============================================================================

#-------------------------------------------------------------------------------
# Source PATH
#-------------------------------------------------------------------------------
Import('env')

import os

os.environ['USE_ENCAPSULATED_TID'] = "FALSE"
os.environ['USE_QSEE'] = "TRUE"

env.Append(CPPDEFINES=['USE_QSEE'])
env.Append(CPPDEFINES=['CONFIG_MSM8998'])

if env.has_key('USES_NO_CP'):
  env.Append(CCFLAGS = ' -DUSES_NO_CP ')

#------------------------------------------------------------------------------
# We need to specify "neon" to generate SIMD instructions in 32-bit mode
#------------------------------------------------------------------------------
if env['PROC'] == 'scorpion':
  env.Append(CCFLAGS = " -mfpu=neon ")

env.Append(CPPDEFINES = 'MLDAP_APP=1')

env.Append(CFLAGS = ' -fstack-protector -fstack-protector-all')
env.Append(CCFLAGS = " -Wno-error -Wno-unused-value ")
env.RequirePrivateApi('SECUREMSM')
env.PublishPrivateApi('SSE_COMMON_API', [
   '${INC_ROOT}/core/securemsm/sse/common/include',
])

includes = [
   '#../../core/api/boot/qfprom/',
   '#../../core/api/services',
   '#../../core/api/securemsm/trustzone/qsee',
   '#../../core/api/securemsm/trustzone/gp',
   '#../../core/api/kernel/libstd/stringl',
   '#../../core/securemsm/accesscontrol/api',
   '#../../core/kernel/smmu/ACv3.0/common/inc/',
   '#../../core/securemsm/secrsa/shared/inc',
   '#../../core/securemsm/secmath/shared/inc',
   '#../../core/securemsm/unifiedcrypto/core/ecc/inc',
   '#../../core/securemsm/unifiedcrypto/shared/ecc/inc',
   '#../../core/securemsm/unifiedcrypto/shared/aes/inc',
   '#../../core/securemsm/unifiedcrypto/core/aes/inc',
   '#../../core/securemsm/unifiedcrypto/shared/sha/inc',
   '#../../core/securemsm/unifiedcrypto/core/sha/inc',
   '#../../core/securemsm/trustzone/qsapps/common',
   '#../../core/securemsm/trustzone/qsapps/libs/services/src',
   '#../../core/securemsm/trustzone/qsapps/libs/applib/common_applib/inc',
   '#../../core/securemsm/trustzone/qsapps/libs/applib/common/inc',
   '#../../core/securemsm/trustzone/qsapps/libs/applib/common_applib/inc',
   '#../../core/securemsm/trustzone/qsapps/libs/applib/gp/inc',
   '#../../core/securemsm/trustzone/qsapps/libs/applib/qsee/src',
   '#../../core/securemsm/trustzone/qsee/mink/include',
   '#../../core/securemsm/trustzone/qsapps/common',
   '#../../core/securemsm/trustzone/qsapps/mldap/src',
   '#../../core/securemsm/trustzone/qsapps/mldap/src/x509',
   '#../../core/securemsm/trustzone/qsapps/mldap/src/crypto/include',
   '#../../core/securemsm/trustzone/qsapps/mldap/src/qsee',
   '#../../core/securemsm/trustzone/qsapps/mldap/src/base64',
   '#../../core/securemsm/trustzone/qsapps/mldap/src/wb',
   '#../../core/securemsm/trustzone/qsapps/mldap/src/include',
]

#----------------------------------------------------------------------------
# App core Objects
#----------------------------------------------------------------------------
sources = [
        'qsee/qsee_agent_main.c',
        'CryptoPlatform.c',
        'CommandHandler.c',
        'qsee/CryptoPlatformQSEE.c',
        'qsee/SfsFileOperations.c',
        'common/ServiceName.c',
        'common/TLV.c',

        'crypto/sec_alloc.c',
        'crypto/bn/bn_wrapper.c',
        'crypto/rsa/rsa_wrapper.c',
        'crypto/mem_clr.c',

        'x509/asn1.c',
        'x509/asn1rsa.c',
        'x509/asn1ec.c',
        'x509/asn1gen.c',
        'x509/x509v3.c',
        'x509/da_cert_parcer.c',
        'x509/da_cert_gencer.c',

        'base64/base64.c',

        'crypto/bn/bn_lib.c',
        'crypto/bn/bn_ctx.c',
        'crypto/bn/bn_rand.c',
        'crypto/bn/bn_asm.c',
        'crypto/bn/bn_word.c',
        'crypto/bn/bn_add.c',
        'crypto/bn/bn_mul.c',
        'crypto/bn/bn_mod.c',
        'crypto/bn/bn_div.c',
        'crypto/bn/bn_exp.c',
        'crypto/bn/bn_sqr.c',
        'crypto/bn/bn_shift.c',
        'crypto/bn/bn_mont.c',
        'crypto/bn/bn_recp.c',
        'crypto/bn/bn_gcd.c',
        'crypto/bn/bn_prime.c',
        'crypto/rand/rand_lib.c',
        'crypto/ec/ec_key.c',
        'crypto/ec/ec_lib.c',
        'crypto/ec/ec_curve.c',
        'crypto/ec/ec_cvt.c',
        'crypto/ec/ec_mult.c',
        'crypto/ec/ecp_mont.c',
        'crypto/ec/ecp_smpl.c',
        'crypto/rsa/rsa_gen.c',
        'crypto/rsa/rsa.c',
        'crypto/hash/sha512.c',
]

if env.has_key('USES_NO_CP'):
  sources.extend(
       ['app_content_protection_noship.c',
        'cp_utils.c',
       ]
  )

arm_libs = [
  File(env.SubstRealPath('${MUSL32PATH}/lib/libc.a')),
  File(env.SubstRealPath('${LLVMLIB}/libclang_rt.builtins-arm.a'))
]

target_name = 'mldap'
app_name = 'mldap'

#-------------------------------------------------------------------------------
# Add metadata to image
#-------------------------------------------------------------------------------
md = {
   'appName':    'mldap',
   'privileges': ['default',
                  'I2C',
                  'OEMUnwrapKeys',
                  'CertValidate',
                  'SPI',              
                  'TLMM',
                 ],
}

mldap_units = env.SecureAppBuilder(
  sources = sources,
  includes = includes,
  metadata = md,
  image = target_name,
  user_libs = arm_libs,
  heap_size = '0x10000',
  stack_size = '0x16000'
)

for image in env['IMAGE_ALIASES']:
  op = env.Alias(image, mldap_units)
