summaryrefslogtreecommitdiff
path: root/Core/EM/SLP20/SlpSupport.c
blob: 015947320ea22e9a803a2f84541a365e3b44ad0d (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2011, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**             5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093          **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************

//****************************************************************************
// $Header: /Alaska/BIN/Modules/SLP/SlpSupport.c 12    8/03/11 2:45p Vyacheslava $
//
// $Revision: 12 $
//
// $Date: 8/03/11 2:45p $
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Modules/SLP/SlpSupport.c $
// 
// 12    8/03/11 2:45p Vyacheslava
// 
// 11    8/01/11 3:25p Vyacheslava
// [TAG]  		EIP64110
// [Category]  	Spec Update
// [Severity]  	Normal
// [Description]  	This file has been updated to be compliant with PI 1.2,
// and UEFI 2.3.1 specifications. 
// 
// 10    7/12/11 4:44p Vyacheslava
// 
// 9     7/12/11 3:56p Vyacheslava
// 
// 8     7/01/11 12:19p Vyacheslava
// [TAG]  		EIP63678
// [Category]  	Improvement
// [Description]  	Added checking for dummy binaries. 
// 
// 7     7/01/11 12:00p Vyacheslava
// [TAG]  		EIP63678
// [Category]  	New Feature
// [Description]  	Preserve the OEM Activation SLIC during the BIOS
// recovery path.
// [Files]  		SlpSupport.sdl
// SlpSupport.mak
// SlpSupport.c
// SlpSupport.dxs
// SlpSupport.h
// SlpSupport.cif
// 
// 6     8/25/10 11:44a Vyacheslava
// Added comments.
// 
// 5     5/18/10 4:15p Vyacheslava
// Update for OEM ID and OEM Table ID from the Marker binary. (EIP#38438)
// 
// 4     8/19/09 10:47a Vyacheslava
// Added support for ACPI 3.0 (EIP#25335 )
// 
// 3     6/01/09 4:53p Vyacheslava
// EIP#22212: Synopsis: (1) SimpleBootFlag, (2) SLP, (3) CSPLibrary
// Description: Aptio Enhancement Guidelines for Engineers
//
// 2     10/09/07 3:55p Vyacheslava
// Fixed issue with token conversion. Added function CopyToken.
//
// 1     3/30/07 3:39p Felixp
//
//****************************************************************************
//<AMI_FHDR_START>
//
// Name:  SlpSupport.c
//
// Description:
//  Updated the XSDT with the SLIC OEM Activation ACPI Table.
// 
//<AMI_FHDR_END>
//****************************************************************************


//---------------------------------------------------------------------------

#include <AmiDxeLib.h>
#include <Protocol/AcpiTable.h>
#include <AmiHobs.h>
#include "SlpSupport.h"

//---------------------------------------------------------------------------


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   LocateAndLoadRawData
//
// Description: This fills the PubKey and Marker structures of SLIC table.
//
// Input:       
//  *FfsGuid    - Pointer to GUID of the FFS file to read
//  *Address    - Address of the buffer to read data into
//  Size        - Size of the buffer
//
// Output:
//  EFI_STATUS    - Sucessful
//  EFI_NOT_FOUND - Couldn't find the binaries
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS LocateAndLoadRawData(
    IN EFI_GUID *FfsGuid,
    IN VOID *Address,
    IN UINTN Size )
{
    EFI_STATUS                   Status;
    EFI_HANDLE                   *HandleBuffer = NULL;
    UINTN                        NumberOfHandles;
    UINTN                        Index;
    UINT32                       Authentication;

#if PI_SPECIFICATION_VERSION >= 0x0001000A
    EFI_GUID EfiFirmwareVolumeProtocolGuid = EFI_FIRMWARE_VOLUME2_PROTOCOL_GUID;
    EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVolumeProtocol = NULL;
#else
    EFI_GUID EfiFirmwareVolumeProtocolGuid = EFI_FIRMWARE_VOLUME_PROTOCOL_GUID;
    EFI_FIRMWARE_VOLUME_PROTOCOL *FwVolumeProtocol = NULL;
#endif

    // Locate the Firmware volume protocol
    Status = pBS->LocateHandleBuffer(
        ByProtocol,
        &EfiFirmwareVolumeProtocolGuid, 
        NULL,
        &NumberOfHandles,
        &HandleBuffer
    );
    if (EFI_ERROR(Status)) 
        return Status;
    
    // Find and read raw data
    for (Index = 0; Index < NumberOfHandles; Index++) {

        Status = pBS->HandleProtocol(
            HandleBuffer[Index],
            &EfiFirmwareVolumeProtocolGuid,
            &FwVolumeProtocol
        );
        if (EFI_ERROR(Status)) 
            continue;
        
        Status = FwVolumeProtocol->ReadSection(
            FwVolumeProtocol,
            FfsGuid,
            EFI_SECTION_RAW,
            0x0,
            &Address,
            &Size,
            &Authentication
        );
        if (Status == EFI_SUCCESS) 
            break;
    }
    
    pBS->FreePool(HandleBuffer);
    return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PreserveSlicBinaries
//
// Description: This function preserves the the Marker and PubKey binaries.
//
// Input:       VOID
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS PreserveSlicBinaries(VOID)
{
    EFI_STATUS Status = EFI_NOT_FOUND;
    RECOVERY_IMAGE_HOB *RecoveryHob; 
    EFI_GUID HobListGuid = HOB_LIST_GUID;
    EFI_GUID RecoveryHobGuid = AMI_RECOVERY_IMAGE_HOB_GUID;
    EFI_GUID PubKeyGuid = OEM_SLP_PUBLIC_KEY_GUID;
    EFI_GUID MarkerGuid = OEM_SLP_MARKER_GUID;
	EFI_CONFIGURATION_TABLE	*Table = pST->ConfigurationTable;
    EFI_PHYSICAL_ADDRESS Addr;
	UINTN  i;
    UINT8  *FwPubKeyPtr = NULL;
    UINT8  *FwMarkerPtr = NULL;
    UINT8  *RecPubKeyPtr = NULL;
    UINT8  *RecMarkerPtr = NULL;

    // Find the Product Key place in the recovery HOB
	for( i = 0; i < pST->NumberOfTableEntries; i++, Table++ ) {

        if ( !MemCmp(&Table->VendorGuid, &HobListGuid, sizeof(EFI_GUID)) ) {

            RecoveryHob = Table->VendorTable;
            Status = FindNextHobByGuid(&RecoveryHobGuid, &RecoveryHob);
            if (!EFI_ERROR(Status)) {

                // Find Public Key and Marker binaries in the Recovery image
                Status = EFI_NOT_FOUND;
                for ( Addr = RecoveryHob->Address; Addr < RecoveryHob->Address + FLASH_SIZE; Addr++ ) {

                    // Find the Public Key binary GUID
                    if ( !MemCmp((UINT8*)Addr, &PubKeyGuid, sizeof(EFI_GUID)) )
                        RecPubKeyPtr = (UINT8*)Addr;

                    // Find the Marker binary GUID
                    else if ( !MemCmp((UINT8*)Addr, &MarkerGuid, sizeof(EFI_GUID)) )
                        RecMarkerPtr = (UINT8*)Addr;

                    // if all binaries are found then find original places in the Firmware
                    if ( RecPubKeyPtr != NULL && RecMarkerPtr != NULL ) {

                        // Find Public Key and Marker binaries in the BIOS firmware
                        for ( Addr = FLASH_DEVICE_BASE_ADDRESS; Addr < FLASH_UPPER_ADDRESS; Addr++ ) {
        
                            // Find the Public Key binary GUID
                            if ( !MemCmp((UINT8*)Addr, &PubKeyGuid, sizeof(EFI_GUID)) )
                                FwPubKeyPtr = (UINT8*)Addr;

                            // Find the Marker binary GUID
                            else if ( !MemCmp((UINT8*)Addr, &MarkerGuid, sizeof(EFI_GUID)) )
                                FwMarkerPtr = (UINT8*)Addr;
        
                            // Preserve the SLIC binaries
                            if ( FwPubKeyPtr != NULL && FwMarkerPtr != NULL ) {

                                MemCpy( 
                                    RecPubKeyPtr, 
                                    FwPubKeyPtr, 
                                    sizeof(OEM_PUBLIC_KEY_STRUCTURE) + sizeof(EFI_FFS_FILE_HEADER) + sizeof(UINT16)
                                );

                                MemCpy( 
                                    RecMarkerPtr, 
                                    FwMarkerPtr, 
                                    sizeof(WINDOWS_MARKER_STRUCTURE) + sizeof(EFI_FFS_FILE_HEADER) + sizeof(UINT16)
                                );

                                Status = EFI_SUCCESS;
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    TRACE((TRACE_ALWAYS,"PreserveSlicBinaries: Status = %r\n",Status));
    return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PublishSlicTable
//
// Description: This function publish SLIC table in the ACPI.
//
// Input:       VOID
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS PublishSlicTable(VOID)
{
    EFI_STATUS Status;
    EFI_GUID PubKeyGuid = OEM_SLP_PUBLIC_KEY_GUID;
    EFI_GUID MarkerGuid = OEM_SLP_MARKER_GUID;
    EFI_ACPI_SLP SlpTable = {
        {SLIC_SIG, sizeof(EFI_ACPI_SLP), 0x01, 0x00, {0}, {0}, ACPI_OEM_REV, CREATOR_ID_AMI, CREATOR_REV_MS},
        {0},
        {0}
    };
    EFI_ACPI_TABLE_PROTOCOL *AcpiTableProtocol = NULL;
    UINTN  TableKey = 0;
    
    // Locate SLP Public Key binary
    Status = LocateAndLoadRawData(
        &PubKeyGuid, 
        &SlpTable.PubKey, 
        sizeof(SlpTable.PubKey)
    );
    if (EFI_ERROR(Status)) {
        TRACE((TRACE_ALWAYS, "Slp Public Key Binaries Not Found!\n"));
        return Status;
    }
    
    // Locate SLP Marker binary
    Status = LocateAndLoadRawData(
        &MarkerGuid, 
        &SlpTable.WinMarker, 
        sizeof(SlpTable.WinMarker)
    );
    if (EFI_ERROR(Status)) {
        TRACE((TRACE_ALWAYS, "Slp Marker Binaries Not Found!\n"));
        return Status;
    }

    // The dummy SLIC will be not published
    if ( SlpTable.WinMarker.StructType == 0xFFFFFFFF && SlpTable.PubKey.StructType == 0xFFFFFFFF ) {
        TRACE((TRACE_ALWAYS, "SlpSupport: Found dummy binaries. The SLIC will be not published!\n"));
        return EFI_ABORTED;
    }

    // Copy OEM ID and OEM Table ID from Marker's binary to SLIC header
    MemCpy( 
        (UINT8*)&SlpTable.Header.OemId,
        (UINT8*)&SlpTable.WinMarker.sOEMID,
        sizeof(SlpTable.WinMarker.sOEMID)
    );
    MemCpy( 
        (UINT8*)&SlpTable.Header.OemTblId,
        (UINT8*)&SlpTable.WinMarker.sOEMTABLEID,
        sizeof(SlpTable.WinMarker.sOEMTABLEID)
    );

    // Locate the ACPI table protocol
    Status = pBS->LocateProtocol(
        &gEfiAcpiTableProtocolGuid, 
        NULL, 
        &AcpiTableProtocol
    );
    if (EFI_ERROR(Status)) {
        TRACE((TRACE_ALWAYS, "Unable to locate AcpiTableProtocol!\n"));
        return Status;
    }
    
    // Publish SLIC to ACPI table
    Status = AcpiTableProtocol->InstallAcpiTable(
        AcpiTableProtocol,
        &SlpTable,
        sizeof(EFI_ACPI_SLP), 
        &TableKey
    );
    if (!EFI_ERROR(Status))
        TRACE((TRACE_ALWAYS, "SLIC table has been published.\n"));

    return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SlpSupport_EntryPoint
//
// Description: This function is the entry point of the eModule.
//
// Input:       
//  ImageHandle  - Image handle
//  *SystemTable - Pointer to the system table
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI SlpSupport_EntryPoint(
    IN EFI_HANDLE        ImageHandle,
    IN EFI_SYSTEM_TABLE  *SystemTable )
{
    EFI_STATUS Status = EFI_SUCCESS;

    InitAmiLib(ImageHandle, SystemTable);

    // Check if it's a Recovery Mode then preserve the Marker and PubKey binaries
    if (GetBootMode() == BOOT_IN_RECOVERY_MODE)
        Status = PreserveSlicBinaries();
    if (!EFI_ERROR(Status))
        Status = PublishSlicTable();

    return Status;
}


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