blob: c93d802226ebef1496e55175611c17ecbc1bf594 [file] [log] [blame]
unknown7f43bd52015-09-01 09:48:13 -04001//**********************************************************************;
2// Copyright (c) 2015, Intel Corporation
3// All rights reserved.
Philip Tricca1ea84a52015-11-19 18:07:06 -08004//
5// Redistribution and use in source and binary forms, with or without
unknown7f43bd52015-09-01 09:48:13 -04006// modification, are permitted provided that the following conditions are met:
Philip Tricca1ea84a52015-11-19 18:07:06 -08007//
8// 1. Redistributions of source code must retain the above copyright notice,
unknown7f43bd52015-09-01 09:48:13 -04009// this list of conditions and the following disclaimer.
Philip Tricca1ea84a52015-11-19 18:07:06 -080010//
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
unknown7f43bd52015-09-01 09:48:13 -040013// and/or other materials provided with the distribution.
Philip Tricca1ea84a52015-11-19 18:07:06 -080014//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
unknown7f43bd52015-09-01 09:48:13 -040025// THE POSSIBILITY OF SUCH DAMAGE.
26//**********************************************************************;
27
Philip Tricca364c0ce2016-07-23 16:48:37 -070028#include "sapi/tpm20.h"
unknown7f43bd52015-09-01 09:48:13 -040029#include <stdio.h>
30#include <stdlib.h>
Philip Triccac3dedc22016-01-15 13:47:22 -080031#include "sysapi_util.h"
unknown7f43bd52015-09-01 09:48:13 -040032
unknown7f43bd52015-09-01 09:48:13 -040033// Get the TPM 2.0 commands supported by the TPM.
34TSS2_RC GetCommands( TSS2_SYS_CONTEXT *resMgrSysContext, TPML_CCA **supportedCommands )
35{
36 UINT32 numCommands;
37 TPMI_YES_NO moreData;
38 TPMS_CAPABILITY_DATA capabilityData;
39 TPMA_CC *commandPtr;
40 TSS2_RC rval = TSS2_RC_SUCCESS;
41 UINT32 i;
Philip Tricca4c1b4a02016-06-21 05:44:51 -070042
unknown7f43bd52015-09-01 09:48:13 -040043 // First get the number of commands
44 rval = Tss2_Sys_GetCapability( resMgrSysContext, 0,
45 TPM_CAP_TPM_PROPERTIES, TPM_PT_TOTAL_COMMANDS,
46 1, 0, &capabilityData, 0 );
47 if( rval == TPM_RC_SUCCESS &&
48 capabilityData.capability == TPM_CAP_TPM_PROPERTIES &&
49 capabilityData.data.tpmProperties.count == 1 &&
50 capabilityData.data.tpmProperties.tpmProperty[0].property == TPM_PT_TOTAL_COMMANDS )
51 {
52 numCommands = capabilityData.data.tpmProperties.tpmProperty[0].value;
53 }
54 else
55 {
56 goto returnFromGetCommands;
57 }
58
59 // Allocate memory for them
Philip Tricca71800292016-06-21 05:02:17 -070060 *supportedCommands = (TPML_CCA *)malloc( numCommands * sizeof( TPMA_CC ) + sizeof( UINT32 ) );
unknown7f43bd52015-09-01 09:48:13 -040061 if( !*supportedCommands )
62 {
Philip Tricca4c1b4a02016-06-21 05:44:51 -070063 rval = TSS2_BASE_RC_INSUFFICIENT_BUFFER + TSS2_RESMGR_ERROR_LEVEL;
unknown7f43bd52015-09-01 09:48:13 -040064 goto returnFromGetCommands;
65 }
66
67 for( commandPtr = &( ( *supportedCommands )->commandAttributes[0] ), ( *supportedCommands )->count = 0;
68 ( *supportedCommands )->count < numCommands;
69 ( *supportedCommands )->count += capabilityData.data.command.count,
70 commandPtr += capabilityData.data.command.count )
71 {
72 // Now get the command structures for all of them.
73 rval = Tss2_Sys_GetCapability( resMgrSysContext, 0,
74 TPM_CAP_COMMANDS, TPM_CC_FIRST,
75 numCommands, &moreData, &capabilityData, 0 );
76
77 if( rval == TPM_RC_SUCCESS &&
78 capabilityData.capability == TPM_CAP_COMMANDS &&
79 capabilityData.data.command.count >= 1 )
80 {
81 for( i = 0; i < capabilityData.data.command.count; i ++ )
82 {
83 commandPtr[ ( *supportedCommands )->count + i ].val =
84 capabilityData.data.command.commandAttributes[i].val;
85 }
86 }
87 else
88 {
89 break;
90 }
91 }
Philip Tricca4c1b4a02016-06-21 05:44:51 -070092
unknown7f43bd52015-09-01 09:48:13 -040093returnFromGetCommands:
94 return rval;
95}
96
97//
98// Searches in a list for command attributes bit field for a command code.
99// Assumes that the supportedCommands structure has been populated by
100// calling GetCommands beforehand.
101//
102// Returns:
103// 1, if found. In this case, cmdAttributes points to the attributes bit field.
104// 0, if not found. This means that the TPM doesn't support this command.
Philip Tricca1ea84a52015-11-19 18:07:06 -0800105//
unknown7f43bd52015-09-01 09:48:13 -0400106UINT8 GetCommandAttributes( TPM_CC commandCode, TPML_CCA *supportedCommands, TPMA_CC *cmdAttributes )
107{
108 UINT32 i;
109 UINT8 rval = 0;
Philip Tricca4c1b4a02016-06-21 05:44:51 -0700110
unknown7f43bd52015-09-01 09:48:13 -0400111 for( i = 0; i < supportedCommands->count; i++ )
112 {
113 if( (TPM_CC)( supportedCommands->commandAttributes[i].commandIndex ) == commandCode )
114 {
115 rval = 1;
116 *cmdAttributes = supportedCommands->commandAttributes[i];
117 break;
118 }
119 }
120
121 return rval;
root14b5bab2015-09-02 16:24:20 -0400122}
123