]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableParsing.c
1 /** @file
2 Functions in this module are associated with variable parsing operations and
3 are intended to be usable across variable driver source files.
4
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "VariableParsing.h"
11
12 /**
13
14 This code checks if variable header is valid or not.
15
16 @param[in] Variable Pointer to the Variable Header.
17 @param[in] VariableStoreEnd Pointer to the Variable Store End.
18
19 @retval TRUE Variable header is valid.
20 @retval FALSE Variable header is not valid.
21
22 **/
23 BOOLEAN
24 IsValidVariableHeader (
25 IN VARIABLE_HEADER *Variable,
26 IN VARIABLE_HEADER *VariableStoreEnd
27 )
28 {
29 if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {
30 //
31 // Variable is NULL or has reached the end of variable store,
32 // or the StartId is not correct.
33 //
34 return FALSE;
35 }
36
37 return TRUE;
38 }
39
40 /**
41
42 This code gets the current status of Variable Store.
43
44 @param[in] VarStoreHeader Pointer to the Variable Store Header.
45
46 @retval EfiRaw Variable store status is raw.
47 @retval EfiValid Variable store status is valid.
48 @retval EfiInvalid Variable store status is invalid.
49
50 **/
51 VARIABLE_STORE_STATUS
52 GetVariableStoreStatus (
53 IN VARIABLE_STORE_HEADER *VarStoreHeader
54 )
55 {
56 if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||
57 CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
58 (VarStoreHeader->Format == VARIABLE_STORE_FORMATTED) &&
59 (VarStoreHeader->State == VARIABLE_STORE_HEALTHY)
60 )
61 {
62 return EfiValid;
63 } else if ((((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff) &&
64 (((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff) &&
65 (((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff) &&
66 (((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff) &&
67 (VarStoreHeader->Size == 0xffffffff) &&
68 (VarStoreHeader->Format == 0xff) &&
69 (VarStoreHeader->State == 0xff)
70 )
71 {
72 return EfiRaw;
73 } else {
74 return EfiInvalid;
75 }
76 }
77
78 /**
79 This code gets the size of variable header.
80
81 @param[in] AuthFormat TRUE indicates authenticated variables are used.
82 FALSE indicates authenticated variables are not used.
83
84 @return Size of variable header in bytes in type UINTN.
85
86 **/
87 UINTN
88 GetVariableHeaderSize (
89 IN BOOLEAN AuthFormat
90 )
91 {
92 UINTN Value;
93
94 if (AuthFormat) {
95 Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
96 } else {
97 Value = sizeof (VARIABLE_HEADER);
98 }
99
100 return Value;
101 }
102
103 /**
104
105 This code gets the size of name of variable.
106
107 @param[in] Variable Pointer to the variable header.
108 @param[in] AuthFormat TRUE indicates authenticated variables are used.
109 FALSE indicates authenticated variables are not used.
110
111 @return UINTN Size of variable in bytes.
112
113 **/
114 UINTN
115 NameSizeOfVariable (
116 IN VARIABLE_HEADER *Variable,
117 IN BOOLEAN AuthFormat
118 )
119 {
120 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
121
122 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;
123 if (AuthFormat) {
124 if ((AuthVariable->State == (UINT8)(-1)) ||
125 (AuthVariable->DataSize == (UINT32)(-1)) ||
126 (AuthVariable->NameSize == (UINT32)(-1)) ||
127 (AuthVariable->Attributes == (UINT32)(-1)))
128 {
129 return 0;
130 }
131
132 return (UINTN)AuthVariable->NameSize;
133 } else {
134 if ((Variable->State == (UINT8)(-1)) ||
135 (Variable->DataSize == (UINT32)(-1)) ||
136 (Variable->NameSize == (UINT32)(-1)) ||
137 (Variable->Attributes == (UINT32)(-1)))
138 {
139 return 0;
140 }
141
142 return (UINTN)Variable->NameSize;
143 }
144 }
145
146 /**
147 This code sets the size of name of variable.
148
149 @param[in] Variable Pointer to the Variable Header.
150 @param[in] NameSize Name size to set.
151 @param[in] AuthFormat TRUE indicates authenticated variables are used.
152 FALSE indicates authenticated variables are not used.
153
154 **/
155 VOID
156 SetNameSizeOfVariable (
157 IN VARIABLE_HEADER *Variable,
158 IN UINTN NameSize,
159 IN BOOLEAN AuthFormat
160 )
161 {
162 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
163
164 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;
165 if (AuthFormat) {
166 AuthVariable->NameSize = (UINT32)NameSize;
167 } else {
168 Variable->NameSize = (UINT32)NameSize;
169 }
170 }
171
172 /**
173
174 This code gets the size of variable data.
175
176 @param[in] Variable Pointer to the Variable Header.
177 @param[in] AuthFormat TRUE indicates authenticated variables are used.
178 FALSE indicates authenticated variables are not used.
179
180 @return Size of variable in bytes.
181
182 **/
183 UINTN
184 DataSizeOfVariable (
185 IN VARIABLE_HEADER *Variable,
186 IN BOOLEAN AuthFormat
187 )
188 {
189 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
190
191 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;
192 if (AuthFormat) {
193 if ((AuthVariable->State == (UINT8)(-1)) ||
194 (AuthVariable->DataSize == (UINT32)(-1)) ||
195 (AuthVariable->NameSize == (UINT32)(-1)) ||
196 (AuthVariable->Attributes == (UINT32)(-1)))
197 {
198 return 0;
199 }
200
201 return (UINTN)AuthVariable->DataSize;
202 } else {
203 if ((Variable->State == (UINT8)(-1)) ||
204 (Variable->DataSize == (UINT32)(-1)) ||
205 (Variable->NameSize == (UINT32)(-1)) ||
206 (Variable->Attributes == (UINT32)(-1)))
207 {
208 return 0;
209 }
210
211 return (UINTN)Variable->DataSize;
212 }
213 }
214
215 /**
216 This code sets the size of variable data.
217
218 @param[in] Variable Pointer to the Variable Header.
219 @param[in] DataSize Data size to set.
220 @param[in] AuthFormat TRUE indicates authenticated variables are used.
221 FALSE indicates authenticated variables are not used.
222
223 **/
224 VOID
225 SetDataSizeOfVariable (
226 IN VARIABLE_HEADER *Variable,
227 IN UINTN DataSize,
228 IN BOOLEAN AuthFormat
229 )
230 {
231 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
232
233 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;
234 if (AuthFormat) {
235 AuthVariable->DataSize = (UINT32)DataSize;
236 } else {
237 Variable->DataSize = (UINT32)DataSize;
238 }
239 }
240
241 /**
242
243 This code gets the pointer to the variable name.
244
245 @param[in] Variable Pointer to the Variable Header.
246 @param[in] AuthFormat TRUE indicates authenticated variables are used.
247 FALSE indicates authenticated variables are not used.
248
249 @return Pointer to Variable Name which is Unicode encoding.
250
251 **/
252 CHAR16 *
253 GetVariableNamePtr (
254 IN VARIABLE_HEADER *Variable,
255 IN BOOLEAN AuthFormat
256 )
257 {
258 return (CHAR16 *)((UINTN)Variable + GetVariableHeaderSize (AuthFormat));
259 }
260
261 /**
262 This code gets the pointer to the variable guid.
263
264 @param[in] Variable Pointer to the Variable Header.
265 @param[in] AuthFormat TRUE indicates authenticated variables are used.
266 FALSE indicates authenticated variables are not used.
267
268 @return A EFI_GUID* pointer to Vendor Guid.
269
270 **/
271 EFI_GUID *
272 GetVendorGuidPtr (
273 IN VARIABLE_HEADER *Variable,
274 IN BOOLEAN AuthFormat
275 )
276 {
277 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
278
279 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;
280 if (AuthFormat) {
281 return &AuthVariable->VendorGuid;
282 } else {
283 return &Variable->VendorGuid;
284 }
285 }
286
287 /**
288
289 This code gets the pointer to the variable data.
290
291 @param[in] Variable Pointer to the Variable Header.
292 @param[in] AuthFormat TRUE indicates authenticated variables are used.
293 FALSE indicates authenticated variables are not used.
294
295 @return Pointer to Variable Data.
296
297 **/
298 UINT8 *
299 GetVariableDataPtr (
300 IN VARIABLE_HEADER *Variable,
301 IN BOOLEAN AuthFormat
302 )
303 {
304 UINTN Value;
305
306 //
307 // Be careful about pad size for alignment.
308 //
309 Value = (UINTN)GetVariableNamePtr (Variable, AuthFormat);
310 Value += NameSizeOfVariable (Variable, AuthFormat);
311 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable, AuthFormat));
312
313 return (UINT8 *)Value;
314 }
315
316 /**
317 This code gets the variable data offset related to variable header.
318
319 @param[in] Variable Pointer to the Variable Header.
320 @param[in] AuthFormat TRUE indicates authenticated variables are used.
321 FALSE indicates authenticated variables are not used.
322
323 @return Variable Data offset.
324
325 **/
326 UINTN
327 GetVariableDataOffset (
328 IN VARIABLE_HEADER *Variable,
329 IN BOOLEAN AuthFormat
330 )
331 {
332 UINTN Value;
333
334 //
335 // Be careful about pad size for alignment
336 //
337 Value = GetVariableHeaderSize (AuthFormat);
338 Value += NameSizeOfVariable (Variable, AuthFormat);
339 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable, AuthFormat));
340
341 return Value;
342 }
343
344 /**
345
346 This code gets the pointer to the next variable header.
347
348 @param[in] Variable Pointer to the Variable Header.
349 @param[in] AuthFormat TRUE indicates authenticated variables are used.
350 FALSE indicates authenticated variables are not used.
351
352 @return Pointer to next variable header.
353
354 **/
355 VARIABLE_HEADER *
356 GetNextVariablePtr (
357 IN VARIABLE_HEADER *Variable,
358 IN BOOLEAN AuthFormat
359 )
360 {
361 UINTN Value;
362
363 Value = (UINTN)GetVariableDataPtr (Variable, AuthFormat);
364 Value += DataSizeOfVariable (Variable, AuthFormat);
365 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable, AuthFormat));
366
367 //
368 // Be careful about pad size for alignment.
369 //
370 return (VARIABLE_HEADER *)HEADER_ALIGN (Value);
371 }
372
373 /**
374
375 Gets the pointer to the first variable header in given variable store area.
376
377 @param[in] VarStoreHeader Pointer to the Variable Store Header.
378
379 @return Pointer to the first variable header.
380
381 **/
382 VARIABLE_HEADER *
383 GetStartPointer (
384 IN VARIABLE_STORE_HEADER *VarStoreHeader
385 )
386 {
387 //
388 // The start of variable store.
389 //
390 return (VARIABLE_HEADER *)HEADER_ALIGN (VarStoreHeader + 1);
391 }
392
393 /**
394
395 Gets the pointer to the end of the variable storage area.
396
397 This function gets pointer to the end of the variable storage
398 area, according to the input variable store header.
399
400 @param[in] VarStoreHeader Pointer to the Variable Store Header.
401
402 @return Pointer to the end of the variable storage area.
403
404 **/
405 VARIABLE_HEADER *
406 GetEndPointer (
407 IN VARIABLE_STORE_HEADER *VarStoreHeader
408 )
409 {
410 //
411 // The end of variable store
412 //
413 return (VARIABLE_HEADER *)HEADER_ALIGN ((UINTN)VarStoreHeader + VarStoreHeader->Size);
414 }
415
416 /**
417 Compare two EFI_TIME data.
418
419
420 @param[in] FirstTime A pointer to the first EFI_TIME data.
421 @param[in] SecondTime A pointer to the second EFI_TIME data.
422
423 @retval TRUE The FirstTime is not later than the SecondTime.
424 @retval FALSE The FirstTime is later than the SecondTime.
425
426 **/
427 BOOLEAN
428 VariableCompareTimeStampInternal (
429 IN EFI_TIME *FirstTime,
430 IN EFI_TIME *SecondTime
431 )
432 {
433 if (FirstTime->Year != SecondTime->Year) {
434 return (BOOLEAN)(FirstTime->Year < SecondTime->Year);
435 } else if (FirstTime->Month != SecondTime->Month) {
436 return (BOOLEAN)(FirstTime->Month < SecondTime->Month);
437 } else if (FirstTime->Day != SecondTime->Day) {
438 return (BOOLEAN)(FirstTime->Day < SecondTime->Day);
439 } else if (FirstTime->Hour != SecondTime->Hour) {
440 return (BOOLEAN)(FirstTime->Hour < SecondTime->Hour);
441 } else if (FirstTime->Minute != SecondTime->Minute) {
442 return (BOOLEAN)(FirstTime->Minute < SecondTime->Minute);
443 }
444
445 return (BOOLEAN)(FirstTime->Second <= SecondTime->Second);
446 }
447
448 /**
449 Find the variable in the specified variable store.
450
451 @param[in] VariableName Name of the variable to be found
452 @param[in] VendorGuid Vendor GUID to be found.
453 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
454 check at runtime when searching variable.
455 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
456 @param[in] AuthFormat TRUE indicates authenticated variables are used.
457 FALSE indicates authenticated variables are not used.
458
459 @retval EFI_SUCCESS Variable found successfully
460 @retval EFI_NOT_FOUND Variable not found
461 **/
462 EFI_STATUS
463 FindVariableEx (
464 IN CHAR16 *VariableName,
465 IN EFI_GUID *VendorGuid,
466 IN BOOLEAN IgnoreRtCheck,
467 IN OUT VARIABLE_POINTER_TRACK *PtrTrack,
468 IN BOOLEAN AuthFormat
469 )
470 {
471 VARIABLE_HEADER *InDeletedVariable;
472 VOID *Point;
473
474 PtrTrack->InDeletedTransitionPtr = NULL;
475
476 //
477 // Find the variable by walk through HOB, volatile and non-volatile variable store.
478 //
479 InDeletedVariable = NULL;
480
481 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
482 ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
483 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr, AuthFormat)
484 )
485 {
486 if ((PtrTrack->CurrPtr->State == VAR_ADDED) ||
487 (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED))
488 )
489 {
490 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
491 if (VariableName[0] == 0) {
492 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
493 InDeletedVariable = PtrTrack->CurrPtr;
494 } else {
495 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
496 return EFI_SUCCESS;
497 }
498 } else {
499 if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr, AuthFormat))) {
500 Point = (VOID *)GetVariableNamePtr (PtrTrack->CurrPtr, AuthFormat);
501
502 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr, AuthFormat) != 0);
503 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr, AuthFormat)) == 0) {
504 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
505 InDeletedVariable = PtrTrack->CurrPtr;
506 } else {
507 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
508 return EFI_SUCCESS;
509 }
510 }
511 }
512 }
513 }
514 }
515 }
516
517 PtrTrack->CurrPtr = InDeletedVariable;
518 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
519 }
520
521 /**
522 This code finds the next available variable.
523
524 Caution: This function may receive untrusted input.
525 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
526
527 @param[in] VariableName Pointer to variable name.
528 @param[in] VendorGuid Variable Vendor Guid.
529 @param[in] VariableStoreList A list of variable stores that should be used to get the next variable.
530 The maximum number of entries is the max value of VARIABLE_STORE_TYPE.
531 @param[out] VariablePtr Pointer to variable header address.
532 @param[in] AuthFormat TRUE indicates authenticated variables are used.
533 FALSE indicates authenticated variables are not used.
534
535 @retval EFI_SUCCESS The function completed successfully.
536 @retval EFI_NOT_FOUND The next variable was not found.
537 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
538 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
539 GUID of an existing variable.
540
541 **/
542 EFI_STATUS
543 EFIAPI
544 VariableServiceGetNextVariableInternal (
545 IN CHAR16 *VariableName,
546 IN EFI_GUID *VendorGuid,
547 IN VARIABLE_STORE_HEADER **VariableStoreList,
548 OUT VARIABLE_HEADER **VariablePtr,
549 IN BOOLEAN AuthFormat
550 )
551 {
552 EFI_STATUS Status;
553 VARIABLE_STORE_TYPE StoreType;
554 VARIABLE_POINTER_TRACK Variable;
555 VARIABLE_POINTER_TRACK VariableInHob;
556 VARIABLE_POINTER_TRACK VariablePtrTrack;
557
558 Status = EFI_NOT_FOUND;
559
560 if (VariableStoreList == NULL) {
561 return EFI_INVALID_PARAMETER;
562 }
563
564 ZeroMem (&Variable, sizeof (Variable));
565
566 // Check if the variable exists in the given variable store list
567 for (StoreType = (VARIABLE_STORE_TYPE)0; StoreType < VariableStoreTypeMax; StoreType++) {
568 if (VariableStoreList[StoreType] == NULL) {
569 continue;
570 }
571
572 Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
573 Variable.EndPtr = GetEndPointer (VariableStoreList[StoreType]);
574 Variable.Volatile = (BOOLEAN)(StoreType == VariableStoreTypeVolatile);
575
576 Status = FindVariableEx (VariableName, VendorGuid, FALSE, &Variable, AuthFormat);
577 if (!EFI_ERROR (Status)) {
578 break;
579 }
580 }
581
582 if ((Variable.CurrPtr == NULL) || EFI_ERROR (Status)) {
583 //
584 // For VariableName is an empty string, FindVariableEx() will try to find and return
585 // the first qualified variable, and if FindVariableEx() returns error (EFI_NOT_FOUND)
586 // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
587 //
588 if (VariableName[0] != 0) {
589 //
590 // For VariableName is not an empty string, and FindVariableEx() returns error as
591 // VariableName and VendorGuid are not a name and GUID of an existing variable,
592 // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
593 //
594 Status = EFI_INVALID_PARAMETER;
595 }
596
597 goto Done;
598 }
599
600 if (VariableName[0] != 0) {
601 //
602 // If variable name is not empty, get next variable.
603 //
604 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
605 }
606
607 while (TRUE) {
608 //
609 // Switch to the next variable store if needed
610 //
611 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
612 //
613 // Find current storage index
614 //
615 for (StoreType = (VARIABLE_STORE_TYPE)0; StoreType < VariableStoreTypeMax; StoreType++) {
616 if ((VariableStoreList[StoreType] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreList[StoreType]))) {
617 break;
618 }
619 }
620
621 ASSERT (StoreType < VariableStoreTypeMax);
622 //
623 // Switch to next storage
624 //
625 for (StoreType++; StoreType < VariableStoreTypeMax; StoreType++) {
626 if (VariableStoreList[StoreType] != NULL) {
627 break;
628 }
629 }
630
631 //
632 // Capture the case that
633 // 1. current storage is the last one, or
634 // 2. no further storage
635 //
636 if (StoreType == VariableStoreTypeMax) {
637 Status = EFI_NOT_FOUND;
638 goto Done;
639 }
640
641 Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
642 Variable.EndPtr = GetEndPointer (VariableStoreList[StoreType]);
643 Variable.CurrPtr = Variable.StartPtr;
644 }
645
646 //
647 // Variable is found
648 //
649 if ((Variable.CurrPtr->State == VAR_ADDED) || (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED))) {
650 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
651 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
652 //
653 // If it is a IN_DELETED_TRANSITION variable,
654 // and there is also a same ADDED one at the same time,
655 // don't return it.
656 //
657 VariablePtrTrack.StartPtr = Variable.StartPtr;
658 VariablePtrTrack.EndPtr = Variable.EndPtr;
659 Status = FindVariableEx (
660 GetVariableNamePtr (Variable.CurrPtr, AuthFormat),
661 GetVendorGuidPtr (Variable.CurrPtr, AuthFormat),
662 FALSE,
663 &VariablePtrTrack,
664 AuthFormat
665 );
666 if (!EFI_ERROR (Status) && (VariablePtrTrack.CurrPtr->State == VAR_ADDED)) {
667 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
668 continue;
669 }
670 }
671
672 //
673 // Don't return NV variable when HOB overrides it
674 //
675 if ((VariableStoreList[VariableStoreTypeHob] != NULL) && (VariableStoreList[VariableStoreTypeNv] != NULL) &&
676 (Variable.StartPtr == GetStartPointer (VariableStoreList[VariableStoreTypeNv]))
677 )
678 {
679 VariableInHob.StartPtr = GetStartPointer (VariableStoreList[VariableStoreTypeHob]);
680 VariableInHob.EndPtr = GetEndPointer (VariableStoreList[VariableStoreTypeHob]);
681 Status = FindVariableEx (
682 GetVariableNamePtr (Variable.CurrPtr, AuthFormat),
683 GetVendorGuidPtr (Variable.CurrPtr, AuthFormat),
684 FALSE,
685 &VariableInHob,
686 AuthFormat
687 );
688 if (!EFI_ERROR (Status)) {
689 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
690 continue;
691 }
692 }
693
694 *VariablePtr = Variable.CurrPtr;
695 Status = EFI_SUCCESS;
696 goto Done;
697 }
698 }
699
700 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
701 }
702
703 Done:
704 return Status;
705 }
706
707 /**
708 Routine used to track statistical information about variable usage.
709 The data is stored in the EFI system table so it can be accessed later.
710 VariableInfo.efi can dump out the table. Only Boot Services variable
711 accesses are tracked by this code. The PcdVariableCollectStatistics
712 build flag controls if this feature is enabled.
713
714 A read that hits in the cache will have Read and Cache true for
715 the transaction. Data is allocated by this routine, but never
716 freed.
717
718 @param[in] VariableName Name of the Variable to track.
719 @param[in] VendorGuid Guid of the Variable to track.
720 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
721 @param[in] Read TRUE if GetVariable() was called.
722 @param[in] Write TRUE if SetVariable() was called.
723 @param[in] Delete TRUE if deleted via SetVariable().
724 @param[in] Cache TRUE for a cache hit.
725 @param[in,out] VariableInfo Pointer to a pointer of VARIABLE_INFO_ENTRY structures.
726
727 **/
728 VOID
729 UpdateVariableInfo (
730 IN CHAR16 *VariableName,
731 IN EFI_GUID *VendorGuid,
732 IN BOOLEAN Volatile,
733 IN BOOLEAN Read,
734 IN BOOLEAN Write,
735 IN BOOLEAN Delete,
736 IN BOOLEAN Cache,
737 IN OUT VARIABLE_INFO_ENTRY **VariableInfo
738 )
739 {
740 VARIABLE_INFO_ENTRY *Entry;
741
742 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
743 if ((VariableName == NULL) || (VendorGuid == NULL) || (VariableInfo == NULL)) {
744 return;
745 }
746
747 if (AtRuntime ()) {
748 // Don't collect statistics at runtime.
749 return;
750 }
751
752 if (*VariableInfo == NULL) {
753 //
754 // On the first call allocate a entry and place a pointer to it in
755 // the EFI System Table.
756 //
757 *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
758 ASSERT (*VariableInfo != NULL);
759
760 CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
761 (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
762 ASSERT ((*VariableInfo)->Name != NULL);
763 StrCpyS ((*VariableInfo)->Name, StrSize (VariableName)/sizeof (CHAR16), VariableName);
764 (*VariableInfo)->Volatile = Volatile;
765 }
766
767 for (Entry = (*VariableInfo); Entry != NULL; Entry = Entry->Next) {
768 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
769 if (StrCmp (VariableName, Entry->Name) == 0) {
770 if (Read) {
771 Entry->ReadCount++;
772 }
773
774 if (Write) {
775 Entry->WriteCount++;
776 }
777
778 if (Delete) {
779 Entry->DeleteCount++;
780 }
781
782 if (Cache) {
783 Entry->CacheCount++;
784 }
785
786 return;
787 }
788 }
789
790 if (Entry->Next == NULL) {
791 //
792 // If the entry is not in the table add it.
793 // Next iteration of the loop will fill in the data.
794 //
795 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
796 ASSERT (Entry->Next != NULL);
797
798 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
799 Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
800 ASSERT (Entry->Next->Name != NULL);
801 StrCpyS (Entry->Next->Name, StrSize (VariableName)/sizeof (CHAR16), VariableName);
802 Entry->Next->Volatile = Volatile;
803 }
804 }
805 }
806 }