]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VarCheck.c
MdeModulePkg/Universal/Variable: Use safe string functions to refine code.
[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_SYS_PREP_ORDER_VARIABLE_NAME,
408 {
409 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
410 0,
411 VARIABLE_ATTRIBUTE_NV_BS_RT,
412 sizeof (UINT16),
413 MAX_UINTN
414 },
415 InternalVarCheckSizeArray
416 },
417 {
418 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME,
419 {
420 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
421 0,
422 VARIABLE_ATTRIBUTE_NV_BS_RT,
423 sizeof (UINT16),
424 sizeof (UINT16)
425 },
426 NULL
427 },
428 {
429 EFI_SETUP_MODE_NAME,
430 {
431 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
432 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
433 VARIABLE_ATTRIBUTE_BS_RT,
434 sizeof (UINT8),
435 sizeof (UINT8)
436 },
437 NULL
438 },
439 {
440 EFI_KEY_EXCHANGE_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_PLATFORM_KEY_NAME,
452 {
453 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
454 0,
455 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
456 1,
457 MAX_UINTN
458 },
459 NULL
460 },
461 {
462 EFI_SIGNATURE_SUPPORT_NAME,
463 {
464 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
465 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
466 VARIABLE_ATTRIBUTE_BS_RT,
467 sizeof (EFI_GUID),
468 MAX_UINTN
469 },
470 InternalVarCheckSizeArray
471 },
472 {
473 EFI_SECURE_BOOT_MODE_NAME,
474 {
475 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
476 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
477 VARIABLE_ATTRIBUTE_BS_RT,
478 sizeof (UINT8),
479 sizeof (UINT8)
480 },
481 NULL
482 },
483 {
484 EFI_KEK_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_PK_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_DB_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_DBX_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_DBT_DEFAULT_VARIABLE_NAME,
529 {
530 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
531 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
532 VARIABLE_ATTRIBUTE_BS_RT,
533 1,
534 MAX_UINTN
535 },
536 NULL
537 },
538 {
539 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,
540 {
541 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
542 0,
543 VARIABLE_ATTRIBUTE_BS_RT,
544 sizeof (UINT64),
545 sizeof (UINT64)
546 },
547 NULL
548 },
549 {
550 EFI_OS_INDICATIONS_VARIABLE_NAME,
551 {
552 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
553 0,
554 VARIABLE_ATTRIBUTE_NV_BS_RT,
555 sizeof (UINT64),
556 sizeof (UINT64)
557 },
558 NULL
559 },
560 {
561 EFI_VENDOR_KEYS_VARIABLE_NAME,
562 {
563 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
564 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
565 VARIABLE_ATTRIBUTE_BS_RT,
566 sizeof (UINT8),
567 sizeof (UINT8)
568 },
569 NULL
570 },
571 };
572 UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {
573 {
574 L"Boot####",
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"Driver####",
586 {
587 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
588 0,
589 VARIABLE_ATTRIBUTE_NV_BS_RT,
590 sizeof (UINT32) + sizeof (UINT16),
591 MAX_UINTN
592 },
593 InternalVarCheckLoadOption
594 },
595 {
596 L"SysPrep####",
597 {
598 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
599 0,
600 VARIABLE_ATTRIBUTE_NV_BS_RT,
601 sizeof (UINT32) + sizeof (UINT16),
602 MAX_UINTN
603 },
604 InternalVarCheckLoadOption
605 },
606 {
607 L"Key####",
608 {
609 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
610 0,
611 VARIABLE_ATTRIBUTE_NV_BS_RT,
612 sizeof (EFI_KEY_OPTION),
613 sizeof (EFI_KEY_OPTION) + 3 * sizeof (EFI_INPUT_KEY)
614 },
615 InternalVarCheckKeyOption
616 },
617 };
618
619 //
620 // EFI_IMAGE_SECURITY_DATABASE_GUID
621 //
622 UEFI_DEFINED_VARIABLE_ENTRY mImageSecurityVariableList[] = {
623 {
624 EFI_IMAGE_SECURITY_DATABASE,
625 {
626 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
627 0,
628 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
629 1,
630 MAX_UINTN
631 },
632 NULL
633 },
634 {
635 EFI_IMAGE_SECURITY_DATABASE1,
636 {
637 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
638 0,
639 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
640 1,
641 MAX_UINTN
642 },
643 NULL
644 },
645 {
646 EFI_IMAGE_SECURITY_DATABASE2,
647 {
648 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
649 0,
650 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
651 1,
652 MAX_UINTN
653 },
654 NULL
655 },
656 };
657
658 /**
659 Get UEFI defined global variable or image security database variable property.
660 The code will check if variable guid is global variable or image security database guid first.
661 If yes, further check if variable name is in mGlobalVariableList, mGlobalVariableList2 or mImageSecurityVariableList.
662
663 @param[in] VariableName Pointer to variable name.
664 @param[in] VendorGuid Variable Vendor Guid.
665 @param[in] WildcardMatch Try wildcard match or not.
666 @param[out] VariableProperty Pointer to variable property.
667 @param[out] VarCheckFunction Pointer to check function.
668
669 @retval EFI_SUCCESS Variable is not global variable or image security database variable.
670 @retval EFI_INVALID_PARAMETER Variable is global variable or image security database variable, but variable name is not in the lists.
671
672 **/
673 EFI_STATUS
674 GetUefiDefinedVariableProperty (
675 IN CHAR16 *VariableName,
676 IN EFI_GUID *VendorGuid,
677 IN BOOLEAN WildcardMatch,
678 OUT VAR_CHECK_VARIABLE_PROPERTY **VariableProperty,
679 OUT INTERNAL_VAR_CHECK_FUNCTION *VarCheckFunction OPTIONAL
680 )
681 {
682 UINTN Index;
683 UINTN NameLength;
684
685 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {
686 //
687 // Try list 1, exactly match.
688 //
689 for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {
690 if (StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) {
691 if (VarCheckFunction != NULL) {
692 *VarCheckFunction = mGlobalVariableList[Index].CheckFunction;
693 }
694 *VariableProperty = &mGlobalVariableList[Index].VariableProperty;
695 return EFI_SUCCESS;
696 }
697 }
698
699 //
700 // Try list 2.
701 //
702 NameLength = StrLen (VariableName) - 4;
703 for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {
704 if (WildcardMatch) {
705 if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&
706 (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&
707 IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&
708 IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&
709 IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&
710 IsHexaDecimalDigitCharacter (VariableName[NameLength + 3])) {
711 if (VarCheckFunction != NULL) {
712 *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;
713 }
714 *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;
715 return EFI_SUCCESS;
716 }
717 }
718 if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) {
719 if (VarCheckFunction != NULL) {
720 *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;
721 }
722 *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;
723 return EFI_SUCCESS;
724 }
725 }
726
727 //
728 // The variable name is not in the lists.
729 //
730 return EFI_INVALID_PARAMETER;
731 }
732
733 if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid)) {
734 for (Index = 0; Index < sizeof (mImageSecurityVariableList)/sizeof (mImageSecurityVariableList[0]); Index++) {
735 if (StrCmp (mImageSecurityVariableList[Index].Name, VariableName) == 0) {
736 if (VarCheckFunction != NULL) {
737 *VarCheckFunction = mImageSecurityVariableList[Index].CheckFunction;
738 }
739 *VariableProperty = &mImageSecurityVariableList[Index].VariableProperty;
740 return EFI_SUCCESS;
741 }
742 }
743
744 return EFI_INVALID_PARAMETER;
745 }
746
747 //
748 // It is not global variable, image security database variable.
749 //
750 return EFI_SUCCESS;
751 }
752
753 /**
754 Internal SetVariable check.
755
756 @param[in] VariableName Name of Variable to set.
757 @param[in] VendorGuid Variable vendor GUID.
758 @param[in] Attributes Attribute value of the variable.
759 @param[in] DataSize Size of Data to set.
760 @param[in] Data Data pointer.
761
762 @retval EFI_SUCCESS The SetVariable check result was success.
763 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied,
764 or the DataSize exceeds the minimum or maximum allowed,
765 or the Data value is not following UEFI spec for UEFI defined variables.
766 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
767 @retval Others The return status from check handler.
768
769 **/
770 EFI_STATUS
771 EFIAPI
772 InternalVarCheckSetVariableCheck (
773 IN CHAR16 *VariableName,
774 IN EFI_GUID *VendorGuid,
775 IN UINT32 Attributes,
776 IN UINTN DataSize,
777 IN VOID *Data
778 )
779 {
780 EFI_STATUS Status;
781 UINTN Index;
782 LIST_ENTRY *Link;
783 VAR_CHECK_VARIABLE_ENTRY *Entry;
784 CHAR16 *Name;
785 VAR_CHECK_VARIABLE_PROPERTY *Property;
786 INTERNAL_VAR_CHECK_FUNCTION VarCheckFunction;
787
788 if (!mEndOfDxe) {
789 //
790 // Only do check after End Of Dxe.
791 //
792 return EFI_SUCCESS;
793 }
794
795 Property = NULL;
796 VarCheckFunction = NULL;
797
798 for ( Link = GetFirstNode (&mVarCheckVariableList)
799 ; !IsNull (&mVarCheckVariableList, Link)
800 ; Link = GetNextNode (&mVarCheckVariableList, Link)
801 ) {
802 Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);
803 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
804 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {
805 Property = &Entry->VariableProperty;
806 break;
807 }
808 }
809 if (Property == NULL) {
810 Status = GetUefiDefinedVariableProperty (VariableName, VendorGuid, TRUE, &Property, &VarCheckFunction);
811 if (EFI_ERROR (Status)) {
812 //
813 // To prevent name collisions with possible future globally defined variables,
814 // other internal firmware data variables that are not defined here must be
815 // saved with a unique VendorGuid other than EFI_GLOBAL_VARIABLE or
816 // any other GUID defined by the UEFI Specification. Implementations must
817 // only permit the creation of variables with a UEFI Specification-defined
818 // VendorGuid when these variables are documented in the UEFI Specification.
819 //
820 DEBUG ((EFI_D_INFO, "Variable Check UEFI defined variable fail %r - %s not in %g namespace\n", Status, VariableName, VendorGuid));
821 return Status;
822 }
823 }
824 if (Property != NULL) {
825 if (mEnableLocking && ((Property->Property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) != 0)) {
826 DEBUG ((EFI_D_INFO, "[Variable]: Var Check ReadOnly variable fail %r - %g:%s\n", EFI_WRITE_PROTECTED, VendorGuid, VariableName));
827 return EFI_WRITE_PROTECTED;
828 }
829 if (!((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0))) {
830 //
831 // Not to delete variable.
832 //
833 if ((Attributes != 0) && ((Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Property->Attributes)) {
834 DEBUG ((EFI_D_INFO, "Variable Check Attributes(0x%08x to 0x%08x) fail %r - %g:%s\n", Property->Attributes, Attributes, EFI_INVALID_PARAMETER, VendorGuid, VariableName));
835 return EFI_INVALID_PARAMETER;
836 }
837 if (DataSize != 0) {
838 if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) {
839 DEBUG ((EFI_D_INFO, "Variable 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));
840 return EFI_INVALID_PARAMETER;
841 }
842 if (VarCheckFunction != NULL) {
843 Status = VarCheckFunction (
844 Property,
845 DataSize,
846 Data
847 );
848 if (EFI_ERROR (Status)) {
849 DEBUG ((EFI_D_INFO, "Internal Variable Check function fail %r - %g:%s\n", Status, VendorGuid, VariableName));
850 return Status;
851 }
852 }
853 }
854 }
855 }
856
857 for (Index = 0; Index < mNumberOfHandler; Index++) {
858 Status = mHandlerTable[Index] (
859 VariableName,
860 VendorGuid,
861 Attributes,
862 DataSize,
863 Data
864 );
865 if (EFI_ERROR (Status)) {
866 DEBUG ((EFI_D_INFO, "Variable Check handler fail %r - %g:%s\n", Status, VendorGuid, VariableName));
867 return Status;
868 }
869 }
870 return EFI_SUCCESS;
871 }
872
873 /**
874 Reallocates more global memory to store the registered handler list.
875
876 @retval RETURN_SUCCESS Reallocate memory successfully.
877 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocate.
878
879 **/
880 RETURN_STATUS
881 EFIAPI
882 ReallocateHandlerTable (
883 VOID
884 )
885 {
886 VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *HandlerTable;
887
888 //
889 // Reallocate memory for check handler table.
890 //
891 HandlerTable = ReallocateRuntimePool (
892 mMaxNumberOfHandler * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER),
893 (mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE) * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER),
894 mHandlerTable
895 );
896
897 //
898 // No enough resource to allocate.
899 //
900 if (HandlerTable == NULL) {
901 return RETURN_OUT_OF_RESOURCES;
902 }
903
904 mHandlerTable = HandlerTable;
905 //
906 // Increase max handler number.
907 //
908 mMaxNumberOfHandler = mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE;
909 return RETURN_SUCCESS;
910 }
911
912 /**
913 Register SetVariable check handler.
914
915 @param[in] Handler Pointer to check handler.
916
917 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.
918 @retval EFI_INVALID_PARAMETER Handler is NULL.
919 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
920 already been signaled.
921 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.
922 @retval EFI_UNSUPPORTED This interface is not implemented.
923 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.
924
925 **/
926 EFI_STATUS
927 EFIAPI
928 VarCheckRegisterSetVariableCheckHandler (
929 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler
930 )
931 {
932 EFI_STATUS Status;
933
934 if (Handler == NULL) {
935 return EFI_INVALID_PARAMETER;
936 }
937
938 if (mEndOfDxe) {
939 return EFI_ACCESS_DENIED;
940 }
941
942 DEBUG ((EFI_D_INFO, "RegisterSetVariableCheckHandler - 0x%x\n", Handler));
943
944 Status = EFI_SUCCESS;
945
946 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
947
948 //
949 // Check whether the handler list is enough to store new handler.
950 //
951 if (mNumberOfHandler == mMaxNumberOfHandler) {
952 //
953 // Allocate more resources for new handler.
954 //
955 Status = ReallocateHandlerTable();
956 if (EFI_ERROR (Status)) {
957 goto Done;
958 }
959 }
960
961 //
962 // Register new handler into the handler list.
963 //
964 mHandlerTable[mNumberOfHandler] = Handler;
965 mNumberOfHandler++;
966
967 Done:
968 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
969
970 return Status;
971 }
972
973 /**
974 Variable property get function.
975
976 @param[in] Name Pointer to the variable name.
977 @param[in] Guid Pointer to the vendor GUID.
978 @param[in] WildcardMatch Try wildcard match or not.
979
980 @return Pointer to the property of variable specified by the Name and Guid.
981
982 **/
983 VAR_CHECK_VARIABLE_PROPERTY *
984 VariablePropertyGetFunction (
985 IN CHAR16 *Name,
986 IN EFI_GUID *Guid,
987 IN BOOLEAN WildcardMatch
988 )
989 {
990 LIST_ENTRY *Link;
991 VAR_CHECK_VARIABLE_ENTRY *Entry;
992 CHAR16 *VariableName;
993 VAR_CHECK_VARIABLE_PROPERTY *Property;
994
995 Property = NULL;
996
997 for ( Link = GetFirstNode (&mVarCheckVariableList)
998 ; !IsNull (&mVarCheckVariableList, Link)
999 ; Link = GetNextNode (&mVarCheckVariableList, Link)
1000 ) {
1001 Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);
1002 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1003 if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {
1004 return &Entry->VariableProperty;
1005 }
1006 }
1007
1008 GetUefiDefinedVariableProperty (Name, Guid, WildcardMatch, &Property, NULL);
1009
1010 return Property;
1011 }
1012
1013 /**
1014 Internal variable property set.
1015
1016 @param[in] Name Pointer to the variable name.
1017 @param[in] Guid Pointer to the vendor GUID.
1018 @param[in] VariableProperty Pointer to the input variable property.
1019
1020 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
1021 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
1022
1023 **/
1024 EFI_STATUS
1025 EFIAPI
1026 InternalVarCheckVariablePropertySet (
1027 IN CHAR16 *Name,
1028 IN EFI_GUID *Guid,
1029 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1030 )
1031 {
1032 EFI_STATUS Status;
1033 VAR_CHECK_VARIABLE_ENTRY *Entry;
1034 CHAR16 *VariableName;
1035 VAR_CHECK_VARIABLE_PROPERTY *Property;
1036
1037 Status = EFI_SUCCESS;
1038
1039 Property = VariablePropertyGetFunction (Name, Guid, FALSE);
1040 if (Property != NULL) {
1041 CopyMem (Property, VariableProperty, sizeof (*VariableProperty));
1042 } else {
1043 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (Name));
1044 if (Entry == NULL) {
1045 Status = EFI_OUT_OF_RESOURCES;
1046 goto Done;
1047 }
1048 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1049 StrCpyS (VariableName, StrSize(Name)/sizeof(CHAR16), Name);
1050 CopyGuid (&Entry->Guid, Guid);
1051 CopyMem (&Entry->VariableProperty, VariableProperty, sizeof (*VariableProperty));
1052 InsertTailList (&mVarCheckVariableList, &Entry->Link);
1053 }
1054
1055 Done:
1056 return Status;
1057 }
1058
1059 /**
1060 Variable property set.
1061
1062 @param[in] Name Pointer to the variable name.
1063 @param[in] Guid Pointer to the vendor GUID.
1064 @param[in] VariableProperty Pointer to the input variable property.
1065
1066 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
1067 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,
1068 or the fields of VariableProperty are not valid.
1069 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
1070 already been signaled.
1071 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
1072
1073 **/
1074 EFI_STATUS
1075 EFIAPI
1076 VarCheckVariablePropertySet (
1077 IN CHAR16 *Name,
1078 IN EFI_GUID *Guid,
1079 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1080 )
1081 {
1082 EFI_STATUS Status;
1083
1084 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
1085 return EFI_INVALID_PARAMETER;
1086 }
1087
1088 if (VariableProperty == NULL) {
1089 return EFI_INVALID_PARAMETER;
1090 }
1091
1092 if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {
1093 return EFI_INVALID_PARAMETER;
1094 }
1095
1096 if (mEndOfDxe) {
1097 return EFI_ACCESS_DENIED;
1098 }
1099
1100 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1101
1102 Status = InternalVarCheckVariablePropertySet (Name, Guid, VariableProperty);
1103
1104 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1105
1106 return Status;
1107 }
1108
1109 /**
1110 Internal variable property get.
1111
1112 @param[in] Name Pointer to the variable name.
1113 @param[in] Guid Pointer to the vendor GUID.
1114 @param[out] VariableProperty Pointer to the output variable property.
1115
1116 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
1117 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
1118
1119 **/
1120 EFI_STATUS
1121 EFIAPI
1122 InternalVarCheckVariablePropertyGet (
1123 IN CHAR16 *Name,
1124 IN EFI_GUID *Guid,
1125 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1126 )
1127 {
1128 LIST_ENTRY *Link;
1129 VARIABLE_ENTRY *Entry;
1130 CHAR16 *VariableName;
1131 BOOLEAN Found;
1132 VAR_CHECK_VARIABLE_PROPERTY *Property;
1133
1134 Found = FALSE;
1135
1136 Property = VariablePropertyGetFunction (Name, Guid, TRUE);
1137 if (Property != NULL) {
1138 CopyMem (VariableProperty, Property, sizeof (*VariableProperty));
1139 Found = TRUE;
1140 }
1141
1142 for ( Link = GetFirstNode (&mLockedVariableList)
1143 ; !IsNull (&mLockedVariableList, Link)
1144 ; Link = GetNextNode (&mLockedVariableList, Link)
1145 ) {
1146 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);
1147 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1148 if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {
1149 VariableProperty->Property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
1150 if (!Found) {
1151 VariableProperty->Revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
1152 Found = TRUE;
1153 }
1154 }
1155 }
1156
1157 return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);
1158 }
1159
1160 /**
1161 Variable property get.
1162
1163 @param[in] Name Pointer to the variable name.
1164 @param[in] Guid Pointer to the vendor GUID.
1165 @param[out] VariableProperty Pointer to the output variable property.
1166
1167 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
1168 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.
1169 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
1170
1171 **/
1172 EFI_STATUS
1173 EFIAPI
1174 VarCheckVariablePropertyGet (
1175 IN CHAR16 *Name,
1176 IN EFI_GUID *Guid,
1177 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1178 )
1179 {
1180 EFI_STATUS Status;
1181
1182 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
1183 return EFI_INVALID_PARAMETER;
1184 }
1185
1186 if (VariableProperty == NULL) {
1187 return EFI_INVALID_PARAMETER;
1188 }
1189
1190 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1191
1192 Status = InternalVarCheckVariablePropertyGet (Name, Guid, VariableProperty);
1193
1194 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1195
1196 return Status;
1197 }
1198