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