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