1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use libc::*;

pub fn TEEC_PARAM_TYPES(p0:uint32_t, p1:uint32_t, p2:uint32_t, p3:uint32_t) -> uint32_t {
    let tmp = p1 << 4 | p2 << 8 | p3 << 12;
    return p0 | tmp;
}

pub const TEEC_CONFIG_PAYLOAD_REF_COUNT: uint32_t = 4;

pub const TEEC_CONFIG_SHAREDMEM_MAX_SIZE: c_ulong = -1 as c_long as c_ulong;

pub const TEEC_NONE: uint32_t                  = 0x00000000;
pub const TEEC_VALUE_INPUT: uint32_t           = 0x00000001;
pub const TEEC_VALUE_OUTPUT: uint32_t          = 0x00000002;
pub const TEEC_VALUE_INOUT: uint32_t           = 0x00000003;
pub const TEEC_MEMREF_TEMP_INPUT: uint32_t     = 0x00000005;
pub const TEEC_MEMREF_TEMP_OUTPUT: uint32_t    = 0x00000006;
pub const TEEC_MEMREF_TEMP_INOUT: uint32_t     = 0x00000007;
pub const TEEC_MEMREF_WHOLE: uint32_t          = 0x0000000C;
pub const TEEC_MEMREF_PARTIAL_INPUT: uint32_t  = 0x0000000D;
pub const TEEC_MEMREF_PARTIAL_OUTPUT: uint32_t = 0x0000000E;
pub const TEEC_MEMREF_PARTIAL_INOUT: uint32_t  = 0x0000000F;

pub const TEEC_MEM_INPUT: uint32_t  = 0x00000001;
pub const TEEC_MEM_OUTPUT: uint32_t = 0x00000002;

pub const TEEC_SUCCESS: uint32_t               = 0x00000000;
pub const TEEC_ERROR_GENERIC: uint32_t         = 0xFFFF0000;
pub const TEEC_ERROR_ACCESS_DENIED: uint32_t   = 0xFFFF0001;
pub const TEEC_ERROR_CANCEL: uint32_t          = 0xFFFF0002;
pub const TEEC_ERROR_ACCESS_CONFLICT: uint32_t = 0xFFFF0003;
pub const TEEC_ERROR_EXCESS_DATA: uint32_t     = 0xFFFF0004;
pub const TEEC_ERROR_BAD_FORMAT: uint32_t      = 0xFFFF0005;
pub const TEEC_ERROR_BAD_PARAMETERS: uint32_t  = 0xFFFF0006;
pub const TEEC_ERROR_BAD_STATE: uint32_t       = 0xFFFF0007;
pub const TEEC_ERROR_ITEM_NOT_FOUND: uint32_t  = 0xFFFF0008;
pub const TEEC_ERROR_NOT_IMPLEMENTED: uint32_t = 0xFFFF0009;
pub const TEEC_ERROR_NOT_SUPPORTED: uint32_t   = 0xFFFF000A;
pub const TEEC_ERROR_NO_DATA: uint32_t         = 0xFFFF000B;
pub const TEEC_ERROR_OUT_OF_MEMORY: uint32_t   = 0xFFFF000C;
pub const TEEC_ERROR_BUSY: uint32_t            = 0xFFFF000D;
pub const TEEC_ERROR_COMMUNICATION: uint32_t   = 0xFFFF000E;
pub const TEEC_ERROR_SECURITY: uint32_t        = 0xFFFF000F;
pub const TEEC_ERROR_SHORT_BUFFER: uint32_t    = 0xFFFF0010;
pub const TEEC_ERROR_EXTERNAL_CANCEL: uint32_t = 0xFFFF0011;
pub const TEEC_ERROR_TARGET_DEAD: uint32_t     = 0xFFFF3024;

