1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3 * Copyright (c) 2015-2018, Intel Corporation
4 *
5 * Copyright 2015, Andreas Fuchs @ Fraunhofer SIT
6 *
7 * All rights reserved.
8 ***********************************************************************/
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <inttypes.h>
15
16 #include "tss2_common.h"
17 #include "tss2_tpm2_types.h"
18 #include "tss2_mu.h"
19
20 #include "sysapi_util.h"
21 #define LOGMODULE sys
22 #include "util/log.h"
23
24 static const TSS2_ABI_VERSION CURRENT = TSS2_ABI_VERSION_CURRENT;
25 #define CURRENT_CREATOR (CURRENT.tssCreator)
26 #define CURRENT_FAMILY (CURRENT.tssFamily)
27 #define CURRENT_LEVEL (CURRENT.tssLevel)
28 #define CURRENT_VERSION (CURRENT.tssVersion)
29
Tss2_Sys_Initialize(TSS2_SYS_CONTEXT * sysContext,size_t contextSize,TSS2_TCTI_CONTEXT * tctiContext,TSS2_ABI_VERSION * abiVersion)30 TSS2_RC Tss2_Sys_Initialize(
31 TSS2_SYS_CONTEXT *sysContext,
32 size_t contextSize,
33 TSS2_TCTI_CONTEXT *tctiContext,
34 TSS2_ABI_VERSION *abiVersion)
35 {
36 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
37
38 if (!ctx || !tctiContext)
39 return TSS2_SYS_RC_BAD_REFERENCE;
40
41 if (contextSize < sizeof(_TSS2_SYS_CONTEXT_BLOB))
42 return TSS2_SYS_RC_INSUFFICIENT_CONTEXT;
43
44 if (!TSS2_TCTI_TRANSMIT (tctiContext) ||
45 !TSS2_TCTI_RECEIVE (tctiContext))
46 return TSS2_SYS_RC_BAD_TCTI_STRUCTURE;
47
48 /* Checks for ABI negotiation. */
49 if (abiVersion != NULL &&
50 (abiVersion->tssCreator != CURRENT_CREATOR ||
51 abiVersion->tssFamily != CURRENT_FAMILY ||
52 abiVersion->tssLevel != CURRENT_LEVEL ||
53 abiVersion->tssVersion != CURRENT_VERSION)) {
54 LOG_ERROR("ABI-Version of application %" PRIx32 ".%" PRIu32 ".%"
55 PRIu32 ".%" PRIu32 " differs from ABI version of SAPI %"
56 PRIx32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32,
57 abiVersion->tssCreator, abiVersion->tssFamily,
58 abiVersion->tssLevel, abiVersion->tssVersion,
59 CURRENT_CREATOR, CURRENT_FAMILY,
60 CURRENT_LEVEL, CURRENT_VERSION);
61 return TSS2_SYS_RC_ABI_MISMATCH;
62 }
63
64 ctx->tctiContext = tctiContext;
65 InitSysContextPtrs(ctx, contextSize);
66 InitSysContextFields(ctx);
67 ctx->previousStage = CMD_STAGE_INITIALIZE;
68
69 return TSS2_RC_SUCCESS;
70 }
71