]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
d458f1c608f1e978a1dbf361246a52ce83829554
[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 // Check if the variable exists in the given variable store list
555 for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTypeMax; StoreType++) {
556 if (VariableStoreList[StoreType] == NULL) {
557 continue;
558 }
559
560 Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
561 Variable.EndPtr = GetEndPointer (VariableStoreList[StoreType]);
562 Variable.Volatile = (BOOLEAN) (StoreType == VariableStoreTypeVolatile);
563
564 Status = FindVariableEx (VariableName, VendorGuid, FALSE, &Variable, AuthFormat);
565 if (!EFI_ERROR (Status)) {
566 break;
567 }
568 }
569
570 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
571 //
572 // For VariableName is an empty string, FindVariableEx() will try to find and return
573 // the first qualified variable, and if FindVariableEx() returns error (EFI_NOT_FOUND)
574 // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
575 //
576 if (VariableName[0] != 0) {
577 //
578 // For VariableName is not an empty string, and FindVariableEx() returns error as
579 // VariableName and VendorGuid are not a name and GUID of an existing variable,
580 // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
581 //
582 Status = EFI_INVALID_PARAMETER;
583 }
584 goto Done;
585 }
586
587 if (VariableName[0] != 0) {
588 //
589 // If variable name is not empty, get next variable.
590 //
591 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
592 }
593
594 while (TRUE) {
595 //
596 // Switch to the next variable store if needed
597 //
598 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
599 //
600 // Find current storage index
601 //
602 for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTypeMax; StoreType++) {
603 if ((VariableStoreList[StoreType] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreList[StoreType]))) {
604 break;
605 }
606 }
607 ASSERT (StoreType < VariableStoreTypeMax);
608 //
609 // Switch to next storage
610 //
611 for (StoreType++; StoreType < VariableStoreTypeMax; StoreType++) {
612 if (VariableStoreList[StoreType] != NULL) {
613 break;
614 }
615 }
616 //
617 // Capture the case that
618 // 1. current storage is the last one, or
619 // 2. no further storage
620 //
621 if (StoreType == VariableStoreTypeMax) {
622 Status = EFI_NOT_FOUND;
623 goto Done;
624 }
625 Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
626 Variable.EndPtr = GetEndPointer (VariableStoreList[StoreType]);
627 Variable.CurrPtr = Variable.StartPtr;
628 }
629
630 //
631 // Variable is found
632 //
633 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
634 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
635 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
636 //
637 // If it is a IN_DELETED_TRANSITION variable,
638 // and there is also a same ADDED one at the same time,
639 // don't return it.
640 //
641 VariablePtrTrack.StartPtr = Variable.StartPtr;
642 VariablePtrTrack.EndPtr = Variable.EndPtr;
643 Status = FindVariableEx (
644 GetVariableNamePtr (Variable.CurrPtr, AuthFormat),
645 GetVendorGuidPtr (Variable.CurrPtr, AuthFormat),
646 FALSE,
647 &VariablePtrTrack,
648 AuthFormat
649 );
650 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {
651 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
652 continue;
653 }
654 }
655
656 //
657 // Don't return NV variable when HOB overrides it
658 //
659 if ((VariableStoreList[VariableStoreTypeHob] != NULL) && (VariableStoreList[VariableStoreTypeNv] != NULL) &&
660 (Variable.StartPtr == GetStartPointer (VariableStoreList[VariableStoreTypeNv]))
661 ) {
662 VariableInHob.StartPtr = GetStartPointer (VariableStoreList[VariableStoreTypeHob]);
663 VariableInHob.EndPtr = GetEndPointer (VariableStoreList[VariableStoreTypeHob]);
664 Status = FindVariableEx (
665 GetVariableNamePtr (Variable.CurrPtr, AuthFormat),
666 GetVendorGuidPtr (Variable.CurrPtr, AuthFormat),
667 FALSE,
668 &VariableInHob,
669 AuthFormat
670 );
671 if (!EFI_ERROR (Status)) {
672 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
673 continue;
674 }
675 }
676
677 *VariablePtr = Variable.CurrPtr;
678 Status = EFI_SUCCESS;
679 goto Done;
680 }
681 }
682
683 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr, AuthFormat);
684 }
685
686 Done:
687 return Status;
688 }
689
690 /**
691 Routine used to track statistical information about variable usage.
692 The data is stored in the EFI system table so it can be accessed later.
693 VariableInfo.efi can dump out the table. Only Boot Services variable
694 accesses are tracked by this code. The PcdVariableCollectStatistics
695 build flag controls if this feature is enabled.
696
697 A read that hits in the cache will have Read and Cache true for
698 the transaction. Data is allocated by this routine, but never
699 freed.
700
701 @param[in] VariableName Name of the Variable to track.
702 @param[in] VendorGuid Guid of the Variable to track.
703 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
704 @param[in] Read TRUE if GetVariable() was called.
705 @param[in] Write TRUE if SetVariable() was called.
706 @param[in] Delete TRUE if deleted via SetVariable().
707 @param[in] Cache TRUE for a cache hit.
708 @param[in,out] VariableInfo Pointer to a pointer of VARIABLE_INFO_ENTRY structures.
709
710 **/
711 VOID
712 UpdateVariableInfo (
713 IN CHAR16 *VariableName,
714 IN EFI_GUID *VendorGuid,
715 IN BOOLEAN Volatile,
716 IN BOOLEAN Read,
717 IN BOOLEAN Write,
718 IN BOOLEAN Delete,
719 IN BOOLEAN Cache,
720 IN OUT VARIABLE_INFO_ENTRY **VariableInfo
721 )
722 {
723 VARIABLE_INFO_ENTRY *Entry;
724
725 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
726 if (VariableName == NULL || VendorGuid == NULL || VariableInfo == NULL) {
727 return;
728 }
729 if (AtRuntime ()) {
730 // Don't collect statistics at runtime.
731 return;
732 }
733
734 if (*VariableInfo == NULL) {
735 //
736 // On the first call allocate a entry and place a pointer to it in
737 // the EFI System Table.
738 //
739 *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
740 ASSERT (*VariableInfo != NULL);
741
742 CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
743 (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
744 ASSERT ((*VariableInfo)->Name != NULL);
745 StrCpyS ((*VariableInfo)->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
746 (*VariableInfo)->Volatile = Volatile;
747 }
748
749
750 for (Entry = (*VariableInfo); Entry != NULL; Entry = Entry->Next) {
751 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
752 if (StrCmp (VariableName, Entry->Name) == 0) {
753 if (Read) {
754 Entry->ReadCount++;
755 }
756 if (Write) {
757 Entry->WriteCount++;
758 }
759 if (Delete) {
760 Entry->DeleteCount++;
761 }
762 if (Cache) {
763 Entry->CacheCount++;
764 }
765
766 return;
767 }
768 }
769
770 if (Entry->Next == NULL) {
771 //
772 // If the entry is not in the table add it.
773 // Next iteration of the loop will fill in the data.
774 //
775 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
776 ASSERT (Entry->Next != NULL);
777
778 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
779 Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
780 ASSERT (Entry->Next->Name != NULL);
781 StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
782 Entry->Next->Volatile = Volatile;
783 }
784 }
785 }
786 }