/*
 * Copyright (c) 2013-2015 TRUSTONIC LIMITED
 * All rights reserved
 *
 * The present software is the confidential and proprietary information of
 * TRUSTONIC LIMITED. You shall not disclose the present software and shall
 * use it only in accordance with the terms of the license agreement you
 * entered into with TRUSTONIC LIMITED. This software may be subject to
 * export or import laws in certain countries.
 */

#ifndef TLAPIINTERNAL_H_
#define TLAPIINTERNAL_H_

#include "mcLoadFormat.h"

extern mclfTlHeader_t const _stext;

#define MCLIB_ENTRY (((mclfTextHeader_ptr)&_stext.textHeader)->mcLibEntry)

extern uint32_t tlApiWorkBuffer[];

#define TLAPI_SYS_ENTRY (MCLIB_ENTRY)
#define TLAPI_LOG_ENTRY (MCLIB_ENTRY)
#define TLAPI_COM_ENTRY (MCLIB_ENTRY)
#define TLAPI_MARSHAL_ENTRY (MCLIB_ENTRY)
#define TLAPI_SEC_ENTRY (MCLIB_ENTRY)
#define TLAPI_CR_ENTRY (MCLIB_ENTRY)
#define TLAPI_MEM_ENTRY (MCLIB_ENTRY)
#define TLAPI_GP_ENTRY (MCLIB_ENTRY)
#define TLAPI_TUI_ENTRY (MCLIB_ENTRY)
#define TLAPI_TPLAY_ENTRY (MCLIB_ENTRY)

//=============================================================================

// Housekeeping

/*
 * NOTE These values must correspond to the positions of the corresponding
 * functions in the array mcLibTlfptrTable defined in
 * McLib/LibInterface/tlCall.c. If you change one, remember to change the
 * other.
 */

#define TLAPI_FNC_INIT_ID                               0

#define TLAPI_FNC_SYS_GETVERSON                         1
#define TLAPI_FNC_SYS_MC_VERSION                        2
#define TLAPI_FNC_SYS_UNUSED                            3
#define TLAPI_FNC_SYS_EXIT                              4

#define TLAPI_FNC_LOGVPRINTF_ID                         5

// Com
#define TLAPI_FNC_COM_WAIT_NOTIFICATION                 6
#define TLAPI_FNC_COM_NOTIFY                            7

// Marshal
#define TLAPI_FNC_MARSHAL_CALL_DRIVER                   8

// Security
#define TLAPI_FNC_SEC_WRAP                              9
#define TLAPI_FNC_SEC_UNWRAP                            10
#define TLAPI_FNC_SEC_GET_SUID                          11

#define TLAPI_FNC_MARSHAL_CALL_DRIVER_EX                12

// Crypto
#define TLAPI_FNC_CR_ABORT_ID                           13
#define TLAPI_FNC_CR_GENERATERANDOM_ID                  14
#define TLAPI_FNC_CR_GENERATEKEYPAIR_ID                 15

#define TLAPI_FNC_CR_CHIPHER_INIT_ID                    16
#define TLAPI_FNC_CR_CHIPHER_UPDATE_ID                  17
#define TLAPI_FNC_CR_CHIPHER_FINAL_ID                   18

#define TLAPI_FNC_CR_SIGN_INIT_ID                       19
#define TLAPI_FNC_CR_SIGN_UPDATE_ID                     20
#define TLAPI_FNC_CR_SIGN_SIGN_ID                       21
#define TLAPI_FNC_CR_SIGN_VERIFY_ID                     22

#define TLAPI_FNC_CR_DIGEST_INIT_ID                     23
#define TLAPI_FNC_CR_DIGEST_UPDATE_ID                   24
#define TLAPI_FNC_CR_DIGEST_FINAL_ID                    25

#define TLAPI_FNC_SYS_GET_VIRT_MEM_TYPE                 26

#define TLAPI_FNC_SEC_DERIVE_KEY                        27

// Alloc
#define TLAPI_FNC_MEM_ALLOC                             28
#define TLAPI_FNC_MEM_REALLOC                           29
#define TLAPI_FNC_MEM_FREE                              30

// TODO: Do they have to be removed?
#define TLAPI_FNC_CR_DIGEST_WITHOUT_CLOSE_ID            31
#define TLAPI_FNC_CR_OVERWRITE_KEY_ID                   32
#define TLAPI_FNC_CR_MAC_INIT_ID                        33
#define TLAPI_FNC_CR_SIGN_SIGN_WITHOUT_CLOSE_ID         34
#define TLAPI_FNC_CR_SIGN_VERIFY_WITHOUT_CLOSE_ID       35
#define TLAPI_FNC_CR_CIPHER_WITHOUT_CLOSE_ID            36

