xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/src/subsystem/PP.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Introduction
36*5c591343SA. Cody Schuffelen // This file contains the functions that support the physical presence operations
37*5c591343SA. Cody Schuffelen // of the TPM.
38*5c591343SA. Cody Schuffelen 
39*5c591343SA. Cody Schuffelen //** Includes
40*5c591343SA. Cody Schuffelen 
41*5c591343SA. Cody Schuffelen #include "Tpm.h"
42*5c591343SA. Cody Schuffelen 
43*5c591343SA. Cody Schuffelen //** Functions
44*5c591343SA. Cody Schuffelen 
45*5c591343SA. Cody Schuffelen //*** PhysicalPresencePreInstall_Init()
46*5c591343SA. Cody Schuffelen // This function is used to initialize the array of commands that always require
47*5c591343SA. Cody Schuffelen // confirmation with physical presence. The array is an array of bits that
48*5c591343SA. Cody Schuffelen // has a correspondence with the command code.
49*5c591343SA. Cody Schuffelen //
50*5c591343SA. Cody Schuffelen // This command should only ever be executable in a manufacturing setting or in
51*5c591343SA. Cody Schuffelen // a simulation.
52*5c591343SA. Cody Schuffelen //
53*5c591343SA. Cody Schuffelen // When set, these cannot be cleared.
54*5c591343SA. Cody Schuffelen //
55*5c591343SA. Cody Schuffelen void
PhysicalPresencePreInstall_Init(void)56*5c591343SA. Cody Schuffelen PhysicalPresencePreInstall_Init(
57*5c591343SA. Cody Schuffelen     void
58*5c591343SA. Cody Schuffelen     )
59*5c591343SA. Cody Schuffelen {
60*5c591343SA. Cody Schuffelen     COMMAND_INDEX        commandIndex;
61*5c591343SA. Cody Schuffelen     // Clear all the PP commands
62*5c591343SA. Cody Schuffelen     MemorySet(&gp.ppList, 0, sizeof(gp.ppList));
63*5c591343SA. Cody Schuffelen 
64*5c591343SA. Cody Schuffelen     // Any command that is PP_REQUIRED should be SET
65*5c591343SA. Cody Schuffelen     for(commandIndex = 0; commandIndex < COMMAND_COUNT; commandIndex++)
66*5c591343SA. Cody Schuffelen     {
67*5c591343SA. Cody Schuffelen         if(s_commandAttributes[commandIndex] & IS_IMPLEMENTED
68*5c591343SA. Cody Schuffelen            &&  s_commandAttributes[commandIndex] & PP_REQUIRED)
69*5c591343SA. Cody Schuffelen             SET_BIT(commandIndex, gp.ppList);
70*5c591343SA. Cody Schuffelen     }
71*5c591343SA. Cody Schuffelen     // Write PP list to NV
72*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(ppList);
73*5c591343SA. Cody Schuffelen     return;
74*5c591343SA. Cody Schuffelen }
75*5c591343SA. Cody Schuffelen 
76*5c591343SA. Cody Schuffelen //*** PhysicalPresenceCommandSet()
77*5c591343SA. Cody Schuffelen // This function is used to set the indicator that a command requires
78*5c591343SA. Cody Schuffelen // PP confirmation.
79*5c591343SA. Cody Schuffelen void
PhysicalPresenceCommandSet(TPM_CC commandCode)80*5c591343SA. Cody Schuffelen PhysicalPresenceCommandSet(
81*5c591343SA. Cody Schuffelen     TPM_CC           commandCode    // IN: command code
82*5c591343SA. Cody Schuffelen     )
83*5c591343SA. Cody Schuffelen {
84*5c591343SA. Cody Schuffelen     COMMAND_INDEX       commandIndex = CommandCodeToCommandIndex(commandCode);
85*5c591343SA. Cody Schuffelen 
86*5c591343SA. Cody Schuffelen     // if the command isn't implemented, the do nothing
87*5c591343SA. Cody Schuffelen     if(commandIndex == UNIMPLEMENTED_COMMAND_INDEX)
88*5c591343SA. Cody Schuffelen         return;
89*5c591343SA. Cody Schuffelen 
90*5c591343SA. Cody Schuffelen     // only set the bit if this is a command for which PP is allowed
91*5c591343SA. Cody Schuffelen     if(s_commandAttributes[commandIndex] & PP_COMMAND)
92*5c591343SA. Cody Schuffelen         SET_BIT(commandIndex, gp.ppList);
93*5c591343SA. Cody Schuffelen     return;
94*5c591343SA. Cody Schuffelen }
95*5c591343SA. Cody Schuffelen 
96*5c591343SA. Cody Schuffelen //*** PhysicalPresenceCommandClear()
97*5c591343SA. Cody Schuffelen // This function is used to clear the indicator that a command requires PP
98*5c591343SA. Cody Schuffelen // confirmation.
99*5c591343SA. Cody Schuffelen void
PhysicalPresenceCommandClear(TPM_CC commandCode)100*5c591343SA. Cody Schuffelen PhysicalPresenceCommandClear(
101*5c591343SA. Cody Schuffelen     TPM_CC           commandCode    // IN: command code
102*5c591343SA. Cody Schuffelen     )
103*5c591343SA. Cody Schuffelen {
104*5c591343SA. Cody Schuffelen     COMMAND_INDEX       commandIndex = CommandCodeToCommandIndex(commandCode);
105*5c591343SA. Cody Schuffelen 
106*5c591343SA. Cody Schuffelen     // If the command isn't implemented, then don't do anything
107*5c591343SA. Cody Schuffelen     if(commandIndex == UNIMPLEMENTED_COMMAND_INDEX)
108*5c591343SA. Cody Schuffelen         return;
109*5c591343SA. Cody Schuffelen 
110*5c591343SA. Cody Schuffelen     // Only clear the bit if the command does not require PP
111*5c591343SA. Cody Schuffelen     if((s_commandAttributes[commandIndex] & PP_REQUIRED) == 0)
112*5c591343SA. Cody Schuffelen         CLEAR_BIT(commandIndex, gp.ppList);
113*5c591343SA. Cody Schuffelen 
114*5c591343SA. Cody Schuffelen     return;
115*5c591343SA. Cody Schuffelen }
116*5c591343SA. Cody Schuffelen 
117*5c591343SA. Cody Schuffelen //*** PhysicalPresenceIsRequired()
118*5c591343SA. Cody Schuffelen // This function indicates if PP confirmation is required for a command.
119*5c591343SA. Cody Schuffelen //  Return Type: BOOL
120*5c591343SA. Cody Schuffelen //      TRUE(1)         physical presence is required
121*5c591343SA. Cody Schuffelen //      FALSE(0)        physical presence is not required
122*5c591343SA. Cody Schuffelen BOOL
PhysicalPresenceIsRequired(COMMAND_INDEX commandIndex)123*5c591343SA. Cody Schuffelen PhysicalPresenceIsRequired(
124*5c591343SA. Cody Schuffelen     COMMAND_INDEX    commandIndex   // IN: command index
125*5c591343SA. Cody Schuffelen     )
126*5c591343SA. Cody Schuffelen {
127*5c591343SA. Cody Schuffelen     // Check the bit map.  If the bit is SET, PP authorization is required
128*5c591343SA. Cody Schuffelen     return (TEST_BIT(commandIndex, gp.ppList));
129*5c591343SA. Cody Schuffelen }
130*5c591343SA. Cody Schuffelen 
131*5c591343SA. Cody Schuffelen //*** PhysicalPresenceCapGetCCList()
132*5c591343SA. Cody Schuffelen // This function returns a list of commands that require PP confirmation. The
133*5c591343SA. Cody Schuffelen // list starts from the first implemented command that has a command code that
134*5c591343SA. Cody Schuffelen // the same or greater than 'commandCode'.
135*5c591343SA. Cody Schuffelen //  Return Type: TPMI_YES_NO
136*5c591343SA. Cody Schuffelen //      YES         if there are more command codes available
137*5c591343SA. Cody Schuffelen //      NO          all the available command codes have been returned
138*5c591343SA. Cody Schuffelen TPMI_YES_NO
PhysicalPresenceCapGetCCList(TPM_CC commandCode,UINT32 count,TPML_CC * commandList)139*5c591343SA. Cody Schuffelen PhysicalPresenceCapGetCCList(
140*5c591343SA. Cody Schuffelen     TPM_CC           commandCode,   // IN: start command code
141*5c591343SA. Cody Schuffelen     UINT32           count,         // IN: count of returned TPM_CC
142*5c591343SA. Cody Schuffelen     TPML_CC         *commandList    // OUT: list of TPM_CC
143*5c591343SA. Cody Schuffelen     )
144*5c591343SA. Cody Schuffelen {
145*5c591343SA. Cody Schuffelen     TPMI_YES_NO     more = NO;
146*5c591343SA. Cody Schuffelen     COMMAND_INDEX   commandIndex;
147*5c591343SA. Cody Schuffelen 
148*5c591343SA. Cody Schuffelen     // Initialize output handle list
149*5c591343SA. Cody Schuffelen     commandList->count = 0;
150*5c591343SA. Cody Schuffelen 
151*5c591343SA. Cody Schuffelen     // The maximum count of command we may return is MAX_CAP_CC
152*5c591343SA. Cody Schuffelen     if(count > MAX_CAP_CC) count = MAX_CAP_CC;
153*5c591343SA. Cody Schuffelen 
154*5c591343SA. Cody Schuffelen     // Collect PP commands
155*5c591343SA. Cody Schuffelen     for(commandIndex = GetClosestCommandIndex(commandCode);
156*5c591343SA. Cody Schuffelen     commandIndex != UNIMPLEMENTED_COMMAND_INDEX;
157*5c591343SA. Cody Schuffelen         commandIndex = GetNextCommandIndex(commandIndex))
158*5c591343SA. Cody Schuffelen     {
159*5c591343SA. Cody Schuffelen         if(PhysicalPresenceIsRequired(commandIndex))
160*5c591343SA. Cody Schuffelen         {
161*5c591343SA. Cody Schuffelen             if(commandList->count < count)
162*5c591343SA. Cody Schuffelen             {
163*5c591343SA. Cody Schuffelen                 // If we have not filled up the return list, add this command
164*5c591343SA. Cody Schuffelen                 // code to it
165*5c591343SA. Cody Schuffelen                 commandList->commandCodes[commandList->count]
166*5c591343SA. Cody Schuffelen                     = GetCommandCode(commandIndex);
167*5c591343SA. Cody Schuffelen                 commandList->count++;
168*5c591343SA. Cody Schuffelen             }
169*5c591343SA. Cody Schuffelen             else
170*5c591343SA. Cody Schuffelen             {
171*5c591343SA. Cody Schuffelen                 // If the return list is full but we still have PP command
172*5c591343SA. Cody Schuffelen                 // available, report this and stop iterating
173*5c591343SA. Cody Schuffelen                 more = YES;
174*5c591343SA. Cody Schuffelen                 break;
175*5c591343SA. Cody Schuffelen             }
176*5c591343SA. Cody Schuffelen         }
177*5c591343SA. Cody Schuffelen     }
178*5c591343SA. Cody Schuffelen     return more;
179*5c591343SA. Cody Schuffelen }