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