unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 1 | //**********************************************************************; |
| 2 | // Copyright (c) 2015, Intel Corporation |
| 3 | // All rights reserved. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 6 | // modification, are permitted provided that the following conditions are met: |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 9 | // this list of conditions and the following disclaimer. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions and the following disclaimer in the documentation |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 13 | // and/or other materials provided with the distribution. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 14 | // |
| 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 |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 25 | // THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | //**********************************************************************; |
| 27 | |
Philip Tricca | 364c0ce | 2016-07-23 16:48:37 -0700 | [diff] [blame^] | 28 | #include "sapi/tpm20.h" |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 31 | #include "sysapi_util.h" |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 32 | |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 33 | // Get the TPM 2.0 commands supported by the TPM. |
| 34 | TSS2_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 Tricca | 4c1b4a0 | 2016-06-21 05:44:51 -0700 | [diff] [blame] | 42 | |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 43 | // 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 Tricca | 7180029 | 2016-06-21 05:02:17 -0700 | [diff] [blame] | 60 | *supportedCommands = (TPML_CCA *)malloc( numCommands * sizeof( TPMA_CC ) + sizeof( UINT32 ) ); |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 61 | if( !*supportedCommands ) |
| 62 | { |
Philip Tricca | 4c1b4a0 | 2016-06-21 05:44:51 -0700 | [diff] [blame] | 63 | rval = TSS2_BASE_RC_INSUFFICIENT_BUFFER + TSS2_RESMGR_ERROR_LEVEL; |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 64 | 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 Tricca | 4c1b4a0 | 2016-06-21 05:44:51 -0700 | [diff] [blame] | 92 | |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 93 | returnFromGetCommands: |
| 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 Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 105 | // |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 106 | UINT8 GetCommandAttributes( TPM_CC commandCode, TPML_CCA *supportedCommands, TPMA_CC *cmdAttributes ) |
| 107 | { |
| 108 | UINT32 i; |
| 109 | UINT8 rval = 0; |
Philip Tricca | 4c1b4a0 | 2016-06-21 05:44:51 -0700 | [diff] [blame] | 110 | |
unknown | 7f43bd5 | 2015-09-01 09:48:13 -0400 | [diff] [blame] | 111 | 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; |
root | 14b5bab | 2015-09-02 16:24:20 -0400 | [diff] [blame] | 122 | } |
| 123 | |