]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
This revision can only work with Intel(c) UDK Debugger Tool version 1.3 or greater...
[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
1766 CacheVariable->InDeletedTransitionPtr->State = State;\r
1767 }\r
1768 } else {\r
1769 goto Done;\r
1770 }\r
1771 }\r
1772\r
0c18794e 1773 State = Variable->CurrPtr->State;\r
1774 State &= VAR_DELETED;\r
1775\r
1776 Status = UpdateVariableStore (\r
1777 &mVariableModuleGlobal->VariableGlobal,\r
1778 Variable->Volatile,\r
1779 FALSE,\r
1780 Fvb,\r
1781 (UINTN) &Variable->CurrPtr->State,\r
1782 sizeof (UINT8),\r
1783 &State\r
2d3fb919 1784 );\r
0c18794e 1785 if (!EFI_ERROR (Status)) {\r
1786 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1787 if (!Variable->Volatile) {\r
1788 CacheVariable->CurrPtr->State = State;\r
335e2681 1789 FlushHobVariableToFlash (VariableName, VendorGuid);\r
0c18794e 1790 }\r
1791 }\r
2d3fb919 1792 goto Done;\r
0c18794e 1793 }\r
1794 //\r
1795 // If the variable is marked valid, and the same data has been passed in,\r
1796 // then return to the caller immediately.\r
1797 //\r
1798 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1799 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0) &&\r
2d3fb919 1800 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
1801 (TimeStamp == NULL)) {\r
1802 //\r
1803 // Variable content unchanged and no need to update timestamp, just return.\r
1804 //\r
0c18794e 1805 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
1806 Status = EFI_SUCCESS;\r
1807 goto Done;\r
1808 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1809 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1810\r
1811 //\r
1812 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable\r
1813 //\r
1814 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
2d3fb919 1815 //\r
1816 // Cache the previous variable data into StorageArea.\r
1817 //\r
1818 DataOffset = sizeof (VARIABLE_HEADER) + Variable->CurrPtr->NameSize + GET_PAD_SIZE (Variable->CurrPtr->NameSize);\r
1819 CopyMem (mStorageArea, (UINT8*)((UINTN) Variable->CurrPtr + DataOffset), Variable->CurrPtr->DataSize);\r
1820\r
1821 if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) ||\r
1822 (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
1823 //\r
1824 // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data\r
1825 // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of\r
1826 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.\r
1827 //\r
1828 BufSize = AppendSignatureList (mStorageArea, Variable->CurrPtr->DataSize, Data, DataSize);\r
1829 if (BufSize == Variable->CurrPtr->DataSize) {\r
1830 if ((TimeStamp == NULL) || CompareTimeStamp (TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
1831 //\r
1832 // New EFI_SIGNATURE_DATA is not found and timestamp is not later\r
1833 // than current timestamp, return EFI_SUCCESS directly.\r
1834 //\r
1835 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
1836 Status = EFI_SUCCESS;\r
1837 goto Done;\r
1838 }\r
1839 }\r
1840 } else {\r
1841 //\r
1842 // For other Variables, append the new data to the end of previous data.\r
1843 //\r
1844 CopyMem ((UINT8*)((UINTN) mStorageArea + Variable->CurrPtr->DataSize), Data, DataSize);\r
1845 BufSize = Variable->CurrPtr->DataSize + DataSize;\r
1846 }\r
1847\r
1848 RevBufSize = MIN (PcdGet32 (PcdMaxVariableSize), ScratchDataSize);\r
0c18794e 1849 if (BufSize > RevBufSize) {\r
1850 //\r
1851 // If variable size (previous + current) is bigger than reserved buffer in runtime,\r
1852 // return EFI_OUT_OF_RESOURCES.\r
1853 //\r
1854 return EFI_OUT_OF_RESOURCES;\r
1855 }\r
2d3fb919 1856\r
0c18794e 1857 //\r
1858 // Override Data and DataSize which are used for combined data area including previous and new data.\r
1859 //\r
1860 Data = mStorageArea;\r
1861 DataSize = BufSize;\r
1862 }\r
1863\r
1864 //\r
1865 // Mark the old variable as in delete transition.\r
1866 //\r
1867 State = Variable->CurrPtr->State;\r
1868 State &= VAR_IN_DELETED_TRANSITION;\r
1869\r
1870 Status = UpdateVariableStore (\r
1871 &mVariableModuleGlobal->VariableGlobal,\r
1872 Variable->Volatile,\r
1873 FALSE,\r
1874 Fvb,\r
1875 (UINTN) &Variable->CurrPtr->State,\r
1876 sizeof (UINT8),\r
1877 &State\r
2d3fb919 1878 );\r
0c18794e 1879 if (EFI_ERROR (Status)) {\r
2d3fb919 1880 goto Done;\r
1881 }\r
0c18794e 1882 if (!Variable->Volatile) {\r
1883 CacheVariable->CurrPtr->State = State;\r
1884 }\r
2d3fb919 1885 }\r
0c18794e 1886 } else {\r
1887 //\r
1888 // Not found existing variable. Create a new variable.\r
0c18794e 1889 //\r
2d3fb919 1890\r
1891 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
1892 Status = EFI_SUCCESS;\r
0c18794e 1893 goto Done;\r
1894 }\r
2d3fb919 1895\r
0c18794e 1896 //\r
1897 // Make sure we are trying to create a new variable.\r
2d3fb919 1898 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
0c18794e 1899 //\r
1900 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1901 Status = EFI_NOT_FOUND;\r
1902 goto Done;\r
1903 }\r
2d3fb919 1904\r
0c18794e 1905 //\r
1906 // Only variable have NV|RT attribute can be created in Runtime.\r
1907 //\r
1908 if (AtRuntime () &&\r
1909 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
1910 Status = EFI_INVALID_PARAMETER;\r
1911 goto Done;\r
2d3fb919 1912 }\r
0c18794e 1913 }\r
1914\r
1915 //\r
1916 // Function part - create a new variable and copy the data.\r
1917 // Both update a variable and create a variable will come here.\r
1918\r
1919 SetMem (NextVariable, ScratchSize, 0xff);\r
1920\r
1921 NextVariable->StartId = VARIABLE_DATA;\r
1922 //\r
1923 // NextVariable->State = VAR_ADDED;\r
1924 //\r
1925 NextVariable->Reserved = 0;\r
1926 NextVariable->PubKeyIndex = KeyIndex;\r
1927 NextVariable->MonotonicCount = MonotonicCount;\r
2d3fb919 1928 ZeroMem (&NextVariable->TimeStamp, sizeof (EFI_TIME));\r
0c18794e 1929\r
3b4151bc 1930 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2d3fb919 1931 (TimeStamp != NULL)) {\r
3b4151bc 1932 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
1933 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
1934 } else {\r
0c18794e 1935 //\r
1936 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
1937 // when the new TimeStamp value is later than the current timestamp associated\r
1938 // with the variable, we need associate the new timestamp with the updated value.\r
1939 //\r
2d3fb919 1940 if (Variable->CurrPtr != NULL) {\r
1941 if (CompareTimeStamp (&Variable->CurrPtr->TimeStamp, TimeStamp)) {\r
1942 CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
1943 }\r
0c18794e 1944 }\r
3b4151bc 1945 }\r
0c18794e 1946 }\r
1947\r
1948 //\r
2d3fb919 1949 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
0c18794e 1950 // Attributes bitmask parameter of a GetVariable() call.\r
1951 //\r
1952 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2d3fb919 1953\r
0c18794e 1954 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1955 VarNameSize = StrSize (VariableName);\r
1956 CopyMem (\r
1957 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1958 VariableName,\r
1959 VarNameSize\r
1960 );\r
1961 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1962 CopyMem (\r
1963 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1964 Data,\r
1965 DataSize\r
1966 );\r
1967 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1968 //\r
1969 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1970 // NextVariable->DataSize should not include pad size so that variable\r
1971 // service can get actual size in GetVariable.\r
1972 //\r
1973 NextVariable->NameSize = (UINT32)VarNameSize;\r
1974 NextVariable->DataSize = (UINT32)DataSize;\r
1975\r
1976 //\r
1977 // The actual size of the variable that stores in storage should\r
1978 // include pad size.\r
1979 //\r
1980 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
1981 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
1982 //\r
1983 // Create a nonvolatile variable.\r
1984 //\r
1985 Volatile = FALSE;\r
1986 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
2d3fb919 1987 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
0c18794e 1988 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2d3fb919 1989 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)\r
0c18794e 1990 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
1991 if (AtRuntime ()) {\r
1992 Status = EFI_OUT_OF_RESOURCES;\r
1993 goto Done;\r
1994 }\r
1995 //\r
1996 // Perform garbage collection & reclaim operation.\r
1997 //\r
83758cdc 1998 Status = Reclaim (\r
1999 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2000 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2001 FALSE,\r
23b06935 2002 Variable,\r
83758cdc 2003 FALSE,\r
2004 FALSE\r
2005 );\r
0c18794e 2006 if (EFI_ERROR (Status)) {\r
2007 goto Done;\r
2008 }\r
2009 //\r
2010 // If still no enough space, return out of resources.\r
2011 //\r
2d3fb919 2012 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
0c18794e 2013 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2d3fb919 2014 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)\r
0c18794e 2015 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
2016 Status = EFI_OUT_OF_RESOURCES;\r
2017 goto Done;\r
2018 }\r
23b06935
SZ
2019 if (Variable->CurrPtr != NULL) {\r
2020 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2021 CacheVariable->InDeletedTransitionPtr = NULL;\r
2022 }\r
0c18794e 2023 }\r
2024 //\r
2025 // Four steps\r
2026 // 1. Write variable header\r
2d3fb919 2027 // 2. Set variable state to header valid\r
0c18794e 2028 // 3. Write variable data\r
2029 // 4. Set variable state to valid\r
2030 //\r
2031 //\r
2032 // Step 1:\r
2033 //\r
2034 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
2035 Status = UpdateVariableStore (\r
2036 &mVariableModuleGlobal->VariableGlobal,\r
2037 FALSE,\r
2038 TRUE,\r
2039 Fvb,\r
2040 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2041 sizeof (VARIABLE_HEADER),\r
2042 (UINT8 *) NextVariable\r
2043 );\r
2044\r
2045 if (EFI_ERROR (Status)) {\r
2046 goto Done;\r
2047 }\r
2048\r
2049 //\r
2050 // Step 2:\r
2051 //\r
2052 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2053 Status = UpdateVariableStore (\r
2054 &mVariableModuleGlobal->VariableGlobal,\r
2055 FALSE,\r
2056 TRUE,\r
2057 Fvb,\r
2058 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2059 sizeof (UINT8),\r
2060 &NextVariable->State\r
2061 );\r
2062\r
2063 if (EFI_ERROR (Status)) {\r
2064 goto Done;\r
2065 }\r
2066 //\r
2067 // Step 3:\r
2068 //\r
2069 Status = UpdateVariableStore (\r
2070 &mVariableModuleGlobal->VariableGlobal,\r
2071 FALSE,\r
2072 TRUE,\r
2073 Fvb,\r
2074 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
2075 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
2076 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
2077 );\r
2078\r
2079 if (EFI_ERROR (Status)) {\r
2080 goto Done;\r
2081 }\r
2082 //\r
2083 // Step 4:\r
2084 //\r
2085 NextVariable->State = VAR_ADDED;\r
2086 Status = UpdateVariableStore (\r
2087 &mVariableModuleGlobal->VariableGlobal,\r
2088 FALSE,\r
2089 TRUE,\r
2090 Fvb,\r
2091 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2092 sizeof (UINT8),\r
2093 &NextVariable->State\r
2094 );\r
2095\r
2096 if (EFI_ERROR (Status)) {\r
2097 goto Done;\r
2098 }\r
2099\r
2100 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2101\r
2102 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2103 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2104 } else {\r
2105 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
2106 }\r
2107 //\r
2108 // update the memory copy of Flash region.\r
2109 //\r
2110 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
2111 } else {\r
2112 //\r
2113 // Create a volatile variable.\r
2d3fb919 2114 //\r
0c18794e 2115 Volatile = TRUE;\r
2116\r
2117 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
2118 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
2119 //\r
2120 // Perform garbage collection & reclaim operation.\r
2121 //\r
83758cdc 2122 Status = Reclaim (\r
2123 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
2124 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
2125 TRUE,\r
23b06935 2126 Variable,\r
83758cdc 2127 FALSE,\r
2128 FALSE\r
2129 );\r
0c18794e 2130 if (EFI_ERROR (Status)) {\r
2131 goto Done;\r
2132 }\r
2133 //\r
2134 // If still no enough space, return out of resources.\r
2135 //\r
2136 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
2137 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
2138 ) {\r
2139 Status = EFI_OUT_OF_RESOURCES;\r
2140 goto Done;\r
2141 }\r
23b06935
SZ
2142 if (Variable->CurrPtr != NULL) {\r
2143 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2144 CacheVariable->InDeletedTransitionPtr = NULL;\r
2145 }\r
0c18794e 2146 }\r
2147\r
2148 NextVariable->State = VAR_ADDED;\r
2149 Status = UpdateVariableStore (\r
2150 &mVariableModuleGlobal->VariableGlobal,\r
2151 TRUE,\r
2152 TRUE,\r
2153 Fvb,\r
2154 mVariableModuleGlobal->VolatileLastVariableOffset,\r
2155 (UINT32) VarSize,\r
2156 (UINT8 *) NextVariable\r
2157 );\r
2158\r
2159 if (EFI_ERROR (Status)) {\r
2160 goto Done;\r
2161 }\r
2162\r
2163 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2164 }\r
2165\r
2166 //\r
2167 // Mark the old variable as deleted.\r
2168 //\r
23b06935
SZ
2169 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2170 if (Variable->InDeletedTransitionPtr != NULL) {\r
2171 //\r
2172 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2173 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2174 //\r
2175 State = Variable->InDeletedTransitionPtr->State;\r
2176 State &= VAR_DELETED;\r
2177 Status = UpdateVariableStore (\r
2178 &mVariableModuleGlobal->VariableGlobal,\r
2179 Variable->Volatile,\r
2180 FALSE,\r
2181 Fvb,\r
2182 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2183 sizeof (UINT8),\r
2184 &State\r
2185 );\r
2186 if (!EFI_ERROR (Status)) {\r
2187 if (!Variable->Volatile) {\r
2188 CacheVariable->InDeletedTransitionPtr->State = State;\r
2189 }\r
2190 } else {\r
2191 goto Done;\r
2192 }\r
2193 }\r
2194\r
0c18794e 2195 State = Variable->CurrPtr->State;\r
2196 State &= VAR_DELETED;\r
2197\r
2198 Status = UpdateVariableStore (\r
2199 &mVariableModuleGlobal->VariableGlobal,\r
2200 Variable->Volatile,\r
2201 FALSE,\r
2202 Fvb,\r
2203 (UINTN) &Variable->CurrPtr->State,\r
2204 sizeof (UINT8),\r
2205 &State\r
2206 );\r
2d3fb919 2207 if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
0c18794e 2208 CacheVariable->CurrPtr->State = State;\r
2209 }\r
2210 }\r
2211\r
2212 if (!EFI_ERROR (Status)) {\r
2213 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
2214 if (!Volatile) {\r
2215 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2216 }\r
0c18794e 2217 }\r
2218\r
2219Done:\r
2220 return Status;\r
2221}\r
2222\r
a5f15e30
SZ
2223/**\r
2224 Check if a Unicode character is a hexadecimal character.\r
2225\r
2226 This function checks if a Unicode character is a \r
2227 hexadecimal character. The valid hexadecimal character is \r
2228 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
2229\r
2230\r
2231 @param Char The character to check against.\r
2232\r
2233 @retval TRUE If the Char is a hexadecmial character.\r
2234 @retval FALSE If the Char is not a hexadecmial character.\r
2235\r
2236**/\r
2237BOOLEAN\r
2238EFIAPI\r
2239IsHexaDecimalDigitCharacter (\r
2240 IN CHAR16 Char\r
2241 )\r
2242{\r
2243 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2244}\r
2245\r
2246/**\r
2247\r
2248 This code checks if variable is hardware error record variable or not.\r
2249\r
2250 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
2251 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
2252\r
2253 @param VariableName Pointer to variable name.\r
2254 @param VendorGuid Variable Vendor Guid.\r
2255\r
2256 @retval TRUE Variable is hardware error record variable.\r
2257 @retval FALSE Variable is not hardware error record variable.\r
2258\r
2259**/\r
2260BOOLEAN\r
2261EFIAPI\r
2262IsHwErrRecVariable (\r
2263 IN CHAR16 *VariableName,\r
2264 IN EFI_GUID *VendorGuid\r
2265 )\r
2266{\r
2267 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
2268 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
2269 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
2270 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
2271 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
2272 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
2273 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
2274 return FALSE;\r
2275 }\r
2276\r
2277 return TRUE;\r
2278}\r
2279\r
05a643f9 2280/**\r
2281 This code checks if variable should be treated as read-only variable.\r
2282\r
2283 @param[in] VariableName Name of the Variable.\r
2284 @param[in] VendorGuid GUID of the Variable.\r
2285\r
2286 @retval TRUE This variable is read-only variable.\r
2287 @retval FALSE This variable is NOT read-only variable.\r
2288 \r
2289**/\r
2290BOOLEAN\r
2291IsReadOnlyVariable (\r
2292 IN CHAR16 *VariableName,\r
2293 IN EFI_GUID *VendorGuid\r
2294 )\r
2295{\r
2296 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {\r
2297 if ((StrCmp (VariableName, EFI_SETUP_MODE_NAME) == 0) ||\r
2298 (StrCmp (VariableName, EFI_SIGNATURE_SUPPORT_NAME) == 0) ||\r
2299 (StrCmp (VariableName, EFI_SECURE_BOOT_MODE_NAME) == 0)) {\r
2300 return TRUE;\r
2301 }\r
2302 }\r
2303 \r
2304 return FALSE;\r
2305}\r
2306\r
0c18794e 2307/**\r
2308\r
2309 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2310\r
dc204d5a
JY
2311 Caution: This function may receive untrusted input.\r
2312 This function may be invoked in SMM mode, and datasize is external input.\r
2313 This function will do basic validation, before parse the data.\r
2314\r
0c18794e 2315 @param VariableName Name of Variable to be found.\r
2316 @param VendorGuid Variable vendor GUID.\r
2317 @param Attributes Attribute value of the variable found.\r
2318 @param DataSize Size of Data found. If size is less than the\r
2319 data, this value contains the required size.\r
2320 @param Data Data pointer.\r
2d3fb919 2321\r
0c18794e 2322 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2323 @return EFI_SUCCESS Find the specified variable.\r
2324 @return EFI_NOT_FOUND Not found.\r
2325 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2326\r
2327**/\r
2328EFI_STATUS\r
2329EFIAPI\r
2330VariableServiceGetVariable (\r
2331 IN CHAR16 *VariableName,\r
2332 IN EFI_GUID *VendorGuid,\r
2333 OUT UINT32 *Attributes OPTIONAL,\r
2334 IN OUT UINTN *DataSize,\r
2335 OUT VOID *Data\r
2336 )\r
2337{\r
2338 EFI_STATUS Status;\r
2339 VARIABLE_POINTER_TRACK Variable;\r
2340 UINTN VarDataSize;\r
2341\r
2342 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2343 return EFI_INVALID_PARAMETER;\r
2344 }\r
2345\r
2346 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2d3fb919 2347\r
ecc722ad 2348 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2349 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2350 goto Done;\r
2351 }\r
2352\r
2353 //\r
2354 // Get data size\r
2355 //\r
2356 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2357 ASSERT (VarDataSize != 0);\r
2358\r
2359 if (*DataSize >= VarDataSize) {\r
2360 if (Data == NULL) {\r
2361 Status = EFI_INVALID_PARAMETER;\r
2362 goto Done;\r
2363 }\r
2364\r
2365 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2366 if (Attributes != NULL) {\r
2367 *Attributes = Variable.CurrPtr->Attributes;\r
2368 }\r
2369\r
2370 *DataSize = VarDataSize;\r
2371 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2d3fb919 2372\r
0c18794e 2373 Status = EFI_SUCCESS;\r
2374 goto Done;\r
2375 } else {\r
2376 *DataSize = VarDataSize;\r
2377 Status = EFI_BUFFER_TOO_SMALL;\r
2378 goto Done;\r
2379 }\r
2380\r
2381Done:\r
2382 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2383 return Status;\r
2384}\r
2385\r
2386\r
2387\r
2388/**\r
2389\r
2390 This code Finds the Next available variable.\r
2391\r
dc204d5a
JY
2392 Caution: This function may receive untrusted input.\r
2393 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2394\r
0c18794e 2395 @param VariableNameSize Size of the variable name.\r
2396 @param VariableName Pointer to variable name.\r
2397 @param VendorGuid Variable Vendor Guid.\r
2398\r
2399 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2400 @return EFI_SUCCESS Find the specified variable.\r
2401 @return EFI_NOT_FOUND Not found.\r
2402 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2403\r
2404**/\r
2405EFI_STATUS\r
2406EFIAPI\r
2407VariableServiceGetNextVariableName (\r
2408 IN OUT UINTN *VariableNameSize,\r
2409 IN OUT CHAR16 *VariableName,\r
2410 IN OUT EFI_GUID *VendorGuid\r
2411 )\r
2412{\r
9a000b46 2413 VARIABLE_STORE_TYPE Type;\r
0c18794e 2414 VARIABLE_POINTER_TRACK Variable;\r
9a000b46 2415 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2416 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
0c18794e 2417 UINTN VarNameSize;\r
2418 EFI_STATUS Status;\r
9a000b46 2419 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
0c18794e 2420\r
2421 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2422 return EFI_INVALID_PARAMETER;\r
2423 }\r
2424\r
2425 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2426\r
ecc722ad 2427 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
0c18794e 2428 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2429 goto Done;\r
2430 }\r
2431\r
2432 if (VariableName[0] != 0) {\r
2433 //\r
2434 // If variable name is not NULL, get next variable.\r
2435 //\r
2436 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2437 }\r
2438\r
9a000b46
RN
2439 //\r
2440 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2441 // The index and attributes mapping must be kept in this order as FindVariable\r
2442 // makes use of this mapping to implement search algorithm.\r
2443 //\r
2444 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2445 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2446 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2447\r
0c18794e 2448 while (TRUE) {\r
2449 //\r
9a000b46 2450 // Switch from Volatile to HOB, to Non-Volatile.\r
0c18794e 2451 //\r
9a000b46
RN
2452 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
2453 (Variable.CurrPtr == NULL) ||\r
2454 !IsValidVariableHeader (Variable.CurrPtr)\r
2455 ) {\r
2456 //\r
2457 // Find current storage index\r
2458 //\r
2459 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2460 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2461 break;\r
2462 }\r
2463 }\r
2464 ASSERT (Type < VariableStoreTypeMax);\r
2465 //\r
2466 // Switch to next storage\r
2467 //\r
2468 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2469 if (VariableStoreHeader[Type] != NULL) {\r
2470 break;\r
2471 }\r
2472 }\r
2473 //\r
2d3fb919 2474 // Capture the case that\r
9a000b46
RN
2475 // 1. current storage is the last one, or\r
2476 // 2. no further storage\r
2477 //\r
2478 if (Type == VariableStoreTypeMax) {\r
0c18794e 2479 Status = EFI_NOT_FOUND;\r
2480 goto Done;\r
2481 }\r
9a000b46
RN
2482 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2483 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2484 Variable.CurrPtr = Variable.StartPtr;\r
0c18794e 2485 }\r
9a000b46 2486\r
0c18794e 2487 //\r
2488 // Variable is found\r
2489 //\r
23b06935
SZ
2490 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2491 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
2492 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2493 //\r
2494 // If it is a IN_DELETED_TRANSITION variable,\r
2495 // and there is also a same ADDED one at the same time,\r
2496 // don't return it.\r
2497 //\r
2498 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
2499 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
2500 Status = FindVariableEx (\r
2501 GetVariableNamePtr (Variable.CurrPtr),\r
2502 &Variable.CurrPtr->VendorGuid,\r
2503 FALSE,\r
2504 &VariablePtrTrack\r
2505 );\r
2506 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
2507 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2508 continue;\r
2509 }\r
2510 }\r
9a000b46
RN
2511\r
2512 //\r
2513 // Don't return NV variable when HOB overrides it\r
2514 //\r
2d3fb919 2515 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
9a000b46
RN
2516 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2517 ) {\r
2518 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2519 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2520 Status = FindVariableEx (\r
2521 GetVariableNamePtr (Variable.CurrPtr),\r
2522 &Variable.CurrPtr->VendorGuid,\r
ecc722ad 2523 FALSE,\r
9a000b46
RN
2524 &VariableInHob\r
2525 );\r
2526 if (!EFI_ERROR (Status)) {\r
2527 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2528 continue;\r
2529 }\r
2530 }\r
2531\r
0c18794e 2532 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2533 ASSERT (VarNameSize != 0);\r
2534\r
2535 if (VarNameSize <= *VariableNameSize) {\r
9a000b46
RN
2536 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2537 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
0c18794e 2538 Status = EFI_SUCCESS;\r
2539 } else {\r
2540 Status = EFI_BUFFER_TOO_SMALL;\r
2541 }\r
2542\r
2543 *VariableNameSize = VarNameSize;\r
2544 goto Done;\r
2545 }\r
2546 }\r
2547\r
2548 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2549 }\r
2550\r
2551Done:\r
2552 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2553 return Status;\r
2554}\r
2555\r
2556/**\r
2557\r
2558 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2559\r
dc204d5a
JY
2560 Caution: This function may receive untrusted input.\r
2561 This function may be invoked in SMM mode, and datasize and data are external input.\r
2562 This function will do basic validation, before parse the data.\r
2563 This function will parse the authentication carefully to avoid security issues, like\r
2564 buffer overflow, integer overflow.\r
2565 This function will check attribute carefully to avoid authentication bypass.\r
2566\r
0c18794e 2567 @param VariableName Name of Variable to be found.\r
2568 @param VendorGuid Variable vendor GUID.\r
2569 @param Attributes Attribute value of the variable found\r
2570 @param DataSize Size of Data found. If size is less than the\r
2571 data, this value contains the required size.\r
2572 @param Data Data pointer.\r
2573\r
2574 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2575 @return EFI_SUCCESS Set successfully.\r
2576 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2577 @return EFI_NOT_FOUND Not found.\r
2578 @return EFI_WRITE_PROTECTED Variable is read-only.\r
2579\r
2580**/\r
2581EFI_STATUS\r
2582EFIAPI\r
2583VariableServiceSetVariable (\r
2584 IN CHAR16 *VariableName,\r
2585 IN EFI_GUID *VendorGuid,\r
2586 IN UINT32 Attributes,\r
2587 IN UINTN DataSize,\r
2588 IN VOID *Data\r
2589 )\r
2590{\r
2591 VARIABLE_POINTER_TRACK Variable;\r
2592 EFI_STATUS Status;\r
2593 VARIABLE_HEADER *NextVariable;\r
2594 EFI_PHYSICAL_ADDRESS Point;\r
2595 UINTN PayloadSize;\r
2596\r
2597 //\r
2598 // Check input parameters.\r
2599 //\r
2600 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2601 return EFI_INVALID_PARAMETER;\r
2d3fb919 2602 }\r
0c18794e 2603\r
05a643f9 2604 if (IsReadOnlyVariable (VariableName, VendorGuid)) {\r
2605 return EFI_WRITE_PROTECTED;\r
2606 }\r
2607\r
0c18794e 2608 if (DataSize != 0 && Data == NULL) {\r
2609 return EFI_INVALID_PARAMETER;\r
2610 }\r
2611\r
275beb2b 2612 //\r
2613 // Check for reserverd bit in variable attribute.\r
2614 //\r
2615 if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
2616 return EFI_INVALID_PARAMETER;\r
2617 }\r
2618\r
0c18794e 2619 //\r
2620 // Make sure if runtime bit is set, boot service bit is set also.\r
2621 //\r
2622 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2623 return EFI_INVALID_PARAMETER;\r
2624 }\r
2625\r
2626 //\r
2d3fb919 2627 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
0c18794e 2628 // cannot be set both.\r
2629 //\r
2d3fb919 2630 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
0c18794e 2631 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
2632 return EFI_INVALID_PARAMETER;\r
2d3fb919 2633 }\r
0c18794e 2634\r
2635 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
2636 if (DataSize < AUTHINFO_SIZE) {\r
2637 //\r
2d3fb919 2638 // Try to write Authenticated Variable without AuthInfo.\r
0c18794e 2639 //\r
2640 return EFI_SECURITY_VIOLATION;\r
2d3fb919 2641 }\r
2642 PayloadSize = DataSize - AUTHINFO_SIZE;\r
2643 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
2644 //\r
2645 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
2646 //\r
2647 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
6bc4e19f 2648 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
2649 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
2d3fb919 2650 return EFI_SECURITY_VIOLATION;\r
2651 }\r
2652 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
0c18794e 2653 } else {\r
2d3fb919 2654 PayloadSize = DataSize;\r
0c18794e 2655 }\r
2d3fb919 2656\r
0c18794e 2657 //\r
2658 // The size of the VariableName, including the Unicode Null in bytes plus\r
2659 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2660 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
2661 //\r
2662 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2663 if ((PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r
2664 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r
2665 return EFI_INVALID_PARAMETER;\r
2666 }\r
a5f15e30 2667 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
0c18794e 2668 return EFI_INVALID_PARAMETER;\r
2669 }\r
2670 } else {\r
2671 //\r
2672 // The size of the VariableName, including the Unicode Null in bytes plus\r
2673 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
2674 //\r
2675 if ((PayloadSize > PcdGet32 (PcdMaxVariableSize)) ||\r
2676 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxVariableSize))) {\r
2677 return EFI_INVALID_PARAMETER;\r
2d3fb919 2678 }\r
2679 }\r
0c18794e 2680\r
021a1af9
SZ
2681 if (AtRuntime ()) {\r
2682 //\r
2683 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence\r
2684 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.\r
2685 //\r
2686 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {\r
2687 return EFI_WRITE_PROTECTED;\r
2688 }\r
2689 }\r
2690\r
0c18794e 2691 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2692\r
2693 //\r
2694 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
2695 //\r
2696 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
2697 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2698 //\r
2699 // Parse non-volatile variable data and get last variable offset.\r
2700 //\r
2701 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2d3fb919 2702 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))\r
0c18794e 2703 && IsValidVariableHeader (NextVariable)) {\r
2704 NextVariable = GetNextVariablePtr (NextVariable);\r
2705 }\r
2706 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2707 }\r
2708\r
2709 //\r
2710 // Check whether the input variable is already existed.\r
2711 //\r
ecc722ad 2712 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2713 if (!EFI_ERROR (Status)) {\r
2714 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
2715 return EFI_WRITE_PROTECTED;\r
2716 }\r
2717 }\r
2718 \r
0c18794e 2719 //\r
2720 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
2721 //\r
2722 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2723 //\r
2724 // Process PK, KEK, Sigdb seperately.\r
2725 //\r
2726 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
2727 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
2728 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
2729 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
ecc722ad 2730 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && \r
2731 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) {\r
05a643f9 2732 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
2733 if (EFI_ERROR (Status)) {\r
2734 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
2735 }\r
0c18794e 2736 } else {\r
2737 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
2738 }\r
2739\r
2740 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
2741 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2742\r
2743 return Status;\r
2744}\r
2745\r
2746/**\r
2747\r
2748 This code returns information about the EFI variables.\r
2749\r
dc204d5a
JY
2750 Caution: This function may receive untrusted input.\r
2751 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2752\r
0c18794e 2753 @param Attributes Attributes bitmask to specify the type of variables\r
2754 on which to return information.\r
2755 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2756 for the EFI variables associated with the attributes specified.\r
2757 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2758 for EFI variables associated with the attributes specified.\r
2759 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2760 associated with the attributes specified.\r
2761\r
2762 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2763 @return EFI_SUCCESS Query successfully.\r
2764 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
2765\r
2766**/\r
2767EFI_STATUS\r
2768EFIAPI\r
2769VariableServiceQueryVariableInfo (\r
2770 IN UINT32 Attributes,\r
2771 OUT UINT64 *MaximumVariableStorageSize,\r
2772 OUT UINT64 *RemainingVariableStorageSize,\r
2773 OUT UINT64 *MaximumVariableSize\r
2774 )\r
2775{\r
2776 VARIABLE_HEADER *Variable;\r
2777 VARIABLE_HEADER *NextVariable;\r
2778 UINT64 VariableSize;\r
2779 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2780 UINT64 CommonVariableTotalSize;\r
2781 UINT64 HwErrVariableTotalSize;\r
2782\r
2783 CommonVariableTotalSize = 0;\r
2784 HwErrVariableTotalSize = 0;\r
2785\r
2786 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
2787 return EFI_INVALID_PARAMETER;\r
2788 }\r
2789\r
2790 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
2791 //\r
2792 // Make sure the Attributes combination is supported by the platform.\r
2793 //\r
2d3fb919 2794 return EFI_UNSUPPORTED;\r
0c18794e 2795 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2796 //\r
2797 // Make sure if runtime bit is set, boot service bit is set also.\r
2798 //\r
2799 return EFI_INVALID_PARAMETER;\r
2800 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
2801 //\r
2802 // Make sure RT Attribute is set if we are in Runtime phase.\r
2803 //\r
2804 return EFI_INVALID_PARAMETER;\r
2805 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2806 //\r
2807 // Make sure Hw Attribute is set with NV.\r
2808 //\r
2809 return EFI_INVALID_PARAMETER;\r
2810 }\r
2811\r
2812 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2813\r
2814 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2815 //\r
2816 // Query is Volatile related.\r
2817 //\r
2818 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
2819 } else {\r
2820 //\r
2821 // Query is Non-Volatile related.\r
2822 //\r
2823 VariableStoreHeader = mNvVariableCache;\r
2824 }\r
2825\r
2826 //\r
2827 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2828 // with the storage size (excluding the storage header size).\r
2829 //\r
2830 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
2831\r
2832 //\r
2833 // Harware error record variable needs larger size.\r
2834 //\r
2835 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2836 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2837 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2838 } else {\r
2839 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2840 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2841 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2842 }\r
2843\r
2844 //\r
2845 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2846 //\r
2847 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
2848 }\r
2849\r
2850 //\r
2851 // Point to the starting address of the variables.\r
2852 //\r
2853 Variable = GetStartPointer (VariableStoreHeader);\r
2854\r
2855 //\r
2856 // Now walk through the related variable store.\r
2857 //\r
2858 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
2859 NextVariable = GetNextVariablePtr (Variable);\r
2860 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2861\r
2862 if (AtRuntime ()) {\r
2863 //\r
2864 // We don't take the state of the variables in mind\r
2865 // when calculating RemainingVariableStorageSize,\r
2866 // since the space occupied by variables not marked with\r
2867 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2868 //\r
2869 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2870 HwErrVariableTotalSize += VariableSize;\r
2871 } else {\r
2872 CommonVariableTotalSize += VariableSize;\r
2873 }\r
2874 } else {\r
2875 //\r
2876 // Only care about Variables with State VAR_ADDED, because\r
2877 // the space not marked as VAR_ADDED is reclaimable now.\r
2878 //\r
2879 if (Variable->State == VAR_ADDED) {\r
2880 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2881 HwErrVariableTotalSize += VariableSize;\r
2882 } else {\r
2883 CommonVariableTotalSize += VariableSize;\r
2884 }\r
2885 }\r
2886 }\r
2887\r
2888 //\r
2889 // Go to the next one.\r
2890 //\r
2891 Variable = NextVariable;\r
2892 }\r
2893\r
2894 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2895 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2896 }else {\r
2897 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2898 }\r
2899\r
2900 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2901 *MaximumVariableSize = 0;\r
2902 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2903 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2904 }\r
2905\r
2906 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2907 return EFI_SUCCESS;\r
2908}\r
2909\r
2910\r
2911/**\r
2912 This function reclaims variable storage if free size is below the threshold.\r
2d3fb919 2913\r
876ac395 2914 Caution: This function may be invoked at SMM mode.\r
2915 Care must be taken to make sure not security issue.\r
dc204d5a 2916\r
0c18794e 2917**/\r
2918VOID\r
2919ReclaimForOS(\r
2920 VOID\r
2921 )\r
2922{\r
2923 EFI_STATUS Status;\r
2924 UINTN CommonVariableSpace;\r
2925 UINTN RemainingCommonVariableSpace;\r
2926 UINTN RemainingHwErrVariableSpace;\r
2927\r
2d3fb919 2928 Status = EFI_SUCCESS;\r
0c18794e 2929\r
2930 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2931\r
2932 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2933\r
2934 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
2935 //\r
2936 // Check if the free area is blow a threshold.\r
2937 //\r
2938 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
2d3fb919 2939 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
0c18794e 2940 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
2941 Status = Reclaim (\r
2942 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2943 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2944 FALSE,\r
335e2681 2945 NULL,\r
83758cdc 2946 FALSE,\r
335e2681 2947 FALSE\r
0c18794e 2948 );\r
2949 ASSERT_EFI_ERROR (Status);\r
2950 }\r
2951}\r
2952\r
335e2681
SZ
2953/**\r
2954 Flush the HOB variable to flash.\r
2955\r
2956 @param[in] VariableName Name of variable has been updated or deleted.\r
2957 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
2958\r
2959**/\r
2960VOID\r
2961FlushHobVariableToFlash (\r
2962 IN CHAR16 *VariableName,\r
2963 IN EFI_GUID *VendorGuid\r
2964 )\r
2965{\r
2966 EFI_STATUS Status;\r
2967 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2968 VARIABLE_HEADER *Variable;\r
2969 VOID *VariableData;\r
2970 BOOLEAN ErrorFlag;\r
2971\r
2972 ErrorFlag = FALSE;\r
2973\r
2974 //\r
2975 // Flush the HOB variable to flash.\r
2976 //\r
2977 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
2978 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2979 //\r
2980 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
2981 //\r
2982 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
2983 for ( Variable = GetStartPointer (VariableStoreHeader)\r
2984 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
2985 ; Variable = GetNextVariablePtr (Variable)\r
2986 ) {\r
2987 if (Variable->State != VAR_ADDED) {\r
2988 //\r
2989 // The HOB variable has been set to DELETED state in local.\r
2990 //\r
2991 continue;\r
2992 }\r
2993 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
2994 if (VendorGuid == NULL || VariableName == NULL ||\r
2995 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
2996 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
2997 VariableData = GetVariableDataPtr (Variable);\r
2998 Status = VariableServiceSetVariable (\r
2999 GetVariableNamePtr (Variable),\r
3000 &Variable->VendorGuid,\r
3001 Variable->Attributes,\r
3002 Variable->DataSize,\r
3003 VariableData\r
3004 );\r
3005 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
3006 } else {\r
3007 //\r
3008 // The updated or deleted variable is matched with the HOB variable.\r
3009 // Don't break here because we will try to set other HOB variables\r
3010 // since this variable could be set successfully.\r
3011 //\r
3012 Status = EFI_SUCCESS;\r
3013 }\r
3014 if (!EFI_ERROR (Status)) {\r
3015 //\r
3016 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3017 // set the HOB variable to DELETED state in local.\r
3018 //\r
3019 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
3020 Variable->State &= VAR_DELETED;\r
3021 } else {\r
3022 ErrorFlag = TRUE;\r
3023 }\r
3024 }\r
3025 if (ErrorFlag) {\r
3026 //\r
3027 // We still have HOB variable(s) not flushed in flash.\r
3028 //\r
3029 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3030 } else {\r
3031 //\r
3032 // All HOB variables have been flushed in flash.\r
3033 //\r
3034 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3035 if (!AtRuntime ()) {\r
3036 FreePool ((VOID *) VariableStoreHeader);\r
3037 }\r
3038 }\r
3039 }\r
3040\r
3041}\r
0c18794e 3042\r
3043/**\r
3044 Initializes variable write service after FVB was ready.\r
3045\r
3046 @retval EFI_SUCCESS Function successfully executed.\r
3047 @retval Others Fail to initialize the variable service.\r
3048\r
3049**/\r
3050EFI_STATUS\r
3051VariableWriteServiceInitialize (\r
3052 VOID\r
3053 )\r
3054{\r
3055 EFI_STATUS Status;\r
3056 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3057 UINTN Index;\r
3058 UINT8 Data;\r
3059 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
0c18794e 3060\r
3061 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
3062 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2d3fb919 3063\r
0c18794e 3064 //\r
3065 // Check if the free area is really free.\r
3066 //\r
9a000b46 3067 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
0c18794e 3068 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3069 if (Data != 0xff) {\r
3070 //\r
3071 // There must be something wrong in variable store, do reclaim operation.\r
3072 //\r
3073 Status = Reclaim (\r
3074 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3075 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3076 FALSE,\r
335e2681 3077 NULL,\r
83758cdc 3078 FALSE,\r
335e2681 3079 TRUE\r
0c18794e 3080 );\r
3081 if (EFI_ERROR (Status)) {\r
3082 return Status;\r
3083 }\r
3084 break;\r
3085 }\r
3086 }\r
3087\r
335e2681 3088 FlushHobVariableToFlash (NULL, NULL);\r
9a000b46 3089\r
0c18794e 3090 //\r
3091 // Authenticated variable initialize.\r
3092 //\r
3093 Status = AutenticatedVariableServiceInitialize ();\r
3094\r
3095 return Status;\r
3096}\r
3097\r
3098\r
3099/**\r
3100 Initializes variable store area for non-volatile and volatile variable.\r
3101\r
3102 @retval EFI_SUCCESS Function successfully executed.\r
3103 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3104\r
3105**/\r
3106EFI_STATUS\r
3107VariableCommonInitialize (\r
3108 VOID\r
3109 )\r
3110{\r
3111 EFI_STATUS Status;\r
3112 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
3113 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3114 VARIABLE_HEADER *NextVariable;\r
3115 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
3116 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3117 UINT64 VariableStoreLength;\r
3118 UINTN ScratchSize;\r
3119 UINTN VariableSize;\r
9a000b46 3120 EFI_HOB_GUID_TYPE *GuidHob;\r
0c18794e 3121\r
3122 //\r
3123 // Allocate runtime memory for variable driver global structure.\r
3124 //\r
3125 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
3126 if (mVariableModuleGlobal == NULL) {\r
3127 return EFI_OUT_OF_RESOURCES;\r
3128 }\r
3129\r
3130 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
3131\r
3132 //\r
3133 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3134 // is stored with common variable in the same NV region. So the platform integrator should\r
2d3fb919 3135 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of\r
0c18794e 3136 // PcdFlashNvStorageVariableSize.\r
3137 //\r
3138 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
3139\r
9a000b46
RN
3140 //\r
3141 // Get HOB variable store.\r
3142 //\r
3143 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
3144 if (GuidHob != NULL) {\r
3145 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 3146 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
9a000b46 3147 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
3148 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
3149 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
3150 return EFI_OUT_OF_RESOURCES;\r
3151 }\r
9a000b46
RN
3152 } else {\r
3153 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
3154 }\r
3155 }\r
3156\r
0c18794e 3157 //\r
3158 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
3159 //\r
3160 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3161 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
3162 if (VolatileVariableStore == NULL) {\r
3163 FreePool (mVariableModuleGlobal);\r
3164 return EFI_OUT_OF_RESOURCES;\r
3165 }\r
3166\r
3167 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
3168\r
3169 //\r
3170 // Initialize Variable Specific Data.\r
3171 //\r
3172 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
3173 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
3174 mVariableModuleGlobal->FvbInstance = NULL;\r
3175\r
3176 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
3177 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
3178 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
3179 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
3180 VolatileVariableStore->Reserved = 0;\r
3181 VolatileVariableStore->Reserved1 = 0;\r
3182\r
3183 //\r
9a000b46 3184 // Get non-volatile variable store.\r
0c18794e 3185 //\r
3186\r
3187 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3188 if (TempVariableStoreHeader == 0) {\r
3189 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3190 }\r
4d832aab 3191 \r
3192 //\r
3193 // Check if the Firmware Volume is not corrupted\r
3194 //\r
3195 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||\r
3196 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {\r
3197 Status = EFI_VOLUME_CORRUPTED;\r
3198 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3199 goto Done;\r
3200 }\r
3201\r
0c18794e 3202 VariableStoreBase = TempVariableStoreHeader + \\r
3203 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
3204 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
3205 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
3206\r
3207 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3208 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
3209 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
3210 Status = EFI_VOLUME_CORRUPTED;\r
3211 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
3212 goto Done;\r
2d3fb919 3213 }\r
0c18794e 3214 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
2d3fb919 3215\r
0c18794e 3216 //\r
3217 // Parse non-volatile variable data and get last variable offset.\r
3218 //\r
3219 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3220 while (IsValidVariableHeader (NextVariable)) {\r
3221 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
3222 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3223 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
3224 } else {\r
3225 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
3226 }\r
3227\r
3228 NextVariable = GetNextVariablePtr (NextVariable);\r
3229 }\r
3230\r
3231 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
2d3fb919 3232\r
0c18794e 3233 //\r
3234 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3235 // Keep the memory and the FLASH in sync as updates occur\r
3236 //\r
3237 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);\r
3238 if (mNvVariableCache == NULL) {\r
3239 Status = EFI_OUT_OF_RESOURCES;\r
3240 goto Done;\r
3241 }\r
3242 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);\r
3243 Status = EFI_SUCCESS;\r
3244\r
3245Done:\r
3246 if (EFI_ERROR (Status)) {\r
3247 FreePool (mVariableModuleGlobal);\r
3248 FreePool (VolatileVariableStore);\r
3249 }\r
3250\r
3251 return Status;\r
3252}\r
3253\r
3254\r
3255/**\r
3256 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
3257\r
3258 @param[in] Address The Flash address.\r
3259 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
3260 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
3261\r
3262**/\r
3263EFI_STATUS\r
3264GetFvbInfoByAddress (\r
3265 IN EFI_PHYSICAL_ADDRESS Address,\r
3266 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
3267 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
3268 )\r
3269{\r
3270 EFI_STATUS Status;\r
3271 EFI_HANDLE *HandleBuffer;\r
3272 UINTN HandleCount;\r
3273 UINTN Index;\r
3274 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
3275 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
3276 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
3277 EFI_FVB_ATTRIBUTES_2 Attributes;\r
2d3fb919 3278\r
0c18794e 3279 //\r
3280 // Get all FVB handles.\r
3281 //\r
3282 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
3283 if (EFI_ERROR (Status)) {\r
3284 return EFI_NOT_FOUND;\r
3285 }\r
3286\r
3287 //\r
3288 // Get the FVB to access variable store.\r
3289 //\r
3290 Fvb = NULL;\r
3291 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
3292 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
3293 if (EFI_ERROR (Status)) {\r
3294 Status = EFI_NOT_FOUND;\r
3295 break;\r
3296 }\r
3297\r
3298 //\r
3299 // Ensure this FVB protocol supported Write operation.\r
3300 //\r
3301 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
3302 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2d3fb919 3303 continue;\r
0c18794e 3304 }\r
2d3fb919 3305\r
0c18794e 3306 //\r
3307 // Compare the address and select the right one.\r
3308 //\r
3309 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
3310 if (EFI_ERROR (Status)) {\r
3311 continue;\r
3312 }\r
3313\r
3314 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
3315 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
3316 if (FvbHandle != NULL) {\r
3317 *FvbHandle = HandleBuffer[Index];\r
3318 }\r
3319 if (FvbProtocol != NULL) {\r
3320 *FvbProtocol = Fvb;\r
3321 }\r
3322 Status = EFI_SUCCESS;\r
3323 break;\r
3324 }\r
3325 }\r
3326 FreePool (HandleBuffer);\r
3327\r
3328 if (Fvb == NULL) {\r
3329 Status = EFI_NOT_FOUND;\r
3330 }\r
2d3fb919 3331\r
3332 return Status;\r
0c18794e 3333}\r
3334\r