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