summaryrefslogtreecommitdiff
path: root/Core/EM/Runtime/Runtime.c
blob: cebe355234eeab1e13f0c7df2889d1d201c31833 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Core/Runtime/Runtime.c 23    6/16/11 9:07a Felixp $
//
// $Revision: 23 $
//
// $Date: 6/16/11 9:07a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Runtime/Runtime.c $
// 
// 23    6/16/11 9:07a Felixp
// Enhancements(EIP 49676): 
// 1. FindDescriptor: upated to ignore boot time memory map descriptors
// 2. SetVirtualAddressMap: updated to skip virtual address change
// callbacks that reside in the boot time memory.
// 
// 22    9/22/10 6:57p Felixp
// Minor improvements: use InitAmiRuntimeLib instead of InitAmiLib
// 
// 21    3/12/10 12:00p Felixp
// 
// 20    3/12/10 10:26a Felixp
// Bug fix: Potential problems with UEFI OS. 
// pST was used here even after it has been virtualized in the
// AmiLibGoVirtual. 
// The local copy of the pointer is created.
// 
// 19    7/08/09 12:00p Felixp
// 
// 18    6/24/09 4:23p Robert
// updated comments, copyright and some coding standard items
// 
// 17    5/14/09 9:34a Felixp
// New feature: SMM version of Runtime Services
// Runtime driver is updated to install SMM version of the runtime
// services table.
// 
// 15    11/20/08 2:58p Felixp
// Bug fix in SetVirtualAddressMap( bug fix in the runtime images fixup
// logic )
// 
// 14    11/07/07 11:54a Felixp
// Bug fix in FindDescriptor routine
// 
// 13    8/07/07 2:23p Felixp
// New StatusCodes added
// 
// 7     10/27/06 10:31a Felixp
// checkpoints on begin/end of SetVirtualAddressMap added
// 
// 6     10/27/06 10:15a Felixp
// Bug fixes in SetVirtualAddressMap
// 
// 5     10/07/06 10:22a Felixp
// New Runtime protocol defined by DXE CIS 0.91 implemented
// (Old(DXE CIS 0.90) Runtime protocol and AMI Runtime protocols removed).
// 
// 3     1/18/05 3:22p Felixp
// PrintDebugMessage renamed to Trace
// 
// 14    12/23/04 9:29a Felixp
// ArchProtocols moved to Include\Protocols
// 
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:	Runtime.c
//
// Description:	
//  This file contains the Runtime Architectural Protocol along with 
//  some runtime functions that are included in the Runtime Services Table
//  SetVirtualAddressMap, ConvertPointer, and CalculateCrc32.  The entry
//  point for the Runtime Driver also contains a call to InitParts which 
//  uses the RuntimeInitialize and RuntimeSmmInitialize eLinks to 
//  initialize other pieces of the Runtime Structure
//
//<AMI_FHDR_END>
//**********************************************************************

//=============================================================================
// Includes
#include <Protocol/Runtime.h>
#include <Protocol/LoadedImage.h>
#include <AmiDxeLib.h>

//=============================================================================
// Protocol Definition
EFI_RUNTIME_ARCH_PROTOCOL Runtime = 
{
        {&Runtime.ImageHead,&Runtime.ImageHead}, //EFI_LIST_ENTRY ImageHead;
        {&Runtime.EventHead,&Runtime.EventHead}, //EFI_LIST_ENTRY EventHead;
        0, //UINTN MemoryDescriptorSize;
        0, //UINT32 MemoryDesciptorVersion;
        0, //UINTN MemoryMapSize;
        NULL, //EFI_MEMORY_DESCRIPTOR *MemoryMapPhysical;
        NULL, //EFI_MEMORY_DESCRIPTOR *MemoryMapVirtual;
        FALSE, //BOOLEAN VirtualMode;
        FALSE //BOOLEAN AtRuntime;
} ;


//=============================================================================
// Module specific Global Variables

static EFI_MEMORY_DESCRIPTOR *pMap = NULL;
static UINTN MapSize, DescSize;
static UINT32 CrcTable[256];

//=============================================================================
// Function Prototypes

//this funciton is created from InitList.c template file during build process
VOID InitParts(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable);


EFI_STATUS SetVirtualAddressMap(
	IN UINTN MemoryMapSize, IN UINTN DescriptorSize,
	IN UINT32 DescriptorVersion, IN EFI_MEMORY_DESCRIPTOR *VirtualMap
);

EFI_STATUS ConvertPointer(
	IN UINTN DebugDisposition, IN VOID **Address
);

EFI_STATUS CalculateCrc32(
	IN VOID *Data, IN UINTN DataSize, OUT UINT32 *Crc32
);

VOID InitCrc();

