OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
ValidateNvram.c
Go to the documentation of this file.
1
16#include "ocvalidate.h"
17#include "OcValidateLib.h"
18#include "NvramKeyInfo.h"
19
28STATIC
29BOOLEAN
31 IN CONST VOID *PrimaryEntry,
32 IN CONST VOID *SecondaryEntry
33 )
34{
35 CONST OC_STRING *NvramAddPrimaryEntry;
36 CONST OC_STRING *NvramAddSecondaryEntry;
37 CONST CHAR8 *NvramAddPrimaryGUIDString;
38 CONST CHAR8 *NvramAddSecondaryGUIDString;
39
40 NvramAddPrimaryEntry = *(CONST OC_STRING **)PrimaryEntry;
41 NvramAddSecondaryEntry = *(CONST OC_STRING **)SecondaryEntry;
42 NvramAddPrimaryGUIDString = OC_BLOB_GET (NvramAddPrimaryEntry);
43 NvramAddSecondaryGUIDString = OC_BLOB_GET (NvramAddSecondaryEntry);
44
45 return StringIsDuplicated ("NVRAM->Add", NvramAddPrimaryGUIDString, NvramAddSecondaryGUIDString);
46}
47
56STATIC
57BOOLEAN
59 IN CONST VOID *PrimaryEntry,
60 IN CONST VOID *SecondaryEntry
61 )
62{
63 CONST OC_STRING *NvramDeletePrimaryEntry;
64 CONST OC_STRING *NvramDeleteSecondaryEntry;
65 CONST CHAR8 *NvramDeletePrimaryGUIDString;
66 CONST CHAR8 *NvramDeleteSecondaryGUIDString;
67
68 NvramDeletePrimaryEntry = *(CONST OC_STRING **)PrimaryEntry;
69 NvramDeleteSecondaryEntry = *(CONST OC_STRING **)SecondaryEntry;
70 NvramDeletePrimaryGUIDString = OC_BLOB_GET (NvramDeletePrimaryEntry);
71 NvramDeleteSecondaryGUIDString = OC_BLOB_GET (NvramDeleteSecondaryEntry);
72
73 return StringIsDuplicated ("NVRAM->Delete", NvramDeletePrimaryGUIDString, NvramDeleteSecondaryGUIDString);
74}
75
84STATIC
85BOOLEAN
87 IN CONST VOID *PrimaryEntry,
88 IN CONST VOID *SecondaryEntry
89 )
90{
91 CONST OC_STRING *NvramLegacySchemaPrimaryEntry;
92 CONST OC_STRING *NvramLegacySchemaSecondaryEntry;
93 CONST CHAR8 *NvramLegacySchemaPrimaryGUIDString;
94 CONST CHAR8 *NvramLegacySchemaSecondaryGUIDString;
95
96 NvramLegacySchemaPrimaryEntry = *(CONST OC_STRING **)PrimaryEntry;
97 NvramLegacySchemaSecondaryEntry = *(CONST OC_STRING **)SecondaryEntry;
98 NvramLegacySchemaPrimaryGUIDString = OC_BLOB_GET (NvramLegacySchemaPrimaryEntry);
99 NvramLegacySchemaSecondaryGUIDString = OC_BLOB_GET (NvramLegacySchemaSecondaryEntry);
100
101 return StringIsDuplicated ("NVRAM->LegacySchema", NvramLegacySchemaPrimaryGUIDString, NvramLegacySchemaSecondaryGUIDString);
102}
103
104STATIC
105UINT32
107 IN CONST CHAR8 *AsciiGuid,
108 IN CONST OC_ASSOC *VariableMap
109 )
110{
111 UINT32 ErrorCount;
112 EFI_STATUS Status;
113 GUID Guid;
114 UINT32 VariableIndex;
115 UINTN Index;
116 UINTN Index2;
117
118 ErrorCount = 0;
119
120 Status = AsciiStrToGuid (AsciiGuid, &Guid);
121 if (EFI_ERROR (Status)) {
122 DEBUG ((DEBUG_WARN, "NVRAM->Add: Unable to check %a due to borked GUID format!\n", AsciiGuid));
123 ++ErrorCount;
124 return ErrorCount;
125 }
126
127 for (Index = 0; Index < mGUIDMapsCount; ++Index) {
128 if (CompareGuid (&Guid, mGUIDMaps[Index].Guid)) {
129 for (VariableIndex = 0; VariableIndex < VariableMap->Count; ++VariableIndex) {
130 for (Index2 = 0; Index2 < mGUIDMaps[Index].NvramKeyMapsCount; ++Index2) {
131 if (AsciiStrCmp (mGUIDMaps[Index].NvramKeyMaps[Index2].KeyName, OC_BLOB_GET (VariableMap->Keys[VariableIndex])) == 0) {
132 if (!mGUIDMaps[Index].NvramKeyMaps[Index2].KeyChecker (
133 OC_BLOB_GET (VariableMap->Values[VariableIndex]),
134 VariableMap->Values[VariableIndex]->Size
135 ))
136 {
137 DEBUG ((
138 DEBUG_WARN,
139 "NVRAM->Add->%g->%a has illegal value!\n",
140 &Guid,
141 OC_BLOB_GET (VariableMap->Keys[VariableIndex])
142 ));
143 ++ErrorCount;
144 } else {
145 //
146 // Special check for UIScale when it is valid.
147 //
148 if (AsciiStrCmp (OC_BLOB_GET (VariableMap->Keys[VariableIndex]), "UIScale") == 0) {
149 mHasNvramUIScale = TRUE;
150 }
151 }
152 }
153 }
154 }
155 }
156 }
157
158 return ErrorCount;
159}
160
161STATIC
162UINT32
164 IN OC_GLOBAL_CONFIG *Config
165 )
166{
167 UINT32 ErrorCount;
168 UINT32 GuidIndex;
169 UINT32 VariableIndex;
170 CONST CHAR8 *AsciiGuid;
171 CONST CHAR8 *AsciiNvramKey;
172 OC_ASSOC *VariableMap;
173
174 ErrorCount = 0;
175
176 for (GuidIndex = 0; GuidIndex < Config->Nvram.Add.Count; ++GuidIndex) {
177 AsciiGuid = OC_BLOB_GET (Config->Nvram.Add.Keys[GuidIndex]);
178
179 if (!AsciiGuidIsLegal (AsciiGuid)) {
180 DEBUG ((DEBUG_WARN, "NVRAM->Add[%u] has borked GUID!\n", GuidIndex));
181 ++ErrorCount;
182 }
183
184 VariableMap = Config->Nvram.Add.Values[GuidIndex];
185
186 for (VariableIndex = 0; VariableIndex < VariableMap->Count; ++VariableIndex) {
187 AsciiNvramKey = OC_BLOB_GET (VariableMap->Keys[VariableIndex]);
188
189 //
190 // Sanitise strings.
191 //
192 if (!AsciiPropertyIsLegal (AsciiNvramKey)) {
193 DEBUG ((
194 DEBUG_WARN,
195 "NVRAM->Add[%u]->Key[%u] contains illegal character!\n",
196 GuidIndex,
197 VariableIndex
198 ));
199 ++ErrorCount;
200 }
201 }
202
203 //
204 // Check duplicated properties in NVRAM->Add.
205 //
206 ErrorCount += FindArrayDuplication (
207 VariableMap->Keys,
208 VariableMap->Count,
209 sizeof (VariableMap->Keys[0]),
211 );
212
213 //
214 // Check for accepted values for NVRAM keys.
215 //
216 ErrorCount += ValidateNvramKeyByGuid (AsciiGuid, VariableMap);
217 }
218
219 //
220 // Check duplicated entries in NVRAM->Add.
221 //
222 ErrorCount += FindArrayDuplication (
223 Config->Nvram.Add.Keys,
224 Config->Nvram.Add.Count,
225 sizeof (Config->Nvram.Add.Keys[0]),
227 );
228
229 return ErrorCount;
230}
231
232STATIC
233UINT32
235 IN OC_GLOBAL_CONFIG *Config
236 )
237{
238 UINT32 ErrorCount;
239 UINT32 GuidIndex;
240 UINT32 VariableIndex;
241 CONST CHAR8 *AsciiGuid;
242 CONST CHAR8 *AsciiNvramKey;
243
244 ErrorCount = 0;
245
246 for (GuidIndex = 0; GuidIndex < Config->Nvram.Delete.Count; ++GuidIndex) {
247 AsciiGuid = OC_BLOB_GET (Config->Nvram.Delete.Keys[GuidIndex]);
248
249 if (!AsciiGuidIsLegal (AsciiGuid)) {
250 DEBUG ((DEBUG_WARN, "NVRAM->Delete[%u] has borked GUID!\n", GuidIndex));
251 ++ErrorCount;
252 }
253
254 for (VariableIndex = 0; VariableIndex < Config->Nvram.Delete.Values[GuidIndex]->Count; ++VariableIndex) {
255 AsciiNvramKey = OC_BLOB_GET (Config->Nvram.Delete.Values[GuidIndex]->Values[VariableIndex]);
256
257 //
258 // Sanitise strings.
259 //
260 if (!AsciiPropertyIsLegal (AsciiNvramKey)) {
261 DEBUG ((
262 DEBUG_WARN,
263 "NVRAM->Delete[%u]->Key[%u] contains illegal character!\n",
264 GuidIndex,
265 VariableIndex
266 ));
267 ++ErrorCount;
268 }
269 }
270
271 //
272 // Check duplicated properties in NVRAM->Delete.
273 //
274 ErrorCount += FindArrayDuplication (
275 Config->Nvram.Delete.Values[GuidIndex]->Values,
276 Config->Nvram.Delete.Values[GuidIndex]->Count,
277 sizeof (Config->Nvram.Delete.Values[GuidIndex]->Values[0]),
279 );
280 }
281
282 //
283 // Check duplicated entries in NVRAM->Delete.
284 //
285 ErrorCount += FindArrayDuplication (
286 Config->Nvram.Delete.Keys,
287 Config->Nvram.Delete.Count,
288 sizeof (Config->Nvram.Delete.Keys[0]),
290 );
291
292 return ErrorCount;
293}
294
295STATIC
296UINT32
298 IN OC_GLOBAL_CONFIG *Config
299 )
300{
301 UINT32 ErrorCount;
302 UINT32 GuidIndex;
303 UINT32 VariableIndex;
304 CONST CHAR8 *AsciiGuid;
305 CONST CHAR8 *AsciiNvramKey;
306
307 ErrorCount = 0;
308
309 for (GuidIndex = 0; GuidIndex < Config->Nvram.Legacy.Count; ++GuidIndex) {
310 AsciiGuid = OC_BLOB_GET (Config->Nvram.Legacy.Keys[GuidIndex]);
311
312 if (!AsciiGuidIsLegal (AsciiGuid)) {
313 DEBUG ((DEBUG_WARN, "NVRAM->LegacySchema[%u] has borked GUID!\n", GuidIndex));
314 ++ErrorCount;
315 }
316
317 for (VariableIndex = 0; VariableIndex < Config->Nvram.Legacy.Values[GuidIndex]->Count; ++VariableIndex) {
318 AsciiNvramKey = OC_BLOB_GET (Config->Nvram.Legacy.Values[GuidIndex]->Values[VariableIndex]);
319
320 //
321 // Sanitise strings.
322 //
323 if (!AsciiPropertyIsLegal (AsciiNvramKey)) {
324 DEBUG ((
325 DEBUG_WARN,
326 "NVRAM->LegacySchema[%u]->Key[%u] contains illegal character!\n",
327 GuidIndex,
328 VariableIndex
329 ));
330 ++ErrorCount;
331 }
332 }
333
334 //
335 // Check duplicated properties in NVRAM->LegacySchema.
336 //
337 ErrorCount += FindArrayDuplication (
338 Config->Nvram.Legacy.Values[GuidIndex]->Values,
339 Config->Nvram.Legacy.Values[GuidIndex]->Count,
340 sizeof (Config->Nvram.Legacy.Values[GuidIndex]->Values[0]),
342 );
343 }
344
345 //
346 // Check duplicated entries in NVRAM->LegacySchema.
347 //
348 ErrorCount += FindArrayDuplication (
349 Config->Nvram.Legacy.Keys,
350 Config->Nvram.Legacy.Count,
351 sizeof (Config->Nvram.Legacy.Keys[0]),
353 );
354
355 return ErrorCount;
356}
357
358UINT32
360 IN OC_GLOBAL_CONFIG *Config
361 )
362{
363 UINT32 ErrorCount;
364 UINTN Index;
365 STATIC CONFIG_CHECK NvramCheckers[] = {
369 };
370
371 DEBUG ((DEBUG_VERBOSE, "config loaded into %a!\n", __func__));
372
373 ErrorCount = 0;
374
375 for (Index = 0; Index < ARRAY_SIZE (NvramCheckers); ++Index) {
376 ErrorCount += NvramCheckers[Index](Config);
377 }
378
379 return ReportError (__func__, ErrorCount);
380}
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
UINTN mGUIDMapsCount
NVRAM_GUID_MAP mGUIDMaps[]
BOOLEAN mHasNvramUIScale
#define OC_BLOB_GET(Blob)
UINT32 ReportError(IN CONST CHAR8 *FuncName, IN UINT32 ErrorCount)
BOOLEAN StringIsDuplicated(IN CONST CHAR8 *EntrySection, IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString)
UINT32 FindArrayDuplication(IN VOID *First, IN UINTN Number, IN UINTN Size, IN DUPLICATION_CHECK DupChecker)
BOOLEAN AsciiGuidIsLegal(IN CONST CHAR8 *AsciiGuid)
BOOLEAN AsciiPropertyIsLegal(IN CONST CHAR8 *Property)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
STATIC UINT32 CheckNvramDelete(IN OC_GLOBAL_CONFIG *Config)
UINT32 CheckNvram(IN OC_GLOBAL_CONFIG *Config)
STATIC UINT32 CheckNvramAdd(IN OC_GLOBAL_CONFIG *Config)
STATIC BOOLEAN NvramDeleteHasDuplication(IN CONST VOID *PrimaryEntry, IN CONST VOID *SecondaryEntry)
STATIC BOOLEAN NvramLegacySchemaHasDuplication(IN CONST VOID *PrimaryEntry, IN CONST VOID *SecondaryEntry)
STATIC BOOLEAN NvramAddHasDuplication(IN CONST VOID *PrimaryEntry, IN CONST VOID *SecondaryEntry)
STATIC UINT32 CheckNvramSchema(IN OC_GLOBAL_CONFIG *Config)
STATIC UINT32 ValidateNvramKeyByGuid(IN CONST CHAR8 *AsciiGuid, IN CONST OC_ASSOC *VariableMap)
UINT32(* CONFIG_CHECK)(IN OC_GLOBAL_CONFIG *Config)
Definition ocvalidate.h:27
UINTN NvramKeyMapsCount