summaryrefslogtreecommitdiff
path: root/Board/EM/TCG2/Common/Tpm20Acpi/Tpm20Acpi.c
blob: 6d1201926f1aa474c19ac12bc566848e88838e08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************
//*************************************************************************
// $Header: /Alaska/SOURCE/Modules/TCG2/Common/Tpm20PlatformDxe/Tpm20Acpi/Tpm20Acpi.c 2     4/21/14 3:18p Fredericko $
//
// $Revision: 2 $
//
// $Date: 4/21/14 3:18p $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/TCG2/Common/Tpm20PlatformDxe/Tpm20Acpi/Tpm20Acpi.c $
// 
// 2     4/21/14 3:18p Fredericko
// 
// 1     4/21/14 2:19p Fredericko
// 
// 1     10/08/13 12:06p Fredericko
// Initial Check-In for Tpm-Next module
// 
// 2     10/03/13 3:01p Fredericko
// 
// 1     7/10/13 5:58p Fredericko
// [TAG]  		EIP120969
// [Category]  	New Feature
// [Description]  	TCG (TPM20)
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:  
//
// Description:
//
//<AMI_FHDR_END>
//*************************************************************************

#include "Tpm20Acpi.h"
#include <Token.h>
#include <AmiDxeLib.h>
#include "Tpm20CRBLib.h"


EFI_TPM2_ACPI_TABLE  mTpm2AcpiTemplate = {
  {
    EFI_ACPI_5_0_TRUSTED_COMPUTING_PLATFORM_2_TABLE_SIGNATURE,
    sizeof (mTpm2AcpiTemplate),
    EFI_TPM2_ACPI_TABLE_REVISION,
    //
    // initialize to 0
    // 
    //
  },
  0, // Flags
  (EFI_PHYSICAL_ADDRESS)(UINTN)0xFFFFFFFF, // Control Area
  EFI_TPM2_ACPI_TABLE_START_METHOD_ACPI,
};




//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   GetHob
//
// Description: Find instance of a HOB type in a HOB list
//
// Input:
//      Type          The HOB type to return.
//      HobStart      The first HOB in the HOB list.
//
// Output:
//      Pointer to the Hob matching the type or NULL
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
VOID* GetHob(
    IN UINT16 Type,
    IN VOID   *HobStart  )
{
    EFI_PEI_HOB_POINTERS Hob;

    Hob.Raw = HobStart;

    //
    // Return input if not found
    //
    if ( HobStart == NULL )
    {
        return HobStart;
    }

    //
    // Parse the HOB list, stop if end of list or matching type found.
    //
    while ( !END_OF_HOB_LIST( Hob ))
    {
        if ( Hob.Header->HobType == Type )
        {
            break;
        }

        Hob.Raw = GET_NEXT_HOB( Hob );
    }

    //
    // Return input if not found
    //
    if ( END_OF_HOB_LIST( Hob ))
    {
        return HobStart;
    }

    return (VOID*)(Hob.Raw);
}

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   CompareGuid
//
// Description: Compares two input GUIDs
//
// Input:       Comparision status
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
BOOLEAN CompareGuid(
    EFI_GUID *G1,
    EFI_GUID *G2 )
{
    UINT32 *p1 = (UINT32*)G1, *p2 = (UINT32*)G2;
    UINTN  i;

    for ( i = 0; i < 4; ++i )
    {
        if ( p1[i] != p2[i] )
        {
            return FALSE;
        }
    }
    return TRUE;
    ;
}

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   GetNextGuidHob
//
// Description: Find GUID HOB
//
// Input:       HobStart    A pointer to the start hob.
//              Guid        A pointer to a guid.
// Output:
//              Buffer          A pointer to the buffer.
//              BufferSize      Buffer size.
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS GetNextGuidHob(
    IN OUT VOID          **HobStart,
    IN EFI_GUID          * Guid,
    OUT VOID             **Buffer,
    OUT UINTN            *BufferSize OPTIONAL )
{
    EFI_STATUS           Status;
    EFI_PEI_HOB_POINTERS GuidHob;

    if ( Buffer == NULL )
    {
        return EFI_INVALID_PARAMETER;
    }

    for ( Status = EFI_NOT_FOUND; EFI_ERROR( Status );)
    {
        GuidHob.Raw = *HobStart;

        if ( END_OF_HOB_LIST( GuidHob ))
        {
            return EFI_NOT_FOUND;
        }

        GuidHob.Raw = GetHob( EFI_HOB_TYPE_GUID_EXTENSION, *HobStart );

        if ( GuidHob.Header->HobType == EFI_HOB_TYPE_GUID_EXTENSION )
        {
            if ( CompareGuid( Guid, &GuidHob.Guid->Name ))
            {
                Status  = EFI_SUCCESS;
                *Buffer = (VOID*)((UINT8*)(&GuidHob.Guid->Name) 
                          + sizeof (EFI_GUID));

                if ( BufferSize != NULL )
                {
                    *BufferSize = GuidHob.Header->HobLength
                                  - sizeof (EFI_HOB_GUID_TYPE);
                }
            }
        }

        *HobStart = GET_NEXT_HOB( GuidHob );
    }

    return Status;
}



