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