]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
SecurityPkg Variable: Introduce PcdReclaimVariableSpaceAtEndOfDxe
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / Variable.c
CommitLineData
0c18794e 1/** @file\r
021a1af9 2 The common variable operation routines shared by DXE_RUNTIME variable\r
0c18794e 3 module and DXE_SMM variable module.\r
4\r
dc204d5a
JY
5 Caution: This module requires additional review when modified.\r
6 This driver will have external input - variable data. They may be input in SMM mode.\r
7 This external input must be validated carefully to avoid security issue like\r
8 buffer overflow, integer overflow.\r
9\r
10 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.\r
11 They need check input parameter.\r
12\r
13 VariableServiceGetVariable() and VariableServiceSetVariable() are external API\r
14 to receive datasize and data buffer. The size should be checked carefully.\r
15\r
16 VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r
17 integer overflow. It should also check attribute to avoid authentication bypass.\r
18\r
17409b7a 19Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
2d3fb919 20This program and the accompanying materials\r
21are licensed and made available under the terms and conditions of the BSD License\r
22which accompanies this distribution. The full text of the license may be found at\r
0c18794e 23http://opensource.org/licenses/bsd-license.php\r
24\r
2d3fb919 25THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
0c18794e 26WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
27\r
28**/\r
29\r
30#include "Variable.h"\r
31#include "AuthService.h"\r
32\r
33VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r
34\r
35///\r
36/// Define a memory cache that improves the search performance for a variable.\r
37///\r
6ab9f441 38VARIABLE_STORE_HEADER *mNvVariableCache = NULL;\r
0c18794e 39\r
40///\r
41/// The memory entry used for variable statistics data.\r
42///\r
6ab9f441
RN
43VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
44\r
45///\r
46/// The list to store the variables which cannot be set after the EFI_END_OF_DXE_EVENT_GROUP_GUID\r
47/// or EVT_GROUP_READY_TO_BOOT event.\r
48///\r
49LIST_ENTRY mLockedVariableList = INITIALIZE_LIST_HEAD_VARIABLE (mLockedVariableList);\r
50\r
51///\r
52/// The flag to indicate whether the platform has left the DXE phase of execution.\r
53///\r
54BOOLEAN mEndOfDxe = FALSE;\r
55\r
56///\r
57/// The flag to indicate whether the variable storage locking is enabled.\r
58///\r
59BOOLEAN mEnableLocking = TRUE;\r
0c18794e 60\r
c4b5cc43
SZ
61//\r
62// It will record the current boot error flag before EndOfDxe.\r
63//\r
64VAR_ERROR_FLAG mCurrentBootVarErrFlag = VAR_ERROR_FLAG_NO_ERROR;\r
c1d93242 65\r
db3c5441
SZ
66/**\r
67\r
c1d93242
JY
68 SecureBoot Hook for auth variable update.\r
69\r
70 @param[in] VariableName Name of Variable to be found.\r
71 @param[in] VendorGuid Variable vendor GUID.\r
72**/\r
73VOID\r
74EFIAPI\r
75SecureBootHook (\r
76 IN CHAR16 *VariableName,\r
77 IN EFI_GUID *VendorGuid\r
78 );\r
79\r
0c18794e 80/**\r
2d3fb919 81 Routine used to track statistical information about variable usage.\r
0c18794e 82 The data is stored in the EFI system table so it can be accessed later.\r
2d3fb919 83 VariableInfo.efi can dump out the table. Only Boot Services variable\r
0c18794e 84 accesses are tracked by this code. The PcdVariableCollectStatistics\r
2d3fb919 85 build flag controls if this feature is enabled.\r
0c18794e 86\r
2d3fb919 87 A read that hits in the cache will have Read and Cache true for\r
0c18794e 88 the transaction. Data is allocated by this routine, but never\r
89 freed.\r
90\r
91 @param[in] VariableName Name of the Variable to track.\r
92 @param[in] VendorGuid Guid of the Variable to track.\r
93 @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
94 @param[in] Read TRUE if GetVariable() was called.\r
95 @param[in] Write TRUE if SetVariable() was called.\r
96 @param[in] Delete TRUE if deleted via SetVariable().\r
97 @param[in] Cache TRUE for a cache hit.\r
98\r
99**/\r
100VOID\r
101UpdateVariableInfo (\r
102 IN CHAR16 *VariableName,\r
103 IN EFI_GUID *VendorGuid,\r
104 IN BOOLEAN Volatile,\r
105 IN BOOLEAN Read,\r
106 IN BOOLEAN Write,\r
107 IN BOOLEAN Delete,\r
108 IN BOOLEAN Cache\r
109 )\r
110{\r
111 VARIABLE_INFO_ENTRY *Entry;\r
112\r
113 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
114\r
115 if (AtRuntime ()) {\r
116 // Don't collect statistics at runtime.\r
117 return;\r
118 }\r
119\r
120 if (gVariableInfo == NULL) {\r
121 //\r
122 // On the first call allocate a entry and place a pointer to it in\r
123 // the EFI System Table.\r
124 //\r
125 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
126 ASSERT (gVariableInfo != NULL);\r
127\r
128 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
129 gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
130 ASSERT (gVariableInfo->Name != NULL);\r
131 StrCpy (gVariableInfo->Name, VariableName);\r
132 gVariableInfo->Volatile = Volatile;\r
133 }\r
134\r
2d3fb919 135\r
0c18794e 136 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
137 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
138 if (StrCmp (VariableName, Entry->Name) == 0) {\r
139 if (Read) {\r
140 Entry->ReadCount++;\r
141 }\r
142 if (Write) {\r
143 Entry->WriteCount++;\r
144 }\r
145 if (Delete) {\r
146 Entry->DeleteCount++;\r
147 }\r
148 if (Cache) {\r
149 Entry->CacheCount++;\r
150 }\r
151\r
152 return;\r
153 }\r
154 }\r
155\r
156 if (Entry->Next == NULL) {\r
157 //\r
158 // If the entry is not in the table add it.\r
159 // Next iteration of the loop will fill in the data.\r
160 //\r
161 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
162 ASSERT (Entry->Next != NULL);\r
163\r
164 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
165 Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
166 ASSERT (Entry->Next->Name != NULL);\r
167 StrCpy (Entry->Next->Name, VariableName);\r
168 Entry->Next->Volatile = Volatile;\r
169 }\r
170\r
171 }\r
172 }\r
173}\r
174\r
175\r
176/**\r
177\r
178 This code checks if variable header is valid or not.\r
179\r
6ebffb67
SZ
180 @param Variable Pointer to the Variable Header.\r
181 @param VariableStoreEnd Pointer to the Variable Store End.\r
0c18794e 182\r
6ebffb67
SZ
183 @retval TRUE Variable header is valid.\r
184 @retval FALSE Variable header is not valid.\r
0c18794e 185\r
186**/\r
187BOOLEAN\r
188IsValidVariableHeader (\r
6ebffb67
SZ
189 IN VARIABLE_HEADER *Variable,\r
190 IN VARIABLE_HEADER *VariableStoreEnd\r
0c18794e 191 )\r
192{\r
6ebffb67
SZ
193 if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
194 //\r
195 // Variable is NULL or has reached the end of variable store,\r
196 // or the StartId is not correct.\r
197 //\r
0c18794e 198 return FALSE;\r
199 }\r
200\r
201 return TRUE;\r
202}\r
203\r
204\r
205/**\r
206\r
207 This function writes data to the FWH at the correct LBA even if the LBAs\r
208 are fragmented.\r
209\r
210 @param Global Pointer to VARAIBLE_GLOBAL structure.\r
211 @param Volatile Point out the Variable is Volatile or Non-Volatile.\r
212 @param SetByIndex TRUE if target pointer is given as index.\r
213 FALSE if target pointer is absolute.\r
214 @param Fvb Pointer to the writable FVB protocol.\r
215 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
216 structure.\r
217 @param DataSize Size of data to be written.\r
218 @param Buffer Pointer to the buffer from which data is written.\r
219\r
220 @retval EFI_INVALID_PARAMETER Parameters not valid.\r
221 @retval EFI_SUCCESS Variable store successfully updated.\r
222\r
223**/\r
224EFI_STATUS\r
225UpdateVariableStore (\r
226 IN VARIABLE_GLOBAL *Global,\r
227 IN BOOLEAN Volatile,\r
228 IN BOOLEAN SetByIndex,\r
229 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
230 IN UINTN DataPtrIndex,\r
231 IN UINT32 DataSize,\r
232 IN UINT8 *Buffer\r
233 )\r
234{\r
235 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
236 UINTN BlockIndex2;\r
237 UINTN LinearOffset;\r
238 UINTN CurrWriteSize;\r
239 UINTN CurrWritePtr;\r
240 UINT8 *CurrBuffer;\r
241 EFI_LBA LbaNumber;\r
242 UINTN Size;\r
243 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
244 VARIABLE_STORE_HEADER *VolatileBase;\r
245 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
246 EFI_PHYSICAL_ADDRESS DataPtr;\r
247 EFI_STATUS Status;\r
248\r
249 FwVolHeader = NULL;\r
250 DataPtr = DataPtrIndex;\r
251\r
252 //\r
253 // Check if the Data is Volatile.\r
254 //\r
255 if (!Volatile) {\r
648f98d1 256 if (Fvb == NULL) {\r
257 return EFI_INVALID_PARAMETER;\r
258 }\r
0c18794e 259 Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
260 ASSERT_EFI_ERROR (Status);\r
261\r
262 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
263 //\r
264 // Data Pointer should point to the actual Address where data is to be\r
265 // written.\r
266 //\r
267 if (SetByIndex) {\r
268 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
269 }\r
270\r
271 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
272 return EFI_INVALID_PARAMETER;\r
273 }\r
274 } else {\r
275 //\r
276 // Data Pointer should point to the actual Address where data is to be\r
277 // written.\r
278 //\r
279 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
280 if (SetByIndex) {\r
281 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
282 }\r
283\r
284 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
285 return EFI_INVALID_PARAMETER;\r
286 }\r
2d3fb919 287\r
0c18794e 288 //\r
289 // If Volatile Variable just do a simple mem copy.\r
2d3fb919 290 //\r
0c18794e 291 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
292 return EFI_SUCCESS;\r
293 }\r
2d3fb919 294\r
0c18794e 295 //\r
296 // If we are here we are dealing with Non-Volatile Variables.\r
297 //\r
298 LinearOffset = (UINTN) FwVolHeader;\r
299 CurrWritePtr = (UINTN) DataPtr;\r
300 CurrWriteSize = DataSize;\r
301 CurrBuffer = Buffer;\r
302 LbaNumber = 0;\r
303\r
304 if (CurrWritePtr < LinearOffset) {\r
305 return EFI_INVALID_PARAMETER;\r
306 }\r
307\r
308 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
309 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
310 //\r
311 // Check to see if the Variable Writes are spanning through multiple\r
312 // blocks.\r
313 //\r
314 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
315 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
316 Status = Fvb->Write (\r
317 Fvb,\r
318 LbaNumber,\r
319 (UINTN) (CurrWritePtr - LinearOffset),\r
320 &CurrWriteSize,\r
321 CurrBuffer\r
322 );\r
323 return Status;\r
324 } else {\r
325 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
326 Status = Fvb->Write (\r
327 Fvb,\r
328 LbaNumber,\r
329 (UINTN) (CurrWritePtr - LinearOffset),\r
330 &Size,\r
331 CurrBuffer\r
332 );\r
333 if (EFI_ERROR (Status)) {\r
334 return Status;\r
335 }\r
336\r
337 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
338 CurrBuffer = CurrBuffer + Size;\r
339 CurrWriteSize = CurrWriteSize - Size;\r
340 }\r
341 }\r
342\r
343 LinearOffset += PtrBlockMapEntry->Length;\r
344 LbaNumber++;\r
345 }\r
346 }\r
347\r
348 return EFI_SUCCESS;\r
349}\r
350\r
351\r
352/**\r
353\r
354 This code gets the current status of Variable Store.\r
355\r
356 @param VarStoreHeader Pointer to the Variable Store Header.\r
357\r
358 @retval EfiRaw Variable store status is raw.\r
359 @retval EfiValid Variable store status is valid.\r
360 @retval EfiInvalid Variable store status is invalid.\r
361\r
362**/\r
363VARIABLE_STORE_STATUS\r
364GetVariableStoreStatus (\r
365 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
366 )\r
367{\r
368 if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&\r
369 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
370 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
371 ) {\r
372\r
373 return EfiValid;\r
374 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
375 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
376 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
377 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
378 VarStoreHeader->Size == 0xffffffff &&\r
379 VarStoreHeader->Format == 0xff &&\r
380 VarStoreHeader->State == 0xff\r
381 ) {\r
382\r
383 return EfiRaw;\r
384 } else {\r
385 return EfiInvalid;\r
386 }\r
387}\r
388\r
389\r
390/**\r
391\r
392 This code gets the size of name of variable.\r
393\r
394 @param Variable Pointer to the Variable Header.\r
395\r
396 @return UINTN Size of variable in bytes.\r
397\r
398**/\r
399UINTN\r
400NameSizeOfVariable (\r
401 IN VARIABLE_HEADER *Variable\r
402 )\r
403{\r
404 if (Variable->State == (UINT8) (-1) ||\r
405 Variable->DataSize == (UINT32) (-1) ||\r
406 Variable->NameSize == (UINT32) (-1) ||\r
407 Variable->Attributes == (UINT32) (-1)) {\r
408 return 0;\r
409 }\r
410 return (UINTN) Variable->NameSize;\r
411}\r
412\r
413/**\r
414\r
415 This code gets the size of variable data.\r
416\r
417 @param Variable Pointer to the Variable Header.\r
418\r
419 @return Size of variable in bytes.\r
420\r
421**/\r
422UINTN\r
423DataSizeOfVariable (\r
424 IN VARIABLE_HEADER *Variable\r
425 )\r
426{\r
427 if (Variable->State == (UINT8) (-1) ||\r
428 Variable->DataSize == (UINT32) (-1) ||\r
429 Variable->NameSize == (UINT32) (-1) ||\r
430 Variable->Attributes == (UINT32) (-1)) {\r
431 return 0;\r
432 }\r
433 return (UINTN) Variable->DataSize;\r
434}\r
435\r
436/**\r
437\r
438 This code gets the pointer to the variable name.\r
439\r
440 @param Variable Pointer to the Variable Header.\r
441\r
442 @return Pointer to Variable Name which is Unicode encoding.\r
443\r
444**/\r
445CHAR16 *\r
446GetVariableNamePtr (\r
447 IN VARIABLE_HEADER *Variable\r
448 )\r
449{\r
450\r
451 return (CHAR16 *) (Variable + 1);\r
452}\r
453\r
454/**\r
455\r
456 This code gets the pointer to the variable data.\r
457\r
458 @param Variable Pointer to the Variable Header.\r
459\r
460 @return Pointer to Variable Data.\r
461\r
462**/\r
463UINT8 *\r
464GetVariableDataPtr (\r
465 IN VARIABLE_HEADER *Variable\r
466 )\r
467{\r
468 UINTN Value;\r
2d3fb919 469\r
0c18794e 470 //\r
471 // Be careful about pad size for alignment.\r
472 //\r
473 Value = (UINTN) GetVariableNamePtr (Variable);\r
474 Value += NameSizeOfVariable (Variable);\r
475 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
476\r
477 return (UINT8 *) Value;\r
478}\r
479\r
480\r
481/**\r
482\r
483 This code gets the pointer to the next variable header.\r
484\r
485 @param Variable Pointer to the Variable Header.\r
486\r
487 @return Pointer to next variable header.\r
488\r
489**/\r
490VARIABLE_HEADER *\r
491GetNextVariablePtr (\r
492 IN VARIABLE_HEADER *Variable\r
493 )\r
494{\r
495 UINTN Value;\r
496\r
0c18794e 497 Value = (UINTN) GetVariableDataPtr (Variable);\r
498 Value += DataSizeOfVariable (Variable);\r
499 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
500\r
501 //\r
502 // Be careful about pad size for alignment.\r
503 //\r
504 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
505}\r
506\r
507/**\r
508\r
509 Gets the pointer to the first variable header in given variable store area.\r
510\r
511 @param VarStoreHeader Pointer to the Variable Store Header.\r
512\r
513 @return Pointer to the first variable header.\r
514\r
515**/\r
516VARIABLE_HEADER *\r
517GetStartPointer (\r
518 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
519 )\r
520{\r
521 //\r
522 // The end of variable store.\r
523 //\r
524 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
525}\r
526\r
527/**\r
528\r
529 Gets the pointer to the end of the variable storage area.\r
530\r
531 This function gets pointer to the end of the variable storage\r
532 area, according to the input variable store header.\r
533\r
534 @param VarStoreHeader Pointer to the Variable Store Header.\r
535\r
2d3fb919 536 @return Pointer to the end of the variable storage area.\r
0c18794e 537\r
538**/\r
539VARIABLE_HEADER *\r
540GetEndPointer (\r
541 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
542 )\r
543{\r
544 //\r
545 // The end of variable store\r
546 //\r
547 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
548}\r
549\r
952ba83c
SZ
550/**\r
551 Record variable error flag.\r
552\r
553 @param[in] Flag Variable error flag to record.\r
554 @param[in] VariableName Name of variable.\r
555 @param[in] VendorGuid Guid of variable.\r
556 @param[in] Attributes Attributes of the variable.\r
557 @param[in] VariableSize Size of the variable.\r
558\r
559**/\r
560VOID\r
561RecordVarErrorFlag (\r
562 IN VAR_ERROR_FLAG Flag,\r
563 IN CHAR16 *VariableName,\r
564 IN EFI_GUID *VendorGuid,\r
565 IN UINT32 Attributes,\r
566 IN UINTN VariableSize\r
567 )\r
568{\r
569 EFI_STATUS Status;\r
570 VARIABLE_POINTER_TRACK Variable;\r
571 VAR_ERROR_FLAG *VarErrFlag;\r
572 VAR_ERROR_FLAG TempFlag;\r
573\r
574 DEBUG_CODE (\r
575 DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));\r
576 if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {\r
577 if (AtRuntime ()) {\r
578 DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
579 } else {\r
580 DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
581 }\r
582 } else {\r
583 DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));\r
584 }\r
585 );\r
586\r
c4b5cc43
SZ
587 if (!mEndOfDxe) {\r
588 //\r
589 // Before EndOfDxe, just record the current boot variable error flag to local variable,\r
590 // and leave the variable error flag in NV flash as the last boot variable error flag.\r
591 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash\r
592 // will be initialized to this local current boot variable error flag.\r
593 //\r
594 mCurrentBootVarErrFlag &= Flag;\r
595 return;\r
596 }\r
597\r
952ba83c
SZ
598 //\r
599 // Record error flag (it should have be initialized).\r
600 //\r
601 Status = FindVariable (\r
602 VAR_ERROR_FLAG_NAME,\r
603 &gEdkiiVarErrorFlagGuid,\r
604 &Variable,\r
605 &mVariableModuleGlobal->VariableGlobal,\r
606 FALSE\r
607 );\r
608 if (!EFI_ERROR (Status)) {\r
609 VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);\r
610 TempFlag = *VarErrFlag;\r
611 TempFlag &= Flag;\r
612 if (TempFlag == *VarErrFlag) {\r
613 return;\r
614 }\r
615 Status = UpdateVariableStore (\r
616 &mVariableModuleGlobal->VariableGlobal,\r
617 FALSE,\r
618 FALSE,\r
619 mVariableModuleGlobal->FvbInstance,\r
620 (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
621 sizeof (TempFlag),\r
622 &TempFlag\r
623 );\r
624 if (!EFI_ERROR (Status)) {\r
625 //\r
626 // Update the data in NV cache.\r
627 //\r
628 *VarErrFlag = Flag;\r
629 }\r
630 }\r
631}\r
632\r
633/**\r
634 Initialize variable error flag.\r
635\r
636 Before EndOfDxe, the variable indicates the last boot variable error flag,\r
637 then it means the last boot variable error flag must be got before EndOfDxe.\r
638 After EndOfDxe, the variable indicates the current boot variable error flag,\r
639 then it means the current boot variable error flag must be got after EndOfDxe.\r
640\r
641**/\r
642VOID\r
643InitializeVarErrorFlag (\r
644 VOID\r
645 )\r
646{\r
647 EFI_STATUS Status;\r
648 VARIABLE_POINTER_TRACK Variable;\r
649 VAR_ERROR_FLAG Flag;\r
650 VAR_ERROR_FLAG VarErrFlag;\r
651\r
652 if (!mEndOfDxe) {\r
653 return;\r
654 }\r
655\r
c4b5cc43 656 Flag = mCurrentBootVarErrFlag;\r
952ba83c
SZ
657 DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));\r
658\r
659 Status = FindVariable (\r
660 VAR_ERROR_FLAG_NAME,\r
661 &gEdkiiVarErrorFlagGuid,\r
662 &Variable,\r
663 &mVariableModuleGlobal->VariableGlobal,\r
664 FALSE\r
665 );\r
666 if (!EFI_ERROR (Status)) {\r
667 VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));\r
668 if (VarErrFlag == Flag) {\r
669 return;\r
670 }\r
671 }\r
672\r
673 UpdateVariable (\r
674 VAR_ERROR_FLAG_NAME,\r
675 &gEdkiiVarErrorFlagGuid,\r
676 &Flag,\r
677 sizeof (Flag),\r
678 VARIABLE_ATTRIBUTE_NV_BS_RT,\r
679 0,\r
680 0,\r
681 &Variable,\r
682 NULL\r
683 );\r
684}\r
685\r
686/**\r
687 Is user variable?\r
688\r
689 @param[in] Variable Pointer to variable header.\r
690\r
691 @retval TRUE User variable.\r
692 @retval FALSE System variable.\r
693\r
694**/\r
695BOOLEAN\r
696IsUserVariable (\r
697 IN VARIABLE_HEADER *Variable\r
698 )\r
699{\r
700 VAR_CHECK_VARIABLE_PROPERTY Property;\r
701\r
702 //\r
703 // Only after End Of Dxe, the variables belong to system variable are fixed.\r
704 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
705 // then no need to check if the variable is user variable or not specially.\r
706 //\r
707 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
708 if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
709 return TRUE;\r
710 }\r
711 }\r
712 return FALSE;\r
713}\r
714\r
715/**\r
716 Calculate common user variable total size.\r
717\r
718**/\r
719VOID\r
720CalculateCommonUserVariableTotalSize (\r
721 VOID\r
722 )\r
723{\r
724 VARIABLE_HEADER *Variable;\r
725 VARIABLE_HEADER *NextVariable;\r
726 UINTN VariableSize;\r
727 VAR_CHECK_VARIABLE_PROPERTY Property;\r
728\r
729 //\r
730 // Only after End Of Dxe, the variables belong to system variable are fixed.\r
731 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
732 // then no need to calculate the common user variable total size specially.\r
733 //\r
734 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
735 Variable = GetStartPointer (mNvVariableCache);\r
736 while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r
737 NextVariable = GetNextVariablePtr (Variable);\r
738 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
739 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
740 if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
741 //\r
742 // No property, it is user variable.\r
743 //\r
744 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
745 }\r
746 }\r
747\r
748 Variable = NextVariable;\r
749 }\r
750 }\r
751}\r
752\r
753/**\r
754 Initialize variable quota.\r
755\r
756**/\r
757VOID\r
758InitializeVariableQuota (\r
759 VOID\r
760 )\r
761{\r
762 STATIC BOOLEAN Initialized;\r
763\r
764 if (!mEndOfDxe || Initialized) {\r
765 return;\r
766 }\r
767 Initialized = TRUE;\r
768\r
769 InitializeVarErrorFlag ();\r
770 CalculateCommonUserVariableTotalSize ();\r
771}\r
772\r
83758cdc 773/**\r
774\r
775 Check the PubKeyIndex is a valid key or not.\r
776\r
20333c6d 777 This function will iterate the NV storage to see if this PubKeyIndex is still referenced\r
83758cdc 778 by any valid count-based auth variabe.\r
20333c6d 779\r
83758cdc 780 @param[in] PubKeyIndex Index of the public key in public key store.\r
781\r
782 @retval TRUE The PubKeyIndex is still in use.\r
783 @retval FALSE The PubKeyIndex is not referenced by any count-based auth variabe.\r
20333c6d 784\r
83758cdc 785**/\r
786BOOLEAN\r
787IsValidPubKeyIndex (\r
788 IN UINT32 PubKeyIndex\r
789 )\r
790{\r
791 VARIABLE_HEADER *Variable;\r
6ebffb67 792 VARIABLE_HEADER *VariableStoreEnd;\r
83758cdc 793\r
794 if (PubKeyIndex > mPubKeyNumber) {\r
795 return FALSE;\r
796 }\r
6ebffb67 797\r
9a12e582 798 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
6ebffb67
SZ
799 VariableStoreEnd = GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
800\r
801 while (IsValidVariableHeader (Variable, VariableStoreEnd)) {\r
20333c6d 802 if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
83758cdc 803 Variable->PubKeyIndex == PubKeyIndex) {\r
804 return TRUE;\r
805 }\r
806 Variable = GetNextVariablePtr (Variable);\r
807 }\r
20333c6d 808\r
83758cdc 809 return FALSE;\r
810}\r
0c18794e 811\r
812/**\r
813\r
83758cdc 814 Get the number of valid public key in PubKeyStore.\r
20333c6d 815\r
83758cdc 816 @param[in] PubKeyNumber Number of the public key in public key store.\r
817\r
818 @return Number of valid public key in PubKeyStore.\r
819\r
820**/\r
821UINT32\r
822GetValidPubKeyNumber (\r
823 IN UINT32 PubKeyNumber\r
824 )\r
825{\r
826 UINT32 PubKeyIndex;\r
827 UINT32 Counter;\r
828\r
829 Counter = 0;\r
20333c6d 830\r
83758cdc 831 for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) {\r
832 if (IsValidPubKeyIndex (PubKeyIndex)) {\r
833 Counter++;\r
834 }\r
835 }\r
20333c6d 836\r
83758cdc 837 return Counter;\r
838}\r
839\r
840/**\r
841\r
842 Filter the useless key in public key store.\r
843\r
20333c6d 844 This function will find out all valid public keys in public key database, save them in new allocated\r
83758cdc 845 buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer\r
846 NewPubKeyIndex and NewPubKeyStore with FreePool().\r
847\r
848 @param[in] PubKeyStore Point to the public key database.\r
849 @param[in] PubKeyNumber Number of the public key in PubKeyStore.\r
850 @param[out] NewPubKeyIndex Point to an array of new PubKeyIndex corresponds to NewPubKeyStore.\r
851 @param[out] NewPubKeyStore Saved all valid public keys in PubKeyStore.\r
852 @param[out] NewPubKeySize Buffer size of the NewPubKeyStore.\r
20333c6d 853\r
83758cdc 854 @retval EFI_SUCCESS Trim operation is complete successfully.\r
855 @retval EFI_OUT_OF_RESOURCES No enough memory resources, or no useless key in PubKeyStore.\r
20333c6d 856\r
83758cdc 857**/\r
858EFI_STATUS\r
859PubKeyStoreFilter (\r
860 IN UINT8 *PubKeyStore,\r
861 IN UINT32 PubKeyNumber,\r
862 OUT UINT32 **NewPubKeyIndex,\r
863 OUT UINT8 **NewPubKeyStore,\r
864 OUT UINT32 *NewPubKeySize\r
865 )\r
866{\r
867 UINT32 PubKeyIndex;\r
868 UINT32 CopiedKey;\r
869 UINT32 NewPubKeyNumber;\r
20333c6d 870\r
83758cdc 871 NewPubKeyNumber = GetValidPubKeyNumber (PubKeyNumber);\r
872 if (NewPubKeyNumber == PubKeyNumber) {\r
873 return EFI_OUT_OF_RESOURCES;\r
874 }\r
875\r
876 if (NewPubKeyNumber != 0) {\r
877 *NewPubKeySize = NewPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE;\r
878 } else {\r
879 *NewPubKeySize = sizeof (UINT8);\r
880 }\r
881\r
882 *NewPubKeyStore = AllocatePool (*NewPubKeySize);\r
883 if (*NewPubKeyStore == NULL) {\r
884 return EFI_OUT_OF_RESOURCES;\r
885 }\r
0c18794e 886\r
83758cdc 887 *NewPubKeyIndex = AllocateZeroPool ((PubKeyNumber + 1) * sizeof (UINT32));\r
888 if (*NewPubKeyIndex == NULL) {\r
889 FreePool (*NewPubKeyStore);\r
128ef095 890 *NewPubKeyStore = NULL;\r
83758cdc 891 return EFI_OUT_OF_RESOURCES;\r
892 }\r
0c18794e 893\r
83758cdc 894 CopiedKey = 0;\r
895 for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) {\r
896 if (IsValidPubKeyIndex (PubKeyIndex)) {\r
897 CopyMem (\r
898 *NewPubKeyStore + CopiedKey * EFI_CERT_TYPE_RSA2048_SIZE,\r
899 PubKeyStore + (PubKeyIndex - 1) * EFI_CERT_TYPE_RSA2048_SIZE,\r
900 EFI_CERT_TYPE_RSA2048_SIZE\r
901 );\r
902 (*NewPubKeyIndex)[PubKeyIndex] = ++CopiedKey;\r
903 }\r
904 }\r
905 return EFI_SUCCESS;\r
906}\r
907\r
908/**\r
909\r
910 Variable store garbage collection and reclaim operation.\r
911\r
912 If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.\r
913 If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex\r
914 for all the count-based authenticate variable in NV storage.\r
915\r
ca5a7d87 916 @param[in] VariableBase Base address of variable store.\r
917 @param[out] LastVariableOffset Offset of last variable.\r
918 @param[in] IsVolatile The variable store is volatile or not;\r
919 if it is non-volatile, need FTW.\r
920 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.\r
7baf3c69
SZ
921 @param[in] NewVariable Pointer to new variable.\r
922 @param[in] NewVariableSize New variable size.\r
ca5a7d87 923 @param[in] ReclaimPubKeyStore Reclaim for public key database or not.\r
20333c6d 924\r
83758cdc 925 @return EFI_SUCCESS Reclaim operation has finished successfully.\r
7baf3c69 926 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.\r
ca5a7d87 927 @return EFI_DEVICE_ERROR The public key database doesn't exist.\r
83758cdc 928 @return Others Unexpect error happened during reclaim operation.\r
0c18794e 929\r
930**/\r
931EFI_STATUS\r
932Reclaim (\r
ca5a7d87 933 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
934 OUT UINTN *LastVariableOffset,\r
935 IN BOOLEAN IsVolatile,\r
936 IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,\r
7baf3c69
SZ
937 IN VARIABLE_HEADER *NewVariable,\r
938 IN UINTN NewVariableSize,\r
939 IN BOOLEAN ReclaimPubKeyStore\r
0c18794e 940 )\r
941{\r
942 VARIABLE_HEADER *Variable;\r
943 VARIABLE_HEADER *AddedVariable;\r
944 VARIABLE_HEADER *NextVariable;\r
945 VARIABLE_HEADER *NextAddedVariable;\r
946 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
947 UINT8 *ValidBuffer;\r
948 UINTN MaximumBufferSize;\r
949 UINTN VariableSize;\r
0c18794e 950 UINTN NameSize;\r
951 UINT8 *CurrPtr;\r
952 VOID *Point0;\r
953 VOID *Point1;\r
954 BOOLEAN FoundAdded;\r
955 EFI_STATUS Status;\r
8f3a9e58 956 UINTN CommonVariableTotalSize;\r
952ba83c 957 UINTN CommonUserVariableTotalSize;\r
8f3a9e58 958 UINTN HwErrVariableTotalSize;\r
83758cdc 959 UINT32 *NewPubKeyIndex;\r
960 UINT8 *NewPubKeyStore;\r
961 UINT32 NewPubKeySize;\r
962 VARIABLE_HEADER *PubKeyHeader;\r
23b06935 963 VARIABLE_HEADER *UpdatingVariable;\r
7baf3c69 964 VARIABLE_HEADER *UpdatingInDeletedTransition;\r
23b06935
SZ
965\r
966 UpdatingVariable = NULL;\r
7baf3c69 967 UpdatingInDeletedTransition = NULL;\r
23b06935
SZ
968 if (UpdatingPtrTrack != NULL) {\r
969 UpdatingVariable = UpdatingPtrTrack->CurrPtr;\r
7baf3c69 970 UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;\r
23b06935 971 }\r
0c18794e 972\r
973 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
8f3a9e58
SZ
974\r
975 CommonVariableTotalSize = 0;\r
952ba83c 976 CommonUserVariableTotalSize = 0;\r
8f3a9e58 977 HwErrVariableTotalSize = 0;\r
83758cdc 978 NewPubKeyIndex = NULL;\r
979 NewPubKeyStore = NULL;\r
980 NewPubKeySize = 0;\r
981 PubKeyHeader = NULL;\r
0c18794e 982\r
128ef095
SZ
983 if (IsVolatile) {\r
984 //\r
985 // Start Pointers for the variable.\r
986 //\r
987 Variable = GetStartPointer (VariableStoreHeader);\r
988 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
989\r
6ebffb67 990 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
128ef095
SZ
991 NextVariable = GetNextVariablePtr (Variable);\r
992 if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
993 Variable != UpdatingVariable &&\r
994 Variable != UpdatingInDeletedTransition\r
995 ) {\r
996 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
997 MaximumBufferSize += VariableSize;\r
998 }\r
999\r
1000 Variable = NextVariable;\r
0c18794e 1001 }\r
1002\r
128ef095
SZ
1003 if (NewVariable != NULL) {\r
1004 //\r
1005 // Add the new variable size.\r
1006 //\r
1007 MaximumBufferSize += NewVariableSize;\r
1008 }\r
0c18794e 1009\r
7baf3c69 1010 //\r
128ef095
SZ
1011 // Reserve the 1 Bytes with Oxff to identify the\r
1012 // end of the variable buffer.\r
7baf3c69 1013 //\r
128ef095
SZ
1014 MaximumBufferSize += 1;\r
1015 ValidBuffer = AllocatePool (MaximumBufferSize);\r
1016 if (ValidBuffer == NULL) {\r
1017 return EFI_OUT_OF_RESOURCES;\r
1018 }\r
1019 } else {\r
1020 //\r
1021 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache\r
1022 // as the buffer to reduce SMRAM consumption for SMM variable driver.\r
1023 //\r
1024 MaximumBufferSize = mNvVariableCache->Size;\r
1025 ValidBuffer = (UINT8 *) mNvVariableCache;\r
0c18794e 1026 }\r
1027\r
1028 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
1029\r
1030 //\r
1031 // Copy variable store header.\r
1032 //\r
1033 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
1034 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
1035\r
83758cdc 1036 if (ReclaimPubKeyStore) {\r
7baf3c69 1037 ASSERT (IsVolatile == FALSE);\r
83758cdc 1038 //\r
1039 // Trim the PubKeyStore and get new PubKeyIndex.\r
1040 //\r
1041 Status = PubKeyStoreFilter (\r
1042 mPubKeyStore,\r
1043 mPubKeyNumber,\r
1044 &NewPubKeyIndex,\r
1045 &NewPubKeyStore,\r
1046 &NewPubKeySize\r
1047 );\r
1048 if (EFI_ERROR (Status)) {\r
128ef095 1049 goto Done;\r
83758cdc 1050 }\r
3a4b498e 1051 ASSERT ((NewPubKeyIndex != NULL) && (NewPubKeyStore != NULL));\r
0c18794e 1052\r
83758cdc 1053 //\r
1054 // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION).\r
1055 //\r
128ef095 1056 Variable = GetStartPointer (VariableStoreHeader);\r
6ebffb67 1057 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
83758cdc 1058 NextVariable = GetNextVariablePtr (Variable);\r
1059 if (Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
20333c6d 1060 if ((StrCmp (GetVariableNamePtr (Variable), AUTHVAR_KEYDB_NAME) == 0) &&\r
83758cdc 1061 (CompareGuid (&Variable->VendorGuid, &gEfiAuthenticatedVariableGuid))) {\r
1062 //\r
1063 // Skip the public key database, it will be reinstalled later.\r
1064 //\r
1065 PubKeyHeader = Variable;\r
0c18794e 1066 Variable = NextVariable;\r
1067 continue;\r
1068 }\r
20333c6d 1069\r
83758cdc 1070 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1071 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
1072 ((VARIABLE_HEADER*) CurrPtr)->PubKeyIndex = NewPubKeyIndex[Variable->PubKeyIndex];\r
1073 CurrPtr += VariableSize;\r
7baf3c69 1074 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
83758cdc 1075 HwErrVariableTotalSize += VariableSize;\r
7baf3c69 1076 } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
83758cdc 1077 CommonVariableTotalSize += VariableSize;\r
952ba83c
SZ
1078 if (IsUserVariable (Variable)) {\r
1079 CommonUserVariableTotalSize += VariableSize;\r
17409b7a 1080 }\r
83758cdc 1081 }\r
952ba83c 1082 }\r
83758cdc 1083 Variable = NextVariable;\r
0c18794e 1084 }\r
0c18794e 1085\r
83758cdc 1086 //\r
1087 // Reinstall the new public key database.\r
1088 //\r
12cbe232 1089 ASSERT (PubKeyHeader != NULL);\r
ca5a7d87 1090 if (PubKeyHeader == NULL) {\r
128ef095
SZ
1091 Status = EFI_DEVICE_ERROR;\r
1092 goto Done;\r
ca5a7d87 1093 }\r
83758cdc 1094 CopyMem (CurrPtr, (UINT8*) PubKeyHeader, sizeof (VARIABLE_HEADER));\r
1095 Variable = (VARIABLE_HEADER*) CurrPtr;\r
1096 Variable->DataSize = NewPubKeySize;\r
1097 StrCpy (GetVariableNamePtr (Variable), GetVariableNamePtr (PubKeyHeader));\r
1098 CopyMem (GetVariableDataPtr (Variable), NewPubKeyStore, NewPubKeySize);\r
20333c6d 1099 CurrPtr = (UINT8*) GetNextVariablePtr (Variable);\r
83758cdc 1100 CommonVariableTotalSize += (UINTN) CurrPtr - (UINTN) Variable;\r
952ba83c
SZ
1101 if (IsUserVariable (Variable)) {\r
1102 CommonUserVariableTotalSize += (UINTN) CurrPtr - (UINTN) Variable;\r
1103 }\r
83758cdc 1104 } else {\r
1105 //\r
1106 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
1107 //\r
1108 Variable = GetStartPointer (VariableStoreHeader);\r
6ebffb67 1109 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
83758cdc 1110 NextVariable = GetNextVariablePtr (Variable);\r
7baf3c69 1111 if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {\r
0c18794e 1112 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1113 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
0c18794e 1114 CurrPtr += VariableSize;\r
1115 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
8f3a9e58 1116 HwErrVariableTotalSize += VariableSize;\r
0c18794e 1117 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
8f3a9e58 1118 CommonVariableTotalSize += VariableSize;\r
952ba83c
SZ
1119 if (IsUserVariable (Variable)) {\r
1120 CommonUserVariableTotalSize += VariableSize;\r
17409b7a 1121 }\r
0c18794e 1122 }\r
952ba83c 1123 }\r
83758cdc 1124 Variable = NextVariable;\r
0c18794e 1125 }\r
1126\r
83758cdc 1127 //\r
1128 // Reinstall all in delete transition variables.\r
1129 //\r
7baf3c69 1130 Variable = GetStartPointer (VariableStoreHeader);\r
6ebffb67 1131 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
83758cdc 1132 NextVariable = GetNextVariablePtr (Variable);\r
7baf3c69 1133 if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
83758cdc 1134\r
1135 //\r
1136 // Buffer has cached all ADDED variable.\r
1137 // Per IN_DELETED variable, we have to guarantee that\r
1138 // no ADDED one in previous buffer.\r
1139 //\r
1140\r
1141 FoundAdded = FALSE;\r
1142 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
6ebffb67 1143 while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {\r
83758cdc 1144 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
1145 NameSize = NameSizeOfVariable (AddedVariable);\r
1146 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
1147 NameSize == NameSizeOfVariable (Variable)\r
1148 ) {\r
1149 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
1150 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
23b06935 1151 if (CompareMem (Point0, Point1, NameSize) == 0) {\r
83758cdc 1152 FoundAdded = TRUE;\r
1153 break;\r
1154 }\r
1155 }\r
1156 AddedVariable = NextAddedVariable;\r
1157 }\r
1158 if (!FoundAdded) {\r
1159 //\r
1160 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r
1161 //\r
1162 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1163 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
1164 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
1165 CurrPtr += VariableSize;\r
1166 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1167 HwErrVariableTotalSize += VariableSize;\r
1168 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1169 CommonVariableTotalSize += VariableSize;\r
952ba83c
SZ
1170 if (IsUserVariable (Variable)) {\r
1171 CommonUserVariableTotalSize += VariableSize;\r
17409b7a 1172 }\r
83758cdc 1173 }\r
1174 }\r
952ba83c 1175 }\r
83758cdc 1176\r
1177 Variable = NextVariable;\r
1178 }\r
7baf3c69
SZ
1179\r
1180 //\r
1181 // Install the new variable if it is not NULL.\r
1182 //\r
1183 if (NewVariable != NULL) {\r
1184 if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r
1185 //\r
1186 // No enough space to store the new variable.\r
1187 //\r
1188 Status = EFI_OUT_OF_RESOURCES;\r
1189 goto Done;\r
1190 }\r
1191 if (!IsVolatile) {\r
1192 if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1193 HwErrVariableTotalSize += NewVariableSize;\r
1194 } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1195 CommonVariableTotalSize += NewVariableSize;\r
952ba83c
SZ
1196 if (IsUserVariable (NewVariable)) {\r
1197 CommonUserVariableTotalSize += NewVariableSize;\r
17409b7a 1198 }\r
952ba83c 1199 }\r
7baf3c69 1200 if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||\r
952ba83c
SZ
1201 (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) ||\r
1202 (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
7baf3c69
SZ
1203 //\r
1204 // No enough space to store the new variable by NV or NV+HR attribute.\r
1205 //\r
1206 Status = EFI_OUT_OF_RESOURCES;\r
1207 goto Done;\r
1208 }\r
1209 }\r
1210\r
1211 CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);\r
1212 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
1213 if (UpdatingVariable != NULL) {\r
1214 UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));\r
1215 UpdatingPtrTrack->InDeletedTransitionPtr = NULL;\r
1216 }\r
1217 CurrPtr += NewVariableSize;\r
1218 }\r
0c18794e 1219 }\r
1220\r
1221 if (IsVolatile) {\r
1222 //\r
1223 // If volatile variable store, just copy valid buffer.\r
1224 //\r
1225 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
7baf3c69 1226 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));\r
128ef095 1227 *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
0c18794e 1228 Status = EFI_SUCCESS;\r
1229 } else {\r
1230 //\r
1231 // If non-volatile variable store, perform FTW here.\r
1232 //\r
1233 Status = FtwVariableSpace (\r
1234 VariableBase,\r
128ef095 1235 (VARIABLE_STORE_HEADER *) ValidBuffer\r
0c18794e 1236 );\r
128ef095
SZ
1237 if (!EFI_ERROR (Status)) {\r
1238 *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
8f3a9e58
SZ
1239 mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
1240 mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
952ba83c 1241 mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r
128ef095 1242 } else {\r
952ba83c
SZ
1243 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
1244 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
1245 NextVariable = GetNextVariablePtr (Variable);\r
1246 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
128ef095 1247 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
952ba83c 1248 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
128ef095 1249 } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
952ba83c
SZ
1250 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
1251 if (IsUserVariable (Variable)) {\r
1252 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
17409b7a 1253 }\r
952ba83c 1254 }\r
8f3a9e58 1255\r
952ba83c 1256 Variable = NextVariable;\r
128ef095 1257 }\r
952ba83c 1258 *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r
8f3a9e58 1259 }\r
0c18794e 1260 }\r
1261\r
128ef095
SZ
1262Done:\r
1263 if (IsVolatile) {\r
1264 FreePool (ValidBuffer);\r
1265 } else {\r
1266 //\r
1267 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.\r
1268 //\r
1269 CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
83758cdc 1270\r
128ef095
SZ
1271 if (NewPubKeyStore != NULL) {\r
1272 FreePool (NewPubKeyStore);\r
1273 }\r
7baf3c69 1274\r
128ef095
SZ
1275 if (NewPubKeyIndex != NULL) {\r
1276 FreePool (NewPubKeyIndex);\r
1277 }\r
1278 }\r
0c18794e 1279\r
1280 return Status;\r
1281}\r
1282\r
9a000b46
RN
1283/**\r
1284 Find the variable in the specified variable store.\r
1285\r
ecc722ad 1286 @param[in] VariableName Name of the variable to be found\r
1287 @param[in] VendorGuid Vendor GUID to be found.\r
9622df63
SZ
1288 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
1289 check at runtime when searching variable.\r
ecc722ad 1290 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
9a000b46 1291\r
ecc722ad 1292 @retval EFI_SUCCESS Variable found successfully\r
1293 @retval EFI_NOT_FOUND Variable not found\r
9a000b46
RN
1294**/\r
1295EFI_STATUS\r
1296FindVariableEx (\r
1297 IN CHAR16 *VariableName,\r
1298 IN EFI_GUID *VendorGuid,\r
9622df63 1299 IN BOOLEAN IgnoreRtCheck,\r
9a000b46
RN
1300 IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
1301 )\r
1302{\r
1303 VARIABLE_HEADER *InDeletedVariable;\r
1304 VOID *Point;\r
1305\r
23b06935
SZ
1306 PtrTrack->InDeletedTransitionPtr = NULL;\r
1307\r
9a000b46
RN
1308 //\r
1309 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
1310 //\r
1311 InDeletedVariable = NULL;\r
1312\r
1313 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
6ebffb67 1314 ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
9a000b46
RN
1315 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
1316 ) {\r
2d3fb919 1317 if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
9a000b46
RN
1318 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
1319 ) {\r
9622df63 1320 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
9a000b46
RN
1321 if (VariableName[0] == 0) {\r
1322 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1323 InDeletedVariable = PtrTrack->CurrPtr;\r
1324 } else {\r
23b06935 1325 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
9a000b46
RN
1326 return EFI_SUCCESS;\r
1327 }\r
1328 } else {\r
1329 if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {\r
1330 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
1331\r
1332 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
1333 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
1334 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1335 InDeletedVariable = PtrTrack->CurrPtr;\r
1336 } else {\r
23b06935 1337 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
9a000b46
RN
1338 return EFI_SUCCESS;\r
1339 }\r
1340 }\r
1341 }\r
1342 }\r
1343 }\r
1344 }\r
1345 }\r
1346\r
1347 PtrTrack->CurrPtr = InDeletedVariable;\r
1348 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
1349}\r
1350\r
0c18794e 1351\r
1352/**\r
1353 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
1354\r
1355 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
1356 If VariableName is an empty string, then we just return the first\r
1357 qualified variable without comparing VariableName and VendorGuid.\r
9622df63
SZ
1358 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
1359 at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
1360 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
0c18794e 1361\r
ecc722ad 1362 @param[in] VariableName Name of the variable to be found.\r
1363 @param[in] VendorGuid Vendor GUID to be found.\r
1364 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
0c18794e 1365 including the range searched and the target position.\r
ecc722ad 1366 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
0c18794e 1367 base of volatile variable storage area, base of\r
1368 NV variable storage area, and a lock.\r
9622df63
SZ
1369 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
1370 check at runtime when searching variable.\r
0c18794e 1371\r
1372 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
1373 VendorGuid is NULL.\r
1374 @retval EFI_SUCCESS Variable successfully found.\r
1375 @retval EFI_NOT_FOUND Variable not found\r
1376\r
1377**/\r
1378EFI_STATUS\r
1379FindVariable (\r
1380 IN CHAR16 *VariableName,\r
1381 IN EFI_GUID *VendorGuid,\r
1382 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
ecc722ad 1383 IN VARIABLE_GLOBAL *Global,\r
9622df63 1384 IN BOOLEAN IgnoreRtCheck\r
0c18794e 1385 )\r
1386{\r
9a000b46
RN
1387 EFI_STATUS Status;\r
1388 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
1389 VARIABLE_STORE_TYPE Type;\r
1390\r
1391 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
1392 return EFI_INVALID_PARAMETER;\r
1393 }\r
0c18794e 1394\r
1395 //\r
9a000b46 1396 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
0c18794e 1397 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
1398 // make use of this mapping to implement search algorithm.\r
1399 //\r
9a000b46
RN
1400 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
1401 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
1402 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
0c18794e 1403\r
1404 //\r
9a000b46 1405 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
0c18794e 1406 //\r
9a000b46
RN
1407 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
1408 if (VariableStoreHeader[Type] == NULL) {\r
1409 continue;\r
1410 }\r
0c18794e 1411\r
9a000b46
RN
1412 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
1413 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
1414 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
0c18794e 1415\r
9622df63 1416 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
9a000b46
RN
1417 if (!EFI_ERROR (Status)) {\r
1418 return Status;\r
0c18794e 1419 }\r
1420 }\r
0c18794e 1421 return EFI_NOT_FOUND;\r
1422}\r
1423\r
1424/**\r
1425 Get index from supported language codes according to language string.\r
1426\r
1427 This code is used to get corresponding index in supported language codes. It can handle\r
1428 RFC4646 and ISO639 language tags.\r
1429 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
1430 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
1431\r
1432 For example:\r
1433 SupportedLang = "engfraengfra"\r
1434 Lang = "eng"\r
1435 Iso639Language = TRUE\r
1436 The return value is "0".\r
1437 Another example:\r
1438 SupportedLang = "en;fr;en-US;fr-FR"\r
1439 Lang = "fr-FR"\r
1440 Iso639Language = FALSE\r
1441 The return value is "3".\r
1442\r
1443 @param SupportedLang Platform supported language codes.\r
1444 @param Lang Configured language.\r
1445 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
1446\r
1447 @retval The index of language in the language codes.\r
1448\r
1449**/\r
1450UINTN\r
1451GetIndexFromSupportedLangCodes(\r
1452 IN CHAR8 *SupportedLang,\r
1453 IN CHAR8 *Lang,\r
1454 IN BOOLEAN Iso639Language\r
2d3fb919 1455 )\r
0c18794e 1456{\r
1457 UINTN Index;\r
1458 UINTN CompareLength;\r
1459 UINTN LanguageLength;\r
1460\r
1461 if (Iso639Language) {\r
1462 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1463 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
1464 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
1465 //\r
1466 // Successfully find the index of Lang string in SupportedLang string.\r
1467 //\r
1468 Index = Index / CompareLength;\r
1469 return Index;\r
1470 }\r
1471 }\r
1472 ASSERT (FALSE);\r
1473 return 0;\r
1474 } else {\r
1475 //\r
1476 // Compare RFC4646 language code\r
1477 //\r
1478 Index = 0;\r
1479 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
1480\r
1481 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
1482 //\r
1483 // Skip ';' characters in SupportedLang\r
1484 //\r
1485 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
1486 //\r
1487 // Determine the length of the next language code in SupportedLang\r
1488 //\r
1489 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
2d3fb919 1490\r
1491 if ((CompareLength == LanguageLength) &&\r
0c18794e 1492 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
1493 //\r
1494 // Successfully find the index of Lang string in SupportedLang string.\r
1495 //\r
1496 return Index;\r
1497 }\r
1498 }\r
1499 ASSERT (FALSE);\r
1500 return 0;\r
1501 }\r
1502}\r
1503\r
1504/**\r
1505 Get language string from supported language codes according to index.\r
1506\r
1507 This code is used to get corresponding language strings in supported language codes. It can handle\r
1508 RFC4646 and ISO639 language tags.\r
1509 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
1510 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
1511\r
1512 For example:\r
1513 SupportedLang = "engfraengfra"\r
1514 Index = "1"\r
1515 Iso639Language = TRUE\r
1516 The return value is "fra".\r
1517 Another example:\r
1518 SupportedLang = "en;fr;en-US;fr-FR"\r
1519 Index = "1"\r
1520 Iso639Language = FALSE\r
1521 The return value is "fr".\r
1522\r
1523 @param SupportedLang Platform supported language codes.\r
1524 @param Index The index in supported language codes.\r
1525 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
1526\r
1527 @retval The language string in the language codes.\r
1528\r
1529**/\r
1530CHAR8 *\r
1531GetLangFromSupportedLangCodes (\r
1532 IN CHAR8 *SupportedLang,\r
1533 IN UINTN Index,\r
1534 IN BOOLEAN Iso639Language\r
1535)\r
1536{\r
1537 UINTN SubIndex;\r
1538 UINTN CompareLength;\r
1539 CHAR8 *Supported;\r
1540\r
1541 SubIndex = 0;\r
1542 Supported = SupportedLang;\r
1543 if (Iso639Language) {\r
1544 //\r
1545 // According to the index of Lang string in SupportedLang string to get the language.\r
1546 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
1547 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1548 //\r
1549 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1550 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
1551 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
2d3fb919 1552\r
0c18794e 1553 } else {\r
1554 while (TRUE) {\r
1555 //\r
1556 // Take semicolon as delimitation, sequentially traverse supported language codes.\r
1557 //\r
1558 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
1559 Supported++;\r
1560 }\r
1561 if ((*Supported == '\0') && (SubIndex != Index)) {\r
1562 //\r
1563 // Have completed the traverse, but not find corrsponding string.\r
1564 // This case is not allowed to happen.\r
1565 //\r
1566 ASSERT(FALSE);\r
1567 return NULL;\r
1568 }\r
1569 if (SubIndex == Index) {\r
1570 //\r
1571 // According to the index of Lang string in SupportedLang string to get the language.\r
1572 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1573 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1574 //\r
1575 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
1576 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
1577 }\r
1578 SubIndex++;\r
1579\r
1580 //\r
1581 // Skip ';' characters in Supported\r
1582 //\r
1583 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1584 }\r
1585 }\r
1586}\r
1587\r
1588/**\r
2d3fb919 1589 Returns a pointer to an allocated buffer that contains the best matching language\r
1590 from a set of supported languages.\r
1591\r
1592 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
0c18794e 1593 code types may not be mixed in a single call to this function. This function\r
1594 supports a variable argument list that allows the caller to pass in a prioritized\r
1595 list of language codes to test against all the language codes in SupportedLanguages.\r
1596\r
1597 If SupportedLanguages is NULL, then ASSERT().\r
1598\r
1599 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
2d3fb919 1600 contains a set of language codes in the format\r
0c18794e 1601 specified by Iso639Language.\r
1602 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1603 in ISO 639-2 format. If FALSE, then all language\r
1604 codes are assumed to be in RFC 4646 language format\r
2d3fb919 1605 @param[in] ... A variable argument list that contains pointers to\r
0c18794e 1606 Null-terminated ASCII strings that contain one or more\r
1607 language codes in the format specified by Iso639Language.\r
1608 The first language code from each of these language\r
1609 code lists is used to determine if it is an exact or\r
2d3fb919 1610 close match to any of the language codes in\r
0c18794e 1611 SupportedLanguages. Close matches only apply to RFC 4646\r
1612 language codes, and the matching algorithm from RFC 4647\r
2d3fb919 1613 is used to determine if a close match is present. If\r
0c18794e 1614 an exact or close match is found, then the matching\r
1615 language code from SupportedLanguages is returned. If\r
1616 no matches are found, then the next variable argument\r
2d3fb919 1617 parameter is evaluated. The variable argument list\r
0c18794e 1618 is terminated by a NULL.\r
1619\r
1620 @retval NULL The best matching language could not be found in SupportedLanguages.\r
2d3fb919 1621 @retval NULL There are not enough resources available to return the best matching\r
0c18794e 1622 language.\r
2d3fb919 1623 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
0c18794e 1624 language in SupportedLanguages.\r
1625\r
1626**/\r
1627CHAR8 *\r
1628EFIAPI\r
1629VariableGetBestLanguage (\r
2d3fb919 1630 IN CONST CHAR8 *SupportedLanguages,\r
0c18794e 1631 IN BOOLEAN Iso639Language,\r
1632 ...\r
1633 )\r
1634{\r
1635 VA_LIST Args;\r
1636 CHAR8 *Language;\r
1637 UINTN CompareLength;\r
1638 UINTN LanguageLength;\r
1639 CONST CHAR8 *Supported;\r
1640 CHAR8 *Buffer;\r
1641\r
648f98d1 1642 if (SupportedLanguages == NULL) {\r
1643 return NULL;\r
1644 }\r
0c18794e 1645\r
1646 VA_START (Args, Iso639Language);\r
1647 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1648 //\r
1649 // Default to ISO 639-2 mode\r
1650 //\r
1651 CompareLength = 3;\r
1652 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1653\r
1654 //\r
1655 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1656 //\r
1657 if (!Iso639Language) {\r
1658 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1659 }\r
1660\r
1661 //\r
1662 // Trim back the length of Language used until it is empty\r
1663 //\r
1664 while (LanguageLength > 0) {\r
1665 //\r
1666 // Loop through all language codes in SupportedLanguages\r
1667 //\r
1668 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1669 //\r
1670 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1671 //\r
1672 if (!Iso639Language) {\r
1673 //\r
1674 // Skip ';' characters in Supported\r
1675 //\r
1676 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1677 //\r
1678 // Determine the length of the next language code in Supported\r
1679 //\r
1680 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1681 //\r
1682 // If Language is longer than the Supported, then skip to the next language\r
1683 //\r
1684 if (LanguageLength > CompareLength) {\r
1685 continue;\r
1686 }\r
1687 }\r
1688 //\r
1689 // See if the first LanguageLength characters in Supported match Language\r
1690 //\r
1691 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1692 VA_END (Args);\r
1693\r
1694 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1695 Buffer[CompareLength] = '\0';\r
1696 return CopyMem (Buffer, Supported, CompareLength);\r
1697 }\r
1698 }\r
1699\r
1700 if (Iso639Language) {\r
1701 //\r
1702 // If ISO 639 mode, then each language can only be tested once\r
1703 //\r
1704 LanguageLength = 0;\r
1705 } else {\r
1706 //\r
2d3fb919 1707 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
0c18794e 1708 //\r
1709 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1710 }\r
1711 }\r
1712 }\r
1713 VA_END (Args);\r
1714\r
1715 //\r
2d3fb919 1716 // No matches were found\r
0c18794e 1717 //\r
1718 return NULL;\r
1719}\r
1720\r
b2bd493e
SZ
1721/**\r
1722 This function is to check if the remaining variable space is enough to set\r
1723 all Variables from argument list successfully. The purpose of the check\r
1724 is to keep the consistency of the Variables to be in variable storage.\r
1725\r
1726 Note: Variables are assumed to be in same storage.\r
1727 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
1728 so follow the argument sequence to check the Variables.\r
1729\r
1730 @param[in] Attributes Variable attributes for Variable entries.\r
9a12e582 1731 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
20333c6d 1732 A NULL terminates the list. The VariableSize of\r
9a12e582
DG
1733 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
1734 It will be changed to variable total size as output.\r
b2bd493e
SZ
1735\r
1736 @retval TRUE Have enough variable space to set the Variables successfully.\r
1737 @retval FALSE No enough variable space to set the Variables successfully.\r
1738\r
1739**/\r
1740BOOLEAN\r
1741EFIAPI\r
1742CheckRemainingSpaceForConsistency (\r
1743 IN UINT32 Attributes,\r
1744 ...\r
1745 )\r
1746{\r
1747 EFI_STATUS Status;\r
1748 VA_LIST Args;\r
1749 VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r
1750 UINT64 MaximumVariableStorageSize;\r
1751 UINT64 RemainingVariableStorageSize;\r
1752 UINT64 MaximumVariableSize;\r
1753 UINTN TotalNeededSize;\r
1754 UINTN OriginalVarSize;\r
1755 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1756 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
1757 VARIABLE_HEADER *NextVariable;\r
9a12e582
DG
1758 UINTN VarNameSize;\r
1759 UINTN VarDataSize;\r
b2bd493e
SZ
1760\r
1761 //\r
1762 // Non-Volatile related.\r
1763 //\r
1764 VariableStoreHeader = mNvVariableCache;\r
1765\r
1766 Status = VariableServiceQueryVariableInfoInternal (\r
1767 Attributes,\r
1768 &MaximumVariableStorageSize,\r
1769 &RemainingVariableStorageSize,\r
1770 &MaximumVariableSize\r
1771 );\r
1772 ASSERT_EFI_ERROR (Status);\r
1773\r
1774 TotalNeededSize = 0;\r
1775 VA_START (Args, Attributes);\r
1776 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1777 while (VariableEntry != NULL) {\r
9a12e582
DG
1778 //\r
1779 // Calculate variable total size.\r
1780 //\r
1781 VarNameSize = StrSize (VariableEntry->Name);\r
1782 VarNameSize += GET_PAD_SIZE (VarNameSize);\r
1783 VarDataSize = VariableEntry->VariableSize;\r
1784 VarDataSize += GET_PAD_SIZE (VarDataSize);\r
1785 VariableEntry->VariableSize = HEADER_ALIGN (sizeof (VARIABLE_HEADER) + VarNameSize + VarDataSize);\r
1786\r
b2bd493e
SZ
1787 TotalNeededSize += VariableEntry->VariableSize;\r
1788 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1789 }\r
1790 VA_END (Args);\r
1791\r
1792 if (RemainingVariableStorageSize >= TotalNeededSize) {\r
1793 //\r
1794 // Already have enough space.\r
1795 //\r
1796 return TRUE;\r
1797 } else if (AtRuntime ()) {\r
1798 //\r
1799 // At runtime, no reclaim.\r
1800 // The original variable space of Variables can't be reused.\r
1801 //\r
1802 return FALSE;\r
1803 }\r
1804\r
1805 VA_START (Args, Attributes);\r
1806 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1807 while (VariableEntry != NULL) {\r
1808 //\r
1809 // Check if Variable[Index] has been present and get its size.\r
1810 //\r
1811 OriginalVarSize = 0;\r
1812 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
1813 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
1814 Status = FindVariableEx (\r
1815 VariableEntry->Name,\r
1816 VariableEntry->Guid,\r
1817 FALSE,\r
1818 &VariablePtrTrack\r
1819 );\r
1820 if (!EFI_ERROR (Status)) {\r
1821 //\r
1822 // Get size of Variable[Index].\r
1823 //\r
1824 NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
1825 OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
1826 //\r
1827 // Add the original size of Variable[Index] to remaining variable storage size.\r
1828 //\r
1829 RemainingVariableStorageSize += OriginalVarSize;\r
1830 }\r
1831 if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
1832 //\r
1833 // No enough space for Variable[Index].\r
1834 //\r
1835 VA_END (Args);\r
1836 return FALSE;\r
1837 }\r
1838 //\r
1839 // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
1840 //\r
1841 RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
1842 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1843 }\r
1844 VA_END (Args);\r
1845\r
1846 return TRUE;\r
1847}\r
1848\r
0c18794e 1849/**\r
1850 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
1851\r
1852 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
1853\r
1854 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1855 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
1856\r
1857 @param[in] VariableName Name of variable.\r
1858\r
1859 @param[in] Data Variable data.\r
1860\r
1861 @param[in] DataSize Size of data. 0 means delete.\r
1862\r
9bc5dabb
SZ
1863 @retval EFI_SUCCESS The update operation is successful or ignored.\r
1864 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r
1865 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r
1866 @retval Others Other errors happened during the update operation.\r
1867\r
0c18794e 1868**/\r
9bc5dabb 1869EFI_STATUS\r
4d832aab 1870AutoUpdateLangVariable (\r
0c18794e 1871 IN CHAR16 *VariableName,\r
1872 IN VOID *Data,\r
1873 IN UINTN DataSize\r
1874 )\r
1875{\r
1876 EFI_STATUS Status;\r
1877 CHAR8 *BestPlatformLang;\r
1878 CHAR8 *BestLang;\r
1879 UINTN Index;\r
1880 UINT32 Attributes;\r
1881 VARIABLE_POINTER_TRACK Variable;\r
1882 BOOLEAN SetLanguageCodes;\r
b2bd493e 1883 VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r
0c18794e 1884\r
1885 //\r
1886 // Don't do updates for delete operation\r
1887 //\r
1888 if (DataSize == 0) {\r
9bc5dabb 1889 return EFI_SUCCESS;\r
0c18794e 1890 }\r
1891\r
1892 SetLanguageCodes = FALSE;\r
1893\r
6675a21f 1894 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
0c18794e 1895 //\r
1896 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1897 //\r
1898 if (AtRuntime ()) {\r
9bc5dabb 1899 return EFI_WRITE_PROTECTED;\r
0c18794e 1900 }\r
1901\r
1902 SetLanguageCodes = TRUE;\r
1903\r
1904 //\r
1905 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1906 // Therefore, in variable driver, only store the original value for other use.\r
1907 //\r
1908 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1909 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1910 }\r
1911 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1912 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1913\r
1914 //\r
2d3fb919 1915 // PlatformLang holds a single language from PlatformLangCodes,\r
0c18794e 1916 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
1917 //\r
1918 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1919 FreePool (mVariableModuleGlobal->PlatformLang);\r
1920 }\r
1921 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1922 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
1923\r
6675a21f 1924 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
0c18794e 1925 //\r
1926 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
1927 //\r
1928 if (AtRuntime ()) {\r
9bc5dabb 1929 return EFI_WRITE_PROTECTED;\r
0c18794e 1930 }\r
1931\r
1932 SetLanguageCodes = TRUE;\r
1933\r
1934 //\r
1935 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1936 // Therefore, in variable driver, only store the original value for other use.\r
1937 //\r
1938 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1939 FreePool (mVariableModuleGlobal->LangCodes);\r
1940 }\r
1941 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1942 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1943 }\r
1944\r
2d3fb919 1945 if (SetLanguageCodes\r
0c18794e 1946 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1947 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1948 //\r
1949 // Update Lang if PlatformLang is already set\r
1950 // Update PlatformLang if Lang is already set\r
1951 //\r
6675a21f 1952 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 1953 if (!EFI_ERROR (Status)) {\r
1954 //\r
1955 // Update Lang\r
1956 //\r
6675a21f 1957 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
0c18794e 1958 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1959 DataSize = Variable.CurrPtr->DataSize;\r
1960 } else {\r
6675a21f 1961 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 1962 if (!EFI_ERROR (Status)) {\r
1963 //\r
1964 // Update PlatformLang\r
1965 //\r
6675a21f 1966 VariableName = EFI_LANG_VARIABLE_NAME;\r
0c18794e 1967 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1968 DataSize = Variable.CurrPtr->DataSize;\r
1969 } else {\r
1970 //\r
1971 // Neither PlatformLang nor Lang is set, directly return\r
1972 //\r
9bc5dabb 1973 return EFI_SUCCESS;\r
0c18794e 1974 }\r
1975 }\r
1976 }\r
2d3fb919 1977\r
9bc5dabb
SZ
1978 Status = EFI_SUCCESS;\r
1979\r
0c18794e 1980 //\r
1981 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1982 //\r
1983 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
1984\r
6675a21f 1985 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
0c18794e 1986 //\r
1987 // Update Lang when PlatformLangCodes/LangCodes were set.\r
1988 //\r
1989 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1990 //\r
1991 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1992 //\r
1993 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1994 if (BestPlatformLang != NULL) {\r
1995 //\r
1996 // Get the corresponding index in language codes.\r
1997 //\r
1998 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
1999\r
2000 //\r
2001 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
2002 //\r
2003 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
2004\r
2005 //\r
9a12e582 2006 // Check the variable space for both Lang and PlatformLang variable.\r
b2bd493e 2007 //\r
9a12e582 2008 VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
b2bd493e
SZ
2009 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
2010 VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
20333c6d 2011\r
9a12e582 2012 VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r
b2bd493e
SZ
2013 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
2014 VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
2015 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
2016 //\r
2017 // No enough variable space to set both Lang and PlatformLang successfully.\r
2018 //\r
2019 Status = EFI_OUT_OF_RESOURCES;\r
2020 } else {\r
2021 //\r
2022 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
2023 //\r
2024 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2025\r
b2bd493e
SZ
2026 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
2027 ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);\r
2028 }\r
0c18794e 2029\r
9bc5dabb 2030 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r
0c18794e 2031 }\r
2032 }\r
2033\r
6675a21f 2034 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
0c18794e 2035 //\r
2036 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
2037 //\r
2038 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
2039 //\r
2040 // When setting Lang, firstly get most matched language string from supported language codes.\r
2041 //\r
2042 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
2043 if (BestLang != NULL) {\r
2044 //\r
2045 // Get the corresponding index in language codes.\r
2046 //\r
2047 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
2048\r
2049 //\r
2050 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
2051 //\r
2052 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
2053\r
2054 //\r
9a12e582 2055 // Check the variable space for both PlatformLang and Lang variable.\r
0c18794e 2056 //\r
9a12e582 2057 VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);\r
b2bd493e
SZ
2058 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
2059 VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
9a12e582
DG
2060\r
2061 VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
b2bd493e
SZ
2062 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
2063 VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r
2064 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
2065 //\r
2066 // No enough variable space to set both PlatformLang and Lang successfully.\r
2067 //\r
2068 Status = EFI_OUT_OF_RESOURCES;\r
2069 } else {\r
2070 //\r
2071 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
2072 //\r
2073 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2074\r
b2bd493e
SZ
2075 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang,\r
2076 AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);\r
2077 }\r
0c18794e 2078\r
9bc5dabb 2079 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
0c18794e 2080 }\r
2081 }\r
2082 }\r
9bc5dabb 2083\r
b2bd493e
SZ
2084 if (SetLanguageCodes) {\r
2085 //\r
2086 // Continue to set PlatformLangCodes or LangCodes.\r
2087 //\r
2088 return EFI_SUCCESS;\r
2089 } else {\r
2090 return Status;\r
2091 }\r
0c18794e 2092}\r
2093\r
2094/**\r
2095 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
2096 index of associated public key is needed.\r
2097\r
2098 @param[in] VariableName Name of variable.\r
2099 @param[in] VendorGuid Guid of variable.\r
2100 @param[in] Data Variable data.\r
2101 @param[in] DataSize Size of data. 0 means delete.\r
2102 @param[in] Attributes Attributes of the variable.\r
2103 @param[in] KeyIndex Index of associated public key.\r
2104 @param[in] MonotonicCount Value of associated monotonic count.\r
23b06935 2105 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
0c18794e 2106 @param[in] TimeStamp Value of associated TimeStamp.\r
2d3fb919 2107\r
0c18794e 2108 @retval EFI_SUCCESS The update operation is success.\r
2109 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
2110\r
2111**/\r
2112EFI_STATUS\r
2113UpdateVariable (\r
2114 IN CHAR16 *VariableName,\r
2115 IN EFI_GUID *VendorGuid,\r
2116 IN VOID *Data,\r
2117 IN UINTN DataSize,\r
2118 IN UINT32 Attributes OPTIONAL,\r
2119 IN UINT32 KeyIndex OPTIONAL,\r
2120 IN UINT64 MonotonicCount OPTIONAL,\r
23b06935 2121 IN OUT VARIABLE_POINTER_TRACK *CacheVariable,\r
0c18794e 2122 IN EFI_TIME *TimeStamp OPTIONAL\r
2123 )\r
2124{\r
2125 EFI_STATUS Status;\r
2126 VARIABLE_HEADER *NextVariable;\r
2127 UINTN ScratchSize;\r
732d199d 2128 UINTN MaxDataSize;\r
0c18794e 2129 UINTN VarNameOffset;\r
2130 UINTN VarDataOffset;\r
2131 UINTN VarNameSize;\r
2132 UINTN VarSize;\r
2133 BOOLEAN Volatile;\r
2134 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
2135 UINT8 State;\r
0c18794e 2136 VARIABLE_POINTER_TRACK *Variable;\r
2137 VARIABLE_POINTER_TRACK NvVariable;\r
2138 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2139 UINTN CacheOffset;\r
fddbbc66
SZ
2140 UINT8 *BufferForMerge;\r
2141 UINTN MergedBufSize;\r
2142 BOOLEAN DataReady;\r
0c18794e 2143 UINTN DataOffset;\r
952ba83c
SZ
2144 BOOLEAN IsCommonVariable;\r
2145 BOOLEAN IsCommonUserVariable;\r
0c18794e 2146\r
2147 if (mVariableModuleGlobal->FvbInstance == NULL) {\r
2148 //\r
2149 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.\r
2150 //\r
2151 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2152 //\r
2153 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
2154 //\r
2155 return EFI_NOT_AVAILABLE_YET;\r
2156 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2157 //\r
2158 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
2159 // The authenticated variable perhaps is not initialized, just return here.\r
2160 //\r
2161 return EFI_NOT_AVAILABLE_YET;\r
2162 }\r
2163 }\r
2164\r
2165 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
2166 Variable = CacheVariable;\r
2167 } else {\r
2168 //\r
2169 // Update/Delete existing NV variable.\r
2170 // CacheVariable points to the variable in the memory copy of Flash area\r
2171 // Now let Variable points to the same variable in Flash area.\r
2172 //\r
2173 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
2d3fb919 2174 Variable = &NvVariable;\r
0c18794e 2175 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
2176 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
2177 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
23b06935
SZ
2178 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
2179 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
2180 } else {\r
2181 Variable->InDeletedTransitionPtr = NULL;\r
2182 }\r
0c18794e 2183 Variable->Volatile = FALSE;\r
2d3fb919 2184 }\r
0c18794e 2185\r
2186 Fvb = mVariableModuleGlobal->FvbInstance;\r
0c18794e 2187\r
2188 //\r
2189 // Tricky part: Use scratch data area at the end of volatile variable store\r
2190 // as a temporary storage.\r
2191 //\r
2192 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
2193 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
fddbbc66
SZ
2194 SetMem (NextVariable, ScratchSize, 0xff);\r
2195 DataReady = FALSE;\r
0c18794e 2196\r
2197 if (Variable->CurrPtr != NULL) {\r
2198 //\r
2199 // Update/Delete existing variable.\r
2200 //\r
2d3fb919 2201 if (AtRuntime ()) {\r
0c18794e 2202 //\r
2d3fb919 2203 // If AtRuntime and the variable is Volatile and Runtime Access,\r
2204 // the volatile is ReadOnly, and SetVariable should be aborted and\r
0c18794e 2205 // return EFI_WRITE_PROTECTED.\r
2206 //\r
2207 if (Variable->Volatile) {\r
2208 Status = EFI_WRITE_PROTECTED;\r
2209 goto Done;\r
2210 }\r
2211 //\r
2212 // Only variable that have NV attributes can be updated/deleted in Runtime.\r
2213 //\r
2214 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2215 Status = EFI_INVALID_PARAMETER;\r
2d3fb919 2216 goto Done;\r
0c18794e 2217 }\r
20333c6d 2218\r
ecc722ad 2219 //\r
2220 // Only variable that have RT attributes can be updated/deleted in Runtime.\r
2221 //\r
2222 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r
2223 Status = EFI_INVALID_PARAMETER;\r
2224 goto Done;\r
2225 }\r
0c18794e 2226 }\r
2227\r
2228 //\r
2229 // Setting a data variable with no access, or zero DataSize attributes\r
2230 // causes it to be deleted.\r
2d3fb919 2231 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r
2232 // not delete the variable.\r
0c18794e 2233 //\r
2d3fb919 2234 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r
23b06935
SZ
2235 if (Variable->InDeletedTransitionPtr != NULL) {\r
2236 //\r
2237 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
2238 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2239 //\r
2240 State = Variable->InDeletedTransitionPtr->State;\r
2241 State &= VAR_DELETED;\r
2242 Status = UpdateVariableStore (\r
2243 &mVariableModuleGlobal->VariableGlobal,\r
2244 Variable->Volatile,\r
2245 FALSE,\r
2246 Fvb,\r
2247 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2248 sizeof (UINT8),\r
2249 &State\r
2250 );\r
2251 if (!EFI_ERROR (Status)) {\r
2252 if (!Variable->Volatile) {\r
0cc565de 2253 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
2254 CacheVariable->InDeletedTransitionPtr->State = State;\r
2255 }\r
2256 } else {\r
2257 goto Done;\r
2258 }\r
2259 }\r
2260\r
0c18794e 2261 State = Variable->CurrPtr->State;\r
2262 State &= VAR_DELETED;\r
2263\r
2264 Status = UpdateVariableStore (\r
2265 &mVariableModuleGlobal->VariableGlobal,\r
2266 Variable->Volatile,\r
2267 FALSE,\r
2268 Fvb,\r
2269 (UINTN) &Variable->CurrPtr->State,\r
2270 sizeof (UINT8),\r
2271 &State\r
2d3fb919 2272 );\r
0c18794e 2273 if (!EFI_ERROR (Status)) {\r
2274 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
2275 if (!Variable->Volatile) {\r
2276 CacheVariable->CurrPtr->State = State;\r
335e2681 2277 FlushHobVariableToFlash (VariableName, VendorGuid);\r
0c18794e 2278 }\r
2279 }\r
2d3fb919 2280 goto Done;\r
0c18794e 2281 }\r
2282 //\r
2283 // If the variable is marked valid, and the same data has been passed in,\r
2284 // then return to the caller immediately.\r
2285 //\r
2286 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
fddbbc66 2287 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0) &&\r
2d3fb919 2288 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
2289 (TimeStamp == NULL)) {\r
2290 //\r
2291 // Variable content unchanged and no need to update timestamp, just return.\r
2292 //\r
0c18794e 2293 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
2294 Status = EFI_SUCCESS;\r
2295 goto Done;\r
2296 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
2297 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
2298\r
2299 //\r
2300 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable\r
2301 //\r
2302 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
2d3fb919 2303 //\r
fddbbc66
SZ
2304 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.\r
2305 // From DataOffset of NextVariable is to save the existing variable data.\r
2d3fb919 2306 //\r
2307 DataOffset = sizeof (VARIABLE_HEADER) + Variable->CurrPtr->NameSize + GET_PAD_SIZE (Variable->CurrPtr->NameSize);\r
fddbbc66
SZ
2308 BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);\r
2309 CopyMem (BufferForMerge, (UINT8 *) ((UINTN) Variable->CurrPtr + DataOffset), Variable->CurrPtr->DataSize);\r
2d3fb919 2310\r
732d199d 2311 //\r
20333c6d 2312 // Set Max Common Variable Data Size as default MaxDataSize\r
732d199d 2313 //\r
fddbbc66 2314 MaxDataSize = PcdGet32 (PcdMaxVariableSize) - DataOffset;\r
732d199d 2315\r
5767f22f 2316 if ((CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
20333c6d
QL
2317 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
2318 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0))) ||\r
2319 (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
2d3fb919 2320 //\r
5767f22f 2321 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of\r
2d3fb919 2322 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.\r
2323 //\r
732d199d 2324 Status = AppendSignatureList (\r
fddbbc66 2325 BufferForMerge,\r
20333c6d 2326 Variable->CurrPtr->DataSize,\r
732d199d 2327 MaxDataSize - Variable->CurrPtr->DataSize,\r
fddbbc66
SZ
2328 Data,\r
2329 DataSize,\r
2330 &MergedBufSize\r
732d199d 2331 );\r
2332 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2333 //\r
fddbbc66 2334 // Signature List is too long, Failed to Append.\r
732d199d 2335 //\r
2336 Status = EFI_INVALID_PARAMETER;\r
2337 goto Done;\r
2338 }\r
2339\r
fddbbc66 2340 if (MergedBufSize == Variable->CurrPtr->DataSize) {\r
2d3fb919 2341 if ((TimeStamp == NULL) || CompareTimeStamp (TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
2342 //\r
2343 // New EFI_SIGNATURE_DATA is not found and timestamp is not later\r
2344 // than current timestamp, return EFI_SUCCESS directly.\r
2345 //\r
2346 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
2347 Status = EFI_SUCCESS;\r
2348 goto Done;\r
2349 }\r
2350 }\r
2351 } else {\r
2352 //\r
fddbbc66 2353 // For other Variables, append the new data to the end of existing data.\r
732d199d 2354 // Max Harware error record variable data size is different from common variable\r
2d3fb919 2355 //\r
732d199d 2356 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
fddbbc66 2357 MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r
732d199d 2358 }\r
2359\r
2360 if (Variable->CurrPtr->DataSize + DataSize > MaxDataSize) {\r
2361 //\r
fddbbc66 2362 // Existing data size + new data size exceed maximum variable size limitation.\r
732d199d 2363 //\r
2364 Status = EFI_INVALID_PARAMETER;\r
2365 goto Done;\r
2366 }\r
fddbbc66
SZ
2367 CopyMem ((UINT8*) ((UINTN) BufferForMerge + Variable->CurrPtr->DataSize), Data, DataSize);\r
2368 MergedBufSize = Variable->CurrPtr->DataSize + DataSize;\r
2d3fb919 2369 }\r
2370\r
0c18794e 2371 //\r
fddbbc66 2372 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r
0c18794e 2373 //\r
fddbbc66
SZ
2374 Data = BufferForMerge;\r
2375 DataSize = MergedBufSize;\r
2376 DataReady = TRUE;\r
0c18794e 2377 }\r
2378\r
2379 //\r
2380 // Mark the old variable as in delete transition.\r
2381 //\r
2382 State = Variable->CurrPtr->State;\r
2383 State &= VAR_IN_DELETED_TRANSITION;\r
2384\r
2385 Status = UpdateVariableStore (\r
2386 &mVariableModuleGlobal->VariableGlobal,\r
2387 Variable->Volatile,\r
2388 FALSE,\r
2389 Fvb,\r
2390 (UINTN) &Variable->CurrPtr->State,\r
2391 sizeof (UINT8),\r
2392 &State\r
2d3fb919 2393 );\r
0c18794e 2394 if (EFI_ERROR (Status)) {\r
2d3fb919 2395 goto Done;\r
2396 }\r
0c18794e 2397 if (!Variable->Volatile) {\r
2398 CacheVariable->CurrPtr->State = State;\r
2399 }\r
2d3fb919 2400 }\r
0c18794e 2401 } else {\r
2402 //\r
2403 // Not found existing variable. Create a new variable.\r
0c18794e 2404 //\r
2d3fb919 2405\r
2406 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
2407 Status = EFI_SUCCESS;\r
0c18794e 2408 goto Done;\r
2409 }\r
2d3fb919 2410\r
0c18794e 2411 //\r
2412 // Make sure we are trying to create a new variable.\r
2d3fb919 2413 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
0c18794e 2414 //\r
2415 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
2416 Status = EFI_NOT_FOUND;\r
2417 goto Done;\r
2418 }\r
2d3fb919 2419\r
0c18794e 2420 //\r
2421 // Only variable have NV|RT attribute can be created in Runtime.\r
2422 //\r
2423 if (AtRuntime () &&\r
2424 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
2425 Status = EFI_INVALID_PARAMETER;\r
2426 goto Done;\r
2d3fb919 2427 }\r
0c18794e 2428 }\r
2429\r
2430 //\r
2431 // Function part - create a new variable and copy the data.\r
2432 // Both update a variable and create a variable will come here.\r
fddbbc66 2433 //\r
0c18794e 2434 NextVariable->StartId = VARIABLE_DATA;\r
2435 //\r
2436 // NextVariable->State = VAR_ADDED;\r
2437 //\r
2438 NextVariable->Reserved = 0;\r
2439 NextVariable->PubKeyIndex = KeyIndex;\r
2440 NextVariable->MonotonicCount = MonotonicCount;\r
2d3fb919 2441 ZeroMem (&NextVariable->TimeStamp, sizeof (EFI_TIME));\r
0c18794e 2442\r
3b4151bc 2443 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2d3fb919 2444 (TimeStamp != NULL)) {\r
3b4151bc 2445 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
2446 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2447 } else {\r
0c18794e 2448 //\r
2449 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
2450 // when the new TimeStamp value is later than the current timestamp associated\r
2451 // with the variable, we need associate the new timestamp with the updated value.\r
2452 //\r
2d3fb919 2453 if (Variable->CurrPtr != NULL) {\r
2454 if (CompareTimeStamp (&Variable->CurrPtr->TimeStamp, TimeStamp)) {\r
2455 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2456 }\r
0c18794e 2457 }\r
3b4151bc 2458 }\r
0c18794e 2459 }\r
2460\r
2461 //\r
2d3fb919 2462 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
0c18794e 2463 // Attributes bitmask parameter of a GetVariable() call.\r
2464 //\r
2465 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2d3fb919 2466\r
0c18794e 2467 VarNameOffset = sizeof (VARIABLE_HEADER);\r
2468 VarNameSize = StrSize (VariableName);\r
2469 CopyMem (\r
2470 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
2471 VariableName,\r
2472 VarNameSize\r
2473 );\r
2474 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
fddbbc66
SZ
2475\r
2476 //\r
2477 // If DataReady is TRUE, it means the variable data has been saved into\r
2478 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r
2479 //\r
2480 if (!DataReady) {\r
2481 CopyMem (\r
2482 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
2483 Data,\r
2484 DataSize\r
2485 );\r
2486 }\r
2487\r
0c18794e 2488 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
2489 //\r
2490 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2491 // NextVariable->DataSize should not include pad size so that variable\r
2492 // service can get actual size in GetVariable.\r
2493 //\r
2494 NextVariable->NameSize = (UINT32)VarNameSize;\r
2495 NextVariable->DataSize = (UINT32)DataSize;\r
2496\r
2497 //\r
2498 // The actual size of the variable that stores in storage should\r
2499 // include pad size.\r
2500 //\r
2501 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
2502 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2503 //\r
2504 // Create a nonvolatile variable.\r
2505 //\r
2506 Volatile = FALSE;\r
952ba83c
SZ
2507\r
2508 IsCommonVariable = FALSE;\r
2509 IsCommonUserVariable = FALSE;\r
2510 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
2511 IsCommonVariable = TRUE;\r
2512 IsCommonUserVariable = IsUserVariable (NextVariable);\r
2513 }\r
2d3fb919 2514 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
0c18794e 2515 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
952ba83c
SZ
2516 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
2517 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
2518 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
0c18794e 2519 if (AtRuntime ()) {\r
952ba83c
SZ
2520 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2521 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2522 }\r
2523 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
2524 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2525 }\r
0c18794e 2526 Status = EFI_OUT_OF_RESOURCES;\r
2527 goto Done;\r
2528 }\r
2529 //\r
7baf3c69 2530 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
0c18794e 2531 //\r
83758cdc 2532 Status = Reclaim (\r
2533 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2534 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2535 FALSE,\r
23b06935 2536 Variable,\r
7baf3c69
SZ
2537 NextVariable,\r
2538 HEADER_ALIGN (VarSize),\r
83758cdc 2539 FALSE\r
2540 );\r
7baf3c69
SZ
2541 if (!EFI_ERROR (Status)) {\r
2542 //\r
2543 // The new variable has been integrated successfully during reclaiming.\r
2544 //\r
2545 if (Variable->CurrPtr != NULL) {\r
2546 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2547 CacheVariable->InDeletedTransitionPtr = NULL;\r
2548 }\r
2549 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
2550 FlushHobVariableToFlash (VariableName, VendorGuid);\r
952ba83c
SZ
2551 } else {\r
2552 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2553 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
17409b7a 2554 }\r
952ba83c
SZ
2555 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
2556 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2557 }\r
2558 }\r
7baf3c69 2559 goto Done;\r
0c18794e 2560 }\r
2561 //\r
2562 // Four steps\r
2563 // 1. Write variable header\r
2d3fb919 2564 // 2. Set variable state to header valid\r
0c18794e 2565 // 3. Write variable data\r
2566 // 4. Set variable state to valid\r
2567 //\r
2568 //\r
2569 // Step 1:\r
2570 //\r
2571 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
2572 Status = UpdateVariableStore (\r
2573 &mVariableModuleGlobal->VariableGlobal,\r
2574 FALSE,\r
2575 TRUE,\r
2576 Fvb,\r
2577 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2578 sizeof (VARIABLE_HEADER),\r
2579 (UINT8 *) NextVariable\r
2580 );\r
2581\r
2582 if (EFI_ERROR (Status)) {\r
2583 goto Done;\r
2584 }\r
2585\r
2586 //\r
2587 // Step 2:\r
2588 //\r
2589 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2590 Status = UpdateVariableStore (\r
2591 &mVariableModuleGlobal->VariableGlobal,\r
2592 FALSE,\r
2593 TRUE,\r
2594 Fvb,\r
2595 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2596 sizeof (UINT8),\r
2597 &NextVariable->State\r
2598 );\r
2599\r
2600 if (EFI_ERROR (Status)) {\r
2601 goto Done;\r
2602 }\r
2603 //\r
2604 // Step 3:\r
2605 //\r
2606 Status = UpdateVariableStore (\r
2607 &mVariableModuleGlobal->VariableGlobal,\r
2608 FALSE,\r
2609 TRUE,\r
2610 Fvb,\r
2611 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
2612 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
2613 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
2614 );\r
2615\r
2616 if (EFI_ERROR (Status)) {\r
2617 goto Done;\r
2618 }\r
2619 //\r
2620 // Step 4:\r
2621 //\r
2622 NextVariable->State = VAR_ADDED;\r
2623 Status = UpdateVariableStore (\r
2624 &mVariableModuleGlobal->VariableGlobal,\r
2625 FALSE,\r
2626 TRUE,\r
2627 Fvb,\r
2628 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2629 sizeof (UINT8),\r
2630 &NextVariable->State\r
2631 );\r
2632\r
2633 if (EFI_ERROR (Status)) {\r
2634 goto Done;\r
2635 }\r
2636\r
2637 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2638\r
2639 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2640 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2641 } else {\r
2642 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
952ba83c
SZ
2643 if (IsCommonUserVariable) {\r
2644 mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r
17409b7a 2645 }\r
952ba83c 2646 }\r
0c18794e 2647 //\r
2648 // update the memory copy of Flash region.\r
2649 //\r
2650 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
2651 } else {\r
2652 //\r
2653 // Create a volatile variable.\r
2d3fb919 2654 //\r
0c18794e 2655 Volatile = TRUE;\r
2656\r
2657 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
2658 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
2659 //\r
7baf3c69 2660 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
0c18794e 2661 //\r
83758cdc 2662 Status = Reclaim (\r
2663 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
2664 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
2665 TRUE,\r
23b06935 2666 Variable,\r
7baf3c69
SZ
2667 NextVariable,\r
2668 HEADER_ALIGN (VarSize),\r
83758cdc 2669 FALSE\r
2670 );\r
7baf3c69
SZ
2671 if (!EFI_ERROR (Status)) {\r
2672 //\r
2673 // The new variable has been integrated successfully during reclaiming.\r
2674 //\r
2675 if (Variable->CurrPtr != NULL) {\r
2676 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2677 CacheVariable->InDeletedTransitionPtr = NULL;\r
2678 }\r
2679 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
23b06935 2680 }\r
7baf3c69 2681 goto Done;\r
0c18794e 2682 }\r
2683\r
2684 NextVariable->State = VAR_ADDED;\r
2685 Status = UpdateVariableStore (\r
2686 &mVariableModuleGlobal->VariableGlobal,\r
2687 TRUE,\r
2688 TRUE,\r
2689 Fvb,\r
2690 mVariableModuleGlobal->VolatileLastVariableOffset,\r
2691 (UINT32) VarSize,\r
2692 (UINT8 *) NextVariable\r
2693 );\r
2694\r
2695 if (EFI_ERROR (Status)) {\r
2696 goto Done;\r
2697 }\r
2698\r
2699 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2700 }\r
2701\r
2702 //\r
2703 // Mark the old variable as deleted.\r
2704 //\r
23b06935
SZ
2705 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2706 if (Variable->InDeletedTransitionPtr != NULL) {\r
2707 //\r
2708 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2709 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2710 //\r
2711 State = Variable->InDeletedTransitionPtr->State;\r
2712 State &= VAR_DELETED;\r
2713 Status = UpdateVariableStore (\r
2714 &mVariableModuleGlobal->VariableGlobal,\r
2715 Variable->Volatile,\r
2716 FALSE,\r
2717 Fvb,\r
2718 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2719 sizeof (UINT8),\r
2720 &State\r
2721 );\r
2722 if (!EFI_ERROR (Status)) {\r
2723 if (!Variable->Volatile) {\r
0cc565de 2724 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
2725 CacheVariable->InDeletedTransitionPtr->State = State;\r
2726 }\r
2727 } else {\r
2728 goto Done;\r
2729 }\r
2730 }\r
2731\r
0c18794e 2732 State = Variable->CurrPtr->State;\r
2733 State &= VAR_DELETED;\r
2734\r
2735 Status = UpdateVariableStore (\r
2736 &mVariableModuleGlobal->VariableGlobal,\r
2737 Variable->Volatile,\r
2738 FALSE,\r
2739 Fvb,\r
2740 (UINTN) &Variable->CurrPtr->State,\r
2741 sizeof (UINT8),\r
2742 &State\r
2743 );\r
2d3fb919 2744 if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
0c18794e 2745 CacheVariable->CurrPtr->State = State;\r
2746 }\r
2747 }\r
2748\r
2749 if (!EFI_ERROR (Status)) {\r
2750 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
2751 if (!Volatile) {\r
2752 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2753 }\r
0c18794e 2754 }\r
2755\r
2756Done:\r
2757 return Status;\r
2758}\r
2759\r
a5f15e30
SZ
2760/**\r
2761 Check if a Unicode character is a hexadecimal character.\r
2762\r
20333c6d
QL
2763 This function checks if a Unicode character is a\r
2764 hexadecimal character. The valid hexadecimal character is\r
a5f15e30
SZ
2765 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
2766\r
2767\r
2768 @param Char The character to check against.\r
2769\r
2770 @retval TRUE If the Char is a hexadecmial character.\r
2771 @retval FALSE If the Char is not a hexadecmial character.\r
2772\r
2773**/\r
2774BOOLEAN\r
2775EFIAPI\r
2776IsHexaDecimalDigitCharacter (\r
2777 IN CHAR16 Char\r
2778 )\r
2779{\r
2780 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2781}\r
2782\r
2783/**\r
2784\r
2785 This code checks if variable is hardware error record variable or not.\r
2786\r
2787 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
2788 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
2789\r
2790 @param VariableName Pointer to variable name.\r
2791 @param VendorGuid Variable Vendor Guid.\r
2792\r
2793 @retval TRUE Variable is hardware error record variable.\r
2794 @retval FALSE Variable is not hardware error record variable.\r
2795\r
2796**/\r
2797BOOLEAN\r
2798EFIAPI\r
2799IsHwErrRecVariable (\r
2800 IN CHAR16 *VariableName,\r
2801 IN EFI_GUID *VendorGuid\r
2802 )\r
2803{\r
2804 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
2805 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
2806 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
2807 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
2808 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
2809 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
2810 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
2811 return FALSE;\r
2812 }\r
2813\r
2814 return TRUE;\r
2815}\r
2816\r
6ab9f441
RN
2817/**\r
2818 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
2819\r
2820 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
2821 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
2822 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
2823\r
2824 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
2825 as pending to be read-only.\r
2826 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
2827 Or VariableName is an empty string.\r
2828 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
2829 already been signaled.\r
2830 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
2831**/\r
2832EFI_STATUS\r
2833EFIAPI\r
2834VariableLockRequestToLock (\r
2835 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
2836 IN CHAR16 *VariableName,\r
2837 IN EFI_GUID *VendorGuid\r
2838 )\r
2839{\r
2840 VARIABLE_ENTRY *Entry;\r
17409b7a 2841 CHAR16 *Name;\r
6ab9f441
RN
2842\r
2843 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2844 return EFI_INVALID_PARAMETER;\r
2845 }\r
2846\r
2847 if (mEndOfDxe) {\r
2848 return EFI_ACCESS_DENIED;\r
2849 }\r
2850\r
17409b7a 2851 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (VariableName));\r
6ab9f441
RN
2852 if (Entry == NULL) {\r
2853 return EFI_OUT_OF_RESOURCES;\r
2854 }\r
2855\r
2856 DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
2857\r
2858 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2859\r
17409b7a
SZ
2860 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
2861 StrnCpy (Name, VariableName, StrLen (VariableName));\r
6ab9f441
RN
2862 CopyGuid (&Entry->Guid, VendorGuid);\r
2863 InsertTailList (&mLockedVariableList, &Entry->Link);\r
2864\r
2865 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2866\r
2867 return EFI_SUCCESS;\r
2868}\r
2869\r
0c18794e 2870/**\r
2871\r
2872 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2873\r
dc204d5a
JY
2874 Caution: This function may receive untrusted input.\r
2875 This function may be invoked in SMM mode, and datasize is external input.\r
2876 This function will do basic validation, before parse the data.\r
2877\r
0c18794e 2878 @param VariableName Name of Variable to be found.\r
2879 @param VendorGuid Variable vendor GUID.\r
2880 @param Attributes Attribute value of the variable found.\r
2881 @param DataSize Size of Data found. If size is less than the\r
2882 data, this value contains the required size.\r
2883 @param Data Data pointer.\r
2d3fb919 2884\r
0c18794e 2885 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2886 @return EFI_SUCCESS Find the specified variable.\r
2887 @return EFI_NOT_FOUND Not found.\r
2888 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2889\r
2890**/\r
2891EFI_STATUS\r
2892EFIAPI\r
2893VariableServiceGetVariable (\r
2894 IN CHAR16 *VariableName,\r
2895 IN EFI_GUID *VendorGuid,\r
2896 OUT UINT32 *Attributes OPTIONAL,\r
2897 IN OUT UINTN *DataSize,\r
2898 OUT VOID *Data\r
2899 )\r
2900{\r
2901 EFI_STATUS Status;\r
2902 VARIABLE_POINTER_TRACK Variable;\r
2903 UINTN VarDataSize;\r
2904\r
2905 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2906 return EFI_INVALID_PARAMETER;\r
2907 }\r
2908\r
2909 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2d3fb919 2910\r
ecc722ad 2911 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2912 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2913 goto Done;\r
2914 }\r
2915\r
2916 //\r
2917 // Get data size\r
2918 //\r
2919 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2920 ASSERT (VarDataSize != 0);\r
2921\r
2922 if (*DataSize >= VarDataSize) {\r
2923 if (Data == NULL) {\r
2924 Status = EFI_INVALID_PARAMETER;\r
2925 goto Done;\r
2926 }\r
2927\r
2928 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2929 if (Attributes != NULL) {\r
2930 *Attributes = Variable.CurrPtr->Attributes;\r
2931 }\r
2932\r
2933 *DataSize = VarDataSize;\r
2934 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2d3fb919 2935\r
0c18794e 2936 Status = EFI_SUCCESS;\r
2937 goto Done;\r
2938 } else {\r
2939 *DataSize = VarDataSize;\r
2940 Status = EFI_BUFFER_TOO_SMALL;\r
2941 goto Done;\r
2942 }\r
2943\r
2944Done:\r
2945 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2946 return Status;\r
2947}\r
2948\r
2949\r
2950\r
2951/**\r
2952\r
2953 This code Finds the Next available variable.\r
2954\r
dc204d5a
JY
2955 Caution: This function may receive untrusted input.\r
2956 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2957\r
0c18794e 2958 @param VariableNameSize Size of the variable name.\r
2959 @param VariableName Pointer to variable name.\r
2960 @param VendorGuid Variable Vendor Guid.\r
2961\r
2962 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2963 @return EFI_SUCCESS Find the specified variable.\r
2964 @return EFI_NOT_FOUND Not found.\r
2965 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2966\r
2967**/\r
2968EFI_STATUS\r
2969EFIAPI\r
2970VariableServiceGetNextVariableName (\r
2971 IN OUT UINTN *VariableNameSize,\r
2972 IN OUT CHAR16 *VariableName,\r
2973 IN OUT EFI_GUID *VendorGuid\r
2974 )\r
2975{\r
9a000b46 2976 VARIABLE_STORE_TYPE Type;\r
0c18794e 2977 VARIABLE_POINTER_TRACK Variable;\r
9a000b46 2978 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2979 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
0c18794e 2980 UINTN VarNameSize;\r
2981 EFI_STATUS Status;\r
9a000b46 2982 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
0c18794e 2983\r
2984 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2985 return EFI_INVALID_PARAMETER;\r
2986 }\r
2987\r
2988 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2989\r
ecc722ad 2990 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2991 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2992 goto Done;\r
2993 }\r
2994\r
2995 if (VariableName[0] != 0) {\r
2996 //\r
2997 // If variable name is not NULL, get next variable.\r
2998 //\r
2999 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3000 }\r
3001\r
9a000b46
RN
3002 //\r
3003 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
3004 // The index and attributes mapping must be kept in this order as FindVariable\r
3005 // makes use of this mapping to implement search algorithm.\r
3006 //\r
3007 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
3008 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3009 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
3010\r
0c18794e 3011 while (TRUE) {\r
3012 //\r
9a000b46 3013 // Switch from Volatile to HOB, to Non-Volatile.\r
0c18794e 3014 //\r
6ebffb67 3015 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
9a000b46
RN
3016 //\r
3017 // Find current storage index\r
3018 //\r
3019 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
3020 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
3021 break;\r
3022 }\r
3023 }\r
3024 ASSERT (Type < VariableStoreTypeMax);\r
3025 //\r
3026 // Switch to next storage\r
3027 //\r
3028 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
3029 if (VariableStoreHeader[Type] != NULL) {\r
3030 break;\r
3031 }\r
3032 }\r
3033 //\r
2d3fb919 3034 // Capture the case that\r
9a000b46
RN
3035 // 1. current storage is the last one, or\r
3036 // 2. no further storage\r
3037 //\r
3038 if (Type == VariableStoreTypeMax) {\r
0c18794e 3039 Status = EFI_NOT_FOUND;\r
3040 goto Done;\r
3041 }\r
9a000b46
RN
3042 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
3043 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
3044 Variable.CurrPtr = Variable.StartPtr;\r
0c18794e 3045 }\r
9a000b46 3046\r
0c18794e 3047 //\r
3048 // Variable is found\r
3049 //\r
23b06935
SZ
3050 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3051 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
3052 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3053 //\r
3054 // If it is a IN_DELETED_TRANSITION variable,\r
3055 // and there is also a same ADDED one at the same time,\r
3056 // don't return it.\r
3057 //\r
3058 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
3059 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
3060 Status = FindVariableEx (\r
3061 GetVariableNamePtr (Variable.CurrPtr),\r
3062 &Variable.CurrPtr->VendorGuid,\r
3063 FALSE,\r
3064 &VariablePtrTrack\r
3065 );\r
3066 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
3067 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3068 continue;\r
3069 }\r
3070 }\r
9a000b46
RN
3071\r
3072 //\r
3073 // Don't return NV variable when HOB overrides it\r
3074 //\r
2d3fb919 3075 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
9a000b46
RN
3076 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
3077 ) {\r
3078 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
3079 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
3080 Status = FindVariableEx (\r
3081 GetVariableNamePtr (Variable.CurrPtr),\r
3082 &Variable.CurrPtr->VendorGuid,\r
ecc722ad 3083 FALSE,\r
9a000b46
RN
3084 &VariableInHob\r
3085 );\r
3086 if (!EFI_ERROR (Status)) {\r
3087 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3088 continue;\r
3089 }\r
3090 }\r
3091\r
0c18794e 3092 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
3093 ASSERT (VarNameSize != 0);\r
3094\r
3095 if (VarNameSize <= *VariableNameSize) {\r
9a000b46
RN
3096 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
3097 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
0c18794e 3098 Status = EFI_SUCCESS;\r
3099 } else {\r
3100 Status = EFI_BUFFER_TOO_SMALL;\r
3101 }\r
3102\r
3103 *VariableNameSize = VarNameSize;\r
3104 goto Done;\r
3105 }\r
3106 }\r
3107\r
3108 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3109 }\r
3110\r
3111Done:\r
3112 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3113 return Status;\r
3114}\r
3115\r
3116/**\r
3117\r
3118 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
3119\r
dc204d5a
JY
3120 Caution: This function may receive untrusted input.\r
3121 This function may be invoked in SMM mode, and datasize and data are external input.\r
3122 This function will do basic validation, before parse the data.\r
3123 This function will parse the authentication carefully to avoid security issues, like\r
3124 buffer overflow, integer overflow.\r
3125 This function will check attribute carefully to avoid authentication bypass.\r
3126\r
0c18794e 3127 @param VariableName Name of Variable to be found.\r
3128 @param VendorGuid Variable vendor GUID.\r
3129 @param Attributes Attribute value of the variable found\r
3130 @param DataSize Size of Data found. If size is less than the\r
3131 data, this value contains the required size.\r
3132 @param Data Data pointer.\r
3133\r
3134 @return EFI_INVALID_PARAMETER Invalid parameter.\r
3135 @return EFI_SUCCESS Set successfully.\r
3136 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
3137 @return EFI_NOT_FOUND Not found.\r
3138 @return EFI_WRITE_PROTECTED Variable is read-only.\r
3139\r
3140**/\r
3141EFI_STATUS\r
3142EFIAPI\r
3143VariableServiceSetVariable (\r
3144 IN CHAR16 *VariableName,\r
3145 IN EFI_GUID *VendorGuid,\r
3146 IN UINT32 Attributes,\r
3147 IN UINTN DataSize,\r
3148 IN VOID *Data\r
3149 )\r
3150{\r
3151 VARIABLE_POINTER_TRACK Variable;\r
3152 EFI_STATUS Status;\r
3153 VARIABLE_HEADER *NextVariable;\r
3154 EFI_PHYSICAL_ADDRESS Point;\r
3155 UINTN PayloadSize;\r
6ab9f441
RN
3156 LIST_ENTRY *Link;\r
3157 VARIABLE_ENTRY *Entry;\r
17409b7a 3158 CHAR16 *Name;\r
0c18794e 3159\r
3160 //\r
3161 // Check input parameters.\r
3162 //\r
3163 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
3164 return EFI_INVALID_PARAMETER;\r
2d3fb919 3165 }\r
0c18794e 3166\r
3167 if (DataSize != 0 && Data == NULL) {\r
3168 return EFI_INVALID_PARAMETER;\r
3169 }\r
3170\r
275beb2b 3171 //\r
3172 // Check for reserverd bit in variable attribute.\r
3173 //\r
3174 if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
3175 return EFI_INVALID_PARAMETER;\r
3176 }\r
3177\r
0c18794e 3178 //\r
3179 // Make sure if runtime bit is set, boot service bit is set also.\r
3180 //\r
3181 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3182 return EFI_INVALID_PARAMETER;\r
3183 }\r
3184\r
3185 //\r
2d3fb919 3186 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
0c18794e 3187 // cannot be set both.\r
3188 //\r
2d3fb919 3189 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
0c18794e 3190 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
3191 return EFI_INVALID_PARAMETER;\r
2d3fb919 3192 }\r
0c18794e 3193\r
3194 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
3195 if (DataSize < AUTHINFO_SIZE) {\r
3196 //\r
2d3fb919 3197 // Try to write Authenticated Variable without AuthInfo.\r
0c18794e 3198 //\r
3199 return EFI_SECURITY_VIOLATION;\r
2d3fb919 3200 }\r
3201 PayloadSize = DataSize - AUTHINFO_SIZE;\r
3202 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
3203 //\r
3204 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
3205 //\r
3206 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
6bc4e19f 3207 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
3208 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
2d3fb919 3209 return EFI_SECURITY_VIOLATION;\r
3210 }\r
3211 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
0c18794e 3212 } else {\r
2d3fb919 3213 PayloadSize = DataSize;\r
0c18794e 3214 }\r
2d3fb919 3215\r
56251c66 3216 if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
3217 //\r
20333c6d
QL
3218 // Prevent whole variable size overflow\r
3219 //\r
56251c66 3220 return EFI_INVALID_PARAMETER;\r
3221 }\r
3222\r
0c18794e 3223 //\r
3224 // The size of the VariableName, including the Unicode Null in bytes plus\r
3225 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
3226 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
3227 //\r
3228 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
56251c66 3229 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
0c18794e 3230 return EFI_INVALID_PARAMETER;\r
3231 }\r
a5f15e30 3232 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
0c18794e 3233 return EFI_INVALID_PARAMETER;\r
3234 }\r
3235 } else {\r
3236 //\r
3237 // The size of the VariableName, including the Unicode Null in bytes plus\r
3238 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
3239 //\r
56251c66 3240 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
0c18794e 3241 return EFI_INVALID_PARAMETER;\r
2d3fb919 3242 }\r
3243 }\r
0c18794e 3244\r
3245 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3246\r
3247 //\r
3248 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
3249 //\r
3250 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
3251 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
3252 //\r
3253 // Parse non-volatile variable data and get last variable offset.\r
3254 //\r
3255 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
6ebffb67 3256 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
0c18794e 3257 NextVariable = GetNextVariablePtr (NextVariable);\r
3258 }\r
3259 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
3260 }\r
3261\r
6ab9f441
RN
3262 if (mEndOfDxe && mEnableLocking) {\r
3263 //\r
3264 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
3265 //\r
3266 for ( Link = GetFirstNode (&mLockedVariableList)\r
3267 ; !IsNull (&mLockedVariableList, Link)\r
3268 ; Link = GetNextNode (&mLockedVariableList, Link)\r
3269 ) {\r
3270 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
17409b7a
SZ
3271 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
3272 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
6ab9f441
RN
3273 Status = EFI_WRITE_PROTECTED;\r
3274 DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
3275 goto Done;\r
3276 }\r
3277 }\r
3278 }\r
3279\r
17409b7a
SZ
3280 Status = InternalVarCheckSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r
3281 if (EFI_ERROR (Status)) {\r
3282 goto Done;\r
3283 }\r
3284\r
0c18794e 3285 //\r
3286 // Check whether the input variable is already existed.\r
3287 //\r
ecc722ad 3288 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
3289 if (!EFI_ERROR (Status)) {\r
3290 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
6ab9f441
RN
3291 Status = EFI_WRITE_PROTECTED;\r
3292 goto Done;\r
ecc722ad 3293 }\r
6e67fec0
SZ
3294 if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r
3295 //\r
3296 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
3297 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
3298 // 1. No access attributes specified\r
3299 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
3300 //\r
3301 Status = EFI_INVALID_PARAMETER;\r
952ba83c 3302 DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable.CurrPtr->Attributes, Attributes, VendorGuid, VariableName));\r
6e67fec0
SZ
3303 goto Done;\r
3304 }\r
ecc722ad 3305 }\r
b2bd493e
SZ
3306\r
3307 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
9bc5dabb 3308 //\r
b2bd493e 3309 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
9bc5dabb 3310 //\r
b2bd493e
SZ
3311 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
3312 if (EFI_ERROR (Status)) {\r
3313 //\r
3314 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
3315 //\r
3316 goto Done;\r
3317 }\r
9bc5dabb
SZ
3318 }\r
3319\r
0c18794e 3320 //\r
3321 // Process PK, KEK, Sigdb seperately.\r
3322 //\r
3323 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
3324 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
3325 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
3326 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
20333c6d 3327 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
d547f31c
LE
3328 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) ||\r
3329 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
3330 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)\r
3331 )\r
3332 ) {\r
05a643f9 3333 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
3334 if (EFI_ERROR (Status)) {\r
3335 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
3336 }\r
0c18794e 3337 } else {\r
3338 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
3339 }\r
3340\r
6ab9f441 3341Done:\r
0c18794e 3342 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
3343 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3344\r
c1d93242
JY
3345 if (!AtRuntime ()) {\r
3346 if (!EFI_ERROR (Status)) {\r
3347 SecureBootHook (\r
3348 VariableName,\r
3349 VendorGuid\r
3350 );\r
3351 }\r
3352 }\r
3353\r
0c18794e 3354 return Status;\r
3355}\r
3356\r
3357/**\r
3358\r
3359 This code returns information about the EFI variables.\r
3360\r
dc204d5a
JY
3361 Caution: This function may receive untrusted input.\r
3362 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3363\r
0c18794e 3364 @param Attributes Attributes bitmask to specify the type of variables\r
3365 on which to return information.\r
3366 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3367 for the EFI variables associated with the attributes specified.\r
3368 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3369 for EFI variables associated with the attributes specified.\r
3370 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3371 associated with the attributes specified.\r
3372\r
0c18794e 3373 @return EFI_SUCCESS Query successfully.\r
0c18794e 3374\r
3375**/\r
3376EFI_STATUS\r
3377EFIAPI\r
b2bd493e 3378VariableServiceQueryVariableInfoInternal (\r
0c18794e 3379 IN UINT32 Attributes,\r
3380 OUT UINT64 *MaximumVariableStorageSize,\r
3381 OUT UINT64 *RemainingVariableStorageSize,\r
3382 OUT UINT64 *MaximumVariableSize\r
3383 )\r
3384{\r
3385 VARIABLE_HEADER *Variable;\r
3386 VARIABLE_HEADER *NextVariable;\r
3387 UINT64 VariableSize;\r
3388 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3389 UINT64 CommonVariableTotalSize;\r
3390 UINT64 HwErrVariableTotalSize;\r
b2bd493e
SZ
3391 EFI_STATUS Status;\r
3392 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
0c18794e 3393\r
3394 CommonVariableTotalSize = 0;\r
3395 HwErrVariableTotalSize = 0;\r
3396\r
0c18794e 3397 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
3398 //\r
3399 // Query is Volatile related.\r
3400 //\r
3401 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
3402 } else {\r
3403 //\r
3404 // Query is Non-Volatile related.\r
3405 //\r
3406 VariableStoreHeader = mNvVariableCache;\r
3407 }\r
3408\r
3409 //\r
3410 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
3411 // with the storage size (excluding the storage header size).\r
3412 //\r
3413 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
3414\r
3415 //\r
3416 // Harware error record variable needs larger size.\r
3417 //\r
3418 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3419 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3420 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
3421 } else {\r
3422 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
952ba83c
SZ
3423 if (AtRuntime ()) {\r
3424 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
3425 } else {\r
3426 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
3427 }\r
0c18794e 3428 }\r
3429\r
3430 //\r
3431 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
3432 //\r
3433 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
3434 }\r
3435\r
3436 //\r
3437 // Point to the starting address of the variables.\r
3438 //\r
3439 Variable = GetStartPointer (VariableStoreHeader);\r
3440\r
3441 //\r
3442 // Now walk through the related variable store.\r
3443 //\r
6ebffb67 3444 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
0c18794e 3445 NextVariable = GetNextVariablePtr (Variable);\r
3446 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
3447\r
3448 if (AtRuntime ()) {\r
3449 //\r
3450 // We don't take the state of the variables in mind\r
3451 // when calculating RemainingVariableStorageSize,\r
3452 // since the space occupied by variables not marked with\r
3453 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
3454 //\r
3455 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3456 HwErrVariableTotalSize += VariableSize;\r
3457 } else {\r
3458 CommonVariableTotalSize += VariableSize;\r
3459 }\r
3460 } else {\r
3461 //\r
3462 // Only care about Variables with State VAR_ADDED, because\r
3463 // the space not marked as VAR_ADDED is reclaimable now.\r
3464 //\r
3465 if (Variable->State == VAR_ADDED) {\r
3466 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3467 HwErrVariableTotalSize += VariableSize;\r
3468 } else {\r
3469 CommonVariableTotalSize += VariableSize;\r
3470 }\r
b2bd493e
SZ
3471 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3472 //\r
3473 // If it is a IN_DELETED_TRANSITION variable,\r
3474 // and there is not also a same ADDED one at the same time,\r
3475 // this IN_DELETED_TRANSITION variable is valid.\r
3476 //\r
3477 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
3478 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
3479 Status = FindVariableEx (\r
3480 GetVariableNamePtr (Variable),\r
3481 &Variable->VendorGuid,\r
3482 FALSE,\r
3483 &VariablePtrTrack\r
3484 );\r
3485 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
3486 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3487 HwErrVariableTotalSize += VariableSize;\r
3488 } else {\r
3489 CommonVariableTotalSize += VariableSize;\r
3490 }\r
3491 }\r
0c18794e 3492 }\r
3493 }\r
3494\r
3495 //\r
3496 // Go to the next one.\r
3497 //\r
3498 Variable = NextVariable;\r
3499 }\r
3500\r
3501 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
3502 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
952ba83c
SZ
3503 } else {\r
3504 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
3505 *RemainingVariableStorageSize = 0;\r
3506 } else {\r
3507 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
3508 }\r
0c18794e 3509 }\r
3510\r
3511 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
3512 *MaximumVariableSize = 0;\r
3513 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
3514 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
3515 }\r
3516\r
0c18794e 3517 return EFI_SUCCESS;\r
3518}\r
3519\r
b2bd493e
SZ
3520/**\r
3521\r
3522 This code returns information about the EFI variables.\r
3523\r
3524 Caution: This function may receive untrusted input.\r
3525 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3526\r
3527 @param Attributes Attributes bitmask to specify the type of variables\r
3528 on which to return information.\r
3529 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3530 for the EFI variables associated with the attributes specified.\r
3531 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3532 for EFI variables associated with the attributes specified.\r
3533 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3534 associated with the attributes specified.\r
3535\r
3536 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
3537 @return EFI_SUCCESS Query successfully.\r
3538 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
3539\r
3540**/\r
3541EFI_STATUS\r
3542EFIAPI\r
3543VariableServiceQueryVariableInfo (\r
3544 IN UINT32 Attributes,\r
3545 OUT UINT64 *MaximumVariableStorageSize,\r
3546 OUT UINT64 *RemainingVariableStorageSize,\r
3547 OUT UINT64 *MaximumVariableSize\r
3548 )\r
3549{\r
3550 EFI_STATUS Status;\r
3551\r
3552 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
3553 return EFI_INVALID_PARAMETER;\r
3554 }\r
3555\r
952ba83c 3556 if ((Attributes & VARIABLE_ATTRIBUTE_NV_BS_RT_AT_HR_AW) == 0) {\r
b2bd493e
SZ
3557 //\r
3558 // Make sure the Attributes combination is supported by the platform.\r
3559 //\r
3560 return EFI_UNSUPPORTED;\r
3561 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3562 //\r
3563 // Make sure if runtime bit is set, boot service bit is set also.\r
3564 //\r
3565 return EFI_INVALID_PARAMETER;\r
3566 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
3567 //\r
3568 // Make sure RT Attribute is set if we are in Runtime phase.\r
3569 //\r
3570 return EFI_INVALID_PARAMETER;\r
3571 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3572 //\r
3573 // Make sure Hw Attribute is set with NV.\r
3574 //\r
3575 return EFI_INVALID_PARAMETER;\r
3576 }\r
3577\r
3578 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3579\r
3580 Status = VariableServiceQueryVariableInfoInternal (\r
3581 Attributes,\r
3582 MaximumVariableStorageSize,\r
3583 RemainingVariableStorageSize,\r
3584 MaximumVariableSize\r
3585 );\r
3586\r
3587 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3588 return Status;\r
3589}\r
0c18794e 3590\r
3591/**\r
3592 This function reclaims variable storage if free size is below the threshold.\r
2d3fb919 3593\r
876ac395 3594 Caution: This function may be invoked at SMM mode.\r
3595 Care must be taken to make sure not security issue.\r
dc204d5a 3596\r
0c18794e 3597**/\r
3598VOID\r
3599ReclaimForOS(\r
3600 VOID\r
3601 )\r
3602{\r
3603 EFI_STATUS Status;\r
952ba83c 3604 UINTN RemainingCommonRuntimeVariableSpace;\r
0c18794e 3605 UINTN RemainingHwErrVariableSpace;\r
93626a53
SZ
3606 STATIC BOOLEAN Reclaimed;\r
3607\r
3608 //\r
3609 // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
3610 //\r
3611 if (Reclaimed) {\r
3612 return;\r
3613 }\r
3614 Reclaimed = TRUE;\r
0c18794e 3615\r
2d3fb919 3616 Status = EFI_SUCCESS;\r
0c18794e 3617\r
952ba83c
SZ
3618 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
3619 RemainingCommonRuntimeVariableSpace = 0;\r
3620 } else {\r
3621 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
3622 }\r
0c18794e 3623\r
3624 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
3625 //\r
952ba83c 3626 // Check if the free area is below a threshold.\r
0c18794e 3627 //\r
952ba83c 3628 if ((RemainingCommonRuntimeVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
2d3fb919 3629 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
0c18794e 3630 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
3631 Status = Reclaim (\r
3632 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3633 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3634 FALSE,\r
335e2681 3635 NULL,\r
7baf3c69
SZ
3636 NULL,\r
3637 0,\r
335e2681 3638 FALSE\r
0c18794e 3639 );\r
3640 ASSERT_EFI_ERROR (Status);\r
3641 }\r
3642}\r
3643\r
039a40aa
SZ
3644/**\r
3645 Init non-volatile variable store.\r
3646\r
3647 @retval EFI_SUCCESS Function successfully executed.\r
3648 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3649 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3650\r
3651**/\r
3652EFI_STATUS\r
3653InitNonVolatileVariableStore (\r
3654 VOID\r
3655 )\r
3656{\r
3657 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
952ba83c 3658 VARIABLE_HEADER *Variable;\r
039a40aa
SZ
3659 VARIABLE_HEADER *NextVariable;\r
3660 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3661 UINT64 VariableStoreLength;\r
3662 UINTN VariableSize;\r
3663 EFI_HOB_GUID_TYPE *GuidHob;\r
3664 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3665 UINT8 *NvStorageData;\r
3666 UINT32 NvStorageSize;\r
3667 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
3668 UINT32 BackUpOffset;\r
3669 UINT32 BackUpSize;\r
952ba83c
SZ
3670 UINT32 HwErrStorageSize;\r
3671 UINT32 MaxUserNvVariableSpaceSize;\r
3672 UINT32 BoottimeReservedNvVariableSpaceSize;\r
039a40aa
SZ
3673\r
3674 mVariableModuleGlobal->FvbInstance = NULL;\r
3675\r
039a40aa
SZ
3676 //\r
3677 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3678 // Keep the memory and the FLASH in sync as updates occur.\r
3679 //\r
3680 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
3681 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
3682 if (NvStorageData == NULL) {\r
3683 return EFI_OUT_OF_RESOURCES;\r
3684 }\r
3685\r
3686 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3687 if (NvStorageBase == 0) {\r
3688 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3689 }\r
3690 //\r
3691 // Copy NV storage data to the memory buffer.\r
3692 //\r
3693 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3694\r
3695 //\r
3696 // Check the FTW last write data hob.\r
3697 //\r
3698 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3699 if (GuidHob != NULL) {\r
3700 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3701 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3702 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3703 //\r
3704 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3705 //\r
3706 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3707 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3708 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3709 //\r
3710 // Flash NV storage from the Offset is backed up in spare block.\r
3711 //\r
3712 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3713 BackUpSize = NvStorageSize - BackUpOffset;\r
3714 DEBUG ((EFI_D_INFO, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress));\r
3715 //\r
3716 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3717 //\r
3718 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3719 }\r
3720 }\r
3721\r
3722 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3723\r
3724 //\r
3725 // Check if the Firmware Volume is not corrupted\r
3726 //\r
3727 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3728 FreePool (NvStorageData);\r
3729 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3730 return EFI_VOLUME_CORRUPTED;\r
3731 }\r
3732\r
3733 VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
3734 VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
3735\r
3736 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3737 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3738 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
3739 FreePool (NvStorageData);\r
3740 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3741 return EFI_VOLUME_CORRUPTED;\r
3742 }\r
3743 ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
3744\r
952ba83c
SZ
3745\r
3746 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3747\r
3748 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3749 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
3750 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
3751\r
3752 //\r
3753 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3754 // is stored with common variable in the same NV region. So the platform integrator should\r
3755 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
3756 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
3757 //\r
3758 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3759 //\r
3760 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
3761 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
3762 //\r
3763 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3764 //\r
3765 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
3766 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
3767 //\r
3768 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3769\r
3770 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3771 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
3772 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
3773\r
3774 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
3775\r
039a40aa
SZ
3776 //\r
3777 // The max variable or hardware error variable size should be < variable store size.\r
3778 //\r
3779 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) < VariableStoreLength);\r
3780\r
3781 //\r
3782 // Parse non-volatile variable data and get last variable offset.\r
3783 //\r
952ba83c
SZ
3784 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3785 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
3786 NextVariable = GetNextVariablePtr (Variable);\r
3787 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
3788 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3789 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
039a40aa 3790 } else {\r
952ba83c 3791 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
039a40aa
SZ
3792 }\r
3793\r
952ba83c 3794 Variable = NextVariable;\r
039a40aa 3795 }\r
952ba83c 3796 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
039a40aa
SZ
3797\r
3798 return EFI_SUCCESS;\r
3799}\r
3800\r
335e2681
SZ
3801/**\r
3802 Flush the HOB variable to flash.\r
3803\r
3804 @param[in] VariableName Name of variable has been updated or deleted.\r
3805 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3806\r
3807**/\r
3808VOID\r
3809FlushHobVariableToFlash (\r
3810 IN CHAR16 *VariableName,\r
3811 IN EFI_GUID *VendorGuid\r
3812 )\r
3813{\r
3814 EFI_STATUS Status;\r
3815 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3816 VARIABLE_HEADER *Variable;\r
3817 VOID *VariableData;\r
3818 BOOLEAN ErrorFlag;\r
3819\r
3820 ErrorFlag = FALSE;\r
3821\r
3822 //\r
3823 // Flush the HOB variable to flash.\r
3824 //\r
3825 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3826 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3827 //\r
3828 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3829 //\r
3830 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3831 for ( Variable = GetStartPointer (VariableStoreHeader)\r
6ebffb67 3832 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
335e2681
SZ
3833 ; Variable = GetNextVariablePtr (Variable)\r
3834 ) {\r
3835 if (Variable->State != VAR_ADDED) {\r
3836 //\r
3837 // The HOB variable has been set to DELETED state in local.\r
3838 //\r
3839 continue;\r
3840 }\r
3841 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3842 if (VendorGuid == NULL || VariableName == NULL ||\r
3843 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
3844 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3845 VariableData = GetVariableDataPtr (Variable);\r
3846 Status = VariableServiceSetVariable (\r
3847 GetVariableNamePtr (Variable),\r
3848 &Variable->VendorGuid,\r
3849 Variable->Attributes,\r
3850 Variable->DataSize,\r
3851 VariableData\r
3852 );\r
3853 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
3854 } else {\r
3855 //\r
3856 // The updated or deleted variable is matched with the HOB variable.\r
3857 // Don't break here because we will try to set other HOB variables\r
3858 // since this variable could be set successfully.\r
3859 //\r
3860 Status = EFI_SUCCESS;\r
3861 }\r
3862 if (!EFI_ERROR (Status)) {\r
3863 //\r
3864 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3865 // set the HOB variable to DELETED state in local.\r
3866 //\r
3867 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
3868 Variable->State &= VAR_DELETED;\r
3869 } else {\r
3870 ErrorFlag = TRUE;\r
3871 }\r
3872 }\r
3873 if (ErrorFlag) {\r
3874 //\r
3875 // We still have HOB variable(s) not flushed in flash.\r
3876 //\r
3877 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3878 } else {\r
3879 //\r
3880 // All HOB variables have been flushed in flash.\r
3881 //\r
3882 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3883 if (!AtRuntime ()) {\r
3884 FreePool ((VOID *) VariableStoreHeader);\r
3885 }\r
3886 }\r
3887 }\r
3888\r
3889}\r
0c18794e 3890\r
3891/**\r
039a40aa 3892 Initializes variable write service after FTW was ready.\r
0c18794e 3893\r
3894 @retval EFI_SUCCESS Function successfully executed.\r
3895 @retval Others Fail to initialize the variable service.\r
3896\r
3897**/\r
3898EFI_STATUS\r
3899VariableWriteServiceInitialize (\r
3900 VOID\r
3901 )\r
3902{\r
3903 EFI_STATUS Status;\r
3904 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3905 UINTN Index;\r
3906 UINT8 Data;\r
3907 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
039a40aa
SZ
3908 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3909\r
3910 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3911 if (NvStorageBase == 0) {\r
3912 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3913 }\r
3914 VariableStoreBase = NvStorageBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageBase))->HeaderLength);\r
0c18794e 3915\r
039a40aa
SZ
3916 //\r
3917 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
3918 //\r
3919 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
0c18794e 3920 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2d3fb919 3921\r
0c18794e 3922 //\r
3923 // Check if the free area is really free.\r
3924 //\r
9a000b46 3925 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
0c18794e 3926 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3927 if (Data != 0xff) {\r
3928 //\r
3929 // There must be something wrong in variable store, do reclaim operation.\r
3930 //\r
3931 Status = Reclaim (\r
3932 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3933 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3934 FALSE,\r
335e2681 3935 NULL,\r
7baf3c69
SZ
3936 NULL,\r
3937 0,\r
3938 FALSE\r
0c18794e 3939 );\r
3940 if (EFI_ERROR (Status)) {\r
3941 return Status;\r
3942 }\r
3943 break;\r
3944 }\r
3945 }\r
3946\r
335e2681 3947 FlushHobVariableToFlash (NULL, NULL);\r
9a000b46 3948\r
0c18794e 3949 //\r
3950 // Authenticated variable initialize.\r
3951 //\r
3952 Status = AutenticatedVariableServiceInitialize ();\r
3953\r
3954 return Status;\r
3955}\r
3956\r
3957\r
3958/**\r
3959 Initializes variable store area for non-volatile and volatile variable.\r
3960\r
3961 @retval EFI_SUCCESS Function successfully executed.\r
3962 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3963\r
3964**/\r
3965EFI_STATUS\r
3966VariableCommonInitialize (\r
3967 VOID\r
3968 )\r
3969{\r
3970 EFI_STATUS Status;\r
3971 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
3972 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
0c18794e 3973 UINT64 VariableStoreLength;\r
3974 UINTN ScratchSize;\r
9a000b46 3975 EFI_HOB_GUID_TYPE *GuidHob;\r
0c18794e 3976\r
3977 //\r
3978 // Allocate runtime memory for variable driver global structure.\r
3979 //\r
3980 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
3981 if (mVariableModuleGlobal == NULL) {\r
3982 return EFI_OUT_OF_RESOURCES;\r
3983 }\r
3984\r
3985 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
3986\r
9a000b46
RN
3987 //\r
3988 // Get HOB variable store.\r
3989 //\r
3990 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
3991 if (GuidHob != NULL) {\r
3992 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 3993 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
9a000b46 3994 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
3995 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
3996 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
039a40aa 3997 FreePool (mVariableModuleGlobal);\r
335e2681
SZ
3998 return EFI_OUT_OF_RESOURCES;\r
3999 }\r
9a000b46
RN
4000 } else {\r
4001 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
4002 }\r
4003 }\r
4004\r
0c18794e 4005 //\r
4006 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
4007 //\r
4008 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
4009 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
4010 if (VolatileVariableStore == NULL) {\r
039a40aa
SZ
4011 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
4012 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
4013 }\r
0c18794e 4014 FreePool (mVariableModuleGlobal);\r
4015 return EFI_OUT_OF_RESOURCES;\r
4016 }\r
4017\r
4018 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
4019\r
4020 //\r
4021 // Initialize Variable Specific Data.\r
4022 //\r
4023 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
4024 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
0c18794e 4025\r
4026 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
4027 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
4028 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
4029 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
4030 VolatileVariableStore->Reserved = 0;\r
4031 VolatileVariableStore->Reserved1 = 0;\r
4032\r
4033 //\r
039a40aa 4034 // Init non-volatile variable store.\r
0c18794e 4035 //\r
039a40aa 4036 Status = InitNonVolatileVariableStore ();\r
0c18794e 4037 if (EFI_ERROR (Status)) {\r
039a40aa
SZ
4038 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
4039 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
4040 }\r
0c18794e 4041 FreePool (mVariableModuleGlobal);\r
4042 FreePool (VolatileVariableStore);\r
4043 }\r
4044\r
4045 return Status;\r
4046}\r
4047\r
4048\r
4049/**\r
4050 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
4051\r
4052 @param[in] Address The Flash address.\r
4053 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
4054 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
4055\r
4056**/\r
4057EFI_STATUS\r
4058GetFvbInfoByAddress (\r
4059 IN EFI_PHYSICAL_ADDRESS Address,\r
4060 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
4061 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
4062 )\r
4063{\r
4064 EFI_STATUS Status;\r
4065 EFI_HANDLE *HandleBuffer;\r
4066 UINTN HandleCount;\r
4067 UINTN Index;\r
4068 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
4069 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
0c18794e 4070 EFI_FVB_ATTRIBUTES_2 Attributes;\r
931aae94
DG
4071 UINTN BlockSize;\r
4072 UINTN NumberOfBlocks;\r
2d3fb919 4073\r
d4193108 4074 HandleBuffer = NULL;\r
0c18794e 4075 //\r
4076 // Get all FVB handles.\r
4077 //\r
4078 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
4079 if (EFI_ERROR (Status)) {\r
4080 return EFI_NOT_FOUND;\r
4081 }\r
4082\r
4083 //\r
4084 // Get the FVB to access variable store.\r
4085 //\r
4086 Fvb = NULL;\r
4087 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
4088 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
4089 if (EFI_ERROR (Status)) {\r
4090 Status = EFI_NOT_FOUND;\r
4091 break;\r
4092 }\r
4093\r
4094 //\r
4095 // Ensure this FVB protocol supported Write operation.\r
4096 //\r
4097 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
4098 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2d3fb919 4099 continue;\r
0c18794e 4100 }\r
2d3fb919 4101\r
0c18794e 4102 //\r
4103 // Compare the address and select the right one.\r
4104 //\r
4105 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
4106 if (EFI_ERROR (Status)) {\r
4107 continue;\r
4108 }\r
4109\r
931aae94
DG
4110 //\r
4111 // Assume one FVB has one type of BlockSize.\r
4112 //\r
4113 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
4114 if (EFI_ERROR (Status)) {\r
4115 continue;\r
4116 }\r
4117\r
4118 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
0c18794e 4119 if (FvbHandle != NULL) {\r
4120 *FvbHandle = HandleBuffer[Index];\r
4121 }\r
4122 if (FvbProtocol != NULL) {\r
4123 *FvbProtocol = Fvb;\r
4124 }\r
4125 Status = EFI_SUCCESS;\r
4126 break;\r
4127 }\r
4128 }\r
4129 FreePool (HandleBuffer);\r
4130\r
4131 if (Fvb == NULL) {\r
4132 Status = EFI_NOT_FOUND;\r
4133 }\r
2d3fb919 4134\r
4135 return Status;\r
0c18794e 4136}\r