]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/Pei/Variable.c
Change Variable driver (Pei/RuntimeDxe) to support the default variable data stored...
[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
328 @param Index The index of the variable store.\r
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
364 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength);\r
365\r
366 if (IndexTable != NULL) {\r
367 GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);\r
368 if (GuidHob != NULL) {\r
369 *IndexTable = GET_GUID_HOB_DATA (GuidHob);\r
370 } else {\r
371 //\r
372 // If it's the first time to access variable region in flash, create a guid hob to record\r
373 // VAR_ADDED type variable info.\r
374 // Note that as the resource of PEI phase is limited, only store the limited number of \r
375 // VAR_ADDED type variables to reduce access time.\r
376 //\r
377 *IndexTable = BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));\r
378 (*IndexTable)->Length = 0;\r
379 (*IndexTable)->StartPtr = GetStartPointer (VariableStoreHeader);\r
380 (*IndexTable)->EndPtr = GetEndPointer (VariableStoreHeader);\r
381 (*IndexTable)->GoneThrough = 0;\r
382 }\r
383 }\r
384 }\r
385 break;\r
386 }\r
387\r
388 return VariableStoreHeader;\r
389}\r
33479ddf 390\r
391/**\r
0f7aff72 392 Find the variable in the specified variable store.\r
33479ddf 393\r
0f7aff72
RN
394 @param VariableStoreHeader Pointer to the variable store header.\r
395 @param IndexTable Pointer to the index table.\r
396 @param VariableName Name of the variable to be found\r
397 @param VendorGuid Vendor GUID to be found.\r
398 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
33479ddf 399\r
400 @retval EFI_SUCCESS Variable found successfully\r
401 @retval EFI_NOT_FOUND Variable not found\r
402 @retval EFI_INVALID_PARAMETER Invalid variable name\r
403\r
404**/\r
8d3a5c82 405EFI_STATUS\r
0f7aff72
RN
406FindVariableEx (\r
407 IN VARIABLE_STORE_HEADER *VariableStoreHeader,\r
408 IN VARIABLE_INDEX_TABLE *IndexTable,\r
409 IN CONST CHAR16 *VariableName,\r
410 IN CONST EFI_GUID *VendorGuid,\r
411 OUT VARIABLE_POINTER_TRACK *PtrTrack\r
8d3a5c82 412 )\r
8d3a5c82 413{\r
8d3a5c82 414 VARIABLE_HEADER *Variable;\r
841014ba 415 VARIABLE_HEADER *LastVariable;\r
8d3a5c82 416 VARIABLE_HEADER *MaxIndex;\r
0f7aff72
RN
417 UINTN Index;\r
418 UINTN Offset;\r
841014ba 419 BOOLEAN StopRecord;\r
8d3a5c82 420\r
0f7aff72 421 if (VariableStoreHeader == NULL) {\r
8d3a5c82 422 return EFI_INVALID_PARAMETER;\r
423 }\r
0f7aff72
RN
424\r
425 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
426 return EFI_UNSUPPORTED;\r
427 }\r
428\r
429 if (~VariableStoreHeader->Size == 0) {\r
430 return EFI_NOT_FOUND;\r
431 }\r
432\r
433 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);\r
434 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);\r
435\r
8d3a5c82 436 //\r
437 // No Variable Address equals zero, so 0 as initial value is safe.\r
438 //\r
0f7aff72 439 MaxIndex = NULL;\r
8d3a5c82 440\r
0f7aff72 441 if (IndexTable != NULL) {\r
841014ba 442 //\r
0f7aff72
RN
443 // traverse the variable index table to look for varible.\r
444 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.\r
841014ba 445 //\r
0f7aff72
RN
446 for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {\r
447 ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));\r
448 Offset += IndexTable->Index[Index];\r
449 MaxIndex = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);\r
8d3a5c82 450 if (CompareWithValidVariable (MaxIndex, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
8d3a5c82 451 return EFI_SUCCESS;\r
452 }\r
453 }\r
454\r
33479ddf 455 if (IndexTable->GoneThrough != 0) {\r
0f7aff72
RN
456 //\r
457 // If the table has all the existing variables indexed and we still cannot find it.\r
458 //\r
8d3a5c82 459 return EFI_NOT_FOUND;\r
460 }\r
461 }\r
0f7aff72 462\r
8d3a5c82 463 if (MaxIndex != NULL) {\r
0f7aff72
RN
464 //\r
465 // HOB exists but the variable cannot be found in HOB\r
466 // If not found in HOB, then let's start from the MaxIndex we've found.\r
467 //\r
841014ba 468 Variable = GetNextVariablePtr (MaxIndex);\r
469 LastVariable = MaxIndex;\r
8d3a5c82 470 } else {\r
0f7aff72
RN
471 //\r
472 // Start Pointers for the variable.\r
473 // Actual Data Pointer where data can be written.\r
474 //\r
475 Variable = PtrTrack->StartPtr;\r
476 LastVariable = PtrTrack->StartPtr;\r
8d3a5c82 477 }\r
0f7aff72 478\r
8d3a5c82 479 //\r
480 // Find the variable by walk through non-volatile variable store\r
481 //\r
0f7aff72
RN
482 StopRecord = FALSE;\r
483 while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {\r
8d3a5c82 484 if (Variable->State == VAR_ADDED) {\r
485 //\r
486 // Record Variable in VariableIndex HOB\r
487 //\r
0f7aff72
RN
488 if ((IndexTable != NULL) && !StopRecord) {\r
489 Offset = (UINTN) Variable - (UINTN) LastVariable;\r
490 if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {\r
491 //\r
492 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),\r
493 // or the record buffer is full.\r
494 //\r
841014ba 495 StopRecord = TRUE;\r
0f7aff72 496 } else {\r
841014ba 497 IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;\r
0f7aff72 498 LastVariable = Variable;\r
841014ba 499 }\r
841014ba 500 }\r
501\r
8d3a5c82 502 if (CompareWithValidVariable (Variable, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
503 return EFI_SUCCESS;\r
504 }\r
505 }\r
506\r
507 Variable = GetNextVariablePtr (Variable);\r
508 }\r
509 //\r
510 // If gone through the VariableStore, that means we never find in Firmware any more.\r
511 //\r
0f7aff72 512 if ((IndexTable != NULL) && !StopRecord) {\r
8d3a5c82 513 IndexTable->GoneThrough = 1;\r
514 }\r
515\r
516 PtrTrack->CurrPtr = NULL;\r
517\r
518 return EFI_NOT_FOUND;\r
519}\r
520\r
0f7aff72
RN
521/**\r
522 Find the variable in HOB and Non-Volatile variable storages.\r
523\r
524 @param VariableName Name of the variable to be found\r
525 @param VendorGuid Vendor GUID to be found.\r
526 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
527\r
528 @retval EFI_SUCCESS Variable found successfully\r
529 @retval EFI_NOT_FOUND Variable not found\r
530 @retval EFI_INVALID_PARAMETER Invalid variable name\r
531**/\r
532EFI_STATUS\r
533FindVariable (\r
534 IN CONST CHAR16 *VariableName,\r
535 IN CONST EFI_GUID *VendorGuid,\r
536 OUT VARIABLE_POINTER_TRACK *PtrTrack\r
537 )\r
538{\r
539 EFI_STATUS Status;\r
540 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
541 VARIABLE_INDEX_TABLE *IndexTable;\r
542 VARIABLE_STORE_TYPE Type;\r
543\r
544 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
545 return EFI_INVALID_PARAMETER;\r
546 }\r
547\r
548 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
549 VariableStoreHeader = GetVariableStore (Type, &IndexTable);\r
550 Status = FindVariableEx (\r
551 VariableStoreHeader,\r
552 IndexTable,\r
553 VariableName,\r
554 VendorGuid, \r
555 PtrTrack\r
556 );\r
557 if (!EFI_ERROR (Status)) {\r
558 return Status;\r
559 }\r
560 }\r
561\r
562 return EFI_NOT_FOUND;\r
563}\r
564\r
33479ddf 565/**\r
566 This service retrieves a variable's value using its name and GUID.\r
567\r
568 Read the specified variable from the UEFI variable store. If the Data \r
569 buffer is too small to hold the contents of the variable, the error\r
570 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer\r
571 size to obtain the data.\r
572\r
573 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
574 @param VariableName A pointer to a null-terminated string that is the variable's name.\r
575 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of\r
576 VariableGuid and VariableName must be unique.\r
577 @param Attributes If non-NULL, on return, points to the variable's attributes.\r
578 @param DataSize On entry, points to the size in bytes of the Data buffer.\r
579 On return, points to the size of the data returned in Data.\r
580 @param Data Points to the buffer which will hold the returned variable value.\r
581\r
582 @retval EFI_SUCCESS The variable was read successfully.\r
583 @retval EFI_NOT_FOUND The variable could not be found.\r
584 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data. \r
585 DataSize is updated with the size required for \r
586 the specified variable.\r
587 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.\r
588 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.\r
589\r
590**/\r
8d3a5c82 591EFI_STATUS\r
592EFIAPI\r
593PeiGetVariable (\r
594 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
595 IN CONST CHAR16 *VariableName,\r
596 IN CONST EFI_GUID *VariableGuid,\r
597 OUT UINT32 *Attributes,\r
598 IN OUT UINTN *DataSize,\r
599 OUT VOID *Data\r
600 )\r
8d3a5c82 601{\r
602 VARIABLE_POINTER_TRACK Variable;\r
603 UINTN VarDataSize;\r
604 EFI_STATUS Status;\r
8d3a5c82 605\r
01f352a7 606 if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {\r
8d3a5c82 607 return EFI_INVALID_PARAMETER;\r
608 }\r
4249fa76 609\r
8d3a5c82 610 //\r
611 // Find existing variable\r
612 //\r
0f7aff72
RN
613 Status = FindVariable (VariableName, VariableGuid, &Variable);\r
614 if (EFI_ERROR (Status)) {\r
8d3a5c82 615 return Status;\r
616 }\r
617 //\r
618 // Get data size\r
619 //\r
130e2569 620 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
8d3a5c82 621 if (*DataSize >= VarDataSize) {\r
39aea48d 622 if (Data == NULL) {\r
623 return EFI_INVALID_PARAMETER;\r
624 }\r
625\r
33479ddf 626 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
8d3a5c82 627\r
628 if (Attributes != NULL) {\r
629 *Attributes = Variable.CurrPtr->Attributes;\r
630 }\r
631\r
632 *DataSize = VarDataSize;\r
633 return EFI_SUCCESS;\r
634 } else {\r
635 *DataSize = VarDataSize;\r
636 return EFI_BUFFER_TOO_SMALL;\r
637 }\r
638}\r
639\r
33479ddf 640/**\r
641 Return the next variable name and GUID.\r
642\r
643 This function is called multiple times to retrieve the VariableName \r
644 and VariableGuid of all variables currently available in the system. \r
645 On each call, the previous results are passed into the interface, \r
646 and, on return, the interface returns the data for the next \r
647 interface. When the entire variable list has been returned, \r
648 EFI_NOT_FOUND is returned.\r
649\r
650 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
651\r
652 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.\r
f7dab9fa 653 On return, the size of the variable name buffer.\r
33479ddf 654 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.\r
655 On return, points to the next variable's null-terminated name string.\r
255f993d 656 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID. \r
33479ddf 657 On return, a pointer to the next variable's GUID.\r
658\r
659 @retval EFI_SUCCESS The variable was read successfully.\r
660 @retval EFI_NOT_FOUND The variable could not be found.\r
661 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting\r
662 data. VariableNameSize is updated with the size\r
663 required for the specified variable.\r
664 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or\r
665 VariableNameSize is NULL.\r
666 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.\r
667\r
668**/\r
8d3a5c82 669EFI_STATUS\r
670EFIAPI\r
671PeiGetNextVariableName (\r
672 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
673 IN OUT UINTN *VariableNameSize,\r
674 IN OUT CHAR16 *VariableName,\r
675 IN OUT EFI_GUID *VariableGuid\r
676 )\r
8d3a5c82 677{\r
0f7aff72 678 VARIABLE_STORE_TYPE Type;\r
8d3a5c82 679 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 680 VARIABLE_POINTER_TRACK VariableInHob;\r
8d3a5c82 681 UINTN VarNameSize;\r
682 EFI_STATUS Status;\r
0f7aff72 683 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
8d3a5c82 684\r
01f352a7 685 if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {\r
8d3a5c82 686 return EFI_INVALID_PARAMETER;\r
687 }\r
688\r
0f7aff72 689 Status = FindVariable (VariableName, VariableGuid, &Variable);\r
8d3a5c82 690 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {\r
691 return Status;\r
692 }\r
693\r
694 if (VariableName[0] != 0) {\r
695 //\r
696 // If variable name is not NULL, get next variable\r
697 //\r
698 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
699 }\r
700\r
0f7aff72
RN
701 VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, NULL);\r
702 VariableStoreHeader[VariableStoreTypeNv] = GetVariableStore (VariableStoreTypeNv, NULL);\r
9cad030b 703\r
0f7aff72
RN
704 while (TRUE) {\r
705 //\r
706 // Switch from HOB to Non-Volatile.\r
707 //\r
708 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
709 (Variable.CurrPtr == NULL) ||\r
710 !IsValidVariableHeader (Variable.CurrPtr)\r
711 ) {\r
712 //\r
713 // Find current storage index\r
714 //\r
715 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
716 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
717 break;\r
718 }\r
719 }\r
720 ASSERT (Type < VariableStoreTypeMax);\r
721 //\r
722 // Switch to next storage\r
723 //\r
724 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
725 if (VariableStoreHeader[Type] != NULL) {\r
726 break;\r
727 }\r
728 }\r
729 //\r
730 // Capture the case that \r
731 // 1. current storage is the last one, or\r
732 // 2. no further storage\r
733 //\r
734 if (Type == VariableStoreTypeMax) {\r
735 return EFI_NOT_FOUND;\r
736 }\r
737 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
738 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
739 Variable.CurrPtr = Variable.StartPtr;\r
740 }\r
8d3a5c82 741\r
0f7aff72 742 if (Variable.CurrPtr->State == VAR_ADDED) {\r
8d3a5c82 743\r
0f7aff72
RN
744 //\r
745 // Don't return NV variable when HOB overrides it\r
746 //\r
747 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
748 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
749 ) {\r
750 Status = FindVariableEx (\r
751 VariableStoreHeader[VariableStoreTypeHob],\r
752 NULL,\r
753 GetVariableNamePtr (Variable.CurrPtr),\r
754 &Variable.CurrPtr->VendorGuid, \r
755 &VariableInHob\r
756 );\r
757 if (!EFI_ERROR (Status)) {\r
758 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
759 continue;\r
8d3a5c82 760 }\r
0f7aff72 761 }\r
8d3a5c82 762\r
0f7aff72
RN
763 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
764 ASSERT (VarNameSize != 0);\r
765\r
766 if (VarNameSize <= *VariableNameSize) {\r
767 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
768\r
769 CopyMem (VariableGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
770\r
771 Status = EFI_SUCCESS;\r
8d3a5c82 772 } else {\r
0f7aff72 773 Status = EFI_BUFFER_TOO_SMALL;\r
8d3a5c82 774 }\r
0f7aff72
RN
775\r
776 *VariableNameSize = VarNameSize;\r
777 //\r
778 // Variable is found\r
779 //\r
780 return Status;\r
8d3a5c82 781 } else {\r
0f7aff72 782 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
8d3a5c82 783 }\r
784 }\r
8d3a5c82 785}\r