]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
SecurityPkg Variable: Support the new introduced PcdMaxAuthVariableSize.
[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
13af4ab0 2156 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
0c18794e 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
13af4ab0 2193 ScratchSize = mVariableModuleGlobal->ScratchBufferSize;\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
13af4ab0 2312 // Set Max Common/Auth Variable Data Size as default MaxDataSize\r
732d199d 2313 //\r
13af4ab0
SZ
2314 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2315 MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r
2316 } else {\r
2317 MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r
2318 }\r
732d199d 2319\r
5767f22f 2320 if ((CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
20333c6d
QL
2321 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
2322 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0))) ||\r
2323 (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
2d3fb919 2324 //\r
5767f22f 2325 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of\r
2d3fb919 2326 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.\r
2327 //\r
732d199d 2328 Status = AppendSignatureList (\r
fddbbc66 2329 BufferForMerge,\r
20333c6d 2330 Variable->CurrPtr->DataSize,\r
732d199d 2331 MaxDataSize - Variable->CurrPtr->DataSize,\r
fddbbc66
SZ
2332 Data,\r
2333 DataSize,\r
2334 &MergedBufSize\r
732d199d 2335 );\r
2336 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2337 //\r
fddbbc66 2338 // Signature List is too long, Failed to Append.\r
732d199d 2339 //\r
2340 Status = EFI_INVALID_PARAMETER;\r
2341 goto Done;\r
2342 }\r
2343\r
fddbbc66 2344 if (MergedBufSize == Variable->CurrPtr->DataSize) {\r
2d3fb919 2345 if ((TimeStamp == NULL) || CompareTimeStamp (TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
2346 //\r
2347 // New EFI_SIGNATURE_DATA is not found and timestamp is not later\r
2348 // than current timestamp, return EFI_SUCCESS directly.\r
2349 //\r
2350 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
2351 Status = EFI_SUCCESS;\r
2352 goto Done;\r
2353 }\r
2354 }\r
2355 } else {\r
2356 //\r
fddbbc66 2357 // For other Variables, append the new data to the end of existing data.\r
13af4ab0 2358 // Max Harware error record variable data size is different from common/auth variable\r
2d3fb919 2359 //\r
732d199d 2360 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
fddbbc66 2361 MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r
732d199d 2362 }\r
2363\r
2364 if (Variable->CurrPtr->DataSize + DataSize > MaxDataSize) {\r
2365 //\r
fddbbc66 2366 // Existing data size + new data size exceed maximum variable size limitation.\r
732d199d 2367 //\r
2368 Status = EFI_INVALID_PARAMETER;\r
2369 goto Done;\r
2370 }\r
fddbbc66
SZ
2371 CopyMem ((UINT8*) ((UINTN) BufferForMerge + Variable->CurrPtr->DataSize), Data, DataSize);\r
2372 MergedBufSize = Variable->CurrPtr->DataSize + DataSize;\r
2d3fb919 2373 }\r
2374\r
0c18794e 2375 //\r
fddbbc66 2376 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r
0c18794e 2377 //\r
fddbbc66
SZ
2378 Data = BufferForMerge;\r
2379 DataSize = MergedBufSize;\r
2380 DataReady = TRUE;\r
0c18794e 2381 }\r
2382\r
2383 //\r
2384 // Mark the old variable as in delete transition.\r
2385 //\r
2386 State = Variable->CurrPtr->State;\r
2387 State &= VAR_IN_DELETED_TRANSITION;\r
2388\r
2389 Status = UpdateVariableStore (\r
2390 &mVariableModuleGlobal->VariableGlobal,\r
2391 Variable->Volatile,\r
2392 FALSE,\r
2393 Fvb,\r
2394 (UINTN) &Variable->CurrPtr->State,\r
2395 sizeof (UINT8),\r
2396 &State\r
2d3fb919 2397 );\r
0c18794e 2398 if (EFI_ERROR (Status)) {\r
2d3fb919 2399 goto Done;\r
2400 }\r
0c18794e 2401 if (!Variable->Volatile) {\r
2402 CacheVariable->CurrPtr->State = State;\r
2403 }\r
2d3fb919 2404 }\r
0c18794e 2405 } else {\r
2406 //\r
2407 // Not found existing variable. Create a new variable.\r
0c18794e 2408 //\r
2d3fb919 2409\r
2410 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
2411 Status = EFI_SUCCESS;\r
0c18794e 2412 goto Done;\r
2413 }\r
2d3fb919 2414\r
0c18794e 2415 //\r
2416 // Make sure we are trying to create a new variable.\r
2d3fb919 2417 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
0c18794e 2418 //\r
2419 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
2420 Status = EFI_NOT_FOUND;\r
2421 goto Done;\r
2422 }\r
2d3fb919 2423\r
0c18794e 2424 //\r
2425 // Only variable have NV|RT attribute can be created in Runtime.\r
2426 //\r
2427 if (AtRuntime () &&\r
2428 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
2429 Status = EFI_INVALID_PARAMETER;\r
2430 goto Done;\r
2d3fb919 2431 }\r
0c18794e 2432 }\r
2433\r
2434 //\r
2435 // Function part - create a new variable and copy the data.\r
2436 // Both update a variable and create a variable will come here.\r
fddbbc66 2437 //\r
0c18794e 2438 NextVariable->StartId = VARIABLE_DATA;\r
2439 //\r
2440 // NextVariable->State = VAR_ADDED;\r
2441 //\r
2442 NextVariable->Reserved = 0;\r
2443 NextVariable->PubKeyIndex = KeyIndex;\r
2444 NextVariable->MonotonicCount = MonotonicCount;\r
2d3fb919 2445 ZeroMem (&NextVariable->TimeStamp, sizeof (EFI_TIME));\r
0c18794e 2446\r
3b4151bc 2447 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2d3fb919 2448 (TimeStamp != NULL)) {\r
3b4151bc 2449 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
2450 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2451 } else {\r
0c18794e 2452 //\r
2453 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
2454 // when the new TimeStamp value is later than the current timestamp associated\r
2455 // with the variable, we need associate the new timestamp with the updated value.\r
2456 //\r
2d3fb919 2457 if (Variable->CurrPtr != NULL) {\r
2458 if (CompareTimeStamp (&Variable->CurrPtr->TimeStamp, TimeStamp)) {\r
2459 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2460 }\r
0c18794e 2461 }\r
3b4151bc 2462 }\r
0c18794e 2463 }\r
2464\r
2465 //\r
2d3fb919 2466 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
0c18794e 2467 // Attributes bitmask parameter of a GetVariable() call.\r
2468 //\r
2469 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2d3fb919 2470\r
0c18794e 2471 VarNameOffset = sizeof (VARIABLE_HEADER);\r
2472 VarNameSize = StrSize (VariableName);\r
2473 CopyMem (\r
2474 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
2475 VariableName,\r
2476 VarNameSize\r
2477 );\r
2478 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
fddbbc66
SZ
2479\r
2480 //\r
2481 // If DataReady is TRUE, it means the variable data has been saved into\r
2482 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r
2483 //\r
2484 if (!DataReady) {\r
2485 CopyMem (\r
2486 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
2487 Data,\r
2488 DataSize\r
2489 );\r
2490 }\r
2491\r
0c18794e 2492 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
2493 //\r
2494 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2495 // NextVariable->DataSize should not include pad size so that variable\r
2496 // service can get actual size in GetVariable.\r
2497 //\r
2498 NextVariable->NameSize = (UINT32)VarNameSize;\r
2499 NextVariable->DataSize = (UINT32)DataSize;\r
2500\r
2501 //\r
2502 // The actual size of the variable that stores in storage should\r
2503 // include pad size.\r
2504 //\r
2505 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
2506 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2507 //\r
2508 // Create a nonvolatile variable.\r
2509 //\r
2510 Volatile = FALSE;\r
952ba83c
SZ
2511\r
2512 IsCommonVariable = FALSE;\r
2513 IsCommonUserVariable = FALSE;\r
2514 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
2515 IsCommonVariable = TRUE;\r
2516 IsCommonUserVariable = IsUserVariable (NextVariable);\r
2517 }\r
2d3fb919 2518 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
0c18794e 2519 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
952ba83c
SZ
2520 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
2521 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
2522 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
0c18794e 2523 if (AtRuntime ()) {\r
952ba83c
SZ
2524 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2525 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2526 }\r
2527 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
2528 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2529 }\r
0c18794e 2530 Status = EFI_OUT_OF_RESOURCES;\r
2531 goto Done;\r
2532 }\r
2533 //\r
7baf3c69 2534 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
0c18794e 2535 //\r
83758cdc 2536 Status = Reclaim (\r
2537 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2538 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2539 FALSE,\r
23b06935 2540 Variable,\r
7baf3c69
SZ
2541 NextVariable,\r
2542 HEADER_ALIGN (VarSize),\r
83758cdc 2543 FALSE\r
2544 );\r
7baf3c69
SZ
2545 if (!EFI_ERROR (Status)) {\r
2546 //\r
2547 // The new variable has been integrated successfully during reclaiming.\r
2548 //\r
2549 if (Variable->CurrPtr != NULL) {\r
2550 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2551 CacheVariable->InDeletedTransitionPtr = NULL;\r
2552 }\r
2553 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
2554 FlushHobVariableToFlash (VariableName, VendorGuid);\r
952ba83c
SZ
2555 } else {\r
2556 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2557 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
17409b7a 2558 }\r
952ba83c
SZ
2559 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
2560 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2561 }\r
2562 }\r
7baf3c69 2563 goto Done;\r
0c18794e 2564 }\r
2565 //\r
2566 // Four steps\r
2567 // 1. Write variable header\r
2d3fb919 2568 // 2. Set variable state to header valid\r
0c18794e 2569 // 3. Write variable data\r
2570 // 4. Set variable state to valid\r
2571 //\r
2572 //\r
2573 // Step 1:\r
2574 //\r
2575 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
2576 Status = UpdateVariableStore (\r
2577 &mVariableModuleGlobal->VariableGlobal,\r
2578 FALSE,\r
2579 TRUE,\r
2580 Fvb,\r
2581 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2582 sizeof (VARIABLE_HEADER),\r
2583 (UINT8 *) NextVariable\r
2584 );\r
2585\r
2586 if (EFI_ERROR (Status)) {\r
2587 goto Done;\r
2588 }\r
2589\r
2590 //\r
2591 // Step 2:\r
2592 //\r
2593 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2594 Status = UpdateVariableStore (\r
2595 &mVariableModuleGlobal->VariableGlobal,\r
2596 FALSE,\r
2597 TRUE,\r
2598 Fvb,\r
2599 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2600 sizeof (UINT8),\r
2601 &NextVariable->State\r
2602 );\r
2603\r
2604 if (EFI_ERROR (Status)) {\r
2605 goto Done;\r
2606 }\r
2607 //\r
2608 // Step 3:\r
2609 //\r
2610 Status = UpdateVariableStore (\r
2611 &mVariableModuleGlobal->VariableGlobal,\r
2612 FALSE,\r
2613 TRUE,\r
2614 Fvb,\r
2615 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
2616 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
2617 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
2618 );\r
2619\r
2620 if (EFI_ERROR (Status)) {\r
2621 goto Done;\r
2622 }\r
2623 //\r
2624 // Step 4:\r
2625 //\r
2626 NextVariable->State = VAR_ADDED;\r
2627 Status = UpdateVariableStore (\r
2628 &mVariableModuleGlobal->VariableGlobal,\r
2629 FALSE,\r
2630 TRUE,\r
2631 Fvb,\r
2632 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2633 sizeof (UINT8),\r
2634 &NextVariable->State\r
2635 );\r
2636\r
2637 if (EFI_ERROR (Status)) {\r
2638 goto Done;\r
2639 }\r
2640\r
2641 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2642\r
2643 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2644 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2645 } else {\r
2646 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
952ba83c
SZ
2647 if (IsCommonUserVariable) {\r
2648 mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r
17409b7a 2649 }\r
952ba83c 2650 }\r
0c18794e 2651 //\r
2652 // update the memory copy of Flash region.\r
2653 //\r
2654 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
2655 } else {\r
2656 //\r
2657 // Create a volatile variable.\r
2d3fb919 2658 //\r
0c18794e 2659 Volatile = TRUE;\r
2660\r
2661 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
2662 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
2663 //\r
7baf3c69 2664 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
0c18794e 2665 //\r
83758cdc 2666 Status = Reclaim (\r
2667 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
2668 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
2669 TRUE,\r
23b06935 2670 Variable,\r
7baf3c69
SZ
2671 NextVariable,\r
2672 HEADER_ALIGN (VarSize),\r
83758cdc 2673 FALSE\r
2674 );\r
7baf3c69
SZ
2675 if (!EFI_ERROR (Status)) {\r
2676 //\r
2677 // The new variable has been integrated successfully during reclaiming.\r
2678 //\r
2679 if (Variable->CurrPtr != NULL) {\r
2680 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2681 CacheVariable->InDeletedTransitionPtr = NULL;\r
2682 }\r
2683 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
23b06935 2684 }\r
7baf3c69 2685 goto Done;\r
0c18794e 2686 }\r
2687\r
2688 NextVariable->State = VAR_ADDED;\r
2689 Status = UpdateVariableStore (\r
2690 &mVariableModuleGlobal->VariableGlobal,\r
2691 TRUE,\r
2692 TRUE,\r
2693 Fvb,\r
2694 mVariableModuleGlobal->VolatileLastVariableOffset,\r
2695 (UINT32) VarSize,\r
2696 (UINT8 *) NextVariable\r
2697 );\r
2698\r
2699 if (EFI_ERROR (Status)) {\r
2700 goto Done;\r
2701 }\r
2702\r
2703 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2704 }\r
2705\r
2706 //\r
2707 // Mark the old variable as deleted.\r
2708 //\r
23b06935
SZ
2709 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2710 if (Variable->InDeletedTransitionPtr != NULL) {\r
2711 //\r
2712 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2713 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2714 //\r
2715 State = Variable->InDeletedTransitionPtr->State;\r
2716 State &= VAR_DELETED;\r
2717 Status = UpdateVariableStore (\r
2718 &mVariableModuleGlobal->VariableGlobal,\r
2719 Variable->Volatile,\r
2720 FALSE,\r
2721 Fvb,\r
2722 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2723 sizeof (UINT8),\r
2724 &State\r
2725 );\r
2726 if (!EFI_ERROR (Status)) {\r
2727 if (!Variable->Volatile) {\r
0cc565de 2728 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
2729 CacheVariable->InDeletedTransitionPtr->State = State;\r
2730 }\r
2731 } else {\r
2732 goto Done;\r
2733 }\r
2734 }\r
2735\r
0c18794e 2736 State = Variable->CurrPtr->State;\r
2737 State &= VAR_DELETED;\r
2738\r
2739 Status = UpdateVariableStore (\r
2740 &mVariableModuleGlobal->VariableGlobal,\r
2741 Variable->Volatile,\r
2742 FALSE,\r
2743 Fvb,\r
2744 (UINTN) &Variable->CurrPtr->State,\r
2745 sizeof (UINT8),\r
2746 &State\r
2747 );\r
2d3fb919 2748 if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
0c18794e 2749 CacheVariable->CurrPtr->State = State;\r
2750 }\r
2751 }\r
2752\r
2753 if (!EFI_ERROR (Status)) {\r
2754 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
2755 if (!Volatile) {\r
2756 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2757 }\r
0c18794e 2758 }\r
2759\r
2760Done:\r
2761 return Status;\r
2762}\r
2763\r
a5f15e30
SZ
2764/**\r
2765 Check if a Unicode character is a hexadecimal character.\r
2766\r
20333c6d
QL
2767 This function checks if a Unicode character is a\r
2768 hexadecimal character. The valid hexadecimal character is\r
a5f15e30
SZ
2769 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
2770\r
2771\r
2772 @param Char The character to check against.\r
2773\r
2774 @retval TRUE If the Char is a hexadecmial character.\r
2775 @retval FALSE If the Char is not a hexadecmial character.\r
2776\r
2777**/\r
2778BOOLEAN\r
2779EFIAPI\r
2780IsHexaDecimalDigitCharacter (\r
2781 IN CHAR16 Char\r
2782 )\r
2783{\r
2784 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2785}\r
2786\r
2787/**\r
2788\r
2789 This code checks if variable is hardware error record variable or not.\r
2790\r
2791 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
2792 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
2793\r
2794 @param VariableName Pointer to variable name.\r
2795 @param VendorGuid Variable Vendor Guid.\r
2796\r
2797 @retval TRUE Variable is hardware error record variable.\r
2798 @retval FALSE Variable is not hardware error record variable.\r
2799\r
2800**/\r
2801BOOLEAN\r
2802EFIAPI\r
2803IsHwErrRecVariable (\r
2804 IN CHAR16 *VariableName,\r
2805 IN EFI_GUID *VendorGuid\r
2806 )\r
2807{\r
2808 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
2809 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
2810 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
2811 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
2812 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
2813 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
2814 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
2815 return FALSE;\r
2816 }\r
2817\r
2818 return TRUE;\r
2819}\r
2820\r
6ab9f441
RN
2821/**\r
2822 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
2823\r
2824 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
2825 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
2826 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
2827\r
2828 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
2829 as pending to be read-only.\r
2830 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
2831 Or VariableName is an empty string.\r
2832 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
2833 already been signaled.\r
2834 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
2835**/\r
2836EFI_STATUS\r
2837EFIAPI\r
2838VariableLockRequestToLock (\r
2839 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
2840 IN CHAR16 *VariableName,\r
2841 IN EFI_GUID *VendorGuid\r
2842 )\r
2843{\r
2844 VARIABLE_ENTRY *Entry;\r
17409b7a 2845 CHAR16 *Name;\r
27a0a8bc
SZ
2846 LIST_ENTRY *Link;\r
2847 VARIABLE_ENTRY *LockedEntry;\r
6ab9f441
RN
2848\r
2849 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2850 return EFI_INVALID_PARAMETER;\r
2851 }\r
2852\r
2853 if (mEndOfDxe) {\r
2854 return EFI_ACCESS_DENIED;\r
2855 }\r
2856\r
17409b7a 2857 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (VariableName));\r
6ab9f441
RN
2858 if (Entry == NULL) {\r
2859 return EFI_OUT_OF_RESOURCES;\r
2860 }\r
2861\r
2862 DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
2863\r
2864 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2865\r
27a0a8bc
SZ
2866 for ( Link = GetFirstNode (&mLockedVariableList)\r
2867 ; !IsNull (&mLockedVariableList, Link)\r
2868 ; Link = GetNextNode (&mLockedVariableList, Link)\r
2869 ) {\r
2870 LockedEntry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
2871 Name = (CHAR16 *) ((UINTN) LockedEntry + sizeof (*LockedEntry));\r
2872 if (CompareGuid (&LockedEntry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
2873 goto Done;\r
2874 }\r
2875 }\r
2876\r
17409b7a
SZ
2877 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
2878 StrnCpy (Name, VariableName, StrLen (VariableName));\r
6ab9f441
RN
2879 CopyGuid (&Entry->Guid, VendorGuid);\r
2880 InsertTailList (&mLockedVariableList, &Entry->Link);\r
2881\r
27a0a8bc 2882Done:\r
6ab9f441
RN
2883 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2884\r
2885 return EFI_SUCCESS;\r
2886}\r
2887\r
0c18794e 2888/**\r
2889\r
2890 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2891\r
dc204d5a
JY
2892 Caution: This function may receive untrusted input.\r
2893 This function may be invoked in SMM mode, and datasize is external input.\r
2894 This function will do basic validation, before parse the data.\r
2895\r
0c18794e 2896 @param VariableName Name of Variable to be found.\r
2897 @param VendorGuid Variable vendor GUID.\r
2898 @param Attributes Attribute value of the variable found.\r
2899 @param DataSize Size of Data found. If size is less than the\r
2900 data, this value contains the required size.\r
2901 @param Data Data pointer.\r
2d3fb919 2902\r
0c18794e 2903 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2904 @return EFI_SUCCESS Find the specified variable.\r
2905 @return EFI_NOT_FOUND Not found.\r
2906 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2907\r
2908**/\r
2909EFI_STATUS\r
2910EFIAPI\r
2911VariableServiceGetVariable (\r
2912 IN CHAR16 *VariableName,\r
2913 IN EFI_GUID *VendorGuid,\r
2914 OUT UINT32 *Attributes OPTIONAL,\r
2915 IN OUT UINTN *DataSize,\r
2916 OUT VOID *Data\r
2917 )\r
2918{\r
2919 EFI_STATUS Status;\r
2920 VARIABLE_POINTER_TRACK Variable;\r
2921 UINTN VarDataSize;\r
2922\r
2923 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2924 return EFI_INVALID_PARAMETER;\r
2925 }\r
2926\r
2927 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2d3fb919 2928\r
ecc722ad 2929 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2930 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2931 goto Done;\r
2932 }\r
2933\r
2934 //\r
2935 // Get data size\r
2936 //\r
2937 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2938 ASSERT (VarDataSize != 0);\r
2939\r
2940 if (*DataSize >= VarDataSize) {\r
2941 if (Data == NULL) {\r
2942 Status = EFI_INVALID_PARAMETER;\r
2943 goto Done;\r
2944 }\r
2945\r
2946 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2947 if (Attributes != NULL) {\r
2948 *Attributes = Variable.CurrPtr->Attributes;\r
2949 }\r
2950\r
2951 *DataSize = VarDataSize;\r
2952 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2d3fb919 2953\r
0c18794e 2954 Status = EFI_SUCCESS;\r
2955 goto Done;\r
2956 } else {\r
2957 *DataSize = VarDataSize;\r
2958 Status = EFI_BUFFER_TOO_SMALL;\r
2959 goto Done;\r
2960 }\r
2961\r
2962Done:\r
2963 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2964 return Status;\r
2965}\r
2966\r
2967\r
2968\r
2969/**\r
2970\r
2971 This code Finds the Next available variable.\r
2972\r
dc204d5a
JY
2973 Caution: This function may receive untrusted input.\r
2974 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2975\r
0c18794e 2976 @param VariableNameSize Size of the variable name.\r
2977 @param VariableName Pointer to variable name.\r
2978 @param VendorGuid Variable Vendor Guid.\r
2979\r
2980 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2981 @return EFI_SUCCESS Find the specified variable.\r
2982 @return EFI_NOT_FOUND Not found.\r
2983 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2984\r
2985**/\r
2986EFI_STATUS\r
2987EFIAPI\r
2988VariableServiceGetNextVariableName (\r
2989 IN OUT UINTN *VariableNameSize,\r
2990 IN OUT CHAR16 *VariableName,\r
2991 IN OUT EFI_GUID *VendorGuid\r
2992 )\r
2993{\r
9a000b46 2994 VARIABLE_STORE_TYPE Type;\r
0c18794e 2995 VARIABLE_POINTER_TRACK Variable;\r
9a000b46 2996 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2997 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
0c18794e 2998 UINTN VarNameSize;\r
2999 EFI_STATUS Status;\r
9a000b46 3000 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
0c18794e 3001\r
3002 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
3003 return EFI_INVALID_PARAMETER;\r
3004 }\r
3005\r
3006 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3007\r
ecc722ad 3008 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 3009 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
3010 goto Done;\r
3011 }\r
3012\r
3013 if (VariableName[0] != 0) {\r
3014 //\r
3015 // If variable name is not NULL, get next variable.\r
3016 //\r
3017 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3018 }\r
3019\r
9a000b46
RN
3020 //\r
3021 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
3022 // The index and attributes mapping must be kept in this order as FindVariable\r
3023 // makes use of this mapping to implement search algorithm.\r
3024 //\r
3025 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
3026 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3027 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
3028\r
0c18794e 3029 while (TRUE) {\r
3030 //\r
9a000b46 3031 // Switch from Volatile to HOB, to Non-Volatile.\r
0c18794e 3032 //\r
6ebffb67 3033 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
9a000b46
RN
3034 //\r
3035 // Find current storage index\r
3036 //\r
3037 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
3038 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
3039 break;\r
3040 }\r
3041 }\r
3042 ASSERT (Type < VariableStoreTypeMax);\r
3043 //\r
3044 // Switch to next storage\r
3045 //\r
3046 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
3047 if (VariableStoreHeader[Type] != NULL) {\r
3048 break;\r
3049 }\r
3050 }\r
3051 //\r
2d3fb919 3052 // Capture the case that\r
9a000b46
RN
3053 // 1. current storage is the last one, or\r
3054 // 2. no further storage\r
3055 //\r
3056 if (Type == VariableStoreTypeMax) {\r
0c18794e 3057 Status = EFI_NOT_FOUND;\r
3058 goto Done;\r
3059 }\r
9a000b46
RN
3060 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
3061 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
3062 Variable.CurrPtr = Variable.StartPtr;\r
0c18794e 3063 }\r
9a000b46 3064\r
0c18794e 3065 //\r
3066 // Variable is found\r
3067 //\r
23b06935
SZ
3068 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3069 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
3070 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3071 //\r
3072 // If it is a IN_DELETED_TRANSITION variable,\r
3073 // and there is also a same ADDED one at the same time,\r
3074 // don't return it.\r
3075 //\r
3076 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
3077 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
3078 Status = FindVariableEx (\r
3079 GetVariableNamePtr (Variable.CurrPtr),\r
3080 &Variable.CurrPtr->VendorGuid,\r
3081 FALSE,\r
3082 &VariablePtrTrack\r
3083 );\r
3084 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
3085 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3086 continue;\r
3087 }\r
3088 }\r
9a000b46
RN
3089\r
3090 //\r
3091 // Don't return NV variable when HOB overrides it\r
3092 //\r
2d3fb919 3093 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
9a000b46
RN
3094 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
3095 ) {\r
3096 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
3097 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
3098 Status = FindVariableEx (\r
3099 GetVariableNamePtr (Variable.CurrPtr),\r
3100 &Variable.CurrPtr->VendorGuid,\r
ecc722ad 3101 FALSE,\r
9a000b46
RN
3102 &VariableInHob\r
3103 );\r
3104 if (!EFI_ERROR (Status)) {\r
3105 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3106 continue;\r
3107 }\r
3108 }\r
3109\r
0c18794e 3110 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
3111 ASSERT (VarNameSize != 0);\r
3112\r
3113 if (VarNameSize <= *VariableNameSize) {\r
9a000b46
RN
3114 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
3115 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
0c18794e 3116 Status = EFI_SUCCESS;\r
3117 } else {\r
3118 Status = EFI_BUFFER_TOO_SMALL;\r
3119 }\r
3120\r
3121 *VariableNameSize = VarNameSize;\r
3122 goto Done;\r
3123 }\r
3124 }\r
3125\r
3126 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3127 }\r
3128\r
3129Done:\r
3130 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3131 return Status;\r
3132}\r
3133\r
3134/**\r
3135\r
3136 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
3137\r
dc204d5a
JY
3138 Caution: This function may receive untrusted input.\r
3139 This function may be invoked in SMM mode, and datasize and data are external input.\r
3140 This function will do basic validation, before parse the data.\r
3141 This function will parse the authentication carefully to avoid security issues, like\r
3142 buffer overflow, integer overflow.\r
3143 This function will check attribute carefully to avoid authentication bypass.\r
3144\r
0c18794e 3145 @param VariableName Name of Variable to be found.\r
3146 @param VendorGuid Variable vendor GUID.\r
3147 @param Attributes Attribute value of the variable found\r
3148 @param DataSize Size of Data found. If size is less than the\r
3149 data, this value contains the required size.\r
3150 @param Data Data pointer.\r
3151\r
3152 @return EFI_INVALID_PARAMETER Invalid parameter.\r
3153 @return EFI_SUCCESS Set successfully.\r
3154 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
3155 @return EFI_NOT_FOUND Not found.\r
3156 @return EFI_WRITE_PROTECTED Variable is read-only.\r
3157\r
3158**/\r
3159EFI_STATUS\r
3160EFIAPI\r
3161VariableServiceSetVariable (\r
3162 IN CHAR16 *VariableName,\r
3163 IN EFI_GUID *VendorGuid,\r
3164 IN UINT32 Attributes,\r
3165 IN UINTN DataSize,\r
3166 IN VOID *Data\r
3167 )\r
3168{\r
3169 VARIABLE_POINTER_TRACK Variable;\r
3170 EFI_STATUS Status;\r
3171 VARIABLE_HEADER *NextVariable;\r
3172 EFI_PHYSICAL_ADDRESS Point;\r
3173 UINTN PayloadSize;\r
6ab9f441
RN
3174 LIST_ENTRY *Link;\r
3175 VARIABLE_ENTRY *Entry;\r
17409b7a 3176 CHAR16 *Name;\r
0c18794e 3177\r
3178 //\r
3179 // Check input parameters.\r
3180 //\r
3181 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
3182 return EFI_INVALID_PARAMETER;\r
2d3fb919 3183 }\r
0c18794e 3184\r
3185 if (DataSize != 0 && Data == NULL) {\r
3186 return EFI_INVALID_PARAMETER;\r
3187 }\r
3188\r
275beb2b 3189 //\r
3190 // Check for reserverd bit in variable attribute.\r
3191 //\r
3192 if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
3193 return EFI_INVALID_PARAMETER;\r
3194 }\r
3195\r
0c18794e 3196 //\r
3197 // Make sure if runtime bit is set, boot service bit is set also.\r
3198 //\r
3199 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3200 return EFI_INVALID_PARAMETER;\r
3201 }\r
3202\r
3203 //\r
2d3fb919 3204 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
0c18794e 3205 // cannot be set both.\r
3206 //\r
2d3fb919 3207 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
0c18794e 3208 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
3209 return EFI_INVALID_PARAMETER;\r
2d3fb919 3210 }\r
0c18794e 3211\r
3212 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
3213 if (DataSize < AUTHINFO_SIZE) {\r
3214 //\r
2d3fb919 3215 // Try to write Authenticated Variable without AuthInfo.\r
0c18794e 3216 //\r
3217 return EFI_SECURITY_VIOLATION;\r
2d3fb919 3218 }\r
3219 PayloadSize = DataSize - AUTHINFO_SIZE;\r
3220 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
3221 //\r
3222 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
3223 //\r
3224 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
6bc4e19f 3225 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
3226 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
2d3fb919 3227 return EFI_SECURITY_VIOLATION;\r
3228 }\r
3229 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
0c18794e 3230 } else {\r
2d3fb919 3231 PayloadSize = DataSize;\r
0c18794e 3232 }\r
2d3fb919 3233\r
56251c66 3234 if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
3235 //\r
20333c6d
QL
3236 // Prevent whole variable size overflow\r
3237 //\r
56251c66 3238 return EFI_INVALID_PARAMETER;\r
3239 }\r
3240\r
0c18794e 3241 //\r
3242 // The size of the VariableName, including the Unicode Null in bytes plus\r
3243 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
13af4ab0 3244 // bytes for HwErrRec.\r
0c18794e 3245 //\r
3246 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
56251c66 3247 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
0c18794e 3248 return EFI_INVALID_PARAMETER;\r
3249 }\r
a5f15e30 3250 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
0c18794e 3251 return EFI_INVALID_PARAMETER;\r
3252 }\r
3253 } else {\r
3254 //\r
3255 // The size of the VariableName, including the Unicode Null in bytes plus\r
13af4ab0 3256 // the DataSize is limited to maximum size of Max(Auth)VariableSize bytes.\r
0c18794e 3257 //\r
13af4ab0
SZ
3258 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3259 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - sizeof (VARIABLE_HEADER)) {\r
3260 return EFI_INVALID_PARAMETER;\r
3261 }\r
3262 } else {\r
3263 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - sizeof (VARIABLE_HEADER)) {\r
3264 return EFI_INVALID_PARAMETER;\r
3265 }\r
2d3fb919 3266 }\r
3267 }\r
0c18794e 3268\r
3269 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3270\r
3271 //\r
3272 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
3273 //\r
3274 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
3275 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
3276 //\r
3277 // Parse non-volatile variable data and get last variable offset.\r
3278 //\r
3279 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
6ebffb67 3280 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
0c18794e 3281 NextVariable = GetNextVariablePtr (NextVariable);\r
3282 }\r
3283 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
3284 }\r
3285\r
6ab9f441
RN
3286 if (mEndOfDxe && mEnableLocking) {\r
3287 //\r
3288 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
3289 //\r
3290 for ( Link = GetFirstNode (&mLockedVariableList)\r
3291 ; !IsNull (&mLockedVariableList, Link)\r
3292 ; Link = GetNextNode (&mLockedVariableList, Link)\r
3293 ) {\r
3294 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
17409b7a
SZ
3295 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
3296 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
6ab9f441
RN
3297 Status = EFI_WRITE_PROTECTED;\r
3298 DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
3299 goto Done;\r
3300 }\r
3301 }\r
3302 }\r
3303\r
17409b7a
SZ
3304 Status = InternalVarCheckSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r
3305 if (EFI_ERROR (Status)) {\r
3306 goto Done;\r
3307 }\r
3308\r
0c18794e 3309 //\r
3310 // Check whether the input variable is already existed.\r
3311 //\r
ecc722ad 3312 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
3313 if (!EFI_ERROR (Status)) {\r
3314 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
6ab9f441
RN
3315 Status = EFI_WRITE_PROTECTED;\r
3316 goto Done;\r
ecc722ad 3317 }\r
6e67fec0
SZ
3318 if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r
3319 //\r
3320 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
3321 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
3322 // 1. No access attributes specified\r
3323 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
3324 //\r
3325 Status = EFI_INVALID_PARAMETER;\r
952ba83c 3326 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
3327 goto Done;\r
3328 }\r
ecc722ad 3329 }\r
b2bd493e
SZ
3330\r
3331 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
9bc5dabb 3332 //\r
b2bd493e 3333 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
9bc5dabb 3334 //\r
b2bd493e
SZ
3335 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
3336 if (EFI_ERROR (Status)) {\r
3337 //\r
3338 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
3339 //\r
3340 goto Done;\r
3341 }\r
9bc5dabb
SZ
3342 }\r
3343\r
0c18794e 3344 //\r
3345 // Process PK, KEK, Sigdb seperately.\r
3346 //\r
3347 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
3348 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
3349 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
3350 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
20333c6d 3351 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
d547f31c
LE
3352 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) ||\r
3353 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
3354 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)\r
3355 )\r
3356 ) {\r
05a643f9 3357 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
3358 if (EFI_ERROR (Status)) {\r
3359 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
3360 }\r
0c18794e 3361 } else {\r
3362 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
3363 }\r
3364\r
6ab9f441 3365Done:\r
0c18794e 3366 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
3367 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3368\r
c1d93242
JY
3369 if (!AtRuntime ()) {\r
3370 if (!EFI_ERROR (Status)) {\r
3371 SecureBootHook (\r
3372 VariableName,\r
3373 VendorGuid\r
3374 );\r
3375 }\r
3376 }\r
3377\r
0c18794e 3378 return Status;\r
3379}\r
3380\r
3381/**\r
3382\r
3383 This code returns information about the EFI variables.\r
3384\r
dc204d5a
JY
3385 Caution: This function may receive untrusted input.\r
3386 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3387\r
0c18794e 3388 @param Attributes Attributes bitmask to specify the type of variables\r
3389 on which to return information.\r
3390 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3391 for the EFI variables associated with the attributes specified.\r
3392 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3393 for EFI variables associated with the attributes specified.\r
3394 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3395 associated with the attributes specified.\r
3396\r
0c18794e 3397 @return EFI_SUCCESS Query successfully.\r
0c18794e 3398\r
3399**/\r
3400EFI_STATUS\r
3401EFIAPI\r
b2bd493e 3402VariableServiceQueryVariableInfoInternal (\r
0c18794e 3403 IN UINT32 Attributes,\r
3404 OUT UINT64 *MaximumVariableStorageSize,\r
3405 OUT UINT64 *RemainingVariableStorageSize,\r
3406 OUT UINT64 *MaximumVariableSize\r
3407 )\r
3408{\r
3409 VARIABLE_HEADER *Variable;\r
3410 VARIABLE_HEADER *NextVariable;\r
3411 UINT64 VariableSize;\r
3412 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3413 UINT64 CommonVariableTotalSize;\r
3414 UINT64 HwErrVariableTotalSize;\r
b2bd493e
SZ
3415 EFI_STATUS Status;\r
3416 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
0c18794e 3417\r
3418 CommonVariableTotalSize = 0;\r
3419 HwErrVariableTotalSize = 0;\r
3420\r
0c18794e 3421 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
3422 //\r
3423 // Query is Volatile related.\r
3424 //\r
3425 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
3426 } else {\r
3427 //\r
3428 // Query is Non-Volatile related.\r
3429 //\r
3430 VariableStoreHeader = mNvVariableCache;\r
3431 }\r
3432\r
3433 //\r
3434 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
3435 // with the storage size (excluding the storage header size).\r
3436 //\r
3437 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
3438\r
3439 //\r
3440 // Harware error record variable needs larger size.\r
3441 //\r
3442 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3443 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3444 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
3445 } else {\r
3446 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
952ba83c
SZ
3447 if (AtRuntime ()) {\r
3448 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
3449 } else {\r
3450 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
3451 }\r
0c18794e 3452 }\r
3453\r
3454 //\r
13af4ab0 3455 // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size.\r
0c18794e 3456 //\r
13af4ab0
SZ
3457 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3458 *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - sizeof (VARIABLE_HEADER);\r
3459 } else {\r
3460 *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - sizeof (VARIABLE_HEADER);\r
3461 }\r
0c18794e 3462 }\r
3463\r
3464 //\r
3465 // Point to the starting address of the variables.\r
3466 //\r
3467 Variable = GetStartPointer (VariableStoreHeader);\r
3468\r
3469 //\r
3470 // Now walk through the related variable store.\r
3471 //\r
6ebffb67 3472 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
0c18794e 3473 NextVariable = GetNextVariablePtr (Variable);\r
3474 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
3475\r
3476 if (AtRuntime ()) {\r
3477 //\r
3478 // We don't take the state of the variables in mind\r
3479 // when calculating RemainingVariableStorageSize,\r
3480 // since the space occupied by variables not marked with\r
3481 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
3482 //\r
3483 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3484 HwErrVariableTotalSize += VariableSize;\r
3485 } else {\r
3486 CommonVariableTotalSize += VariableSize;\r
3487 }\r
3488 } else {\r
3489 //\r
3490 // Only care about Variables with State VAR_ADDED, because\r
3491 // the space not marked as VAR_ADDED is reclaimable now.\r
3492 //\r
3493 if (Variable->State == VAR_ADDED) {\r
3494 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3495 HwErrVariableTotalSize += VariableSize;\r
3496 } else {\r
3497 CommonVariableTotalSize += VariableSize;\r
3498 }\r
b2bd493e
SZ
3499 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3500 //\r
3501 // If it is a IN_DELETED_TRANSITION variable,\r
3502 // and there is not also a same ADDED one at the same time,\r
3503 // this IN_DELETED_TRANSITION variable is valid.\r
3504 //\r
3505 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
3506 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
3507 Status = FindVariableEx (\r
3508 GetVariableNamePtr (Variable),\r
3509 &Variable->VendorGuid,\r
3510 FALSE,\r
3511 &VariablePtrTrack\r
3512 );\r
3513 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
3514 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3515 HwErrVariableTotalSize += VariableSize;\r
3516 } else {\r
3517 CommonVariableTotalSize += VariableSize;\r
3518 }\r
3519 }\r
0c18794e 3520 }\r
3521 }\r
3522\r
3523 //\r
3524 // Go to the next one.\r
3525 //\r
3526 Variable = NextVariable;\r
3527 }\r
3528\r
3529 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
3530 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
952ba83c
SZ
3531 } else {\r
3532 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
3533 *RemainingVariableStorageSize = 0;\r
3534 } else {\r
3535 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
3536 }\r
0c18794e 3537 }\r
3538\r
3539 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
3540 *MaximumVariableSize = 0;\r
3541 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
3542 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
3543 }\r
3544\r
0c18794e 3545 return EFI_SUCCESS;\r
3546}\r
3547\r
b2bd493e
SZ
3548/**\r
3549\r
3550 This code returns information about the EFI variables.\r
3551\r
3552 Caution: This function may receive untrusted input.\r
3553 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3554\r
3555 @param Attributes Attributes bitmask to specify the type of variables\r
3556 on which to return information.\r
3557 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3558 for the EFI variables associated with the attributes specified.\r
3559 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3560 for EFI variables associated with the attributes specified.\r
3561 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3562 associated with the attributes specified.\r
3563\r
3564 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
3565 @return EFI_SUCCESS Query successfully.\r
3566 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
3567\r
3568**/\r
3569EFI_STATUS\r
3570EFIAPI\r
3571VariableServiceQueryVariableInfo (\r
3572 IN UINT32 Attributes,\r
3573 OUT UINT64 *MaximumVariableStorageSize,\r
3574 OUT UINT64 *RemainingVariableStorageSize,\r
3575 OUT UINT64 *MaximumVariableSize\r
3576 )\r
3577{\r
3578 EFI_STATUS Status;\r
3579\r
3580 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
3581 return EFI_INVALID_PARAMETER;\r
3582 }\r
3583\r
952ba83c 3584 if ((Attributes & VARIABLE_ATTRIBUTE_NV_BS_RT_AT_HR_AW) == 0) {\r
b2bd493e
SZ
3585 //\r
3586 // Make sure the Attributes combination is supported by the platform.\r
3587 //\r
3588 return EFI_UNSUPPORTED;\r
3589 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3590 //\r
3591 // Make sure if runtime bit is set, boot service bit is set also.\r
3592 //\r
3593 return EFI_INVALID_PARAMETER;\r
3594 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
3595 //\r
3596 // Make sure RT Attribute is set if we are in Runtime phase.\r
3597 //\r
3598 return EFI_INVALID_PARAMETER;\r
3599 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3600 //\r
3601 // Make sure Hw Attribute is set with NV.\r
3602 //\r
3603 return EFI_INVALID_PARAMETER;\r
3604 }\r
3605\r
3606 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3607\r
3608 Status = VariableServiceQueryVariableInfoInternal (\r
3609 Attributes,\r
3610 MaximumVariableStorageSize,\r
3611 RemainingVariableStorageSize,\r
3612 MaximumVariableSize\r
3613 );\r
3614\r
3615 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3616 return Status;\r
3617}\r
0c18794e 3618\r
3619/**\r
3620 This function reclaims variable storage if free size is below the threshold.\r
2d3fb919 3621\r
876ac395 3622 Caution: This function may be invoked at SMM mode.\r
3623 Care must be taken to make sure not security issue.\r
dc204d5a 3624\r
0c18794e 3625**/\r
3626VOID\r
3627ReclaimForOS(\r
3628 VOID\r
3629 )\r
3630{\r
3631 EFI_STATUS Status;\r
952ba83c 3632 UINTN RemainingCommonRuntimeVariableSpace;\r
0c18794e 3633 UINTN RemainingHwErrVariableSpace;\r
93626a53
SZ
3634 STATIC BOOLEAN Reclaimed;\r
3635\r
3636 //\r
3637 // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
3638 //\r
3639 if (Reclaimed) {\r
3640 return;\r
3641 }\r
3642 Reclaimed = TRUE;\r
0c18794e 3643\r
2d3fb919 3644 Status = EFI_SUCCESS;\r
0c18794e 3645\r
952ba83c
SZ
3646 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
3647 RemainingCommonRuntimeVariableSpace = 0;\r
3648 } else {\r
3649 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
3650 }\r
0c18794e 3651\r
3652 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
13af4ab0 3653\r
0c18794e 3654 //\r
952ba83c 3655 // Check if the free area is below a threshold.\r
0c18794e 3656 //\r
13af4ab0
SZ
3657 if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r
3658 (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r
3659 ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
0c18794e 3660 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
3661 Status = Reclaim (\r
3662 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3663 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3664 FALSE,\r
335e2681 3665 NULL,\r
7baf3c69
SZ
3666 NULL,\r
3667 0,\r
335e2681 3668 FALSE\r
0c18794e 3669 );\r
3670 ASSERT_EFI_ERROR (Status);\r
3671 }\r
3672}\r
3673\r
039a40aa
SZ
3674/**\r
3675 Init non-volatile variable store.\r
3676\r
3677 @retval EFI_SUCCESS Function successfully executed.\r
3678 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3679 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3680\r
3681**/\r
3682EFI_STATUS\r
3683InitNonVolatileVariableStore (\r
3684 VOID\r
3685 )\r
3686{\r
3687 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
952ba83c 3688 VARIABLE_HEADER *Variable;\r
039a40aa
SZ
3689 VARIABLE_HEADER *NextVariable;\r
3690 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3691 UINT64 VariableStoreLength;\r
3692 UINTN VariableSize;\r
3693 EFI_HOB_GUID_TYPE *GuidHob;\r
3694 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3695 UINT8 *NvStorageData;\r
3696 UINT32 NvStorageSize;\r
3697 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
3698 UINT32 BackUpOffset;\r
3699 UINT32 BackUpSize;\r
952ba83c
SZ
3700 UINT32 HwErrStorageSize;\r
3701 UINT32 MaxUserNvVariableSpaceSize;\r
3702 UINT32 BoottimeReservedNvVariableSpaceSize;\r
039a40aa
SZ
3703\r
3704 mVariableModuleGlobal->FvbInstance = NULL;\r
3705\r
039a40aa
SZ
3706 //\r
3707 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3708 // Keep the memory and the FLASH in sync as updates occur.\r
3709 //\r
3710 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
3711 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
3712 if (NvStorageData == NULL) {\r
3713 return EFI_OUT_OF_RESOURCES;\r
3714 }\r
3715\r
3716 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3717 if (NvStorageBase == 0) {\r
3718 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3719 }\r
3720 //\r
3721 // Copy NV storage data to the memory buffer.\r
3722 //\r
3723 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3724\r
3725 //\r
3726 // Check the FTW last write data hob.\r
3727 //\r
3728 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3729 if (GuidHob != NULL) {\r
3730 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3731 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3732 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3733 //\r
3734 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3735 //\r
3736 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3737 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3738 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3739 //\r
3740 // Flash NV storage from the Offset is backed up in spare block.\r
3741 //\r
3742 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3743 BackUpSize = NvStorageSize - BackUpOffset;\r
3744 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
3745 //\r
3746 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3747 //\r
3748 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3749 }\r
3750 }\r
3751\r
3752 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3753\r
3754 //\r
3755 // Check if the Firmware Volume is not corrupted\r
3756 //\r
3757 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3758 FreePool (NvStorageData);\r
3759 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3760 return EFI_VOLUME_CORRUPTED;\r
3761 }\r
3762\r
3763 VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
3764 VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
3765\r
3766 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3767 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3768 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
3769 FreePool (NvStorageData);\r
3770 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3771 return EFI_VOLUME_CORRUPTED;\r
3772 }\r
3773 ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
3774\r
952ba83c
SZ
3775\r
3776 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3777\r
3778 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3779 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
3780 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
3781\r
3782 //\r
3783 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3784 // is stored with common variable in the same NV region. So the platform integrator should\r
3785 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
13af4ab0 3786 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
952ba83c
SZ
3787 //\r
3788 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3789 //\r
3790 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
13af4ab0 3791 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
952ba83c
SZ
3792 //\r
3793 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3794 //\r
3795 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
13af4ab0 3796 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
952ba83c
SZ
3797 //\r
3798 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3799\r
3800 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3801 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
3802 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
3803\r
3804 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
3805\r
039a40aa 3806 //\r
13af4ab0 3807 // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
039a40aa 3808 //\r
13af4ab0
SZ
3809 ASSERT (MAX_NV_VARIABLE_SIZE < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3810\r
3811 mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r
3812 mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r
039a40aa
SZ
3813\r
3814 //\r
3815 // Parse non-volatile variable data and get last variable offset.\r
3816 //\r
952ba83c
SZ
3817 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3818 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
3819 NextVariable = GetNextVariablePtr (Variable);\r
3820 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
3821 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3822 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
039a40aa 3823 } else {\r
952ba83c 3824 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
039a40aa
SZ
3825 }\r
3826\r
952ba83c 3827 Variable = NextVariable;\r
039a40aa 3828 }\r
952ba83c 3829 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
039a40aa
SZ
3830\r
3831 return EFI_SUCCESS;\r
3832}\r
3833\r
335e2681
SZ
3834/**\r
3835 Flush the HOB variable to flash.\r
3836\r
3837 @param[in] VariableName Name of variable has been updated or deleted.\r
3838 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3839\r
3840**/\r
3841VOID\r
3842FlushHobVariableToFlash (\r
3843 IN CHAR16 *VariableName,\r
3844 IN EFI_GUID *VendorGuid\r
3845 )\r
3846{\r
3847 EFI_STATUS Status;\r
3848 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3849 VARIABLE_HEADER *Variable;\r
3850 VOID *VariableData;\r
3851 BOOLEAN ErrorFlag;\r
3852\r
3853 ErrorFlag = FALSE;\r
3854\r
3855 //\r
3856 // Flush the HOB variable to flash.\r
3857 //\r
3858 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3859 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3860 //\r
3861 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3862 //\r
3863 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3864 for ( Variable = GetStartPointer (VariableStoreHeader)\r
6ebffb67 3865 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
335e2681
SZ
3866 ; Variable = GetNextVariablePtr (Variable)\r
3867 ) {\r
3868 if (Variable->State != VAR_ADDED) {\r
3869 //\r
3870 // The HOB variable has been set to DELETED state in local.\r
3871 //\r
3872 continue;\r
3873 }\r
3874 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3875 if (VendorGuid == NULL || VariableName == NULL ||\r
3876 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
3877 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3878 VariableData = GetVariableDataPtr (Variable);\r
3879 Status = VariableServiceSetVariable (\r
3880 GetVariableNamePtr (Variable),\r
3881 &Variable->VendorGuid,\r
3882 Variable->Attributes,\r
3883 Variable->DataSize,\r
3884 VariableData\r
3885 );\r
3886 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
3887 } else {\r
3888 //\r
3889 // The updated or deleted variable is matched with the HOB variable.\r
3890 // Don't break here because we will try to set other HOB variables\r
3891 // since this variable could be set successfully.\r
3892 //\r
3893 Status = EFI_SUCCESS;\r
3894 }\r
3895 if (!EFI_ERROR (Status)) {\r
3896 //\r
3897 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3898 // set the HOB variable to DELETED state in local.\r
3899 //\r
3900 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
3901 Variable->State &= VAR_DELETED;\r
3902 } else {\r
3903 ErrorFlag = TRUE;\r
3904 }\r
3905 }\r
3906 if (ErrorFlag) {\r
3907 //\r
3908 // We still have HOB variable(s) not flushed in flash.\r
3909 //\r
3910 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3911 } else {\r
3912 //\r
3913 // All HOB variables have been flushed in flash.\r
3914 //\r
3915 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3916 if (!AtRuntime ()) {\r
3917 FreePool ((VOID *) VariableStoreHeader);\r
3918 }\r
3919 }\r
3920 }\r
3921\r
3922}\r
0c18794e 3923\r
3924/**\r
039a40aa 3925 Initializes variable write service after FTW was ready.\r
0c18794e 3926\r
3927 @retval EFI_SUCCESS Function successfully executed.\r
3928 @retval Others Fail to initialize the variable service.\r
3929\r
3930**/\r
3931EFI_STATUS\r
3932VariableWriteServiceInitialize (\r
3933 VOID\r
3934 )\r
3935{\r
3936 EFI_STATUS Status;\r
3937 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3938 UINTN Index;\r
3939 UINT8 Data;\r
3940 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
039a40aa
SZ
3941 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3942\r
3943 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3944 if (NvStorageBase == 0) {\r
3945 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3946 }\r
3947 VariableStoreBase = NvStorageBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageBase))->HeaderLength);\r
0c18794e 3948\r
039a40aa
SZ
3949 //\r
3950 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
3951 //\r
3952 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
0c18794e 3953 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2d3fb919 3954\r
0c18794e 3955 //\r
3956 // Check if the free area is really free.\r
3957 //\r
9a000b46 3958 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
0c18794e 3959 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3960 if (Data != 0xff) {\r
3961 //\r
3962 // There must be something wrong in variable store, do reclaim operation.\r
3963 //\r
3964 Status = Reclaim (\r
3965 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3966 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3967 FALSE,\r
335e2681 3968 NULL,\r
7baf3c69
SZ
3969 NULL,\r
3970 0,\r
3971 FALSE\r
0c18794e 3972 );\r
3973 if (EFI_ERROR (Status)) {\r
3974 return Status;\r
3975 }\r
3976 break;\r
3977 }\r
3978 }\r
3979\r
335e2681 3980 FlushHobVariableToFlash (NULL, NULL);\r
9a000b46 3981\r
0c18794e 3982 //\r
3983 // Authenticated variable initialize.\r
3984 //\r
13af4ab0 3985 Status = AutenticatedVariableServiceInitialize (mVariableModuleGlobal->MaxAuthVariableSize - sizeof (VARIABLE_HEADER));\r
0c18794e 3986\r
3987 return Status;\r
3988}\r
3989\r
3990\r
3991/**\r
3992 Initializes variable store area for non-volatile and volatile variable.\r
3993\r
3994 @retval EFI_SUCCESS Function successfully executed.\r
3995 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3996\r
3997**/\r
3998EFI_STATUS\r
3999VariableCommonInitialize (\r
4000 VOID\r
4001 )\r
4002{\r
4003 EFI_STATUS Status;\r
4004 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
4005 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
0c18794e 4006 UINT64 VariableStoreLength;\r
4007 UINTN ScratchSize;\r
9a000b46 4008 EFI_HOB_GUID_TYPE *GuidHob;\r
0c18794e 4009\r
4010 //\r
4011 // Allocate runtime memory for variable driver global structure.\r
4012 //\r
4013 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
4014 if (mVariableModuleGlobal == NULL) {\r
4015 return EFI_OUT_OF_RESOURCES;\r
4016 }\r
4017\r
4018 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
4019\r
9a000b46
RN
4020 //\r
4021 // Get HOB variable store.\r
4022 //\r
4023 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
4024 if (GuidHob != NULL) {\r
4025 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 4026 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
9a000b46 4027 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
4028 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
4029 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
039a40aa 4030 FreePool (mVariableModuleGlobal);\r
335e2681
SZ
4031 return EFI_OUT_OF_RESOURCES;\r
4032 }\r
9a000b46
RN
4033 } else {\r
4034 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
4035 }\r
4036 }\r
4037\r
0c18794e 4038 //\r
4039 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
4040 //\r
13af4ab0
SZ
4041 ScratchSize = MAX_NV_VARIABLE_SIZE;\r
4042 mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r
0c18794e 4043 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
4044 if (VolatileVariableStore == NULL) {\r
039a40aa
SZ
4045 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
4046 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
4047 }\r
0c18794e 4048 FreePool (mVariableModuleGlobal);\r
4049 return EFI_OUT_OF_RESOURCES;\r
4050 }\r
4051\r
4052 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
4053\r
4054 //\r
4055 // Initialize Variable Specific Data.\r
4056 //\r
4057 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
4058 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
0c18794e 4059\r
4060 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
4061 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
4062 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
4063 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
4064 VolatileVariableStore->Reserved = 0;\r
4065 VolatileVariableStore->Reserved1 = 0;\r
4066\r
4067 //\r
039a40aa 4068 // Init non-volatile variable store.\r
0c18794e 4069 //\r
039a40aa 4070 Status = InitNonVolatileVariableStore ();\r
0c18794e 4071 if (EFI_ERROR (Status)) {\r
039a40aa
SZ
4072 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
4073 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
4074 }\r
0c18794e 4075 FreePool (mVariableModuleGlobal);\r
4076 FreePool (VolatileVariableStore);\r
4077 }\r
4078\r
4079 return Status;\r
4080}\r
4081\r
4082\r
4083/**\r
4084 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
4085\r
4086 @param[in] Address The Flash address.\r
4087 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
4088 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
4089\r
4090**/\r
4091EFI_STATUS\r
4092GetFvbInfoByAddress (\r
4093 IN EFI_PHYSICAL_ADDRESS Address,\r
4094 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
4095 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
4096 )\r
4097{\r
4098 EFI_STATUS Status;\r
4099 EFI_HANDLE *HandleBuffer;\r
4100 UINTN HandleCount;\r
4101 UINTN Index;\r
4102 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
4103 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
0c18794e 4104 EFI_FVB_ATTRIBUTES_2 Attributes;\r
931aae94
DG
4105 UINTN BlockSize;\r
4106 UINTN NumberOfBlocks;\r
2d3fb919 4107\r
d4193108 4108 HandleBuffer = NULL;\r
0c18794e 4109 //\r
4110 // Get all FVB handles.\r
4111 //\r
4112 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
4113 if (EFI_ERROR (Status)) {\r
4114 return EFI_NOT_FOUND;\r
4115 }\r
4116\r
4117 //\r
4118 // Get the FVB to access variable store.\r
4119 //\r
4120 Fvb = NULL;\r
4121 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
4122 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
4123 if (EFI_ERROR (Status)) {\r
4124 Status = EFI_NOT_FOUND;\r
4125 break;\r
4126 }\r
4127\r
4128 //\r
4129 // Ensure this FVB protocol supported Write operation.\r
4130 //\r
4131 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
4132 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2d3fb919 4133 continue;\r
0c18794e 4134 }\r
2d3fb919 4135\r
0c18794e 4136 //\r
4137 // Compare the address and select the right one.\r
4138 //\r
4139 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
4140 if (EFI_ERROR (Status)) {\r
4141 continue;\r
4142 }\r
4143\r
931aae94
DG
4144 //\r
4145 // Assume one FVB has one type of BlockSize.\r
4146 //\r
4147 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
4148 if (EFI_ERROR (Status)) {\r
4149 continue;\r
4150 }\r
4151\r
4152 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
0c18794e 4153 if (FvbHandle != NULL) {\r
4154 *FvbHandle = HandleBuffer[Index];\r
4155 }\r
4156 if (FvbProtocol != NULL) {\r
4157 *FvbProtocol = Fvb;\r
4158 }\r
4159 Status = EFI_SUCCESS;\r
4160 break;\r
4161 }\r
4162 }\r
4163 FreePool (HandleBuffer);\r
4164\r
4165 if (Fvb == NULL) {\r
4166 Status = EFI_NOT_FOUND;\r
4167 }\r
2d3fb919 4168\r
4169 return Status;\r
0c18794e 4170}\r