]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/FSVariable/FSVariable.c
Fix AutoUpdateLangVariable() logic to handle the case PlatformLang/Lang is set before...
[mirror_edk2.git] / DuetPkg / FSVariable / FSVariable.c
CommitLineData
9071550e 1/*++\r
2\r
b1f700a8
HT
3Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
707 }\r
708 }\r
709}\r
710\r
d7f79118
RN
711/**\r
712 Returns a pointer to an allocated buffer that contains the best matching language \r
713 from a set of supported languages. \r
714 \r
715 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
716 code types may not be mixed in a single call to this function. This function\r
717 supports a variable argument list that allows the caller to pass in a prioritized\r
718 list of language codes to test against all the language codes in SupportedLanguages.\r
719\r
720 If SupportedLanguages is NULL, then ASSERT().\r
721\r
722 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
723 contains a set of language codes in the format \r
724 specified by Iso639Language.\r
725 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
726 in ISO 639-2 format. If FALSE, then all language\r
727 codes are assumed to be in RFC 4646 language format\r
728 @param[in] ... A variable argument list that contains pointers to \r
729 Null-terminated ASCII strings that contain one or more\r
730 language codes in the format specified by Iso639Language.\r
731 The first language code from each of these language\r
732 code lists is used to determine if it is an exact or\r
733 close match to any of the language codes in \r
734 SupportedLanguages. Close matches only apply to RFC 4646\r
735 language codes, and the matching algorithm from RFC 4647\r
736 is used to determine if a close match is present. If \r
737 an exact or close match is found, then the matching\r
738 language code from SupportedLanguages is returned. If\r
739 no matches are found, then the next variable argument\r
740 parameter is evaluated. The variable argument list \r
741 is terminated by a NULL.\r
742\r
743 @retval NULL The best matching language could not be found in SupportedLanguages.\r
744 @retval NULL There are not enough resources available to return the best matching \r
745 language.\r
746 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
747 language in SupportedLanguages.\r
748\r
749**/\r
750CHAR8 *\r
751VariableGetBestLanguage (\r
752 IN CONST CHAR8 *SupportedLanguages, \r
753 IN BOOLEAN Iso639Language,\r
754 ...\r
755 )\r
756{\r
757 VA_LIST Args;\r
758 CHAR8 *Language;\r
759 UINTN CompareLength;\r
760 UINTN LanguageLength;\r
761 CONST CHAR8 *Supported;\r
762 CHAR8 *Buffer;\r
763\r
764 ASSERT (SupportedLanguages != NULL);\r
765\r
766 VA_START (Args, Iso639Language);\r
767 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
768 //\r
769 // Default to ISO 639-2 mode\r
770 //\r
771 CompareLength = 3;\r
772 LanguageLength = MIN (3, AsciiStrLen (Language));\r
773\r
774 //\r
775 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
776 //\r
777 if (!Iso639Language) {\r
778 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
779 }\r
780\r
781 //\r
782 // Trim back the length of Language used until it is empty\r
783 //\r
784 while (LanguageLength > 0) {\r
785 //\r
786 // Loop through all language codes in SupportedLanguages\r
787 //\r
788 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
789 //\r
790 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
791 //\r
792 if (!Iso639Language) {\r
793 //\r
794 // Skip ';' characters in Supported\r
795 //\r
796 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
797 //\r
798 // Determine the length of the next language code in Supported\r
799 //\r
800 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
801 //\r
802 // If Language is longer than the Supported, then skip to the next language\r
803 //\r
804 if (LanguageLength > CompareLength) {\r
805 continue;\r
806 }\r
807 }\r
808 //\r
809 // See if the first LanguageLength characters in Supported match Language\r
810 //\r
811 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
812 VA_END (Args);\r
813\r
814 Buffer = Iso639Language ? mGlobal->Lang : mGlobal->PlatformLang;\r
815 Buffer[CompareLength] = '\0';\r
816 return CopyMem (Buffer, Supported, CompareLength);\r
817 }\r
818 }\r
819\r
820 if (Iso639Language) {\r
821 //\r
822 // If ISO 639 mode, then each language can only be tested once\r
823 //\r
824 LanguageLength = 0;\r
825 } else {\r
826 //\r
827 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
828 //\r
829 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
830 }\r
831 }\r
832 }\r
833 VA_END (Args);\r
834\r
835 //\r
836 // No matches were found \r
837 //\r
838 return NULL;\r
839}\r
840\r
a24b4043 841/**\r
842 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
843\r
844 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
845\r
846 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
847 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
848\r
849 @param[in] VariableName Name of variable\r
850\r
851 @param[in] Data Variable data\r
852\r
853 @param[in] DataSize Size of data. 0 means delete\r
854\r
a24b4043 855**/\r
d7f79118 856VOID\r
a24b4043 857AutoUpdateLangVariable(\r
858 IN CHAR16 *VariableName,\r
859 IN VOID *Data,\r
860 IN UINTN DataSize\r
861 )\r
862{\r
d7f79118
RN
863 EFI_STATUS Status;\r
864 CHAR8 *BestPlatformLang;\r
865 CHAR8 *BestLang;\r
866 UINTN Index;\r
867 UINT32 Attributes;\r
a24b4043 868 VARIABLE_POINTER_TRACK Variable;\r
d7f79118 869 BOOLEAN SetLanguageCodes;\r
a24b4043 870\r
871 //\r
d7f79118 872 // Don't do updates for delete operation\r
a24b4043 873 //\r
d7f79118
RN
874 if (DataSize == 0) {\r
875 return;\r
876 }\r
877\r
878 SetLanguageCodes = FALSE;\r
a24b4043 879\r
880 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
d7f79118
RN
881 //\r
882 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
883 //\r
884 if (EfiAtRuntime ()) {\r
885 return;\r
886 }\r
887\r
888 SetLanguageCodes = TRUE;\r
889\r
a24b4043 890 //\r
891 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
892 // Therefore, in variable driver, only store the original value for other use.\r
893 //\r
d7f79118
RN
894 if (mGlobal->PlatformLangCodes != NULL) {\r
895 FreePool (mGlobal->PlatformLangCodes);\r
896 }\r
897 mGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
898 ASSERT (mGlobal->PlatformLangCodes != NULL);\r
899\r
a24b4043 900 //\r
d7f79118
RN
901 // PlatformLang holds a single language from PlatformLangCodes, \r
902 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
a24b4043 903 //\r
d7f79118
RN
904 if (mGlobal->PlatformLang != NULL) {\r
905 FreePool (mGlobal->PlatformLang);\r
906 }\r
907 mGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
908 ASSERT (mGlobal->PlatformLang != NULL);\r
a24b4043 909\r
d7f79118 910 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
a24b4043 911 //\r
d7f79118 912 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
a24b4043 913 //\r
d7f79118
RN
914 if (EfiAtRuntime ()) {\r
915 return;\r
916 }\r
917\r
918 SetLanguageCodes = TRUE;\r
a24b4043 919\r
920 //\r
d7f79118
RN
921 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
922 // Therefore, in variable driver, only store the original value for other use.\r
a24b4043 923 //\r
d7f79118
RN
924 if (mGlobal->LangCodes != NULL) {\r
925 FreePool (mGlobal->LangCodes);\r
926 }\r
927 mGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
928 ASSERT (mGlobal->LangCodes != NULL);\r
929 }\r
a24b4043 930\r
d7f79118
RN
931 if (SetLanguageCodes \r
932 && (mGlobal->PlatformLangCodes != NULL)\r
933 && (mGlobal->LangCodes != NULL)) {\r
a24b4043 934 //\r
d7f79118
RN
935 // Update Lang if PlatformLang is already set\r
936 // Update PlatformLang if Lang is already set\r
a24b4043 937 //\r
d7f79118
RN
938 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r
939 if (!EFI_ERROR (Status)) {\r
940 //\r
941 // Update Lang\r
942 //\r
943 VariableName = L"PlatformLang";\r
944 Data = GetVariableDataPtr (Variable.CurrPtr);\r
945 DataSize = Variable.CurrPtr->DataSize;\r
946 } else {\r
947 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable);\r
948 if (!EFI_ERROR (Status)) {\r
949 //\r
950 // Update PlatformLang\r
951 //\r
952 VariableName = L"Lang";\r
953 Data = GetVariableDataPtr (Variable.CurrPtr);\r
954 DataSize = Variable.CurrPtr->DataSize;\r
955 } else {\r
956 //\r
957 // Neither PlatformLang nor Lang is set, directly return\r
958 //\r
959 return;\r
960 }\r
961 }\r
962 }\r
963 \r
964 //\r
965 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
966 //\r
967 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
a24b4043 968\r
d7f79118 969 if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
a24b4043 970 //\r
d7f79118 971 // Update Lang when PlatformLangCodes/LangCodes were set.\r
a24b4043 972 //\r
d7f79118
RN
973 if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r
974 //\r
975 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
976 //\r
977 BestPlatformLang = VariableGetBestLanguage (mGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
978 if (BestPlatformLang != NULL) {\r
979 //\r
980 // Get the corresponding index in language codes.\r
981 //\r
982 Index = GetIndexFromSupportedLangCodes (mGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
a24b4043 983\r
d7f79118
RN
984 //\r
985 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
986 //\r
987 BestLang = GetLangFromSupportedLangCodes (mGlobal->LangCodes, Index, TRUE);\r
a24b4043 988\r
d7f79118
RN
989 //\r
990 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
991 //\r
992 FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable);\r
a24b4043 993\r
d7f79118 994 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
a24b4043 995\r
d7f79118 996 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
a24b4043 997\r
d7f79118
RN
998 ASSERT_EFI_ERROR(Status);\r
999 }\r
1000 }\r
a24b4043 1001\r
d7f79118 1002 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
a24b4043 1003 //\r
d7f79118 1004 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
a24b4043 1005 //\r
d7f79118
RN
1006 if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r
1007 //\r
1008 // When setting Lang, firstly get most matched language string from supported language codes.\r
1009 //\r
1010 BestLang = VariableGetBestLanguage (mGlobal->LangCodes, TRUE, Data, NULL);\r
1011 if (BestLang != NULL) {\r
1012 //\r
1013 // Get the corresponding index in language codes.\r
1014 //\r
1015 Index = GetIndexFromSupportedLangCodes (mGlobal->LangCodes, BestLang, TRUE);\r
a24b4043 1016\r
d7f79118
RN
1017 //\r
1018 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1019 //\r
1020 BestPlatformLang = GetLangFromSupportedLangCodes (mGlobal->PlatformLangCodes, Index, FALSE);\r
1021\r
1022 //\r
1023 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1024 //\r
1025 FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r
a24b4043 1026\r
d7f79118
RN
1027 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r
1028 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
a24b4043 1029\r
d7f79118
RN
1030 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
1031 ASSERT_EFI_ERROR (Status);\r
1032 }\r
1033 }\r
a24b4043 1034 }\r
a24b4043 1035}\r
1036\r
1037/**\r
1038 Update the variable region with Variable information. These are the same \r
1039 arguments as the EFI Variable services.\r
1040\r
1041 @param[in] VariableName Name of variable\r
1042\r
1043 @param[in] VendorGuid Guid of variable\r
1044\r
1045 @param[in] Data Variable data\r
1046\r
1047 @param[in] DataSize Size of data. 0 means delete\r
1048\r
b29a823d 1049 @param[in] Attributes Attribues of the variable\r
a24b4043 1050\r
1051 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1052\r
1053 @retval EFI_SUCCESS The update operation is success.\r
1054\r
1055 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
1056\r
1057**/\r
1058EFI_STATUS\r
1059EFIAPI\r
1060UpdateVariable (\r
1061 IN CHAR16 *VariableName,\r
1062 IN EFI_GUID *VendorGuid,\r
1063 IN VOID *Data,\r
1064 IN UINTN DataSize,\r
1065 IN UINT32 Attributes OPTIONAL,\r
1066 IN VARIABLE_POINTER_TRACK *Variable\r
1067 )\r
1068{\r
1069 EFI_STATUS Status;\r
1070 VARIABLE_HEADER *NextVariable;\r
1071 UINTN VarNameOffset;\r
1072 UINTN VarDataOffset;\r
1073 UINTN VarNameSize;\r
1074 UINTN VarSize;\r
1075 UINT8 State;\r
1076 BOOLEAN Reclaimed;\r
1077 VARIABLE_STORAGE_TYPE StorageType;\r
1078\r
1079 Reclaimed = FALSE;\r
1080\r
1081 if (Variable->CurrPtr != NULL) { \r
1082 //\r
1083 // Update/Delete existing variable\r
1084 //\r
1085 \r
1086 if (EfiAtRuntime ()) { \r
1087 //\r
1088 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
1089 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1090 // return EFI_WRITE_PROTECTED.\r
1091 //\r
1092 if (Variable->Type == Volatile) {\r
1093 return EFI_WRITE_PROTECTED;\r
1094 }\r
1095 //\r
1096 // Only variable have NV attribute can be updated/deleted in Runtime\r
1097 //\r
1098 if (!(Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
1099 return EFI_INVALID_PARAMETER; \r
1100 }\r
1101 }\r
1102 \r
1103 //\r
1104 // Setting a data variable with no access, or zero DataSize attributes\r
1105 // specified causes it to be deleted.\r
1106 //\r
1107 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1108 //\r
1109 // Found this variable in storage\r
1110 //\r
1111 State = Variable->CurrPtr->State;\r
1112 State &= VAR_DELETED;\r
1113\r
1114 Status = mGlobal->VariableStore[Variable->Type]->Write (\r
1115 mGlobal->VariableStore[Variable->Type],\r
1116 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
1117 sizeof (Variable->CurrPtr->State),\r
1118 &State\r
1119 );\r
1120 //\r
1121 // NOTE: Write operation at least can write data to memory cache\r
1122 // Discard file writing failure here.\r
1123 //\r
1124 return EFI_SUCCESS;\r
1125 }\r
1126 \r
1127 //\r
1128 // Found this variable in storage\r
1129 // If the variable is marked valid and the same data has been passed in\r
1130 // then return to the caller immediately.\r
1131 //\r
1132 if ((Variable->CurrPtr->DataSize == DataSize) &&\r
1133 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)\r
1134 ) {\r
1135 return EFI_SUCCESS;\r
1136 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1137 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1138 //\r
1139 // Mark the old variable as in delete transition\r
1140 //\r
1141 State = Variable->CurrPtr->State;\r
1142 State &= VAR_IN_DELETED_TRANSITION;\r
1143\r
1144 Status = mGlobal->VariableStore[Variable->Type]->Write (\r
1145 mGlobal->VariableStore[Variable->Type],\r
1146 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
1147 sizeof (Variable->CurrPtr->State),\r
1148 &State\r
1149 );\r
1150 //\r
1151 // NOTE: Write operation at least can write data to memory cache\r
1152 // Discard file writing failure here.\r
1153 //\r
1154 }\r
1155 } else {\r
1156 //\r
1157 // Create a new variable\r
1158 // \r
1159 \r
1160 //\r
1161 // Make sure we are trying to create a new variable.\r
1162 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
1163 //\r
1164 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1165 return EFI_NOT_FOUND;\r
1166 } \r
1167 //\r
1168 // Only variable have NV|RT attribute can be created in Runtime\r
1169 //\r
1170 if (EfiAtRuntime () &&\r
1171 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
1172 return EFI_INVALID_PARAMETER;\r
1173 } \r
1174 \r
1175 } \r
1176\r
1177 //\r
1178 // Function part - create a new variable and copy the data.\r
1179 // Both update a variable and create a variable will come here. \r
1180 // We can firstly write all the data in memory, then write them to file\r
1181 // This can reduce the times of write operation\r
1182 //\r
1183 \r
1184 NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;\r
1185\r
1186 NextVariable->StartId = VARIABLE_DATA;\r
1187 NextVariable->Attributes = Attributes;\r
1188 NextVariable->State = VAR_ADDED;\r
1189 NextVariable->Reserved = 0;\r
1190 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1191 VarNameSize = StrSize (VariableName);\r
1192 CopyMem (\r
1193 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1194 VariableName,\r
1195 VarNameSize\r
1196 );\r
1197 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1198 CopyMem (\r
1199 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1200 Data,\r
1201 DataSize\r
1202 );\r
1203 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1204 //\r
1205 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1206 // NextVariable->DataSize should not include pad size so that variable\r
1207 // service can get actual size in GetVariable\r
1208 //\r
1209 NextVariable->NameSize = (UINT32)VarNameSize;\r
1210 NextVariable->DataSize = (UINT32)DataSize;\r
1211\r
1212 //\r
1213 // The actual size of the variable that stores in storage should\r
1214 // include pad size.\r
1215 // VarDataOffset: offset from begin of current variable header\r
1216 //\r
1217 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
1218\r
1219 StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;\r
1220\r
1221 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r
1222 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r
1223 ) {\r
1224 if ((StorageType == NonVolatile) && EfiAtRuntime ()) {\r
1225 return EFI_OUT_OF_RESOURCES;\r
1226 }\r
1227 //\r
1228 // Perform garbage collection & reclaim operation\r
1229 //\r
1230 Status = Reclaim (StorageType, Variable->CurrPtr);\r
1231 if (EFI_ERROR (Status)) {\r
1232 //\r
1233 // Reclaim error\r
1234 // we cannot restore to original state, fetal error, report to user\r
1235 //\r
1236 DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));\r
1237 return Status;\r
1238 }\r
1239 //\r
1240 // If still no enough space, return out of resources\r
1241 //\r
1242 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r
1243 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r
1244 ) {\r
1245 return EFI_OUT_OF_RESOURCES;\r
1246 }\r
1247\r
1248 Reclaimed = TRUE;\r
1249 }\r
1250 Status = mGlobal->VariableStore[StorageType]->Write (\r
1251 mGlobal->VariableStore[StorageType],\r
1252 mGlobal->LastVariableOffset[StorageType],\r
1253 VarSize,\r
1254 NextVariable\r
1255 );\r
1256 //\r
1257 // NOTE: Write operation at least can write data to memory cache\r
1258 // Discard file writing failure here.\r
9071550e 1259 //\r
a24b4043 1260 mGlobal->LastVariableOffset[StorageType] += VarSize;\r
1261\r
1262 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1263 mGlobal->HwErrVariableTotalSize += VarSize;\r
1264 } else {\r
1265 mGlobal->CommonVariableTotalSize += VarSize;\r
1266 }\r
9071550e 1267\r
1268 //\r
a24b4043 1269 // Mark the old variable as deleted\r
9071550e 1270 //\r
a24b4043 1271 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1272 State = Variable->CurrPtr->State;\r
1273 State &= VAR_DELETED;\r
9071550e 1274\r
a24b4043 1275 Status = mGlobal->VariableStore[StorageType]->Write (\r
1276 mGlobal->VariableStore[StorageType],\r
1277 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
1278 sizeof (Variable->CurrPtr->State),\r
1279 &State\r
1280 );\r
1281 //\r
1282 // NOTE: Write operation at least can write data to memory cache\r
1283 // Discard file writing failure here.\r
1284 //\r
1285 }\r
1286 return EFI_SUCCESS;\r
9071550e 1287}\r
1288\r
1289EFI_STATUS\r
1290EFIAPI\r
3ffa0f1f 1291DuetGetVariable (\r
9071550e 1292 IN CHAR16 *VariableName,\r
1293 IN EFI_GUID *VendorGuid,\r
1294 OUT UINT32 *Attributes OPTIONAL,\r
1295 IN OUT UINTN *DataSize,\r
1296 OUT VOID *Data\r
1297 )\r
1298/*++\r
1299\r
1300Routine Description:\r
1301\r
1302 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
1303\r
1304Arguments:\r
1305\r
1306 VariableName Name of Variable to be found\r
1307 VendorGuid Variable vendor GUID\r
1308 Attributes OPTIONAL Attribute value of the variable found\r
1309 DataSize Size of Data found. If size is less than the\r
1310 data, this value contains the required size.\r
1311 Data Data pointer\r
1312\r
1313Returns:\r
1314\r
1315 EFI STATUS\r
1316\r
1317--*/\r
1318{\r
1319 VARIABLE_POINTER_TRACK Variable;\r
1320 UINTN VarDataSize;\r
1321 EFI_STATUS Status;\r
1322\r
1323 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1324 return EFI_INVALID_PARAMETER;\r
1325 }\r
1326\r
1327 //\r
1328 // Find existing variable\r
1329 //\r
1330 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1331\r
1332 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1333 return Status;\r
1334 }\r
1335 //\r
1336 // Get data size\r
1337 //\r
1338 VarDataSize = Variable.CurrPtr->DataSize;\r
1339 if (*DataSize >= VarDataSize) {\r
1340 if (Data == NULL) {\r
1341 return EFI_INVALID_PARAMETER;\r
1342 }\r
1343 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1344\r
1345 if (Attributes != NULL) {\r
1346 *Attributes = Variable.CurrPtr->Attributes;\r
1347 }\r
1348\r
1349 *DataSize = VarDataSize;\r
1350\r
1351 return EFI_SUCCESS;\r
1352 } else {\r
1353 *DataSize = VarDataSize;\r
1354 return EFI_BUFFER_TOO_SMALL;\r
1355 }\r
1356}\r
1357\r
1358EFI_STATUS\r
1359EFIAPI\r
1360GetNextVariableName (\r
1361 IN OUT UINTN *VariableNameSize,\r
1362 IN OUT CHAR16 *VariableName,\r
1363 IN OUT EFI_GUID *VendorGuid\r
1364 )\r
1365/*++\r
1366\r
1367Routine Description:\r
1368\r
1369 This code Finds the Next available variable\r
1370\r
1371Arguments:\r
1372\r
1373 VariableNameSize Size of the variable\r
1374 VariableName Pointer to variable name\r
1375 VendorGuid Variable Vendor Guid\r
1376\r
1377Returns:\r
1378\r
1379 EFI STATUS\r
1380\r
1381--*/\r
1382{\r
1383 VARIABLE_POINTER_TRACK Variable;\r
1384 UINTN VarNameSize;\r
1385 EFI_STATUS Status;\r
1386\r
1387 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1388 return EFI_INVALID_PARAMETER;\r
1389 }\r
1390\r
1391 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1392\r
1393 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1394 return Status;\r
1395 }\r
1396\r
1397 if (VariableName[0] != 0) {\r
1398 //\r
1399 // If variable name is not NULL, get next variable\r
1400 //\r
1401 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1402 }\r
1403\r
1404 while (TRUE) {\r
1405 //\r
1406 // The order we find variable is: 1). NonVolatile; 2). Volatile\r
1407 // If both volatile and non-volatile variable store are parsed,\r
1408 // return not found\r
1409 //\r
1410 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
1411 if (Variable.Type == Volatile) {\r
1412 //\r
1413 // Since we met the end of Volatile storage, we have parsed all the stores.\r
1414 //\r
1415 return EFI_NOT_FOUND;\r
1416 }\r
1417\r
1418 //\r
1419 // End of NonVolatile, continue to parse Volatile\r
1420 //\r
1421 Variable.Type = Volatile;\r
1422 Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);\r
1423 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);\r
1424\r
1425 Variable.CurrPtr = Variable.StartPtr;\r
1426 if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
1427 continue;\r
1428 }\r
1429 }\r
1430 //\r
1431 // Variable is found\r
1432 //\r
1433 if (IsValidVariableHeader (Variable.CurrPtr) &&\r
1434 ((Variable.CurrPtr->State == VAR_ADDED) ||\r
1435 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {\r
1436 if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1437 VarNameSize = Variable.CurrPtr->NameSize;\r
1438 if (VarNameSize <= *VariableNameSize) {\r
1439 CopyMem (\r
1440 VariableName,\r
1441 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
1442 VarNameSize\r
1443 );\r
1444 CopyMem (\r
1445 VendorGuid,\r
1446 &Variable.CurrPtr->VendorGuid,\r
1447 sizeof (EFI_GUID)\r
1448 );\r
1449 Status = EFI_SUCCESS;\r
1450 } else {\r
1451 Status = EFI_BUFFER_TOO_SMALL;\r
1452 }\r
1453\r
1454 *VariableNameSize = VarNameSize;\r
1455 return Status;\r
1456 }\r
1457 }\r
1458\r
1459 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1460 }\r
9071550e 1461}\r
1462\r
1463EFI_STATUS\r
1464EFIAPI\r
1465SetVariable (\r
1466 IN CHAR16 *VariableName,\r
1467 IN EFI_GUID *VendorGuid,\r
1468 IN UINT32 Attributes,\r
1469 IN UINTN DataSize,\r
1470 IN VOID *Data\r
1471 )\r
1472/*++\r
1473\r
1474Routine Description:\r
1475\r
1476 This code sets variable in storage blocks (Volatile or Non-Volatile)\r
1477\r
1478Arguments:\r
1479\r
1480 VariableName Name of Variable to be found\r
1481 VendorGuid Variable vendor GUID\r
1482 Attributes Attribute value of the variable found\r
1483 DataSize Size of Data found. If size is less than the\r
1484 data, this value contains the required size.\r
1485 Data Data pointer\r
1486\r
1487Returns:\r
1488 \r
1489 EFI_INVALID_PARAMETER - Invalid parameter\r
1490 EFI_SUCCESS - Set successfully\r
1491 EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r
1492 EFI_NOT_FOUND - Not found\r
1493 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r
1494 EFI_WRITE_PROTECTED - Variable is read-only\r
1495\r
1496--*/\r
1497{\r
1498 VARIABLE_POINTER_TRACK Variable;\r
1499 EFI_STATUS Status;\r
a24b4043 1500\r
9071550e 1501 //\r
1502 // Check input parameters\r
1503 // \r
9071550e 1504 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1505 return EFI_INVALID_PARAMETER;\r
1506 }\r
1507 \r
81c5255c 1508 if (DataSize != 0 && Data == NULL) {\r
1509 return EFI_INVALID_PARAMETER;\r
1510 }\r
1511\r
ff197263 1512 //\r
1513 // Not support authenticated variable write yet.\r
1514 //\r
1515 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1516 return EFI_INVALID_PARAMETER;\r
1517 }\r
1518\r
9071550e 1519 //\r
1520 // Make sure if runtime bit is set, boot service bit is set also\r
1521 //\r
1522 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1523 return EFI_INVALID_PARAMETER;\r
1524 }\r
1525 \r
9071550e 1526 //\r
1527 // The size of the VariableName, including the Unicode Null in bytes plus\r
d4577dce 1528 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
1529 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
9071550e 1530 //\r
1531 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
d4577dce 1532 if ((DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) || \r
1533 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
9071550e 1534 return EFI_INVALID_PARAMETER;\r
a24b4043 1535 }\r
1536 //\r
1537 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
1538 //\r
1539 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
1540 return EFI_INVALID_PARAMETER;\r
1541 }\r
9071550e 1542 } else {\r
d4577dce 1543 if ((DataSize > PcdGet32(PcdMaxVariableSize)) ||\r
1544 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxVariableSize))) {\r
9071550e 1545 return EFI_INVALID_PARAMETER;\r
1546 } \r
1547 } \r
e5653d94 1548\r
9071550e 1549 //\r
1550 // Check whether the input variable is already existed\r
1551 //\r
9071550e 1552 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1553\r
9071550e 1554 //\r
a24b4043 1555 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
9071550e 1556 //\r
a24b4043 1557 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
9071550e 1558\r
a24b4043 1559 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
9071550e 1560\r
a24b4043 1561 return Status;\r
9071550e 1562}\r
1563\r
9071550e 1564EFI_STATUS\r
1565EFIAPI\r
1566QueryVariableInfo (\r
1567 IN UINT32 Attributes,\r
1568 OUT UINT64 *MaximumVariableStorageSize,\r
1569 OUT UINT64 *RemainingVariableStorageSize,\r
1570 OUT UINT64 *MaximumVariableSize\r
1571 )\r
1572/*++\r
1573\r
1574Routine Description:\r
1575\r
1576 This code returns information about the EFI variables.\r
1577\r
1578Arguments:\r
1579\r
1580 Attributes Attributes bitmask to specify the type of variables\r
1581 on which to return information.\r
1582 MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1583 for the EFI variables associated with the attributes specified.\r
1584 RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
1585 for the EFI variables associated with the attributes specified.\r
1586 MaximumVariableSize Pointer to the maximum size of the individual EFI variables\r
1587 associated with the attributes specified.\r
1588\r
1589Returns:\r
1590\r
1591 EFI STATUS\r
1592 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r
1593 EFI_SUCCESS - Query successfully.\r
1594 EFI_UNSUPPORTED - The attribute is not supported on this platform.\r
1595\r
1596--*/\r
1597{\r
1598 VARIABLE_HEADER *Variable;\r
1599 VARIABLE_HEADER *NextVariable;\r
1600 UINT64 VariableSize;\r
1601 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
a24b4043 1602 UINT64 CommonVariableTotalSize;\r
1603 UINT64 HwErrVariableTotalSize;\r
1604\r
1605 CommonVariableTotalSize = 0;\r
1606 HwErrVariableTotalSize = 0;\r
9071550e 1607\r
1608 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
1609 return EFI_INVALID_PARAMETER;\r
1610 }\r
1611 \r
9071550e 1612 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
1613 //\r
1614 // Make sure the Attributes combination is supported by the platform.\r
1615 //\r
1616 return EFI_UNSUPPORTED; \r
ff197263 1617 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
9071550e 1618 //\r
1619 // Make sure if runtime bit is set, boot service bit is set also.\r
1620 //\r
1621 return EFI_INVALID_PARAMETER;\r
1622 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1623 //\r
1624 // Make sure RT Attribute is set if we are in Runtime phase.\r
1625 //\r
1626 return EFI_INVALID_PARAMETER;\r
a24b4043 1627 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1628 //\r
1629 // Make sure Hw Attribute is set with NV.\r
1630 //\r
1631 return EFI_INVALID_PARAMETER;\r
ff197263 1632 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1633 //\r
1634 // Not support authentiated variable write yet.\r
1635 //\r
1636 return EFI_UNSUPPORTED;\r
a24b4043 1637 }\r
9071550e 1638 \r
1639 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[\r
1640 (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile\r
1641 ];\r
1642 //\r
1643 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1644 // with the storage size (excluding the storage header size).\r
1645 //\r
1646 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
9071550e 1647\r
9071550e 1648 //\r
1649 // Harware error record variable needs larger size.\r
1650 //\r
a24b4043 1651 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
d4577dce 1652 *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r
1653 *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
a24b4043 1654 } else {\r
1655 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
d4577dce 1656 ASSERT (PcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
1657 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
a24b4043 1658 }\r
1659\r
1660 //\r
d4577dce 1661 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
a24b4043 1662 //\r
d4577dce 1663 *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
9071550e 1664 }\r
9071550e 1665 \r
1666 //\r
1667 // Point to the starting address of the variables.\r
1668 //\r
1669 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1670\r
1671 //\r
1672 // Now walk through the related variable store.\r
1673 //\r
a24b4043 1674 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
9071550e 1675 NextVariable = GetNextVariablePtr (Variable);\r
1676 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1677\r
1678 if (EfiAtRuntime ()) {\r
1679 //\r
1680 // we don't take the state of the variables in mind\r
1681 // when calculating RemainingVariableStorageSize,\r
1682 // since the space occupied by variables not marked with\r
1683 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
1684 //\r
a24b4043 1685 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1686 HwErrVariableTotalSize += VariableSize;\r
1687 } else {\r
1688 CommonVariableTotalSize += VariableSize;\r
1689 }\r
9071550e 1690 } else {\r
1691 //\r
1692 // Only care about Variables with State VAR_ADDED,because\r
1693 // the space not marked as VAR_ADDED is reclaimable now.\r
1694 //\r
a24b4043 1695 if ((Variable->State == VAR_ADDED) || (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1696 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1697 HwErrVariableTotalSize += VariableSize;\r
1698 } else {\r
1699 CommonVariableTotalSize += VariableSize;\r
1700 }\r
9071550e 1701 }\r
1702 }\r
1703\r
1704 //\r
1705 // Go to the next one\r
1706 //\r
1707 Variable = NextVariable;\r
1708 }\r
1709 \r
a24b4043 1710 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
1711 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
1712 } else {\r
1713 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
9071550e 1714 }\r
1715\r
1716 return EFI_SUCCESS;\r
1717}\r
9071550e 1718\r
1719EFI_STATUS\r
1720EFIAPI\r
1721VariableServiceInitialize (\r
1722 IN EFI_HANDLE ImageHandle,\r
1723 IN EFI_SYSTEM_TABLE *SystemTable\r
1724 )\r
1725/*++\r
1726\r
1727Routine Description:\r
1728 This function does initialization for variable services\r
1729\r
1730Arguments:\r
1731\r
1732 ImageHandle - The firmware allocated handle for the EFI image.\r
1733 SystemTable - A pointer to the EFI System Table.\r
1734\r
1735Returns:\r
1736\r
1737 Status code.\r
1738\r
1739 EFI_NOT_FOUND - Variable store area not found.\r
1740 EFI_SUCCESS - Variable services successfully initialized.\r
1741\r
1742--*/\r
1743{\r
1744 EFI_STATUS Status;\r
1745 EFI_HANDLE NewHandle;\r
1746 VS_DEV *Dev;\r
eb16e240 1747 EFI_PEI_HOB_POINTERS GuidHob;\r
a24b4043 1748 VARIABLE_HEADER *Variable;\r
9071550e 1749 VARIABLE_HEADER *NextVariable;\r
1750 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1751 EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntryData;\r
1752 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;\r
9071550e 1753 UINT64 BaseAddress;\r
1754 UINT64 Length;\r
1755 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
1756\r
1757 Status = gBS->AllocatePool (\r
1758 EfiRuntimeServicesData,\r
1759 (UINTN) sizeof (VARIABLE_GLOBAL),\r
7c04a679 1760 (VOID**) &mGlobal\r
9071550e 1761 );\r
1762 if (EFI_ERROR (Status)) {\r
1763 return Status;\r
1764 }\r
1765\r
a24b4043 1766 ZeroMem (mGlobal, (UINTN) sizeof (VARIABLE_GLOBAL));\r
1767\r
eb16e240 1768 GuidHob.Raw = GetHobList ();\r
1769 FlashMapEntryData = NULL;\r
1770 while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {\r
1771 FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);\r
9071550e 1772 if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r
1773 break;\r
1774 }\r
eb16e240 1775 GuidHob.Raw = GET_NEXT_HOB (GuidHob); \r
9071550e 1776 }\r
1777\r
eb16e240 1778 if (FlashMapEntryData == NULL) {\r
1779 DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));\r
9071550e 1780 Status = EFI_NOT_FOUND;\r
1781 return Status;\r
1782 }\r
be768885 1783 \r
394bbc59 1784 CopyMem(\r
1785 (VOID*)&VariableStoreEntry,\r
1786 (VOID*)&FlashMapEntryData->Entries[0],\r
1787 sizeof(EFI_FLASH_SUBAREA_ENTRY)\r
1788 );\r
9071550e 1789\r
1790 //\r
1791 // Mark the variable storage region of the FLASH as RUNTIME\r
1792 //\r
1793 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);\r
1794 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);\r
1795 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
1796 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
1797 if (EFI_ERROR (Status)) {\r
1798 Status = EFI_UNSUPPORTED;\r
1799 return Status;\r
1800 }\r
1801 Status = gDS->SetMemorySpaceAttributes (\r
1802 BaseAddress,\r
1803 Length,\r
1804 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
1805 );\r
1806 if (EFI_ERROR (Status)) {\r
1807 Status = EFI_UNSUPPORTED;\r
1808 return Status;\r
1809 }\r
7c04a679 1810\r
9071550e 1811 Status = FileStorageConstructor (\r
1812 &mGlobal->VariableStore[NonVolatile], \r
1813 &mGlobal->GoVirtualChildEvent[NonVolatile],\r
1814 VariableStoreEntry.Base,\r
1815 (UINT32) VariableStoreEntry.Length,\r
1816 FlashMapEntryData->VolumeId,\r
1817 FlashMapEntryData->FilePath\r
1818 );\r
1819 ASSERT_EFI_ERROR (Status);\r
1820\r
1821 //\r
1822 // Volatile Storage\r
1823 //\r
1824 Status = MemStorageConstructor (\r
1825 &mGlobal->VariableStore[Volatile],\r
1826 &mGlobal->GoVirtualChildEvent[Volatile],\r
1827 VOLATILE_VARIABLE_STORE_SIZE\r
1828 );\r
1829 ASSERT_EFI_ERROR (Status);\r
1830\r
1831 //\r
1832 // Scratch\r
1833 //\r
1834 Status = gBS->AllocatePool (\r
1835 EfiRuntimeServicesData,\r
1836 VARIABLE_SCRATCH_SIZE,\r
1837 &mGlobal->Scratch\r
1838 );\r
1839 ASSERT_EFI_ERROR (Status);\r
1840\r
1841 //\r
1842 // 1. NV Storage\r
1843 //\r
1844 Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);\r
1845 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
1846 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
1847 if (~VariableStoreHeader->Size == 0) {\r
1848 VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;\r
1849 }\r
1850 }\r
1851 //\r
1852 // Calculate LastVariableOffset\r
1853 //\r
a24b4043 1854 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1855 while (IsValidVariableHeader (Variable)) {\r
1856 UINTN VariableSize = 0;\r
1857 NextVariable = GetNextVariablePtr (Variable);\r
1858 VariableSize = NextVariable - Variable;\r
1859 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1860 mGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
1861 } else {\r
1862 mGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
1863 }\r
1864 Variable = NextVariable;\r
9071550e 1865 }\r
a24b4043 1866\r
1867 mGlobal->LastVariableOffset[NonVolatile] = (UINTN) Variable - (UINTN) VariableStoreHeader;\r
1868 mGlobal->VariableBase[NonVolatile] = VariableStoreHeader;\r
9071550e 1869\r
1870 //\r
1871 // Reclaim if remaining space is too small\r
1872 //\r
1873 if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {\r
1874 Status = Reclaim (NonVolatile, NULL);\r
1875 if (EFI_ERROR (Status)) {\r
1876 //\r
1877 // Reclaim error\r
1878 // we cannot restore to original state\r
1879 //\r
a24b4043 1880 DEBUG ((EFI_D_ERROR, "FSVariable: Reclaim error (fatal error) - %r\n", Status));\r
9071550e 1881 ASSERT_EFI_ERROR (Status);\r
1882 }\r
1883 }\r
1884 \r
1885 //\r
1886 // 2. Volatile Storage\r
1887 //\r
1888 Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);\r
1889 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
1890 mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev); \r
1891 mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);\r
1892 //\r
1893 // init store_header & body in memory.\r
1894 //\r
1895 mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);\r
1896 mGlobal->VariableStore[Volatile]->Write (\r
1897 mGlobal->VariableStore[Volatile],\r
1898 0,\r
1899 sizeof (VARIABLE_STORE_HEADER),\r
1900 &mStoreHeaderTemplate\r
1901 );\r
1902\r
1903\r
3ffa0f1f 1904 SystemTable->RuntimeServices->GetVariable = DuetGetVariable;\r
9071550e 1905 SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;\r
1906 SystemTable->RuntimeServices->SetVariable = SetVariable;\r
1907\r
9071550e 1908 SystemTable->RuntimeServices->QueryVariableInfo = QueryVariableInfo;\r
9071550e 1909\r
1910 //\r
1911 // Now install the Variable Runtime Architectural Protocol on a new handle\r
1912 //\r
1913 NewHandle = NULL;\r
1914 Status = gBS->InstallMultipleProtocolInterfaces (\r
1915 &NewHandle,\r
1916 &gEfiVariableArchProtocolGuid,\r
1917 NULL,\r
1918 &gEfiVariableWriteArchProtocolGuid,\r
1919 NULL,\r
1920 NULL\r
1921 );\r
1922 ASSERT_EFI_ERROR (Status);\r
1923\r
1924 return Status;\r
9071550e 1925}\r
1926\r
1927\r
1928\r
9071550e 1929VOID\r
1930EFIAPI\r
e56dd2ce 1931OnVirtualAddressChangeFsv (\r
9071550e 1932 IN EFI_EVENT Event,\r
1933 IN VOID *Context\r
1934 )\r
1935{\r
1936 UINTN Index;\r
1937\r
1938 for (Index = 0; Index < MaxType; Index++) {\r
1939 mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);\r
7c04a679 1940 EfiConvertPointer (0, (VOID**) &mGlobal->VariableStore[Index]);\r
9071550e 1941 EfiConvertPointer (0, &mGlobal->VariableBase[Index]);\r
1942 }\r
d7f79118
RN
1943 EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLangCodes);\r
1944 EfiConvertPointer (0, (VOID **) &mGlobal->LangCodes);\r
1945 EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLang);\r
9071550e 1946 EfiConvertPointer (0, &mGlobal->Scratch);\r
7c04a679 1947 EfiConvertPointer (0, (VOID**) &mGlobal);\r
9071550e 1948}\r