pub const TEEC_ORIGIN_API: uint32_t         = 0x00000001;
pub const TEEC_ORIGIN_COMMS: uint32_t       = 0x00000002;
pub const TEEC_ORIGIN_TEE: uint32_t         = 0x00000003;
pub const TEEC_ORIGIN_TRUSTED_APP: uint32_t = 0x00000004;

pub const TEEC_LOGIN_PUBLIC: uint32_t            = 0x00000000;
pub const TEEC_LOGIN_USER: uint32_t              = 0x00000001;
pub const TEEC_LOGIN_GROUP: uint32_t             = 0x00000002;
pub const TEEC_LOGIN_APPLICATION: uint32_t       = 0x00000004;
pub const TEEC_LOGIN_USER_APPLICATION: uint32_t  = 0x00000005;
pub const TEEC_LOGIN_GROUP_APPLICATION: uint32_t = 0x00000006;

pub type TEEC_Result = uint32_t;

#[repr(C)]
pub struct TEEC_Context {
    pub fd: c_int,
    pub reg_mem: bool,
}

#[repr(C)]
pub struct TEEC_UUID {
    pub timeLow: uint32_t,
    pub timeMid: uint16_t,
    pub timeHiAndVersion: uint16_t,
    pub clockSeqAndNode: [uint8_t; 8],
}

#[repr(C)]
pub struct TEEC_Session {
    pub ctx: *mut TEEC_Context,
    pub session_id: uint32_t,
}

#[repr(C)]
pub struct TEEC_SharedMemory {
    pub buffer: *mut c_void,
    pub size: size_t,
    pub flags: uint32_t,
    pub id: c_int,
    pub alloced_size: size_t,
    pub shadow_buffer: *mut c_void,
    pub registered_fd: c_int,
    pub buffer_allocated: bool,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct TEEC_TempMemoryReference {
    pub buffer: *mut c_void,
    pub size: size_t,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct TEEC_RegisteredMemoryReference {
    pub parent: *mut TEEC_SharedMemory,
    pub size: size_t,
    pub offset: size_t,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct TEEC_Value {
    pub a: uint32_t,
    pub b: uint32_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union TEEC_Parameter {
    pub tmpref: TEEC_TempMemoryReference,
    pub memref: TEEC_RegisteredMemoryReference,
    pub value: TEEC_Value,
}

#[repr(C)]
pub struct TEEC_Operation {
    pub started: uint32_t,
    pub paramTypes: uint32_t,
    pub params: [TEEC_Parameter; TEEC_CONFIG_PAYLOAD_REF_COUNT as usize],
    pub session: *mut TEEC_Session,
}

extern "C" {
    pub fn TEEC_InitializeContext(name: *const c_char, context: *mut TEEC_Context) -> TEEC_Result;
    pub fn TEEC_FinalizeContext(context: *mut TEEC_Context);
    pub fn TEEC_OpenSession(context: *mut TEEC_Context,
                            session: *mut TEEC_Session,
                            destination: *const TEEC_UUID,
                            connectionMethod: uint32_t,
                            connectionData: *const c_void,
                            operation: *mut TEEC_Operation,
                            returnOrigin: *mut uint32_t) -> TEEC_Result;
    pub fn TEEC_CloseSession(session: *mut TEEC_Session);
    pub fn TEEC_InvokeCommand(session: *mut TEEC_Session,
                              commandID: uint32_t,
                              operation: *mut TEEC_Operation,
                              returnOrigin: *mut uint32_t) -> TEEC_Result;
    pub fn TEEC_RegisterSharedMemory(context: *mut TEEC_Context,
                                     sharedMem: *mut TEEC_SharedMemory) -> TEEC_Result;
    pub fn TEEC_AllocateSharedMemory(context: *mut TEEC_Context,
                                     sharedMem: *mut TEEC_SharedMemory) -> TEEC_Result;
    pub fn TEEC_ReleaseSharedMemory(sharedMemory: *mut TEEC_SharedMemory);
    pub fn TEEC_RequestCancellation(operation: *mut TEEC_Operation);
}