// GP Entrypoints and memory checks
#define TLAPI_FNC_GP_SKELETON                           37
#define TLAPI_FNC_GP_CHKMEMACCESS                       38
#define TLAPI_FNC_GP_CHKCANCEL                          39
#define TLAPI_FNC_GP_MASKCANCEL                         40

// GP Properties
#define TLAPI_FNC_GP_PROP_GET_BOOL                      41
#define TLAPI_FNC_GP_PROP_GET_U32                       42
#define TLAPI_FNC_GP_PROP_GET_UUID                      43
#define TLAPI_FNC_GP_PROP_GET_BINARY                    44
#define TLAPI_FNC_GP_PROP_GET_STRING                    45

// GP StorageAPI
#define TLAPI_FNC_GP_OPEN_PERSIST_OBJ                   46
#define TLAPI_FNC_GP_CREATE_PERSIST_OBJ                 47
#define TLAPI_FNC_GP_CLOSE_OBJ                          48
#define TLAPI_FNC_GP_CLOSEDELETE_PERSIST_OBJ            49
#define TLAPI_FNC_GP_READ_OBJ_DATA                      50
#define TLAPI_FNC_GP_WRITE_OBJ_DATA                     51
#define TLAPI_FNC_GP_TRUNCATE_OBJ_DATA                  52
#define TLAPI_FNC_GP_SEEK_OBJ_DATA                      53

// GP Crypto
#define TLAPI_FNC_GP_CR_GET_OBJECT_INFO                 54
#define TLAPI_FNC_GP_CR_GET_OBJECT_BUFFER_ATTRIBUTE     55
#define TLAPI_FNC_GP_CR_GET_OBJECT_VALUE_ATTRIBUTE      56
#define TLAPI_FNC_GP_CR_INIT_REF_ATTRIBUTE              57
#define TLAPI_FNC_GP_CR_INIT_VALUE_ATTRIBUTE            58
#define TLAPI_FNC_GP_CR_COPY_OBJECT_ATTRIBUTES          59
#define TLAPI_FNC_GP_CR_ALLOCATE_OPERATION              60
#define TLAPI_FNC_GP_CR_FREE_OPERATION                  61
#define TLAPI_FNC_GP_CR_GET_OPERATION_INFO              62
#define TLAPI_FNC_GP_CR_ALLOCATE_TRANSIENT_OBJECT       63
#define TLAPI_FNC_GP_CR_POPULATE_TRANSIENT_OBJECT       64
#define TLAPI_FNC_GP_CR_FREE_TRANSIENT_OBJECT           65
#define TLAPI_FNC_GP_CR_RESET_TRANSIENT_OBJECT          66
#define TLAPI_FNC_GP_CR_SET_OPERATION_KEY               67
#define TLAPI_FNC_GP_CR_SET_OPERATION_KEY2              68
#define TLAPI_FNC_GP_CR_CIPHER_INIT                     69
#define TLAPI_FNC_GP_CR_CIPHER_UPDATE                   70
#define TLAPI_FNC_GP_CR_CIPHER_DO_FINAL                 71
#define TLAPI_FNC_GP_CR_DIGEST_UPDATE                   72
#define TLAPI_FNC_GP_CR_DIGEST_DO_FINAL                 73
#define TLAPI_FNC_GP_CR_MAC_INIT                        74
#define TLAPI_FNC_GP_CR_MAC_UPDATE                      75
#define TLAPI_FNC_GP_CR_MAC_COMPUTE_FINAL               76
#define TLAPI_FNC_GP_CR_MAC_COMPARE_FINAL               77
#define TLAPI_FNC_GP_CR_ASYMMETRIC_ENCRYPT              78
#define TLAPI_FNC_GP_CR_ASYMMETRIC_DECRYPT              79
#define TLAPI_FNC_GP_CR_ASYMMETRIC_SIGN_DIGEST          80
#define TLAPI_FNC_GP_CR_ASYMMETRIC_VERIFY_DIGEST        81
#define TLAPI_FNC_GP_CR_GENERATE_KEY                    82
#define TLAPI_FNC_GP_CR_GENERATE_RANDOM                 83

// Other core functions
#define TLAPI_FNC_GP_PANIC                              84

#define TLAPI_FNC_SEC_ENDORSE                           85

// Trusted UI
#define TLAPI_FNC_TUI_GET_SCREEN_INFO                   86
#define TLAPI_FNC_TUI_OPEN_SESSION                      87
#define TLAPI_FNC_TUI_CLOSE_SESSION                     88
#define TLAPI_FNC_TUI_SET_IMAGE                         89
#define TLAPI_FNC_TUI_GET_TOUCH_EVENT                   90
#define TLAPI_FNC_TUI_GP_WAIT_TOUCH_EVENT               91