VOID* FindHob(
    IN UINTN                   NoTableEntries,
    IN EFI_CONFIGURATION_TABLE *ConfigTable,
    IN EFI_GUID                *HOB_guid )
{
    VOID *HobStart;
    VOID *PtrHob;
    EFI_GUID Hoblistguid = HOB_LIST_GUID;

    while ( NoTableEntries > 0 )
    {
        NoTableEntries--;

        if ((!MemCmp(
                 &ConfigTable[NoTableEntries].VendorGuid,
                 &Hoblistguid, sizeof(EFI_GUID)
                 )))
        {
            HobStart = ConfigTable[NoTableEntries].VendorTable;

            if ( !EFI_ERROR(
                     GetNextGuidHob( &HobStart, HOB_guid, &PtrHob, NULL )
                     ))
            {
                TRACE ((TRACE_ALWAYS, "Hob found = %x \n", PtrHob));
                return PtrHob;
            }
        }
    }
    return NULL;
}



EFI_STATUS
Tpm20PublishAcpiTable (
  VOID
  )
{
  EFI_STATUS    Status;
  UINTN         TableKey = 0;
  EFI_ACPI_TABLE_PROTOCOL     *AcpiTable;
  EFI_TPM2_ACPI_CONTROL_AREA  *CtrlAreaMap = NULL;
  EFI_GUID      Meguid = ME_DATA_HOB_GUID;
  INTEL_PTT_HOB_DATA  *StolenAddress = NULL;

  mTpm2AcpiTemplate.Header.OemRevision     = TPM20TABLEOEMREVISION;
  mTpm2AcpiTemplate.Header.CreatorId  = CREATOR_ID_AMI;
  mTpm2AcpiTemplate.Header.OemTableId = EFI_SIGNATURE_64 ('T', 'p', 'm', '2', 'T', 'a', 'b', 'l');
  
  //
  // Publish the TPM ACPI table
  //
  Status = pBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);
  if(EFI_ERROR(Status))return Status;

  if(!isTpm20CrbPresent())
  {
    mTpm2AcpiTemplate.AddressOfControlArea = 0;
    mTpm2AcpiTemplate.StartMethod = 6;
   }else{
    
     StolenAddress = (INTEL_PTT_HOB_DATA  *)FindHob ( pST->NumberOfTableEntries,
                               pST->ConfigurationTable,
                               &Meguid);

     if(StolenAddress != 0){
        if(((UINTN)((UINT64 *)StolenAddress->BufferAddress)) != 0){
            mTpm2AcpiTemplate.AddressOfControlArea = StolenAddress->BufferAddress;
        }else{
            mTpm2AcpiTemplate.AddressOfControlArea = TPM20_CRBBASE;
        }
     }else{
         mTpm2AcpiTemplate.AddressOfControlArea = TPM20_CRBBASE;
     }

     mTpm2AcpiTemplate.StartMethod = 2;
     CtrlAreaMap = (EFI_TPM2_ACPI_CONTROL_AREA *)(UINTN) mTpm2AcpiTemplate.AddressOfControlArea;
     MemSet (CtrlAreaMap, sizeof(EFI_TPM2_ACPI_CONTROL_AREA), 0);
     CtrlAreaMap->CommandSize  = 0xF80;
     CtrlAreaMap->ResponseSize = 0xF80;
     CtrlAreaMap->Command      = (UINTN)mTpm2AcpiTemplate.AddressOfControlArea + 0x80;
     CtrlAreaMap->Response     = (UINTN)mTpm2AcpiTemplate.AddressOfControlArea + 0x80;
      
     TRACE ((TRACE_ALWAYS, "Ftpm Windows Buffer Control Area Address = %x\n", mTpm2AcpiTemplate.AddressOfControlArea));
     TRACE ((TRACE_ALWAYS, "Ftpm Windows Command/Response Buffer Address = %x\n", CtrlAreaMap->Command));
   }
   
   TRACE ((TRACE_ALWAYS, " Before Installation of ACPI table \n"));
   Status = AcpiTable->InstallAcpiTable (AcpiTable,
                            &mTpm2AcpiTemplate,
                            sizeof(EFI_TPM2_ACPI_TABLE),
                            &TableKey);

  if(EFI_ERROR(Status))return Status;

  return Status;
}


EFI_STATUS
EFIAPI
Tpm20AcpiInitEntry (
  IN EFI_HANDLE                  ImageHandle,
  IN EFI_SYSTEM_TABLE            *SystemTable
)
{ 
   EFI_STATUS Status;

   InitAmiLib( ImageHandle, SystemTable );

   Status = Tpm20PublishAcpiTable ();

   return Status;
}








//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************