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