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