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