]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/Pei/Variable.c
4716cc5124a6837f9d650eb8646b533f763c48a3
[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 - 2014, 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 /**
36 Provide the functionality of the variable services.
37
38 @param FileHandle Handle of the file being invoked.
39 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
40 @param PeiServices General purpose services available to every PEIM.
41
42 @retval EFI_SUCCESS If the interface could be successfully installed
43 @retval Others Returned from PeiServicesInstallPpi()
44 **/
45 EFI_STATUS
46 EFIAPI
47 PeimInitializeVariableServices (
48 IN EFI_PEI_FILE_HANDLE FileHandle,
49 IN CONST EFI_PEI_SERVICES **PeiServices
50 )
51 {
52 return PeiServicesInstallPpi (&mPpiListVariable);
53 }
54
55 /**
56
57 Gets the pointer to the first variable header in given variable store area.
58
59 @param VarStoreHeader Pointer to the Variable Store Header.
60
61 @return Pointer to the first variable header
62
63 **/
64 VARIABLE_HEADER *
65 GetStartPointer (
66 IN VARIABLE_STORE_HEADER *VarStoreHeader
67 )
68 {
69 //
70 // The end of variable store
71 //
72 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
73 }
74
75
76 /**
77 This code gets the pointer to the last variable memory pointer byte.
78
79 @param VarStoreHeader Pointer to the Variable Store Header.
80
81 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
82
83 **/
84 VARIABLE_HEADER *
85 GetEndPointer (
86 IN VARIABLE_STORE_HEADER *VarStoreHeader
87 )
88 {
89 //
90 // The end of variable store
91 //
92 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
93 }
94
95
96 /**
97 This code checks if variable header is valid or not.
98
99 @param Variable Pointer to the Variable Header.
100
101 @retval TRUE Variable header is valid.
102 @retval FALSE Variable header is not valid.
103
104 **/
105 BOOLEAN
106 IsValidVariableHeader (
107 IN VARIABLE_HEADER *Variable
108 )
109 {
110 if (Variable == NULL || Variable->StartId != VARIABLE_DATA ) {
111 return FALSE;
112 }
113
114 return TRUE;
115 }
116
117
118 /**
119 This code gets the size of name of variable.
120
121 @param Variable Pointer to the Variable Header.
122
123 @return Size of variable in bytes in type UINTN.
124
125 **/
126 UINTN
127 NameSizeOfVariable (
128 IN VARIABLE_HEADER *Variable
129 )
130 {
131 if (Variable->State == (UINT8) (-1) ||
132 Variable->DataSize == (UINT32) (-1) ||
133 Variable->NameSize == (UINT32) (-1) ||
134 Variable->Attributes == (UINT32) (-1)) {
135 return 0;
136 }
137 return (UINTN) Variable->NameSize;
138 }
139
140
141 /**
142 This code gets the size of data of variable.
143
144 @param Variable Pointer to the Variable Header.
145
146 @return Size of variable in bytes in type UINTN.
147
148 **/
149 UINTN
150 DataSizeOfVariable (
151 IN VARIABLE_HEADER *Variable
152 )
153 {
154 if (Variable->State == (UINT8) (-1) ||
155 Variable->DataSize == (UINT32) (-1) ||
156 Variable->NameSize == (UINT32) (-1) ||
157 Variable->Attributes == (UINT32) (-1)) {
158 return 0;
159 }
160 return (UINTN) Variable->DataSize;
161 }
162
163 /**
164 This code gets the pointer to the variable name.
165
166 @param Variable Pointer to the Variable Header.
167
168 @return A CHAR16* pointer to Variable Name.
169
170 **/
171 CHAR16 *
172 GetVariableNamePtr (
173 IN VARIABLE_HEADER *Variable
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 @param VariableHeader Pointer to the Variable Header that has consecutive content.
185
186 @return A UINT8* pointer to Variable Data.
187
188 **/
189 UINT8 *
190 GetVariableDataPtr (
191 IN VARIABLE_HEADER *Variable,
192 IN VARIABLE_HEADER *VariableHeader
193 )
194 {
195 UINTN Value;
196
197 //
198 // Be careful about pad size for alignment
199 //
200 Value = (UINTN) GetVariableNamePtr (Variable);
201 Value += NameSizeOfVariable (VariableHeader);
202 Value += GET_PAD_SIZE (NameSizeOfVariable (VariableHeader));
203
204 return (UINT8 *) Value;
205 }
206
207
208 /**
209 This code gets the pointer to the next variable header.
210
211 @param StoreInfo Pointer to variable store info structure.
212 @param Variable Pointer to the Variable Header.
213 @param VariableHeader Pointer to the Variable Header that has consecutive content.
214
215 @return A VARIABLE_HEADER* pointer to next variable header.
216
217 **/
218 VARIABLE_HEADER *
219 GetNextVariablePtr (
220 IN VARIABLE_STORE_INFO *StoreInfo,
221 IN VARIABLE_HEADER *Variable,
222 IN VARIABLE_HEADER *VariableHeader
223 )
224 {
225 EFI_PHYSICAL_ADDRESS TargetAddress;
226 EFI_PHYSICAL_ADDRESS SpareAddress;
227 UINTN Value;
228
229 Value = (UINTN) GetVariableDataPtr (Variable, VariableHeader);
230 Value += DataSizeOfVariable (VariableHeader);
231 Value += GET_PAD_SIZE (DataSizeOfVariable (VariableHeader));
232 //
233 // Be careful about pad size for alignment
234 //
235 Value = HEADER_ALIGN (Value);
236
237 if (StoreInfo->FtwLastWriteData != NULL) {
238 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;
239 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;
240 if (((UINTN) Variable < (UINTN) TargetAddress) && (Value >= (UINTN) TargetAddress)) {
241 //
242 // Next variable is in spare block.
243 //
244 Value = (UINTN) SpareAddress + (Value - (UINTN) TargetAddress);
245 }
246 }
247
248 return (VARIABLE_HEADER *) Value;
249 }
250
251 /**
252 Get variable store status.
253
254 @param VarStoreHeader Pointer to the Variable Store Header.
255
256 @retval EfiRaw Variable store is raw
257 @retval EfiValid Variable store is valid
258 @retval EfiInvalid Variable store is invalid
259
260 **/
261 VARIABLE_STORE_STATUS
262 GetVariableStoreStatus (
263 IN VARIABLE_STORE_HEADER *VarStoreHeader
264 )
265 {
266 if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) &&
267 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
268 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
269 ) {
270
271 return EfiValid;
272 }
273
274 if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
275 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
276 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
277 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
278 VarStoreHeader->Size == 0xffffffff &&
279 VarStoreHeader->Format == 0xff &&
280 VarStoreHeader->State == 0xff
281 ) {
282
283 return EfiRaw;
284 } else {
285 return EfiInvalid;
286 }
287 }
288
289 /**
290 Compare two variable names, one of them may be inconsecutive.
291
292 @param StoreInfo Pointer to variable store info structure.
293 @param Name1 Pointer to one variable name.
294 @param Name2 Pointer to another variable name.
295 @param NameSize Variable name size.
296
297 @retval TRUE Name1 and Name2 are identical.
298 @retval FALSE Name1 and Name2 are not identical.
299
300 **/
301 BOOLEAN
302 CompareVariableName (
303 IN VARIABLE_STORE_INFO *StoreInfo,
304 IN CONST CHAR16 *Name1,
305 IN CONST CHAR16 *Name2,
306 IN UINTN NameSize
307 )
308 {
309 EFI_PHYSICAL_ADDRESS TargetAddress;
310 EFI_PHYSICAL_ADDRESS SpareAddress;
311 UINTN PartialNameSize;
312
313 if (StoreInfo->FtwLastWriteData != NULL) {
314 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;
315 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;
316 if (((UINTN) Name1 < (UINTN) TargetAddress) && (((UINTN) Name1 + NameSize) > (UINTN) TargetAddress)) {
317 //
318 // Name1 is inconsecutive.
319 //
320 PartialNameSize = (UINTN) TargetAddress - (UINTN) Name1;
321 //
322 // Partial content is in NV storage.
323 //
324 if (CompareMem ((UINT8 *) Name1, (UINT8 *) Name2, PartialNameSize) == 0) {
325 //
326 // Another partial content is in spare block.
327 //
328 if (CompareMem ((UINT8 *) (UINTN) SpareAddress, (UINT8 *) Name2 + PartialNameSize, NameSize - PartialNameSize) == 0) {
329 return TRUE;
330 }
331 }
332 return FALSE;
333 } else if (((UINTN) Name2 < (UINTN) TargetAddress) && (((UINTN) Name2 + NameSize) > (UINTN) TargetAddress)) {
334 //
335 // Name2 is inconsecutive.
336 //
337 PartialNameSize = (UINTN) TargetAddress - (UINTN) Name2;
338 //
339 // Partial content is in NV storage.
340 //
341 if (CompareMem ((UINT8 *) Name2, (UINT8 *) Name1, PartialNameSize) == 0) {
342 //
343 // Another partial content is in spare block.
344 //
345 if (CompareMem ((UINT8 *) (UINTN) SpareAddress, (UINT8 *) Name1 + PartialNameSize, NameSize - PartialNameSize) == 0) {
346 return TRUE;
347 }
348 }
349 return FALSE;
350 }
351 }
352
353 //
354 // Both Name1 and Name2 are consecutive.
355 //
356 if (CompareMem ((UINT8 *) Name1, (UINT8 *) Name2, NameSize) == 0) {
357 return TRUE;
358 }
359 return FALSE;
360 }
361
362 /**
363 This function compares a variable with variable entries in database.
364
365 @param StoreInfo Pointer to variable store info structure.
366 @param Variable Pointer to the variable in our database
367 @param VariableHeader Pointer to the Variable Header that has consecutive content.
368 @param VariableName Name of the variable to compare to 'Variable'
369 @param VendorGuid GUID of the variable to compare to 'Variable'
370 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
371
372 @retval EFI_SUCCESS Found match variable
373 @retval EFI_NOT_FOUND Variable not found
374
375 **/
376 EFI_STATUS
377 CompareWithValidVariable (
378 IN VARIABLE_STORE_INFO *StoreInfo,
379 IN VARIABLE_HEADER *Variable,
380 IN VARIABLE_HEADER *VariableHeader,
381 IN CONST CHAR16 *VariableName,
382 IN CONST EFI_GUID *VendorGuid,
383 OUT VARIABLE_POINTER_TRACK *PtrTrack
384 )
385 {
386 VOID *Point;
387
388 if (VariableName[0] == 0) {
389 PtrTrack->CurrPtr = Variable;
390 return EFI_SUCCESS;
391 } else {
392 //
393 // Don't use CompareGuid function here for performance reasons.
394 // Instead we compare the GUID a UINT32 at a time and branch
395 // on the first failed comparison.
396 //
397 if ((((INT32 *) VendorGuid)[0] == ((INT32 *) &VariableHeader->VendorGuid)[0]) &&
398 (((INT32 *) VendorGuid)[1] == ((INT32 *) &VariableHeader->VendorGuid)[1]) &&
399 (((INT32 *) VendorGuid)[2] == ((INT32 *) &VariableHeader->VendorGuid)[2]) &&
400 (((INT32 *) VendorGuid)[3] == ((INT32 *) &VariableHeader->VendorGuid)[3])
401 ) {
402 ASSERT (NameSizeOfVariable (VariableHeader) != 0);
403 Point = (VOID *) GetVariableNamePtr (Variable);
404 if (CompareVariableName (StoreInfo, VariableName, Point, NameSizeOfVariable (VariableHeader))) {
405 PtrTrack->CurrPtr = Variable;
406 return EFI_SUCCESS;
407 }
408 }
409 }
410
411 return EFI_NOT_FOUND;
412 }
413
414 /**
415 Return the variable store header and the store info based on the Index.
416
417 @param Type The type of the variable store.
418 @param StoreInfo Return the store info.
419
420 @return Pointer to the variable store header.
421 **/
422 VARIABLE_STORE_HEADER *
423 GetVariableStore (
424 IN VARIABLE_STORE_TYPE Type,
425 OUT VARIABLE_STORE_INFO *StoreInfo
426 )
427 {
428 EFI_HOB_GUID_TYPE *GuidHob;
429 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
430 VARIABLE_STORE_HEADER *VariableStoreHeader;
431 EFI_PHYSICAL_ADDRESS NvStorageBase;
432 UINT32 NvStorageSize;
433 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;
434 UINT32 BackUpOffset;
435
436 StoreInfo->IndexTable = NULL;
437 StoreInfo->FtwLastWriteData = NULL;
438 VariableStoreHeader = NULL;
439 switch (Type) {
440 case VariableStoreTypeHob:
441 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);
442 if (GuidHob != NULL) {
443 VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob);
444 }
445 break;
446
447 case VariableStoreTypeNv:
448 if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE) {
449 //
450 // The content of NV storage for variable is not reliable in recovery boot mode.
451 //
452
453 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);
454 NvStorageBase = (EFI_PHYSICAL_ADDRESS) (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ?
455 PcdGet64 (PcdFlashNvStorageVariableBase64) :
456 PcdGet32 (PcdFlashNvStorageVariableBase)
457 );
458 //
459 // First let FvHeader point to NV storage base.
460 //
461 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) NvStorageBase;
462
463 //
464 // Check the FTW last write data hob.
465 //
466 BackUpOffset = 0;
467 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);
468 if (GuidHob != NULL) {
469 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);
470 if (FtwLastWriteData->TargetAddress == NvStorageBase) {
471 //
472 // Let FvHeader point to spare block.
473 //
474 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) FtwLastWriteData->SpareAddress;
475 DEBUG ((EFI_D_INFO, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));
476 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) && (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {
477 StoreInfo->FtwLastWriteData = FtwLastWriteData;
478 //
479 // Flash NV storage from the offset is backed up in spare block.
480 //
481 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);
482 DEBUG ((EFI_D_INFO, "PeiVariable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress));
483 //
484 // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.
485 //
486 }
487 }
488
489 //
490 // Check if the Firmware Volume is not corrupted
491 //
492 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {
493 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));
494 break;
495 }
496
497 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength);
498
499 GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);
500 if (GuidHob != NULL) {
501 StoreInfo->IndexTable = GET_GUID_HOB_DATA (GuidHob);
502 } else {
503 //
504 // If it's the first time to access variable region in flash, create a guid hob to record
505 // VAR_ADDED type variable info.
506 // Note that as the resource of PEI phase is limited, only store the limited number of
507 // VAR_ADDED type variables to reduce access time.
508 //
509 StoreInfo->IndexTable = (VARIABLE_INDEX_TABLE *) BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));
510 StoreInfo->IndexTable->Length = 0;
511 StoreInfo->IndexTable->StartPtr = GetStartPointer (VariableStoreHeader);
512 StoreInfo->IndexTable->EndPtr = GetEndPointer (VariableStoreHeader);
513 StoreInfo->IndexTable->GoneThrough = 0;
514 }
515 }
516 break;
517
518 default:
519 ASSERT (FALSE);
520 break;
521 }
522
523 StoreInfo->VariableStoreHeader = VariableStoreHeader;
524 return VariableStoreHeader;
525 }
526
527 /**
528 Get variable header that has consecutive content.
529
530 @param StoreInfo Pointer to variable store info structure.
531 @param Variable Pointer to the Variable Header.
532 @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.
533
534 @retval TRUE Variable header is valid.
535 @retval FALSE Variable header is not valid.
536
537 **/
538 BOOLEAN
539 GetVariableHeader (
540 IN VARIABLE_STORE_INFO *StoreInfo,
541 IN VARIABLE_HEADER *Variable,
542 OUT VARIABLE_HEADER **VariableHeader
543 )
544 {
545 EFI_PHYSICAL_ADDRESS TargetAddress;
546 EFI_PHYSICAL_ADDRESS SpareAddress;
547 EFI_HOB_GUID_TYPE *GuidHob;
548 UINTN PartialHeaderSize;
549
550 if (Variable == NULL) {
551 return FALSE;
552 }
553
554 //
555 // First assume variable header pointed by Variable is consecutive.
556 //
557 *VariableHeader = Variable;
558
559 if (StoreInfo->FtwLastWriteData != NULL) {
560 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;
561 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;
562 if (((UINTN) Variable > (UINTN) SpareAddress) &&
563 (((UINTN) Variable - (UINTN) SpareAddress + (UINTN) TargetAddress) >= (UINTN) GetEndPointer (StoreInfo->VariableStoreHeader))) {
564 //
565 // Reach the end of variable store.
566 //
567 return FALSE;
568 }
569 if (((UINTN) Variable < (UINTN) TargetAddress) && (((UINTN) Variable + sizeof (VARIABLE_HEADER)) > (UINTN) TargetAddress)) {
570 //
571 // Variable header pointed by Variable is inconsecutive,
572 // create a guid hob to combine the two partial variable header content together.
573 //
574 GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);
575 if (GuidHob != NULL) {
576 *VariableHeader = (VARIABLE_HEADER *) GET_GUID_HOB_DATA (GuidHob);
577 } else {
578 *VariableHeader = (VARIABLE_HEADER *) BuildGuidHob (&gEfiCallerIdGuid, sizeof (VARIABLE_HEADER));
579 PartialHeaderSize = (UINTN) TargetAddress - (UINTN) Variable;
580 //
581 // Partial content is in NV storage.
582 //
583 CopyMem ((UINT8 *) *VariableHeader, (UINT8 *) Variable, PartialHeaderSize);
584 //
585 // Another partial content is in spare block.
586 //
587 CopyMem ((UINT8 *) *VariableHeader + PartialHeaderSize, (UINT8 *) (UINTN) SpareAddress, sizeof (VARIABLE_HEADER) - PartialHeaderSize);
588 }
589 }
590 } else {
591 if (Variable >= GetEndPointer (StoreInfo->VariableStoreHeader)) {
592 //
593 // Reach the end of variable store.
594 //
595 return FALSE;
596 }
597 }
598
599 return IsValidVariableHeader (*VariableHeader);
600 }
601
602 /**
603 Get variable name or data to output buffer.
604
605 @param StoreInfo Pointer to variable store info structure.
606 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
607 @param Size Variable name/data size.
608 @param Buffer Pointer to output buffer to hold the variable name/data.
609
610 **/
611 VOID
612 GetVariableNameOrData (
613 IN VARIABLE_STORE_INFO *StoreInfo,
614 IN UINT8 *NameOrData,
615 IN UINTN Size,
616 OUT UINT8 *Buffer
617 )
618 {
619 EFI_PHYSICAL_ADDRESS TargetAddress;
620 EFI_PHYSICAL_ADDRESS SpareAddress;
621 UINTN PartialSize;
622
623 if (StoreInfo->FtwLastWriteData != NULL) {
624 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;
625 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;
626 if (((UINTN) NameOrData < (UINTN) TargetAddress) && (((UINTN) NameOrData + Size) > (UINTN) TargetAddress)) {
627 //
628 // Variable name/data is inconsecutive.
629 //
630 PartialSize = (UINTN) TargetAddress - (UINTN) NameOrData;
631 //
632 // Partial content is in NV storage.
633 //
634 CopyMem (Buffer, NameOrData, PartialSize);
635 //
636 // Another partial content is in spare block.
637 //
638 CopyMem (Buffer + PartialSize, (UINT8 *) (UINTN) SpareAddress, Size - PartialSize);
639 return;
640 }
641 }
642
643 //
644 // Variable name/data is consecutive.
645 //
646 CopyMem (Buffer, NameOrData, Size);
647 }
648
649 /**
650 Find the variable in the specified variable store.
651
652 @param StoreInfo Pointer to the store info structure.
653 @param VariableName Name of the variable to be found
654 @param VendorGuid Vendor GUID to be found.
655 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
656
657 @retval EFI_SUCCESS Variable found successfully
658 @retval EFI_NOT_FOUND Variable not found
659 @retval EFI_INVALID_PARAMETER Invalid variable name
660
661 **/
662 EFI_STATUS
663 FindVariableEx (
664 IN VARIABLE_STORE_INFO *StoreInfo,
665 IN CONST CHAR16 *VariableName,
666 IN CONST EFI_GUID *VendorGuid,
667 OUT VARIABLE_POINTER_TRACK *PtrTrack
668 )
669 {
670 VARIABLE_HEADER *Variable;
671 VARIABLE_HEADER *LastVariable;
672 VARIABLE_HEADER *MaxIndex;
673 UINTN Index;
674 UINTN Offset;
675 BOOLEAN StopRecord;
676 VARIABLE_HEADER *InDeletedVariable;
677 VARIABLE_STORE_HEADER *VariableStoreHeader;
678 VARIABLE_INDEX_TABLE *IndexTable;
679 VARIABLE_HEADER *VariableHeader;
680
681 VariableStoreHeader = StoreInfo->VariableStoreHeader;
682
683 if (VariableStoreHeader == NULL) {
684 return EFI_INVALID_PARAMETER;
685 }
686
687 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
688 return EFI_UNSUPPORTED;
689 }
690
691 if (~VariableStoreHeader->Size == 0) {
692 return EFI_NOT_FOUND;
693 }
694
695 IndexTable = StoreInfo->IndexTable;
696 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);
697 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);
698
699 InDeletedVariable = NULL;
700
701 //
702 // No Variable Address equals zero, so 0 as initial value is safe.
703 //
704 MaxIndex = NULL;
705 VariableHeader = NULL;
706
707 if (IndexTable != NULL) {
708 //
709 // traverse the variable index table to look for varible.
710 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
711 //
712 for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {
713 ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));
714 Offset += IndexTable->Index[Index];
715 MaxIndex = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);
716 GetVariableHeader (StoreInfo, MaxIndex, &VariableHeader);
717 if (CompareWithValidVariable (StoreInfo, MaxIndex, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
718 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
719 InDeletedVariable = PtrTrack->CurrPtr;
720 } else {
721 return EFI_SUCCESS;
722 }
723 }
724 }
725
726 if (IndexTable->GoneThrough != 0) {
727 //
728 // If the table has all the existing variables indexed, return.
729 //
730 PtrTrack->CurrPtr = InDeletedVariable;
731 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
732 }
733 }
734
735 if (MaxIndex != NULL) {
736 //
737 // HOB exists but the variable cannot be found in HOB
738 // If not found in HOB, then let's start from the MaxIndex we've found.
739 //
740 Variable = GetNextVariablePtr (StoreInfo, MaxIndex, VariableHeader);
741 LastVariable = MaxIndex;
742 } else {
743 //
744 // Start Pointers for the variable.
745 // Actual Data Pointer where data can be written.
746 //
747 Variable = PtrTrack->StartPtr;
748 LastVariable = PtrTrack->StartPtr;
749 }
750
751 //
752 // Find the variable by walk through variable store
753 //
754 StopRecord = FALSE;
755 while (GetVariableHeader (StoreInfo, Variable, &VariableHeader)) {
756 if (VariableHeader->State == VAR_ADDED || VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
757 //
758 // Record Variable in VariableIndex HOB
759 //
760 if ((IndexTable != NULL) && !StopRecord) {
761 Offset = (UINTN) Variable - (UINTN) LastVariable;
762 if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {
763 //
764 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
765 // or the record buffer is full.
766 //
767 StopRecord = TRUE;
768 } else {
769 IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;
770 LastVariable = Variable;
771 }
772 }
773
774 if (CompareWithValidVariable (StoreInfo, Variable, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
775 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
776 InDeletedVariable = PtrTrack->CurrPtr;
777 } else {
778 return EFI_SUCCESS;
779 }
780 }
781 }
782
783 Variable = GetNextVariablePtr (StoreInfo, Variable, VariableHeader);
784 }
785 //
786 // If gone through the VariableStore, that means we never find in Firmware any more.
787 //
788 if ((IndexTable != NULL) && !StopRecord) {
789 IndexTable->GoneThrough = 1;
790 }
791
792 PtrTrack->CurrPtr = InDeletedVariable;
793
794 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
795 }
796
797 /**
798 Find the variable in HOB and Non-Volatile variable storages.
799
800 @param VariableName Name of the variable to be found
801 @param VendorGuid Vendor GUID to be found.
802 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
803 @param StoreInfo Return the store info.
804
805 @retval EFI_SUCCESS Variable found successfully
806 @retval EFI_NOT_FOUND Variable not found
807 @retval EFI_INVALID_PARAMETER Invalid variable name
808 **/
809 EFI_STATUS
810 FindVariable (
811 IN CONST CHAR16 *VariableName,
812 IN CONST EFI_GUID *VendorGuid,
813 OUT VARIABLE_POINTER_TRACK *PtrTrack,
814 OUT VARIABLE_STORE_INFO *StoreInfo
815 )
816 {
817 EFI_STATUS Status;
818 VARIABLE_STORE_TYPE Type;
819
820 if (VariableName[0] != 0 && VendorGuid == NULL) {
821 return EFI_INVALID_PARAMETER;
822 }
823
824 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
825 GetVariableStore (Type, StoreInfo);
826 Status = FindVariableEx (
827 StoreInfo,
828 VariableName,
829 VendorGuid,
830 PtrTrack
831 );
832 if (!EFI_ERROR (Status)) {
833 return Status;
834 }
835 }
836
837 return EFI_NOT_FOUND;
838 }
839
840 /**
841 This service retrieves a variable's value using its name and GUID.
842
843 Read the specified variable from the UEFI variable store. If the Data
844 buffer is too small to hold the contents of the variable, the error
845 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
846 size to obtain the data.
847
848 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
849 @param VariableName A pointer to a null-terminated string that is the variable's name.
850 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
851 VariableGuid and VariableName must be unique.
852 @param Attributes If non-NULL, on return, points to the variable's attributes.
853 @param DataSize On entry, points to the size in bytes of the Data buffer.
854 On return, points to the size of the data returned in Data.
855 @param Data Points to the buffer which will hold the returned variable value.
856
857 @retval EFI_SUCCESS The variable was read successfully.
858 @retval EFI_NOT_FOUND The variable could not be found.
859 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
860 DataSize is updated with the size required for
861 the specified variable.
862 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
863 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
864
865 **/
866 EFI_STATUS
867 EFIAPI
868 PeiGetVariable (
869 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
870 IN CONST CHAR16 *VariableName,
871 IN CONST EFI_GUID *VariableGuid,
872 OUT UINT32 *Attributes,
873 IN OUT UINTN *DataSize,
874 OUT VOID *Data
875 )
876 {
877 VARIABLE_POINTER_TRACK Variable;
878 UINTN VarDataSize;
879 EFI_STATUS Status;
880 VARIABLE_STORE_INFO StoreInfo;
881 VARIABLE_HEADER *VariableHeader;
882
883 if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {
884 return EFI_INVALID_PARAMETER;
885 }
886
887 //
888 // Find existing variable
889 //
890 Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);
891 if (EFI_ERROR (Status)) {
892 return Status;
893 }
894 GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);
895
896 //
897 // Get data size
898 //
899 VarDataSize = DataSizeOfVariable (VariableHeader);
900 if (*DataSize >= VarDataSize) {
901 if (Data == NULL) {
902 return EFI_INVALID_PARAMETER;
903 }
904
905 GetVariableNameOrData (&StoreInfo, GetVariableDataPtr (Variable.CurrPtr, VariableHeader), VarDataSize, Data);
906
907 if (Attributes != NULL) {
908 *Attributes = VariableHeader->Attributes;
909 }
910
911 *DataSize = VarDataSize;
912 return EFI_SUCCESS;
913 } else {
914 *DataSize = VarDataSize;
915 return EFI_BUFFER_TOO_SMALL;
916 }
917 }
918
919 /**
920 Return the next variable name and GUID.
921
922 This function is called multiple times to retrieve the VariableName
923 and VariableGuid of all variables currently available in the system.
924 On each call, the previous results are passed into the interface,
925 and, on return, the interface returns the data for the next
926 interface. When the entire variable list has been returned,
927 EFI_NOT_FOUND is returned.
928
929 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
930
931 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
932 On return, the size of the variable name buffer.
933 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
934 On return, points to the next variable's null-terminated name string.
935 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
936 On return, a pointer to the next variable's GUID.
937
938 @retval EFI_SUCCESS The variable was read successfully.
939 @retval EFI_NOT_FOUND The variable could not be found.
940 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
941 data. VariableNameSize is updated with the size
942 required for the specified variable.
943 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
944 VariableNameSize is NULL.
945 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
946
947 **/
948 EFI_STATUS
949 EFIAPI
950 PeiGetNextVariableName (
951 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
952 IN OUT UINTN *VariableNameSize,
953 IN OUT CHAR16 *VariableName,
954 IN OUT EFI_GUID *VariableGuid
955 )
956 {
957 VARIABLE_STORE_TYPE Type;
958 VARIABLE_POINTER_TRACK Variable;
959 VARIABLE_POINTER_TRACK VariableInHob;
960 VARIABLE_POINTER_TRACK VariablePtrTrack;
961 UINTN VarNameSize;
962 EFI_STATUS Status;
963 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
964 VARIABLE_HEADER *VariableHeader;
965 VARIABLE_STORE_INFO StoreInfo;
966 VARIABLE_STORE_INFO StoreInfoForNv;
967 VARIABLE_STORE_INFO StoreInfoForHob;
968
969 if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {
970 return EFI_INVALID_PARAMETER;
971 }
972
973 Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);
974 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {
975 return Status;
976 }
977
978 if (VariableName[0] != 0) {
979 //
980 // If variable name is not NULL, get next variable
981 //
982 GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);
983 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
984 }
985
986 VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, &StoreInfoForHob);
987 VariableStoreHeader[VariableStoreTypeNv] = GetVariableStore (VariableStoreTypeNv, &StoreInfoForNv);
988
989 while (TRUE) {
990 //
991 // Switch from HOB to Non-Volatile.
992 //
993 while (!GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader)) {
994 //
995 // Find current storage index
996 //
997 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
998 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
999 break;
1000 }
1001 }
1002 ASSERT (Type < VariableStoreTypeMax);
1003 //
1004 // Switch to next storage
1005 //
1006 for (Type++; Type < VariableStoreTypeMax; Type++) {
1007 if (VariableStoreHeader[Type] != NULL) {
1008 break;
1009 }
1010 }
1011 //
1012 // Capture the case that
1013 // 1. current storage is the last one, or
1014 // 2. no further storage
1015 //
1016 if (Type == VariableStoreTypeMax) {
1017 return EFI_NOT_FOUND;
1018 }
1019 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
1020 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
1021 Variable.CurrPtr = Variable.StartPtr;
1022 GetVariableStore (Type, &StoreInfo);
1023 }
1024
1025 if (VariableHeader->State == VAR_ADDED || VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1026 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1027 //
1028 // If it is a IN_DELETED_TRANSITION variable,
1029 // and there is also a same ADDED one at the same time,
1030 // don't return it.
1031 //
1032 Status = FindVariableEx (
1033 &StoreInfo,
1034 GetVariableNamePtr (Variable.CurrPtr),
1035 &VariableHeader->VendorGuid,
1036 &VariablePtrTrack
1037 );
1038 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr != Variable.CurrPtr) {
1039 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
1040 continue;
1041 }
1042 }
1043
1044 //
1045 // Don't return NV variable when HOB overrides it
1046 //
1047 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
1048 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
1049 ) {
1050 Status = FindVariableEx (
1051 &StoreInfoForHob,
1052 GetVariableNamePtr (Variable.CurrPtr),
1053 &VariableHeader->VendorGuid,
1054 &VariableInHob
1055 );
1056 if (!EFI_ERROR (Status)) {
1057 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
1058 continue;
1059 }
1060 }
1061
1062 VarNameSize = NameSizeOfVariable (VariableHeader);
1063 ASSERT (VarNameSize != 0);
1064
1065 if (VarNameSize <= *VariableNameSize) {
1066 GetVariableNameOrData (&StoreInfo, (UINT8 *) GetVariableNamePtr (Variable.CurrPtr), VarNameSize, (UINT8 *) VariableName);
1067
1068 CopyMem (VariableGuid, &VariableHeader->VendorGuid, sizeof (EFI_GUID));
1069
1070 Status = EFI_SUCCESS;
1071 } else {
1072 Status = EFI_BUFFER_TOO_SMALL;
1073 }
1074
1075 *VariableNameSize = VarNameSize;
1076 //
1077 // Variable is found
1078 //
1079 return Status;
1080 } else {
1081 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
1082 }
1083 }
1084 }