]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
Remove Variable PPI dependency from S3Resume module, check return status of locating...
[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
05a643f9 1958/**\r
1959 This code checks if variable should be treated as read-only variable.\r
1960\r
1961 @param[in] VariableName Name of the Variable.\r
1962 @param[in] VendorGuid GUID of the Variable.\r
1963\r
1964 @retval TRUE This variable is read-only variable.\r
1965 @retval FALSE This variable is NOT read-only variable.\r
1966 \r
1967**/\r
1968BOOLEAN\r
1969IsReadOnlyVariable (\r
1970 IN CHAR16 *VariableName,\r
1971 IN EFI_GUID *VendorGuid\r
1972 )\r
1973{\r
1974 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {\r
1975 if ((StrCmp (VariableName, EFI_SETUP_MODE_NAME) == 0) ||\r
1976 (StrCmp (VariableName, EFI_SIGNATURE_SUPPORT_NAME) == 0) ||\r
1977 (StrCmp (VariableName, EFI_SECURE_BOOT_MODE_NAME) == 0)) {\r
1978 return TRUE;\r
1979 }\r
1980 }\r
1981 \r
1982 return FALSE;\r
1983}\r
1984\r
0c18794e 1985/**\r
1986\r
1987 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
1988\r
dc204d5a
JY
1989 Caution: This function may receive untrusted input.\r
1990 This function may be invoked in SMM mode, and datasize is external input.\r
1991 This function will do basic validation, before parse the data.\r
1992\r
0c18794e 1993 @param VariableName Name of Variable to be found.\r
1994 @param VendorGuid Variable vendor GUID.\r
1995 @param Attributes Attribute value of the variable found.\r
1996 @param DataSize Size of Data found. If size is less than the\r
1997 data, this value contains the required size.\r
1998 @param Data Data pointer.\r
2d3fb919 1999\r
0c18794e 2000 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2001 @return EFI_SUCCESS Find the specified variable.\r
2002 @return EFI_NOT_FOUND Not found.\r
2003 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2004\r
2005**/\r
2006EFI_STATUS\r
2007EFIAPI\r
2008VariableServiceGetVariable (\r
2009 IN CHAR16 *VariableName,\r
2010 IN EFI_GUID *VendorGuid,\r
2011 OUT UINT32 *Attributes OPTIONAL,\r
2012 IN OUT UINTN *DataSize,\r
2013 OUT VOID *Data\r
2014 )\r
2015{\r
2016 EFI_STATUS Status;\r
2017 VARIABLE_POINTER_TRACK Variable;\r
2018 UINTN VarDataSize;\r
2019\r
2020 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2021 return EFI_INVALID_PARAMETER;\r
2022 }\r
2023\r
2024 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2d3fb919 2025\r
ecc722ad 2026 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2027 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2028 goto Done;\r
2029 }\r
2030\r
2031 //\r
2032 // Get data size\r
2033 //\r
2034 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2035 ASSERT (VarDataSize != 0);\r
2036\r
2037 if (*DataSize >= VarDataSize) {\r
2038 if (Data == NULL) {\r
2039 Status = EFI_INVALID_PARAMETER;\r
2040 goto Done;\r
2041 }\r
2042\r
2043 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2044 if (Attributes != NULL) {\r
2045 *Attributes = Variable.CurrPtr->Attributes;\r
2046 }\r
2047\r
2048 *DataSize = VarDataSize;\r
2049 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2d3fb919 2050\r
0c18794e 2051 Status = EFI_SUCCESS;\r
2052 goto Done;\r
2053 } else {\r
2054 *DataSize = VarDataSize;\r
2055 Status = EFI_BUFFER_TOO_SMALL;\r
2056 goto Done;\r
2057 }\r
2058\r
2059Done:\r
2060 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2061 return Status;\r
2062}\r
2063\r
2064\r
2065\r
2066/**\r
2067\r
2068 This code Finds the Next available variable.\r
2069\r
dc204d5a
JY
2070 Caution: This function may receive untrusted input.\r
2071 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2072\r
0c18794e 2073 @param VariableNameSize Size of the variable name.\r
2074 @param VariableName Pointer to variable name.\r
2075 @param VendorGuid Variable Vendor Guid.\r
2076\r
2077 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2078 @return EFI_SUCCESS Find the specified variable.\r
2079 @return EFI_NOT_FOUND Not found.\r
2080 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2081\r
2082**/\r
2083EFI_STATUS\r
2084EFIAPI\r
2085VariableServiceGetNextVariableName (\r
2086 IN OUT UINTN *VariableNameSize,\r
2087 IN OUT CHAR16 *VariableName,\r
2088 IN OUT EFI_GUID *VendorGuid\r
2089 )\r
2090{\r
9a000b46 2091 VARIABLE_STORE_TYPE Type;\r
0c18794e 2092 VARIABLE_POINTER_TRACK Variable;\r
9a000b46 2093 VARIABLE_POINTER_TRACK VariableInHob;\r
0c18794e 2094 UINTN VarNameSize;\r
2095 EFI_STATUS Status;\r
9a000b46 2096 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
0c18794e 2097\r
2098 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2099 return EFI_INVALID_PARAMETER;\r
2100 }\r
2101\r
2102 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2103\r
ecc722ad 2104 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2105 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2106 goto Done;\r
2107 }\r
2108\r
2109 if (VariableName[0] != 0) {\r
2110 //\r
2111 // If variable name is not NULL, get next variable.\r
2112 //\r
2113 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2114 }\r
2115\r
9a000b46
RN
2116 //\r
2117 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2118 // The index and attributes mapping must be kept in this order as FindVariable\r
2119 // makes use of this mapping to implement search algorithm.\r
2120 //\r
2121 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2122 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2123 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2124\r
0c18794e 2125 while (TRUE) {\r
2126 //\r
9a000b46 2127 // Switch from Volatile to HOB, to Non-Volatile.\r
0c18794e 2128 //\r
9a000b46
RN
2129 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
2130 (Variable.CurrPtr == NULL) ||\r
2131 !IsValidVariableHeader (Variable.CurrPtr)\r
2132 ) {\r
2133 //\r
2134 // Find current storage index\r
2135 //\r
2136 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2137 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2138 break;\r
2139 }\r
2140 }\r
2141 ASSERT (Type < VariableStoreTypeMax);\r
2142 //\r
2143 // Switch to next storage\r
2144 //\r
2145 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2146 if (VariableStoreHeader[Type] != NULL) {\r
2147 break;\r
2148 }\r
2149 }\r
2150 //\r
2d3fb919 2151 // Capture the case that\r
9a000b46
RN
2152 // 1. current storage is the last one, or\r
2153 // 2. no further storage\r
2154 //\r
2155 if (Type == VariableStoreTypeMax) {\r
0c18794e 2156 Status = EFI_NOT_FOUND;\r
2157 goto Done;\r
2158 }\r
9a000b46
RN
2159 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2160 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2161 Variable.CurrPtr = Variable.StartPtr;\r
0c18794e 2162 }\r
9a000b46 2163\r
0c18794e 2164 //\r
2165 // Variable is found\r
2166 //\r
9a000b46 2167 if (Variable.CurrPtr->State == VAR_ADDED) {\r
0c18794e 2168 if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {\r
9a000b46
RN
2169\r
2170 //\r
2171 // Don't return NV variable when HOB overrides it\r
2172 //\r
2d3fb919 2173 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
9a000b46
RN
2174 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2175 ) {\r
2176 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2177 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2178 Status = FindVariableEx (\r
2179 GetVariableNamePtr (Variable.CurrPtr),\r
2180 &Variable.CurrPtr->VendorGuid,\r
ecc722ad 2181 FALSE,\r
9a000b46
RN
2182 &VariableInHob\r
2183 );\r
2184 if (!EFI_ERROR (Status)) {\r
2185 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2186 continue;\r
2187 }\r
2188 }\r
2189\r
0c18794e 2190 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2191 ASSERT (VarNameSize != 0);\r
2192\r
2193 if (VarNameSize <= *VariableNameSize) {\r
9a000b46
RN
2194 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2195 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
0c18794e 2196 Status = EFI_SUCCESS;\r
2197 } else {\r
2198 Status = EFI_BUFFER_TOO_SMALL;\r
2199 }\r
2200\r
2201 *VariableNameSize = VarNameSize;\r
2202 goto Done;\r
2203 }\r
2204 }\r
2205\r
2206 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2207 }\r
2208\r
2209Done:\r
2210 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2211 return Status;\r
2212}\r
2213\r
2214/**\r
2215\r
2216 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2217\r
dc204d5a
JY
2218 Caution: This function may receive untrusted input.\r
2219 This function may be invoked in SMM mode, and datasize and data are external input.\r
2220 This function will do basic validation, before parse the data.\r
2221 This function will parse the authentication carefully to avoid security issues, like\r
2222 buffer overflow, integer overflow.\r
2223 This function will check attribute carefully to avoid authentication bypass.\r
2224\r
0c18794e 2225 @param VariableName Name of Variable to be found.\r
2226 @param VendorGuid Variable vendor GUID.\r
2227 @param Attributes Attribute value of the variable found\r
2228 @param DataSize Size of Data found. If size is less than the\r
2229 data, this value contains the required size.\r
2230 @param Data Data pointer.\r
2231\r
2232 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2233 @return EFI_SUCCESS Set successfully.\r
2234 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2235 @return EFI_NOT_FOUND Not found.\r
2236 @return EFI_WRITE_PROTECTED Variable is read-only.\r
2237\r
2238**/\r
2239EFI_STATUS\r
2240EFIAPI\r
2241VariableServiceSetVariable (\r
2242 IN CHAR16 *VariableName,\r
2243 IN EFI_GUID *VendorGuid,\r
2244 IN UINT32 Attributes,\r
2245 IN UINTN DataSize,\r
2246 IN VOID *Data\r
2247 )\r
2248{\r
2249 VARIABLE_POINTER_TRACK Variable;\r
2250 EFI_STATUS Status;\r
2251 VARIABLE_HEADER *NextVariable;\r
2252 EFI_PHYSICAL_ADDRESS Point;\r
2253 UINTN PayloadSize;\r
2254\r
2255 //\r
2256 // Check input parameters.\r
2257 //\r
2258 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2259 return EFI_INVALID_PARAMETER;\r
2d3fb919 2260 }\r
0c18794e 2261\r
05a643f9 2262 if (IsReadOnlyVariable (VariableName, VendorGuid)) {\r
2263 return EFI_WRITE_PROTECTED;\r
2264 }\r
2265\r
0c18794e 2266 if (DataSize != 0 && Data == NULL) {\r
2267 return EFI_INVALID_PARAMETER;\r
2268 }\r
2269\r
2270 //\r
2271 // Make sure if runtime bit is set, boot service bit is set also.\r
2272 //\r
2273 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2274 return EFI_INVALID_PARAMETER;\r
2275 }\r
2276\r
2277 //\r
2d3fb919 2278 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
0c18794e 2279 // cannot be set both.\r
2280 //\r
2d3fb919 2281 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
0c18794e 2282 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
2283 return EFI_INVALID_PARAMETER;\r
2d3fb919 2284 }\r
0c18794e 2285\r
2286 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
2287 if (DataSize < AUTHINFO_SIZE) {\r
2288 //\r
2d3fb919 2289 // Try to write Authenticated Variable without AuthInfo.\r
0c18794e 2290 //\r
2291 return EFI_SECURITY_VIOLATION;\r
2d3fb919 2292 }\r
2293 PayloadSize = DataSize - AUTHINFO_SIZE;\r
2294 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
2295 //\r
2296 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
2297 //\r
2298 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
6bc4e19f 2299 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
2300 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
2d3fb919 2301 return EFI_SECURITY_VIOLATION;\r
2302 }\r
2303 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
0c18794e 2304 } else {\r
2d3fb919 2305 PayloadSize = DataSize;\r
0c18794e 2306 }\r
2d3fb919 2307\r
0c18794e 2308 //\r
2309 // The size of the VariableName, including the Unicode Null in bytes plus\r
2310 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2311 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
2312 //\r
2313 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2314 if ((PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r
2315 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r
2316 return EFI_INVALID_PARAMETER;\r
2317 }\r
a5f15e30 2318 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
0c18794e 2319 return EFI_INVALID_PARAMETER;\r
2320 }\r
2321 } else {\r
2322 //\r
2323 // The size of the VariableName, including the Unicode Null in bytes plus\r
2324 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
2325 //\r
2326 if ((PayloadSize > PcdGet32 (PcdMaxVariableSize)) ||\r
2327 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxVariableSize))) {\r
2328 return EFI_INVALID_PARAMETER;\r
2d3fb919 2329 }\r
2330 }\r
0c18794e 2331\r
021a1af9
SZ
2332 if (AtRuntime ()) {\r
2333 //\r
2334 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence\r
2335 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.\r
2336 //\r
2337 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {\r
2338 return EFI_WRITE_PROTECTED;\r
2339 }\r
2340 }\r
2341\r
0c18794e 2342 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2343\r
2344 //\r
2345 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
2346 //\r
2347 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
2348 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2349 //\r
2350 // Parse non-volatile variable data and get last variable offset.\r
2351 //\r
2352 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2d3fb919 2353 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))\r
0c18794e 2354 && IsValidVariableHeader (NextVariable)) {\r
2355 NextVariable = GetNextVariablePtr (NextVariable);\r
2356 }\r
2357 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2358 }\r
2359\r
2360 //\r
2361 // Check whether the input variable is already existed.\r
2362 //\r
ecc722ad 2363 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2364 if (!EFI_ERROR (Status)) {\r
2365 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
2366 return EFI_WRITE_PROTECTED;\r
2367 }\r
2368 }\r
2369 \r
0c18794e 2370 //\r
2371 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
2372 //\r
2373 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2374 //\r
2375 // Process PK, KEK, Sigdb seperately.\r
2376 //\r
2377 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
2378 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
2379 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
2380 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
ecc722ad 2381 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && \r
2382 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) {\r
05a643f9 2383 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
2384 if (EFI_ERROR (Status)) {\r
2385 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
2386 }\r
0c18794e 2387 } else {\r
2388 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
2389 }\r
2390\r
2391 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
2392 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2393\r
2394 return Status;\r
2395}\r
2396\r
2397/**\r
2398\r
2399 This code returns information about the EFI variables.\r
2400\r
dc204d5a
JY
2401 Caution: This function may receive untrusted input.\r
2402 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2403\r
0c18794e 2404 @param Attributes Attributes bitmask to specify the type of variables\r
2405 on which to return information.\r
2406 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2407 for the EFI variables associated with the attributes specified.\r
2408 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2409 for EFI variables associated with the attributes specified.\r
2410 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2411 associated with the attributes specified.\r
2412\r
2413 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2414 @return EFI_SUCCESS Query successfully.\r
2415 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
2416\r
2417**/\r
2418EFI_STATUS\r
2419EFIAPI\r
2420VariableServiceQueryVariableInfo (\r
2421 IN UINT32 Attributes,\r
2422 OUT UINT64 *MaximumVariableStorageSize,\r
2423 OUT UINT64 *RemainingVariableStorageSize,\r
2424 OUT UINT64 *MaximumVariableSize\r
2425 )\r
2426{\r
2427 VARIABLE_HEADER *Variable;\r
2428 VARIABLE_HEADER *NextVariable;\r
2429 UINT64 VariableSize;\r
2430 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2431 UINT64 CommonVariableTotalSize;\r
2432 UINT64 HwErrVariableTotalSize;\r
2433\r
2434 CommonVariableTotalSize = 0;\r
2435 HwErrVariableTotalSize = 0;\r
2436\r
2437 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
2438 return EFI_INVALID_PARAMETER;\r
2439 }\r
2440\r
2441 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
2442 //\r
2443 // Make sure the Attributes combination is supported by the platform.\r
2444 //\r
2d3fb919 2445 return EFI_UNSUPPORTED;\r
0c18794e 2446 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2447 //\r
2448 // Make sure if runtime bit is set, boot service bit is set also.\r
2449 //\r
2450 return EFI_INVALID_PARAMETER;\r
2451 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
2452 //\r
2453 // Make sure RT Attribute is set if we are in Runtime phase.\r
2454 //\r
2455 return EFI_INVALID_PARAMETER;\r
2456 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2457 //\r
2458 // Make sure Hw Attribute is set with NV.\r
2459 //\r
2460 return EFI_INVALID_PARAMETER;\r
2461 }\r
2462\r
2463 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2464\r
2465 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2466 //\r
2467 // Query is Volatile related.\r
2468 //\r
2469 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
2470 } else {\r
2471 //\r
2472 // Query is Non-Volatile related.\r
2473 //\r
2474 VariableStoreHeader = mNvVariableCache;\r
2475 }\r
2476\r
2477 //\r
2478 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2479 // with the storage size (excluding the storage header size).\r
2480 //\r
2481 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
2482\r
2483 //\r
2484 // Harware error record variable needs larger size.\r
2485 //\r
2486 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2487 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2488 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2489 } else {\r
2490 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2491 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2492 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2493 }\r
2494\r
2495 //\r
2496 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2497 //\r
2498 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
2499 }\r
2500\r
2501 //\r
2502 // Point to the starting address of the variables.\r
2503 //\r
2504 Variable = GetStartPointer (VariableStoreHeader);\r
2505\r
2506 //\r
2507 // Now walk through the related variable store.\r
2508 //\r
2509 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
2510 NextVariable = GetNextVariablePtr (Variable);\r
2511 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2512\r
2513 if (AtRuntime ()) {\r
2514 //\r
2515 // We don't take the state of the variables in mind\r
2516 // when calculating RemainingVariableStorageSize,\r
2517 // since the space occupied by variables not marked with\r
2518 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2519 //\r
2520 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2521 HwErrVariableTotalSize += VariableSize;\r
2522 } else {\r
2523 CommonVariableTotalSize += VariableSize;\r
2524 }\r
2525 } else {\r
2526 //\r
2527 // Only care about Variables with State VAR_ADDED, because\r
2528 // the space not marked as VAR_ADDED is reclaimable now.\r
2529 //\r
2530 if (Variable->State == VAR_ADDED) {\r
2531 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2532 HwErrVariableTotalSize += VariableSize;\r
2533 } else {\r
2534 CommonVariableTotalSize += VariableSize;\r
2535 }\r
2536 }\r
2537 }\r
2538\r
2539 //\r
2540 // Go to the next one.\r
2541 //\r
2542 Variable = NextVariable;\r
2543 }\r
2544\r
2545 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2546 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2547 }else {\r
2548 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2549 }\r
2550\r
2551 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2552 *MaximumVariableSize = 0;\r
2553 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2554 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2555 }\r
2556\r
2557 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2558 return EFI_SUCCESS;\r
2559}\r
2560\r
2561\r
2562/**\r
2563 This function reclaims variable storage if free size is below the threshold.\r
2d3fb919 2564\r
876ac395 2565 Caution: This function may be invoked at SMM mode.\r
2566 Care must be taken to make sure not security issue.\r
dc204d5a 2567\r
0c18794e 2568**/\r
2569VOID\r
2570ReclaimForOS(\r
2571 VOID\r
2572 )\r
2573{\r
2574 EFI_STATUS Status;\r
2575 UINTN CommonVariableSpace;\r
2576 UINTN RemainingCommonVariableSpace;\r
2577 UINTN RemainingHwErrVariableSpace;\r
2578\r
2d3fb919 2579 Status = EFI_SUCCESS;\r
0c18794e 2580\r
2581 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2582\r
2583 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2584\r
2585 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
2586 //\r
2587 // Check if the free area is blow a threshold.\r
2588 //\r
2589 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
2d3fb919 2590 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
0c18794e 2591 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
2592 Status = Reclaim (\r
2593 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2594 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2595 FALSE,\r
2596 NULL\r
2597 );\r
2598 ASSERT_EFI_ERROR (Status);\r
2599 }\r
2600}\r
2601\r
2602\r
2603/**\r
2604 Initializes variable write service after FVB was ready.\r
2605\r
2606 @retval EFI_SUCCESS Function successfully executed.\r
2607 @retval Others Fail to initialize the variable service.\r
2608\r
2609**/\r
2610EFI_STATUS\r
2611VariableWriteServiceInitialize (\r
2612 VOID\r
2613 )\r
2614{\r
2615 EFI_STATUS Status;\r
2616 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2617 UINTN Index;\r
2618 UINT8 Data;\r
2619 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
9a000b46
RN
2620 VARIABLE_HEADER *Variable;\r
2621 VOID *VariableData;\r
0c18794e 2622\r
2623 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2624 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2d3fb919 2625\r
0c18794e 2626 //\r
2627 // Check if the free area is really free.\r
2628 //\r
9a000b46 2629 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
0c18794e 2630 Data = ((UINT8 *) mNvVariableCache)[Index];\r
2631 if (Data != 0xff) {\r
2632 //\r
2633 // There must be something wrong in variable store, do reclaim operation.\r
2634 //\r
2635 Status = Reclaim (\r
2636 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2637 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2638 FALSE,\r
2639 NULL\r
2640 );\r
2641 if (EFI_ERROR (Status)) {\r
2642 return Status;\r
2643 }\r
2644 break;\r
2645 }\r
2646 }\r
2647\r
2d3fb919 2648\r
9a000b46
RN
2649 //\r
2650 // Flush the HOB variable to flash and invalidate HOB variable.\r
2651 //\r
2652 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
2653 //\r
2654 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB\r
2655 //\r
2656 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2657 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
2658\r
2659 for ( Variable = GetStartPointer (VariableStoreHeader)\r
2660 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
2661 ; Variable = GetNextVariablePtr (Variable)\r
2662 ) {\r
2663 ASSERT (Variable->State == VAR_ADDED);\r
2664 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
2665 VariableData = GetVariableDataPtr (Variable);\r
2666 Status = VariableServiceSetVariable (\r
2667 GetVariableNamePtr (Variable),\r
2668 &Variable->VendorGuid,\r
2669 Variable->Attributes,\r
2670 Variable->DataSize,\r
2671 VariableData\r
2672 );\r
2673 ASSERT_EFI_ERROR (Status);\r
2674 }\r
2675 }\r
2676\r
0c18794e 2677 //\r
2678 // Authenticated variable initialize.\r
2679 //\r
2680 Status = AutenticatedVariableServiceInitialize ();\r
2681\r
2682 return Status;\r
2683}\r
2684\r
2685\r
2686/**\r
2687 Initializes variable store area for non-volatile and volatile variable.\r
2688\r
2689 @retval EFI_SUCCESS Function successfully executed.\r
2690 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2691\r
2692**/\r
2693EFI_STATUS\r
2694VariableCommonInitialize (\r
2695 VOID\r
2696 )\r
2697{\r
2698 EFI_STATUS Status;\r
2699 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2700 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2701 VARIABLE_HEADER *NextVariable;\r
2702 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
2703 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
2704 UINT64 VariableStoreLength;\r
2705 UINTN ScratchSize;\r
2706 UINTN VariableSize;\r
9a000b46 2707 EFI_HOB_GUID_TYPE *GuidHob;\r
0c18794e 2708\r
2709 //\r
2710 // Allocate runtime memory for variable driver global structure.\r
2711 //\r
2712 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
2713 if (mVariableModuleGlobal == NULL) {\r
2714 return EFI_OUT_OF_RESOURCES;\r
2715 }\r
2716\r
2717 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
2718\r
2719 //\r
2720 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2721 // is stored with common variable in the same NV region. So the platform integrator should\r
2d3fb919 2722 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of\r
0c18794e 2723 // PcdFlashNvStorageVariableSize.\r
2724 //\r
2725 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
2726\r
9a000b46
RN
2727 //\r
2728 // Get HOB variable store.\r
2729 //\r
2730 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
2731 if (GuidHob != NULL) {\r
2732 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
2733 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
2734 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
2735 } else {\r
2736 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
2737 }\r
2738 }\r
2739\r
0c18794e 2740 //\r
2741 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
2742 //\r
2743 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2744 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
2745 if (VolatileVariableStore == NULL) {\r
2746 FreePool (mVariableModuleGlobal);\r
2747 return EFI_OUT_OF_RESOURCES;\r
2748 }\r
2749\r
2750 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
2751\r
2752 //\r
2753 // Initialize Variable Specific Data.\r
2754 //\r
2755 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
2756 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
2757 mVariableModuleGlobal->FvbInstance = NULL;\r
2758\r
2759 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
2760 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
2761 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
2762 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
2763 VolatileVariableStore->Reserved = 0;\r
2764 VolatileVariableStore->Reserved1 = 0;\r
2765\r
2766 //\r
9a000b46 2767 // Get non-volatile variable store.\r
0c18794e 2768 //\r
2769\r
2770 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
2771 if (TempVariableStoreHeader == 0) {\r
2772 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
2773 }\r
4d832aab 2774 \r
2775 //\r
2776 // Check if the Firmware Volume is not corrupted\r
2777 //\r
2778 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||\r
2779 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {\r
2780 Status = EFI_VOLUME_CORRUPTED;\r
2781 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
2782 goto Done;\r
2783 }\r
2784\r
0c18794e 2785 VariableStoreBase = TempVariableStoreHeader + \\r
2786 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
2787 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
2788 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
2789\r
2790 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
2791 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2792 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
2793 Status = EFI_VOLUME_CORRUPTED;\r
2794 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
2795 goto Done;\r
2d3fb919 2796 }\r
0c18794e 2797 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
2d3fb919 2798\r
0c18794e 2799 //\r
2800 // Parse non-volatile variable data and get last variable offset.\r
2801 //\r
2802 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
2803 while (IsValidVariableHeader (NextVariable)) {\r
2804 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
2805 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2806 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2807 } else {\r
2808 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2809 }\r
2810\r
2811 NextVariable = GetNextVariablePtr (NextVariable);\r
2812 }\r
2813\r
2814 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
2d3fb919 2815\r
0c18794e 2816 //\r
2817 // Allocate runtime memory used for a memory copy of the FLASH region.\r
2818 // Keep the memory and the FLASH in sync as updates occur\r
2819 //\r
2820 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);\r
2821 if (mNvVariableCache == NULL) {\r
2822 Status = EFI_OUT_OF_RESOURCES;\r
2823 goto Done;\r
2824 }\r
2825 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);\r
2826 Status = EFI_SUCCESS;\r
2827\r
2828Done:\r
2829 if (EFI_ERROR (Status)) {\r
2830 FreePool (mVariableModuleGlobal);\r
2831 FreePool (VolatileVariableStore);\r
2832 }\r
2833\r
2834 return Status;\r
2835}\r
2836\r
2837\r
2838/**\r
2839 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
2840\r
2841 @param[in] Address The Flash address.\r
2842 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
2843 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
2844\r
2845**/\r
2846EFI_STATUS\r
2847GetFvbInfoByAddress (\r
2848 IN EFI_PHYSICAL_ADDRESS Address,\r
2849 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
2850 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
2851 )\r
2852{\r
2853 EFI_STATUS Status;\r
2854 EFI_HANDLE *HandleBuffer;\r
2855 UINTN HandleCount;\r
2856 UINTN Index;\r
2857 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
2858 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
2859 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
2860 EFI_FVB_ATTRIBUTES_2 Attributes;\r
2d3fb919 2861\r
0c18794e 2862 //\r
2863 // Get all FVB handles.\r
2864 //\r
2865 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
2866 if (EFI_ERROR (Status)) {\r
2867 return EFI_NOT_FOUND;\r
2868 }\r
2869\r
2870 //\r
2871 // Get the FVB to access variable store.\r
2872 //\r
2873 Fvb = NULL;\r
2874 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
2875 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
2876 if (EFI_ERROR (Status)) {\r
2877 Status = EFI_NOT_FOUND;\r
2878 break;\r
2879 }\r
2880\r
2881 //\r
2882 // Ensure this FVB protocol supported Write operation.\r
2883 //\r
2884 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
2885 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2d3fb919 2886 continue;\r
0c18794e 2887 }\r
2d3fb919 2888\r
0c18794e 2889 //\r
2890 // Compare the address and select the right one.\r
2891 //\r
2892 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
2893 if (EFI_ERROR (Status)) {\r
2894 continue;\r
2895 }\r
2896\r
2897 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
2898 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
2899 if (FvbHandle != NULL) {\r
2900 *FvbHandle = HandleBuffer[Index];\r
2901 }\r
2902 if (FvbProtocol != NULL) {\r
2903 *FvbProtocol = Fvb;\r
2904 }\r
2905 Status = EFI_SUCCESS;\r
2906 break;\r
2907 }\r
2908 }\r
2909 FreePool (HandleBuffer);\r
2910\r
2911 if (Fvb == NULL) {\r
2912 Status = EFI_NOT_FOUND;\r
2913 }\r
2d3fb919 2914\r
2915 return Status;\r
0c18794e 2916}\r
2917\r