]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/Pei/Variable.c
MdeModulePkg: Variable drivers robustly handle crashes during Reclaim().
[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
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 //
551 // First assume variable header pointed by Variable is consecutive.
552 //
553 *VariableHeader = Variable;
554
555 if ((Variable != NULL) && (StoreInfo->FtwLastWriteData != NULL)) {
556 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;
557 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;
558 if (((UINTN) Variable < (UINTN) TargetAddress) && (((UINTN) Variable + sizeof (VARIABLE_HEADER)) > (UINTN) TargetAddress)) {
559 //
560 // Variable header pointed by Variable is inconsecutive,
561 // create a guid hob to combine the two partial variable header content together.
562 //
563 GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);
564 if (GuidHob != NULL) {
565 *VariableHeader = (VARIABLE_HEADER *) GET_GUID_HOB_DATA (GuidHob);
566 } else {
567 *VariableHeader = (VARIABLE_HEADER *) BuildGuidHob (&gEfiCallerIdGuid, sizeof (VARIABLE_HEADER));
568 PartialHeaderSize = (UINTN) TargetAddress - (UINTN) Variable;
569 //
570 // Partial content is in NV storage.
571 //
572 CopyMem ((UINT8 *) *VariableHeader, (UINT8 *) Variable, PartialHeaderSize);
573 //
574 // Another partial content is in spare block.
575 //
576 CopyMem ((UINT8 *) *VariableHeader + PartialHeaderSize, (UINT8 *) (UINTN) SpareAddress, sizeof (VARIABLE_HEADER) - PartialHeaderSize);
577 }
578 }
579 }
580
581 return IsValidVariableHeader (*VariableHeader);
582 }
583
584 /**
585 Get variable name or data to output buffer.
586
587 @param StoreInfo Pointer to variable store info structure.
588 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
589 @param Size Variable name/data size.
590 @param Buffer Pointer to output buffer to hold the variable name/data.
591
592 **/
593 VOID
594 GetVariableNameOrData (
595 IN VARIABLE_STORE_INFO *StoreInfo,
596 IN UINT8 *NameOrData,
597 IN UINTN Size,
598 OUT UINT8 *Buffer
599 )
600 {
601 EFI_PHYSICAL_ADDRESS TargetAddress;
602 EFI_PHYSICAL_ADDRESS SpareAddress;
603 UINTN PartialSize;
604
605 if (StoreInfo->FtwLastWriteData != NULL) {
606 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;
607 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;
608 if (((UINTN) NameOrData < (UINTN) TargetAddress) && (((UINTN) NameOrData + Size) > (UINTN) TargetAddress)) {
609 //
610 // Variable name/data is inconsecutive.
611 //
612 PartialSize = (UINTN) TargetAddress - (UINTN) NameOrData;
613 //
614 // Partial content is in NV storage.
615 //
616 CopyMem (Buffer, NameOrData, PartialSize);
617 //
618 // Another partial content is in spare block.
619 //
620 CopyMem (Buffer + PartialSize, (UINT8 *) (UINTN) SpareAddress, Size - PartialSize);
621 return;
622 }
623 }
624
625 //
626 // Variable name/data is consecutive.
627 //
628 CopyMem (Buffer, NameOrData, Size);
629 }
630
631 /**
632 Find the variable in the specified variable store.
633
634 @param StoreInfo Pointer to the store info structure.
635 @param VariableName Name of the variable to be found
636 @param VendorGuid Vendor GUID to be found.
637 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
638
639 @retval EFI_SUCCESS Variable found successfully
640 @retval EFI_NOT_FOUND Variable not found
641 @retval EFI_INVALID_PARAMETER Invalid variable name
642
643 **/
644 EFI_STATUS
645 FindVariableEx (
646 IN VARIABLE_STORE_INFO *StoreInfo,
647 IN CONST CHAR16 *VariableName,
648 IN CONST EFI_GUID *VendorGuid,
649 OUT VARIABLE_POINTER_TRACK *PtrTrack
650 )
651 {
652 VARIABLE_HEADER *Variable;
653 VARIABLE_HEADER *LastVariable;
654 VARIABLE_HEADER *MaxIndex;
655 UINTN Index;
656 UINTN Offset;
657 BOOLEAN StopRecord;
658 VARIABLE_HEADER *InDeletedVariable;
659 VARIABLE_STORE_HEADER *VariableStoreHeader;
660 VARIABLE_INDEX_TABLE *IndexTable;
661 VARIABLE_HEADER *VariableHeader;
662
663 VariableStoreHeader = StoreInfo->VariableStoreHeader;
664
665 if (VariableStoreHeader == NULL) {
666 return EFI_INVALID_PARAMETER;
667 }
668
669 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
670 return EFI_UNSUPPORTED;
671 }
672
673 if (~VariableStoreHeader->Size == 0) {
674 return EFI_NOT_FOUND;
675 }
676
677 IndexTable = StoreInfo->IndexTable;
678 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);
679 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);
680
681 InDeletedVariable = NULL;
682
683 //
684 // No Variable Address equals zero, so 0 as initial value is safe.
685 //
686 MaxIndex = NULL;
687 VariableHeader = NULL;
688
689 if (IndexTable != NULL) {
690 //
691 // traverse the variable index table to look for varible.
692 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
693 //
694 for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {
695 ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));
696 Offset += IndexTable->Index[Index];
697 MaxIndex = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);
698 GetVariableHeader (StoreInfo, MaxIndex, &VariableHeader);
699 if (CompareWithValidVariable (StoreInfo, MaxIndex, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
700 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
701 InDeletedVariable = PtrTrack->CurrPtr;
702 } else {
703 return EFI_SUCCESS;
704 }
705 }
706 }
707
708 if (IndexTable->GoneThrough != 0) {
709 //
710 // If the table has all the existing variables indexed, return.
711 //
712 PtrTrack->CurrPtr = InDeletedVariable;
713 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
714 }
715 }
716
717 if (MaxIndex != NULL) {
718 //
719 // HOB exists but the variable cannot be found in HOB
720 // If not found in HOB, then let's start from the MaxIndex we've found.
721 //
722 Variable = GetNextVariablePtr (StoreInfo, MaxIndex, VariableHeader);
723 LastVariable = MaxIndex;
724 } else {
725 //
726 // Start Pointers for the variable.
727 // Actual Data Pointer where data can be written.
728 //
729 Variable = PtrTrack->StartPtr;
730 LastVariable = PtrTrack->StartPtr;
731 }
732
733 //
734 // Find the variable by walk through variable store
735 //
736 StopRecord = FALSE;
737 while (GetVariableHeader (StoreInfo, Variable, &VariableHeader)) {
738 if (VariableHeader->State == VAR_ADDED || VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
739 //
740 // Record Variable in VariableIndex HOB
741 //
742 if ((IndexTable != NULL) && !StopRecord) {
743 Offset = (UINTN) Variable - (UINTN) LastVariable;
744 if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {
745 //
746 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
747 // or the record buffer is full.
748 //
749 StopRecord = TRUE;
750 } else {
751 IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;
752 LastVariable = Variable;
753 }
754 }
755
756 if (CompareWithValidVariable (StoreInfo, Variable, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
757 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
758 InDeletedVariable = PtrTrack->CurrPtr;
759 } else {
760 return EFI_SUCCESS;
761 }
762 }
763 }
764
765 Variable = GetNextVariablePtr (StoreInfo, Variable, VariableHeader);
766 }
767 //
768 // If gone through the VariableStore, that means we never find in Firmware any more.
769 //
770 if ((IndexTable != NULL) && !StopRecord) {
771 IndexTable->GoneThrough = 1;
772 }
773
774 PtrTrack->CurrPtr = InDeletedVariable;
775
776 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
777 }
778
779 /**
780 Find the variable in HOB and Non-Volatile variable storages.
781
782 @param VariableName Name of the variable to be found
783 @param VendorGuid Vendor GUID to be found.
784 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
785 @param StoreInfo Return the store info.
786
787 @retval EFI_SUCCESS Variable found successfully
788 @retval EFI_NOT_FOUND Variable not found
789 @retval EFI_INVALID_PARAMETER Invalid variable name
790 **/
791 EFI_STATUS
792 FindVariable (
793 IN CONST CHAR16 *VariableName,
794 IN CONST EFI_GUID *VendorGuid,
795 OUT VARIABLE_POINTER_TRACK *PtrTrack,
796 OUT VARIABLE_STORE_INFO *StoreInfo
797 )
798 {
799 EFI_STATUS Status;
800 VARIABLE_STORE_TYPE Type;
801
802 if (VariableName[0] != 0 && VendorGuid == NULL) {
803 return EFI_INVALID_PARAMETER;
804 }
805
806 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
807 GetVariableStore (Type, StoreInfo);
808 Status = FindVariableEx (
809 StoreInfo,
810 VariableName,
811 VendorGuid,
812 PtrTrack
813 );
814 if (!EFI_ERROR (Status)) {
815 return Status;
816 }
817 }
818
819 return EFI_NOT_FOUND;
820 }
821
822 /**
823 This service retrieves a variable's value using its name and GUID.
824
825 Read the specified variable from the UEFI variable store. If the Data
826 buffer is too small to hold the contents of the variable, the error
827 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
828 size to obtain the data.
829
830 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
831 @param VariableName A pointer to a null-terminated string that is the variable's name.
832 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
833 VariableGuid and VariableName must be unique.
834 @param Attributes If non-NULL, on return, points to the variable's attributes.
835 @param DataSize On entry, points to the size in bytes of the Data buffer.
836 On return, points to the size of the data returned in Data.
837 @param Data Points to the buffer which will hold the returned variable value.
838
839 @retval EFI_SUCCESS The variable was read successfully.
840 @retval EFI_NOT_FOUND The variable could not be found.
841 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
842 DataSize is updated with the size required for
843 the specified variable.
844 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
845 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
846
847 **/
848 EFI_STATUS
849 EFIAPI
850 PeiGetVariable (
851 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
852 IN CONST CHAR16 *VariableName,
853 IN CONST EFI_GUID *VariableGuid,
854 OUT UINT32 *Attributes,
855 IN OUT UINTN *DataSize,
856 OUT VOID *Data
857 )
858 {
859 VARIABLE_POINTER_TRACK Variable;
860 UINTN VarDataSize;
861 EFI_STATUS Status;
862 VARIABLE_STORE_INFO StoreInfo;
863 VARIABLE_HEADER *VariableHeader;
864
865 if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {
866 return EFI_INVALID_PARAMETER;
867 }
868
869 //
870 // Find existing variable
871 //
872 Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);
873 if (EFI_ERROR (Status)) {
874 return Status;
875 }
876 GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);
877
878 //
879 // Get data size
880 //
881 VarDataSize = DataSizeOfVariable (VariableHeader);
882 if (*DataSize >= VarDataSize) {
883 if (Data == NULL) {
884 return EFI_INVALID_PARAMETER;
885 }
886
887 GetVariableNameOrData (&StoreInfo, GetVariableDataPtr (Variable.CurrPtr, VariableHeader), VarDataSize, Data);
888
889 if (Attributes != NULL) {
890 *Attributes = VariableHeader->Attributes;
891 }
892
893 *DataSize = VarDataSize;
894 return EFI_SUCCESS;
895 } else {
896 *DataSize = VarDataSize;
897 return EFI_BUFFER_TOO_SMALL;
898 }
899 }
900
901 /**
902 Return the next variable name and GUID.
903
904 This function is called multiple times to retrieve the VariableName
905 and VariableGuid of all variables currently available in the system.
906 On each call, the previous results are passed into the interface,
907 and, on return, the interface returns the data for the next
908 interface. When the entire variable list has been returned,
909 EFI_NOT_FOUND is returned.
910
911 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
912
913 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
914 On return, the size of the variable name buffer.
915 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
916 On return, points to the next variable's null-terminated name string.
917 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
918 On return, a pointer to the next variable's GUID.
919
920 @retval EFI_SUCCESS The variable was read successfully.
921 @retval EFI_NOT_FOUND The variable could not be found.
922 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
923 data. VariableNameSize is updated with the size
924 required for the specified variable.
925 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
926 VariableNameSize is NULL.
927 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
928
929 **/
930 EFI_STATUS
931 EFIAPI
932 PeiGetNextVariableName (
933 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
934 IN OUT UINTN *VariableNameSize,
935 IN OUT CHAR16 *VariableName,
936 IN OUT EFI_GUID *VariableGuid
937 )
938 {
939 VARIABLE_STORE_TYPE Type;
940 VARIABLE_POINTER_TRACK Variable;
941 VARIABLE_POINTER_TRACK VariableInHob;
942 VARIABLE_POINTER_TRACK VariablePtrTrack;
943 UINTN VarNameSize;
944 EFI_STATUS Status;
945 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
946 VARIABLE_HEADER *VariableHeader;
947 VARIABLE_STORE_INFO StoreInfo;
948 VARIABLE_STORE_INFO StoreInfoForNv;
949 VARIABLE_STORE_INFO StoreInfoForHob;
950
951 if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {
952 return EFI_INVALID_PARAMETER;
953 }
954
955 Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);
956 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {
957 return Status;
958 }
959
960 if (VariableName[0] != 0) {
961 //
962 // If variable name is not NULL, get next variable
963 //
964 GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);
965 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
966 }
967
968 VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, &StoreInfoForHob);
969 VariableStoreHeader[VariableStoreTypeNv] = GetVariableStore (VariableStoreTypeNv, &StoreInfoForNv);
970
971 while (TRUE) {
972 //
973 // Switch from HOB to Non-Volatile.
974 //
975 while (!GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader)) {
976 //
977 // Find current storage index
978 //
979 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
980 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
981 break;
982 }
983 }
984 ASSERT (Type < VariableStoreTypeMax);
985 //
986 // Switch to next storage
987 //
988 for (Type++; Type < VariableStoreTypeMax; Type++) {
989 if (VariableStoreHeader[Type] != NULL) {
990 break;
991 }
992 }
993 //
994 // Capture the case that
995 // 1. current storage is the last one, or
996 // 2. no further storage
997 //
998 if (Type == VariableStoreTypeMax) {
999 return EFI_NOT_FOUND;
1000 }
1001 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
1002 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
1003 Variable.CurrPtr = Variable.StartPtr;
1004 GetVariableStore (Type, &StoreInfo);
1005 }
1006
1007 if (VariableHeader->State == VAR_ADDED || VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1008 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1009 //
1010 // If it is a IN_DELETED_TRANSITION variable,
1011 // and there is also a same ADDED one at the same time,
1012 // don't return it.
1013 //
1014 Status = FindVariableEx (
1015 &StoreInfo,
1016 GetVariableNamePtr (Variable.CurrPtr),
1017 &VariableHeader->VendorGuid,
1018 &VariablePtrTrack
1019 );
1020 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr != Variable.CurrPtr) {
1021 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
1022 continue;
1023 }
1024 }
1025
1026 //
1027 // Don't return NV variable when HOB overrides it
1028 //
1029 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
1030 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
1031 ) {
1032 Status = FindVariableEx (
1033 &StoreInfoForHob,
1034 GetVariableNamePtr (Variable.CurrPtr),
1035 &VariableHeader->VendorGuid,
1036 &VariableInHob
1037 );
1038 if (!EFI_ERROR (Status)) {
1039 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
1040 continue;
1041 }
1042 }
1043
1044 VarNameSize = NameSizeOfVariable (VariableHeader);
1045 ASSERT (VarNameSize != 0);
1046
1047 if (VarNameSize <= *VariableNameSize) {
1048 GetVariableNameOrData (&StoreInfo, (UINT8 *) GetVariableNamePtr (Variable.CurrPtr), VarNameSize, (UINT8 *) VariableName);
1049
1050 CopyMem (VariableGuid, &VariableHeader->VendorGuid, sizeof (EFI_GUID));
1051
1052 Status = EFI_SUCCESS;
1053 } else {
1054 Status = EFI_BUFFER_TOO_SMALL;
1055 }
1056
1057 *VariableNameSize = VarNameSize;
1058 //
1059 // Variable is found
1060 //
1061 return Status;
1062 } else {
1063 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);
1064 }
1065 }
1066 }