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