]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/Pei/Variable.c
SecurityPkg/VariableAuthenticated: Check if there is a NV Variable Storage header...
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / Pei / Variable.c
1 /** @file
2 Implement ReadOnly Variable Services required by PEIM and install PEI
3 ReadOnly Varaiable2 PPI. These services operates the non-volatile
4 storage space.
5
6 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17
18 #include "Variable.h"
19
20 //
21 // Module globals
22 //
23 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi = {
24 PeiGetVariable,
25 PeiGetNextVariableName
26 };
27
28 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable = {
29 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
30 &gEfiPeiReadOnlyVariable2PpiGuid,
31 &mVariablePpi
32 };
33
34 /**
35 Provide the functionality of the variable services.
36
37 @param FileHandle Handle of the file being invoked.
38 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
39 @param PeiServices General purpose services available to every PEIM.
40
41 @retval EFI_SUCCESS If the interface could be successfully installed
42 @retval Others Returned from PeiServicesInstallPpi()
43 **/
44 EFI_STATUS
45 EFIAPI
46 PeimInitializeVariableServices (
47 IN EFI_PEI_FILE_HANDLE FileHandle,
48 IN CONST EFI_PEI_SERVICES **PeiServices
49 )
50 {
51 return PeiServicesInstallPpi (&mPpiListVariable);
52 }
53
54 /**
55
56 Gets the pointer to the first variable header in given variable store area.
57
58 @param VarStoreHeader Pointer to the Variable Store Header.
59
60 @return Pointer to the first variable header
61
62 **/
63 VARIABLE_HEADER *
64 GetStartPointer (
65 IN VARIABLE_STORE_HEADER *VarStoreHeader
66 )
67 {
68 //
69 // The end of variable store
70 //
71 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
72 }
73
74
75 /**
76 This code gets the pointer to the last variable memory pointer byte.
77
78 @param VarStoreHeader Pointer to the Variable Store Header.
79
80 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
81
82 **/
83 VARIABLE_HEADER *
84 GetEndPointer (
85 IN VARIABLE_STORE_HEADER *VarStoreHeader
86 )
87 {
88 //
89 // The end of variable store
90 //
91 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
92 }
93
94
95 /**
96 This code checks if variable header is valid or not.
97
98 @param Variable Pointer to the Variable Header.
99
100 @retval TRUE Variable header is valid.
101 @retval FALSE Variable header is not valid.
102
103 **/
104 BOOLEAN
105 IsValidVariableHeader (
106 IN VARIABLE_HEADER *Variable
107 )
108 {
109 if (Variable == NULL || Variable->StartId != VARIABLE_DATA ) {
110 return FALSE;
111 }
112
113 return TRUE;
114 }
115
116
117 /**
118 This code gets the size of name of variable.
119
120 @param Variable Pointer to the Variable Header.
121
122 @return Size of variable in bytes in type UINTN.
123
124 **/
125 UINTN
126 NameSizeOfVariable (
127 IN VARIABLE_HEADER *Variable
128 )
129 {
130 if (Variable->State == (UINT8) (-1) ||
131 Variable->DataSize == (UINT32) (-1) ||
132 Variable->NameSize == (UINT32) (-1) ||
133 Variable->Attributes == (UINT32) (-1)) {
134 return 0;
135 }
136 return (UINTN) Variable->NameSize;
137 }
138
139
140 /**
141 This code gets the size of data of variable.
142
143 @param Variable Pointer to the Variable Header.
144
145 @return Size of variable in bytes in type UINTN.
146
147 **/
148 UINTN
149 DataSizeOfVariable (
150 IN VARIABLE_HEADER *Variable
151 )
152 {
153 if (Variable->State == (UINT8) (-1) ||
154 Variable->DataSize == (UINT32) (-1) ||
155 Variable->NameSize == (UINT32) (-1) ||
156 Variable->Attributes == (UINT32) (-1)) {
157 return 0;
158 }
159 return (UINTN) Variable->DataSize;
160 }
161
162 /**
163 This code gets the pointer to the variable name.
164
165 @param Variable Pointer to the Variable Header.
166
167 @return A CHAR16* pointer to Variable Name.
168
169 **/
170 CHAR16 *
171 GetVariableNamePtr (
172 IN VARIABLE_HEADER *Variable
173 )
174 {
175
176 return (CHAR16 *) (Variable + 1);
177 }
178
179
180 /**
181 This code gets the pointer to the variable data.
182
183 @param Variable Pointer to the Variable Header.
184
185 @return A UINT8* pointer to Variable Data.
186
187 **/
188 UINT8 *
189 GetVariableDataPtr (
190 IN VARIABLE_HEADER *Variable
191 )
192 {
193 UINTN Value;
194
195 //
196 // Be careful about pad size for alignment
197 //
198 Value = (UINTN) GetVariableNamePtr (Variable);
199 Value += NameSizeOfVariable (Variable);
200 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
201
202 return (UINT8 *) Value;
203 }
204
205
206 /**
207 This code gets the pointer to the next variable header.
208
209 @param Variable Pointer to the Variable Header.
210
211 @return A VARIABLE_HEADER* pointer to next variable header.
212
213 **/
214 VARIABLE_HEADER *
215 GetNextVariablePtr (
216 IN VARIABLE_HEADER *Variable
217 )
218 {
219 UINTN Value;
220
221 if (!IsValidVariableHeader (Variable)) {
222 return NULL;
223 }
224
225 Value = (UINTN) GetVariableDataPtr (Variable);
226 Value += DataSizeOfVariable (Variable);
227 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
228
229 //
230 // Be careful about pad size for alignment
231 //
232 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
233 }
234
235 /**
236 This code gets the pointer to the variable name.
237
238 @param VarStoreHeader Pointer to the Variable Store Header.
239
240 @retval EfiRaw Variable store is raw
241 @retval EfiValid Variable store is valid
242 @retval EfiInvalid Variable store is invalid
243
244 **/
245 VARIABLE_STORE_STATUS
246 GetVariableStoreStatus (
247 IN VARIABLE_STORE_HEADER *VarStoreHeader
248 )
249 {
250
251 if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&
252 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
253 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
254 ) {
255
256 return EfiValid;
257 }
258
259 if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
260 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
261 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
262 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
263 VarStoreHeader->Size == 0xffffffff &&
264 VarStoreHeader->Format == 0xff &&
265 VarStoreHeader->State == 0xff
266 ) {
267
268 return EfiRaw;
269 } else {
270 return EfiInvalid;
271 }
272 }
273
274
275 /**
276 This function compares a variable with variable entries in database.
277
278 @param Variable Pointer to the variable in our database
279 @param VariableName Name of the variable to compare to 'Variable'
280 @param VendorGuid GUID of the variable to compare to 'Variable'
281 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
282
283 @retval EFI_SUCCESS Found match variable
284 @retval EFI_NOT_FOUND Variable not found
285
286 **/
287 EFI_STATUS
288 CompareWithValidVariable (
289 IN VARIABLE_HEADER *Variable,
290 IN CONST CHAR16 *VariableName,
291 IN CONST EFI_GUID *VendorGuid,
292 OUT VARIABLE_POINTER_TRACK *PtrTrack
293 )
294 {
295 VOID *Point;
296
297 if (VariableName[0] == 0) {
298 PtrTrack->CurrPtr = Variable;
299 return EFI_SUCCESS;
300 } else {
301 //
302 // Don't use CompareGuid function here for performance reasons.
303 // Instead we compare the GUID a UINT32 at a time and branch
304 // on the first failed comparison.
305 //
306 if ((((INT32 *) VendorGuid)[0] == ((INT32 *) &Variable->VendorGuid)[0]) &&
307 (((INT32 *) VendorGuid)[1] == ((INT32 *) &Variable->VendorGuid)[1]) &&
308 (((INT32 *) VendorGuid)[2] == ((INT32 *) &Variable->VendorGuid)[2]) &&
309 (((INT32 *) VendorGuid)[3] == ((INT32 *) &Variable->VendorGuid)[3])
310 ) {
311 ASSERT (NameSizeOfVariable (Variable) != 0);
312 Point = (VOID *) GetVariableNamePtr (Variable);
313 if (CompareMem (VariableName, Point, NameSizeOfVariable (Variable)) == 0) {
314 PtrTrack->CurrPtr = Variable;
315 return EFI_SUCCESS;
316 }
317 }
318 }
319
320 return EFI_NOT_FOUND;
321 }
322
323 /**
324 Return the variable store header and the index table based on the Index.
325
326 @param Type The type of the variable store.
327 @param IndexTable Return the index table.
328
329 @return Pointer to the variable store header.
330 **/
331 VARIABLE_STORE_HEADER *
332 GetVariableStore (
333 IN VARIABLE_STORE_TYPE Type,
334 OUT VARIABLE_INDEX_TABLE **IndexTable OPTIONAL
335 )
336 {
337 EFI_HOB_GUID_TYPE *GuidHob;
338 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
339 VARIABLE_STORE_HEADER *VariableStoreHeader;
340
341 if (IndexTable != NULL) {
342 *IndexTable = NULL;
343 }
344 VariableStoreHeader = NULL;
345 switch (Type) {
346 case VariableStoreTypeHob:
347 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);
348 if (GuidHob != NULL) {
349 VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob);
350 }
351 break;
352
353 case VariableStoreTypeNv:
354 if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE) {
355 //
356 // The content of NV storage for variable is not reliable in recovery boot mode.
357 //
358 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ?
359 PcdGet64 (PcdFlashNvStorageVariableBase64) :
360 PcdGet32 (PcdFlashNvStorageVariableBase)
361 );
362
363 //
364 // Check if the Firmware Volume is not corrupted
365 //
366 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {
367 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));
368 break;
369 }
370
371 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength);
372
373 if (IndexTable != NULL) {
374 GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);
375 if (GuidHob != NULL) {
376 *IndexTable = GET_GUID_HOB_DATA (GuidHob);
377 } else {
378 //
379 // If it's the first time to access variable region in flash, create a guid hob to record
380 // VAR_ADDED type variable info.
381 // Note that as the resource of PEI phase is limited, only store the limited number of
382 // VAR_ADDED type variables to reduce access time.
383 //
384 *IndexTable = BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));
385 (*IndexTable)->Length = 0;
386 (*IndexTable)->StartPtr = GetStartPointer (VariableStoreHeader);
387 (*IndexTable)->EndPtr = GetEndPointer (VariableStoreHeader);
388 (*IndexTable)->GoneThrough = 0;
389 }
390 }
391 }
392 break;
393
394 default:
395 ASSERT (FALSE);
396 break;
397 }
398
399 return VariableStoreHeader;
400 }
401
402 /**
403 Find the variable in the specified variable store.
404
405 @param VariableStoreHeader Pointer to the variable store header.
406 @param IndexTable Pointer to the index table.
407 @param VariableName Name of the variable to be found
408 @param VendorGuid Vendor GUID to be found.
409 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
410
411 @retval EFI_SUCCESS Variable found successfully
412 @retval EFI_NOT_FOUND Variable not found
413 @retval EFI_INVALID_PARAMETER Invalid variable name
414
415 **/
416 EFI_STATUS
417 FindVariableEx (
418 IN VARIABLE_STORE_HEADER *VariableStoreHeader,
419 IN VARIABLE_INDEX_TABLE *IndexTable,
420 IN CONST CHAR16 *VariableName,
421 IN CONST EFI_GUID *VendorGuid,
422 OUT VARIABLE_POINTER_TRACK *PtrTrack
423 )
424 {
425 VARIABLE_HEADER *Variable;
426 VARIABLE_HEADER *LastVariable;
427 VARIABLE_HEADER *MaxIndex;
428 UINTN Index;
429 UINTN Offset;
430 BOOLEAN StopRecord;
431
432 if (VariableStoreHeader == NULL) {
433 return EFI_INVALID_PARAMETER;
434 }
435
436 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
437 return EFI_UNSUPPORTED;
438 }
439
440 if (~VariableStoreHeader->Size == 0) {
441 return EFI_NOT_FOUND;
442 }
443
444 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);
445 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);
446
447 //
448 // No Variable Address equals zero, so 0 as initial value is safe.
449 //
450 MaxIndex = NULL;
451
452 if (IndexTable != NULL) {
453 //
454 // traverse the variable index table to look for varible.
455 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
456 //
457 for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {
458 ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));
459 Offset += IndexTable->Index[Index];
460 MaxIndex = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);
461 if (CompareWithValidVariable (MaxIndex, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
462 return EFI_SUCCESS;
463 }
464 }
465
466 if (IndexTable->GoneThrough != 0) {
467 //
468 // If the table has all the existing variables indexed and we still cannot find it.
469 //
470 return EFI_NOT_FOUND;
471 }
472 }
473
474 if (MaxIndex != NULL) {
475 //
476 // HOB exists but the variable cannot be found in HOB
477 // If not found in HOB, then let's start from the MaxIndex we've found.
478 //
479 Variable = GetNextVariablePtr (MaxIndex);
480 LastVariable = MaxIndex;
481 } else {
482 //
483 // Start Pointers for the variable.
484 // Actual Data Pointer where data can be written.
485 //
486 Variable = PtrTrack->StartPtr;
487 LastVariable = PtrTrack->StartPtr;
488 }
489
490 //
491 // Find the variable by walk through non-volatile variable store
492 //
493 StopRecord = FALSE;
494 while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {
495 if (Variable->State == VAR_ADDED) {
496 //
497 // Record Variable in VariableIndex HOB
498 //
499 if ((IndexTable != NULL) && !StopRecord) {
500 Offset = (UINTN) Variable - (UINTN) LastVariable;
501 if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {
502 //
503 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
504 // or the record buffer is full.
505 //
506 StopRecord = TRUE;
507 } else {
508 IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;
509 LastVariable = Variable;
510 }
511 }
512
513 if (CompareWithValidVariable (Variable, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
514 return EFI_SUCCESS;
515 }
516 }
517
518 Variable = GetNextVariablePtr (Variable);
519 }
520 //
521 // If gone through the VariableStore, that means we never find in Firmware any more.
522 //
523 if ((IndexTable != NULL) && !StopRecord) {
524 IndexTable->GoneThrough = 1;
525 }
526
527 PtrTrack->CurrPtr = NULL;
528
529 return EFI_NOT_FOUND;
530 }
531
532 /**
533 Find the variable in HOB and Non-Volatile variable storages.
534
535 @param VariableName Name of the variable to be found
536 @param VendorGuid Vendor GUID to be found.
537 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
538
539 @retval EFI_SUCCESS Variable found successfully
540 @retval EFI_NOT_FOUND Variable not found
541 @retval EFI_INVALID_PARAMETER Invalid variable name
542 **/
543 EFI_STATUS
544 FindVariable (
545 IN CONST CHAR16 *VariableName,
546 IN CONST EFI_GUID *VendorGuid,
547 OUT VARIABLE_POINTER_TRACK *PtrTrack
548 )
549 {
550 EFI_STATUS Status;
551 VARIABLE_STORE_HEADER *VariableStoreHeader;
552 VARIABLE_INDEX_TABLE *IndexTable;
553 VARIABLE_STORE_TYPE Type;
554
555 if (VariableName[0] != 0 && VendorGuid == NULL) {
556 return EFI_INVALID_PARAMETER;
557 }
558
559 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
560 VariableStoreHeader = GetVariableStore (Type, &IndexTable);
561 Status = FindVariableEx (
562 VariableStoreHeader,
563 IndexTable,
564 VariableName,
565 VendorGuid,
566 PtrTrack
567 );
568 if (!EFI_ERROR (Status)) {
569 return Status;
570 }
571 }
572
573 return EFI_NOT_FOUND;
574 }
575
576 /**
577 This service retrieves a variable's value using its name and GUID.
578
579 Read the specified variable from the UEFI variable store. If the Data
580 buffer is too small to hold the contents of the variable, the error
581 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
582 size to obtain the data.
583
584 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
585 @param VariableName A pointer to a null-terminated string that is the variable's name.
586 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
587 VariableGuid and VariableName must be unique.
588 @param Attributes If non-NULL, on return, points to the variable's attributes.
589 @param DataSize On entry, points to the size in bytes of the Data buffer.
590 On return, points to the size of the data returned in Data.
591 @param Data Points to the buffer which will hold the returned variable value.
592
593 @retval EFI_SUCCESS The variable was read successfully.
594 @retval EFI_NOT_FOUND The variable could not be found.
595 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
596 DataSize is updated with the size required for
597 the specified variable.
598 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
599 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
600
601 **/
602 EFI_STATUS
603 EFIAPI
604 PeiGetVariable (
605 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
606 IN CONST CHAR16 *VariableName,
607 IN CONST EFI_GUID *VariableGuid,
608 OUT UINT32 *Attributes,
609 IN OUT UINTN *DataSize,
610 OUT VOID *Data
611 )
612 {
613 VARIABLE_POINTER_TRACK Variable;
614 UINTN VarDataSize;
615 EFI_STATUS Status;
616
617 if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {
618 return EFI_INVALID_PARAMETER;
619 }
620
621 //
622 // Find existing variable
623 //
624 Status = FindVariable (VariableName, VariableGuid, &Variable);
625 if (EFI_ERROR (Status)) {
626 return Status;
627 }
628 //
629 // Get data size
630 //
631 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
632 if (*DataSize >= VarDataSize) {
633 if (Data == NULL) {
634 return EFI_INVALID_PARAMETER;
635 }
636
637 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
638
639 if (Attributes != NULL) {
640 *Attributes = Variable.CurrPtr->Attributes;
641 }
642
643 *DataSize = VarDataSize;
644 return EFI_SUCCESS;
645 } else {
646 *DataSize = VarDataSize;
647 return EFI_BUFFER_TOO_SMALL;
648 }
649 }
650
651 /**
652 Return the next variable name and GUID.
653
654 This function is called multiple times to retrieve the VariableName
655 and VariableGuid of all variables currently available in the system.
656 On each call, the previous results are passed into the interface,
657 and, on return, the interface returns the data for the next
658 interface. When the entire variable list has been returned,
659 EFI_NOT_FOUND is returned.
660
661 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
662
663 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
664 On return, the size of the variable name buffer.
665 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
666 On return, points to the next variable's null-terminated name string.
667 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
668 On return, a pointer to the next variable's GUID.
669
670 @retval EFI_SUCCESS The variable was read successfully.
671 @retval EFI_NOT_FOUND The variable could not be found.
672 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
673 data. VariableNameSize is updated with the size
674 required for the specified variable.
675 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
676 VariableNameSize is NULL.
677 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
678
679 **/
680 EFI_STATUS
681 EFIAPI
682 PeiGetNextVariableName (
683 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
684 IN OUT UINTN *VariableNameSize,
685 IN OUT CHAR16 *VariableName,
686 IN OUT EFI_GUID *VariableGuid
687 )
688 {
689 VARIABLE_STORE_TYPE Type;
690 VARIABLE_POINTER_TRACK Variable;
691 VARIABLE_POINTER_TRACK VariableInHob;
692 UINTN VarNameSize;
693 EFI_STATUS Status;
694 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
695
696 if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {
697 return EFI_INVALID_PARAMETER;
698 }
699
700 Status = FindVariable (VariableName, VariableGuid, &Variable);
701 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {
702 return Status;
703 }
704
705 if (VariableName[0] != 0) {
706 //
707 // If variable name is not NULL, get next variable
708 //
709 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
710 }
711
712 VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, NULL);
713 VariableStoreHeader[VariableStoreTypeNv] = GetVariableStore (VariableStoreTypeNv, NULL);
714
715 while (TRUE) {
716 //
717 // Switch from HOB to Non-Volatile.
718 //
719 while ((Variable.CurrPtr >= Variable.EndPtr) ||
720 (Variable.CurrPtr == NULL) ||
721 !IsValidVariableHeader (Variable.CurrPtr)
722 ) {
723 //
724 // Find current storage index
725 //
726 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
727 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
728 break;
729 }
730 }
731 ASSERT (Type < VariableStoreTypeMax);
732 //
733 // Switch to next storage
734 //
735 for (Type++; Type < VariableStoreTypeMax; Type++) {
736 if (VariableStoreHeader[Type] != NULL) {
737 break;
738 }
739 }
740 //
741 // Capture the case that
742 // 1. current storage is the last one, or
743 // 2. no further storage
744 //
745 if (Type == VariableStoreTypeMax) {
746 return EFI_NOT_FOUND;
747 }
748 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
749 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
750 Variable.CurrPtr = Variable.StartPtr;
751 }
752
753 if (Variable.CurrPtr->State == VAR_ADDED) {
754
755 //
756 // Don't return NV variable when HOB overrides it
757 //
758 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
759 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
760 ) {
761 Status = FindVariableEx (
762 VariableStoreHeader[VariableStoreTypeHob],
763 NULL,
764 GetVariableNamePtr (Variable.CurrPtr),
765 &Variable.CurrPtr->VendorGuid,
766 &VariableInHob
767 );
768 if (!EFI_ERROR (Status)) {
769 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
770 continue;
771 }
772 }
773
774 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
775 ASSERT (VarNameSize != 0);
776
777 if (VarNameSize <= *VariableNameSize) {
778 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);
779
780 CopyMem (VariableGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));
781
782 Status = EFI_SUCCESS;
783 } else {
784 Status = EFI_BUFFER_TOO_SMALL;
785 }
786
787 *VariableNameSize = VarNameSize;
788 //
789 // Variable is found
790 //
791 return Status;
792 } else {
793 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
794 }
795 }
796 }