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