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