]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/Pei/Variable.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / Pei / Variable.c
CommitLineData
504214c4 1/** @file\r
504214c4 2 Implement ReadOnly Variable Services required by PEIM and install\r
02c57ded 3 PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.\r
8d3a5c82 4\r
3c007f36 5Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
91a33d41 6Copyright (c) Microsoft Corporation.<BR>\r
9d510e61 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
8d3a5c82 8\r
504214c4 9**/\r
8d3a5c82 10\r
8d3a5c82 11#include "Variable.h"\r
12\r
13//\r
14// Module globals\r
15//\r
1436aea4 16EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi = {\r
8d3a5c82 17 PeiGetVariable,\r
18 PeiGetNextVariableName\r
19};\r
20\r
1436aea4 21EFI_PEI_PPI_DESCRIPTOR mPpiListVariable = {\r
8d3a5c82 22 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
23 &gEfiPeiReadOnlyVariable2PpiGuid,\r
24 &mVariablePpi\r
25};\r
26\r
33479ddf 27/**\r
8d3a5c82 28 Provide the functionality of the variable services.\r
77ba12cc
SZ
29\r
30 @param FileHandle Handle of the file being invoked.\r
721dfef3 31 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().\r
33479ddf 32 @param PeiServices General purpose services available to every PEIM.\r
8d3a5c82 33\r
33479ddf 34 @retval EFI_SUCCESS If the interface could be successfully installed\r
35 @retval Others Returned from PeiServicesInstallPpi()\r
33479ddf 36**/\r
37EFI_STATUS\r
38EFIAPI\r
39PeimInitializeVariableServices (\r
1436aea4
MK
40 IN EFI_PEI_FILE_HANDLE FileHandle,\r
41 IN CONST EFI_PEI_SERVICES **PeiServices\r
33479ddf 42 )\r
8d3a5c82 43{\r
33479ddf 44 return PeiServicesInstallPpi (&mPpiListVariable);\r
8d3a5c82 45}\r
46\r
33479ddf 47/**\r
9cad030b 48\r
7c80e839 49 Gets the pointer to the first variable header in given variable store area.\r
9cad030b 50\r
7c80e839 51 @param VarStoreHeader Pointer to the Variable Store Header.\r
52\r
4063d37c 53 @return Pointer to the first variable header.\r
9cad030b 54\r
33479ddf 55**/\r
56VARIABLE_HEADER *\r
57GetStartPointer (\r
1436aea4 58 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
33479ddf 59 )\r
9cad030b 60{\r
61 //\r
4063d37c 62 // The start of variable store\r
9cad030b 63 //\r
1436aea4 64 return (VARIABLE_HEADER *)HEADER_ALIGN (VarStoreHeader + 1);\r
9cad030b 65}\r
66\r
33479ddf 67/**\r
9cad030b 68\r
4063d37c
SZ
69 Gets the pointer to the end of the variable storage area.\r
70\r
71 This function gets pointer to the end of the variable storage\r
72 area, according to the input variable store header.\r
73\r
74 @param VarStoreHeader Pointer to the Variable Store Header.\r
9cad030b 75\r
4063d37c 76 @return Pointer to the end of the variable storage area.\r
9cad030b 77\r
33479ddf 78**/\r
79VARIABLE_HEADER *\r
80GetEndPointer (\r
1436aea4 81 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
33479ddf 82 )\r
9cad030b 83{\r
84 //\r
85 // The end of variable store\r
86 //\r
1436aea4 87 return (VARIABLE_HEADER *)HEADER_ALIGN ((UINTN)VarStoreHeader + VarStoreHeader->Size);\r
9cad030b 88}\r
89\r
33479ddf 90/**\r
8d3a5c82 91 This code checks if variable header is valid or not.\r
92\r
33479ddf 93 @param Variable Pointer to the Variable Header.\r
8d3a5c82 94\r
33479ddf 95 @retval TRUE Variable header is valid.\r
96 @retval FALSE Variable header is not valid.\r
8d3a5c82 97\r
33479ddf 98**/\r
99BOOLEAN\r
100IsValidVariableHeader (\r
1436aea4 101 IN VARIABLE_HEADER *Variable\r
33479ddf 102 )\r
8d3a5c82 103{\r
1436aea4 104 if ((Variable == NULL) || (Variable->StartId != VARIABLE_DATA)) {\r
130e2569 105 return FALSE;\r
106 }\r
107\r
108 return TRUE;\r
8d3a5c82 109}\r
110\r
77ba12cc
SZ
111/**\r
112 This code gets the size of variable header.\r
113\r
114 @param AuthFlag Authenticated variable flag.\r
115\r
116 @return Size of variable header in bytes in type UINTN.\r
117\r
118**/\r
119UINTN\r
120GetVariableHeaderSize (\r
1436aea4 121 IN BOOLEAN AuthFlag\r
77ba12cc
SZ
122 )\r
123{\r
1436aea4 124 UINTN Value;\r
77ba12cc
SZ
125\r
126 if (AuthFlag) {\r
127 Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
128 } else {\r
129 Value = sizeof (VARIABLE_HEADER);\r
130 }\r
131\r
132 return Value;\r
133}\r
130e2569 134\r
33479ddf 135/**\r
130e2569 136 This code gets the size of name of variable.\r
137\r
33479ddf 138 @param Variable Pointer to the Variable Header.\r
77ba12cc 139 @param AuthFlag Authenticated variable flag.\r
130e2569 140\r
33479ddf 141 @return Size of variable in bytes in type UINTN.\r
130e2569 142\r
33479ddf 143**/\r
144UINTN\r
145NameSizeOfVariable (\r
1436aea4
MK
146 IN VARIABLE_HEADER *Variable,\r
147 IN BOOLEAN AuthFlag\r
33479ddf 148 )\r
130e2569 149{\r
1436aea4 150 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
77ba12cc 151\r
1436aea4 152 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;\r
77ba12cc 153 if (AuthFlag) {\r
1436aea4
MK
154 if ((AuthVariable->State == (UINT8)(-1)) ||\r
155 (AuthVariable->DataSize == (UINT32)(-1)) ||\r
156 (AuthVariable->NameSize == (UINT32)(-1)) ||\r
157 (AuthVariable->Attributes == (UINT32)(-1)))\r
158 {\r
77ba12cc
SZ
159 return 0;\r
160 }\r
1436aea4
MK
161\r
162 return (UINTN)AuthVariable->NameSize;\r
77ba12cc 163 } else {\r
1436aea4
MK
164 if ((Variable->State == (UINT8)(-1)) ||\r
165 (Variable->DataSize == (UINT32)(-1)) ||\r
166 (Variable->NameSize == (UINT32)(-1)) ||\r
167 (Variable->Attributes == (UINT32)(-1)))\r
168 {\r
77ba12cc
SZ
169 return 0;\r
170 }\r
1436aea4
MK
171\r
172 return (UINTN)Variable->NameSize;\r
130e2569 173 }\r
130e2569 174}\r
175\r
33479ddf 176/**\r
177 This code gets the size of data of variable.\r
130e2569 178\r
33479ddf 179 @param Variable Pointer to the Variable Header.\r
77ba12cc 180 @param AuthFlag Authenticated variable flag.\r
130e2569 181\r
33479ddf 182 @return Size of variable in bytes in type UINTN.\r
130e2569 183\r
33479ddf 184**/\r
185UINTN\r
186DataSizeOfVariable (\r
1436aea4
MK
187 IN VARIABLE_HEADER *Variable,\r
188 IN BOOLEAN AuthFlag\r
33479ddf 189 )\r
130e2569 190{\r
1436aea4 191 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
77ba12cc 192\r
1436aea4 193 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;\r
77ba12cc 194 if (AuthFlag) {\r
1436aea4
MK
195 if ((AuthVariable->State == (UINT8)(-1)) ||\r
196 (AuthVariable->DataSize == (UINT32)(-1)) ||\r
197 (AuthVariable->NameSize == (UINT32)(-1)) ||\r
198 (AuthVariable->Attributes == (UINT32)(-1)))\r
199 {\r
77ba12cc
SZ
200 return 0;\r
201 }\r
1436aea4
MK
202\r
203 return (UINTN)AuthVariable->DataSize;\r
77ba12cc 204 } else {\r
1436aea4
MK
205 if ((Variable->State == (UINT8)(-1)) ||\r
206 (Variable->DataSize == (UINT32)(-1)) ||\r
207 (Variable->NameSize == (UINT32)(-1)) ||\r
208 (Variable->Attributes == (UINT32)(-1)))\r
209 {\r
77ba12cc
SZ
210 return 0;\r
211 }\r
1436aea4
MK
212\r
213 return (UINTN)Variable->DataSize;\r
130e2569 214 }\r
130e2569 215}\r
216\r
33479ddf 217/**\r
130e2569 218 This code gets the pointer to the variable name.\r
8d3a5c82 219\r
33479ddf 220 @param Variable Pointer to the Variable Header.\r
77ba12cc 221 @param AuthFlag Authenticated variable flag.\r
130e2569 222\r
33479ddf 223 @return A CHAR16* pointer to Variable Name.\r
130e2569 224\r
33479ddf 225**/\r
226CHAR16 *\r
227GetVariableNamePtr (\r
1436aea4
MK
228 IN VARIABLE_HEADER *Variable,\r
229 IN BOOLEAN AuthFlag\r
33479ddf 230 )\r
130e2569 231{\r
1436aea4 232 return (CHAR16 *)((UINTN)Variable + GetVariableHeaderSize (AuthFlag));\r
130e2569 233}\r
234\r
77ba12cc
SZ
235/**\r
236 This code gets the pointer to the variable guid.\r
237\r
238 @param Variable Pointer to the Variable Header.\r
239 @param AuthFlag Authenticated variable flag.\r
240\r
241 @return A EFI_GUID* pointer to Vendor Guid.\r
242\r
243**/\r
244EFI_GUID *\r
245GetVendorGuidPtr (\r
1436aea4
MK
246 IN VARIABLE_HEADER *Variable,\r
247 IN BOOLEAN AuthFlag\r
77ba12cc
SZ
248 )\r
249{\r
1436aea4 250 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
77ba12cc 251\r
1436aea4 252 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *)Variable;\r
77ba12cc
SZ
253 if (AuthFlag) {\r
254 return &AuthVariable->VendorGuid;\r
255 } else {\r
256 return &Variable->VendorGuid;\r
257 }\r
258}\r
130e2569 259\r
33479ddf 260/**\r
130e2569 261 This code gets the pointer to the variable data.\r
262\r
3e02ebb2
SZ
263 @param Variable Pointer to the Variable Header.\r
264 @param VariableHeader Pointer to the Variable Header that has consecutive content.\r
77ba12cc 265 @param AuthFlag Authenticated variable flag.\r
130e2569 266\r
33479ddf 267 @return A UINT8* pointer to Variable Data.\r
130e2569 268\r
33479ddf 269**/\r
270UINT8 *\r
271GetVariableDataPtr (\r
1436aea4
MK
272 IN VARIABLE_HEADER *Variable,\r
273 IN VARIABLE_HEADER *VariableHeader,\r
274 IN BOOLEAN AuthFlag\r
33479ddf 275 )\r
130e2569 276{\r
1436aea4 277 UINTN Value;\r
77ba12cc 278\r
130e2569 279 //\r
280 // Be careful about pad size for alignment\r
281 //\r
1436aea4 282 Value = (UINTN)GetVariableNamePtr (Variable, AuthFlag);\r
77ba12cc
SZ
283 Value += NameSizeOfVariable (VariableHeader, AuthFlag);\r
284 Value += GET_PAD_SIZE (NameSizeOfVariable (VariableHeader, AuthFlag));\r
130e2569 285\r
1436aea4 286 return (UINT8 *)Value;\r
130e2569 287}\r
288\r
33479ddf 289/**\r
130e2569 290 This code gets the pointer to the next variable header.\r
291\r
3e02ebb2
SZ
292 @param StoreInfo Pointer to variable store info structure.\r
293 @param Variable Pointer to the Variable Header.\r
294 @param VariableHeader Pointer to the Variable Header that has consecutive content.\r
130e2569 295\r
33479ddf 296 @return A VARIABLE_HEADER* pointer to next variable header.\r
8d3a5c82 297\r
33479ddf 298**/\r
299VARIABLE_HEADER *\r
300GetNextVariablePtr (\r
1436aea4
MK
301 IN VARIABLE_STORE_INFO *StoreInfo,\r
302 IN VARIABLE_HEADER *Variable,\r
303 IN VARIABLE_HEADER *VariableHeader\r
33479ddf 304 )\r
8d3a5c82 305{\r
3e02ebb2
SZ
306 EFI_PHYSICAL_ADDRESS TargetAddress;\r
307 EFI_PHYSICAL_ADDRESS SpareAddress;\r
308 UINTN Value;\r
130e2569 309\r
1436aea4 310 Value = (UINTN)GetVariableDataPtr (Variable, VariableHeader, StoreInfo->AuthFlag);\r
77ba12cc
SZ
311 Value += DataSizeOfVariable (VariableHeader, StoreInfo->AuthFlag);\r
312 Value += GET_PAD_SIZE (DataSizeOfVariable (VariableHeader, StoreInfo->AuthFlag));\r
130e2569 313 //\r
314 // Be careful about pad size for alignment\r
315 //\r
3e02ebb2
SZ
316 Value = HEADER_ALIGN (Value);\r
317\r
318 if (StoreInfo->FtwLastWriteData != NULL) {\r
319 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
1436aea4
MK
320 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
321 if (((UINTN)Variable < (UINTN)TargetAddress) && (Value >= (UINTN)TargetAddress)) {\r
3e02ebb2
SZ
322 //\r
323 // Next variable is in spare block.\r
324 //\r
1436aea4 325 Value = (UINTN)SpareAddress + (Value - (UINTN)TargetAddress);\r
3e02ebb2
SZ
326 }\r
327 }\r
328\r
1436aea4 329 return (VARIABLE_HEADER *)Value;\r
8d3a5c82 330}\r
331\r
33479ddf 332/**\r
3e02ebb2 333 Get variable store status.\r
33479ddf 334\r
335 @param VarStoreHeader Pointer to the Variable Store Header.\r
336\r
337 @retval EfiRaw Variable store is raw\r
338 @retval EfiValid Variable store is valid\r
339 @retval EfiInvalid Variable store is invalid\r
130e2569 340\r
33479ddf 341**/\r
8d3a5c82 342VARIABLE_STORE_STATUS\r
8d3a5c82 343GetVariableStoreStatus (\r
1436aea4 344 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
8d3a5c82 345 )\r
8d3a5c82 346{\r
77ba12cc
SZ
347 if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||\r
348 CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&\r
1436aea4
MK
349 (VarStoreHeader->Format == VARIABLE_STORE_FORMATTED) &&\r
350 (VarStoreHeader->State == VARIABLE_STORE_HEALTHY)\r
351 )\r
352 {\r
8d3a5c82 353 return EfiValid;\r
354 }\r
355\r
1436aea4
MK
356 if ((((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff) &&\r
357 (((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff) &&\r
358 (((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff) &&\r
359 (((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff) &&\r
360 (VarStoreHeader->Size == 0xffffffff) &&\r
361 (VarStoreHeader->Format == 0xff) &&\r
362 (VarStoreHeader->State == 0xff)\r
363 )\r
364 {\r
8d3a5c82 365 return EfiRaw;\r
366 } else {\r
367 return EfiInvalid;\r
368 }\r
369}\r
370\r
3e02ebb2
SZ
371/**\r
372 Compare two variable names, one of them may be inconsecutive.\r
373\r
374 @param StoreInfo Pointer to variable store info structure.\r
375 @param Name1 Pointer to one variable name.\r
376 @param Name2 Pointer to another variable name.\r
377 @param NameSize Variable name size.\r
378\r
379 @retval TRUE Name1 and Name2 are identical.\r
380 @retval FALSE Name1 and Name2 are not identical.\r
381\r
382**/\r
383BOOLEAN\r
384CompareVariableName (\r
1436aea4
MK
385 IN VARIABLE_STORE_INFO *StoreInfo,\r
386 IN CONST CHAR16 *Name1,\r
387 IN CONST CHAR16 *Name2,\r
388 IN UINTN NameSize\r
3e02ebb2
SZ
389 )\r
390{\r
391 EFI_PHYSICAL_ADDRESS TargetAddress;\r
392 EFI_PHYSICAL_ADDRESS SpareAddress;\r
393 UINTN PartialNameSize;\r
394\r
395 if (StoreInfo->FtwLastWriteData != NULL) {\r
396 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
1436aea4
MK
397 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
398 if (((UINTN)Name1 < (UINTN)TargetAddress) && (((UINTN)Name1 + NameSize) > (UINTN)TargetAddress)) {\r
3e02ebb2
SZ
399 //\r
400 // Name1 is inconsecutive.\r
401 //\r
1436aea4 402 PartialNameSize = (UINTN)TargetAddress - (UINTN)Name1;\r
3e02ebb2
SZ
403 //\r
404 // Partial content is in NV storage.\r
405 //\r
1436aea4 406 if (CompareMem ((UINT8 *)Name1, (UINT8 *)Name2, PartialNameSize) == 0) {\r
3e02ebb2
SZ
407 //\r
408 // Another partial content is in spare block.\r
409 //\r
1436aea4 410 if (CompareMem ((UINT8 *)(UINTN)SpareAddress, (UINT8 *)Name2 + PartialNameSize, NameSize - PartialNameSize) == 0) {\r
3e02ebb2
SZ
411 return TRUE;\r
412 }\r
413 }\r
1436aea4 414\r
3e02ebb2 415 return FALSE;\r
1436aea4 416 } else if (((UINTN)Name2 < (UINTN)TargetAddress) && (((UINTN)Name2 + NameSize) > (UINTN)TargetAddress)) {\r
3e02ebb2
SZ
417 //\r
418 // Name2 is inconsecutive.\r
419 //\r
1436aea4 420 PartialNameSize = (UINTN)TargetAddress - (UINTN)Name2;\r
3e02ebb2
SZ
421 //\r
422 // Partial content is in NV storage.\r
423 //\r
1436aea4 424 if (CompareMem ((UINT8 *)Name2, (UINT8 *)Name1, PartialNameSize) == 0) {\r
3e02ebb2
SZ
425 //\r
426 // Another partial content is in spare block.\r
427 //\r
1436aea4 428 if (CompareMem ((UINT8 *)(UINTN)SpareAddress, (UINT8 *)Name1 + PartialNameSize, NameSize - PartialNameSize) == 0) {\r
3e02ebb2
SZ
429 return TRUE;\r
430 }\r
431 }\r
1436aea4 432\r
3e02ebb2
SZ
433 return FALSE;\r
434 }\r
435 }\r
436\r
437 //\r
438 // Both Name1 and Name2 are consecutive.\r
439 //\r
1436aea4 440 if (CompareMem ((UINT8 *)Name1, (UINT8 *)Name2, NameSize) == 0) {\r
3e02ebb2
SZ
441 return TRUE;\r
442 }\r
1436aea4 443\r
3e02ebb2
SZ
444 return FALSE;\r
445}\r
33479ddf 446\r
447/**\r
448 This function compares a variable with variable entries in database.\r
449\r
3e02ebb2 450 @param StoreInfo Pointer to variable store info structure.\r
33479ddf 451 @param Variable Pointer to the variable in our database\r
3e02ebb2 452 @param VariableHeader Pointer to the Variable Header that has consecutive content.\r
33479ddf 453 @param VariableName Name of the variable to compare to 'Variable'\r
454 @param VendorGuid GUID of the variable to compare to 'Variable'\r
455 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
456\r
457 @retval EFI_SUCCESS Found match variable\r
458 @retval EFI_NOT_FOUND Variable not found\r
459\r
460**/\r
8d3a5c82 461EFI_STATUS\r
462CompareWithValidVariable (\r
1436aea4
MK
463 IN VARIABLE_STORE_INFO *StoreInfo,\r
464 IN VARIABLE_HEADER *Variable,\r
465 IN VARIABLE_HEADER *VariableHeader,\r
466 IN CONST CHAR16 *VariableName,\r
467 IN CONST EFI_GUID *VendorGuid,\r
468 OUT VARIABLE_POINTER_TRACK *PtrTrack\r
8d3a5c82 469 )\r
8d3a5c82 470{\r
77ba12cc
SZ
471 VOID *Point;\r
472 EFI_GUID *TempVendorGuid;\r
473\r
474 TempVendorGuid = GetVendorGuidPtr (VariableHeader, StoreInfo->AuthFlag);\r
130e2569 475\r
8d3a5c82 476 if (VariableName[0] == 0) {\r
477 PtrTrack->CurrPtr = Variable;\r
478 return EFI_SUCCESS;\r
479 } else {\r
480 //\r
481 // Don't use CompareGuid function here for performance reasons.\r
482 // Instead we compare the GUID a UINT32 at a time and branch\r
483 // on the first failed comparison.\r
484 //\r
1436aea4
MK
485 if ((((INT32 *)VendorGuid)[0] == ((INT32 *)TempVendorGuid)[0]) &&\r
486 (((INT32 *)VendorGuid)[1] == ((INT32 *)TempVendorGuid)[1]) &&\r
487 (((INT32 *)VendorGuid)[2] == ((INT32 *)TempVendorGuid)[2]) &&\r
488 (((INT32 *)VendorGuid)[3] == ((INT32 *)TempVendorGuid)[3])\r
489 )\r
490 {\r
77ba12cc 491 ASSERT (NameSizeOfVariable (VariableHeader, StoreInfo->AuthFlag) != 0);\r
1436aea4 492 Point = (VOID *)GetVariableNamePtr (Variable, StoreInfo->AuthFlag);\r
77ba12cc 493 if (CompareVariableName (StoreInfo, VariableName, Point, NameSizeOfVariable (VariableHeader, StoreInfo->AuthFlag))) {\r
8d3a5c82 494 PtrTrack->CurrPtr = Variable;\r
495 return EFI_SUCCESS;\r
496 }\r
497 }\r
498 }\r
499\r
500 return EFI_NOT_FOUND;\r
501}\r
502\r
09808bd3
SZ
503/**\r
504 Get HOB variable store.\r
505\r
5f463523
SZ
506 @param[out] StoreInfo Return the store info.\r
507 @param[out] VariableStoreHeader Return variable store header.\r
508\r
09808bd3
SZ
509**/\r
510VOID\r
511GetHobVariableStore (\r
1436aea4
MK
512 OUT VARIABLE_STORE_INFO *StoreInfo,\r
513 OUT VARIABLE_STORE_HEADER **VariableStoreHeader\r
09808bd3
SZ
514 )\r
515{\r
1436aea4 516 EFI_HOB_GUID_TYPE *GuidHob;\r
09808bd3 517\r
fdd3e77a
SZ
518 //\r
519 // Make sure there is no more than one Variable HOB.\r
520 //\r
db52c7f7 521 DEBUG_CODE_BEGIN ();\r
1436aea4
MK
522 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
523 if (GuidHob != NULL) {\r
524 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {\r
525 DEBUG ((DEBUG_ERROR, "ERROR: Found two Auth Variable HOBs\n"));\r
526 ASSERT (FALSE);\r
527 } else if (GetFirstGuidHob (&gEfiVariableGuid) != NULL) {\r
528 DEBUG ((DEBUG_ERROR, "ERROR: Found one Auth + one Normal Variable HOBs\n"));\r
529 ASSERT (FALSE);\r
530 }\r
531 } else {\r
532 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
fdd3e77a 533 if (GuidHob != NULL) {\r
1436aea4
MK
534 if ((GetNextGuidHob (&gEfiVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {\r
535 DEBUG ((DEBUG_ERROR, "ERROR: Found two Normal Variable HOBs\n"));\r
fdd3e77a 536 ASSERT (FALSE);\r
fdd3e77a
SZ
537 }\r
538 }\r
1436aea4
MK
539 }\r
540\r
db52c7f7 541 DEBUG_CODE_END ();\r
fdd3e77a 542\r
09808bd3
SZ
543 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
544 if (GuidHob != NULL) {\r
1436aea4
MK
545 *VariableStoreHeader = (VARIABLE_STORE_HEADER *)GET_GUID_HOB_DATA (GuidHob);\r
546 StoreInfo->AuthFlag = TRUE;\r
09808bd3
SZ
547 } else {\r
548 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
549 if (GuidHob != NULL) {\r
1436aea4
MK
550 *VariableStoreHeader = (VARIABLE_STORE_HEADER *)GET_GUID_HOB_DATA (GuidHob);\r
551 StoreInfo->AuthFlag = FALSE;\r
09808bd3
SZ
552 }\r
553 }\r
554}\r
555\r
0f7aff72 556/**\r
3e02ebb2 557 Return the variable store header and the store info based on the Index.\r
0f7aff72 558\r
4efa9e59 559 @param Type The type of the variable store.\r
3e02ebb2 560 @param StoreInfo Return the store info.\r
0f7aff72
RN
561\r
562 @return Pointer to the variable store header.\r
563**/\r
564VARIABLE_STORE_HEADER *\r
565GetVariableStore (\r
1436aea4
MK
566 IN VARIABLE_STORE_TYPE Type,\r
567 OUT VARIABLE_STORE_INFO *StoreInfo\r
0f7aff72
RN
568 )\r
569{\r
3e02ebb2
SZ
570 EFI_HOB_GUID_TYPE *GuidHob;\r
571 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
572 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
573 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
574 UINT32 NvStorageSize;\r
575 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
576 UINT32 BackUpOffset;\r
577\r
1436aea4 578 StoreInfo->IndexTable = NULL;\r
3e02ebb2 579 StoreInfo->FtwLastWriteData = NULL;\r
1436aea4
MK
580 StoreInfo->AuthFlag = FALSE;\r
581 VariableStoreHeader = NULL;\r
0f7aff72
RN
582 switch (Type) {\r
583 case VariableStoreTypeHob:\r
09808bd3
SZ
584 GetHobVariableStore (StoreInfo, &VariableStoreHeader);\r
585\r
0f7aff72
RN
586 break;\r
587\r
588 case VariableStoreTypeNv:\r
3c007f36 589 if (!PcdGetBool (PcdEmuVariableNvModeEnable)) {\r
0f7aff72 590 //\r
3c007f36 591 // Emulated non-volatile variable mode is not enabled.\r
0f7aff72 592 //\r
3e02ebb2
SZ
593\r
594 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
1436aea4
MK
595 NvStorageBase = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ?\r
596 PcdGet64 (PcdFlashNvStorageVariableBase64) :\r
597 PcdGet32 (PcdFlashNvStorageVariableBase)\r
3e02ebb2 598 );\r
3c007f36
SZ
599 ASSERT (NvStorageBase != 0);\r
600\r
3e02ebb2
SZ
601 //\r
602 // First let FvHeader point to NV storage base.\r
603 //\r
1436aea4 604 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)NvStorageBase;\r
3e02ebb2
SZ
605\r
606 //\r
607 // Check the FTW last write data hob.\r
608 //\r
609 BackUpOffset = 0;\r
1436aea4 610 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3e02ebb2 611 if (GuidHob != NULL) {\r
1436aea4 612 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *)GET_GUID_HOB_DATA (GuidHob);\r
3e02ebb2
SZ
613 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
614 //\r
615 // Let FvHeader point to spare block.\r
616 //\r
1436aea4
MK
617 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FtwLastWriteData->SpareAddress;\r
618 DEBUG ((DEBUG_INFO, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN)FtwLastWriteData->SpareAddress));\r
3e02ebb2
SZ
619 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) && (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
620 StoreInfo->FtwLastWriteData = FtwLastWriteData;\r
621 //\r
622 // Flash NV storage from the offset is backed up in spare block.\r
623 //\r
1436aea4
MK
624 BackUpOffset = (UINT32)(FtwLastWriteData->TargetAddress - NvStorageBase);\r
625 DEBUG ((DEBUG_INFO, "PeiVariable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN)FtwLastWriteData->SpareAddress));\r
3e02ebb2
SZ
626 //\r
627 // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.\r
628 //\r
629 }\r
630 }\r
d6550260 631\r
632 //\r
633 // Check if the Firmware Volume is not corrupted\r
634 //\r
635 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
87000d77 636 DEBUG ((DEBUG_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
d6550260 637 break;\r
638 }\r
639\r
1436aea4 640 VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINT8 *)FvHeader + FvHeader->HeaderLength);\r
0f7aff72 641\r
1436aea4 642 StoreInfo->AuthFlag = (BOOLEAN)(CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid));\r
77ba12cc 643\r
3e02ebb2
SZ
644 GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);\r
645 if (GuidHob != NULL) {\r
646 StoreInfo->IndexTable = GET_GUID_HOB_DATA (GuidHob);\r
647 } else {\r
648 //\r
649 // If it's the first time to access variable region in flash, create a guid hob to record\r
650 // VAR_ADDED type variable info.\r
77ba12cc 651 // Note that as the resource of PEI phase is limited, only store the limited number of\r
3e02ebb2
SZ
652 // VAR_ADDED type variables to reduce access time.\r
653 //\r
1436aea4 654 StoreInfo->IndexTable = (VARIABLE_INDEX_TABLE *)BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));\r
3e02ebb2
SZ
655 StoreInfo->IndexTable->Length = 0;\r
656 StoreInfo->IndexTable->StartPtr = GetStartPointer (VariableStoreHeader);\r
1436aea4 657 StoreInfo->IndexTable->EndPtr = GetEndPointer (VariableStoreHeader);\r
3e02ebb2 658 StoreInfo->IndexTable->GoneThrough = 0;\r
0f7aff72
RN
659 }\r
660 }\r
1436aea4 661\r
0f7aff72 662 break;\r
de2a15ee
RN
663\r
664 default:\r
665 ASSERT (FALSE);\r
666 break;\r
0f7aff72
RN
667 }\r
668\r
3e02ebb2 669 StoreInfo->VariableStoreHeader = VariableStoreHeader;\r
0f7aff72
RN
670 return VariableStoreHeader;\r
671}\r
33479ddf 672\r
3e02ebb2
SZ
673/**\r
674 Get variable header that has consecutive content.\r
675\r
676 @param StoreInfo Pointer to variable store info structure.\r
677 @param Variable Pointer to the Variable Header.\r
678 @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.\r
679\r
680 @retval TRUE Variable header is valid.\r
681 @retval FALSE Variable header is not valid.\r
682\r
683**/\r
684BOOLEAN\r
685GetVariableHeader (\r
1436aea4
MK
686 IN VARIABLE_STORE_INFO *StoreInfo,\r
687 IN VARIABLE_HEADER *Variable,\r
688 OUT VARIABLE_HEADER **VariableHeader\r
3e02ebb2
SZ
689 )\r
690{\r
691 EFI_PHYSICAL_ADDRESS TargetAddress;\r
692 EFI_PHYSICAL_ADDRESS SpareAddress;\r
693 EFI_HOB_GUID_TYPE *GuidHob;\r
694 UINTN PartialHeaderSize;\r
695\r
6ebffb67
SZ
696 if (Variable == NULL) {\r
697 return FALSE;\r
698 }\r
699\r
77ba12cc
SZ
700 //\r
701 // First assume variable header pointed by Variable is consecutive.\r
702 //\r
3e02ebb2
SZ
703 *VariableHeader = Variable;\r
704\r
6ebffb67 705 if (StoreInfo->FtwLastWriteData != NULL) {\r
3e02ebb2 706 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
1436aea4
MK
707 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
708 if (((UINTN)Variable > (UINTN)SpareAddress) &&\r
709 (((UINTN)Variable - (UINTN)SpareAddress + (UINTN)TargetAddress) >= (UINTN)GetEndPointer (StoreInfo->VariableStoreHeader)))\r
710 {\r
6ebffb67
SZ
711 //\r
712 // Reach the end of variable store.\r
713 //\r
714 return FALSE;\r
715 }\r
1436aea4
MK
716\r
717 if (((UINTN)Variable < (UINTN)TargetAddress) && (((UINTN)Variable + GetVariableHeaderSize (StoreInfo->AuthFlag)) > (UINTN)TargetAddress)) {\r
3e02ebb2
SZ
718 //\r
719 // Variable header pointed by Variable is inconsecutive,\r
720 // create a guid hob to combine the two partial variable header content together.\r
721 //\r
722 GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);\r
723 if (GuidHob != NULL) {\r
1436aea4 724 *VariableHeader = (VARIABLE_HEADER *)GET_GUID_HOB_DATA (GuidHob);\r
3e02ebb2 725 } else {\r
1436aea4
MK
726 *VariableHeader = (VARIABLE_HEADER *)BuildGuidHob (&gEfiCallerIdGuid, GetVariableHeaderSize (StoreInfo->AuthFlag));\r
727 PartialHeaderSize = (UINTN)TargetAddress - (UINTN)Variable;\r
3e02ebb2
SZ
728 //\r
729 // Partial content is in NV storage.\r
730 //\r
1436aea4 731 CopyMem ((UINT8 *)*VariableHeader, (UINT8 *)Variable, PartialHeaderSize);\r
3e02ebb2
SZ
732 //\r
733 // Another partial content is in spare block.\r
734 //\r
1436aea4 735 CopyMem ((UINT8 *)*VariableHeader + PartialHeaderSize, (UINT8 *)(UINTN)SpareAddress, GetVariableHeaderSize (StoreInfo->AuthFlag) - PartialHeaderSize);\r
3e02ebb2
SZ
736 }\r
737 }\r
6ebffb67
SZ
738 } else {\r
739 if (Variable >= GetEndPointer (StoreInfo->VariableStoreHeader)) {\r
740 //\r
741 // Reach the end of variable store.\r
742 //\r
743 return FALSE;\r
744 }\r
3e02ebb2
SZ
745 }\r
746\r
747 return IsValidVariableHeader (*VariableHeader);\r
748}\r
749\r
750/**\r
751 Get variable name or data to output buffer.\r
752\r
753 @param StoreInfo Pointer to variable store info structure.\r
754 @param NameOrData Pointer to the variable name/data that may be inconsecutive.\r
755 @param Size Variable name/data size.\r
756 @param Buffer Pointer to output buffer to hold the variable name/data.\r
757\r
758**/\r
759VOID\r
760GetVariableNameOrData (\r
1436aea4
MK
761 IN VARIABLE_STORE_INFO *StoreInfo,\r
762 IN UINT8 *NameOrData,\r
763 IN UINTN Size,\r
764 OUT UINT8 *Buffer\r
3e02ebb2
SZ
765 )\r
766{\r
767 EFI_PHYSICAL_ADDRESS TargetAddress;\r
768 EFI_PHYSICAL_ADDRESS SpareAddress;\r
769 UINTN PartialSize;\r
77ba12cc 770\r
3e02ebb2
SZ
771 if (StoreInfo->FtwLastWriteData != NULL) {\r
772 TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
1436aea4
MK
773 SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
774 if (((UINTN)NameOrData < (UINTN)TargetAddress) && (((UINTN)NameOrData + Size) > (UINTN)TargetAddress)) {\r
3e02ebb2
SZ
775 //\r
776 // Variable name/data is inconsecutive.\r
777 //\r
1436aea4 778 PartialSize = (UINTN)TargetAddress - (UINTN)NameOrData;\r
3e02ebb2
SZ
779 //\r
780 // Partial content is in NV storage.\r
781 //\r
782 CopyMem (Buffer, NameOrData, PartialSize);\r
783 //\r
784 // Another partial content is in spare block.\r
785 //\r
1436aea4 786 CopyMem (Buffer + PartialSize, (UINT8 *)(UINTN)SpareAddress, Size - PartialSize);\r
3e02ebb2
SZ
787 return;\r
788 }\r
789 }\r
790\r
791 //\r
792 // Variable name/data is consecutive.\r
793 //\r
794 CopyMem (Buffer, NameOrData, Size);\r
795}\r
796\r
33479ddf 797/**\r
0f7aff72 798 Find the variable in the specified variable store.\r
33479ddf 799\r
3e02ebb2 800 @param StoreInfo Pointer to the store info structure.\r
0f7aff72
RN
801 @param VariableName Name of the variable to be found\r
802 @param VendorGuid Vendor GUID to be found.\r
803 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
33479ddf 804\r
805 @retval EFI_SUCCESS Variable found successfully\r
806 @retval EFI_NOT_FOUND Variable not found\r
807 @retval EFI_INVALID_PARAMETER Invalid variable name\r
808\r
809**/\r
8d3a5c82 810EFI_STATUS\r
0f7aff72 811FindVariableEx (\r
1436aea4
MK
812 IN VARIABLE_STORE_INFO *StoreInfo,\r
813 IN CONST CHAR16 *VariableName,\r
814 IN CONST EFI_GUID *VendorGuid,\r
815 OUT VARIABLE_POINTER_TRACK *PtrTrack\r
8d3a5c82 816 )\r
8d3a5c82 817{\r
1436aea4
MK
818 VARIABLE_HEADER *Variable;\r
819 VARIABLE_HEADER *LastVariable;\r
820 VARIABLE_HEADER *MaxIndex;\r
821 UINTN Index;\r
822 UINTN Offset;\r
823 BOOLEAN StopRecord;\r
824 VARIABLE_HEADER *InDeletedVariable;\r
825 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
826 VARIABLE_INDEX_TABLE *IndexTable;\r
827 VARIABLE_HEADER *VariableHeader;\r
3e02ebb2
SZ
828\r
829 VariableStoreHeader = StoreInfo->VariableStoreHeader;\r
8d3a5c82 830\r
0f7aff72 831 if (VariableStoreHeader == NULL) {\r
8d3a5c82 832 return EFI_INVALID_PARAMETER;\r
833 }\r
0f7aff72
RN
834\r
835 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
836 return EFI_UNSUPPORTED;\r
837 }\r
838\r
839 if (~VariableStoreHeader->Size == 0) {\r
840 return EFI_NOT_FOUND;\r
841 }\r
842\r
1436aea4 843 IndexTable = StoreInfo->IndexTable;\r
0f7aff72 844 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);\r
1436aea4 845 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);\r
0f7aff72 846\r
932e0f66
SZ
847 InDeletedVariable = NULL;\r
848\r
8d3a5c82 849 //\r
850 // No Variable Address equals zero, so 0 as initial value is safe.\r
851 //\r
1436aea4 852 MaxIndex = NULL;\r
3e02ebb2 853 VariableHeader = NULL;\r
8d3a5c82 854\r
0f7aff72 855 if (IndexTable != NULL) {\r
841014ba 856 //\r
0f7aff72
RN
857 // traverse the variable index table to look for varible.\r
858 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.\r
841014ba 859 //\r
0f7aff72
RN
860 for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {\r
861 ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));\r
1436aea4
MK
862 Offset += IndexTable->Index[Index];\r
863 MaxIndex = (VARIABLE_HEADER *)((UINT8 *)IndexTable->StartPtr + Offset);\r
3e02ebb2
SZ
864 GetVariableHeader (StoreInfo, MaxIndex, &VariableHeader);\r
865 if (CompareWithValidVariable (StoreInfo, MaxIndex, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
866 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
932e0f66
SZ
867 InDeletedVariable = PtrTrack->CurrPtr;\r
868 } else {\r
869 return EFI_SUCCESS;\r
870 }\r
8d3a5c82 871 }\r
872 }\r
873\r
33479ddf 874 if (IndexTable->GoneThrough != 0) {\r
0f7aff72 875 //\r
932e0f66 876 // If the table has all the existing variables indexed, return.\r
0f7aff72 877 //\r
932e0f66
SZ
878 PtrTrack->CurrPtr = InDeletedVariable;\r
879 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
8d3a5c82 880 }\r
881 }\r
0f7aff72 882\r
8d3a5c82 883 if (MaxIndex != NULL) {\r
0f7aff72
RN
884 //\r
885 // HOB exists but the variable cannot be found in HOB\r
886 // If not found in HOB, then let's start from the MaxIndex we've found.\r
887 //\r
3e02ebb2 888 Variable = GetNextVariablePtr (StoreInfo, MaxIndex, VariableHeader);\r
841014ba 889 LastVariable = MaxIndex;\r
8d3a5c82 890 } else {\r
0f7aff72
RN
891 //\r
892 // Start Pointers for the variable.\r
893 // Actual Data Pointer where data can be written.\r
894 //\r
895 Variable = PtrTrack->StartPtr;\r
896 LastVariable = PtrTrack->StartPtr;\r
8d3a5c82 897 }\r
0f7aff72 898\r
8d3a5c82 899 //\r
932e0f66 900 // Find the variable by walk through variable store\r
8d3a5c82 901 //\r
0f7aff72 902 StopRecord = FALSE;\r
3e02ebb2 903 while (GetVariableHeader (StoreInfo, Variable, &VariableHeader)) {\r
1436aea4 904 if ((VariableHeader->State == VAR_ADDED) || (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED))) {\r
8d3a5c82 905 //\r
906 // Record Variable in VariableIndex HOB\r
907 //\r
0f7aff72 908 if ((IndexTable != NULL) && !StopRecord) {\r
1436aea4 909 Offset = (UINTN)Variable - (UINTN)LastVariable;\r
121955fd 910 if ((Offset > 0x0FFFF) || (IndexTable->Length >= sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {\r
0f7aff72
RN
911 //\r
912 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),\r
913 // or the record buffer is full.\r
914 //\r
841014ba 915 StopRecord = TRUE;\r
0f7aff72 916 } else {\r
1436aea4
MK
917 IndexTable->Index[IndexTable->Length++] = (UINT16)Offset;\r
918 LastVariable = Variable;\r
841014ba 919 }\r
841014ba 920 }\r
921\r
3e02ebb2
SZ
922 if (CompareWithValidVariable (StoreInfo, Variable, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
923 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
932e0f66
SZ
924 InDeletedVariable = PtrTrack->CurrPtr;\r
925 } else {\r
926 return EFI_SUCCESS;\r
927 }\r
8d3a5c82 928 }\r
929 }\r
930\r
3e02ebb2 931 Variable = GetNextVariablePtr (StoreInfo, Variable, VariableHeader);\r
8d3a5c82 932 }\r
1436aea4 933\r
8d3a5c82 934 //\r
935 // If gone through the VariableStore, that means we never find in Firmware any more.\r
936 //\r
0f7aff72 937 if ((IndexTable != NULL) && !StopRecord) {\r
8d3a5c82 938 IndexTable->GoneThrough = 1;\r
939 }\r
940\r
932e0f66 941 PtrTrack->CurrPtr = InDeletedVariable;\r
8d3a5c82 942\r
932e0f66 943 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
8d3a5c82 944}\r
945\r
0f7aff72
RN
946/**\r
947 Find the variable in HOB and Non-Volatile variable storages.\r
948\r
949 @param VariableName Name of the variable to be found\r
950 @param VendorGuid Vendor GUID to be found.\r
951 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
3e02ebb2 952 @param StoreInfo Return the store info.\r
0f7aff72
RN
953\r
954 @retval EFI_SUCCESS Variable found successfully\r
955 @retval EFI_NOT_FOUND Variable not found\r
956 @retval EFI_INVALID_PARAMETER Invalid variable name\r
957**/\r
958EFI_STATUS\r
959FindVariable (\r
960 IN CONST CHAR16 *VariableName,\r
961 IN CONST EFI_GUID *VendorGuid,\r
3e02ebb2
SZ
962 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
963 OUT VARIABLE_STORE_INFO *StoreInfo\r
0f7aff72
RN
964 )\r
965{\r
1436aea4
MK
966 EFI_STATUS Status;\r
967 VARIABLE_STORE_TYPE Type;\r
0f7aff72 968\r
1436aea4 969 if ((VariableName[0] != 0) && (VendorGuid == NULL)) {\r
0f7aff72
RN
970 return EFI_INVALID_PARAMETER;\r
971 }\r
972\r
1436aea4 973 for (Type = (VARIABLE_STORE_TYPE)0; Type < VariableStoreTypeMax; Type++) {\r
3e02ebb2 974 GetVariableStore (Type, StoreInfo);\r
0f7aff72 975 Status = FindVariableEx (\r
3e02ebb2 976 StoreInfo,\r
0f7aff72 977 VariableName,\r
77ba12cc 978 VendorGuid,\r
0f7aff72
RN
979 PtrTrack\r
980 );\r
981 if (!EFI_ERROR (Status)) {\r
982 return Status;\r
983 }\r
984 }\r
985\r
986 return EFI_NOT_FOUND;\r
987}\r
988\r
33479ddf 989/**\r
990 This service retrieves a variable's value using its name and GUID.\r
991\r
77ba12cc 992 Read the specified variable from the UEFI variable store. If the Data\r
33479ddf 993 buffer is too small to hold the contents of the variable, the error\r
994 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer\r
995 size to obtain the data.\r
996\r
997 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
998 @param VariableName A pointer to a null-terminated string that is the variable's name.\r
999 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of\r
1000 VariableGuid and VariableName must be unique.\r
1001 @param Attributes If non-NULL, on return, points to the variable's attributes.\r
1002 @param DataSize On entry, points to the size in bytes of the Data buffer.\r
1003 On return, points to the size of the data returned in Data.\r
1004 @param Data Points to the buffer which will hold the returned variable value.\r
9977995e 1005 May be NULL with a zero DataSize in order to determine the size of the buffer needed.\r
33479ddf 1006\r
1007 @retval EFI_SUCCESS The variable was read successfully.\r
9977995e 1008 @retval EFI_NOT_FOUND The variable was be found.\r
77ba12cc
SZ
1009 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.\r
1010 DataSize is updated with the size required for\r
33479ddf 1011 the specified variable.\r
1012 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.\r
1013 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.\r
1014\r
1015**/\r
8d3a5c82 1016EFI_STATUS\r
1017EFIAPI\r
1018PeiGetVariable (\r
1436aea4
MK
1019 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
1020 IN CONST CHAR16 *VariableName,\r
1021 IN CONST EFI_GUID *VariableGuid,\r
1022 OUT UINT32 *Attributes,\r
1023 IN OUT UINTN *DataSize,\r
1024 OUT VOID *Data OPTIONAL\r
8d3a5c82 1025 )\r
8d3a5c82 1026{\r
1027 VARIABLE_POINTER_TRACK Variable;\r
1028 UINTN VarDataSize;\r
1029 EFI_STATUS Status;\r
3e02ebb2
SZ
1030 VARIABLE_STORE_INFO StoreInfo;\r
1031 VARIABLE_HEADER *VariableHeader;\r
8d3a5c82 1032\r
1436aea4 1033 if ((VariableName == NULL) || (VariableGuid == NULL) || (DataSize == NULL)) {\r
8d3a5c82 1034 return EFI_INVALID_PARAMETER;\r
1035 }\r
4249fa76 1036\r
e19eab61
SZ
1037 if (VariableName[0] == 0) {\r
1038 return EFI_NOT_FOUND;\r
1039 }\r
1040\r
eb774e2e
SZ
1041 VariableHeader = NULL;\r
1042\r
8d3a5c82 1043 //\r
1044 // Find existing variable\r
1045 //\r
3e02ebb2 1046 Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);\r
0f7aff72 1047 if (EFI_ERROR (Status)) {\r
8d3a5c82 1048 return Status;\r
1049 }\r
1436aea4 1050\r
3e02ebb2
SZ
1051 GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);\r
1052\r
8d3a5c82 1053 //\r
1054 // Get data size\r
1055 //\r
77ba12cc 1056 VarDataSize = DataSizeOfVariable (VariableHeader, StoreInfo.AuthFlag);\r
8d3a5c82 1057 if (*DataSize >= VarDataSize) {\r
39aea48d 1058 if (Data == NULL) {\r
1059 return EFI_INVALID_PARAMETER;\r
1060 }\r
1061\r
77ba12cc 1062 GetVariableNameOrData (&StoreInfo, GetVariableDataPtr (Variable.CurrPtr, VariableHeader, StoreInfo.AuthFlag), VarDataSize, Data);\r
91a33d41 1063 Status = EFI_SUCCESS;\r
8d3a5c82 1064 } else {\r
91a33d41 1065 Status = EFI_BUFFER_TOO_SMALL;\r
8d3a5c82 1066 }\r
91a33d41
MK
1067\r
1068 if (Attributes != NULL) {\r
1069 *Attributes = VariableHeader->Attributes;\r
1070 }\r
1436aea4 1071\r
91a33d41
MK
1072 *DataSize = VarDataSize;\r
1073\r
1074 return Status;\r
8d3a5c82 1075}\r
1076\r
33479ddf 1077/**\r
1078 Return the next variable name and GUID.\r
1079\r
77ba12cc
SZ
1080 This function is called multiple times to retrieve the VariableName\r
1081 and VariableGuid of all variables currently available in the system.\r
1082 On each call, the previous results are passed into the interface,\r
1083 and, on return, the interface returns the data for the next\r
1084 interface. When the entire variable list has been returned,\r
33479ddf 1085 EFI_NOT_FOUND is returned.\r
1086\r
1087 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
1088\r
1089 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.\r
f7dab9fa 1090 On return, the size of the variable name buffer.\r
33479ddf 1091 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.\r
1092 On return, points to the next variable's null-terminated name string.\r
77ba12cc 1093 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.\r
33479ddf 1094 On return, a pointer to the next variable's GUID.\r
1095\r
1096 @retval EFI_SUCCESS The variable was read successfully.\r
1097 @retval EFI_NOT_FOUND The variable could not be found.\r
1098 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting\r
1099 data. VariableNameSize is updated with the size\r
1100 required for the specified variable.\r
1101 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or\r
1102 VariableNameSize is NULL.\r
1103 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.\r
1104\r
1105**/\r
8d3a5c82 1106EFI_STATUS\r
1107EFIAPI\r
1108PeiGetNextVariableName (\r
1436aea4
MK
1109 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
1110 IN OUT UINTN *VariableNameSize,\r
1111 IN OUT CHAR16 *VariableName,\r
1112 IN OUT EFI_GUID *VariableGuid\r
8d3a5c82 1113 )\r
8d3a5c82 1114{\r
0f7aff72 1115 VARIABLE_STORE_TYPE Type;\r
8d3a5c82 1116 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 1117 VARIABLE_POINTER_TRACK VariableInHob;\r
932e0f66 1118 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
8d3a5c82 1119 UINTN VarNameSize;\r
1120 EFI_STATUS Status;\r
0f7aff72 1121 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
3e02ebb2
SZ
1122 VARIABLE_HEADER *VariableHeader;\r
1123 VARIABLE_STORE_INFO StoreInfo;\r
1124 VARIABLE_STORE_INFO StoreInfoForNv;\r
1125 VARIABLE_STORE_INFO StoreInfoForHob;\r
8d3a5c82 1126\r
1436aea4 1127 if ((VariableName == NULL) || (VariableGuid == NULL) || (VariableNameSize == NULL)) {\r
8d3a5c82 1128 return EFI_INVALID_PARAMETER;\r
1129 }\r
1130\r
eb774e2e
SZ
1131 VariableHeader = NULL;\r
1132\r
3e02ebb2 1133 Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);\r
1436aea4 1134 if ((Variable.CurrPtr == NULL) || (Status != EFI_SUCCESS)) {\r
8d3a5c82 1135 return Status;\r
1136 }\r
1137\r
1138 if (VariableName[0] != 0) {\r
1139 //\r
1140 // If variable name is not NULL, get next variable\r
1141 //\r
3e02ebb2
SZ
1142 GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);\r
1143 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
8d3a5c82 1144 }\r
1145\r
3e02ebb2
SZ
1146 VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, &StoreInfoForHob);\r
1147 VariableStoreHeader[VariableStoreTypeNv] = GetVariableStore (VariableStoreTypeNv, &StoreInfoForNv);\r
9cad030b 1148\r
0f7aff72
RN
1149 while (TRUE) {\r
1150 //\r
1151 // Switch from HOB to Non-Volatile.\r
1152 //\r
3e02ebb2 1153 while (!GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader)) {\r
0f7aff72
RN
1154 //\r
1155 // Find current storage index\r
1156 //\r
1436aea4 1157 for (Type = (VARIABLE_STORE_TYPE)0; Type < VariableStoreTypeMax; Type++) {\r
0f7aff72
RN
1158 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
1159 break;\r
1160 }\r
1161 }\r
1436aea4 1162\r
0f7aff72
RN
1163 ASSERT (Type < VariableStoreTypeMax);\r
1164 //\r
1165 // Switch to next storage\r
1166 //\r
1167 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
1168 if (VariableStoreHeader[Type] != NULL) {\r
1169 break;\r
1170 }\r
1171 }\r
1436aea4 1172\r
0f7aff72 1173 //\r
77ba12cc 1174 // Capture the case that\r
0f7aff72
RN
1175 // 1. current storage is the last one, or\r
1176 // 2. no further storage\r
1177 //\r
1178 if (Type == VariableStoreTypeMax) {\r
1179 return EFI_NOT_FOUND;\r
1180 }\r
1436aea4 1181\r
0f7aff72 1182 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
1436aea4 1183 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
0f7aff72 1184 Variable.CurrPtr = Variable.StartPtr;\r
3e02ebb2 1185 GetVariableStore (Type, &StoreInfo);\r
0f7aff72 1186 }\r
8d3a5c82 1187\r
1436aea4 1188 if ((VariableHeader->State == VAR_ADDED) || (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED))) {\r
3e02ebb2 1189 if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
932e0f66
SZ
1190 //\r
1191 // If it is a IN_DELETED_TRANSITION variable,\r
1192 // and there is also a same ADDED one at the same time,\r
1193 // don't return it.\r
1194 //\r
932e0f66 1195 Status = FindVariableEx (\r
3e02ebb2 1196 &StoreInfo,\r
77ba12cc
SZ
1197 GetVariableNamePtr (Variable.CurrPtr, StoreInfo.AuthFlag),\r
1198 GetVendorGuidPtr (VariableHeader, StoreInfo.AuthFlag),\r
932e0f66
SZ
1199 &VariablePtrTrack\r
1200 );\r
1436aea4 1201 if (!EFI_ERROR (Status) && (VariablePtrTrack.CurrPtr != Variable.CurrPtr)) {\r
3e02ebb2 1202 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
932e0f66
SZ
1203 continue;\r
1204 }\r
1205 }\r
8d3a5c82 1206\r
0f7aff72
RN
1207 //\r
1208 // Don't return NV variable when HOB overrides it\r
1209 //\r
1210 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
1211 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
1436aea4
MK
1212 )\r
1213 {\r
0f7aff72 1214 Status = FindVariableEx (\r
3e02ebb2 1215 &StoreInfoForHob,\r
77ba12cc
SZ
1216 GetVariableNamePtr (Variable.CurrPtr, StoreInfo.AuthFlag),\r
1217 GetVendorGuidPtr (VariableHeader, StoreInfo.AuthFlag),\r
0f7aff72
RN
1218 &VariableInHob\r
1219 );\r
1220 if (!EFI_ERROR (Status)) {\r
3e02ebb2 1221 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
0f7aff72 1222 continue;\r
8d3a5c82 1223 }\r
0f7aff72 1224 }\r
8d3a5c82 1225\r
77ba12cc 1226 VarNameSize = NameSizeOfVariable (VariableHeader, StoreInfo.AuthFlag);\r
0f7aff72
RN
1227 ASSERT (VarNameSize != 0);\r
1228\r
1229 if (VarNameSize <= *VariableNameSize) {\r
1436aea4 1230 GetVariableNameOrData (&StoreInfo, (UINT8 *)GetVariableNamePtr (Variable.CurrPtr, StoreInfo.AuthFlag), VarNameSize, (UINT8 *)VariableName);\r
0f7aff72 1231\r
77ba12cc 1232 CopyMem (VariableGuid, GetVendorGuidPtr (VariableHeader, StoreInfo.AuthFlag), sizeof (EFI_GUID));\r
0f7aff72
RN
1233\r
1234 Status = EFI_SUCCESS;\r
8d3a5c82 1235 } else {\r
0f7aff72 1236 Status = EFI_BUFFER_TOO_SMALL;\r
8d3a5c82 1237 }\r
0f7aff72
RN
1238\r
1239 *VariableNameSize = VarNameSize;\r
1240 //\r
1241 // Variable is found\r
1242 //\r
1243 return Status;\r
8d3a5c82 1244 } else {\r
3e02ebb2 1245 Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
8d3a5c82 1246 }\r
1247 }\r
8d3a5c82 1248}\r