]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
Return EFI_UNSUPPORTED if READY_TO_BOOT function is invoked at SMM runtime.
[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
6bc4e19f 19Copyright (c) 2009 - 2012, 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
38VARIABLE_STORE_HEADER *mNvVariableCache = NULL;\r
39\r
40///\r
41/// The memory entry used for variable statistics data.\r
42///\r
43VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
44\r
45\r
46/**\r
2d3fb919 47 Routine used to track statistical information about variable usage.\r
0c18794e 48 The data is stored in the EFI system table so it can be accessed later.\r
2d3fb919 49 VariableInfo.efi can dump out the table. Only Boot Services variable\r
0c18794e 50 accesses are tracked by this code. The PcdVariableCollectStatistics\r
2d3fb919 51 build flag controls if this feature is enabled.\r
0c18794e 52\r
2d3fb919 53 A read that hits in the cache will have Read and Cache true for\r
0c18794e 54 the transaction. Data is allocated by this routine, but never\r
55 freed.\r
56\r
57 @param[in] VariableName Name of the Variable to track.\r
58 @param[in] VendorGuid Guid of the Variable to track.\r
59 @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
60 @param[in] Read TRUE if GetVariable() was called.\r
61 @param[in] Write TRUE if SetVariable() was called.\r
62 @param[in] Delete TRUE if deleted via SetVariable().\r
63 @param[in] Cache TRUE for a cache hit.\r
64\r
65**/\r
66VOID\r
67UpdateVariableInfo (\r
68 IN CHAR16 *VariableName,\r
69 IN EFI_GUID *VendorGuid,\r
70 IN BOOLEAN Volatile,\r
71 IN BOOLEAN Read,\r
72 IN BOOLEAN Write,\r
73 IN BOOLEAN Delete,\r
74 IN BOOLEAN Cache\r
75 )\r
76{\r
77 VARIABLE_INFO_ENTRY *Entry;\r
78\r
79 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
80\r
81 if (AtRuntime ()) {\r
82 // Don't collect statistics at runtime.\r
83 return;\r
84 }\r
85\r
86 if (gVariableInfo == NULL) {\r
87 //\r
88 // On the first call allocate a entry and place a pointer to it in\r
89 // the EFI System Table.\r
90 //\r
91 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
92 ASSERT (gVariableInfo != NULL);\r
93\r
94 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
95 gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
96 ASSERT (gVariableInfo->Name != NULL);\r
97 StrCpy (gVariableInfo->Name, VariableName);\r
98 gVariableInfo->Volatile = Volatile;\r
99 }\r
100\r
2d3fb919 101\r
0c18794e 102 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
103 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
104 if (StrCmp (VariableName, Entry->Name) == 0) {\r
105 if (Read) {\r
106 Entry->ReadCount++;\r
107 }\r
108 if (Write) {\r
109 Entry->WriteCount++;\r
110 }\r
111 if (Delete) {\r
112 Entry->DeleteCount++;\r
113 }\r
114 if (Cache) {\r
115 Entry->CacheCount++;\r
116 }\r
117\r
118 return;\r
119 }\r
120 }\r
121\r
122 if (Entry->Next == NULL) {\r
123 //\r
124 // If the entry is not in the table add it.\r
125 // Next iteration of the loop will fill in the data.\r
126 //\r
127 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
128 ASSERT (Entry->Next != NULL);\r
129\r
130 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
131 Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
132 ASSERT (Entry->Next->Name != NULL);\r
133 StrCpy (Entry->Next->Name, VariableName);\r
134 Entry->Next->Volatile = Volatile;\r
135 }\r
136\r
137 }\r
138 }\r
139}\r
140\r
141\r
142/**\r
143\r
144 This code checks if variable header is valid or not.\r
145\r
146 @param Variable Pointer to the Variable Header.\r
147\r
148 @retval TRUE Variable header is valid.\r
149 @retval FALSE Variable header is not valid.\r
150\r
151**/\r
152BOOLEAN\r
153IsValidVariableHeader (\r
154 IN VARIABLE_HEADER *Variable\r
155 )\r
156{\r
157 if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
158 return FALSE;\r
159 }\r
160\r
161 return TRUE;\r
162}\r
163\r
164\r
165/**\r
166\r
167 This function writes data to the FWH at the correct LBA even if the LBAs\r
168 are fragmented.\r
169\r
170 @param Global Pointer to VARAIBLE_GLOBAL structure.\r
171 @param Volatile Point out the Variable is Volatile or Non-Volatile.\r
172 @param SetByIndex TRUE if target pointer is given as index.\r
173 FALSE if target pointer is absolute.\r
174 @param Fvb Pointer to the writable FVB protocol.\r
175 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
176 structure.\r
177 @param DataSize Size of data to be written.\r
178 @param Buffer Pointer to the buffer from which data is written.\r
179\r
180 @retval EFI_INVALID_PARAMETER Parameters not valid.\r
181 @retval EFI_SUCCESS Variable store successfully updated.\r
182\r
183**/\r
184EFI_STATUS\r
185UpdateVariableStore (\r
186 IN VARIABLE_GLOBAL *Global,\r
187 IN BOOLEAN Volatile,\r
188 IN BOOLEAN SetByIndex,\r
189 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
190 IN UINTN DataPtrIndex,\r
191 IN UINT32 DataSize,\r
192 IN UINT8 *Buffer\r
193 )\r
194{\r
195 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
196 UINTN BlockIndex2;\r
197 UINTN LinearOffset;\r
198 UINTN CurrWriteSize;\r
199 UINTN CurrWritePtr;\r
200 UINT8 *CurrBuffer;\r
201 EFI_LBA LbaNumber;\r
202 UINTN Size;\r
203 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
204 VARIABLE_STORE_HEADER *VolatileBase;\r
205 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
206 EFI_PHYSICAL_ADDRESS DataPtr;\r
207 EFI_STATUS Status;\r
208\r
209 FwVolHeader = NULL;\r
210 DataPtr = DataPtrIndex;\r
211\r
212 //\r
213 // Check if the Data is Volatile.\r
214 //\r
215 if (!Volatile) {\r
648f98d1 216 if (Fvb == NULL) {\r
217 return EFI_INVALID_PARAMETER;\r
218 }\r
0c18794e 219 Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
220 ASSERT_EFI_ERROR (Status);\r
221\r
222 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
223 //\r
224 // Data Pointer should point to the actual Address where data is to be\r
225 // written.\r
226 //\r
227 if (SetByIndex) {\r
228 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
229 }\r
230\r
231 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
232 return EFI_INVALID_PARAMETER;\r
233 }\r
234 } else {\r
235 //\r
236 // Data Pointer should point to the actual Address where data is to be\r
237 // written.\r
238 //\r
239 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
240 if (SetByIndex) {\r
241 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
242 }\r
243\r
244 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
245 return EFI_INVALID_PARAMETER;\r
246 }\r
2d3fb919 247\r
0c18794e 248 //\r
249 // If Volatile Variable just do a simple mem copy.\r
2d3fb919 250 //\r
0c18794e 251 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
252 return EFI_SUCCESS;\r
253 }\r
2d3fb919 254\r
0c18794e 255 //\r
256 // If we are here we are dealing with Non-Volatile Variables.\r
257 //\r
258 LinearOffset = (UINTN) FwVolHeader;\r
259 CurrWritePtr = (UINTN) DataPtr;\r
260 CurrWriteSize = DataSize;\r
261 CurrBuffer = Buffer;\r
262 LbaNumber = 0;\r
263\r
264 if (CurrWritePtr < LinearOffset) {\r
265 return EFI_INVALID_PARAMETER;\r
266 }\r
267\r
268 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
269 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
270 //\r
271 // Check to see if the Variable Writes are spanning through multiple\r
272 // blocks.\r
273 //\r
274 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
275 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
276 Status = Fvb->Write (\r
277 Fvb,\r
278 LbaNumber,\r
279 (UINTN) (CurrWritePtr - LinearOffset),\r
280 &CurrWriteSize,\r
281 CurrBuffer\r
282 );\r
283 return Status;\r
284 } else {\r
285 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
286 Status = Fvb->Write (\r
287 Fvb,\r
288 LbaNumber,\r
289 (UINTN) (CurrWritePtr - LinearOffset),\r
290 &Size,\r
291 CurrBuffer\r
292 );\r
293 if (EFI_ERROR (Status)) {\r
294 return Status;\r
295 }\r
296\r
297 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
298 CurrBuffer = CurrBuffer + Size;\r
299 CurrWriteSize = CurrWriteSize - Size;\r
300 }\r
301 }\r
302\r
303 LinearOffset += PtrBlockMapEntry->Length;\r
304 LbaNumber++;\r
305 }\r
306 }\r
307\r
308 return EFI_SUCCESS;\r
309}\r
310\r
311\r
312/**\r
313\r
314 This code gets the current status of Variable Store.\r
315\r
316 @param VarStoreHeader Pointer to the Variable Store Header.\r
317\r
318 @retval EfiRaw Variable store status is raw.\r
319 @retval EfiValid Variable store status is valid.\r
320 @retval EfiInvalid Variable store status is invalid.\r
321\r
322**/\r
323VARIABLE_STORE_STATUS\r
324GetVariableStoreStatus (\r
325 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
326 )\r
327{\r
328 if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&\r
329 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
330 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
331 ) {\r
332\r
333 return EfiValid;\r
334 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
335 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
336 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
337 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
338 VarStoreHeader->Size == 0xffffffff &&\r
339 VarStoreHeader->Format == 0xff &&\r
340 VarStoreHeader->State == 0xff\r
341 ) {\r
342\r
343 return EfiRaw;\r
344 } else {\r
345 return EfiInvalid;\r
346 }\r
347}\r
348\r
349\r
350/**\r
351\r
352 This code gets the size of name of variable.\r
353\r
354 @param Variable Pointer to the Variable Header.\r
355\r
356 @return UINTN Size of variable in bytes.\r
357\r
358**/\r
359UINTN\r
360NameSizeOfVariable (\r
361 IN VARIABLE_HEADER *Variable\r
362 )\r
363{\r
364 if (Variable->State == (UINT8) (-1) ||\r
365 Variable->DataSize == (UINT32) (-1) ||\r
366 Variable->NameSize == (UINT32) (-1) ||\r
367 Variable->Attributes == (UINT32) (-1)) {\r
368 return 0;\r
369 }\r
370 return (UINTN) Variable->NameSize;\r
371}\r
372\r
373/**\r
374\r
375 This code gets the size of variable data.\r
376\r
377 @param Variable Pointer to the Variable Header.\r
378\r
379 @return Size of variable in bytes.\r
380\r
381**/\r
382UINTN\r
383DataSizeOfVariable (\r
384 IN VARIABLE_HEADER *Variable\r
385 )\r
386{\r
387 if (Variable->State == (UINT8) (-1) ||\r
388 Variable->DataSize == (UINT32) (-1) ||\r
389 Variable->NameSize == (UINT32) (-1) ||\r
390 Variable->Attributes == (UINT32) (-1)) {\r
391 return 0;\r
392 }\r
393 return (UINTN) Variable->DataSize;\r
394}\r
395\r
396/**\r
397\r
398 This code gets the pointer to the variable name.\r
399\r
400 @param Variable Pointer to the Variable Header.\r
401\r
402 @return Pointer to Variable Name which is Unicode encoding.\r
403\r
404**/\r
405CHAR16 *\r
406GetVariableNamePtr (\r
407 IN VARIABLE_HEADER *Variable\r
408 )\r
409{\r
410\r
411 return (CHAR16 *) (Variable + 1);\r
412}\r
413\r
414/**\r
415\r
416 This code gets the pointer to the variable data.\r
417\r
418 @param Variable Pointer to the Variable Header.\r
419\r
420 @return Pointer to Variable Data.\r
421\r
422**/\r
423UINT8 *\r
424GetVariableDataPtr (\r
425 IN VARIABLE_HEADER *Variable\r
426 )\r
427{\r
428 UINTN Value;\r
2d3fb919 429\r
0c18794e 430 //\r
431 // Be careful about pad size for alignment.\r
432 //\r
433 Value = (UINTN) GetVariableNamePtr (Variable);\r
434 Value += NameSizeOfVariable (Variable);\r
435 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
436\r
437 return (UINT8 *) Value;\r
438}\r
439\r
440\r
441/**\r
442\r
443 This code gets the pointer to the next variable header.\r
444\r
445 @param Variable Pointer to the Variable Header.\r
446\r
447 @return Pointer to next variable header.\r
448\r
449**/\r
450VARIABLE_HEADER *\r
451GetNextVariablePtr (\r
452 IN VARIABLE_HEADER *Variable\r
453 )\r
454{\r
455 UINTN Value;\r
456\r
457 if (!IsValidVariableHeader (Variable)) {\r
458 return NULL;\r
459 }\r
460\r
461 Value = (UINTN) GetVariableDataPtr (Variable);\r
462 Value += DataSizeOfVariable (Variable);\r
463 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
464\r
465 //\r
466 // Be careful about pad size for alignment.\r
467 //\r
468 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
469}\r
470\r
471/**\r
472\r
473 Gets the pointer to the first variable header in given variable store area.\r
474\r
475 @param VarStoreHeader Pointer to the Variable Store Header.\r
476\r
477 @return Pointer to the first variable header.\r
478\r
479**/\r
480VARIABLE_HEADER *\r
481GetStartPointer (\r
482 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
483 )\r
484{\r
485 //\r
486 // The end of variable store.\r
487 //\r
488 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
489}\r
490\r
491/**\r
492\r
493 Gets the pointer to the end of the variable storage area.\r
494\r
495 This function gets pointer to the end of the variable storage\r
496 area, according to the input variable store header.\r
497\r
498 @param VarStoreHeader Pointer to the Variable Store Header.\r
499\r
2d3fb919 500 @return Pointer to the end of the variable storage area.\r
0c18794e 501\r
502**/\r
503VARIABLE_HEADER *\r
504GetEndPointer (\r
505 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
506 )\r
507{\r
508 //\r
509 // The end of variable store\r
510 //\r
511 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
512}\r
513\r
514\r
515/**\r
516\r
517 Variable store garbage collection and reclaim operation.\r
518\r
519 @param VariableBase Base address of variable store.\r
520 @param LastVariableOffset Offset of last variable.\r
521 @param IsVolatile The variable store is volatile or not;\r
522 if it is non-volatile, need FTW.\r
523 @param UpdatingVariable Pointer to updating variable.\r
524\r
525 @return EFI_OUT_OF_RESOURCES\r
526 @return EFI_SUCCESS\r
527 @return Others\r
528\r
529**/\r
530EFI_STATUS\r
531Reclaim (\r
532 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
533 OUT UINTN *LastVariableOffset,\r
534 IN BOOLEAN IsVolatile,\r
535 IN VARIABLE_HEADER *UpdatingVariable\r
536 )\r
537{\r
538 VARIABLE_HEADER *Variable;\r
539 VARIABLE_HEADER *AddedVariable;\r
540 VARIABLE_HEADER *NextVariable;\r
541 VARIABLE_HEADER *NextAddedVariable;\r
542 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
543 UINT8 *ValidBuffer;\r
544 UINTN MaximumBufferSize;\r
545 UINTN VariableSize;\r
546 UINTN VariableNameSize;\r
547 UINTN UpdatingVariableNameSize;\r
548 UINTN NameSize;\r
549 UINT8 *CurrPtr;\r
550 VOID *Point0;\r
551 VOID *Point1;\r
552 BOOLEAN FoundAdded;\r
553 EFI_STATUS Status;\r
554 CHAR16 *VariableNamePtr;\r
555 CHAR16 *UpdatingVariableNamePtr;\r
556\r
557 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
558 //\r
559 // Recalculate the total size of Common/HwErr type variables in non-volatile area.\r
560 //\r
561 if (!IsVolatile) {\r
562 mVariableModuleGlobal->CommonVariableTotalSize = 0;\r
563 mVariableModuleGlobal->HwErrVariableTotalSize = 0;\r
564 }\r
565\r
566 //\r
567 // Start Pointers for the variable.\r
568 //\r
569 Variable = GetStartPointer (VariableStoreHeader);\r
570 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
571\r
572 while (IsValidVariableHeader (Variable)) {\r
573 NextVariable = GetNextVariablePtr (Variable);\r
2d3fb919 574 if (Variable->State == VAR_ADDED ||\r
0c18794e 575 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
576 ) {\r
577 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
578 MaximumBufferSize += VariableSize;\r
579 }\r
580\r
581 Variable = NextVariable;\r
582 }\r
583\r
584 //\r
2d3fb919 585 // Reserve the 1 Bytes with Oxff to identify the\r
586 // end of the variable buffer.\r
587 //\r
0c18794e 588 MaximumBufferSize += 1;\r
589 ValidBuffer = AllocatePool (MaximumBufferSize);\r
590 if (ValidBuffer == NULL) {\r
591 return EFI_OUT_OF_RESOURCES;\r
592 }\r
593\r
594 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
595\r
596 //\r
597 // Copy variable store header.\r
598 //\r
599 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
600 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
601\r
602 //\r
603 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
2d3fb919 604 //\r
0c18794e 605 Variable = GetStartPointer (VariableStoreHeader);\r
606 while (IsValidVariableHeader (Variable)) {\r
607 NextVariable = GetNextVariablePtr (Variable);\r
608 if (Variable->State == VAR_ADDED) {\r
609 if (UpdatingVariable != NULL) {\r
610 if (UpdatingVariable == Variable) {\r
611 Variable = NextVariable;\r
612 continue;\r
613 }\r
614\r
615 VariableNameSize = NameSizeOfVariable(Variable);\r
616 UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);\r
617\r
618 VariableNamePtr = GetVariableNamePtr (Variable);\r
619 UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);\r
620 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&\r
621 VariableNameSize == UpdatingVariableNameSize &&\r
622 CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {\r
623 Variable = NextVariable;\r
624 continue;\r
625 }\r
626 }\r
627 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
628 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
629 CurrPtr += VariableSize;\r
630 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
631 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
632 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
633 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
634 }\r
635 }\r
636 Variable = NextVariable;\r
637 }\r
638\r
639 //\r
640 // Reinstall the variable being updated if it is not NULL.\r
641 //\r
642 if (UpdatingVariable != NULL) {\r
643 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;\r
644 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);\r
645 CurrPtr += VariableSize;\r
646 if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
647 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
648 } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
649 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
650 }\r
651 }\r
652\r
653 //\r
654 // Reinstall all in delete transition variables.\r
2d3fb919 655 //\r
0c18794e 656 Variable = GetStartPointer (VariableStoreHeader);\r
657 while (IsValidVariableHeader (Variable)) {\r
658 NextVariable = GetNextVariablePtr (Variable);\r
659 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
660\r
661 //\r
2d3fb919 662 // Buffer has cached all ADDED variable.\r
0c18794e 663 // Per IN_DELETED variable, we have to guarantee that\r
2d3fb919 664 // no ADDED one in previous buffer.\r
665 //\r
666\r
0c18794e 667 FoundAdded = FALSE;\r
668 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
669 while (IsValidVariableHeader (AddedVariable)) {\r
670 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
671 NameSize = NameSizeOfVariable (AddedVariable);\r
672 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
673 NameSize == NameSizeOfVariable (Variable)\r
674 ) {\r
675 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
676 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
677 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {\r
678 FoundAdded = TRUE;\r
679 break;\r
680 }\r
681 }\r
682 AddedVariable = NextAddedVariable;\r
683 }\r
684 if (!FoundAdded) {\r
685 //\r
686 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r
687 //\r
688 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
689 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
690 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
691 CurrPtr += VariableSize;\r
692 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
693 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
694 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
695 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
696 }\r
697 }\r
698 }\r
699\r
700 Variable = NextVariable;\r
701 }\r
702\r
703 if (IsVolatile) {\r
704 //\r
705 // If volatile variable store, just copy valid buffer.\r
706 //\r
707 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
708 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r
709 Status = EFI_SUCCESS;\r
710 } else {\r
711 //\r
712 // If non-volatile variable store, perform FTW here.\r
713 //\r
714 Status = FtwVariableSpace (\r
715 VariableBase,\r
716 ValidBuffer,\r
717 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r
718 );\r
719 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
720 }\r
721 if (!EFI_ERROR (Status)) {\r
722 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r
723 } else {\r
724 *LastVariableOffset = 0;\r
725 }\r
726\r
727 FreePool (ValidBuffer);\r
728\r
729 return Status;\r
730}\r
731\r
9a000b46
RN
732/**\r
733 Find the variable in the specified variable store.\r
734\r
ecc722ad 735 @param[in] VariableName Name of the variable to be found\r
736 @param[in] VendorGuid Vendor GUID to be found.\r
9622df63
SZ
737 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
738 check at runtime when searching variable.\r
ecc722ad 739 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
9a000b46 740\r
ecc722ad 741 @retval EFI_SUCCESS Variable found successfully\r
742 @retval EFI_NOT_FOUND Variable not found\r
9a000b46
RN
743**/\r
744EFI_STATUS\r
745FindVariableEx (\r
746 IN CHAR16 *VariableName,\r
747 IN EFI_GUID *VendorGuid,\r
9622df63 748 IN BOOLEAN IgnoreRtCheck,\r
9a000b46
RN
749 IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
750 )\r
751{\r
752 VARIABLE_HEADER *InDeletedVariable;\r
753 VOID *Point;\r
754\r
755 //\r
756 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
757 //\r
758 InDeletedVariable = NULL;\r
759\r
760 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
761 ; (PtrTrack->CurrPtr < PtrTrack->EndPtr) && IsValidVariableHeader (PtrTrack->CurrPtr)\r
762 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
763 ) {\r
2d3fb919 764 if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
9a000b46
RN
765 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
766 ) {\r
9622df63 767 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
9a000b46
RN
768 if (VariableName[0] == 0) {\r
769 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
770 InDeletedVariable = PtrTrack->CurrPtr;\r
771 } else {\r
772 return EFI_SUCCESS;\r
773 }\r
774 } else {\r
775 if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {\r
776 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
777\r
778 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
779 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
780 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
781 InDeletedVariable = PtrTrack->CurrPtr;\r
782 } else {\r
783 return EFI_SUCCESS;\r
784 }\r
785 }\r
786 }\r
787 }\r
788 }\r
789 }\r
790 }\r
791\r
792 PtrTrack->CurrPtr = InDeletedVariable;\r
793 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
794}\r
795\r
0c18794e 796\r
797/**\r
798 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
799\r
800 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
801 If VariableName is an empty string, then we just return the first\r
802 qualified variable without comparing VariableName and VendorGuid.\r
9622df63
SZ
803 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
804 at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
805 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
0c18794e 806\r
ecc722ad 807 @param[in] VariableName Name of the variable to be found.\r
808 @param[in] VendorGuid Vendor GUID to be found.\r
809 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
0c18794e 810 including the range searched and the target position.\r
ecc722ad 811 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
0c18794e 812 base of volatile variable storage area, base of\r
813 NV variable storage area, and a lock.\r
9622df63
SZ
814 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
815 check at runtime when searching variable.\r
0c18794e 816\r
817 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
818 VendorGuid is NULL.\r
819 @retval EFI_SUCCESS Variable successfully found.\r
820 @retval EFI_NOT_FOUND Variable not found\r
821\r
822**/\r
823EFI_STATUS\r
824FindVariable (\r
825 IN CHAR16 *VariableName,\r
826 IN EFI_GUID *VendorGuid,\r
827 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
ecc722ad 828 IN VARIABLE_GLOBAL *Global,\r
9622df63 829 IN BOOLEAN IgnoreRtCheck\r
0c18794e 830 )\r
831{\r
9a000b46
RN
832 EFI_STATUS Status;\r
833 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
834 VARIABLE_STORE_TYPE Type;\r
835\r
836 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
837 return EFI_INVALID_PARAMETER;\r
838 }\r
0c18794e 839\r
840 //\r
9a000b46 841 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
0c18794e 842 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
843 // make use of this mapping to implement search algorithm.\r
844 //\r
9a000b46
RN
845 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
846 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
847 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
0c18794e 848\r
849 //\r
9a000b46 850 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
0c18794e 851 //\r
9a000b46
RN
852 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
853 if (VariableStoreHeader[Type] == NULL) {\r
854 continue;\r
855 }\r
0c18794e 856\r
9a000b46
RN
857 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
858 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
859 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
0c18794e 860\r
9622df63 861 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
9a000b46
RN
862 if (!EFI_ERROR (Status)) {\r
863 return Status;\r
0c18794e 864 }\r
865 }\r
0c18794e 866 return EFI_NOT_FOUND;\r
867}\r
868\r
869/**\r
870 Get index from supported language codes according to language string.\r
871\r
872 This code is used to get corresponding index in supported language codes. It can handle\r
873 RFC4646 and ISO639 language tags.\r
874 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
875 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
876\r
877 For example:\r
878 SupportedLang = "engfraengfra"\r
879 Lang = "eng"\r
880 Iso639Language = TRUE\r
881 The return value is "0".\r
882 Another example:\r
883 SupportedLang = "en;fr;en-US;fr-FR"\r
884 Lang = "fr-FR"\r
885 Iso639Language = FALSE\r
886 The return value is "3".\r
887\r
888 @param SupportedLang Platform supported language codes.\r
889 @param Lang Configured language.\r
890 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
891\r
892 @retval The index of language in the language codes.\r
893\r
894**/\r
895UINTN\r
896GetIndexFromSupportedLangCodes(\r
897 IN CHAR8 *SupportedLang,\r
898 IN CHAR8 *Lang,\r
899 IN BOOLEAN Iso639Language\r
2d3fb919 900 )\r
0c18794e 901{\r
902 UINTN Index;\r
903 UINTN CompareLength;\r
904 UINTN LanguageLength;\r
905\r
906 if (Iso639Language) {\r
907 CompareLength = ISO_639_2_ENTRY_SIZE;\r
908 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
909 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
910 //\r
911 // Successfully find the index of Lang string in SupportedLang string.\r
912 //\r
913 Index = Index / CompareLength;\r
914 return Index;\r
915 }\r
916 }\r
917 ASSERT (FALSE);\r
918 return 0;\r
919 } else {\r
920 //\r
921 // Compare RFC4646 language code\r
922 //\r
923 Index = 0;\r
924 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
925\r
926 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
927 //\r
928 // Skip ';' characters in SupportedLang\r
929 //\r
930 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
931 //\r
932 // Determine the length of the next language code in SupportedLang\r
933 //\r
934 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
2d3fb919 935\r
936 if ((CompareLength == LanguageLength) &&\r
0c18794e 937 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
938 //\r
939 // Successfully find the index of Lang string in SupportedLang string.\r
940 //\r
941 return Index;\r
942 }\r
943 }\r
944 ASSERT (FALSE);\r
945 return 0;\r
946 }\r
947}\r
948\r
949/**\r
950 Get language string from supported language codes according to index.\r
951\r
952 This code is used to get corresponding language strings in supported language codes. It can handle\r
953 RFC4646 and ISO639 language tags.\r
954 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
955 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
956\r
957 For example:\r
958 SupportedLang = "engfraengfra"\r
959 Index = "1"\r
960 Iso639Language = TRUE\r
961 The return value is "fra".\r
962 Another example:\r
963 SupportedLang = "en;fr;en-US;fr-FR"\r
964 Index = "1"\r
965 Iso639Language = FALSE\r
966 The return value is "fr".\r
967\r
968 @param SupportedLang Platform supported language codes.\r
969 @param Index The index in supported language codes.\r
970 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
971\r
972 @retval The language string in the language codes.\r
973\r
974**/\r
975CHAR8 *\r
976GetLangFromSupportedLangCodes (\r
977 IN CHAR8 *SupportedLang,\r
978 IN UINTN Index,\r
979 IN BOOLEAN Iso639Language\r
980)\r
981{\r
982 UINTN SubIndex;\r
983 UINTN CompareLength;\r
984 CHAR8 *Supported;\r
985\r
986 SubIndex = 0;\r
987 Supported = SupportedLang;\r
988 if (Iso639Language) {\r
989 //\r
990 // According to the index of Lang string in SupportedLang string to get the language.\r
991 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
992 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
993 //\r
994 CompareLength = ISO_639_2_ENTRY_SIZE;\r
995 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
996 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
2d3fb919 997\r
0c18794e 998 } else {\r
999 while (TRUE) {\r
1000 //\r
1001 // Take semicolon as delimitation, sequentially traverse supported language codes.\r
1002 //\r
1003 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
1004 Supported++;\r
1005 }\r
1006 if ((*Supported == '\0') && (SubIndex != Index)) {\r
1007 //\r
1008 // Have completed the traverse, but not find corrsponding string.\r
1009 // This case is not allowed to happen.\r
1010 //\r
1011 ASSERT(FALSE);\r
1012 return NULL;\r
1013 }\r
1014 if (SubIndex == Index) {\r
1015 //\r
1016 // According to the index of Lang string in SupportedLang string to get the language.\r
1017 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1018 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1019 //\r
1020 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
1021 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
1022 }\r
1023 SubIndex++;\r
1024\r
1025 //\r
1026 // Skip ';' characters in Supported\r
1027 //\r
1028 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1029 }\r
1030 }\r
1031}\r
1032\r
1033/**\r
2d3fb919 1034 Returns a pointer to an allocated buffer that contains the best matching language\r
1035 from a set of supported languages.\r
1036\r
1037 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
0c18794e 1038 code types may not be mixed in a single call to this function. This function\r
1039 supports a variable argument list that allows the caller to pass in a prioritized\r
1040 list of language codes to test against all the language codes in SupportedLanguages.\r
1041\r
1042 If SupportedLanguages is NULL, then ASSERT().\r
1043\r
1044 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
2d3fb919 1045 contains a set of language codes in the format\r
0c18794e 1046 specified by Iso639Language.\r
1047 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1048 in ISO 639-2 format. If FALSE, then all language\r
1049 codes are assumed to be in RFC 4646 language format\r
2d3fb919 1050 @param[in] ... A variable argument list that contains pointers to\r
0c18794e 1051 Null-terminated ASCII strings that contain one or more\r
1052 language codes in the format specified by Iso639Language.\r
1053 The first language code from each of these language\r
1054 code lists is used to determine if it is an exact or\r
2d3fb919 1055 close match to any of the language codes in\r
0c18794e 1056 SupportedLanguages. Close matches only apply to RFC 4646\r
1057 language codes, and the matching algorithm from RFC 4647\r
2d3fb919 1058 is used to determine if a close match is present. If\r
0c18794e 1059 an exact or close match is found, then the matching\r
1060 language code from SupportedLanguages is returned. If\r
1061 no matches are found, then the next variable argument\r
2d3fb919 1062 parameter is evaluated. The variable argument list\r
0c18794e 1063 is terminated by a NULL.\r
1064\r
1065 @retval NULL The best matching language could not be found in SupportedLanguages.\r
2d3fb919 1066 @retval NULL There are not enough resources available to return the best matching\r
0c18794e 1067 language.\r
2d3fb919 1068 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
0c18794e 1069 language in SupportedLanguages.\r
1070\r
1071**/\r
1072CHAR8 *\r
1073EFIAPI\r
1074VariableGetBestLanguage (\r
2d3fb919 1075 IN CONST CHAR8 *SupportedLanguages,\r
0c18794e 1076 IN BOOLEAN Iso639Language,\r
1077 ...\r
1078 )\r
1079{\r
1080 VA_LIST Args;\r
1081 CHAR8 *Language;\r
1082 UINTN CompareLength;\r
1083 UINTN LanguageLength;\r
1084 CONST CHAR8 *Supported;\r
1085 CHAR8 *Buffer;\r
1086\r
648f98d1 1087 if (SupportedLanguages == NULL) {\r
1088 return NULL;\r
1089 }\r
0c18794e 1090\r
1091 VA_START (Args, Iso639Language);\r
1092 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1093 //\r
1094 // Default to ISO 639-2 mode\r
1095 //\r
1096 CompareLength = 3;\r
1097 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1098\r
1099 //\r
1100 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1101 //\r
1102 if (!Iso639Language) {\r
1103 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1104 }\r
1105\r
1106 //\r
1107 // Trim back the length of Language used until it is empty\r
1108 //\r
1109 while (LanguageLength > 0) {\r
1110 //\r
1111 // Loop through all language codes in SupportedLanguages\r
1112 //\r
1113 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1114 //\r
1115 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1116 //\r
1117 if (!Iso639Language) {\r
1118 //\r
1119 // Skip ';' characters in Supported\r
1120 //\r
1121 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1122 //\r
1123 // Determine the length of the next language code in Supported\r
1124 //\r
1125 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1126 //\r
1127 // If Language is longer than the Supported, then skip to the next language\r
1128 //\r
1129 if (LanguageLength > CompareLength) {\r
1130 continue;\r
1131 }\r
1132 }\r
1133 //\r
1134 // See if the first LanguageLength characters in Supported match Language\r
1135 //\r
1136 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1137 VA_END (Args);\r
1138\r
1139 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1140 Buffer[CompareLength] = '\0';\r
1141 return CopyMem (Buffer, Supported, CompareLength);\r
1142 }\r
1143 }\r
1144\r
1145 if (Iso639Language) {\r
1146 //\r
1147 // If ISO 639 mode, then each language can only be tested once\r
1148 //\r
1149 LanguageLength = 0;\r
1150 } else {\r
1151 //\r
2d3fb919 1152 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
0c18794e 1153 //\r
1154 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1155 }\r
1156 }\r
1157 }\r
1158 VA_END (Args);\r
1159\r
1160 //\r
2d3fb919 1161 // No matches were found\r
0c18794e 1162 //\r
1163 return NULL;\r
1164}\r
1165\r
1166/**\r
1167 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
1168\r
1169 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
1170\r
1171 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1172 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
1173\r
1174 @param[in] VariableName Name of variable.\r
1175\r
1176 @param[in] Data Variable data.\r
1177\r
1178 @param[in] DataSize Size of data. 0 means delete.\r
1179\r
1180**/\r
1181VOID\r
4d832aab 1182AutoUpdateLangVariable (\r
0c18794e 1183 IN CHAR16 *VariableName,\r
1184 IN VOID *Data,\r
1185 IN UINTN DataSize\r
1186 )\r
1187{\r
1188 EFI_STATUS Status;\r
1189 CHAR8 *BestPlatformLang;\r
1190 CHAR8 *BestLang;\r
1191 UINTN Index;\r
1192 UINT32 Attributes;\r
1193 VARIABLE_POINTER_TRACK Variable;\r
1194 BOOLEAN SetLanguageCodes;\r
1195\r
1196 //\r
1197 // Don't do updates for delete operation\r
1198 //\r
1199 if (DataSize == 0) {\r
1200 return;\r
1201 }\r
1202\r
1203 SetLanguageCodes = FALSE;\r
1204\r
1205 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
1206 //\r
1207 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1208 //\r
1209 if (AtRuntime ()) {\r
1210 return;\r
1211 }\r
1212\r
1213 SetLanguageCodes = TRUE;\r
1214\r
1215 //\r
1216 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1217 // Therefore, in variable driver, only store the original value for other use.\r
1218 //\r
1219 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1220 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1221 }\r
1222 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1223 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1224\r
1225 //\r
2d3fb919 1226 // PlatformLang holds a single language from PlatformLangCodes,\r
0c18794e 1227 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
1228 //\r
1229 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1230 FreePool (mVariableModuleGlobal->PlatformLang);\r
1231 }\r
1232 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1233 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
1234\r
1235 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
1236 //\r
1237 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
1238 //\r
1239 if (AtRuntime ()) {\r
1240 return;\r
1241 }\r
1242\r
1243 SetLanguageCodes = TRUE;\r
1244\r
1245 //\r
1246 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1247 // Therefore, in variable driver, only store the original value for other use.\r
1248 //\r
1249 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1250 FreePool (mVariableModuleGlobal->LangCodes);\r
1251 }\r
1252 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1253 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1254 }\r
1255\r
2d3fb919 1256 if (SetLanguageCodes\r
0c18794e 1257 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1258 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1259 //\r
1260 // Update Lang if PlatformLang is already set\r
1261 // Update PlatformLang if Lang is already set\r
1262 //\r
ecc722ad 1263 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 1264 if (!EFI_ERROR (Status)) {\r
1265 //\r
1266 // Update Lang\r
1267 //\r
1268 VariableName = L"PlatformLang";\r
1269 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1270 DataSize = Variable.CurrPtr->DataSize;\r
1271 } else {\r
ecc722ad 1272 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 1273 if (!EFI_ERROR (Status)) {\r
1274 //\r
1275 // Update PlatformLang\r
1276 //\r
1277 VariableName = L"Lang";\r
1278 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1279 DataSize = Variable.CurrPtr->DataSize;\r
1280 } else {\r
1281 //\r
1282 // Neither PlatformLang nor Lang is set, directly return\r
1283 //\r
1284 return;\r
1285 }\r
1286 }\r
1287 }\r
2d3fb919 1288\r
0c18794e 1289 //\r
1290 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1291 //\r
1292 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
1293\r
1294 if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
1295 //\r
1296 // Update Lang when PlatformLangCodes/LangCodes were set.\r
1297 //\r
1298 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1299 //\r
1300 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1301 //\r
1302 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1303 if (BestPlatformLang != NULL) {\r
1304 //\r
1305 // Get the corresponding index in language codes.\r
1306 //\r
1307 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
1308\r
1309 //\r
1310 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1311 //\r
1312 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
1313\r
1314 //\r
1315 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1316 //\r
ecc722ad 1317 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 1318\r
1319 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang,\r
1320 ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);\r
1321\r
1322 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
1323\r
1324 ASSERT_EFI_ERROR(Status);\r
1325 }\r
1326 }\r
1327\r
1328 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
1329 //\r
1330 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
1331 //\r
1332 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1333 //\r
1334 // When setting Lang, firstly get most matched language string from supported language codes.\r
1335 //\r
1336 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
1337 if (BestLang != NULL) {\r
1338 //\r
1339 // Get the corresponding index in language codes.\r
1340 //\r
1341 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
1342\r
1343 //\r
1344 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1345 //\r
1346 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
1347\r
1348 //\r
1349 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1350 //\r
ecc722ad 1351 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 1352\r
2d3fb919 1353 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang,\r
0c18794e 1354 AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);\r
1355\r
1356 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
1357 ASSERT_EFI_ERROR (Status);\r
1358 }\r
1359 }\r
1360 }\r
1361}\r
1362\r
1363/**\r
1364 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
1365 index of associated public key is needed.\r
1366\r
1367 @param[in] VariableName Name of variable.\r
1368 @param[in] VendorGuid Guid of variable.\r
1369 @param[in] Data Variable data.\r
1370 @param[in] DataSize Size of data. 0 means delete.\r
1371 @param[in] Attributes Attributes of the variable.\r
1372 @param[in] KeyIndex Index of associated public key.\r
1373 @param[in] MonotonicCount Value of associated monotonic count.\r
1374 @param[in] CacheVariable The variable information which is used to keep track of variable usage.\r
1375 @param[in] TimeStamp Value of associated TimeStamp.\r
2d3fb919 1376\r
0c18794e 1377 @retval EFI_SUCCESS The update operation is success.\r
1378 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
1379\r
1380**/\r
1381EFI_STATUS\r
1382UpdateVariable (\r
1383 IN CHAR16 *VariableName,\r
1384 IN EFI_GUID *VendorGuid,\r
1385 IN VOID *Data,\r
1386 IN UINTN DataSize,\r
1387 IN UINT32 Attributes OPTIONAL,\r
1388 IN UINT32 KeyIndex OPTIONAL,\r
1389 IN UINT64 MonotonicCount OPTIONAL,\r
1390 IN VARIABLE_POINTER_TRACK *CacheVariable,\r
1391 IN EFI_TIME *TimeStamp OPTIONAL\r
1392 )\r
1393{\r
1394 EFI_STATUS Status;\r
1395 VARIABLE_HEADER *NextVariable;\r
1396 UINTN ScratchSize;\r
1397 UINTN ScratchDataSize;\r
1398 UINTN NonVolatileVarableStoreSize;\r
1399 UINTN VarNameOffset;\r
1400 UINTN VarDataOffset;\r
1401 UINTN VarNameSize;\r
1402 UINTN VarSize;\r
1403 BOOLEAN Volatile;\r
1404 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
1405 UINT8 State;\r
1406 BOOLEAN Reclaimed;\r
1407 VARIABLE_POINTER_TRACK *Variable;\r
1408 VARIABLE_POINTER_TRACK NvVariable;\r
1409 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1410 UINTN CacheOffset;\r
1411 UINTN BufSize;\r
1412 UINTN DataOffset;\r
1413 UINTN RevBufSize;\r
1414\r
1415 if (mVariableModuleGlobal->FvbInstance == NULL) {\r
1416 //\r
1417 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.\r
1418 //\r
1419 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
1420 //\r
1421 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
1422 //\r
1423 return EFI_NOT_AVAILABLE_YET;\r
1424 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1425 //\r
1426 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
1427 // The authenticated variable perhaps is not initialized, just return here.\r
1428 //\r
1429 return EFI_NOT_AVAILABLE_YET;\r
1430 }\r
1431 }\r
1432\r
1433 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
1434 Variable = CacheVariable;\r
1435 } else {\r
1436 //\r
1437 // Update/Delete existing NV variable.\r
1438 // CacheVariable points to the variable in the memory copy of Flash area\r
1439 // Now let Variable points to the same variable in Flash area.\r
1440 //\r
1441 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
2d3fb919 1442 Variable = &NvVariable;\r
0c18794e 1443 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
1444 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
1445 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
1446 Variable->Volatile = FALSE;\r
2d3fb919 1447 }\r
0c18794e 1448\r
1449 Fvb = mVariableModuleGlobal->FvbInstance;\r
1450 Reclaimed = FALSE;\r
1451\r
1452 //\r
1453 // Tricky part: Use scratch data area at the end of volatile variable store\r
1454 // as a temporary storage.\r
1455 //\r
1456 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
1457 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
1458 ScratchDataSize = ScratchSize - sizeof (VARIABLE_HEADER) - StrSize (VariableName) - GET_PAD_SIZE (StrSize (VariableName));\r
1459\r
1460 if (Variable->CurrPtr != NULL) {\r
1461 //\r
1462 // Update/Delete existing variable.\r
1463 //\r
2d3fb919 1464 if (AtRuntime ()) {\r
0c18794e 1465 //\r
2d3fb919 1466 // If AtRuntime and the variable is Volatile and Runtime Access,\r
1467 // the volatile is ReadOnly, and SetVariable should be aborted and\r
0c18794e 1468 // return EFI_WRITE_PROTECTED.\r
1469 //\r
1470 if (Variable->Volatile) {\r
1471 Status = EFI_WRITE_PROTECTED;\r
1472 goto Done;\r
1473 }\r
1474 //\r
1475 // Only variable that have NV attributes can be updated/deleted in Runtime.\r
1476 //\r
1477 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1478 Status = EFI_INVALID_PARAMETER;\r
2d3fb919 1479 goto Done;\r
0c18794e 1480 }\r
ecc722ad 1481 \r
1482 //\r
1483 // Only variable that have RT attributes can be updated/deleted in Runtime.\r
1484 //\r
1485 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r
1486 Status = EFI_INVALID_PARAMETER;\r
1487 goto Done;\r
1488 }\r
0c18794e 1489 }\r
1490\r
1491 //\r
1492 // Setting a data variable with no access, or zero DataSize attributes\r
1493 // causes it to be deleted.\r
2d3fb919 1494 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r
1495 // not delete the variable.\r
0c18794e 1496 //\r
2d3fb919 1497 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r
0c18794e 1498 State = Variable->CurrPtr->State;\r
1499 State &= VAR_DELETED;\r
1500\r
1501 Status = UpdateVariableStore (\r
1502 &mVariableModuleGlobal->VariableGlobal,\r
1503 Variable->Volatile,\r
1504 FALSE,\r
1505 Fvb,\r
1506 (UINTN) &Variable->CurrPtr->State,\r
1507 sizeof (UINT8),\r
1508 &State\r
2d3fb919 1509 );\r
0c18794e 1510 if (!EFI_ERROR (Status)) {\r
1511 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1512 if (!Variable->Volatile) {\r
1513 CacheVariable->CurrPtr->State = State;\r
1514 }\r
1515 }\r
2d3fb919 1516 goto Done;\r
0c18794e 1517 }\r
1518 //\r
1519 // If the variable is marked valid, and the same data has been passed in,\r
1520 // then return to the caller immediately.\r
1521 //\r
1522 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1523 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0) &&\r
2d3fb919 1524 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
1525 (TimeStamp == NULL)) {\r
1526 //\r
1527 // Variable content unchanged and no need to update timestamp, just return.\r
1528 //\r
0c18794e 1529 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
1530 Status = EFI_SUCCESS;\r
1531 goto Done;\r
1532 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1533 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1534\r
1535 //\r
1536 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable\r
1537 //\r
1538 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
2d3fb919 1539 //\r
1540 // Cache the previous variable data into StorageArea.\r
1541 //\r
1542 DataOffset = sizeof (VARIABLE_HEADER) + Variable->CurrPtr->NameSize + GET_PAD_SIZE (Variable->CurrPtr->NameSize);\r
1543 CopyMem (mStorageArea, (UINT8*)((UINTN) Variable->CurrPtr + DataOffset), Variable->CurrPtr->DataSize);\r
1544\r
1545 if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) ||\r
1546 (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
1547 //\r
1548 // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data\r
1549 // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of\r
1550 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.\r
1551 //\r
1552 BufSize = AppendSignatureList (mStorageArea, Variable->CurrPtr->DataSize, Data, DataSize);\r
1553 if (BufSize == Variable->CurrPtr->DataSize) {\r
1554 if ((TimeStamp == NULL) || CompareTimeStamp (TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
1555 //\r
1556 // New EFI_SIGNATURE_DATA is not found and timestamp is not later\r
1557 // than current timestamp, return EFI_SUCCESS directly.\r
1558 //\r
1559 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
1560 Status = EFI_SUCCESS;\r
1561 goto Done;\r
1562 }\r
1563 }\r
1564 } else {\r
1565 //\r
1566 // For other Variables, append the new data to the end of previous data.\r
1567 //\r
1568 CopyMem ((UINT8*)((UINTN) mStorageArea + Variable->CurrPtr->DataSize), Data, DataSize);\r
1569 BufSize = Variable->CurrPtr->DataSize + DataSize;\r
1570 }\r
1571\r
1572 RevBufSize = MIN (PcdGet32 (PcdMaxVariableSize), ScratchDataSize);\r
0c18794e 1573 if (BufSize > RevBufSize) {\r
1574 //\r
1575 // If variable size (previous + current) is bigger than reserved buffer in runtime,\r
1576 // return EFI_OUT_OF_RESOURCES.\r
1577 //\r
1578 return EFI_OUT_OF_RESOURCES;\r
1579 }\r
2d3fb919 1580\r
0c18794e 1581 //\r
1582 // Override Data and DataSize which are used for combined data area including previous and new data.\r
1583 //\r
1584 Data = mStorageArea;\r
1585 DataSize = BufSize;\r
1586 }\r
1587\r
1588 //\r
1589 // Mark the old variable as in delete transition.\r
1590 //\r
1591 State = Variable->CurrPtr->State;\r
1592 State &= VAR_IN_DELETED_TRANSITION;\r
1593\r
1594 Status = UpdateVariableStore (\r
1595 &mVariableModuleGlobal->VariableGlobal,\r
1596 Variable->Volatile,\r
1597 FALSE,\r
1598 Fvb,\r
1599 (UINTN) &Variable->CurrPtr->State,\r
1600 sizeof (UINT8),\r
1601 &State\r
2d3fb919 1602 );\r
0c18794e 1603 if (EFI_ERROR (Status)) {\r
2d3fb919 1604 goto Done;\r
1605 }\r
0c18794e 1606 if (!Variable->Volatile) {\r
1607 CacheVariable->CurrPtr->State = State;\r
1608 }\r
2d3fb919 1609 }\r
0c18794e 1610 } else {\r
1611 //\r
1612 // Not found existing variable. Create a new variable.\r
0c18794e 1613 //\r
2d3fb919 1614\r
1615 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
1616 Status = EFI_SUCCESS;\r
0c18794e 1617 goto Done;\r
1618 }\r
2d3fb919 1619\r
0c18794e 1620 //\r
1621 // Make sure we are trying to create a new variable.\r
2d3fb919 1622 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
0c18794e 1623 //\r
1624 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1625 Status = EFI_NOT_FOUND;\r
1626 goto Done;\r
1627 }\r
2d3fb919 1628\r
0c18794e 1629 //\r
1630 // Only variable have NV|RT attribute can be created in Runtime.\r
1631 //\r
1632 if (AtRuntime () &&\r
1633 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
1634 Status = EFI_INVALID_PARAMETER;\r
1635 goto Done;\r
2d3fb919 1636 }\r
0c18794e 1637 }\r
1638\r
1639 //\r
1640 // Function part - create a new variable and copy the data.\r
1641 // Both update a variable and create a variable will come here.\r
1642\r
1643 SetMem (NextVariable, ScratchSize, 0xff);\r
1644\r
1645 NextVariable->StartId = VARIABLE_DATA;\r
1646 //\r
1647 // NextVariable->State = VAR_ADDED;\r
1648 //\r
1649 NextVariable->Reserved = 0;\r
1650 NextVariable->PubKeyIndex = KeyIndex;\r
1651 NextVariable->MonotonicCount = MonotonicCount;\r
2d3fb919 1652 ZeroMem (&NextVariable->TimeStamp, sizeof (EFI_TIME));\r
0c18794e 1653\r
3b4151bc 1654 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2d3fb919 1655 (TimeStamp != NULL)) {\r
3b4151bc 1656 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
1657 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
1658 } else {\r
0c18794e 1659 //\r
1660 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
1661 // when the new TimeStamp value is later than the current timestamp associated\r
1662 // with the variable, we need associate the new timestamp with the updated value.\r
1663 //\r
2d3fb919 1664 if (Variable->CurrPtr != NULL) {\r
1665 if (CompareTimeStamp (&Variable->CurrPtr->TimeStamp, TimeStamp)) {\r
1666 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
1667 }\r
0c18794e 1668 }\r
3b4151bc 1669 }\r
0c18794e 1670 }\r
1671\r
1672 //\r
2d3fb919 1673 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
0c18794e 1674 // Attributes bitmask parameter of a GetVariable() call.\r
1675 //\r
1676 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2d3fb919 1677\r
0c18794e 1678 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1679 VarNameSize = StrSize (VariableName);\r
1680 CopyMem (\r
1681 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1682 VariableName,\r
1683 VarNameSize\r
1684 );\r
1685 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1686 CopyMem (\r
1687 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1688 Data,\r
1689 DataSize\r
1690 );\r
1691 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1692 //\r
1693 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1694 // NextVariable->DataSize should not include pad size so that variable\r
1695 // service can get actual size in GetVariable.\r
1696 //\r
1697 NextVariable->NameSize = (UINT32)VarNameSize;\r
1698 NextVariable->DataSize = (UINT32)DataSize;\r
1699\r
1700 //\r
1701 // The actual size of the variable that stores in storage should\r
1702 // include pad size.\r
1703 //\r
1704 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
1705 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
1706 //\r
1707 // Create a nonvolatile variable.\r
1708 //\r
1709 Volatile = FALSE;\r
1710 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
2d3fb919 1711 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
0c18794e 1712 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2d3fb919 1713 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)\r
0c18794e 1714 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
1715 if (AtRuntime ()) {\r
1716 Status = EFI_OUT_OF_RESOURCES;\r
1717 goto Done;\r
1718 }\r
1719 //\r
1720 // Perform garbage collection & reclaim operation.\r
1721 //\r
2d3fb919 1722 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
0c18794e 1723 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);\r
1724 if (EFI_ERROR (Status)) {\r
1725 goto Done;\r
1726 }\r
1727 //\r
1728 // If still no enough space, return out of resources.\r
1729 //\r
2d3fb919 1730 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
0c18794e 1731 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2d3fb919 1732 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)\r
0c18794e 1733 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
1734 Status = EFI_OUT_OF_RESOURCES;\r
1735 goto Done;\r
1736 }\r
1737 Reclaimed = TRUE;\r
1738 }\r
1739 //\r
1740 // Four steps\r
1741 // 1. Write variable header\r
2d3fb919 1742 // 2. Set variable state to header valid\r
0c18794e 1743 // 3. Write variable data\r
1744 // 4. Set variable state to valid\r
1745 //\r
1746 //\r
1747 // Step 1:\r
1748 //\r
1749 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
1750 Status = UpdateVariableStore (\r
1751 &mVariableModuleGlobal->VariableGlobal,\r
1752 FALSE,\r
1753 TRUE,\r
1754 Fvb,\r
1755 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
1756 sizeof (VARIABLE_HEADER),\r
1757 (UINT8 *) NextVariable\r
1758 );\r
1759\r
1760 if (EFI_ERROR (Status)) {\r
1761 goto Done;\r
1762 }\r
1763\r
1764 //\r
1765 // Step 2:\r
1766 //\r
1767 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1768 Status = UpdateVariableStore (\r
1769 &mVariableModuleGlobal->VariableGlobal,\r
1770 FALSE,\r
1771 TRUE,\r
1772 Fvb,\r
1773 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1774 sizeof (UINT8),\r
1775 &NextVariable->State\r
1776 );\r
1777\r
1778 if (EFI_ERROR (Status)) {\r
1779 goto Done;\r
1780 }\r
1781 //\r
1782 // Step 3:\r
1783 //\r
1784 Status = UpdateVariableStore (\r
1785 &mVariableModuleGlobal->VariableGlobal,\r
1786 FALSE,\r
1787 TRUE,\r
1788 Fvb,\r
1789 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
1790 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1791 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1792 );\r
1793\r
1794 if (EFI_ERROR (Status)) {\r
1795 goto Done;\r
1796 }\r
1797 //\r
1798 // Step 4:\r
1799 //\r
1800 NextVariable->State = VAR_ADDED;\r
1801 Status = UpdateVariableStore (\r
1802 &mVariableModuleGlobal->VariableGlobal,\r
1803 FALSE,\r
1804 TRUE,\r
1805 Fvb,\r
1806 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1807 sizeof (UINT8),\r
1808 &NextVariable->State\r
1809 );\r
1810\r
1811 if (EFI_ERROR (Status)) {\r
1812 goto Done;\r
1813 }\r
1814\r
1815 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
1816\r
1817 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1818 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
1819 } else {\r
1820 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
1821 }\r
1822 //\r
1823 // update the memory copy of Flash region.\r
1824 //\r
1825 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
1826 } else {\r
1827 //\r
1828 // Create a volatile variable.\r
2d3fb919 1829 //\r
0c18794e 1830 Volatile = TRUE;\r
1831\r
1832 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
1833 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
1834 //\r
1835 // Perform garbage collection & reclaim operation.\r
1836 //\r
2d3fb919 1837 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
0c18794e 1838 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);\r
1839 if (EFI_ERROR (Status)) {\r
1840 goto Done;\r
1841 }\r
1842 //\r
1843 // If still no enough space, return out of resources.\r
1844 //\r
1845 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
1846 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
1847 ) {\r
1848 Status = EFI_OUT_OF_RESOURCES;\r
1849 goto Done;\r
1850 }\r
1851 Reclaimed = TRUE;\r
1852 }\r
1853\r
1854 NextVariable->State = VAR_ADDED;\r
1855 Status = UpdateVariableStore (\r
1856 &mVariableModuleGlobal->VariableGlobal,\r
1857 TRUE,\r
1858 TRUE,\r
1859 Fvb,\r
1860 mVariableModuleGlobal->VolatileLastVariableOffset,\r
1861 (UINT32) VarSize,\r
1862 (UINT8 *) NextVariable\r
1863 );\r
1864\r
1865 if (EFI_ERROR (Status)) {\r
1866 goto Done;\r
1867 }\r
1868\r
1869 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
1870 }\r
1871\r
1872 //\r
1873 // Mark the old variable as deleted.\r
1874 //\r
1875 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1876 State = Variable->CurrPtr->State;\r
1877 State &= VAR_DELETED;\r
1878\r
1879 Status = UpdateVariableStore (\r
1880 &mVariableModuleGlobal->VariableGlobal,\r
1881 Variable->Volatile,\r
1882 FALSE,\r
1883 Fvb,\r
1884 (UINTN) &Variable->CurrPtr->State,\r
1885 sizeof (UINT8),\r
1886 &State\r
1887 );\r
2d3fb919 1888 if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
0c18794e 1889 CacheVariable->CurrPtr->State = State;\r
1890 }\r
1891 }\r
1892\r
1893 if (!EFI_ERROR (Status)) {\r
1894 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
1895 }\r
1896\r
1897Done:\r
1898 return Status;\r
1899}\r
1900\r
a5f15e30
SZ
1901/**\r
1902 Check if a Unicode character is a hexadecimal character.\r
1903\r
1904 This function checks if a Unicode character is a \r
1905 hexadecimal character. The valid hexadecimal character is \r
1906 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
1907\r
1908\r
1909 @param Char The character to check against.\r
1910\r
1911 @retval TRUE If the Char is a hexadecmial character.\r
1912 @retval FALSE If the Char is not a hexadecmial character.\r
1913\r
1914**/\r
1915BOOLEAN\r
1916EFIAPI\r
1917IsHexaDecimalDigitCharacter (\r
1918 IN CHAR16 Char\r
1919 )\r
1920{\r
1921 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
1922}\r
1923\r
1924/**\r
1925\r
1926 This code checks if variable is hardware error record variable or not.\r
1927\r
1928 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
1929 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
1930\r
1931 @param VariableName Pointer to variable name.\r
1932 @param VendorGuid Variable Vendor Guid.\r
1933\r
1934 @retval TRUE Variable is hardware error record variable.\r
1935 @retval FALSE Variable is not hardware error record variable.\r
1936\r
1937**/\r
1938BOOLEAN\r
1939EFIAPI\r
1940IsHwErrRecVariable (\r
1941 IN CHAR16 *VariableName,\r
1942 IN EFI_GUID *VendorGuid\r
1943 )\r
1944{\r
1945 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
1946 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
1947 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
1948 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
1949 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
1950 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
1951 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
1952 return FALSE;\r
1953 }\r
1954\r
1955 return TRUE;\r
1956}\r
1957\r
0c18794e 1958/**\r
1959\r
1960 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
1961\r
dc204d5a
JY
1962 Caution: This function may receive untrusted input.\r
1963 This function may be invoked in SMM mode, and datasize is external input.\r
1964 This function will do basic validation, before parse the data.\r
1965\r
0c18794e 1966 @param VariableName Name of Variable to be found.\r
1967 @param VendorGuid Variable vendor GUID.\r
1968 @param Attributes Attribute value of the variable found.\r
1969 @param DataSize Size of Data found. If size is less than the\r
1970 data, this value contains the required size.\r
1971 @param Data Data pointer.\r
2d3fb919 1972\r
0c18794e 1973 @return EFI_INVALID_PARAMETER Invalid parameter.\r
1974 @return EFI_SUCCESS Find the specified variable.\r
1975 @return EFI_NOT_FOUND Not found.\r
1976 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
1977\r
1978**/\r
1979EFI_STATUS\r
1980EFIAPI\r
1981VariableServiceGetVariable (\r
1982 IN CHAR16 *VariableName,\r
1983 IN EFI_GUID *VendorGuid,\r
1984 OUT UINT32 *Attributes OPTIONAL,\r
1985 IN OUT UINTN *DataSize,\r
1986 OUT VOID *Data\r
1987 )\r
1988{\r
1989 EFI_STATUS Status;\r
1990 VARIABLE_POINTER_TRACK Variable;\r
1991 UINTN VarDataSize;\r
1992\r
1993 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1994 return EFI_INVALID_PARAMETER;\r
1995 }\r
1996\r
1997 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2d3fb919 1998\r
ecc722ad 1999 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2000 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2001 goto Done;\r
2002 }\r
2003\r
2004 //\r
2005 // Get data size\r
2006 //\r
2007 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2008 ASSERT (VarDataSize != 0);\r
2009\r
2010 if (*DataSize >= VarDataSize) {\r
2011 if (Data == NULL) {\r
2012 Status = EFI_INVALID_PARAMETER;\r
2013 goto Done;\r
2014 }\r
2015\r
2016 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2017 if (Attributes != NULL) {\r
2018 *Attributes = Variable.CurrPtr->Attributes;\r
2019 }\r
2020\r
2021 *DataSize = VarDataSize;\r
2022 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2d3fb919 2023\r
0c18794e 2024 Status = EFI_SUCCESS;\r
2025 goto Done;\r
2026 } else {\r
2027 *DataSize = VarDataSize;\r
2028 Status = EFI_BUFFER_TOO_SMALL;\r
2029 goto Done;\r
2030 }\r
2031\r
2032Done:\r
2033 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2034 return Status;\r
2035}\r
2036\r
2037\r
2038\r
2039/**\r
2040\r
2041 This code Finds the Next available variable.\r
2042\r
dc204d5a
JY
2043 Caution: This function may receive untrusted input.\r
2044 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2045\r
0c18794e 2046 @param VariableNameSize Size of the variable name.\r
2047 @param VariableName Pointer to variable name.\r
2048 @param VendorGuid Variable Vendor Guid.\r
2049\r
2050 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2051 @return EFI_SUCCESS Find the specified variable.\r
2052 @return EFI_NOT_FOUND Not found.\r
2053 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2054\r
2055**/\r
2056EFI_STATUS\r
2057EFIAPI\r
2058VariableServiceGetNextVariableName (\r
2059 IN OUT UINTN *VariableNameSize,\r
2060 IN OUT CHAR16 *VariableName,\r
2061 IN OUT EFI_GUID *VendorGuid\r
2062 )\r
2063{\r
9a000b46 2064 VARIABLE_STORE_TYPE Type;\r
0c18794e 2065 VARIABLE_POINTER_TRACK Variable;\r
9a000b46 2066 VARIABLE_POINTER_TRACK VariableInHob;\r
0c18794e 2067 UINTN VarNameSize;\r
2068 EFI_STATUS Status;\r
9a000b46 2069 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
0c18794e 2070\r
2071 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2072 return EFI_INVALID_PARAMETER;\r
2073 }\r
2074\r
2075 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2076\r
ecc722ad 2077 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2078 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2079 goto Done;\r
2080 }\r
2081\r
2082 if (VariableName[0] != 0) {\r
2083 //\r
2084 // If variable name is not NULL, get next variable.\r
2085 //\r
2086 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2087 }\r
2088\r
9a000b46
RN
2089 //\r
2090 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2091 // The index and attributes mapping must be kept in this order as FindVariable\r
2092 // makes use of this mapping to implement search algorithm.\r
2093 //\r
2094 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2095 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2096 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2097\r
0c18794e 2098 while (TRUE) {\r
2099 //\r
9a000b46 2100 // Switch from Volatile to HOB, to Non-Volatile.\r
0c18794e 2101 //\r
9a000b46
RN
2102 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
2103 (Variable.CurrPtr == NULL) ||\r
2104 !IsValidVariableHeader (Variable.CurrPtr)\r
2105 ) {\r
2106 //\r
2107 // Find current storage index\r
2108 //\r
2109 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2110 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2111 break;\r
2112 }\r
2113 }\r
2114 ASSERT (Type < VariableStoreTypeMax);\r
2115 //\r
2116 // Switch to next storage\r
2117 //\r
2118 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2119 if (VariableStoreHeader[Type] != NULL) {\r
2120 break;\r
2121 }\r
2122 }\r
2123 //\r
2d3fb919 2124 // Capture the case that\r
9a000b46
RN
2125 // 1. current storage is the last one, or\r
2126 // 2. no further storage\r
2127 //\r
2128 if (Type == VariableStoreTypeMax) {\r
0c18794e 2129 Status = EFI_NOT_FOUND;\r
2130 goto Done;\r
2131 }\r
9a000b46
RN
2132 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2133 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2134 Variable.CurrPtr = Variable.StartPtr;\r
0c18794e 2135 }\r
9a000b46 2136\r
0c18794e 2137 //\r
2138 // Variable is found\r
2139 //\r
9a000b46 2140 if (Variable.CurrPtr->State == VAR_ADDED) {\r
0c18794e 2141 if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {\r
9a000b46
RN
2142\r
2143 //\r
2144 // Don't return NV variable when HOB overrides it\r
2145 //\r
2d3fb919 2146 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
9a000b46
RN
2147 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2148 ) {\r
2149 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2150 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2151 Status = FindVariableEx (\r
2152 GetVariableNamePtr (Variable.CurrPtr),\r
2153 &Variable.CurrPtr->VendorGuid,\r
ecc722ad 2154 FALSE,\r
9a000b46
RN
2155 &VariableInHob\r
2156 );\r
2157 if (!EFI_ERROR (Status)) {\r
2158 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2159 continue;\r
2160 }\r
2161 }\r
2162\r
0c18794e 2163 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2164 ASSERT (VarNameSize != 0);\r
2165\r
2166 if (VarNameSize <= *VariableNameSize) {\r
9a000b46
RN
2167 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2168 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
0c18794e 2169 Status = EFI_SUCCESS;\r
2170 } else {\r
2171 Status = EFI_BUFFER_TOO_SMALL;\r
2172 }\r
2173\r
2174 *VariableNameSize = VarNameSize;\r
2175 goto Done;\r
2176 }\r
2177 }\r
2178\r
2179 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2180 }\r
2181\r
2182Done:\r
2183 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2184 return Status;\r
2185}\r
2186\r
2187/**\r
2188\r
2189 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2190\r
dc204d5a
JY
2191 Caution: This function may receive untrusted input.\r
2192 This function may be invoked in SMM mode, and datasize and data are external input.\r
2193 This function will do basic validation, before parse the data.\r
2194 This function will parse the authentication carefully to avoid security issues, like\r
2195 buffer overflow, integer overflow.\r
2196 This function will check attribute carefully to avoid authentication bypass.\r
2197\r
0c18794e 2198 @param VariableName Name of Variable to be found.\r
2199 @param VendorGuid Variable vendor GUID.\r
2200 @param Attributes Attribute value of the variable found\r
2201 @param DataSize Size of Data found. If size is less than the\r
2202 data, this value contains the required size.\r
2203 @param Data Data pointer.\r
2204\r
2205 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2206 @return EFI_SUCCESS Set successfully.\r
2207 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2208 @return EFI_NOT_FOUND Not found.\r
2209 @return EFI_WRITE_PROTECTED Variable is read-only.\r
2210\r
2211**/\r
2212EFI_STATUS\r
2213EFIAPI\r
2214VariableServiceSetVariable (\r
2215 IN CHAR16 *VariableName,\r
2216 IN EFI_GUID *VendorGuid,\r
2217 IN UINT32 Attributes,\r
2218 IN UINTN DataSize,\r
2219 IN VOID *Data\r
2220 )\r
2221{\r
2222 VARIABLE_POINTER_TRACK Variable;\r
2223 EFI_STATUS Status;\r
2224 VARIABLE_HEADER *NextVariable;\r
2225 EFI_PHYSICAL_ADDRESS Point;\r
2226 UINTN PayloadSize;\r
2227\r
2228 //\r
2229 // Check input parameters.\r
2230 //\r
2231 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2232 return EFI_INVALID_PARAMETER;\r
2d3fb919 2233 }\r
0c18794e 2234\r
2235 if (DataSize != 0 && Data == NULL) {\r
2236 return EFI_INVALID_PARAMETER;\r
2237 }\r
2238\r
2239 //\r
2240 // Make sure if runtime bit is set, boot service bit is set also.\r
2241 //\r
2242 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2243 return EFI_INVALID_PARAMETER;\r
2244 }\r
2245\r
2246 //\r
2d3fb919 2247 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
0c18794e 2248 // cannot be set both.\r
2249 //\r
2d3fb919 2250 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
0c18794e 2251 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
2252 return EFI_INVALID_PARAMETER;\r
2d3fb919 2253 }\r
0c18794e 2254\r
2255 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
2256 if (DataSize < AUTHINFO_SIZE) {\r
2257 //\r
2d3fb919 2258 // Try to write Authenticated Variable without AuthInfo.\r
0c18794e 2259 //\r
2260 return EFI_SECURITY_VIOLATION;\r
2d3fb919 2261 }\r
2262 PayloadSize = DataSize - AUTHINFO_SIZE;\r
2263 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
2264 //\r
2265 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
2266 //\r
2267 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
6bc4e19f 2268 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
2269 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
2d3fb919 2270 return EFI_SECURITY_VIOLATION;\r
2271 }\r
2272 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
0c18794e 2273 } else {\r
2d3fb919 2274 PayloadSize = DataSize;\r
0c18794e 2275 }\r
2d3fb919 2276\r
0c18794e 2277 //\r
2278 // The size of the VariableName, including the Unicode Null in bytes plus\r
2279 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2280 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
2281 //\r
2282 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2283 if ((PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r
2284 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r
2285 return EFI_INVALID_PARAMETER;\r
2286 }\r
a5f15e30 2287 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
0c18794e 2288 return EFI_INVALID_PARAMETER;\r
2289 }\r
2290 } else {\r
2291 //\r
2292 // The size of the VariableName, including the Unicode Null in bytes plus\r
2293 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
2294 //\r
2295 if ((PayloadSize > PcdGet32 (PcdMaxVariableSize)) ||\r
2296 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxVariableSize))) {\r
2297 return EFI_INVALID_PARAMETER;\r
2d3fb919 2298 }\r
2299 }\r
0c18794e 2300\r
021a1af9
SZ
2301 if (AtRuntime ()) {\r
2302 //\r
2303 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence\r
2304 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.\r
2305 //\r
2306 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {\r
2307 return EFI_WRITE_PROTECTED;\r
2308 }\r
2309 }\r
2310\r
0c18794e 2311 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2312\r
2313 //\r
2314 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
2315 //\r
2316 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
2317 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2318 //\r
2319 // Parse non-volatile variable data and get last variable offset.\r
2320 //\r
2321 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2d3fb919 2322 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))\r
0c18794e 2323 && IsValidVariableHeader (NextVariable)) {\r
2324 NextVariable = GetNextVariablePtr (NextVariable);\r
2325 }\r
2326 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2327 }\r
2328\r
2329 //\r
2330 // Check whether the input variable is already existed.\r
2331 //\r
ecc722ad 2332 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2333 if (!EFI_ERROR (Status)) {\r
2334 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
2335 return EFI_WRITE_PROTECTED;\r
2336 }\r
2337 }\r
2338 \r
0c18794e 2339 //\r
2340 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
2341 //\r
2342 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2343 //\r
2344 // Process PK, KEK, Sigdb seperately.\r
2345 //\r
2346 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
2347 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
2348 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
2349 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
ecc722ad 2350 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && \r
2351 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) {\r
0c18794e 2352 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
2353 } else {\r
2354 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
2355 }\r
2356\r
2357 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
2358 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2359\r
2360 return Status;\r
2361}\r
2362\r
2363/**\r
2364\r
2365 This code returns information about the EFI variables.\r
2366\r
dc204d5a
JY
2367 Caution: This function may receive untrusted input.\r
2368 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2369\r
0c18794e 2370 @param Attributes Attributes bitmask to specify the type of variables\r
2371 on which to return information.\r
2372 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2373 for the EFI variables associated with the attributes specified.\r
2374 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2375 for EFI variables associated with the attributes specified.\r
2376 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2377 associated with the attributes specified.\r
2378\r
2379 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2380 @return EFI_SUCCESS Query successfully.\r
2381 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
2382\r
2383**/\r
2384EFI_STATUS\r
2385EFIAPI\r
2386VariableServiceQueryVariableInfo (\r
2387 IN UINT32 Attributes,\r
2388 OUT UINT64 *MaximumVariableStorageSize,\r
2389 OUT UINT64 *RemainingVariableStorageSize,\r
2390 OUT UINT64 *MaximumVariableSize\r
2391 )\r
2392{\r
2393 VARIABLE_HEADER *Variable;\r
2394 VARIABLE_HEADER *NextVariable;\r
2395 UINT64 VariableSize;\r
2396 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2397 UINT64 CommonVariableTotalSize;\r
2398 UINT64 HwErrVariableTotalSize;\r
2399\r
2400 CommonVariableTotalSize = 0;\r
2401 HwErrVariableTotalSize = 0;\r
2402\r
2403 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
2404 return EFI_INVALID_PARAMETER;\r
2405 }\r
2406\r
2407 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
2408 //\r
2409 // Make sure the Attributes combination is supported by the platform.\r
2410 //\r
2d3fb919 2411 return EFI_UNSUPPORTED;\r
0c18794e 2412 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2413 //\r
2414 // Make sure if runtime bit is set, boot service bit is set also.\r
2415 //\r
2416 return EFI_INVALID_PARAMETER;\r
2417 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
2418 //\r
2419 // Make sure RT Attribute is set if we are in Runtime phase.\r
2420 //\r
2421 return EFI_INVALID_PARAMETER;\r
2422 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2423 //\r
2424 // Make sure Hw Attribute is set with NV.\r
2425 //\r
2426 return EFI_INVALID_PARAMETER;\r
2427 }\r
2428\r
2429 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2430\r
2431 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2432 //\r
2433 // Query is Volatile related.\r
2434 //\r
2435 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
2436 } else {\r
2437 //\r
2438 // Query is Non-Volatile related.\r
2439 //\r
2440 VariableStoreHeader = mNvVariableCache;\r
2441 }\r
2442\r
2443 //\r
2444 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2445 // with the storage size (excluding the storage header size).\r
2446 //\r
2447 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
2448\r
2449 //\r
2450 // Harware error record variable needs larger size.\r
2451 //\r
2452 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2453 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2454 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2455 } else {\r
2456 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2457 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2458 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2459 }\r
2460\r
2461 //\r
2462 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2463 //\r
2464 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
2465 }\r
2466\r
2467 //\r
2468 // Point to the starting address of the variables.\r
2469 //\r
2470 Variable = GetStartPointer (VariableStoreHeader);\r
2471\r
2472 //\r
2473 // Now walk through the related variable store.\r
2474 //\r
2475 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
2476 NextVariable = GetNextVariablePtr (Variable);\r
2477 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2478\r
2479 if (AtRuntime ()) {\r
2480 //\r
2481 // We don't take the state of the variables in mind\r
2482 // when calculating RemainingVariableStorageSize,\r
2483 // since the space occupied by variables not marked with\r
2484 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2485 //\r
2486 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2487 HwErrVariableTotalSize += VariableSize;\r
2488 } else {\r
2489 CommonVariableTotalSize += VariableSize;\r
2490 }\r
2491 } else {\r
2492 //\r
2493 // Only care about Variables with State VAR_ADDED, because\r
2494 // the space not marked as VAR_ADDED is reclaimable now.\r
2495 //\r
2496 if (Variable->State == VAR_ADDED) {\r
2497 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2498 HwErrVariableTotalSize += VariableSize;\r
2499 } else {\r
2500 CommonVariableTotalSize += VariableSize;\r
2501 }\r
2502 }\r
2503 }\r
2504\r
2505 //\r
2506 // Go to the next one.\r
2507 //\r
2508 Variable = NextVariable;\r
2509 }\r
2510\r
2511 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2512 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2513 }else {\r
2514 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2515 }\r
2516\r
2517 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2518 *MaximumVariableSize = 0;\r
2519 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2520 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2521 }\r
2522\r
2523 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2524 return EFI_SUCCESS;\r
2525}\r
2526\r
2527\r
2528/**\r
2529 This function reclaims variable storage if free size is below the threshold.\r
2d3fb919 2530\r
876ac395 2531 Caution: This function may be invoked at SMM mode.\r
2532 Care must be taken to make sure not security issue.\r
dc204d5a 2533\r
0c18794e 2534**/\r
2535VOID\r
2536ReclaimForOS(\r
2537 VOID\r
2538 )\r
2539{\r
2540 EFI_STATUS Status;\r
2541 UINTN CommonVariableSpace;\r
2542 UINTN RemainingCommonVariableSpace;\r
2543 UINTN RemainingHwErrVariableSpace;\r
2544\r
2d3fb919 2545 Status = EFI_SUCCESS;\r
0c18794e 2546\r
2547 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2548\r
2549 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2550\r
2551 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
2552 //\r
2553 // Check if the free area is blow a threshold.\r
2554 //\r
2555 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
2d3fb919 2556 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
0c18794e 2557 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
2558 Status = Reclaim (\r
2559 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2560 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2561 FALSE,\r
2562 NULL\r
2563 );\r
2564 ASSERT_EFI_ERROR (Status);\r
2565 }\r
2566}\r
2567\r
2568\r
2569/**\r
2570 Initializes variable write service after FVB was ready.\r
2571\r
2572 @retval EFI_SUCCESS Function successfully executed.\r
2573 @retval Others Fail to initialize the variable service.\r
2574\r
2575**/\r
2576EFI_STATUS\r
2577VariableWriteServiceInitialize (\r
2578 VOID\r
2579 )\r
2580{\r
2581 EFI_STATUS Status;\r
2582 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2583 UINTN Index;\r
2584 UINT8 Data;\r
2585 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
9a000b46
RN
2586 VARIABLE_HEADER *Variable;\r
2587 VOID *VariableData;\r
0c18794e 2588\r
2589 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2590 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2d3fb919 2591\r
0c18794e 2592 //\r
2593 // Check if the free area is really free.\r
2594 //\r
9a000b46 2595 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
0c18794e 2596 Data = ((UINT8 *) mNvVariableCache)[Index];\r
2597 if (Data != 0xff) {\r
2598 //\r
2599 // There must be something wrong in variable store, do reclaim operation.\r
2600 //\r
2601 Status = Reclaim (\r
2602 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2603 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2604 FALSE,\r
2605 NULL\r
2606 );\r
2607 if (EFI_ERROR (Status)) {\r
2608 return Status;\r
2609 }\r
2610 break;\r
2611 }\r
2612 }\r
2613\r
2d3fb919 2614\r
9a000b46
RN
2615 //\r
2616 // Flush the HOB variable to flash and invalidate HOB variable.\r
2617 //\r
2618 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
2619 //\r
2620 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB\r
2621 //\r
2622 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2623 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
2624\r
2625 for ( Variable = GetStartPointer (VariableStoreHeader)\r
2626 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
2627 ; Variable = GetNextVariablePtr (Variable)\r
2628 ) {\r
2629 ASSERT (Variable->State == VAR_ADDED);\r
2630 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
2631 VariableData = GetVariableDataPtr (Variable);\r
2632 Status = VariableServiceSetVariable (\r
2633 GetVariableNamePtr (Variable),\r
2634 &Variable->VendorGuid,\r
2635 Variable->Attributes,\r
2636 Variable->DataSize,\r
2637 VariableData\r
2638 );\r
2639 ASSERT_EFI_ERROR (Status);\r
2640 }\r
2641 }\r
2642\r
0c18794e 2643 //\r
2644 // Authenticated variable initialize.\r
2645 //\r
2646 Status = AutenticatedVariableServiceInitialize ();\r
2647\r
2648 return Status;\r
2649}\r
2650\r
2651\r
2652/**\r
2653 Initializes variable store area for non-volatile and volatile variable.\r
2654\r
2655 @retval EFI_SUCCESS Function successfully executed.\r
2656 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2657\r
2658**/\r
2659EFI_STATUS\r
2660VariableCommonInitialize (\r
2661 VOID\r
2662 )\r
2663{\r
2664 EFI_STATUS Status;\r
2665 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2666 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2667 VARIABLE_HEADER *NextVariable;\r
2668 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
2669 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
2670 UINT64 VariableStoreLength;\r
2671 UINTN ScratchSize;\r
2672 UINTN VariableSize;\r
9a000b46 2673 EFI_HOB_GUID_TYPE *GuidHob;\r
0c18794e 2674\r
2675 //\r
2676 // Allocate runtime memory for variable driver global structure.\r
2677 //\r
2678 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
2679 if (mVariableModuleGlobal == NULL) {\r
2680 return EFI_OUT_OF_RESOURCES;\r
2681 }\r
2682\r
2683 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
2684\r
2685 //\r
2686 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2687 // is stored with common variable in the same NV region. So the platform integrator should\r
2d3fb919 2688 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of\r
0c18794e 2689 // PcdFlashNvStorageVariableSize.\r
2690 //\r
2691 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
2692\r
9a000b46
RN
2693 //\r
2694 // Get HOB variable store.\r
2695 //\r
2696 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
2697 if (GuidHob != NULL) {\r
2698 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
2699 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
2700 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
2701 } else {\r
2702 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
2703 }\r
2704 }\r
2705\r
0c18794e 2706 //\r
2707 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
2708 //\r
2709 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2710 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
2711 if (VolatileVariableStore == NULL) {\r
2712 FreePool (mVariableModuleGlobal);\r
2713 return EFI_OUT_OF_RESOURCES;\r
2714 }\r
2715\r
2716 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
2717\r
2718 //\r
2719 // Initialize Variable Specific Data.\r
2720 //\r
2721 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
2722 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
2723 mVariableModuleGlobal->FvbInstance = NULL;\r
2724\r
2725 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
2726 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
2727 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
2728 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
2729 VolatileVariableStore->Reserved = 0;\r
2730 VolatileVariableStore->Reserved1 = 0;\r
2731\r
2732 //\r
9a000b46 2733 // Get non-volatile variable store.\r
0c18794e 2734 //\r
2735\r
2736 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
2737 if (TempVariableStoreHeader == 0) {\r
2738 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
2739 }\r
4d832aab 2740 \r
2741 //\r
2742 // Check if the Firmware Volume is not corrupted\r
2743 //\r
2744 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||\r
2745 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {\r
2746 Status = EFI_VOLUME_CORRUPTED;\r
2747 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
2748 goto Done;\r
2749 }\r
2750\r
0c18794e 2751 VariableStoreBase = TempVariableStoreHeader + \\r
2752 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
2753 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
2754 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
2755\r
2756 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
2757 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2758 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
2759 Status = EFI_VOLUME_CORRUPTED;\r
2760 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
2761 goto Done;\r
2d3fb919 2762 }\r
0c18794e 2763 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
2d3fb919 2764\r
0c18794e 2765 //\r
2766 // Parse non-volatile variable data and get last variable offset.\r
2767 //\r
2768 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
2769 while (IsValidVariableHeader (NextVariable)) {\r
2770 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
2771 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2772 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2773 } else {\r
2774 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2775 }\r
2776\r
2777 NextVariable = GetNextVariablePtr (NextVariable);\r
2778 }\r
2779\r
2780 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
2d3fb919 2781\r
0c18794e 2782 //\r
2783 // Allocate runtime memory used for a memory copy of the FLASH region.\r
2784 // Keep the memory and the FLASH in sync as updates occur\r
2785 //\r
2786 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);\r
2787 if (mNvVariableCache == NULL) {\r
2788 Status = EFI_OUT_OF_RESOURCES;\r
2789 goto Done;\r
2790 }\r
2791 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);\r
2792 Status = EFI_SUCCESS;\r
2793\r
2794Done:\r
2795 if (EFI_ERROR (Status)) {\r
2796 FreePool (mVariableModuleGlobal);\r
2797 FreePool (VolatileVariableStore);\r
2798 }\r
2799\r
2800 return Status;\r
2801}\r
2802\r
2803\r
2804/**\r
2805 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
2806\r
2807 @param[in] Address The Flash address.\r
2808 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
2809 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
2810\r
2811**/\r
2812EFI_STATUS\r
2813GetFvbInfoByAddress (\r
2814 IN EFI_PHYSICAL_ADDRESS Address,\r
2815 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
2816 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
2817 )\r
2818{\r
2819 EFI_STATUS Status;\r
2820 EFI_HANDLE *HandleBuffer;\r
2821 UINTN HandleCount;\r
2822 UINTN Index;\r
2823 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
2824 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
2825 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
2826 EFI_FVB_ATTRIBUTES_2 Attributes;\r
2d3fb919 2827\r
0c18794e 2828 //\r
2829 // Get all FVB handles.\r
2830 //\r
2831 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
2832 if (EFI_ERROR (Status)) {\r
2833 return EFI_NOT_FOUND;\r
2834 }\r
2835\r
2836 //\r
2837 // Get the FVB to access variable store.\r
2838 //\r
2839 Fvb = NULL;\r
2840 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
2841 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
2842 if (EFI_ERROR (Status)) {\r
2843 Status = EFI_NOT_FOUND;\r
2844 break;\r
2845 }\r
2846\r
2847 //\r
2848 // Ensure this FVB protocol supported Write operation.\r
2849 //\r
2850 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
2851 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2d3fb919 2852 continue;\r
0c18794e 2853 }\r
2d3fb919 2854\r
0c18794e 2855 //\r
2856 // Compare the address and select the right one.\r
2857 //\r
2858 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
2859 if (EFI_ERROR (Status)) {\r
2860 continue;\r
2861 }\r
2862\r
2863 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
2864 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
2865 if (FvbHandle != NULL) {\r
2866 *FvbHandle = HandleBuffer[Index];\r
2867 }\r
2868 if (FvbProtocol != NULL) {\r
2869 *FvbProtocol = Fvb;\r
2870 }\r
2871 Status = EFI_SUCCESS;\r
2872 break;\r
2873 }\r
2874 }\r
2875 FreePool (HandleBuffer);\r
2876\r
2877 if (Fvb == NULL) {\r
2878 Status = EFI_NOT_FOUND;\r
2879 }\r
2d3fb919 2880\r
2881 return Status;\r
0c18794e 2882}\r
2883\r