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