OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
KernelSupport32.c
Go to the documentation of this file.
1
16
17#include <Guid/OcVariable.h>
19
20#include <Library/BaseLib.h>
21#include <Library/BaseMemoryLib.h>
22#include <Library/DebugLib.h>
26#include <Library/OcMachoLib.h>
27#include <Library/OcMemoryLib.h>
28#include <Library/OcMiscLib.h>
29#include <Library/OcStringLib.h>
30#include <Library/PrintLib.h>
31#include <Library/UefiBootServicesTableLib.h>
32#include <Library/UefiLib.h>
33#include <Library/UefiRuntimeServicesTableLib.h>
34
35VOID
37 IN OUT BOOT_COMPAT_CONTEXT *BootCompat
38 )
39{
40 UINT8 *KernelEntry;
41 UINT32 Offset;
42 BOOLEAN Found;
43
44 //
45 // There is no reason to patch the kernel when we do not need it.
46 //
47 if ( !BootCompat->Settings.AvoidRuntimeDefrag
48 && !BootCompat->Settings.DiscardHibernateMap
49 && !BootCompat->Settings.AllowRelocationBlock
50 && !BootCompat->Settings.DisableSingleUser
51 && !BootCompat->Settings.ForceBooterSignature)
52 {
53 return;
54 }
55
56 //
57 // TODO: Add support for 32-bit hibernation some day.
58 //
59 if (BootCompat->ServiceState.AppleHibernateWake) {
60 RUNTIME_DEBUG ((DEBUG_ERROR, "OCABC: Cannot patch 32-bit kernel in hibernate wake!\n"));
61 CpuDeadLoop ();
62 }
63
64 //
65 // There are no call gates in 32-bit mode.
66 //
67 ASSERT (BootCompat->ServiceState.OldKernelCallGate == 0);
68
69 //
70 // Search for kernel entry point.
71 // This sequence seems to be relatively stable for 10.4 ~ 10.6.
72 //
73 STATIC CONST UINT8 mEntryBytes[] = {
74 0x66, 0x8C, 0xDB,
75 0x8E, 0xC3,
76 0x89, 0xC5,
77 };
78
79 Offset = 0;
80
81 if (BootCompat->KernelState.RelocationBlock != 0) {
82 Found = FindPattern (
83 mEntryBytes,
84 NULL,
85 sizeof (mEntryBytes),
86 (VOID *)(UINTN)BootCompat->KernelState.RelocationBlock,
87 BootCompat->KernelState.RelocationBlockUsed,
88 &Offset
89 );
90 } else {
91 Found = FindPattern (
92 mEntryBytes,
93 NULL,
94 sizeof (mEntryBytes),
95 (VOID *)(UINTN)KERNEL_BASE_PADDR,
96 BASE_2MB,
97 &Offset
98 );
99 }
100
101 if (!Found) {
102 RUNTIME_DEBUG ((DEBUG_ERROR, "OCABC: No 32-bit kernel entry!\n"));
103 CpuDeadLoop ();
104 }
105
106 KernelEntry = (VOID *)(UINTN)(KERNEL_BASE_PADDR + Offset);
107 KernelEntry[0] = 0xE8;
108 Offset = (UINTN)((UINTN)AsmAppleMapPrepareKernelState32 - (UINTN)KernelEntry - 5);
109 CopyMem (&KernelEntry[1], &Offset, sizeof (UINT32));
110}
111
112UINTN
113EFIAPI
115 IN UINTN Args
116 )
117{
118 BOOT_COMPAT_CONTEXT *BootCompat;
119
120 BootCompat = GetBootCompatContext ();
121
122 if (BootCompat->ServiceState.AppleHibernateWake) {
123 //
124 // TODO: This is not really implemented.
125 //
127 BootCompat,
128 Args
129 );
130 } else {
132 BootCompat,
133 (VOID *)Args
134 );
135 }
136
137 if (BootCompat->KernelState.RelocationBlock != 0) {
138 Args -= (UINTN)(BootCompat->KernelState.RelocationBlock - KERNEL_BASE_PADDR);
139
140 //
141 // FIXME: This should be done via trampoline as we may overwrite ourselves.
142 // See RelocationCallGate.nasm for more details.
143 //
144 CopyMem (
145 (VOID *)(UINTN)KERNEL_BASE_PADDR,
146 (VOID *)(UINTN)BootCompat->KernelState.RelocationBlock,
148 );
149 }
150
151 return Args;
152}
BOOT_COMPAT_CONTEXT * GetBootCompatContext(VOID)
VOID AppleMapPrepareForHibernateWake(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN UINTN ImageHeaderPage)
#define KERNEL_BASE_PADDR
VOID AsmAppleMapPrepareKernelState32(VOID)
VOID AppleMapPrepareForBooting(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT VOID *BootArgs)
UINTN EFIAPI AppleMapPrepareKernelState32(IN UINTN Args)
VOID AppleMapPrepareKernelJump32(IN OUT BOOT_COMPAT_CONTEXT *BootCompat)
#define RUNTIME_DEBUG(x)
BOOLEAN FindPattern(IN CONST UINT8 *Pattern, IN CONST UINT8 *PatternMask OPTIONAL, IN CONST UINT32 PatternSize, IN CONST UINT8 *Data, IN UINT32 DataSize, IN OUT UINT32 *DataOff)
Definition DataPatcher.c:82
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define ASSERT(x)
Definition coder.h:55
SERVICES_OVERRIDE_STATE ServiceState
KERNEL_SUPPORT_STATE KernelState
EFI_PHYSICAL_ADDRESS RelocationBlock