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) {
206 if ((Desc->Type != EfiReservedMemoryType) && ((Desc->Attribute & EFI_MEMORY_RUNTIME) != 0)) {
210 if (KernelState->VmMapSize + DescriptorSize > sizeof (KernelState->VmMap)) {
211 RUNTIME_DEBUG ((DEBUG_ERROR,
"OCABC: Too many RT entries to memory map\n"));
212 return EFI_OUT_OF_RESOURCES;
218 CopyMem (VirtualDesc, Desc, DescriptorSize);
224 &KernelState->VmContext,
230 if (EFI_ERROR (Status)) {
231 RUNTIME_DEBUG ((DEBUG_ERROR,
"OCABC: RT mapping failure - %r\n", Status));
232 return EFI_OUT_OF_RESOURCES;
238 VirtualDesc = NEXT_MEMORY_DESCRIPTOR (VirtualDesc, DescriptorSize);
239 KernelState->VmMapSize += DescriptorSize;
245 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
250 Status =
gRT->SetVirtualAddressMap (
251 KernelState->VmMapSize,
272 IN UINTN MemoryMapSize,
273 IN UINTN DescriptorSize,
274 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap
279 UINTN NumEntriesLeft;
281 EFI_PHYSICAL_ADDRESS PhysicalStart;
282 EFI_PHYSICAL_ADDRESS PhysicalEnd;
283 EFI_MEMORY_DESCRIPTOR *Desc;
285 NumEntriesLeft = RtReloc->NumEntries;
286 NumEntries = MemoryMapSize / DescriptorSize;
289 for (Index = 0; Index < NumEntries && NumEntriesLeft > 0; ++Index) {
290 PhysicalStart = Desc->PhysicalStart;
293 for (Index2 = 0; Index2 < RtReloc->NumEntries; ++Index2) {
300 if ( (PhysicalStart == RtReloc->RelocInfo[Index2].PhysicalStart)
301 && (PhysicalEnd == RtReloc->RelocInfo[Index2].PhysicalEnd))
303 Desc->Type = RtReloc->RelocInfo[Index2].Type;
309 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
312 if (NumEntriesLeft > 0) {
316 "OCABC: Failed to restore %u entries out of %u\n",
317 (UINT32)NumEntriesLeft,
318 (UINT32)RtReloc->NumEntries
327 IN OUT VOID *BootArgs
336 EFI_MEMORY_DESCRIPTOR *MemoryMap;
337 UINTN DescriptorSize;
341 if (BootCompat->Settings.ProvideCustomSlide) {
348 if (BootCompat->Settings.DisableSingleUser) {
355 if ( BootCompat->Settings.DisableSingleUser
356 || BootCompat->Settings.ForceBooterSignature)
360 if (!EFI_ERROR (Status)) {
361 if (BootCompat->Settings.DisableSingleUser) {
365 Status =
DTGetProperty (Chosen,
"boot-args", (VOID **)&ArgsStr, &ArgsSize);
366 if (!EFI_ERROR (Status) && (ArgsSize > 0)) {
371 if (BootCompat->Settings.ForceBooterSignature) {
372 Status =
DTGetProperty (Chosen,
"boot-signature", (VOID **)&ArgsStr, &ArgsSize);
374 CopyMem (ArgsStr, BootCompat->Settings.BooterSignature, ArgsSize);
380 if ((BootCompat->KernelState.RelocationBlock != 0) && !BootCompat->KernelState.RelocationBlockLegacy) {
394 if (BootCompat->Settings.AvoidRuntimeDefrag) {
396 MemoryMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)(*BA.
MemoryMap);
404 &BootCompat->RtReloc,
421 if (BootCompat->KernelState.ConfigurationTable != NULL) {
423 (VOID *)((UINTN)BA.
SystemTable->ConfigurationTable & (BASE_1GB - 1)),
424 BootCompat->KernelState.ConfigurationTable,
425 sizeof (*BootCompat->KernelState.ConfigurationTable) * BA.
SystemTable->NumberOfTableEntries
430 if (BootCompat->KernelState.RelocationBlock != 0) {
438 IN UINTN ImageHeaderPage
471 if (BootCompat->Settings.DiscardHibernateMap) {
482 if (BootCompat->KernelState.VmMapDescSize == 0) {
483 RUNTIME_DEBUG ((DEBUG_ERROR,
"OCABC: Saved descriptor size cannot be 0\n"));
492 if (BootCompat->Settings.AvoidRuntimeDefrag) {
497 &BootCompat->RtReloc,
499 BootCompat->KernelState.VmMapDescSize,
500 (EFI_MEMORY_DESCRIPTOR *)(UINTN)Handoff->
data
524 if ( !BootCompat->Settings.SetupVirtualMap
525 || (BootCompat->KernelState.VmContext.MemoryPool != NULL))
531 &BootCompat->KernelState.VmContext,
533 BootCompat->ServicePtrs.GetMemoryMap
536 if (EFI_ERROR (Status)) {
537 DEBUG ((DEBUG_ERROR,
"OCABC: Memory pool allocation failure - %r\n", Status));
544 IN OUT EFI_LOADED_IMAGE *LoadedImage,
545 IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL
557 if (BootCompat->Settings.AvoidRuntimeDefrag) {
558 if (BootCompat->KernelState.SysTableRtArea == 0) {
565 BootCompat->KernelState.SysTableRtArea = BASE_4GB;
566 BootCompat->KernelState.SysTableRtAreaSize =
gST->Hdr.HeaderSize;
568 EfiRuntimeServicesData,
569 EFI_SIZE_TO_PAGES (
gST->Hdr.HeaderSize),
570 &BootCompat->KernelState.SysTableRtArea,
575 if (EFI_ERROR (Status)) {
578 "OCABC: Failed to allocate system table memory - %r\n",
581 BootCompat->KernelState.SysTableRtArea = 0;
589 (VOID *)(UINTN)BootCompat->KernelState.SysTableRtArea,
596 BootCompat->KernelState.ConfigurationTable =
gST->ConfigurationTable;
602 LoadedImage->SystemTable =
603 (EFI_SYSTEM_TABLE *)(UINTN)BootCompat->KernelState.SysTableRtArea;
610 IN UINTN MemoryMapSize,
611 IN UINTN DescriptorSize,
612 IN UINT32 DescriptorVersion,
613 IN EFI_MEMORY_DESCRIPTOR *MemoryMap
619 EFI_MEMORY_DESCRIPTOR *Desc;
624 if (BootCompat->Settings.AvoidRuntimeDefrag) {
626 &BootCompat->RtReloc,
630 BootCompat->KernelState.SysTableRtArea,
631 BootCompat->KernelState.SysTableRtAreaSize
639 if (BootCompat->KernelState.RelocationBlockLegacy) {
641 NumEntries = MemoryMapSize / DescriptorSize;
643 for (Index = 0; Index < NumEntries; ++Index) {
644 if ( (Desc->VirtualStart >= BootCompat->KernelState.RelocationBlock + BootCompat->KernelState.RelocationBlockUsed)
647 Desc->VirtualStart -= BootCompat->KernelState.RelocationBlock -
KERNEL_BASE_PADDR;
650 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
657 if (BootCompat->Settings.SetupVirtualMap) {
659 &BootCompat->KernelState,
666 Status =
gRT->SetVirtualAddressMap (
677 if (BootCompat->Settings.AvoidRuntimeDefrag) {
679 (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