#include "Runtime.h"\r
\r
//\r
-// This is a only short term solution.\r
-// There is a change coming to the Runtime AP that\r
-// will make it so the Runtime driver will not have to allocate any buffers. \r
+// Global Variables\r
//\r
-#define MAX_RUNTIME_IMAGE_NUM (64)\r
-#define MAX_RUNTIME_EVENT_NUM (64)\r
-RUNTIME_IMAGE_RELOCATION_DATA mRuntimeImageBuffer[MAX_RUNTIME_IMAGE_NUM];\r
-RUNTIME_NOTIFY_EVENT_DATA mRuntimeEventBuffer[MAX_RUNTIME_EVENT_NUM];\r
-UINTN mRuntimeImageNumber;\r
-UINTN mRuntimeEventNumber;\r
+EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r
+UINTN mVirtualMapDescriptorSize;\r
+UINTN mVirtualMapMaxIndex;\r
+VOID *mMyImageBase;\r
\r
//\r
// The handle onto which the Runtime Architectural Protocol instance is installed\r
// The Runtime Architectural Protocol instance produced by this driver\r
//\r
EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {\r
- RuntimeDriverRegisterImage,\r
- RuntimeDriverRegisterEvent\r
-};\r
-\r
-//\r
-// Global Variables\r
-//\r
-LIST_ENTRY mRelocationList = INITIALIZE_LIST_HEAD_VARIABLE(mRelocationList);\r
-LIST_ENTRY mEventList = INITIALIZE_LIST_HEAD_VARIABLE(mEventList);\r
-BOOLEAN mEfiVirtualMode = FALSE;\r
-EFI_GUID mLocalEfiUgaIoProtocolGuid = EFI_UGA_IO_PROTOCOL_GUID;\r
-EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r
-UINTN mVirtualMapDescriptorSize;\r
-UINTN mVirtualMapMaxIndex;\r
+ INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),\r
+ INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),\r
\r
-EFI_LOADED_IMAGE_PROTOCOL *mMyLoadedImage;\r
+ //\r
+ // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will\r
+ // prevent people from having pointer math bugs in their code.\r
+ // now you have to use *DescriptorSize to make things work.\r
+ //\r
+ sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)), \r
+ EFI_MEMORY_DESCRIPTOR_VERSION, \r
+ 0,\r
+ NULL,\r
+ NULL,\r
+ FALSE,\r
+ FALSE\r
+};\r
\r
//\r
// Worker Functions\r
\r
EFI_STATUS\r
EFIAPI\r
-RuntimeDriverRegisterImage (\r
- IN EFI_RUNTIME_ARCH_PROTOCOL *This,\r
- IN EFI_PHYSICAL_ADDRESS ImageBase,\r
- IN UINTN ImageSize,\r
- IN VOID *RelocationData\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- When a SetVirtualAddressMap() is performed all the runtime images loaded by \r
- DXE must be fixed up with the new virtual address map. To facilitate this the \r
- Runtime Architectural Protocol needs to be informed of every runtime driver \r
- that is registered. All the runtime images loaded by DXE should be registered \r
- with this service by the DXE Core when ExitBootServices() is called. The \r
- images that are registered with this service must have successfully been \r
- loaded into memory with the Boot Service LoadImage(). As a result, no \r
- parameter checking needs to be performed.\r
-\r
-Arguments:\r
-\r
- This - The EFI_RUNTIME_ARCH_PROTOCOL instance. \r
-\r
- ImageBase - Start of image that has been loaded in memory. It is either \r
- a pointer to the DOS or PE header of the image.\r
-\r
- ImageSize - Size of the image in bytes.\r
-\r
- RelocationData - Information about the fixups that were performed on ImageBase \r
- when it was loaded into memory. This information is needed \r
- when the virtual mode fix-ups are reapplied so that data that \r
- has been programmatically updated will not be fixed up. If \r
- code updates a global variable the code is responsible for \r
- fixing up the variable for virtual mode.\r
-\r
-Returns: \r
-\r
- EFI_SUCCESS - The ImageBase has been registered.\r
-\r
---*/\r
-{\r
- RUNTIME_IMAGE_RELOCATION_DATA *RuntimeImage;\r
-\r
- if (mMyLoadedImage->ImageBase == (VOID *) (UINTN) ImageBase) {\r
- //\r
- // We don't want to relocate our selves, as we only run in physical mode.\r
- //\r
- return EFI_SUCCESS;\r
- }\r
-\r
- RuntimeImage = &mRuntimeImageBuffer[mRuntimeImageNumber];\r
- mRuntimeImageNumber++;\r
- ASSERT (mRuntimeImageNumber < MAX_RUNTIME_IMAGE_NUM);\r
-\r
- RuntimeImage->Valid = TRUE;\r
- RuntimeImage->ImageBase = ImageBase;\r
- RuntimeImage->ImageSize = ImageSize;\r
- RuntimeImage->RelocationData = RelocationData;\r
-\r
- InsertTailList (&mRelocationList, &RuntimeImage->Link);\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-RuntimeDriverRegisterEvent (\r
- IN EFI_RUNTIME_ARCH_PROTOCOL *This,\r
- IN UINT32 Type,\r
- IN EFI_TPL NotifyTpl,\r
- IN EFI_EVENT_NOTIFY NotifyFunction,\r
- IN VOID *NotifyContext,\r
- IN EFI_EVENT *Event\r
+RuntimeDriverConvertPointer (\r
+ IN UINTN DebugDisposition,\r
+ IN OUT VOID **ConvertAddress\r
)\r
/*++\r
\r
Routine Description:\r
\r
- This function is used to support the required runtime events. Currently only \r
- runtime events of type EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE needs to be \r
- registered with this service. All the runtime events that exist in the DXE \r
- Core should be registered with this service when ExitBootServices() is called. \r
- All the events that are registered with this service must have been created \r
- with the Boot Service CreateEvent(). As a result, no parameter checking needs \r
- to be performed.\r
+ Determines the new virtual address that is to be used on subsequent memory accesses.\r
\r
Arguments:\r
+ \r
+ DebugDisposition - Supplies type information for the pointer being converted.\r
+ ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
+ for the new virtual address mappings being applied.\r
\r
- This - The EFI_RUNTIME_ARCH_PROTOCOL instance. \r
-\r
- Type - The same as Type passed into CreateEvent().\r
-\r
- NotifyTpl - The same as NotifyTpl passed into CreateEvent().\r
-\r
- NotifyFunction - The same as NotifyFunction passed into CreateEvent().\r
-\r
- NotifyContext - The same as NotifyContext passed into CreateEvent().\r
-\r
- Event - The EFI_EVENT returned by CreateEvent(). Event must be in \r
- runtime memory.\r
-\r
-Returns: \r
+Returns:\r
\r
- EFI_SUCCESS - The Event has been registered.\r
+ EFI_SUCCESS - The pointer pointed to by Address was modified.\r
+ EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
+ of the current memory map. This is normally fatal.\r
+ EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
\r
--*/\r
-{\r
- RUNTIME_NOTIFY_EVENT_DATA *RuntimeEvent;\r
-\r
- RuntimeEvent = &mRuntimeEventBuffer[mRuntimeEventNumber];\r
- mRuntimeEventNumber++;\r
- ASSERT (mRuntimeEventNumber < MAX_RUNTIME_EVENT_NUM);\r
-\r
- RuntimeEvent->Type = Type;\r
- RuntimeEvent->NotifyTpl = NotifyTpl;\r
- RuntimeEvent->NotifyFunction = NotifyFunction;\r
- RuntimeEvent->NotifyContext = NotifyContext;\r
- RuntimeEvent->Event = Event;\r
-\r
- InsertTailList (&mEventList, &RuntimeEvent->Link);\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-RuntimeDriverConvertPointer (\r
- IN UINTN DebugDisposition,\r
- IN OUT VOID **ConvertAddress\r
- )\r
{\r
UINTN Address;\r
VOID *PlabelConvertAddress;\r
// platforms. If you get this ASSERT remove the UINTN and do a 64-bit\r
// multiply.\r
//\r
- ASSERT ((VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r
+ ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r
\r
if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
if (Address >= VirtEntry->PhysicalStart) {\r
RuntimeDriverConvertInternalPointer (\r
IN OUT VOID **ConvertAddress\r
)\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Determines the new virtual address that is to be used on subsequent memory accesses \r
+ for internal pointers.\r
+\r
+Arguments:\r
+ \r
+ ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
+ for the new virtual address mappings being applied.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - The pointer pointed to by Address was modified.\r
+ EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
+ of the current memory map. This is normally fatal.\r
+ EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
+\r
+--*/\r
{\r
return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r
}\r
IN UINT32 DescriptorVersion,\r
IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r
)\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Changes the runtime addressing mode of EFI firmware from physical to virtual.\r
+\r
+Arguments:\r
+ \r
+ MemoryMapSize - The size in bytes of VirtualMap.\r
+ DescriptorSize - The size in bytes of an entry in the VirtualMap.\r
+ DescriptorVersion - The version of the structure entries in VirtualMap.\r
+ VirtualMap - An array of memory descriptors which contain new virtual\r
+ address mapping information for all runtime ranges.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - The virtual address map has been applied.\r
+ EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in\r
+ virtual address mapped mode.\r
+ EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.\r
+ EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory\r
+ map that requires a mapping.\r
+ EFI_NOT_FOUND - A virtual address was supplied for an address that is not found\r
+ in the memory map.\r
+\r
+--*/ \r
{\r
- RUNTIME_NOTIFY_EVENT_DATA *RuntimeEvent;\r
- RUNTIME_IMAGE_RELOCATION_DATA *RuntimeImage;\r
+ EFI_STATUS Status;\r
+ EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r
+ EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r
LIST_ENTRY *Link;\r
UINTN Index;\r
UINTN Index1;\r
EFI_DRIVER_OS_HANDOFF_HEADER *DriverOsHandoffHeader;\r
EFI_DRIVER_OS_HANDOFF *DriverOsHandoff;\r
+ EFI_PHYSICAL_ADDRESS VirtImageBase;\r
+#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
+ EFI_CAPSULE_TABLE *CapsuleTable; \r
+#endif\r
\r
//\r
// Can only switch to virtual addresses once the memory map is locked down,\r
// and can only set it once\r
//\r
- if (!EfiAtRuntime () || mEfiVirtualMode) {\r
+ if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r
return EFI_UNSUPPORTED;\r
}\r
//\r
return EFI_INVALID_PARAMETER;\r
}\r
//\r
- // BugBug: Add code here to verify the memory map. We should\r
- // cache a copy of the system memory map in the EFI System Table\r
- // as a GUID pointer pair.\r
- //\r
- //\r
- // Make sure all virtual translations are satisfied\r
- //\r
- mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
-\r
- //\r
- // BugBug :The following code does not work hence commented out.\r
- // Need to be replaced by something that works.\r
- //\r
- // VirtEntry = VirtualMap;\r
- // for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {\r
- // if (((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) &&\r
- // (VirtEntry->VirtualStart != 0) ) {\r
- // return EFI_NO_MAPPING;\r
- // }\r
- // VirtEntry = NextMemoryDescriptor(VirtEntry, DescriptorSize);\r
- // }\r
- //\r
// We are now committed to go to virtual mode, so lets get to it!\r
//\r
- mEfiVirtualMode = TRUE;\r
+ mRuntime.VirtualMode = TRUE;\r
\r
//\r
// ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r
// globals we need to parse the virtual address map.\r
//\r
mVirtualMapDescriptorSize = DescriptorSize;\r
+ mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
mVirtualMap = VirtualMap;\r
\r
- //\r
- // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
- // The core call RuntimeDriverRegisterEvent() for\r
- // every runtime event and we stored them in the mEventList\r
- //\r
//\r
// Currently the bug in StatusCode/RuntimeLib has been fixed, it will\r
// check whether in Runtime or not (this is judged by looking at\r
);\r
\r
//\r
- // BugBug - Commented out for now because the status code driver is not\r
- // ready for runtime yet. The Status Code driver calls data hub with does\r
- // a bunch of Boot Service things (locks, AllocatePool) and hangs somewhere\r
- // on the way.\r
- //\r
- // ReportStatusCode (\r
- // EfiProgressCode, EfiMaxErrorSeverity,\r
- // 0x03, 0x01, 12, // ReadyToBoot Progress code\r
- // 0x00, 30, L"ConvertPointer"\r
- // );\r
+ // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
+ // All runtime events are stored in a list in Runtime AP.\r
//\r
- for (Link = mEventList.ForwardLink; Link != &mEventList; Link = Link->ForwardLink) {\r
- RuntimeEvent = _CR (Link, RUNTIME_NOTIFY_EVENT_DATA, Link);\r
+ for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r
+ RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r
if ((RuntimeEvent->Type & EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r
RuntimeEvent->NotifyFunction (\r
RuntimeEvent->Event,\r
);\r
}\r
}\r
+\r
//\r
- // Relocate runtime images. Runtime images loaded before the runtime AP was\r
- // started will not be relocated, since they would not have gotten registered.\r
- // This includes the code in this file.\r
+ // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r
//\r
- for (Link = mRelocationList.ForwardLink; Link != &mRelocationList; Link = Link->ForwardLink) {\r
- RuntimeImage = _CR (Link, RUNTIME_IMAGE_RELOCATION_DATA, Link);\r
- if (RuntimeImage->Valid) {\r
- RelocatePeImageForRuntime (RuntimeImage);\r
+ for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r
+ RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r
+ //\r
+ // We don't want to relocate our selves, as we only run in physical mode.\r
+ //\r
+ if (mMyImageBase != RuntimeImage->ImageBase) {\r
+\r
+ VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;\r
+ Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ PeCoffLoaderRelocateImageForRuntime (\r
+ (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,\r
+ VirtImageBase,\r
+ (UINTN) RuntimeImage->ImageSize,\r
+ RuntimeImage->RelocationData\r
+ );\r
+ \r
+ InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);\r
}\r
}\r
+\r
//\r
// Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r
// and recompute the CRC-32\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);\r
+#if (EFI_SPECIFICATION_VERSION < 0x00020000)\r
+ RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ReportStatusCode);\r
+#endif\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);\r
RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);\r
+#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
+ RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);\r
+ RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);\r
+ RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);\r
+#endif\r
RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r
\r
//\r
// Convert the UGA OS Handoff Table if it is present in the Configuration Table\r
//\r
for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {\r
- if (CompareGuid (&mLocalEfiUgaIoProtocolGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
+ if (CompareGuid (&gEfiUgaIoProtocolGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
DriverOsHandoffHeader = gST->ConfigurationTable[Index].VendorTable;\r
for (Index1 = 0; Index1 < DriverOsHandoffHeader->NumberOfEntries; Index1++) {\r
DriverOsHandoff = (EFI_DRIVER_OS_HANDOFF *)\r
\r
RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &(gST->ConfigurationTable[Index].VendorTable));\r
}\r
+\r
+#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
+ if (CompareGuid (&gEfiCapsuleGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
+ CapsuleTable = gST->ConfigurationTable[Index].VendorTable;\r
+ for (Index1 = 0; Index1 < CapsuleTable->CapsuleArrayNumber; Index1++) {\r
+ RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &CapsuleTable->CapsulePtr[Index1]);\r
+ } \r
+ RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &(gST->ConfigurationTable[Index].VendorTable));\r
+ }\r
+#endif\r
}\r
//\r
// Convert the runtime fields of the EFI System Table and recompute the CRC-32\r
\r
--*/\r
{\r
- EFI_STATUS Status;\r
+ EFI_STATUS Status;\r
+ EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r
\r
//\r
// This image needs to be exclued from relocation for virtual mode, so cache\r
Status = gBS->HandleProtocol (\r
ImageHandle,\r
&gEfiLoadedImageProtocolGuid,\r
- (VOID **) &mMyLoadedImage\r
+ (VOID**)&MyLoadedImage\r
);\r
ASSERT_EFI_ERROR (Status);\r
+ mMyImageBase = MyLoadedImage->ImageBase;\r
\r
//\r
// Initialize the table used to compute 32-bit CRCs\r
//\r
// Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r
//\r
- gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
+ gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r
gRT->ConvertPointer = RuntimeDriverConvertPointer;\r
\r
NULL\r
);\r
ASSERT_EFI_ERROR (Status);\r
-\r
- mRuntimeImageNumber = 0;\r
- mRuntimeEventNumber = 0;\r
-\r
- return Status;\r
+ \r
+ return EFI_SUCCESS;\r
}\r