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