21#include <Library/BaseLib.h>
22#include <Library/BaseMemoryLib.h>
23#include <Library/DebugLib.h>
30#include <Library/PrintLib.h>
31#include <Library/UefiBootServicesTableLib.h>
32#include <Library/UefiLib.h>
33#include <Library/UefiRuntimeServicesTableLib.h>
49 IN UINTN MemoryMapSize,
50 IN UINTN DescriptorSize,
51 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
52 IN EFI_PHYSICAL_ADDRESS SysTableArea,
53 IN UINTN SysTableAreaSize
91 EFI_MEMORY_DESCRIPTOR *Desc;
95 RtReloc->NumEntries = 0;
96 RelocInfo = &RtReloc->RelocInfo[0];
97 NumEntries = MemoryMapSize / DescriptorSize;
99 for (Index = 0; Index < NumEntries; ++Index) {
100 if ( ((Desc->Attribute & EFI_MEMORY_RUNTIME) != 0)
101 && (Desc->NumberOfPages > 0)
102 && ((Desc->Type == EfiRuntimeServicesCode) || (Desc->Type == EfiRuntimeServicesData))
105 if (RtReloc->NumEntries ==
ARRAY_SIZE (RtReloc->RelocInfo)) {
109 "OCABC: Cannot save mem type for entry: %Lx (type 0x%x)\n",
110 (UINT64)Desc->PhysicalStart,
119 RelocInfo->
Type = Desc->Type;
120 Desc->Type = EfiMemoryMappedIO;
122 ++RtReloc->NumEntries;
125 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
145 IN UINTN MemoryMapSize,
146 IN UINTN DescriptorSize,
147 IN UINT32 DescriptorVersion,
148 IN EFI_MEMORY_DESCRIPTOR *MemoryMap
172 EFI_MEMORY_DESCRIPTOR *Desc;
173 EFI_MEMORY_DESCRIPTOR *VirtualDesc;
178 NumEntries = MemoryMapSize / DescriptorSize;
179 VirtualDesc = KernelState->VmMap;
180 KernelState->VmMapSize = 0;
181 KernelState->VmMapDescSize = DescriptorSize;
188 for (Index = 0; Index < NumEntries; ++Index) {
207 if ((Desc->Type != EfiReservedMemoryType) && ((Desc->Attribute & EFI_MEMORY_RUNTIME) != 0)) {
211 if (KernelState->VmMapSize + DescriptorSize > sizeof (KernelState->VmMap)) {
212 RUNTIME_DEBUG ((DEBUG_ERROR,
"OCABC: Too many RT entries to memory map\n"));
213 return EFI_OUT_OF_RESOURCES;
219 CopyMem (VirtualDesc, Desc, DescriptorSize);
225 &KernelState->VmContext,
231 if (EFI_ERROR (Status)) {
232 RUNTIME_DEBUG ((DEBUG_ERROR,
"OCABC: RT mapping failure - %r\n", Status));
233 return EFI_OUT_OF_RESOURCES;
239 VirtualDesc = NEXT_MEMORY_DESCRIPTOR (VirtualDesc, DescriptorSize);
240 KernelState->VmMapSize += DescriptorSize;
246 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
251 Status =
gRT->SetVirtualAddressMap (
252 KernelState->VmMapSize,
273 IN UINTN MemoryMapSize,
274 IN UINTN DescriptorSize,
275 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap
280 UINTN NumEntriesLeft;
282 EFI_PHYSICAL_ADDRESS PhysicalStart;
283 EFI_PHYSICAL_ADDRESS PhysicalEnd;
284 EFI_MEMORY_DESCRIPTOR *Desc;
286 NumEntriesLeft = RtReloc->NumEntries;
287 NumEntries = MemoryMapSize / DescriptorSize;
290 for (Index = 0; Index < NumEntries && NumEntriesLeft > 0; ++Index) {
291 PhysicalStart = Desc->PhysicalStart;
294 for (Index2 = 0; Index2 < RtReloc->NumEntries; ++Index2) {
301 if ( (PhysicalStart == RtReloc->RelocInfo[Index2].PhysicalStart)
302 && (PhysicalEnd == RtReloc->RelocInfo[Index2].PhysicalEnd))
304 Desc->Type = RtReloc->RelocInfo[Index2].Type;
310 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
313 if (NumEntriesLeft > 0) {
317 "OCABC: Failed to restore %u entries out of %u\n",
318 (UINT32)NumEntriesLeft,
319 (UINT32)RtReloc->NumEntries
328 IN OUT VOID *BootArgs
337 EFI_MEMORY_DESCRIPTOR *MemoryMap;
338 UINTN DescriptorSize;
342 if (BootCompat->Settings.ProvideCustomSlide) {
349 if (BootCompat->Settings.DisableSingleUser) {
356 if ( BootCompat->Settings.DisableSingleUser
357 || BootCompat->Settings.ForceBooterSignature)
361 if (!EFI_ERROR (Status)) {
362 if (BootCompat->Settings.DisableSingleUser) {
366 Status =
DTGetProperty (Chosen,
"boot-args", (VOID **)&ArgsStr, &ArgsSize);
367 if (!EFI_ERROR (Status) && (ArgsSize > 0)) {
372 if (BootCompat->Settings.ForceBooterSignature) {
373 Status =
DTGetProperty (Chosen,
"boot-signature", (VOID **)&ArgsStr, &ArgsSize);
375 CopyMem (ArgsStr, BootCompat->Settings.BooterSignature, ArgsSize);
381 if ((BootCompat->KernelState.RelocationBlock != 0) && !BootCompat->KernelState.RelocationBlockLegacy) {
395 if (BootCompat->Settings.AvoidRuntimeDefrag) {
397 MemoryMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)(*BA.
MemoryMap);
405 &BootCompat->RtReloc,
422 if (BootCompat->KernelState.ConfigurationTable != NULL) {
424 (VOID *)((UINTN)BA.
SystemTable->ConfigurationTable & (BASE_1GB - 1)),
425 BootCompat->KernelState.ConfigurationTable,
426 sizeof (*BootCompat->KernelState.ConfigurationTable) * BA.
SystemTable->NumberOfTableEntries
431 if (BootCompat->KernelState.RelocationBlock != 0) {
439 IN UINTN ImageHeaderPage
472 if (BootCompat->Settings.DiscardHibernateMap) {
483 if (BootCompat->KernelState.VmMapDescSize == 0) {
484 RUNTIME_DEBUG ((DEBUG_ERROR,
"OCABC: Saved descriptor size cannot be 0\n"));
493 if (BootCompat->Settings.AvoidRuntimeDefrag) {
498 &BootCompat->RtReloc,
500 BootCompat->KernelState.VmMapDescSize,
501 (EFI_MEMORY_DESCRIPTOR *)(UINTN)Handoff->
data
525 if ( !BootCompat->Settings.SetupVirtualMap
526 || (BootCompat->KernelState.VmContext.MemoryPool != NULL))
532 &BootCompat->KernelState.VmContext,
534 BootCompat->ServicePtrs.GetMemoryMap
537 if (EFI_ERROR (Status)) {
538 DEBUG ((DEBUG_ERROR,
"OCABC: Memory pool allocation failure - %r\n", Status));
545 IN OUT EFI_LOADED_IMAGE *LoadedImage,
546 IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL
558 if (BootCompat->Settings.AvoidRuntimeDefrag) {
559 if (BootCompat->KernelState.SysTableRtArea == 0) {
566 BootCompat->KernelState.SysTableRtArea = BASE_4GB;
567 BootCompat->KernelState.SysTableRtAreaSize =
gST->Hdr.HeaderSize;
569 EfiRuntimeServicesData,
570 EFI_SIZE_TO_PAGES (
gST->Hdr.HeaderSize),
571 &BootCompat->KernelState.SysTableRtArea,
576 if (EFI_ERROR (Status)) {
579 "OCABC: Failed to allocate system table memory - %r\n",
582 BootCompat->KernelState.SysTableRtArea = 0;
586 DEBUG ((DEBUG_VERBOSE,
"OCABC: Allocated SysTableRtArea %p\n", BootCompat->KernelState.SysTableRtArea));
592 (VOID *)(UINTN)BootCompat->KernelState.SysTableRtArea,
599 BootCompat->KernelState.ConfigurationTable =
gST->ConfigurationTable;
605 LoadedImage->SystemTable =
606 (EFI_SYSTEM_TABLE *)(UINTN)BootCompat->KernelState.SysTableRtArea;
613 IN UINTN MemoryMapSize,
614 IN UINTN DescriptorSize,
615 IN UINT32 DescriptorVersion,
616 IN EFI_MEMORY_DESCRIPTOR *MemoryMap
622 EFI_MEMORY_DESCRIPTOR *Desc;
627 if (BootCompat->Settings.AvoidRuntimeDefrag) {
629 &BootCompat->RtReloc,
633 BootCompat->KernelState.SysTableRtArea,
634 BootCompat->KernelState.SysTableRtAreaSize
642 if (BootCompat->KernelState.RelocationBlockLegacy) {
644 NumEntries = MemoryMapSize / DescriptorSize;
646 for (Index = 0; Index < NumEntries; ++Index) {
647 if ( (Desc->VirtualStart >= BootCompat->KernelState.RelocationBlock + BootCompat->KernelState.RelocationBlockUsed)
650 Desc->VirtualStart -= BootCompat->KernelState.RelocationBlock -
KERNEL_BASE_PADDR;
653 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
660 if (BootCompat->Settings.SetupVirtualMap) {
662 &BootCompat->KernelState,
669 Status =
gRT->SetVirtualAddressMap (
680 if (BootCompat->Settings.AvoidRuntimeDefrag) {
682 (VOID *)(UINTN)BootCompat->KernelState.SysTableRtArea,
@ kIOHibernateHandoffTypeEnd
@ kIOHibernateHandoffTypeMemoryMap
@ kIOHibernateHandoffType
#define ARRAY_SIZE(Array)
VOID AppleSlideRestore(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT OC_BOOT_ARGUMENTS *BootArgs)
EFI_STATUS AppleRelocationVirtualize(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT OC_BOOT_ARGUMENTS *BA)
VOID AppleRelocationRebase(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT OC_BOOT_ARGUMENTS *BA)
#define KERNEL_BASE_PADDR
#define ESTIMATED_KERNEL_SIZE
STATIC VOID ProtectRtMemoryFromRelocation(IN OUT RT_RELOC_PROTECT_DATA *RtReloc, IN UINTN MemoryMapSize, IN UINTN DescriptorSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN EFI_PHYSICAL_ADDRESS SysTableArea, IN UINTN SysTableAreaSize)
STATIC EFI_STATUS PerformRtMemoryVirtualMapping(IN OUT KERNEL_SUPPORT_STATE *KernelState, IN UINTN MemoryMapSize, IN UINTN DescriptorSize, IN UINT32 DescriptorVersion, IN EFI_MEMORY_DESCRIPTOR *MemoryMap)
VOID AppleMapPrepareForHibernateWake(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN UINTN ImageHeaderPage)
VOID AppleMapPrepareBooterState(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT EFI_LOADED_IMAGE *LoadedImage, IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL)
EFI_STATUS AppleMapPrepareMemState(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN UINTN MemoryMapSize, IN UINTN DescriptorSize, IN UINT32 DescriptorVersion, IN EFI_MEMORY_DESCRIPTOR *MemoryMap)
VOID AppleMapPrepareMemoryPool(IN OUT BOOT_COMPAT_CONTEXT *BootCompat)
STATIC VOID RestoreProtectedRtMemoryTypes(IN RT_RELOC_PROTECT_DATA *RtReloc, IN UINTN MemoryMapSize, IN UINTN DescriptorSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap)
VOID AppleMapPrepareForBooting(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT VOID *BootArgs)
VOID OcRemoveArgumentFromCmd(IN OUT CHAR8 *CommandLine, IN CONST CHAR8 *Argument)
VOID OcParseBootArgs(OUT OC_BOOT_ARGUMENTS *Arguments, IN VOID *BootArgs)
EFI_STATUS DTLookupEntry(IN CONST DTEntry SearchPoint, IN CONST CHAR8 *PathName, IN DTEntry *FoundEntry)
VOID DTInit(IN VOID *Base, IN UINT32 *Length)
EFI_STATUS DTGetProperty(IN CONST DTEntry Entry, IN CHAR8 *PropertyName, IN VOID **PropertyValue, IN UINT32 *PropertySize)
#define AREA_WITHIN_DESCRIPTOR(Desc, Area, AreaSize)
PAGE_MAP_AND_DIRECTORY_POINTER * OcGetCurrentPageTable(OUT UINTN *Flags OPTIONAL)
EFI_STATUS OcAllocatePagesFromTop(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL, IN EFI_ALLOCATE_PAGES AllocatePages OPTIONAL, IN CHECK_ALLOCATION_RANGE CheckRange OPTIONAL)
EFI_STATUS VmMapVirtualPages(IN OUT OC_VMEM_CONTEXT *Context, IN OUT PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL, IN EFI_VIRTUAL_ADDRESS VirtualAddr, IN UINT64 NumPages, IN EFI_PHYSICAL_ADDRESS PhysicalAddr)
#define LAST_DESCRIPTOR_ADDR(Desc)
EFI_STATUS VmAllocateMemoryPool(OUT OC_VMEM_CONTEXT *Context, IN UINTN NumPages, IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL)
#define OC_DEFAULT_VMEM_PAGE_COUNT
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
EFI_RUNTIME_SERVICES * gRT
UINT32 * MemoryMapDescriptorSize
EFI_SYSTEM_TABLE * SystemTable
UINT32 * DeviceTreeLength
EFI_PHYSICAL_ADDRESS PhysicalStart
EFI_PHYSICAL_ADDRESS PhysicalEnd