mcResult_t tlc_open(mc_comm_ctx *comm_ctx) {
	mcResult_t	mcRet;

	LOG_I("open() called");
	do {
		// -------------------------------------------------------------
		// Step 1: Open the MobiCore device
		LOG_I("Opening MobiCore device");
		mcRet = mcOpenDevice(comm_ctx->device_id);
		if (MC_DRV_OK != mcRet)
			LOG_I("mcOpenDevice result: %d", mcRet);


		// -------------------------------------------------------------
		// Step 2: Allocate WSM buffer for the TCI
		LOG_I("Allocating WSM for TCI");
		mcRet = mcMallocWsm(comm_ctx->device_id, 0, sizeof(tciMessage_t), (uint8_t **)&(comm_ctx->tci_msg), 0);
		if (MC_DRV_OK != mcRet) {
			LOG_E("Allocation of TCI WSM failed: %d", mcRet);
			break;
		}

		// -------------------------------------------------------------
		// Step 3: Open session with the Trustlet
		LOG_I("Opening the session");
		bzero(&(comm_ctx->handle), sizeof(mcSessionHandle_t)); // Clear the session handle

		comm_ctx->handle.deviceId = comm_ctx->device_id; // The device ID (default device is used)

		mcRet = mcOpenSession(&(comm_ctx->handle), &(comm_ctx->uuid), (uint8_t *)(comm_ctx->tci_msg),
				(uint32_t) sizeof(tciMessage_t));
		if (MC_DRV_OK != mcRet) {
			LOG_E("Open session failed: %d", mcRet);
			break;
		}

		LOG_I("tlc_open() succeeded");
	} while (false);

	return mcRet;
}
//------------------------------------------------------------------------------
//TEEC_InitializeContext: TEEC_SUCCESS, Another error code from Table 4-2.
//MC_DRV_OK, MC_DRV_ERR_INVALID_OPERATION, MC_DRV_ERR_DAEMON_UNREACHABLE, MC_DRV_ERR_UNKNOWN_DEVICE, MC_DRV_ERR_INVALID_DEVICE_FILE
TEEC_Result TEEC_InitializeContext(
    const char   *name,
    TEEC_Context *context)
{
    LOG_I("== %s() ==============", __func__);

    if (context == NULL) return TEEC_ERROR_BAD_PARAMETERS;
    context->imp.reserved = MC_DEVICE_ID_DEFAULT;

    switch (mcOpenDevice(MC_DEVICE_ID_DEFAULT)) {
    case MC_DRV_OK:
        return TEEC_SUCCESS;
    case MC_DRV_ERR_DAEMON_UNREACHABLE:
        return TEEC_ERROR_COMMUNICATION;
    case MC_DRV_ERR_UNKNOWN_DEVICE:
        return TEEC_ERROR_BAD_PARAMETERS;
    case MC_DRV_ERR_INVALID_DEVICE_FILE:
        return TEEC_ERROR_COMMUNICATION;
    }

    return TEEC_ERROR_GENERIC;
}
Exemplo n.º 3
0
// -------------------------------------------------------------
mcResult_t tlcOpen(void)
{
    mcResult_t mcRet;
    mcVersionInfo_t versionInfo;
    uint8_t* pTrustletData = NULL;
    uint32_t nTrustletSize;

    printf("Opening <t-base device\n");
    mcRet = mcOpenDevice(DEVICE_ID);
    if (MC_DRV_OK != mcRet) 
    {
	    printf("Error opening device: %d\n", mcRet);
	    return mcRet;
    }

    mcRet = mcGetMobiCoreVersion(MC_DEVICE_ID_DEFAULT, &versionInfo);
    if (MC_DRV_OK != mcRet) 
    {
        printf("mcGetMobiCoreVersion failed %d\n", mcRet);
        mcCloseDevice(DEVICE_ID);
        return mcRet;
    }
    LOG_I("productId        = %s", versionInfo.productId);
    LOG_I("versionMci       = 0x%08X", versionInfo.versionMci);
    LOG_I("versionSo        = 0x%08X", versionInfo.versionSo);
    LOG_I("versionMclf      = 0x%08X", versionInfo.versionMclf);
    LOG_I("versionContainer = 0x%08X", versionInfo.versionContainer);
    LOG_I("versionMcConfig  = 0x%08X", versionInfo.versionMcConfig);
    LOG_I("versionTlApi     = 0x%08X", versionInfo.versionTlApi);
    LOG_I("versionDrApi     = 0x%08X", versionInfo.versionDrApi);
    LOG_I("versionCmp       = 0x%08X", versionInfo.versionCmp);

    tci = (dapc_tciMessage_t*)malloc(sizeof(dapc_tciMessage_t));
    dci = (dapc_tciMessage_t*)malloc(sizeof(dapc_tciMessage_t));
    
    if (tci == NULL) 
    {
        printf("Allocation of TCI failed\n");
        mcCloseDevice(DEVICE_ID);
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }
    memset(tci, 0, sizeof(dapc_tciMessage_t));
    
    if (dci == NULL) 
    {
        printf("Allocation of DCI failed\n");
        mcCloseDevice(DEVICE_ID);
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }
    memset(dci, 0, sizeof(dapc_tciMessage_t));      

    nTrustletSize = getFileContent(
                        "05200000000000000000000000000000.tlbin",
                        &pTrustletData);                      
                        
    if (nTrustletSize == 0) 
    {
        printf("Trustlet not found\n");
        free(tci);
        tci = NULL;
        mcCloseDevice(DEVICE_ID);
        return MC_DRV_ERR_TRUSTLET_NOT_FOUND;
    }

    printf("Opening the session\n");
    memset(&sessionHandle, 0, sizeof(sessionHandle));
    sessionHandle.deviceId = DEVICE_ID; // The device ID (default device is used)
    mcRet = mcOpenTrustlet(
            &sessionHandle,
            MC_SPID_RESERVED_TEST, /* mcSpid_t */
            pTrustletData,
            nTrustletSize,
            (uint8_t *) tci,
            sizeof(dapc_tciMessage_t));
            
    if (MC_DRV_OK != mcRet) 
    {
        printf("Open session failed: %d\n", mcRet);
        free(tci);
        tci = NULL;
        mcCloseDevice(DEVICE_ID);
    }
    else 
    {
        printf("open() succeeded\n");
    }
            
    nTrustletSize = getFileContent(
                        "05190000000000000000000000000000.drbin",
                        &pTrustletData);                      
                        
    if (nTrustletSize == 0) 
    {
        printf("Driver not found\n");
        free(tci);
        tci = NULL;
        mcCloseDevice(DEVICE_ID);
        return MC_DRV_ERR_TRUSTLET_NOT_FOUND;
    }
	
    // Whatever the result is, free the buffer
    free(pTrustletData);

    if (MC_DRV_OK != mcRet) 
    {
        printf("Open session failed: %d\n", mcRet);
        free(tci);
        tci = NULL;
        mcCloseDevice(DEVICE_ID);
    }
    else 
    {
        printf("open() succeeded\n");
    }

    return mcRet;
}