// <t-play
#define TLAPI_FNC_TPLAY_PROCESS_DRM_CONTENT             92
#define TLAPI_FNC_TPLAY_OPEN_SESSION                    93
#define TLAPI_FNC_TPLAY_CLOSE_SESSION                   94
#define TLAPI_FNC_TPLAY_CHECK_LINK                      95

//Personalization
#define TLAPI_FNC_GP_DERIVE_KEY                         96
#define TLAPI_FNC_GP_UNWRAP_OBJECT                      97

//Time
#define TLAPI_FNC_SYS_GET_SECURE_REL_TIME               98

// Entropy
#define TLAPI_FNC_GP_CR_ADD_ENTROPY                     99
#define TLAPI_FNC_CR_ADDENTROPY_ID                      100

//Create new Heap
#define TLAPI_FNC_CREATE_HEAP                           101
#define TLAPI_FNC_CREATE_HEAP_GP                        102

/* Endorse exposed in GP land */
#define TLAPI_FNC_GP_ENDORSE                            103

/* New APIs for handling life cycle management of TAs/SDs */
#define TLAPI_FNC_GP_KILL_TA_INSTANCES                  104
#define TLAPI_FNC_GP_TAKE_IDENTITY                      105
#define TLAPI_FNC_GP_REVERT_IDENTITY                    106
#define TLAPI_FNC_GP_DELETE_ALL_OBJECTS                 107

//Trusted UI
#define TLAPI_FNC_TUI_DRAW_BUFFER                       108
#define TLAPI_FNC_TUI_DRAW_IMAGE                        109
#define TLAPI_FNC_TUI_FLIP_FRAME_BUFFERS                110
#define TLAPI_FNC_TUI_FILL_RECT                         111
#define TLAPI_FNC_TUI_GET_IMAGE_INFO                    112
#define TLAPI_FNC_TUI_DECODE_IMAGE                      113
#define TLAPI_FNC_TUI_GET_RECTANGLE_CLIP                114
#define TLAPI_FNC_TUI_SET_RECTANGLE_CLIP                115
#define TLAPI_FNC_TUI_GET_SCREEN_CONTEXT                116

// Extended <t-play
#define TLAPI_FNC_TPLAY_PROCESS_DRM_EX_CONTENT          117

/* Time API */
#define TLAPI_FNC_GP_GET_SYSTEM_TIME                    118
#define TLAPI_FNC_GP_TEE_WAIT                           119
#define TLAPI_FNC_GP_GET_TA_TIME                        120
#define TLAPI_FNC_GP_SET_TA_TIME                        121
#define TLAPI_FNC_GP_GET_REE_TIME                       122

/* Internal client API */
#define TLAPI_FNC_GP_CLOSE_TA_SESSION                   123
#define TLAPI_FNC_GP_OPEN_TA_SESSION                    124
#define TLAPI_FNC_GP_INVOKE_TA_CMD                      125

/* GP Arithmetical API */
#define TLAPI_FNC_GP_BIG_INT_FMM_CONTEXT_SIZE_IN_U32    126
#define TLAPI_FNC_GP_BIG_INT_FMM_SIZE_IN_U32            127
#define TLAPI_FNC_GP_BIG_INT_INIT                       128
#define TLAPI_FNC_GP_BIG_INT_INIT_FMM_CONTEXT           129
#define TLAPI_FNC_GP_BIG_INT_INIT_FMM                   130
#define TLAPI_FNC_GP_BIG_INT_CONVERT_FROM_OCTET_STRING  131
#define TLAPI_FNC_GP_BIG_INT_CONVERT_TO_OCTET_STRING    132
#define TLAPI_FNC_GP_BIG_INT_CONVERT_FROM_S32           133
#define TLAPI_FNC_GP_BIG_INT_CONVERT_TO_S32             134
#define TLAPI_FNC_GP_BIG_INT_CMP                        135
#define TLAPI_FNC_GP_BIG_INT_CMP_S32                    136
#define TLAPI_FNC_GP_BIG_INT_SHIFT_RIGHT                137
#define TLAPI_FNC_GP_BIG_INT_GET_BIT                    138
#define TLAPI_FNC_GP_BIG_INT_GET_BIT_COUNT              139
#define TLAPI_FNC_GP_BIG_INT_ADD                        140
#define TLAPI_FNC_GP_BIG_INT_SUB                        141
#define TLAPI_FNC_GP_BIG_INT_NEG                        142
#define TLAPI_FNC_GP_BIG_INT_MUL                        143
#define TLAPI_FNC_GP_BIG_INT_SQUARE                     144
#define TLAPI_FNC_GP_BIG_INT_DIV                        145
#define TLAPI_FNC_GP_BIG_INT_MOD                        146
#define TLAPI_FNC_GP_BIG_INT_ADD_MOD                    147
#define TLAPI_FNC_GP_BIG_INT_SUB_MOD                    148
#define TLAPI_FNC_GP_BIG_INT_MUL_MOD                    149
#define TLAPI_FNC_GP_BIG_INT_SQUARE_MOD                 150
#define TLAPI_FNC_GP_BIG_INT_INV_MOD                    151
#define TLAPI_FNC_GP_BIG_INT_RELATIVE_PRIME             152
#define TLAPI_FNC_GP_BIG_INT_COMPUTE_EXTENDED_GCD       153
#define TLAPI_FNC_GP_BIG_INT_IS_PROBABLE_PRIME          154
#define TLAPI_FNC_GP_BIG_INT_CONVERT_TO_FMM             155
#define TLAPI_FNC_GP_BIG_INT_CONVERT_FROM_FMM           156
#define TLAPI_FNC_GP_BIG_INT_COMPUTE_FMM                157

