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