]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VarCheck.c
Use SmmMemLib to check communication buffer.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VarCheck.c
1 /** @file
2 Implementation functions and structures for var check protocol.
3
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Variable.h"
16 #include <Library/DevicePathLib.h>
17
18 extern LIST_ENTRY mLockedVariableList;
19 extern BOOLEAN mEndOfDxe;
20 extern BOOLEAN mEnableLocking;
21
22 #define VAR_CHECK_HANDLER_TABLE_SIZE 0x8
23
24 UINT32 mNumberOfHandler = 0;
25 UINT32 mMaxNumberOfHandler = 0;
26 VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *mHandlerTable = NULL;
27
28 typedef struct {
29 LIST_ENTRY Link;
30 EFI_GUID Guid;
31 VAR_CHECK_VARIABLE_PROPERTY VariableProperty;
32 //CHAR16 *Name;
33 } VAR_CHECK_VARIABLE_ENTRY;
34
35 LIST_ENTRY mVarCheckVariableList = INITIALIZE_LIST_HEAD_VARIABLE (mVarCheckVariableList);
36
37 typedef
38 EFI_STATUS
39 (EFIAPI *INTERNAL_VAR_CHECK_FUNCTION) (
40 IN VAR_CHECK_VARIABLE_PROPERTY *Propery,
41 IN UINTN DataSize,
42 IN VOID *Data
43 );
44
45 typedef struct {
46 CHAR16 *Name;
47 VAR_CHECK_VARIABLE_PROPERTY VariableProperty;
48 INTERNAL_VAR_CHECK_FUNCTION CheckFunction;
49 } UEFI_DEFINED_VARIABLE_ENTRY;
50
51 /**
52 Internal check for load option.
53
54 @param[in] VariablePropery Pointer to variable property.
55 @param[in] DataSize Data size.
56 @param[in] Data Pointer to data buffer.
57
58 @retval EFI_SUCCESS The SetVariable check result was success.
59 @retval EFI_INVALID_PARAMETER The data buffer is not a valid load option.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 InternalVarCheckLoadOption (
65 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
66 IN UINTN DataSize,
67 IN VOID *Data
68 )
69 {
70 UINT16 FilePathListLength;
71 CHAR16 *Description;
72 EFI_DEVICE_PATH_PROTOCOL *FilePathList;
73
74 FilePathListLength = *((UINT16 *) ((UINTN) Data + sizeof (UINT32)));
75
76 //
77 // Check Description
78 //
79 Description = (CHAR16 *) ((UINTN) Data + sizeof (UINT32) + sizeof (UINT16));
80 while (Description < (CHAR16 *) ((UINTN) Data + DataSize)) {
81 if (*Description == L'\0') {
82 break;
83 }
84 Description++;
85 }
86 if ((UINTN) Description >= ((UINTN) Data + DataSize)) {
87 return EFI_INVALID_PARAMETER;
88 }
89 Description++;
90
91 //
92 // Check FilePathList
93 //
94 FilePathList = (EFI_DEVICE_PATH_PROTOCOL *) Description;
95 if ((UINTN) FilePathList > (MAX_ADDRESS - FilePathListLength)) {
96 return EFI_INVALID_PARAMETER;
97 }
98 if (((UINTN) FilePathList + FilePathListLength) > ((UINTN) Data + DataSize)) {
99 return EFI_INVALID_PARAMETER;
100 }
101 if (FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
102 return EFI_INVALID_PARAMETER;
103 }
104 if (!IsDevicePathValid (FilePathList, FilePathListLength)) {
105 return EFI_INVALID_PARAMETER;
106 }
107
108 return EFI_SUCCESS;
109 }
110
111 /**
112 Internal check for key option.
113
114 @param[in] VariablePropery Pointer to variable property.
115 @param[in] DataSize Data size.
116 @param[in] Data Pointer to data buffer.
117
118 @retval EFI_SUCCESS The SetVariable check result was success.
119 @retval EFI_INVALID_PARAMETER The data buffer is not a valid key option.
120
121 **/
122 EFI_STATUS
123 EFIAPI
124 InternalVarCheckKeyOption (
125 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
126 IN UINTN DataSize,
127 IN VOID *Data
128 )
129 {
130 if (((DataSize - sizeof (EFI_KEY_OPTION)) % sizeof (EFI_INPUT_KEY)) != 0) {
131 return EFI_INVALID_PARAMETER;
132 }
133
134 return EFI_SUCCESS;
135 }
136
137 /**
138 Internal check for device path.
139
140 @param[in] VariablePropery Pointer to variable property.
141 @param[in] DataSize Data size.
142 @param[in] Data Pointer to data buffer.
143
144 @retval EFI_SUCCESS The SetVariable check result was success.
145 @retval EFI_INVALID_PARAMETER The data buffer is not a valid device path.
146
147 **/
148 EFI_STATUS
149 EFIAPI
150 InternalVarCheckDevicePath (
151 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
152 IN UINTN DataSize,
153 IN VOID *Data
154 )
155 {
156 if (!IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *) Data, DataSize)) {
157 return EFI_INVALID_PARAMETER;
158 }
159 return EFI_SUCCESS;
160 }
161
162 /**
163 Internal check for ASCII string.
164
165 @param[in] VariablePropery Pointer to variable property.
166 @param[in] DataSize Data size.
167 @param[in] Data Pointer to data buffer.
168
169 @retval EFI_SUCCESS The SetVariable check result was success.
170 @retval EFI_INVALID_PARAMETER The data buffer is not a Null-terminated ASCII string.
171
172 **/
173 EFI_STATUS
174 EFIAPI
175 InternalVarCheckAsciiString (
176 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
177 IN UINTN DataSize,
178 IN VOID *Data
179 )
180 {
181 CHAR8 *String;
182 UINTN Index;
183
184 String = (CHAR8 *) Data;
185 if (String[DataSize - 1] == '\0') {
186 return EFI_SUCCESS;
187 } else {
188 for (Index = 1; Index < DataSize && (String[DataSize - 1 - Index] != '\0'); Index++);
189 if (Index == DataSize) {
190 return EFI_INVALID_PARAMETER;
191 }
192 }
193 return EFI_SUCCESS;
194 }
195
196 /**
197 Internal check for size array.
198
199 @param[in] VariablePropery Pointer to variable property.
200 @param[in] DataSize Data size.
201 @param[in] Data Pointer to data buffer.
202
203 @retval EFI_SUCCESS The SetVariable check result was success.
204 @retval EFI_INVALID_PARAMETER The DataSize is not size array.
205
206 **/
207 EFI_STATUS
208 EFIAPI
209 InternalVarCheckSizeArray (
210 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
211 IN UINTN DataSize,
212 IN VOID *Data
213 )
214 {
215 if ((DataSize % VariablePropery->MinSize) != 0) {
216 return EFI_INVALID_PARAMETER;
217 }
218 return EFI_SUCCESS;
219 }
220
221 //
222 // To prevent name collisions with possible future globally defined variables,
223 // other internal firmware data variables that are not defined here must be
224 // saved with a unique VendorGuid other than EFI_GLOBAL_VARIABLE or
225 // any other GUID defined by the UEFI Specification. Implementations must
226 // only permit the creation of variables with a UEFI Specification-defined
227 // VendorGuid when these variables are documented in the UEFI Specification.
228 //
229 UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList[] = {
230 {
231 EFI_LANG_CODES_VARIABLE_NAME,
232 {
233 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
234 0,
235 VARIABLE_ATTRIBUTE_BS_RT,
236 1,
237 MAX_UINTN
238 },
239 InternalVarCheckAsciiString
240 },
241 {
242 EFI_LANG_VARIABLE_NAME,
243 {
244 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
245 0,
246 VARIABLE_ATTRIBUTE_NV_BS_RT,
247 1,
248 MAX_UINTN
249 },
250 InternalVarCheckAsciiString
251 },
252 {
253 EFI_TIME_OUT_VARIABLE_NAME,
254 {
255 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
256 0,
257 VARIABLE_ATTRIBUTE_NV_BS_RT,
258 sizeof (UINT16),
259 sizeof (UINT16)
260 },
261 NULL
262 },
263 {
264 EFI_PLATFORM_LANG_CODES_VARIABLE_NAME,
265 {
266 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
267 0,
268 VARIABLE_ATTRIBUTE_BS_RT,
269 1,
270 MAX_UINTN
271 },
272 InternalVarCheckAsciiString
273 },
274 {
275 EFI_PLATFORM_LANG_VARIABLE_NAME,
276 {
277 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
278 0,
279 VARIABLE_ATTRIBUTE_NV_BS_RT,
280 1,
281 MAX_UINTN
282 },
283 InternalVarCheckAsciiString
284 },
285 {
286 EFI_CON_IN_VARIABLE_NAME,
287 {
288 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
289 0,
290 VARIABLE_ATTRIBUTE_NV_BS_RT,
291 sizeof (EFI_DEVICE_PATH_PROTOCOL),
292 MAX_UINTN
293 },
294 InternalVarCheckDevicePath
295 },
296 {
297 EFI_CON_OUT_VARIABLE_NAME,
298 {
299 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
300 0,
301 VARIABLE_ATTRIBUTE_NV_BS_RT,
302 sizeof (EFI_DEVICE_PATH_PROTOCOL),
303 MAX_UINTN
304 },
305 InternalVarCheckDevicePath
306 },
307 {
308 EFI_ERR_OUT_VARIABLE_NAME,
309 {
310 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
311 0,
312 VARIABLE_ATTRIBUTE_NV_BS_RT,
313 sizeof (EFI_DEVICE_PATH_PROTOCOL),
314 MAX_UINTN
315 },
316 InternalVarCheckDevicePath
317 },
318 {
319 EFI_CON_IN_DEV_VARIABLE_NAME,
320 {
321 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
322 0,
323 VARIABLE_ATTRIBUTE_BS_RT,
324 sizeof (EFI_DEVICE_PATH_PROTOCOL),
325 MAX_UINTN
326 },
327 InternalVarCheckDevicePath
328 },
329 {
330 EFI_CON_OUT_DEV_VARIABLE_NAME,
331 {
332 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
333 0,
334 VARIABLE_ATTRIBUTE_BS_RT,
335 sizeof (EFI_DEVICE_PATH_PROTOCOL),
336 MAX_UINTN
337 },
338 InternalVarCheckDevicePath
339 },
340 {
341 EFI_ERR_OUT_DEV_VARIABLE_NAME,
342 {
343 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
344 0,
345 VARIABLE_ATTRIBUTE_BS_RT,
346 sizeof (EFI_DEVICE_PATH_PROTOCOL),
347 MAX_UINTN
348 },
349 InternalVarCheckDevicePath
350 },
351 {
352 EFI_BOOT_ORDER_VARIABLE_NAME,
353 {
354 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
355 0,
356 VARIABLE_ATTRIBUTE_NV_BS_RT,
357 sizeof (UINT16),
358 MAX_UINTN
359 },
360 InternalVarCheckSizeArray
361 },
362 {
363 EFI_BOOT_NEXT_VARIABLE_NAME,
364 {
365 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
366 0,
367 VARIABLE_ATTRIBUTE_NV_BS_RT,
368 sizeof (UINT16),
369 sizeof (UINT16)
370 },
371 NULL
372 },
373 {
374 EFI_BOOT_CURRENT_VARIABLE_NAME,
375 {
376 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
377 0,
378 VARIABLE_ATTRIBUTE_BS_RT,
379 sizeof (UINT16),
380 sizeof (UINT16)
381 },
382 NULL
383 },
384 {
385 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME,
386 {
387 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
388 0,
389 VARIABLE_ATTRIBUTE_BS_RT,
390 sizeof (UINT32),
391 sizeof (UINT32)
392 },
393 NULL
394 },
395 {
396 EFI_DRIVER_ORDER_VARIABLE_NAME,
397 {
398 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
399 0,
400 VARIABLE_ATTRIBUTE_NV_BS_RT,
401 sizeof (UINT16),
402 MAX_UINTN
403 },
404 InternalVarCheckSizeArray
405 },
406 {
407 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME,
408 {
409 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
410 0,
411 VARIABLE_ATTRIBUTE_NV_BS_RT,
412 sizeof (UINT16),
413 sizeof (UINT16)
414 },
415 NULL
416 },
417 {
418 EFI_SETUP_MODE_NAME,
419 {
420 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
421 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
422 VARIABLE_ATTRIBUTE_BS_RT,
423 sizeof (UINT8),
424 sizeof (UINT8)
425 },
426 NULL
427 },
428 {
429 EFI_KEY_EXCHANGE_KEY_NAME,
430 {
431 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
432 0,
433 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
434 1,
435 MAX_UINTN
436 },
437 NULL
438 },
439 {
440 EFI_PLATFORM_KEY_NAME,
441 {
442 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
443 0,
444 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
445 1,
446 MAX_UINTN
447 },
448 NULL
449 },
450 {
451 EFI_SIGNATURE_SUPPORT_NAME,
452 {
453 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
454 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
455 VARIABLE_ATTRIBUTE_BS_RT,
456 sizeof (EFI_GUID),
457 MAX_UINTN
458 },
459 InternalVarCheckSizeArray
460 },
461 {
462 EFI_SECURE_BOOT_MODE_NAME,
463 {
464 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
465 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
466 VARIABLE_ATTRIBUTE_BS_RT,
467 sizeof (UINT8),
468 sizeof (UINT8)
469 },
470 NULL
471 },
472 {
473 EFI_KEK_DEFAULT_VARIABLE_NAME,
474 {
475 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
476 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
477 VARIABLE_ATTRIBUTE_BS_RT,
478 1,
479 MAX_UINTN
480 },
481 NULL
482 },
483 {
484 EFI_PK_DEFAULT_VARIABLE_NAME,
485 {
486 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
487 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
488 VARIABLE_ATTRIBUTE_BS_RT,
489 1,
490 MAX_UINTN
491 },
492 NULL
493 },
494 {
495 EFI_DB_DEFAULT_VARIABLE_NAME,
496 {
497 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
498 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
499 VARIABLE_ATTRIBUTE_BS_RT,
500 1,
501 MAX_UINTN
502 },
503 NULL
504 },
505 {
506 EFI_DBX_DEFAULT_VARIABLE_NAME,
507 {
508 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
509 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
510 VARIABLE_ATTRIBUTE_BS_RT,
511 1,
512 MAX_UINTN
513 },
514 NULL
515 },
516 {
517 EFI_DBT_DEFAULT_VARIABLE_NAME,
518 {
519 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
520 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
521 VARIABLE_ATTRIBUTE_BS_RT,
522 1,
523 MAX_UINTN
524 },
525 NULL
526 },
527 {
528 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,
529 {
530 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
531 0,
532 VARIABLE_ATTRIBUTE_BS_RT,
533 sizeof (UINT64),
534 sizeof (UINT64)
535 },
536 NULL
537 },
538 {
539 EFI_OS_INDICATIONS_VARIABLE_NAME,
540 {
541 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
542 0,
543 VARIABLE_ATTRIBUTE_NV_BS_RT,
544 sizeof (UINT64),
545 sizeof (UINT64)
546 },
547 NULL
548 },
549 {
550 EFI_VENDOR_KEYS_VARIABLE_NAME,
551 {
552 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
553 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
554 VARIABLE_ATTRIBUTE_BS_RT,
555 sizeof (UINT8),
556 sizeof (UINT8)
557 },
558 NULL
559 },
560 };
561 UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {
562 {
563 L"Boot####",
564 {
565 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
566 0,
567 VARIABLE_ATTRIBUTE_NV_BS_RT,
568 sizeof (UINT32) + sizeof (UINT16),
569 MAX_UINTN
570 },
571 InternalVarCheckLoadOption
572 },
573 {
574 L"Driver####",
575 {
576 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
577 0,
578 VARIABLE_ATTRIBUTE_NV_BS_RT,
579 sizeof (UINT32) + sizeof (UINT16),
580 MAX_UINTN
581 },
582 InternalVarCheckLoadOption
583 },
584 {
585 L"Key####",
586 {
587 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
588 0,
589 VARIABLE_ATTRIBUTE_NV_BS_RT,
590 sizeof (EFI_KEY_OPTION),
591 sizeof (EFI_KEY_OPTION) + 3 * sizeof (EFI_INPUT_KEY)
592 },
593 InternalVarCheckKeyOption
594 },
595 };
596
597 typedef struct {
598 EFI_GUID *Guid;
599 CHAR16 *Name;
600 VAR_CHECK_VARIABLE_PROPERTY VariableProperty;
601 } VARIABLE_DRIVER_VARIABLE_ENTRY;
602
603 VARIABLE_DRIVER_VARIABLE_ENTRY mVariableDriverVariableList[] = {
604 {
605 &gEdkiiVarErrorFlagGuid,
606 VAR_ERROR_FLAG_NAME,
607 {
608 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
609 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
610 VARIABLE_ATTRIBUTE_NV_BS_RT,
611 sizeof (VAR_ERROR_FLAG),
612 sizeof (VAR_ERROR_FLAG),
613 }
614 },
615 };
616
617 /**
618 Get UEFI defined global variable property.
619 The code will check if variable guid is global variable guid first.
620 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2.
621
622 @param[in] VariableName Pointer to variable name.
623 @param[in] VendorGuid Variable Vendor Guid.
624 @param[in] WildcardMatch Try wildcard match or not.
625 @param[out] VariableProperty Pointer to variable property.
626 @param[out] VarCheckFunction Pointer to check function.
627
628 @retval EFI_SUCCESS Variable is not global variable.
629 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists.
630
631 **/
632 EFI_STATUS
633 GetUefiDefinedVariableProperty (
634 IN CHAR16 *VariableName,
635 IN EFI_GUID *VendorGuid,
636 IN BOOLEAN WildcardMatch,
637 OUT VAR_CHECK_VARIABLE_PROPERTY **VariableProperty,
638 OUT INTERNAL_VAR_CHECK_FUNCTION *VarCheckFunction OPTIONAL
639 )
640 {
641 UINTN Index;
642 UINTN NameLength;
643
644 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {
645 //
646 // Try list 1, exactly match.
647 //
648 for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {
649 if (StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) {
650 if (VarCheckFunction != NULL) {
651 *VarCheckFunction = mGlobalVariableList[Index].CheckFunction;
652 }
653 *VariableProperty = &mGlobalVariableList[Index].VariableProperty;
654 return EFI_SUCCESS;
655 }
656 }
657
658 //
659 // Try list 2.
660 //
661 NameLength = StrLen (VariableName) - 4;
662 for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {
663 if (WildcardMatch) {
664 if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&
665 (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&
666 IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&
667 IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&
668 IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&
669 IsHexaDecimalDigitCharacter (VariableName[NameLength + 3])) {
670 if (VarCheckFunction != NULL) {
671 *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;
672 }
673 *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;
674 return EFI_SUCCESS;
675 }
676 }
677 if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) {
678 if (VarCheckFunction != NULL) {
679 *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;
680 }
681 *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;
682 return EFI_SUCCESS;
683 }
684 }
685
686 //
687 // The variable name is not in the lists.
688 //
689 return EFI_INVALID_PARAMETER;
690 }
691
692 //
693 // It is not global variable.
694 //
695 return EFI_SUCCESS;
696 }
697
698 /**
699 Get variable property for variables managed by Varaible driver.
700
701 @param[in] VariableName Pointer to variable name.
702 @param[in] VendorGuid Variable Vendor Guid.
703
704 @return Pointer to variable property.
705
706 **/
707 VAR_CHECK_VARIABLE_PROPERTY *
708 GetVariableDriverVariableProperty (
709 IN CHAR16 *VariableName,
710 IN EFI_GUID *VendorGuid
711 )
712 {
713 UINTN Index;
714
715 for (Index = 0; Index < sizeof (mVariableDriverVariableList)/sizeof (mVariableDriverVariableList[0]); Index++) {
716 if ((CompareGuid (mVariableDriverVariableList[Index].Guid, VendorGuid)) && (StrCmp (mVariableDriverVariableList[Index].Name, VariableName) == 0)) {
717 return &mVariableDriverVariableList[Index].VariableProperty;
718 }
719 }
720
721 return NULL;
722 }
723
724 /**
725 Internal SetVariable check.
726
727 @param[in] VariableName Name of Variable to set.
728 @param[in] VendorGuid Variable vendor GUID.
729 @param[in] Attributes Attribute value of the variable.
730 @param[in] DataSize Size of Data to set.
731 @param[in] Data Data pointer.
732
733 @retval EFI_SUCCESS The SetVariable check result was success.
734 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied,
735 or the DataSize exceeds the minimum or maximum allowed,
736 or the Data value is not following UEFI spec for UEFI defined variables.
737 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
738 @retval Others The return status from check handler.
739
740 **/
741 EFI_STATUS
742 EFIAPI
743 InternalVarCheckSetVariableCheck (
744 IN CHAR16 *VariableName,
745 IN EFI_GUID *VendorGuid,
746 IN UINT32 Attributes,
747 IN UINTN DataSize,
748 IN VOID *Data
749 )
750 {
751 EFI_STATUS Status;
752 UINTN Index;
753 LIST_ENTRY *Link;
754 VAR_CHECK_VARIABLE_ENTRY *Entry;
755 CHAR16 *Name;
756 VAR_CHECK_VARIABLE_PROPERTY *Property;
757 INTERNAL_VAR_CHECK_FUNCTION VarCheckFunction;
758
759 if (!mEndOfDxe) {
760 //
761 // Only do check after End Of Dxe.
762 //
763 return EFI_SUCCESS;
764 }
765
766 Property = NULL;
767 VarCheckFunction = NULL;
768
769 for ( Link = GetFirstNode (&mVarCheckVariableList)
770 ; !IsNull (&mVarCheckVariableList, Link)
771 ; Link = GetNextNode (&mVarCheckVariableList, Link)
772 ) {
773 Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);
774 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
775 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {
776 Property = &Entry->VariableProperty;
777 break;
778 }
779 }
780 if (Property == NULL) {
781 Property = GetVariableDriverVariableProperty (VariableName, VendorGuid);
782 }
783 if (Property == NULL) {
784 Status = GetUefiDefinedVariableProperty (VariableName, VendorGuid, TRUE, &Property, &VarCheckFunction);
785 if (EFI_ERROR (Status)) {
786 DEBUG ((EFI_D_INFO, "[Variable]: Var Check UEFI defined variable fail %r - %g:%s\n", Status, VendorGuid, VariableName));
787 return Status;
788 }
789 }
790 if (Property != NULL) {
791 if (mEnableLocking && ((Property->Property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) != 0)) {
792 DEBUG ((EFI_D_INFO, "[Variable]: Var Check ReadOnly variable fail %r - %g:%s\n", EFI_WRITE_PROTECTED, VendorGuid, VariableName));
793 return EFI_WRITE_PROTECTED;
794 }
795 if (!((DataSize == 0) || (Attributes == 0))) {
796 //
797 // Not to delete variable.
798 //
799 if (Attributes != Property->Attributes) {
800 DEBUG ((EFI_D_INFO, "[Variable]: Var Check Attributes(0x%08x to 0x%08x) fail %r - %g:%s\n", Property->Attributes, Attributes, EFI_INVALID_PARAMETER, VendorGuid, VariableName));
801 return EFI_INVALID_PARAMETER;
802 }
803 if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) {
804 DEBUG ((EFI_D_INFO, "[Variable]: Var Check DataSize fail(0x%x not in 0x%x - 0x%x) %r - %g:%s\n", DataSize, Property->MinSize, Property->MaxSize, EFI_INVALID_PARAMETER, VendorGuid, VariableName));
805 return EFI_INVALID_PARAMETER;
806 }
807 if (VarCheckFunction != NULL) {
808 Status = VarCheckFunction (
809 Property,
810 DataSize,
811 Data
812 );
813 if (EFI_ERROR (Status)) {
814 DEBUG ((EFI_D_INFO, "[Variable]: Internal Var Check function fail %r - %g:%s\n", Status, VendorGuid, VariableName));
815 return Status;
816 }
817 }
818 }
819 }
820
821 for (Index = 0; Index < mNumberOfHandler; Index++) {
822 Status = mHandlerTable[Index] (
823 VariableName,
824 VendorGuid,
825 Attributes,
826 DataSize,
827 Data
828 );
829 if (EFI_ERROR (Status)) {
830 DEBUG ((EFI_D_INFO, "[Variable]: Var Check handler fail %r - %g:%s\n", Status, VendorGuid, VariableName));
831 return Status;
832 }
833 }
834 return EFI_SUCCESS;
835 }
836
837 /**
838 Reallocates more global memory to store the registered handler list.
839
840 @retval RETURN_SUCCESS Reallocate memory successfully.
841 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocate.
842
843 **/
844 RETURN_STATUS
845 EFIAPI
846 ReallocateHandlerTable (
847 VOID
848 )
849 {
850 VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *HandlerTable;
851
852 //
853 // Reallocate memory for check handler table.
854 //
855 HandlerTable = ReallocateRuntimePool (
856 mMaxNumberOfHandler * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER),
857 (mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE) * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER),
858 mHandlerTable
859 );
860
861 //
862 // No enough resource to allocate.
863 //
864 if (HandlerTable == NULL) {
865 return RETURN_OUT_OF_RESOURCES;
866 }
867
868 mHandlerTable = HandlerTable;
869 //
870 // Increase max handler number.
871 //
872 mMaxNumberOfHandler = mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE;
873 return RETURN_SUCCESS;
874 }
875
876 /**
877 Register SetVariable check handler.
878
879 @param[in] Handler Pointer to check handler.
880
881 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.
882 @retval EFI_INVALID_PARAMETER Handler is NULL.
883 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
884 already been signaled.
885 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.
886 @retval EFI_UNSUPPORTED This interface is not implemented.
887 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.
888
889 **/
890 EFI_STATUS
891 EFIAPI
892 VarCheckRegisterSetVariableCheckHandler (
893 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler
894 )
895 {
896 EFI_STATUS Status;
897
898 if (Handler == NULL) {
899 return EFI_INVALID_PARAMETER;
900 }
901
902 if (mEndOfDxe) {
903 return EFI_ACCESS_DENIED;
904 }
905
906 DEBUG ((EFI_D_INFO, "RegisterSetVariableCheckHandler - 0x%x\n", Handler));
907
908 //
909 // Check whether the handler list is enough to store new handler.
910 //
911 if (mNumberOfHandler == mMaxNumberOfHandler) {
912 //
913 // Allocate more resources for new handler.
914 //
915 Status = ReallocateHandlerTable();
916 if (EFI_ERROR (Status)) {
917 return Status;
918 }
919 }
920
921 //
922 // Register new handler into the handler list.
923 //
924 mHandlerTable[mNumberOfHandler] = Handler;
925 mNumberOfHandler++;
926
927 return EFI_SUCCESS;
928 }
929
930 /**
931 Variable property get function.
932
933 @param[in] Name Pointer to the variable name.
934 @param[in] Guid Pointer to the vendor GUID.
935 @param[in] WildcardMatch Try wildcard match or not.
936
937 @return Pointer to the property of variable specified by the Name and Guid.
938
939 **/
940 VAR_CHECK_VARIABLE_PROPERTY *
941 VariablePropertyGetFunction (
942 IN CHAR16 *Name,
943 IN EFI_GUID *Guid,
944 IN BOOLEAN WildcardMatch
945 )
946 {
947 LIST_ENTRY *Link;
948 VAR_CHECK_VARIABLE_ENTRY *Entry;
949 CHAR16 *VariableName;
950 VAR_CHECK_VARIABLE_PROPERTY *Property;
951
952 for ( Link = GetFirstNode (&mVarCheckVariableList)
953 ; !IsNull (&mVarCheckVariableList, Link)
954 ; Link = GetNextNode (&mVarCheckVariableList, Link)
955 ) {
956 Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);
957 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
958 if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {
959 return &Entry->VariableProperty;
960 }
961 }
962
963 Property = GetVariableDriverVariableProperty (Name, Guid);
964 if (Property == NULL) {
965 GetUefiDefinedVariableProperty (Name, Guid, WildcardMatch, &Property, NULL);
966 }
967
968 return Property;
969 }
970
971 /**
972 Variable property set.
973
974 @param[in] Name Pointer to the variable name.
975 @param[in] Guid Pointer to the vendor GUID.
976 @param[in] VariableProperty Pointer to the input variable property.
977
978 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
979 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,
980 or the fields of VariableProperty are not valid.
981 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
982 already been signaled.
983 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
984
985 **/
986 EFI_STATUS
987 EFIAPI
988 VarCheckVariablePropertySet (
989 IN CHAR16 *Name,
990 IN EFI_GUID *Guid,
991 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
992 )
993 {
994 EFI_STATUS Status;
995 VAR_CHECK_VARIABLE_ENTRY *Entry;
996 CHAR16 *VariableName;
997 VAR_CHECK_VARIABLE_PROPERTY *Property;
998
999 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
1000 return EFI_INVALID_PARAMETER;
1001 }
1002
1003 if (VariableProperty == NULL) {
1004 return EFI_INVALID_PARAMETER;
1005 }
1006
1007 if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {
1008 return EFI_INVALID_PARAMETER;
1009 }
1010
1011 if (mEndOfDxe) {
1012 return EFI_ACCESS_DENIED;
1013 }
1014
1015 Status = EFI_SUCCESS;
1016
1017 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1018
1019 Property = VariablePropertyGetFunction (Name, Guid, FALSE);
1020 if (Property != NULL) {
1021 CopyMem (Property, VariableProperty, sizeof (*VariableProperty));
1022 } else {
1023 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (Name));
1024 if (Entry == NULL) {
1025 Status = EFI_OUT_OF_RESOURCES;
1026 goto Done;
1027 }
1028 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1029 StrnCpy (VariableName, Name, StrLen (Name));
1030 CopyGuid (&Entry->Guid, Guid);
1031 CopyMem (&Entry->VariableProperty, VariableProperty, sizeof (*VariableProperty));
1032 InsertTailList (&mVarCheckVariableList, &Entry->Link);
1033 }
1034
1035 Done:
1036 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1037
1038 return Status;
1039 }
1040
1041 /**
1042 Internal variable property get.
1043
1044 @param[in] Name Pointer to the variable name.
1045 @param[in] Guid Pointer to the vendor GUID.
1046 @param[out] VariableProperty Pointer to the output variable property.
1047
1048 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
1049 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
1050
1051 **/
1052 EFI_STATUS
1053 EFIAPI
1054 InternalVarCheckVariablePropertyGet (
1055 IN CHAR16 *Name,
1056 IN EFI_GUID *Guid,
1057 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1058 )
1059 {
1060 LIST_ENTRY *Link;
1061 VARIABLE_ENTRY *Entry;
1062 CHAR16 *VariableName;
1063 BOOLEAN Found;
1064 VAR_CHECK_VARIABLE_PROPERTY *Property;
1065
1066 Found = FALSE;
1067
1068 Property = VariablePropertyGetFunction (Name, Guid, TRUE);
1069 if (Property != NULL) {
1070 CopyMem (VariableProperty, Property, sizeof (*VariableProperty));
1071 Found = TRUE;
1072 }
1073
1074 for ( Link = GetFirstNode (&mLockedVariableList)
1075 ; !IsNull (&mLockedVariableList, Link)
1076 ; Link = GetNextNode (&mLockedVariableList, Link)
1077 ) {
1078 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);
1079 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1080 if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {
1081 VariableProperty->Property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
1082 if (!Found) {
1083 VariableProperty->Revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
1084 Found = TRUE;
1085 }
1086 }
1087 }
1088
1089 return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);
1090 }
1091
1092 /**
1093 Variable property get.
1094
1095 @param[in] Name Pointer to the variable name.
1096 @param[in] Guid Pointer to the vendor GUID.
1097 @param[out] VariableProperty Pointer to the output variable property.
1098
1099 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
1100 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.
1101 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
1102
1103 **/
1104 EFI_STATUS
1105 EFIAPI
1106 VarCheckVariablePropertyGet (
1107 IN CHAR16 *Name,
1108 IN EFI_GUID *Guid,
1109 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1110 )
1111 {
1112 EFI_STATUS Status;
1113
1114 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
1115 return EFI_INVALID_PARAMETER;
1116 }
1117
1118 if (VariableProperty == NULL) {
1119 return EFI_INVALID_PARAMETER;
1120 }
1121
1122 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1123
1124 Status = InternalVarCheckVariablePropertyGet (Name, Guid, VariableProperty);
1125
1126 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1127
1128 return Status;
1129 }
1130