// Other GP Properties
#define TLAPI_FNC_GP_PROP_GET_IDENTITY                  158

/* Diffie--Hellman-style secret agreement */
#define TLAPI_FNC_GP_CR_DERIVE_KEY                      159
#define TLAPI_FNC_CR_AGREE_SECRET_INIT_ID               160
#define TLAPI_FNC_CR_AGREE_SECRET_ID                    161

/* Remaining (introduced by Dragon) Trusted Storage API for Data and Keys */
#define TLAPI_FNC_GP_RENAME_PERSIST_OBJ                 162
#define TLAPI_FNC_GP_ALLOCATE_PERSIST_OBJ_ENUM          163
#define TLAPI_FNC_GP_FREE_PERSIST_OBJ_ENUM              164
#define TLAPI_FNC_GP_RESET_PERSIST_OBJ_ENUM             165
#define TLAPI_FNC_GP_START_PERSIST_OBJ_ENUM             166
#define TLAPI_FNC_GP_GETNEXT_PERSIST_OBJ_ENUM           167
#define TLAPI_FNC_GP_RESTRICT_OBJ_USAGE                 168

#define TLAPI_FNC_GP_TBASE_OPEN_DIRECTORY_OBJ       	169
#define TLAPI_FNC_GP_TBASE_CLOSE_DIRECTORY_OBJ      	170
#define TLAPI_FNC_GP_TBASE_CREATE_PERSIST_OBJ       	171
#define TLAPI_FNC_GP_TBASE_OPEN_PERSIST_OBJ         	172

#define TLAPI_FNC_GP_CR_GET_OBJECT_INFO1                173
#define TLAPI_FNC_GP_RESTRICT_OBJ_USAGE1                174
#define TLAPI_FNC_GP_CLOSEDELETE_PERSIST_OBJ1           175
#define TLAPI_FNC_GP_CR_COPY_OBJECT_ATTRIBUTES1         176

#define TLAPI_FNC_GP_TBASE_START_PERSIST_OBJ_ENUM       177

#define TLAPI_FNC_GP_GET_MY_OTA_IDENTITY                178

/* memory allocation for protected main stack */
#define TLAPI_FNC_STACK_ALLOC                           179

/* GP AE functions */
#define TLAPI_FNC_GP_AE_INIT                            180
#define TLAPI_FNC_GP_AE_UPDATE_AAD                      181
#define TLAPI_FNC_GP_AE_UPDATE                          182
#define TLAPI_FNC_GP_AE_ENCRYPT_FINAL                   183
#define TLAPI_FNC_GP_AE_DECRYPT_FINAL                   184

#define TLAPI_FNC_GP_RESET_OPERATION                    185
#define TLAPI_FNC_GP_CR_COPY_OPERATION                  186
#define TLAPI_FNC_GP_CR_GET_OPERATION_INFO_MULTIPLE     187

// GP Properties enumerators
#define TLAPI_FNC_GP_PROP_ALLOCATE_ENUM                 188
#define TLAPI_FNC_GP_PROP_FREE_ENUM                     189
#define TLAPI_FNC_GP_PROP_START_ENUM                    190
#define TLAPI_FNC_GP_PROP_RESET_ENUM                    191
#define TLAPI_FNC_GP_PROP_GET_NAME                      192
#define TLAPI_FNC_GP_PROP_GET_NEXT                      193

#endif /* TLAPIINTERNAL_H_ */
