]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/FSVariable/FSVariable.c
MdePkg: add missing #defines for decoding PCIe 2.1 extended capability structures
[mirror_edk2.git] / DuetPkg / FSVariable / FSVariable.c
CommitLineData
9071550e 1/*++\r
2\r
452f0207 3Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
b1f700a8 4This program and the accompanying materials\r
9071550e 5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 FSVariable.c\r
15\r
16Abstract:\r
17\r
18 Provide support functions for variable services.\r
19\r
20--*/\r
21\r
22#include "FSVariable.h"\r
23\r
24VARIABLE_STORE_HEADER mStoreHeaderTemplate = {\r
439c34f3 25 VARIABLE_STORE_SIGNATURE,\r
9071550e 26 VOLATILE_VARIABLE_STORE_SIZE,\r
27 VARIABLE_STORE_FORMATTED,\r
28 VARIABLE_STORE_HEALTHY,\r
29 0,\r
30 0\r
31};\r
32\r
33//\r
34// Don't use module globals after the SetVirtualAddress map is signaled\r
35//\r
36VARIABLE_GLOBAL *mGlobal;\r
37\r
a24b4043 38/**\r
39 Update the variable region with Variable information. These are the same \r
40 arguments as the EFI Variable services.\r
41\r
42 @param[in] VariableName Name of variable\r
43\r
44 @param[in] VendorGuid Guid of variable\r
45\r
46 @param[in] Data Variable data\r
47\r
48 @param[in] DataSize Size of data. 0 means delete\r
49\r
b29a823d 50 @param[in] Attributes Attribues of the variable\r
a24b4043 51\r
52 @param[in] Variable The variable information which is used to keep track of variable usage.\r
53\r
54 @retval EFI_SUCCESS The update operation is success.\r
55\r
56 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
57\r
58**/\r
59EFI_STATUS\r
60EFIAPI\r
61UpdateVariable (\r
62 IN CHAR16 *VariableName,\r
63 IN EFI_GUID *VendorGuid,\r
64 IN VOID *Data,\r
65 IN UINTN DataSize,\r
66 IN UINT32 Attributes OPTIONAL,\r
67 IN VARIABLE_POINTER_TRACK *Variable\r
68 );\r
69\r
9071550e 70VOID\r
71EFIAPI\r
e56dd2ce 72OnVirtualAddressChangeFsv (\r
9071550e 73 IN EFI_EVENT Event,\r
74 IN VOID *Context\r
75 );\r
76\r
9071550e 77VOID\r
78EFIAPI\r
79OnSimpleFileSystemInstall (\r
80 IN EFI_EVENT Event,\r
81 IN VOID *Context\r
82 );\r
83\r
9071550e 84BOOLEAN\r
85IsValidVariableHeader (\r
86 IN VARIABLE_HEADER *Variable\r
87 )\r
88/*++\r
89\r
90Routine Description:\r
91\r
92 This code checks if variable header is valid or not.\r
93\r
94Arguments:\r
95 Variable Pointer to the Variable Header.\r
96\r
97Returns:\r
98 TRUE Variable header is valid.\r
99 FALSE Variable header is not valid.\r
100\r
101--*/\r
102{\r
a24b4043 103 if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
9071550e 104 return FALSE;\r
105 }\r
106\r
107 return TRUE;\r
108}\r
109\r
9071550e 110VARIABLE_STORE_STATUS\r
111GetVariableStoreStatus (\r
112 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
113 )\r
114/*++\r
115\r
116Routine Description:\r
117\r
118 This code gets the current status of Variable Store.\r
119\r
120Arguments:\r
121\r
122 VarStoreHeader Pointer to the Variable Store Header.\r
123\r
124Returns:\r
125\r
126 EfiRaw Variable store status is raw\r
127 EfiValid Variable store status is valid\r
128 EfiInvalid Variable store status is invalid\r
129\r
130--*/\r
131{\r
3709c4cd 132 if (CompareGuid (&VarStoreHeader->Signature, &mStoreHeaderTemplate.Signature) &&\r
9071550e 133 (VarStoreHeader->Format == mStoreHeaderTemplate.Format) &&\r
134 (VarStoreHeader->State == mStoreHeaderTemplate.State)\r
135 ) {\r
136 return EfiValid;\r
3709c4cd 137 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == VAR_DEFAULT_VALUE_32 &&\r
138 ((UINT32 *)(&VarStoreHeader->Signature))[1] == VAR_DEFAULT_VALUE_32 &&\r
139 ((UINT32 *)(&VarStoreHeader->Signature))[2] == VAR_DEFAULT_VALUE_32 &&\r
140 ((UINT32 *)(&VarStoreHeader->Signature))[3] == VAR_DEFAULT_VALUE_32 &&\r
141 VarStoreHeader->Size == VAR_DEFAULT_VALUE_32 &&\r
142 VarStoreHeader->Format == VAR_DEFAULT_VALUE &&\r
143 VarStoreHeader->State == VAR_DEFAULT_VALUE\r
9071550e 144 ) {\r
145\r
146 return EfiRaw;\r
147 } else {\r
148 return EfiInvalid;\r
149 }\r
150}\r
151\r
9071550e 152UINT8 *\r
153GetVariableDataPtr (\r
154 IN VARIABLE_HEADER *Variable\r
155 )\r
156/*++\r
157\r
158Routine Description:\r
159\r
160 This code gets the pointer to the variable data.\r
161\r
162Arguments:\r
163\r
164 Variable Pointer to the Variable Header.\r
165\r
166Returns:\r
167\r
168 UINT8* Pointer to Variable Data\r
169\r
170--*/\r
171{\r
172 //\r
173 // Be careful about pad size for alignment\r
174 //\r
175 return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
176}\r
177\r
9071550e 178VARIABLE_HEADER *\r
179GetNextVariablePtr (\r
180 IN VARIABLE_HEADER *Variable\r
181 )\r
182/*++\r
183\r
184Routine Description:\r
185\r
186 This code gets the pointer to the next variable header.\r
187\r
188Arguments:\r
189\r
190 Variable Pointer to the Variable Header.\r
191\r
192Returns:\r
193\r
194 VARIABLE_HEADER* Pointer to next variable header.\r
195\r
196--*/\r
197{\r
198 if (!IsValidVariableHeader (Variable)) {\r
199 return NULL;\r
200 }\r
201 //\r
202 // Be careful about pad size for alignment\r
203 //\r
204 return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
205}\r
206\r
9071550e 207VARIABLE_HEADER *\r
208GetEndPointer (\r
209 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
210 )\r
211/*++\r
212\r
213Routine Description:\r
214\r
215 This code gets the pointer to the last variable memory pointer byte\r
216\r
217Arguments:\r
218\r
219 VarStoreHeader Pointer to the Variable Store Header.\r
220\r
221Returns:\r
222\r
223 VARIABLE_HEADER* Pointer to last unavailable Variable Header\r
224\r
225--*/\r
226{\r
227 //\r
228 // The end of variable store\r
229 //\r
230 return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
231}\r
232\r
233BOOLEAN\r
234ExistNewerVariable (\r
235 IN VARIABLE_HEADER *Variable\r
236 )\r
237/*++\r
238\r
239Routine Description:\r
240\r
241 Check if exist newer variable when doing reclaim\r
242\r
243Arguments:\r
244\r
245 Variable Pointer to start position\r
246\r
247Returns:\r
248\r
249 TRUE - Exists another variable, which is newer than the current one\r
250 FALSE - Doesn't exist another vairable which is newer than the current one\r
251\r
252--*/\r
253{\r
254 VARIABLE_HEADER *NextVariable;\r
255 CHAR16 *VariableName;\r
256 EFI_GUID *VendorGuid;\r
257 \r
258 VendorGuid = &Variable->VendorGuid;\r
259 VariableName = GET_VARIABLE_NAME_PTR(Variable);\r
260 \r
261 NextVariable = GetNextVariablePtr (Variable);\r
262 while (IsValidVariableHeader (NextVariable)) {\r
263 if ((NextVariable->State == VAR_ADDED) || (NextVariable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
264 //\r
265 // If match Guid and Name\r
266 //\r
267 if (CompareGuid (VendorGuid, &NextVariable->VendorGuid)) {\r
268 if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (NextVariable), StrSize (VariableName)) == 0) {\r
269 return TRUE;\r
270 }\r
271 }\r
272 }\r
273 NextVariable = GetNextVariablePtr (NextVariable);\r
274 }\r
275 return FALSE;\r
276}\r
277\r
9071550e 278EFI_STATUS\r
279Reclaim (\r
280 IN VARIABLE_STORAGE_TYPE StorageType,\r
281 IN VARIABLE_HEADER *CurrentVariable OPTIONAL\r
282 )\r
283/*++\r
284\r
285Routine Description:\r
286\r
287 Variable store garbage collection and reclaim operation\r
288\r
289Arguments:\r
290\r
291 IsVolatile The variable store is volatile or not,\r
292 if it is non-volatile, need FTW\r
293 CurrentVairable If it is not NULL, it means not to process\r
294 current variable for Reclaim.\r
295\r
296Returns:\r
297\r
298 EFI STATUS\r
299\r
300--*/\r
301{\r
302 VARIABLE_HEADER *Variable;\r
303 VARIABLE_HEADER *NextVariable;\r
304 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
305 UINT8 *ValidBuffer;\r
306 UINTN ValidBufferSize;\r
307 UINTN VariableSize;\r
308 UINT8 *CurrPtr;\r
309 EFI_STATUS Status;\r
310\r
311 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType];\r
312\r
313 //\r
314 // Start Pointers for the variable.\r
315 //\r
316 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
317\r
a24b4043 318 //\r
319 // recaluate the total size of Common/HwErr type variables in non-volatile area.\r
320 //\r
321 if (!StorageType) {\r
322 mGlobal->CommonVariableTotalSize = 0;\r
323 mGlobal->HwErrVariableTotalSize = 0;\r
324 }\r
9071550e 325 //\r
326 // To make the reclaim, here we just allocate a memory that equal to the original memory\r
327 //\r
328 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER) + VariableStoreHeader->Size;\r
329\r
330 Status = gBS->AllocatePool (\r
331 EfiBootServicesData,\r
332 ValidBufferSize,\r
7c04a679 333 (VOID**) &ValidBuffer\r
9071550e 334 );\r
335 if (EFI_ERROR (Status)) {\r
336 return Status;\r
337 }\r
338\r
339 CurrPtr = ValidBuffer;\r
340\r
341 //\r
342 // Copy variable store header\r
343 //\r
344 CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
345 CurrPtr += sizeof (VARIABLE_STORE_HEADER);\r
346\r
347 //\r
348 // Start Pointers for the variable.\r
349 //\r
350 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
351\r
352 \r
353 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
354 while (IsValidVariableHeader (Variable)) {\r
355 NextVariable = GetNextVariablePtr (Variable);\r
356 //\r
357 // State VAR_ADDED or VAR_IN_DELETED_TRANSITION are to kept,\r
358 // The CurrentVariable, is also saved, as SetVariable may fail due to lack of space\r
359 //\r
360 if (Variable->State == VAR_ADDED) {\r
361 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
362 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
363 ValidBufferSize += VariableSize;\r
364 CurrPtr += VariableSize;\r
a24b4043 365 if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
366 mGlobal->HwErrVariableTotalSize += VariableSize;\r
367 } else if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
368 mGlobal->CommonVariableTotalSize += VariableSize;\r
369 }\r
9071550e 370 } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {\r
371 //\r
372 // As variables that with the same guid and name may exist in NV due to power failure during SetVariable,\r
373 // we will only save the latest valid one\r
374 //\r
375 if (!ExistNewerVariable(Variable)) {\r
376 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
377 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
378 //\r
379 // If CurrentVariable == Variable, mark as VAR_IN_DELETED_TRANSITION\r
380 //\r
381 if (Variable != CurrentVariable){\r
382 ((VARIABLE_HEADER *)CurrPtr)->State = VAR_ADDED;\r
383 }\r
384 CurrPtr += VariableSize;\r
385 ValidBufferSize += VariableSize;\r
a24b4043 386 if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
387 mGlobal->HwErrVariableTotalSize += VariableSize;\r
388 } else if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
389 mGlobal->CommonVariableTotalSize += VariableSize;\r
390 }\r
9071550e 391 }\r
392 }\r
393 Variable = NextVariable;\r
394 }\r
395\r
a24b4043 396 mGlobal->LastVariableOffset[StorageType] = ValidBufferSize;\r
397\r
9071550e 398 //\r
399 // TODO: cannot restore to original state, basic FTW needed\r
400 //\r
401 Status = mGlobal->VariableStore[StorageType]->Erase (\r
402 mGlobal->VariableStore[StorageType]\r
403 );\r
404 Status = mGlobal->VariableStore[StorageType]->Write (\r
405 mGlobal->VariableStore[StorageType],\r
406 0,\r
407 ValidBufferSize,\r
408 ValidBuffer\r
409 );\r
410\r
a24b4043 411 if (EFI_ERROR (Status)) {\r
412 //\r
413 // If error, then reset the last variable offset to zero.\r
414 //\r
415 mGlobal->LastVariableOffset[StorageType] = 0;\r
416 };\r
9071550e 417\r
9071550e 418 gBS->FreePool (ValidBuffer);\r
419\r
420 return Status;\r
421}\r
422\r
9071550e 423EFI_STATUS\r
424FindVariable (\r
425 IN CHAR16 *VariableName,\r
426 IN EFI_GUID *VendorGuid,\r
427 OUT VARIABLE_POINTER_TRACK *PtrTrack\r
428 )\r
429/*++\r
430\r
431Routine Description:\r
432\r
433 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
434\r
435Arguments:\r
436\r
437 VariableName Name of the variable to be found\r
438 VendorGuid Vendor GUID to be found.\r
439 PtrTrack Variable Track Pointer structure that contains\r
440 Variable Information.\r
441 Contains the pointer of Variable header.\r
442\r
443Returns:\r
444\r
445 EFI_INVALID_PARAMETER - Invalid parameter\r
446 EFI_SUCCESS - Find the specified variable\r
447 EFI_NOT_FOUND - Not found\r
448\r
449--*/\r
450{\r
451 VARIABLE_HEADER *Variable;\r
452 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
453 UINTN Index;\r
454 VARIABLE_HEADER *InDeleteVariable;\r
455 UINTN InDeleteIndex;\r
456 VARIABLE_HEADER *InDeleteStartPtr;\r
457 VARIABLE_HEADER *InDeleteEndPtr;\r
458\r
459 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
460 return EFI_INVALID_PARAMETER;\r
461 }\r
462\r
463 InDeleteVariable = NULL;\r
464 InDeleteIndex = (UINTN)-1;\r
465 InDeleteStartPtr = NULL;\r
466 InDeleteEndPtr = NULL;\r
467\r
468 for (Index = 0; Index < MaxType; Index ++) {\r
469 //\r
470 // 0: Non-Volatile, 1: Volatile\r
471 //\r
472 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Index];\r
473\r
474 //\r
475 // Start Pointers for the variable.\r
476 // Actual Data Pointer where data can be written.\r
477 //\r
478 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
479\r
480 //\r
481 // Find the variable by walk through non-volatile and volatile variable store\r
482 //\r
483 PtrTrack->StartPtr = Variable;\r
484 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);\r
485\r
a24b4043 486 while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {\r
9071550e 487 if (Variable->State == VAR_ADDED) {\r
488 if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
489 if (VariableName[0] == 0) {\r
490 PtrTrack->CurrPtr = Variable;\r
491 PtrTrack->Type = (VARIABLE_STORAGE_TYPE) Index;\r
492 return EFI_SUCCESS;\r
493 } else {\r
494 if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {\r
495 if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {\r
496 PtrTrack->CurrPtr = Variable;\r
497 PtrTrack->Type = (VARIABLE_STORAGE_TYPE) Index;\r
498 return EFI_SUCCESS;\r
499 }\r
500 }\r
501 }\r
502 }\r
503 } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {\r
504 //\r
505 // VAR_IN_DELETED_TRANSITION should also be checked.\r
506 //\r
507 if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
508 if (VariableName[0] == 0) {\r
509 InDeleteVariable = Variable;\r
510 InDeleteIndex = Index;\r
511 InDeleteStartPtr = PtrTrack->StartPtr;\r
512 InDeleteEndPtr = PtrTrack->EndPtr;\r
513 } else {\r
514 if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {\r
515 if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {\r
516 InDeleteVariable = Variable;\r
517 InDeleteIndex = Index;\r
518 InDeleteStartPtr = PtrTrack->StartPtr;\r
519 InDeleteEndPtr = PtrTrack->EndPtr;\r
520 }\r
521 }\r
522 }\r
523 }\r
524 }\r
525\r
526 Variable = GetNextVariablePtr (Variable);\r
527 }\r
528 //\r
529 // While (...)\r
530 //\r
531 }\r
532 //\r
a24b4043 533 // for (...)\r
534 //\r
535\r
536 //\r
537 // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,\r
538 // we return it.\r
539 //\r
540 if (InDeleteVariable != NULL) {\r
541 PtrTrack->CurrPtr = InDeleteVariable;\r
542 PtrTrack->Type = (VARIABLE_STORAGE_TYPE) InDeleteIndex;\r
543 PtrTrack->StartPtr = InDeleteStartPtr;\r
544 PtrTrack->EndPtr = InDeleteEndPtr;\r
545 return EFI_SUCCESS;\r
546 }\r
547\r
548 PtrTrack->CurrPtr = NULL;\r
549 return EFI_NOT_FOUND;\r
550}\r
551\r
552/**\r
553 Get index from supported language codes according to language string.\r
554\r
555 This code is used to get corresponding index in supported language codes. It can handle\r
607e91f5 556 RFC4646 and ISO639 language tags.\r
a24b4043 557 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
607e91f5 558 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
a24b4043 559\r
560 For example:\r
561 SupportedLang = "engfraengfra"\r
562 Lang = "eng"\r
563 Iso639Language = TRUE\r
564 The return value is "0".\r
565 Another example:\r
566 SupportedLang = "en;fr;en-US;fr-FR"\r
567 Lang = "fr-FR"\r
568 Iso639Language = FALSE\r
569 The return value is "3".\r
570\r
571 @param SupportedLang Platform supported language codes.\r
572 @param Lang Configured language.\r
607e91f5 573 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
a24b4043 574\r
575 @retval the index of language in the language codes.\r
576\r
577**/\r
578UINTN\r
a24b4043 579GetIndexFromSupportedLangCodes(\r
580 IN CHAR8 *SupportedLang,\r
581 IN CHAR8 *Lang,\r
582 IN BOOLEAN Iso639Language\r
583 ) \r
584{\r
585 UINTN Index;\r
d7f79118
RN
586 UINTN CompareLength;\r
587 UINTN LanguageLength;\r
a24b4043 588\r
a24b4043 589 if (Iso639Language) {\r
d7f79118 590 CompareLength = ISO_639_2_ENTRY_SIZE;\r
a24b4043 591 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
592 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
593 //\r
594 // Successfully find the index of Lang string in SupportedLang string.\r
595 //\r
596 Index = Index / CompareLength;\r
597 return Index;\r
598 }\r
599 }\r
600 ASSERT (FALSE);\r
601 return 0;\r
602 } else {\r
603 //\r
607e91f5 604 // Compare RFC4646 language code\r
a24b4043 605 //\r
d7f79118
RN
606 Index = 0;\r
607 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
608\r
609 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
a24b4043 610 //\r
d7f79118 611 // Skip ';' characters in SupportedLang\r
a24b4043 612 //\r
d7f79118
RN
613 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
614 //\r
615 // Determine the length of the next language code in SupportedLang\r
616 //\r
617 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
618 \r
619 if ((CompareLength == LanguageLength) && \r
620 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
a24b4043 621 //\r
622 // Successfully find the index of Lang string in SupportedLang string.\r
623 //\r
624 return Index;\r
625 }\r
a24b4043 626 }\r
627 ASSERT (FALSE);\r
628 return 0;\r
629 }\r
630}\r
631\r
632/**\r
633 Get language string from supported language codes according to index.\r
634\r
635 This code is used to get corresponding language string in supported language codes. It can handle\r
607e91f5 636 RFC4646 and ISO639 language tags.\r
a24b4043 637 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
607e91f5 638 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
a24b4043 639\r
640 For example:\r
641 SupportedLang = "engfraengfra"\r
642 Index = "1"\r
643 Iso639Language = TRUE\r
644 The return value is "fra".\r
645 Another example:\r
646 SupportedLang = "en;fr;en-US;fr-FR"\r
647 Index = "1"\r
648 Iso639Language = FALSE\r
649 The return value is "fr".\r
650\r
651 @param SupportedLang Platform supported language codes.\r
652 @param Index the index in supported language codes.\r
607e91f5 653 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
a24b4043 654\r
655 @retval the language string in the language codes.\r
656\r
657**/\r
658CHAR8 *\r
a24b4043 659GetLangFromSupportedLangCodes (\r
660 IN CHAR8 *SupportedLang,\r
661 IN UINTN Index,\r
662 IN BOOLEAN Iso639Language\r
663)\r
664{\r
665 UINTN SubIndex;\r
d7f79118 666 UINTN CompareLength;\r
a24b4043 667 CHAR8 *Supported;\r
668\r
669 SubIndex = 0;\r
670 Supported = SupportedLang;\r
671 if (Iso639Language) {\r
672 //\r
673 // according to the index of Lang string in SupportedLang string to get the language.\r
674 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
675 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
676 //\r
d7f79118
RN
677 CompareLength = ISO_639_2_ENTRY_SIZE;\r
678 mGlobal->Lang[CompareLength] = '\0';\r
a24b4043 679 return CopyMem (mGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
d7f79118 680\r
a24b4043 681 } else {\r
682 while (TRUE) {\r
683 //\r
684 // take semicolon as delimitation, sequentially traverse supported language codes.\r
685 //\r
686 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
687 Supported++;\r
688 }\r
689 if ((*Supported == '\0') && (SubIndex != Index)) {\r
690 //\r
691 // Have completed the traverse, but not find corrsponding string.\r
692 // This case is not allowed to happen.\r
693 //\r
694 ASSERT(FALSE);\r
695 return NULL;\r
696 }\r
697 if (SubIndex == Index) {\r
698 //\r
699 // according to the index of Lang string in SupportedLang string to get the language.\r
700 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
701 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
702 //\r
d7f79118 703 mGlobal->PlatformLang[CompareLength] = '\0';\r
a24b4043 704 return CopyMem (mGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
705 }\r
706 SubIndex++;\r
462979af
RN
707\r
708 //\r
709 // Skip ';' characters in Supported\r
710 //\r
711 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
a24b4043 712 }\r
713 }\r
714}\r
715\r
d7f79118
RN
716/**\r
717 Returns a pointer to an allocated buffer that contains the best matching language \r
718 from a set of supported languages. \r
719 \r
720 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
721 code types may not be mixed in a single call to this function. This function\r
722 supports a variable argument list that allows the caller to pass in a prioritized\r
723 list of language codes to test against all the language codes in SupportedLanguages.\r
724\r
725 If SupportedLanguages is NULL, then ASSERT().\r
726\r
727 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
728 contains a set of language codes in the format \r
729 specified by Iso639Language.\r
730 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
731 in ISO 639-2 format. If FALSE, then all language\r
732 codes are assumed to be in RFC 4646 language format\r
733 @param[in] ... A variable argument list that contains pointers to \r
734 Null-terminated ASCII strings that contain one or more\r
735 language codes in the format specified by Iso639Language.\r
736 The first language code from each of these language\r
737 code lists is used to determine if it is an exact or\r
738 close match to any of the language codes in \r
739 SupportedLanguages. Close matches only apply to RFC 4646\r
740 language codes, and the matching algorithm from RFC 4647\r
741 is used to determine if a close match is present. If \r
742 an exact or close match is found, then the matching\r
743 language code from SupportedLanguages is returned. If\r
744 no matches are found, then the next variable argument\r
745 parameter is evaluated. The variable argument list \r
746 is terminated by a NULL.\r
747\r
748 @retval NULL The best matching language could not be found in SupportedLanguages.\r
749 @retval NULL There are not enough resources available to return the best matching \r
750 language.\r
751 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
752 language in SupportedLanguages.\r
753\r
754**/\r
755CHAR8 *\r
e1adae60 756EFIAPI\r
d7f79118
RN
757VariableGetBestLanguage (\r
758 IN CONST CHAR8 *SupportedLanguages, \r
759 IN BOOLEAN Iso639Language,\r
760 ...\r
761 )\r
762{\r
763 VA_LIST Args;\r
764 CHAR8 *Language;\r
765 UINTN CompareLength;\r
766 UINTN LanguageLength;\r
767 CONST CHAR8 *Supported;\r
768 CHAR8 *Buffer;\r
769\r
770 ASSERT (SupportedLanguages != NULL);\r
771\r
772 VA_START (Args, Iso639Language);\r
773 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
774 //\r
775 // Default to ISO 639-2 mode\r
776 //\r
777 CompareLength = 3;\r
778 LanguageLength = MIN (3, AsciiStrLen (Language));\r
779\r
780 //\r
781 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
782 //\r
783 if (!Iso639Language) {\r
784 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
785 }\r
786\r
787 //\r
788 // Trim back the length of Language used until it is empty\r
789 //\r
790 while (LanguageLength > 0) {\r
791 //\r
792 // Loop through all language codes in SupportedLanguages\r
793 //\r
794 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
795 //\r
796 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
797 //\r
798 if (!Iso639Language) {\r
799 //\r
800 // Skip ';' characters in Supported\r
801 //\r
802 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
803 //\r
804 // Determine the length of the next language code in Supported\r
805 //\r
806 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
807 //\r
808 // If Language is longer than the Supported, then skip to the next language\r
809 //\r
810 if (LanguageLength > CompareLength) {\r
811 continue;\r
812 }\r
813 }\r
814 //\r
815 // See if the first LanguageLength characters in Supported match Language\r
816 //\r
817 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
818 VA_END (Args);\r
819\r
820 Buffer = Iso639Language ? mGlobal->Lang : mGlobal->PlatformLang;\r
821 Buffer[CompareLength] = '\0';\r
822 return CopyMem (Buffer, Supported, CompareLength);\r
823 }\r
824 }\r
825\r
826 if (Iso639Language) {\r
827 //\r
828 // If ISO 639 mode, then each language can only be tested once\r
829 //\r
830 LanguageLength = 0;\r
831 } else {\r
832 //\r
833 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
834 //\r
835 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
836 }\r
837 }\r
838 }\r
839 VA_END (Args);\r
840\r
841 //\r
842 // No matches were found \r
843 //\r
844 return NULL;\r
845}\r
846\r
a24b4043 847/**\r
848 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
849\r
850 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
851\r
852 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
853 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
854\r
855 @param[in] VariableName Name of variable\r
856\r
857 @param[in] Data Variable data\r
858\r
859 @param[in] DataSize Size of data. 0 means delete\r
860\r
a24b4043 861**/\r
d7f79118 862VOID\r
a24b4043 863AutoUpdateLangVariable(\r
864 IN CHAR16 *VariableName,\r
865 IN VOID *Data,\r
866 IN UINTN DataSize\r
867 )\r
868{\r
d7f79118
RN
869 EFI_STATUS Status;\r
870 CHAR8 *BestPlatformLang;\r
871 CHAR8 *BestLang;\r
872 UINTN Index;\r
873 UINT32 Attributes;\r
a24b4043 874 VARIABLE_POINTER_TRACK Variable;\r
d7f79118 875 BOOLEAN SetLanguageCodes;\r
a24b4043 876\r
877 //\r
d7f79118 878 // Don't do updates for delete operation\r
a24b4043 879 //\r
d7f79118
RN
880 if (DataSize == 0) {\r
881 return;\r
882 }\r
883\r
884 SetLanguageCodes = FALSE;\r
a24b4043 885\r
886 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
d7f79118
RN
887 //\r
888 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
889 //\r
890 if (EfiAtRuntime ()) {\r
891 return;\r
892 }\r
893\r
894 SetLanguageCodes = TRUE;\r
895\r
a24b4043 896 //\r
897 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
898 // Therefore, in variable driver, only store the original value for other use.\r
899 //\r
d7f79118
RN
900 if (mGlobal->PlatformLangCodes != NULL) {\r
901 FreePool (mGlobal->PlatformLangCodes);\r
902 }\r
903 mGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
904 ASSERT (mGlobal->PlatformLangCodes != NULL);\r
905\r
a24b4043 906 //\r
d7f79118
RN
907 // PlatformLang holds a single language from PlatformLangCodes, \r
908 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
a24b4043 909 //\r
d7f79118
RN
910 if (mGlobal->PlatformLang != NULL) {\r
911 FreePool (mGlobal->PlatformLang);\r
912 }\r
913 mGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
914 ASSERT (mGlobal->PlatformLang != NULL);\r
a24b4043 915\r
d7f79118 916 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
a24b4043 917 //\r
d7f79118 918 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
a24b4043 919 //\r
d7f79118
RN
920 if (EfiAtRuntime ()) {\r
921 return;\r
922 }\r
923\r
924 SetLanguageCodes = TRUE;\r
a24b4043 925\r
926 //\r
d7f79118
RN
927 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
928 // Therefore, in variable driver, only store the original value for other use.\r
a24b4043 929 //\r
d7f79118
RN
930 if (mGlobal->LangCodes != NULL) {\r
931 FreePool (mGlobal->LangCodes);\r
932 }\r
933 mGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
934 ASSERT (mGlobal->LangCodes != NULL);\r
935 }\r
a24b4043 936\r
d7f79118
RN
937 if (SetLanguageCodes \r
938 && (mGlobal->PlatformLangCodes != NULL)\r
939 && (mGlobal->LangCodes != NULL)) {\r
a24b4043 940 //\r
d7f79118
RN
941 // Update Lang if PlatformLang is already set\r
942 // Update PlatformLang if Lang is already set\r
a24b4043 943 //\r
d7f79118
RN
944 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r
945 if (!EFI_ERROR (Status)) {\r
946 //\r
947 // Update Lang\r
948 //\r
949 VariableName = L"PlatformLang";\r
950 Data = GetVariableDataPtr (Variable.CurrPtr);\r
951 DataSize = Variable.CurrPtr->DataSize;\r
952 } else {\r
953 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable);\r
954 if (!EFI_ERROR (Status)) {\r
955 //\r
956 // Update PlatformLang\r
957 //\r
958 VariableName = L"Lang";\r
959 Data = GetVariableDataPtr (Variable.CurrPtr);\r
960 DataSize = Variable.CurrPtr->DataSize;\r
961 } else {\r
962 //\r
963 // Neither PlatformLang nor Lang is set, directly return\r
964 //\r
965 return;\r
966 }\r
967 }\r
968 }\r
969 \r
970 //\r
971 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
972 //\r
973 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
a24b4043 974\r
d7f79118 975 if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
a24b4043 976 //\r
d7f79118 977 // Update Lang when PlatformLangCodes/LangCodes were set.\r
a24b4043 978 //\r
d7f79118
RN
979 if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r
980 //\r
981 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
982 //\r
983 BestPlatformLang = VariableGetBestLanguage (mGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
984 if (BestPlatformLang != NULL) {\r
985 //\r
986 // Get the corresponding index in language codes.\r
987 //\r
988 Index = GetIndexFromSupportedLangCodes (mGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
a24b4043 989\r
d7f79118
RN
990 //\r
991 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
992 //\r
993 BestLang = GetLangFromSupportedLangCodes (mGlobal->LangCodes, Index, TRUE);\r
a24b4043 994\r
d7f79118
RN
995 //\r
996 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
997 //\r
998 FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable);\r
a24b4043 999\r
d7f79118 1000 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
a24b4043 1001\r
d7f79118 1002 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
a24b4043 1003\r
d7f79118
RN
1004 ASSERT_EFI_ERROR(Status);\r
1005 }\r
1006 }\r
a24b4043 1007\r
d7f79118 1008 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
a24b4043 1009 //\r
d7f79118 1010 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
a24b4043 1011 //\r
d7f79118
RN
1012 if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r
1013 //\r
1014 // When setting Lang, firstly get most matched language string from supported language codes.\r
1015 //\r
1016 BestLang = VariableGetBestLanguage (mGlobal->LangCodes, TRUE, Data, NULL);\r
1017 if (BestLang != NULL) {\r
1018 //\r
1019 // Get the corresponding index in language codes.\r
1020 //\r
1021 Index = GetIndexFromSupportedLangCodes (mGlobal->LangCodes, BestLang, TRUE);\r
a24b4043 1022\r
d7f79118
RN
1023 //\r
1024 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1025 //\r
1026 BestPlatformLang = GetLangFromSupportedLangCodes (mGlobal->PlatformLangCodes, Index, FALSE);\r
1027\r
1028 //\r
1029 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1030 //\r
1031 FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r
a24b4043 1032\r
d7f79118
RN
1033 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r
1034 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
a24b4043 1035\r
d7f79118
RN
1036 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
1037 ASSERT_EFI_ERROR (Status);\r
1038 }\r
1039 }\r
a24b4043 1040 }\r
a24b4043 1041}\r
1042\r
1043/**\r
1044 Update the variable region with Variable information. These are the same \r
1045 arguments as the EFI Variable services.\r
1046\r
1047 @param[in] VariableName Name of variable\r
1048\r
1049 @param[in] VendorGuid Guid of variable\r
1050\r
1051 @param[in] Data Variable data\r
1052\r
1053 @param[in] DataSize Size of data. 0 means delete\r
1054\r
b29a823d 1055 @param[in] Attributes Attribues of the variable\r
a24b4043 1056\r
1057 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1058\r
1059 @retval EFI_SUCCESS The update operation is success.\r
1060\r
1061 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
1062\r
1063**/\r
1064EFI_STATUS\r
1065EFIAPI\r
1066UpdateVariable (\r
1067 IN CHAR16 *VariableName,\r
1068 IN EFI_GUID *VendorGuid,\r
1069 IN VOID *Data,\r
1070 IN UINTN DataSize,\r
1071 IN UINT32 Attributes OPTIONAL,\r
1072 IN VARIABLE_POINTER_TRACK *Variable\r
1073 )\r
1074{\r
1075 EFI_STATUS Status;\r
1076 VARIABLE_HEADER *NextVariable;\r
1077 UINTN VarNameOffset;\r
1078 UINTN VarDataOffset;\r
1079 UINTN VarNameSize;\r
1080 UINTN VarSize;\r
1081 UINT8 State;\r
1082 BOOLEAN Reclaimed;\r
1083 VARIABLE_STORAGE_TYPE StorageType;\r
1084\r
1085 Reclaimed = FALSE;\r
1086\r
1087 if (Variable->CurrPtr != NULL) { \r
1088 //\r
1089 // Update/Delete existing variable\r
1090 //\r
1091 \r
1092 if (EfiAtRuntime ()) { \r
1093 //\r
1094 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
1095 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1096 // return EFI_WRITE_PROTECTED.\r
1097 //\r
1098 if (Variable->Type == Volatile) {\r
1099 return EFI_WRITE_PROTECTED;\r
1100 }\r
1101 //\r
1102 // Only variable have NV attribute can be updated/deleted in Runtime\r
1103 //\r
1104 if (!(Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
1105 return EFI_INVALID_PARAMETER; \r
1106 }\r
1107 }\r
1108 \r
1109 //\r
1110 // Setting a data variable with no access, or zero DataSize attributes\r
1111 // specified causes it to be deleted.\r
1112 //\r
1113 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1114 //\r
1115 // Found this variable in storage\r
1116 //\r
1117 State = Variable->CurrPtr->State;\r
1118 State &= VAR_DELETED;\r
1119\r
1120 Status = mGlobal->VariableStore[Variable->Type]->Write (\r
1121 mGlobal->VariableStore[Variable->Type],\r
1122 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
1123 sizeof (Variable->CurrPtr->State),\r
1124 &State\r
1125 );\r
1126 //\r
1127 // NOTE: Write operation at least can write data to memory cache\r
1128 // Discard file writing failure here.\r
1129 //\r
1130 return EFI_SUCCESS;\r
1131 }\r
1132 \r
1133 //\r
1134 // Found this variable in storage\r
1135 // If the variable is marked valid and the same data has been passed in\r
1136 // then return to the caller immediately.\r
1137 //\r
1138 if ((Variable->CurrPtr->DataSize == DataSize) &&\r
1139 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)\r
1140 ) {\r
1141 return EFI_SUCCESS;\r
1142 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1143 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1144 //\r
1145 // Mark the old variable as in delete transition\r
1146 //\r
1147 State = Variable->CurrPtr->State;\r
1148 State &= VAR_IN_DELETED_TRANSITION;\r
1149\r
1150 Status = mGlobal->VariableStore[Variable->Type]->Write (\r
1151 mGlobal->VariableStore[Variable->Type],\r
1152 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
1153 sizeof (Variable->CurrPtr->State),\r
1154 &State\r
1155 );\r
1156 //\r
1157 // NOTE: Write operation at least can write data to memory cache\r
1158 // Discard file writing failure here.\r
1159 //\r
1160 }\r
1161 } else {\r
1162 //\r
1163 // Create a new variable\r
1164 // \r
1165 \r
1166 //\r
1167 // Make sure we are trying to create a new variable.\r
1168 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
1169 //\r
1170 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1171 return EFI_NOT_FOUND;\r
1172 } \r
1173 //\r
1174 // Only variable have NV|RT attribute can be created in Runtime\r
1175 //\r
1176 if (EfiAtRuntime () &&\r
1177 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
1178 return EFI_INVALID_PARAMETER;\r
1179 } \r
1180 \r
1181 } \r
1182\r
1183 //\r
1184 // Function part - create a new variable and copy the data.\r
1185 // Both update a variable and create a variable will come here. \r
1186 // We can firstly write all the data in memory, then write them to file\r
1187 // This can reduce the times of write operation\r
1188 //\r
1189 \r
1190 NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;\r
1191\r
1192 NextVariable->StartId = VARIABLE_DATA;\r
1193 NextVariable->Attributes = Attributes;\r
1194 NextVariable->State = VAR_ADDED;\r
1195 NextVariable->Reserved = 0;\r
1196 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1197 VarNameSize = StrSize (VariableName);\r
1198 CopyMem (\r
1199 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1200 VariableName,\r
1201 VarNameSize\r
1202 );\r
1203 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1204 CopyMem (\r
1205 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1206 Data,\r
1207 DataSize\r
1208 );\r
1209 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1210 //\r
1211 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1212 // NextVariable->DataSize should not include pad size so that variable\r
1213 // service can get actual size in GetVariable\r
1214 //\r
1215 NextVariable->NameSize = (UINT32)VarNameSize;\r
1216 NextVariable->DataSize = (UINT32)DataSize;\r
1217\r
1218 //\r
1219 // The actual size of the variable that stores in storage should\r
1220 // include pad size.\r
1221 // VarDataOffset: offset from begin of current variable header\r
1222 //\r
1223 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
1224\r
1225 StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;\r
1226\r
1227 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r
1228 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r
1229 ) {\r
1230 if ((StorageType == NonVolatile) && EfiAtRuntime ()) {\r
1231 return EFI_OUT_OF_RESOURCES;\r
1232 }\r
1233 //\r
1234 // Perform garbage collection & reclaim operation\r
1235 //\r
1236 Status = Reclaim (StorageType, Variable->CurrPtr);\r
1237 if (EFI_ERROR (Status)) {\r
1238 //\r
1239 // Reclaim error\r
1240 // we cannot restore to original state, fetal error, report to user\r
1241 //\r
1242 DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));\r
1243 return Status;\r
1244 }\r
1245 //\r
1246 // If still no enough space, return out of resources\r
1247 //\r
1248 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r
1249 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r
1250 ) {\r
1251 return EFI_OUT_OF_RESOURCES;\r
1252 }\r
1253\r
1254 Reclaimed = TRUE;\r
1255 }\r
1256 Status = mGlobal->VariableStore[StorageType]->Write (\r
1257 mGlobal->VariableStore[StorageType],\r
1258 mGlobal->LastVariableOffset[StorageType],\r
1259 VarSize,\r
1260 NextVariable\r
1261 );\r
1262 //\r
1263 // NOTE: Write operation at least can write data to memory cache\r
1264 // Discard file writing failure here.\r
9071550e 1265 //\r
a24b4043 1266 mGlobal->LastVariableOffset[StorageType] += VarSize;\r
1267\r
1268 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1269 mGlobal->HwErrVariableTotalSize += VarSize;\r
1270 } else {\r
1271 mGlobal->CommonVariableTotalSize += VarSize;\r
1272 }\r
9071550e 1273\r
1274 //\r
a24b4043 1275 // Mark the old variable as deleted\r
9071550e 1276 //\r
a24b4043 1277 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1278 State = Variable->CurrPtr->State;\r
1279 State &= VAR_DELETED;\r
9071550e 1280\r
a24b4043 1281 Status = mGlobal->VariableStore[StorageType]->Write (\r
1282 mGlobal->VariableStore[StorageType],\r
1283 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
1284 sizeof (Variable->CurrPtr->State),\r
1285 &State\r
1286 );\r
1287 //\r
1288 // NOTE: Write operation at least can write data to memory cache\r
1289 // Discard file writing failure here.\r
1290 //\r
1291 }\r
1292 return EFI_SUCCESS;\r
9071550e 1293}\r
1294\r
1295EFI_STATUS\r
1296EFIAPI\r
3ffa0f1f 1297DuetGetVariable (\r
9071550e 1298 IN CHAR16 *VariableName,\r
1299 IN EFI_GUID *VendorGuid,\r
1300 OUT UINT32 *Attributes OPTIONAL,\r
1301 IN OUT UINTN *DataSize,\r
1302 OUT VOID *Data\r
1303 )\r
1304/*++\r
1305\r
1306Routine Description:\r
1307\r
1308 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
1309\r
1310Arguments:\r
1311\r
1312 VariableName Name of Variable to be found\r
1313 VendorGuid Variable vendor GUID\r
1314 Attributes OPTIONAL Attribute value of the variable found\r
1315 DataSize Size of Data found. If size is less than the\r
1316 data, this value contains the required size.\r
1317 Data Data pointer\r
1318\r
1319Returns:\r
1320\r
1321 EFI STATUS\r
1322\r
1323--*/\r
1324{\r
1325 VARIABLE_POINTER_TRACK Variable;\r
1326 UINTN VarDataSize;\r
1327 EFI_STATUS Status;\r
1328\r
1329 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1330 return EFI_INVALID_PARAMETER;\r
1331 }\r
1332\r
1333 //\r
1334 // Find existing variable\r
1335 //\r
1336 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1337\r
1338 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1339 return Status;\r
1340 }\r
1341 //\r
1342 // Get data size\r
1343 //\r
1344 VarDataSize = Variable.CurrPtr->DataSize;\r
1345 if (*DataSize >= VarDataSize) {\r
1346 if (Data == NULL) {\r
1347 return EFI_INVALID_PARAMETER;\r
1348 }\r
1349 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1350\r
1351 if (Attributes != NULL) {\r
1352 *Attributes = Variable.CurrPtr->Attributes;\r
1353 }\r
1354\r
1355 *DataSize = VarDataSize;\r
1356\r
1357 return EFI_SUCCESS;\r
1358 } else {\r
1359 *DataSize = VarDataSize;\r
1360 return EFI_BUFFER_TOO_SMALL;\r
1361 }\r
1362}\r
1363\r
1364EFI_STATUS\r
1365EFIAPI\r
1366GetNextVariableName (\r
1367 IN OUT UINTN *VariableNameSize,\r
1368 IN OUT CHAR16 *VariableName,\r
1369 IN OUT EFI_GUID *VendorGuid\r
1370 )\r
1371/*++\r
1372\r
1373Routine Description:\r
1374\r
1375 This code Finds the Next available variable\r
1376\r
1377Arguments:\r
1378\r
1379 VariableNameSize Size of the variable\r
1380 VariableName Pointer to variable name\r
1381 VendorGuid Variable Vendor Guid\r
1382\r
1383Returns:\r
1384\r
1385 EFI STATUS\r
1386\r
1387--*/\r
1388{\r
1389 VARIABLE_POINTER_TRACK Variable;\r
1390 UINTN VarNameSize;\r
1391 EFI_STATUS Status;\r
1392\r
1393 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1394 return EFI_INVALID_PARAMETER;\r
1395 }\r
1396\r
1397 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1398\r
1399 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1400 return Status;\r
1401 }\r
1402\r
1403 if (VariableName[0] != 0) {\r
1404 //\r
1405 // If variable name is not NULL, get next variable\r
1406 //\r
1407 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1408 }\r
1409\r
1410 while (TRUE) {\r
1411 //\r
1412 // The order we find variable is: 1). NonVolatile; 2). Volatile\r
1413 // If both volatile and non-volatile variable store are parsed,\r
1414 // return not found\r
1415 //\r
1416 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
1417 if (Variable.Type == Volatile) {\r
1418 //\r
1419 // Since we met the end of Volatile storage, we have parsed all the stores.\r
1420 //\r
1421 return EFI_NOT_FOUND;\r
1422 }\r
1423\r
1424 //\r
1425 // End of NonVolatile, continue to parse Volatile\r
1426 //\r
1427 Variable.Type = Volatile;\r
1428 Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);\r
1429 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);\r
1430\r
1431 Variable.CurrPtr = Variable.StartPtr;\r
1432 if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
1433 continue;\r
1434 }\r
1435 }\r
1436 //\r
1437 // Variable is found\r
1438 //\r
1439 if (IsValidVariableHeader (Variable.CurrPtr) &&\r
1440 ((Variable.CurrPtr->State == VAR_ADDED) ||\r
1441 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {\r
1442 if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1443 VarNameSize = Variable.CurrPtr->NameSize;\r
1444 if (VarNameSize <= *VariableNameSize) {\r
1445 CopyMem (\r
1446 VariableName,\r
1447 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
1448 VarNameSize\r
1449 );\r
1450 CopyMem (\r
1451 VendorGuid,\r
1452 &Variable.CurrPtr->VendorGuid,\r
1453 sizeof (EFI_GUID)\r
1454 );\r
1455 Status = EFI_SUCCESS;\r
1456 } else {\r
1457 Status = EFI_BUFFER_TOO_SMALL;\r
1458 }\r
1459\r
1460 *VariableNameSize = VarNameSize;\r
1461 return Status;\r
1462 }\r
1463 }\r
1464\r
1465 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1466 }\r
9071550e 1467}\r
1468\r
1469EFI_STATUS\r
1470EFIAPI\r
1471SetVariable (\r
1472 IN CHAR16 *VariableName,\r
1473 IN EFI_GUID *VendorGuid,\r
1474 IN UINT32 Attributes,\r
1475 IN UINTN DataSize,\r
1476 IN VOID *Data\r
1477 )\r
1478/*++\r
1479\r
1480Routine Description:\r
1481\r
1482 This code sets variable in storage blocks (Volatile or Non-Volatile)\r
1483\r
1484Arguments:\r
1485\r
1486 VariableName Name of Variable to be found\r
1487 VendorGuid Variable vendor GUID\r
1488 Attributes Attribute value of the variable found\r
1489 DataSize Size of Data found. If size is less than the\r
1490 data, this value contains the required size.\r
1491 Data Data pointer\r
1492\r
1493Returns:\r
1494 \r
1495 EFI_INVALID_PARAMETER - Invalid parameter\r
1496 EFI_SUCCESS - Set successfully\r
1497 EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r
1498 EFI_NOT_FOUND - Not found\r
1499 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r
1500 EFI_WRITE_PROTECTED - Variable is read-only\r
1501\r
1502--*/\r
1503{\r
1504 VARIABLE_POINTER_TRACK Variable;\r
1505 EFI_STATUS Status;\r
a24b4043 1506\r
9071550e 1507 //\r
1508 // Check input parameters\r
1509 // \r
9071550e 1510 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1511 return EFI_INVALID_PARAMETER;\r
1512 }\r
1513 \r
81c5255c 1514 if (DataSize != 0 && Data == NULL) {\r
1515 return EFI_INVALID_PARAMETER;\r
1516 }\r
1517\r
ff197263 1518 //\r
1519 // Not support authenticated variable write yet.\r
1520 //\r
1521 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1522 return EFI_INVALID_PARAMETER;\r
1523 }\r
1524\r
9071550e 1525 //\r
1526 // Make sure if runtime bit is set, boot service bit is set also\r
1527 //\r
1528 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1529 return EFI_INVALID_PARAMETER;\r
1530 }\r
1531 \r
9071550e 1532 //\r
1533 // The size of the VariableName, including the Unicode Null in bytes plus\r
d4577dce 1534 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
1535 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
9071550e 1536 //\r
1537 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
d4577dce 1538 if ((DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) || \r
1539 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
9071550e 1540 return EFI_INVALID_PARAMETER;\r
a24b4043 1541 }\r
1542 //\r
1543 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
1544 //\r
1545 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
1546 return EFI_INVALID_PARAMETER;\r
1547 }\r
9071550e 1548 } else {\r
d4577dce 1549 if ((DataSize > PcdGet32(PcdMaxVariableSize)) ||\r
1550 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxVariableSize))) {\r
9071550e 1551 return EFI_INVALID_PARAMETER;\r
1552 } \r
1553 } \r
e5653d94 1554\r
9071550e 1555 //\r
1556 // Check whether the input variable is already existed\r
1557 //\r
9071550e 1558 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1559\r
9071550e 1560 //\r
a24b4043 1561 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
9071550e 1562 //\r
a24b4043 1563 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
9071550e 1564\r
a24b4043 1565 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
9071550e 1566\r
a24b4043 1567 return Status;\r
9071550e 1568}\r
1569\r
9071550e 1570EFI_STATUS\r
1571EFIAPI\r
1572QueryVariableInfo (\r
1573 IN UINT32 Attributes,\r
1574 OUT UINT64 *MaximumVariableStorageSize,\r
1575 OUT UINT64 *RemainingVariableStorageSize,\r
1576 OUT UINT64 *MaximumVariableSize\r
1577 )\r
1578/*++\r
1579\r
1580Routine Description:\r
1581\r
1582 This code returns information about the EFI variables.\r
1583\r
1584Arguments:\r
1585\r
1586 Attributes Attributes bitmask to specify the type of variables\r
1587 on which to return information.\r
1588 MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1589 for the EFI variables associated with the attributes specified.\r
1590 RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
1591 for the EFI variables associated with the attributes specified.\r
1592 MaximumVariableSize Pointer to the maximum size of the individual EFI variables\r
1593 associated with the attributes specified.\r
1594\r
1595Returns:\r
1596\r
1597 EFI STATUS\r
1598 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r
1599 EFI_SUCCESS - Query successfully.\r
1600 EFI_UNSUPPORTED - The attribute is not supported on this platform.\r
1601\r
1602--*/\r
1603{\r
1604 VARIABLE_HEADER *Variable;\r
1605 VARIABLE_HEADER *NextVariable;\r
1606 UINT64 VariableSize;\r
1607 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
a24b4043 1608 UINT64 CommonVariableTotalSize;\r
1609 UINT64 HwErrVariableTotalSize;\r
1610\r
1611 CommonVariableTotalSize = 0;\r
1612 HwErrVariableTotalSize = 0;\r
9071550e 1613\r
1614 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
1615 return EFI_INVALID_PARAMETER;\r
1616 }\r
1617 \r
9071550e 1618 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
1619 //\r
1620 // Make sure the Attributes combination is supported by the platform.\r
1621 //\r
1622 return EFI_UNSUPPORTED; \r
ff197263 1623 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
9071550e 1624 //\r
1625 // Make sure if runtime bit is set, boot service bit is set also.\r
1626 //\r
1627 return EFI_INVALID_PARAMETER;\r
1628 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1629 //\r
1630 // Make sure RT Attribute is set if we are in Runtime phase.\r
1631 //\r
1632 return EFI_INVALID_PARAMETER;\r
a24b4043 1633 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1634 //\r
1635 // Make sure Hw Attribute is set with NV.\r
1636 //\r
1637 return EFI_INVALID_PARAMETER;\r
ff197263 1638 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1639 //\r
1640 // Not support authentiated variable write yet.\r
1641 //\r
1642 return EFI_UNSUPPORTED;\r
a24b4043 1643 }\r
9071550e 1644 \r
1645 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[\r
1646 (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile\r
1647 ];\r
1648 //\r
1649 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1650 // with the storage size (excluding the storage header size).\r
1651 //\r
1652 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
9071550e 1653\r
9071550e 1654 //\r
1655 // Harware error record variable needs larger size.\r
1656 //\r
a24b4043 1657 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
d4577dce 1658 *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r
1659 *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
a24b4043 1660 } else {\r
1661 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
d4577dce 1662 ASSERT (PcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
1663 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
a24b4043 1664 }\r
1665\r
1666 //\r
d4577dce 1667 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
a24b4043 1668 //\r
d4577dce 1669 *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
9071550e 1670 }\r
9071550e 1671 \r
1672 //\r
1673 // Point to the starting address of the variables.\r
1674 //\r
1675 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1676\r
1677 //\r
1678 // Now walk through the related variable store.\r
1679 //\r
a24b4043 1680 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
9071550e 1681 NextVariable = GetNextVariablePtr (Variable);\r
1682 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1683\r
1684 if (EfiAtRuntime ()) {\r
1685 //\r
1686 // we don't take the state of the variables in mind\r
1687 // when calculating RemainingVariableStorageSize,\r
1688 // since the space occupied by variables not marked with\r
1689 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
1690 //\r
452f0207 1691 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
a24b4043 1692 HwErrVariableTotalSize += VariableSize;\r
1693 } else {\r
1694 CommonVariableTotalSize += VariableSize;\r
1695 }\r
9071550e 1696 } else {\r
1697 //\r
1698 // Only care about Variables with State VAR_ADDED,because\r
1699 // the space not marked as VAR_ADDED is reclaimable now.\r
1700 //\r
a24b4043 1701 if ((Variable->State == VAR_ADDED) || (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
452f0207 1702 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
a24b4043 1703 HwErrVariableTotalSize += VariableSize;\r
1704 } else {\r
1705 CommonVariableTotalSize += VariableSize;\r
1706 }\r
9071550e 1707 }\r
1708 }\r
1709\r
1710 //\r
1711 // Go to the next one\r
1712 //\r
1713 Variable = NextVariable;\r
1714 }\r
1715 \r
a24b4043 1716 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
1717 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
1718 } else {\r
1719 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
9071550e 1720 }\r
1721\r
1722 return EFI_SUCCESS;\r
1723}\r
9071550e 1724\r
1725EFI_STATUS\r
1726EFIAPI\r
1727VariableServiceInitialize (\r
1728 IN EFI_HANDLE ImageHandle,\r
1729 IN EFI_SYSTEM_TABLE *SystemTable\r
1730 )\r
1731/*++\r
1732\r
1733Routine Description:\r
1734 This function does initialization for variable services\r
1735\r
1736Arguments:\r
1737\r
1738 ImageHandle - The firmware allocated handle for the EFI image.\r
1739 SystemTable - A pointer to the EFI System Table.\r
1740\r
1741Returns:\r
1742\r
1743 Status code.\r
1744\r
1745 EFI_NOT_FOUND - Variable store area not found.\r
1746 EFI_SUCCESS - Variable services successfully initialized.\r
1747\r
1748--*/\r
1749{\r
1750 EFI_STATUS Status;\r
1751 EFI_HANDLE NewHandle;\r
1752 VS_DEV *Dev;\r
eb16e240 1753 EFI_PEI_HOB_POINTERS GuidHob;\r
a24b4043 1754 VARIABLE_HEADER *Variable;\r
9071550e 1755 VARIABLE_HEADER *NextVariable;\r
1756 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1757 EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntryData;\r
1758 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;\r
9071550e 1759 UINT64 BaseAddress;\r
1760 UINT64 Length;\r
1761 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
1762\r
1763 Status = gBS->AllocatePool (\r
1764 EfiRuntimeServicesData,\r
1765 (UINTN) sizeof (VARIABLE_GLOBAL),\r
7c04a679 1766 (VOID**) &mGlobal\r
9071550e 1767 );\r
1768 if (EFI_ERROR (Status)) {\r
1769 return Status;\r
1770 }\r
1771\r
a24b4043 1772 ZeroMem (mGlobal, (UINTN) sizeof (VARIABLE_GLOBAL));\r
1773\r
eb16e240 1774 GuidHob.Raw = GetHobList ();\r
1775 FlashMapEntryData = NULL;\r
1776 while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {\r
1777 FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);\r
9071550e 1778 if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r
1779 break;\r
1780 }\r
eb16e240 1781 GuidHob.Raw = GET_NEXT_HOB (GuidHob); \r
9071550e 1782 }\r
1783\r
eb16e240 1784 if (FlashMapEntryData == NULL) {\r
1785 DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));\r
9071550e 1786 Status = EFI_NOT_FOUND;\r
1787 return Status;\r
1788 }\r
be768885 1789 \r
394bbc59 1790 CopyMem(\r
1791 (VOID*)&VariableStoreEntry,\r
1792 (VOID*)&FlashMapEntryData->Entries[0],\r
1793 sizeof(EFI_FLASH_SUBAREA_ENTRY)\r
1794 );\r
9071550e 1795\r
1796 //\r
1797 // Mark the variable storage region of the FLASH as RUNTIME\r
1798 //\r
1799 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);\r
1800 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);\r
1801 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
1802 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
1803 if (EFI_ERROR (Status)) {\r
1804 Status = EFI_UNSUPPORTED;\r
1805 return Status;\r
1806 }\r
1807 Status = gDS->SetMemorySpaceAttributes (\r
1808 BaseAddress,\r
1809 Length,\r
1810 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
1811 );\r
1812 if (EFI_ERROR (Status)) {\r
1813 Status = EFI_UNSUPPORTED;\r
1814 return Status;\r
1815 }\r
7c04a679 1816\r
9071550e 1817 Status = FileStorageConstructor (\r
1818 &mGlobal->VariableStore[NonVolatile], \r
1819 &mGlobal->GoVirtualChildEvent[NonVolatile],\r
1820 VariableStoreEntry.Base,\r
1821 (UINT32) VariableStoreEntry.Length,\r
1822 FlashMapEntryData->VolumeId,\r
1823 FlashMapEntryData->FilePath\r
1824 );\r
1825 ASSERT_EFI_ERROR (Status);\r
1826\r
1827 //\r
1828 // Volatile Storage\r
1829 //\r
1830 Status = MemStorageConstructor (\r
1831 &mGlobal->VariableStore[Volatile],\r
1832 &mGlobal->GoVirtualChildEvent[Volatile],\r
1833 VOLATILE_VARIABLE_STORE_SIZE\r
1834 );\r
1835 ASSERT_EFI_ERROR (Status);\r
1836\r
1837 //\r
1838 // Scratch\r
1839 //\r
1840 Status = gBS->AllocatePool (\r
1841 EfiRuntimeServicesData,\r
1842 VARIABLE_SCRATCH_SIZE,\r
1843 &mGlobal->Scratch\r
1844 );\r
1845 ASSERT_EFI_ERROR (Status);\r
1846\r
1847 //\r
1848 // 1. NV Storage\r
1849 //\r
1850 Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);\r
1851 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
1852 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
1853 if (~VariableStoreHeader->Size == 0) {\r
1854 VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;\r
1855 }\r
1856 }\r
1857 //\r
1858 // Calculate LastVariableOffset\r
1859 //\r
a24b4043 1860 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1861 while (IsValidVariableHeader (Variable)) {\r
1862 UINTN VariableSize = 0;\r
1863 NextVariable = GetNextVariablePtr (Variable);\r
1864 VariableSize = NextVariable - Variable;\r
1865 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1866 mGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
1867 } else {\r
1868 mGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
1869 }\r
1870 Variable = NextVariable;\r
9071550e 1871 }\r
a24b4043 1872\r
1873 mGlobal->LastVariableOffset[NonVolatile] = (UINTN) Variable - (UINTN) VariableStoreHeader;\r
1874 mGlobal->VariableBase[NonVolatile] = VariableStoreHeader;\r
9071550e 1875\r
1876 //\r
1877 // Reclaim if remaining space is too small\r
1878 //\r
1879 if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {\r
1880 Status = Reclaim (NonVolatile, NULL);\r
1881 if (EFI_ERROR (Status)) {\r
1882 //\r
1883 // Reclaim error\r
1884 // we cannot restore to original state\r
1885 //\r
a24b4043 1886 DEBUG ((EFI_D_ERROR, "FSVariable: Reclaim error (fatal error) - %r\n", Status));\r
9071550e 1887 ASSERT_EFI_ERROR (Status);\r
1888 }\r
1889 }\r
1890 \r
1891 //\r
1892 // 2. Volatile Storage\r
1893 //\r
1894 Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);\r
1895 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
1896 mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev); \r
1897 mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);\r
1898 //\r
1899 // init store_header & body in memory.\r
1900 //\r
1901 mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);\r
1902 mGlobal->VariableStore[Volatile]->Write (\r
1903 mGlobal->VariableStore[Volatile],\r
1904 0,\r
1905 sizeof (VARIABLE_STORE_HEADER),\r
1906 &mStoreHeaderTemplate\r
1907 );\r
1908\r
1909\r
3ffa0f1f 1910 SystemTable->RuntimeServices->GetVariable = DuetGetVariable;\r
9071550e 1911 SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;\r
1912 SystemTable->RuntimeServices->SetVariable = SetVariable;\r
1913\r
9071550e 1914 SystemTable->RuntimeServices->QueryVariableInfo = QueryVariableInfo;\r
9071550e 1915\r
1916 //\r
1917 // Now install the Variable Runtime Architectural Protocol on a new handle\r
1918 //\r
1919 NewHandle = NULL;\r
1920 Status = gBS->InstallMultipleProtocolInterfaces (\r
1921 &NewHandle,\r
1922 &gEfiVariableArchProtocolGuid,\r
1923 NULL,\r
1924 &gEfiVariableWriteArchProtocolGuid,\r
1925 NULL,\r
1926 NULL\r
1927 );\r
1928 ASSERT_EFI_ERROR (Status);\r
1929\r
1930 return Status;\r
9071550e 1931}\r
1932\r
1933\r
1934\r
9071550e 1935VOID\r
1936EFIAPI\r
e56dd2ce 1937OnVirtualAddressChangeFsv (\r
9071550e 1938 IN EFI_EVENT Event,\r
1939 IN VOID *Context\r
1940 )\r
1941{\r
1942 UINTN Index;\r
1943\r
1944 for (Index = 0; Index < MaxType; Index++) {\r
1945 mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);\r
7c04a679 1946 EfiConvertPointer (0, (VOID**) &mGlobal->VariableStore[Index]);\r
9071550e 1947 EfiConvertPointer (0, &mGlobal->VariableBase[Index]);\r
1948 }\r
d7f79118
RN
1949 EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLangCodes);\r
1950 EfiConvertPointer (0, (VOID **) &mGlobal->LangCodes);\r
1951 EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLang);\r
9071550e 1952 EfiConvertPointer (0, &mGlobal->Scratch);\r
7c04a679 1953 EfiConvertPointer (0, (VOID**) &mGlobal);\r
9071550e 1954}\r