OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
ValidateBooter.c
Go to the documentation of this file.
1
16#include "ocvalidate.h"
17#include "OcValidateLib.h"
18
19STATIC
20UINT32
22 IN OC_GLOBAL_CONFIG *Config
23 )
24{
25 UINT32 ErrorCount;
26 UINT32 Index;
27 CONST CHAR8 *Comment;
28 BOOLEAN IsMmioWhitelistEnabled;
29 BOOLEAN ShouldEnableDevirtualiseMmio;
30 BOOLEAN IsDevirtualiseMmioEnabled;
31
32 ErrorCount = 0;
33 IsDevirtualiseMmioEnabled = Config->Booter.Quirks.DevirtualiseMmio;
34
35 IsMmioWhitelistEnabled = FALSE;
36 ShouldEnableDevirtualiseMmio = FALSE;
37 for (Index = 0; Index < Config->Booter.MmioWhitelist.Count; ++Index) {
38 Comment = OC_BLOB_GET (&Config->Booter.MmioWhitelist.Values[Index]->Comment);
39 IsMmioWhitelistEnabled = Config->Booter.MmioWhitelist.Values[Index]->Enabled;
40 //
41 // DevirtualiseMmio should be enabled if at least one entry is enabled.
42 //
43 ShouldEnableDevirtualiseMmio = IsMmioWhitelistEnabled;
44
45 //
46 // Sanitise strings.
47 //
48 if (!AsciiCommentIsLegal (Comment)) {
49 DEBUG ((DEBUG_WARN, "Booter->MmioWhitelist[%u]->Comment contains illegal character!\n", Index));
50 ++ErrorCount;
51 }
52 }
53
54 if (ShouldEnableDevirtualiseMmio && !IsDevirtualiseMmioEnabled) {
55 DEBUG ((DEBUG_WARN, "There are enabled entries under Booter->MmioWhitelist, but DevirtualiseMmio is not enabled!\n"));
56 ++ErrorCount;
57 }
58
59 return ErrorCount;
60}
61
62UINT32
64 IN OC_GLOBAL_CONFIG *Config
65 )
66{
67 UINT32 ErrorCount;
68 UINT32 Index;
69 CONST CHAR8 *Comment;
70 CONST CHAR8 *Arch;
71 CONST CHAR8 *Identifier;
72 CONST UINT8 *Find;
73 UINT32 FindSize;
74 CONST UINT8 *Replace;
75 UINT32 ReplaceSize;
76 CONST UINT8 *Mask;
77 UINT32 MaskSize;
78 CONST UINT8 *ReplaceMask;
79 UINT32 ReplaceMaskSize;
80
81 ErrorCount = 0;
82
83 for (Index = 0; Index < Config->Booter.Patch.Count; ++Index) {
84 Comment = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->Comment);
85 Arch = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->Arch);
86 Identifier = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->Identifier);
87 Find = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->Find);
88 FindSize = Config->Booter.Patch.Values[Index]->Find.Size;
89 Replace = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->Replace);
90 ReplaceSize = Config->Booter.Patch.Values[Index]->Replace.Size;
91 Mask = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->Mask);
92 MaskSize = Config->Booter.Patch.Values[Index]->Mask.Size;
93 ReplaceMask = OC_BLOB_GET (&Config->Booter.Patch.Values[Index]->ReplaceMask);
94 ReplaceMaskSize = Config->Booter.Patch.Values[Index]->ReplaceMask.Size;
95
96 //
97 // Sanitise strings.
98 //
99 if (!AsciiCommentIsLegal (Comment)) {
100 DEBUG ((DEBUG_WARN, "Booter->Patch[%u]->Comment contains illegal character!\n", Index));
101 ++ErrorCount;
102 }
103
104 if (!AsciiArchIsLegal (Arch, FALSE)) {
105 DEBUG ((DEBUG_WARN, "Booter->Patch[%u]->Arch is borked (Can only be Any, i386, and x86_64)!\n", Index));
106 ++ErrorCount;
107 }
108
109 if (!AsciiIdentifierIsLegal (Identifier, FALSE)) {
110 DEBUG ((DEBUG_WARN, "Booter->Patch[%u]->Identifier contains illegal character!\n", Index));
111 ++ErrorCount;
112 }
113
114 //
115 // Checks for size.
116 //
117 ErrorCount += ValidatePatch (
118 "Booter->Patch",
119 Index,
120 FALSE,
121 Find,
122 FindSize,
123 Replace,
124 ReplaceSize,
125 Mask,
126 MaskSize,
127 ReplaceMask,
128 ReplaceMaskSize
129 );
130 }
131
132 return ErrorCount;
133}
134
135STATIC
136UINT32
138 IN OC_GLOBAL_CONFIG *Config
139 )
140{
141 UINT32 ErrorCount;
142 UINT32 Index;
143 OC_UEFI_DRIVER_ENTRY *DriverEntry;
144 CONST CHAR8 *Driver;
145 UINT8 MaxSlide;
146 BOOLEAN IsAllowRelocationBlockEnabled;
147 BOOLEAN IsProvideCustomSlideEnabled;
148 BOOLEAN IsEnableSafeModeSlideEnabled;
149 BOOLEAN IsDisableVariableWriteEnabled;
150 BOOLEAN IsEnableWriteUnprotectorEnabled;
151 BOOLEAN IsClearTaskSwitchBitEnabled;
152 BOOLEAN HasOpenRuntimeEfiDriver;
153 INT8 ResizeAppleGpuBars;
154
155 ErrorCount = 0;
156 IsAllowRelocationBlockEnabled = Config->Booter.Quirks.AllowRelocationBlock;
157 IsProvideCustomSlideEnabled = Config->Booter.Quirks.ProvideCustomSlide;
158 IsEnableSafeModeSlideEnabled = Config->Booter.Quirks.EnableSafeModeSlide;
159 IsDisableVariableWriteEnabled = Config->Booter.Quirks.DisableVariableWrite;
160 IsEnableWriteUnprotectorEnabled = Config->Booter.Quirks.EnableWriteUnprotector;
161 IsClearTaskSwitchBitEnabled = Config->Booter.Quirks.ClearTaskSwitchBit;
162 HasOpenRuntimeEfiDriver = FALSE;
163 MaxSlide = Config->Booter.Quirks.ProvideMaxSlide;
164 ResizeAppleGpuBars = Config->Booter.Quirks.ResizeAppleGpuBars;
165
166 for (Index = 0; Index < Config->Uefi.Drivers.Count; ++Index) {
167 DriverEntry = Config->Uefi.Drivers.Values[Index];
168 Driver = OC_BLOB_GET (&DriverEntry->Path);
169
170 //
171 // Skip sanitising UEFI->Drivers as it will be performed when checking UEFI section.
172 //
173 if (DriverEntry->Enabled && (AsciiStrCmp (Driver, "OpenRuntime.efi") == 0)) {
174 HasOpenRuntimeEfiDriver = TRUE;
175 }
176 }
177
178 if (!HasOpenRuntimeEfiDriver) {
179 if (IsProvideCustomSlideEnabled) {
180 DEBUG ((DEBUG_WARN, "Booter->Quirks->ProvideCustomSlide is enabled, but OpenRuntime.efi is not loaded at UEFI->Drivers!\n"));
181 ++ErrorCount;
182 }
183
184 if (IsDisableVariableWriteEnabled) {
185 DEBUG ((DEBUG_WARN, "Booter->Quirks->DisableVariableWrite is enabled, but OpenRuntime.efi is not loaded at UEFI->Drivers!\n"));
186 ++ErrorCount;
187 }
188
189 if (IsEnableWriteUnprotectorEnabled) {
190 DEBUG ((DEBUG_WARN, "Booter->Quirks->EnableWriteUnprotector is enabled, but OpenRuntime.efi is not loaded at UEFI->Drivers!\n"));
191 ++ErrorCount;
192 }
193
194 if (IsClearTaskSwitchBitEnabled) {
195 DEBUG ((DEBUG_WARN, "Booter->Quirks->ClearTaskSwitchBit is enabled, but OpenRuntime.efi is not loaded at UEFI->Drivers!\n"));
196 ++ErrorCount;
197 }
198 }
199
200 if (!IsProvideCustomSlideEnabled) {
201 if (IsAllowRelocationBlockEnabled) {
202 DEBUG ((DEBUG_WARN, "Booter->Quirks->AllowRelocationBlock is enabled, but ProvideCustomSlide is not enabled altogether!\n"));
203 ++ErrorCount;
204 }
205
206 if (IsEnableSafeModeSlideEnabled) {
207 DEBUG ((DEBUG_WARN, "Booter->Quirks->EnableSafeModeSlide is enabled, but ProvideCustomSlide is not enabled altogether!\n"));
208 ++ErrorCount;
209 }
210
211 if (MaxSlide > 0) {
212 DEBUG ((DEBUG_WARN, "Booter->Quirks->ProvideMaxSlide is set to %u, but ProvideCustomSlide is not enabled altogether!\n", MaxSlide));
213 ++ErrorCount;
214 }
215 }
216
217 if (ResizeAppleGpuBars > 10) {
218 DEBUG ((DEBUG_WARN, "Booter->Quirks->ResizeAppleGpuBars is set to %d, which is unsupported by macOS!\n", Config->Booter.Quirks.ResizeAppleGpuBars));
219 ++ErrorCount;
220 } else if (ResizeAppleGpuBars > 8) {
221 DEBUG ((DEBUG_WARN, "Booter->Quirks->ResizeAppleGpuBars is set to %d, which is unstable with macOS sleep-wake!\n", Config->Booter.Quirks.ResizeAppleGpuBars));
222 ++ErrorCount;
223 } else if (ResizeAppleGpuBars > 0) {
224 DEBUG ((DEBUG_WARN, "Booter->Quirks->ResizeAppleGpuBars is set to %d, which is not useful for macOS!\n", Config->Booter.Quirks.ResizeAppleGpuBars));
225 ++ErrorCount;
226 }
227
228 return ErrorCount;
229}
230
231UINT32
233 IN OC_GLOBAL_CONFIG *Config
234 )
235{
236 UINT32 ErrorCount;
237 UINTN Index;
238 STATIC CONFIG_CHECK BooterCheckers[] = {
242 };
243
244 DEBUG ((DEBUG_VERBOSE, "config loaded into %a!\n", __func__));
245
246 ErrorCount = 0;
247
248 for (Index = 0; Index < ARRAY_SIZE (BooterCheckers); ++Index) {
249 ErrorCount += BooterCheckers[Index](Config);
250 }
251
252 return ReportError (__func__, ErrorCount);
253}
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
#define OC_BLOB_GET(Blob)
UINT32 ReportError(IN CONST CHAR8 *FuncName, IN UINT32 ErrorCount)
BOOLEAN AsciiArchIsLegal(IN CONST CHAR8 *Arch, IN BOOLEAN IsKernelArch)
UINT32 ValidatePatch(IN CONST CHAR8 *PatchSection, IN UINT32 PatchIndex, IN BOOLEAN FindSizeCanBeZero, IN CONST UINT8 *Find, IN UINT32 FindSize, IN CONST UINT8 *Replace, IN UINT32 ReplaceSize, IN CONST UINT8 *Mask, IN UINT32 MaskSize, IN CONST UINT8 *ReplaceMask, IN UINT32 ReplaceMaskSize)
BOOLEAN AsciiIdentifierIsLegal(IN CONST CHAR8 *Identifier, IN BOOLEAN IsKernelIdentifier)
BOOLEAN AsciiCommentIsLegal(IN CONST CHAR8 *Comment)
STATIC UINT32 CheckBooterQuirks(IN OC_GLOBAL_CONFIG *Config)
STATIC UINT32 CheckBooterMmioWhitelist(IN OC_GLOBAL_CONFIG *Config)
UINT32 CheckBooterPatch(IN OC_GLOBAL_CONFIG *Config)
UINT32 CheckBooter(IN OC_GLOBAL_CONFIG *Config)
UINT32(* CONFIG_CHECK)(IN OC_GLOBAL_CONFIG *Config)
Definition ocvalidate.h:27