]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/Pei/Variable.c
SecurityPkg: Update VariableAuthenticated driver with following changes:
[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 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength);
363
364 if (IndexTable != NULL) {
365 GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);
366 if (GuidHob != NULL) {
367 *IndexTable = GET_GUID_HOB_DATA (GuidHob);
368 } else {
369 //
370 // If it's the first time to access variable region in flash, create a guid hob to record
371 // VAR_ADDED type variable info.
372 // Note that as the resource of PEI phase is limited, only store the limited number of
373 // VAR_ADDED type variables to reduce access time.
374 //
375 *IndexTable = BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));
376 (*IndexTable)->Length = 0;
377 (*IndexTable)->StartPtr = GetStartPointer (VariableStoreHeader);
378 (*IndexTable)->EndPtr = GetEndPointer (VariableStoreHeader);
379 (*IndexTable)->GoneThrough = 0;
380 }
381 }
382 }
383 break;
384
385 default:
386 ASSERT (FALSE);
387 break;
388 }
389
390 return VariableStoreHeader;
391 }
392
393 /**
394 Find the variable in the specified variable store.
395
396 @param VariableStoreHeader Pointer to the variable store header.
397 @param IndexTable Pointer to the index table.
398 @param VariableName Name of the variable to be found
399 @param VendorGuid Vendor GUID to be found.
400 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
401
402 @retval EFI_SUCCESS Variable found successfully
403 @retval EFI_NOT_FOUND Variable not found
404 @retval EFI_INVALID_PARAMETER Invalid variable name
405
406 **/
407 EFI_STATUS
408 FindVariableEx (
409 IN VARIABLE_STORE_HEADER *VariableStoreHeader,
410 IN VARIABLE_INDEX_TABLE *IndexTable,
411 IN CONST CHAR16 *VariableName,
412 IN CONST EFI_GUID *VendorGuid,
413 OUT VARIABLE_POINTER_TRACK *PtrTrack
414 )
415 {
416 VARIABLE_HEADER *Variable;
417 VARIABLE_HEADER *LastVariable;
418 VARIABLE_HEADER *MaxIndex;
419 UINTN Index;
420 UINTN Offset;
421 BOOLEAN StopRecord;
422
423 if (VariableStoreHeader == NULL) {
424 return EFI_INVALID_PARAMETER;
425 }
426
427 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
428 return EFI_UNSUPPORTED;
429 }
430
431 if (~VariableStoreHeader->Size == 0) {
432 return EFI_NOT_FOUND;
433 }
434
435 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);
436 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);
437
438 //
439 // No Variable Address equals zero, so 0 as initial value is safe.
440 //
441 MaxIndex = NULL;
442
443 if (IndexTable != NULL) {
444 //
445 // traverse the variable index table to look for varible.
446 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
447 //
448 for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {
449 ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));
450 Offset += IndexTable->Index[Index];
451 MaxIndex = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);
452 if (CompareWithValidVariable (MaxIndex, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
453 return EFI_SUCCESS;
454 }
455 }
456
457 if (IndexTable->GoneThrough != 0) {
458 //
459 // If the table has all the existing variables indexed and we still cannot find it.
460 //
461 return EFI_NOT_FOUND;
462 }
463 }
464
465 if (MaxIndex != NULL) {
466 //
467 // HOB exists but the variable cannot be found in HOB
468 // If not found in HOB, then let's start from the MaxIndex we've found.
469 //
470 Variable = GetNextVariablePtr (MaxIndex);
471 LastVariable = MaxIndex;
472 } else {
473 //
474 // Start Pointers for the variable.
475 // Actual Data Pointer where data can be written.
476 //
477 Variable = PtrTrack->StartPtr;
478 LastVariable = PtrTrack->StartPtr;
479 }
480
481 //
482 // Find the variable by walk through non-volatile variable store
483 //
484 StopRecord = FALSE;
485 while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {
486 if (Variable->State == VAR_ADDED) {
487 //
488 // Record Variable in VariableIndex HOB
489 //
490 if ((IndexTable != NULL) && !StopRecord) {
491 Offset = (UINTN) Variable - (UINTN) LastVariable;
492 if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {
493 //
494 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
495 // or the record buffer is full.
496 //
497 StopRecord = TRUE;
498 } else {
499 IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;
500 LastVariable = Variable;
501 }
502 }
503
504 if (CompareWithValidVariable (Variable, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
505 return EFI_SUCCESS;
506 }
507 }
508
509 Variable = GetNextVariablePtr (Variable);
510 }
511 //
512 // If gone through the VariableStore, that means we never find in Firmware any more.
513 //
514 if ((IndexTable != NULL) && !StopRecord) {
515 IndexTable->GoneThrough = 1;
516 }
517
518 PtrTrack->CurrPtr = NULL;
519
520 return EFI_NOT_FOUND;
521 }
522
523 /**
524 Find the variable in HOB and Non-Volatile variable storages.
525
526 @param VariableName Name of the variable to be found
527 @param VendorGuid Vendor GUID to be found.
528 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
529
530 @retval EFI_SUCCESS Variable found successfully
531 @retval EFI_NOT_FOUND Variable not found
532 @retval EFI_INVALID_PARAMETER Invalid variable name
533 **/
534 EFI_STATUS
535 FindVariable (
536 IN CONST CHAR16 *VariableName,
537 IN CONST EFI_GUID *VendorGuid,
538 OUT VARIABLE_POINTER_TRACK *PtrTrack
539 )
540 {
541 EFI_STATUS Status;
542 VARIABLE_STORE_HEADER *VariableStoreHeader;
543 VARIABLE_INDEX_TABLE *IndexTable;
544 VARIABLE_STORE_TYPE Type;
545
546 if (VariableName[0] != 0 && VendorGuid == NULL) {
547 return EFI_INVALID_PARAMETER;
548 }
549
550 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
551 VariableStoreHeader = GetVariableStore (Type, &IndexTable);
552 Status = FindVariableEx (
553 VariableStoreHeader,
554 IndexTable,
555 VariableName,
556 VendorGuid,
557 PtrTrack
558 );
559 if (!EFI_ERROR (Status)) {
560 return Status;
561 }
562 }
563
564 return EFI_NOT_FOUND;
565 }
566
567 /**
568 This service retrieves a variable's value using its name and GUID.
569
570 Read the specified variable from the UEFI variable store. If the Data
571 buffer is too small to hold the contents of the variable, the error
572 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
573 size to obtain the data.
574
575 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
576 @param VariableName A pointer to a null-terminated string that is the variable's name.
577 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
578 VariableGuid and VariableName must be unique.
579 @param Attributes If non-NULL, on return, points to the variable's attributes.
580 @param DataSize On entry, points to the size in bytes of the Data buffer.
581 On return, points to the size of the data returned in Data.
582 @param Data Points to the buffer which will hold the returned variable value.
583
584 @retval EFI_SUCCESS The variable was read successfully.
585 @retval EFI_NOT_FOUND The variable could not be found.
586 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
587 DataSize is updated with the size required for
588 the specified variable.
589 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
590 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
591
592 **/
593 EFI_STATUS
594 EFIAPI
595 PeiGetVariable (
596 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
597 IN CONST CHAR16 *VariableName,
598 IN CONST EFI_GUID *VariableGuid,
599 OUT UINT32 *Attributes,
600 IN OUT UINTN *DataSize,
601 OUT VOID *Data
602 )
603 {
604 VARIABLE_POINTER_TRACK Variable;
605 UINTN VarDataSize;
606 EFI_STATUS Status;
607
608 if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {
609 return EFI_INVALID_PARAMETER;
610 }
611
612 //
613 // Find existing variable
614 //
615 Status = FindVariable (VariableName, VariableGuid, &Variable);
616 if (EFI_ERROR (Status)) {
617 return Status;
618 }
619 //
620 // Get data size
621 //
622 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
623 if (*DataSize >= VarDataSize) {
624 if (Data == NULL) {
625 return EFI_INVALID_PARAMETER;
626 }
627
628 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
629
630 if (Attributes != NULL) {
631 *Attributes = Variable.CurrPtr->Attributes;
632 }
633
634 *DataSize = VarDataSize;
635 return EFI_SUCCESS;
636 } else {
637 *DataSize = VarDataSize;
638 return EFI_BUFFER_TOO_SMALL;
639 }
640 }
641
642 /**
643 Return the next variable name and GUID.
644
645 This function is called multiple times to retrieve the VariableName
646 and VariableGuid of all variables currently available in the system.
647 On each call, the previous results are passed into the interface,
648 and, on return, the interface returns the data for the next
649 interface. When the entire variable list has been returned,
650 EFI_NOT_FOUND is returned.
651
652 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
653
654 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
655 On return, the size of the variable name buffer.
656 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
657 On return, points to the next variable's null-terminated name string.
658 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
659 On return, a pointer to the next variable's GUID.
660
661 @retval EFI_SUCCESS The variable was read successfully.
662 @retval EFI_NOT_FOUND The variable could not be found.
663 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
664 data. VariableNameSize is updated with the size
665 required for the specified variable.
666 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
667 VariableNameSize is NULL.
668 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
669
670 **/
671 EFI_STATUS
672 EFIAPI
673 PeiGetNextVariableName (
674 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
675 IN OUT UINTN *VariableNameSize,
676 IN OUT CHAR16 *VariableName,
677 IN OUT EFI_GUID *VariableGuid
678 )
679 {
680 VARIABLE_STORE_TYPE Type;
681 VARIABLE_POINTER_TRACK Variable;
682 VARIABLE_POINTER_TRACK VariableInHob;
683 UINTN VarNameSize;
684 EFI_STATUS Status;
685 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
686
687 if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {
688 return EFI_INVALID_PARAMETER;
689 }
690
691 Status = FindVariable (VariableName, VariableGuid, &Variable);
692 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {
693 return Status;
694 }
695
696 if (VariableName[0] != 0) {
697 //
698 // If variable name is not NULL, get next variable
699 //
700 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
701 }
702
703 VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, NULL);
704 VariableStoreHeader[VariableStoreTypeNv] = GetVariableStore (VariableStoreTypeNv, NULL);
705
706 while (TRUE) {
707 //
708 // Switch from HOB to Non-Volatile.
709 //
710 while ((Variable.CurrPtr >= Variable.EndPtr) ||
711 (Variable.CurrPtr == NULL) ||
712 !IsValidVariableHeader (Variable.CurrPtr)
713 ) {
714 //
715 // Find current storage index
716 //
717 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
718 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
719 break;
720 }
721 }
722 ASSERT (Type < VariableStoreTypeMax);
723 //
724 // Switch to next storage
725 //
726 for (Type++; Type < VariableStoreTypeMax; Type++) {
727 if (VariableStoreHeader[Type] != NULL) {
728 break;
729 }
730 }
731 //
732 // Capture the case that
733 // 1. current storage is the last one, or
734 // 2. no further storage
735 //
736 if (Type == VariableStoreTypeMax) {
737 return EFI_NOT_FOUND;
738 }
739 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
740 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
741 Variable.CurrPtr = Variable.StartPtr;
742 }
743
744 if (Variable.CurrPtr->State == VAR_ADDED) {
745
746 //
747 // Don't return NV variable when HOB overrides it
748 //
749 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
750 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
751 ) {
752 Status = FindVariableEx (
753 VariableStoreHeader[VariableStoreTypeHob],
754 NULL,
755 GetVariableNamePtr (Variable.CurrPtr),
756 &Variable.CurrPtr->VendorGuid,
757 &VariableInHob
758 );
759 if (!EFI_ERROR (Status)) {
760 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
761 continue;
762 }
763 }
764
765 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
766 ASSERT (VarNameSize != 0);
767
768 if (VarNameSize <= *VariableNameSize) {
769 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);
770
771 CopyMem (VariableGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));
772
773 Status = EFI_SUCCESS;
774 } else {
775 Status = EFI_BUFFER_TOO_SMALL;
776 }
777
778 *VariableNameSize = VarNameSize;
779 //
780 // Variable is found
781 //
782 return Status;
783 } else {
784 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
785 }
786 }
787 }