//=============================================================================
// Function Definitions

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	RuntimeInitialize
//
// Description:	
//  This function is the Initialization function for the runtime driver that is
//  outside of SMM.  It sets up all the pieces of the Runtime Architectural
//  Protocol that exist outside of SMM
//
// Input:   
//  EFI_HANDLE ImageHandle - The firmware allocated handle for this driver
//  EFI_SYSTEM_TABLE *SystemTable - Pointer to the UEFI SystemTable
//
// Output:		
//  EFI_SUCCESS 
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS RuntimeInitialize(
	IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable
)
{
	EFI_HANDLE Handle = NULL;

	InitAmiRuntimeLib(ImageHandle, SystemTable, NULL, NULL);
	InitCrc();

	//this funciton is created from InitList.c template file during build process
	InitParts(ImageHandle, SystemTable);

    // Initialize pointers in the Runtime Services Table
	pRS->SetVirtualAddressMap = SetVirtualAddressMap;
	pRS->ConvertPointer = ConvertPointer;
	pBS->CalculateCrc32 = CalculateCrc32;

    // install the Runtime Achitectural Protocol
	pBS->InstallMultipleProtocolInterfaces(
        &Handle, &gEfiRuntimeArchProtocolGuid, &Runtime, NULL
	);
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	FindDescriptor
//
// Description:	
//  This function finds the memory descriptor that contains the pointer that 
//  is passed into the function.  This is used by the ConvertPointer function 
//  to determine how to find the virtual fixup address of the pointer
//
// Input:   
//	VOID *p - Pointer that needs to be fixed up
//
// Output:		
//  EFI_MEMORY_DESCRIPTOR * - Memory descriptor that contain the pointer that is passed in
//
// Notes:  
//  Used only by ConvertPointer function to help it convert the pointer from
//  a physical address to a virtual address
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_MEMORY_DESCRIPTOR* FindDescriptor(VOID *p)
{
	EFI_MEMORY_DESCRIPTOR *pD;
    EFI_PHYSICAL_ADDRESS Address = (EFI_PHYSICAL_ADDRESS)(UINTN)p;

    if(!pMap) return NULL;
    // Search for the memory descriptor that contains the address of the pointer 
    //  that was passed in
	for(pD = pMap
		;pD < (EFI_MEMORY_DESCRIPTOR*)((UINT8*)pMap + MapSize)
		;pD = (EFI_MEMORY_DESCRIPTOR*)((UINT8*)pD + DescSize)
	)	if (   (pD->Attribute & EFI_MEMORY_RUNTIME)
			&& pD->PhysicalStart <= Address 
			&& pD->PhysicalStart + Shl64(pD->NumberOfPages, EFI_PAGE_SHIFT)
			   > Address
		) return pD;
	return NULL;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SetVirtualAddressMap
//
// Description:	
//  This function is called by a UEFI OS.  It changes the pointers for the 
//  Runtime Services Table and each of the function pointers and variables 
//  that are its elements from physical addresses to virtual addresses.
//
// Input:   
//	IN UINTN MemoryMapSize - Size of the Current Memory Map
//  IN UINTN DescriptorSize - Size of one descriptor in the Memory Map
//  IN UINT32 DescriptorVersion - Version of the MemoryDescriptor Data Structure
//  IN EFI_MEMORY_DESCRIPTOR *VirtualMap - Address to the first Descriptor in the Virtual Memory Map
//
// Output:		
//  EFI_SUCCESS
//
// Notes:  
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS SetVirtualAddressMap(
    IN UINTN MemoryMapSize, 
    IN UINTN DescriptorSize,
    IN UINT32 DescriptorVersion, 
    IN EFI_MEMORY_DESCRIPTOR *VirtualMap
)
{
    EFI_LIST_ENTRY *pLink;
    VOID **p;
    UINTN i;
    UINT32 crc;
    EFI_RUNTIME_IMAGE_ENTRY *pThisImageEntry;
    VOID *VirtualImageBase;

    //Make a local copy of pRS or pST
    //it case one of the runtime drivers linked with this module
    //converts pRS or pST to virtual address in the event handler
    EFI_SYSTEM_TABLE	 *pSTCopy = pST;
    EFI_RUNTIME_SERVICES *pRSCopy = pRS;

    //SetVirtualAddressMap can only be called once	
    if(pMap) return EFI_UNSUPPORTED;
    checkpoint(0xB0);

    //Initialize Global Variables
    pMap = VirtualMap;
    MapSize = MemoryMapSize;
    DescSize = DescriptorSize;

    //Call Event notification funcitons
    for(pLink = Runtime.EventHead.ForwardLink; pLink != &Runtime.EventHead; pLink = pLink->ForwardLink)
    {
        EFI_RUNTIME_EVENT_ENTRY *pEvent = OUTTER(pLink, Link, EFI_RUNTIME_EVENT_ENTRY);
        if (!pEvent->NotifyFunction) continue;
        //Let's see if the notification function we about to call is a runtime function
        if (!FindDescriptor(pEvent->NotifyFunction)){
            TRACE((TRACE_DXE_CORE,"ERROR: Virtual address change callback at address %X resides in the boot time memory\n",pEvent->NotifyFunction));
            ASSERT(FALSE);
            continue;
        }
        ASSERT(!pEvent->NotifyContext || FindDescriptor(pEvent->NotifyFunction));
        pEvent->NotifyFunction(*pEvent->Event, pEvent->NotifyContext);
    }

    //Fixup runtime images
    for(pLink = Runtime.ImageHead.ForwardLink; pLink != &Runtime.ImageHead; pLink = pLink->ForwardLink)
    {
        EFI_RUNTIME_IMAGE_ENTRY *pImage = OUTTER(pLink, Link, EFI_RUNTIME_IMAGE_ENTRY);
        //skip this image; it will be fixed up later
        if (TheImageHandle == pImage->Handle){
            pThisImageEntry = pImage;
            continue;
        }
        VirtualImageBase = pImage->ImageBase;
        ConvertPointer(0, &VirtualImageBase);
        ReallocatePeImage(pImage->ImageBase, VirtualImageBase, pImage->RelocationData);
    }
    //Fixup RuntimeServices
    for(p = (VOID**)((UINT8*)pRSCopy + sizeof(pRSCopy->Hdr)); p < (VOID**)(pRSCopy + 1); p++)
        ConvertPointer(0, p);
    //Fixup Configuration Table
    for(i = 0; i < pSTCopy->NumberOfTableEntries; i++)
        ConvertPointer(0, &pSTCopy->ConfigurationTable[i].VendorTable);
    //Fixup SystemTable
    ConvertPointer(0, &pSTCopy->ConfigurationTable);
    ConvertPointer(0, &pSTCopy->RuntimeServices);
    ConvertPointer(0, &pSTCopy->FirmwareVendor);
    // Update CRC32 of the System Tables
    pRSCopy->Hdr.CRC32 = 0; crc = 0;
    CalculateCrc32(pRSCopy, sizeof(*pRSCopy), &crc);
    pRSCopy->Hdr.CRC32 = crc;
    pSTCopy->Hdr.CRC32 = 0; crc = 0;
    CalculateCrc32(pSTCopy, sizeof(*pSTCopy), &crc);
    pSTCopy->Hdr.CRC32 = crc;

    //finally, fixup ourself
    VirtualImageBase = pThisImageEntry->ImageBase;
    ConvertPointer(0, &VirtualImageBase);
	ReallocatePeImage(pThisImageEntry->ImageBase, VirtualImageBase, pThisImageEntry->RelocationData);

    checkpoint(0xB1);
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ConvertPointer
//
// Description:	
//  This function converts pointers from a Physical Memory Map Pointer 
//  to a Virtual Memory Map Pointer.  Called during the SetVirtualAddressMap
//  function execution.  
//
// Input:   
//	IN UINTN DebugDisposition - If set as EFI_OPTIONAL_POINTER, then Address is allowed to be NULL
//  IN VOID **Address - Pointer to convert to a virtual address
//
// Output:		
//  EFI_SUCCESS - Pointer converted to Virtual Address
//  EFI_INVALID_PARAMETER - DebugDisposition != EFI_OPTIONAL_POINTER and *Address == NULL
//  EFI_INVALID_PARAMETER - Address == NULL
//  EFI_UNSUPPORTED - Virtual Memory Map address has not be set
//  EFI_NOT_FOUND - Descriptor not found that contains the address of *Address
//
// Notes:  
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ConvertPointer(
	IN UINTN DebugDisposition, 
    IN VOID **Address
)
{
	EFI_MEMORY_DESCRIPTOR *pD;

    // Check for error conditions
	if(!pMap) return EFI_UNSUPPORTED;
	if (!Address) return EFI_INVALID_PARAMETER;
	if (!*Address)
		return (DebugDisposition == EFI_OPTIONAL_PTR) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;

    // find the descriptor that contains the address passed in  If not found, return error
	if (!(pD = FindDescriptor(*Address))) return EFI_NOT_FOUND;

    // if the descriptor is found, fixup the address to the new virtual address
	*Address = (VOID*)((EFI_PHYSICAL_ADDRESS)*Address - pD->PhysicalStart + pD->VirtualStart);
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	InitCrc
//
// Description:	
//  This function Initializes the CrcTable with lookup values for future 
//  use durning CRC32 calculations
//
// Input:   
//  None
//
// Output:
//  None
//
// Notes:  
//  At the end of this function the CrcTable will be initialized with lookup 
//  values that make calculating CRC32 values much easier
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID InitCrc( VOID )
{
	UINT32 i;
    UINT32 j;

	for(i = 0; i < 256; i++)
	{
		CrcTable[i] = i;
		for(j = 8; j > 0; j--) 
			CrcTable[i] = (CrcTable[i] & 1) 
    			? (CrcTable[i] >> 1)^0xedb88320 
    			: CrcTable[i] >> 1;
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	CalculateCrc32
//
// Description:	
//  This function Calculates a CRC32 value using the Data Buffer that is 
//  and the DataSize passed in 
//
// Input:   
//  IN VOID *Data - Pointer to the data to have the CRC function run on
//  IN UINTN DataSize - Size in bytes of the Data pointed to by *Data
//
// Output:
//  OUT UINT32 *Crc32 - CRC32 value calculated based on Data and Data Size
//
// Notes:  
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CalculateCrc32(
	IN VOID *Data, IN UINTN DataSize, OUT UINT32 *Crc32
)
{
	UINT32 i, crc = (UINT32)-1;
	if (!Data || !DataSize || !Crc32) return EFI_INVALID_PARAMETER;
	for(i = 0; i < DataSize; i++) crc = (crc >> 8)^CrcTable[(UINT8)crc^((UINT8*)Data)[i]];
	*Crc32 = ~crc;
	return EFI_SUCCESS;
}

//**********************************************************************
// RT SMM BEGIN
//**********************************************************************
//======================================================================
// SMM externs
extern EFI_GUID SmmRsTableGuid;

//======================================================================
// SMM Function Prototypes

//this funciton is created from InitList.c template file during build process
VOID InitParts2(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable);


//======================================================================
// SMM Function Definitions
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	DummySmmRtFunction
//
// Description:	
//  This function is a Dummy function that is used as a default function
//  for the SMM instance of the Runtime Services Table
//
// Input:   
//  None
//
// Output:
//  EFI_UNSUPPORTED
//
// Notes:  
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS DummySmmRtFunction(){ return EFI_UNSUPPORTED; }

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	RuntimeSmmInitialize
//
// Description:	
//  This function initializes the SMM version of the Runtime Services Table
//
// Input:   
//  EFI_HANDLE ImageHandle - The firmware allocated handle for this driver
//  EFI_SYSTEM_TABLE *SystemTable - Pointer to the UEFI SystemTable
//
// Output:
//  EFI_STATUS - based on the return values of the SmmInstallConfigurationTable function
//
// Notes:  
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS RuntimeSmmInitialize(
    EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable
)
{
    EFI_RUNTIME_SERVICES 	SmmRs;
    EFI_STATUS Status;
    VOID **p;

    //copy header
    SmmRs.Hdr = pRS->Hdr;
    //Init Runtime Services function pointers with our dummy function
    for(p = (VOID**)((UINT8*)&SmmRs + sizeof(SmmRs.Hdr)); p < (VOID**)(&SmmRs + 1); p++)
        *p = DummySmmRtFunction;

    // install the SMM version of the Runtime Services Table SMM Configuration Table
    Status = pSmst->SmmInstallConfigurationTable(
        pSmst, &SmmRsTableGuid, &SmmRs, sizeof(EFI_RUNTIME_SERVICES) );
    ASSERT_EFI_ERROR(Status);

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	RuntimeEntry
//
// Description:	
//  This function is the entry point for the Runtime Driver.  It initializes
//  the AMI Library and then it calls the Initialization functions for both
//  the SMM and NON-SMM versions of the Runtime Driver.  It then calls the 
//  InitParts2 function which calls the functions in the RuntimeSmmInitialize
//  eLink.
//
// Input:   
//  EFI_HANDLE ImageHandle - The firmware allocated handle for this driver
//  EFI_SYSTEM_TABLE *SystemTable - Pointer to the UEFI SystemTable
//
// Output:
//  EFI_STATUS - based on the return values of the InitSmmHandlerEx function
//
// Notes:  
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS RuntimeEntry (
    IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable
)
{
    EFI_STATUS Status;

    // Initialize the AMI Library for this module
	InitAmiLib(ImageHandle, SystemTable);
    
    // Initialize both the SMM and Non-SMM verions of the Runtime Services
	Status = InitSmmHandlerEx(
        ImageHandle, SystemTable, RuntimeSmmInitialize, RuntimeInitialize
    );
    InitParts2(ImageHandle, SystemTable);
    return Status;
}
//**********************************************************************
// RT SMM END
//**********************************************************************

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