]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/FSVariable/FSVariable.c
DuetPkg FSVariable: Minor update to the Data parameter for GetVariable()
[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
e286e118 9Copyright (c) 2006 - 2016, 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
1340 //\r
1341 // Find existing variable\r
1342 //\r
1343 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1344\r
1345 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1346 return Status;\r
1347 }\r
1348 //\r
1349 // Get data size\r
1350 //\r
1351 VarDataSize = Variable.CurrPtr->DataSize;\r
1352 if (*DataSize >= VarDataSize) {\r
1353 if (Data == NULL) {\r
1354 return EFI_INVALID_PARAMETER;\r
1355 }\r
1356 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1357\r
1358 if (Attributes != NULL) {\r
1359 *Attributes = Variable.CurrPtr->Attributes;\r
1360 }\r
1361\r
1362 *DataSize = VarDataSize;\r
1363\r
1364 return EFI_SUCCESS;\r
1365 } else {\r
1366 *DataSize = VarDataSize;\r
1367 return EFI_BUFFER_TOO_SMALL;\r
1368 }\r
1369}\r
1370\r
1371EFI_STATUS\r
1372EFIAPI\r
1373GetNextVariableName (\r
1374 IN OUT UINTN *VariableNameSize,\r
1375 IN OUT CHAR16 *VariableName,\r
1376 IN OUT EFI_GUID *VendorGuid\r
1377 )\r
1378/*++\r
1379\r
1380Routine Description:\r
1381\r
1382 This code Finds the Next available variable\r
1383\r
1384Arguments:\r
1385\r
1386 VariableNameSize Size of the variable\r
1387 VariableName Pointer to variable name\r
1388 VendorGuid Variable Vendor Guid\r
1389\r
1390Returns:\r
1391\r
1392 EFI STATUS\r
1393\r
1394--*/\r
1395{\r
1396 VARIABLE_POINTER_TRACK Variable;\r
1397 UINTN VarNameSize;\r
1398 EFI_STATUS Status;\r
1399\r
1400 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1401 return EFI_INVALID_PARAMETER;\r
1402 }\r
1403\r
1404 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1405\r
1406 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1407 return Status;\r
1408 }\r
1409\r
1410 if (VariableName[0] != 0) {\r
1411 //\r
1412 // If variable name is not NULL, get next variable\r
1413 //\r
1414 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1415 }\r
1416\r
1417 while (TRUE) {\r
1418 //\r
1419 // The order we find variable is: 1). NonVolatile; 2). Volatile\r
1420 // If both volatile and non-volatile variable store are parsed,\r
1421 // return not found\r
1422 //\r
1423 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
1424 if (Variable.Type == Volatile) {\r
1425 //\r
1426 // Since we met the end of Volatile storage, we have parsed all the stores.\r
1427 //\r
1428 return EFI_NOT_FOUND;\r
1429 }\r
1430\r
1431 //\r
1432 // End of NonVolatile, continue to parse Volatile\r
1433 //\r
1434 Variable.Type = Volatile;\r
1435 Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);\r
1436 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);\r
1437\r
1438 Variable.CurrPtr = Variable.StartPtr;\r
1439 if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
1440 continue;\r
1441 }\r
1442 }\r
1443 //\r
1444 // Variable is found\r
1445 //\r
1446 if (IsValidVariableHeader (Variable.CurrPtr) &&\r
1447 ((Variable.CurrPtr->State == VAR_ADDED) ||\r
1448 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {\r
1449 if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1450 VarNameSize = Variable.CurrPtr->NameSize;\r
1451 if (VarNameSize <= *VariableNameSize) {\r
1452 CopyMem (\r
1453 VariableName,\r
1454 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
1455 VarNameSize\r
1456 );\r
1457 CopyMem (\r
1458 VendorGuid,\r
1459 &Variable.CurrPtr->VendorGuid,\r
1460 sizeof (EFI_GUID)\r
1461 );\r
1462 Status = EFI_SUCCESS;\r
1463 } else {\r
1464 Status = EFI_BUFFER_TOO_SMALL;\r
1465 }\r
1466\r
1467 *VariableNameSize = VarNameSize;\r
1468 return Status;\r
1469 }\r
1470 }\r
1471\r
1472 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1473 }\r
9071550e 1474}\r
1475\r
1476EFI_STATUS\r
1477EFIAPI\r
1478SetVariable (\r
1479 IN CHAR16 *VariableName,\r
1480 IN EFI_GUID *VendorGuid,\r
1481 IN UINT32 Attributes,\r
1482 IN UINTN DataSize,\r
1483 IN VOID *Data\r
1484 )\r
1485/*++\r
1486\r
1487Routine Description:\r
1488\r
1489 This code sets variable in storage blocks (Volatile or Non-Volatile)\r
1490\r
1491Arguments:\r
1492\r
1493 VariableName Name of Variable to be found\r
1494 VendorGuid Variable vendor GUID\r
1495 Attributes Attribute value of the variable found\r
1496 DataSize Size of Data found. If size is less than the\r
1497 data, this value contains the required size.\r
1498 Data Data pointer\r
1499\r
1500Returns:\r
1501 \r
1502 EFI_INVALID_PARAMETER - Invalid parameter\r
1503 EFI_SUCCESS - Set successfully\r
1504 EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r
1505 EFI_NOT_FOUND - Not found\r
1506 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r
1507 EFI_WRITE_PROTECTED - Variable is read-only\r
1508\r
1509--*/\r
1510{\r
1511 VARIABLE_POINTER_TRACK Variable;\r
1512 EFI_STATUS Status;\r
a24b4043 1513\r
9071550e 1514 //\r
1515 // Check input parameters\r
1516 // \r
9071550e 1517 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1518 return EFI_INVALID_PARAMETER;\r
1519 }\r
1520 \r
81c5255c 1521 if (DataSize != 0 && Data == NULL) {\r
1522 return EFI_INVALID_PARAMETER;\r
1523 }\r
1524\r
ff197263 1525 //\r
1526 // Not support authenticated variable write yet.\r
1527 //\r
1528 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1529 return EFI_INVALID_PARAMETER;\r
1530 }\r
1531\r
9071550e 1532 //\r
1533 // Make sure if runtime bit is set, boot service bit is set also\r
1534 //\r
1535 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1536 return EFI_INVALID_PARAMETER;\r
1537 }\r
1538 \r
9071550e 1539 //\r
1540 // The size of the VariableName, including the Unicode Null in bytes plus\r
d4577dce 1541 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
1542 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
9071550e 1543 //\r
1544 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
d4577dce 1545 if ((DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) || \r
1546 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
9071550e 1547 return EFI_INVALID_PARAMETER;\r
a24b4043 1548 }\r
1549 //\r
1550 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
1551 //\r
1552 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
1553 return EFI_INVALID_PARAMETER;\r
1554 }\r
9071550e 1555 } else {\r
d4577dce 1556 if ((DataSize > PcdGet32(PcdMaxVariableSize)) ||\r
1557 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxVariableSize))) {\r
9071550e 1558 return EFI_INVALID_PARAMETER;\r
1559 } \r
1560 } \r
e5653d94 1561\r
9071550e 1562 //\r
1563 // Check whether the input variable is already existed\r
1564 //\r
9071550e 1565 Status = FindVariable (VariableName, VendorGuid, &Variable);\r
1566\r
9071550e 1567 //\r
a24b4043 1568 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
9071550e 1569 //\r
a24b4043 1570 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
9071550e 1571\r
a24b4043 1572 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
9071550e 1573\r
a24b4043 1574 return Status;\r
9071550e 1575}\r
1576\r
9071550e 1577EFI_STATUS\r
1578EFIAPI\r
1579QueryVariableInfo (\r
1580 IN UINT32 Attributes,\r
1581 OUT UINT64 *MaximumVariableStorageSize,\r
1582 OUT UINT64 *RemainingVariableStorageSize,\r
1583 OUT UINT64 *MaximumVariableSize\r
1584 )\r
1585/*++\r
1586\r
1587Routine Description:\r
1588\r
1589 This code returns information about the EFI variables.\r
1590\r
1591Arguments:\r
1592\r
1593 Attributes Attributes bitmask to specify the type of variables\r
1594 on which to return information.\r
1595 MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1596 for the EFI variables associated with the attributes specified.\r
1597 RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
1598 for the EFI variables associated with the attributes specified.\r
1599 MaximumVariableSize Pointer to the maximum size of the individual EFI variables\r
1600 associated with the attributes specified.\r
1601\r
1602Returns:\r
1603\r
1604 EFI STATUS\r
1605 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r
1606 EFI_SUCCESS - Query successfully.\r
1607 EFI_UNSUPPORTED - The attribute is not supported on this platform.\r
1608\r
1609--*/\r
1610{\r
1611 VARIABLE_HEADER *Variable;\r
1612 VARIABLE_HEADER *NextVariable;\r
1613 UINT64 VariableSize;\r
1614 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
a24b4043 1615 UINT64 CommonVariableTotalSize;\r
1616 UINT64 HwErrVariableTotalSize;\r
1617\r
1618 CommonVariableTotalSize = 0;\r
1619 HwErrVariableTotalSize = 0;\r
9071550e 1620\r
1621 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
1622 return EFI_INVALID_PARAMETER;\r
1623 }\r
1624 \r
9071550e 1625 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
1626 //\r
1627 // Make sure the Attributes combination is supported by the platform.\r
1628 //\r
1629 return EFI_UNSUPPORTED; \r
ff197263 1630 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
9071550e 1631 //\r
1632 // Make sure if runtime bit is set, boot service bit is set also.\r
1633 //\r
1634 return EFI_INVALID_PARAMETER;\r
1635 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1636 //\r
1637 // Make sure RT Attribute is set if we are in Runtime phase.\r
1638 //\r
1639 return EFI_INVALID_PARAMETER;\r
a24b4043 1640 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1641 //\r
1642 // Make sure Hw Attribute is set with NV.\r
1643 //\r
1644 return EFI_INVALID_PARAMETER;\r
ff197263 1645 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1646 //\r
1647 // Not support authentiated variable write yet.\r
1648 //\r
1649 return EFI_UNSUPPORTED;\r
a24b4043 1650 }\r
9071550e 1651 \r
1652 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[\r
1653 (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile\r
1654 ];\r
1655 //\r
1656 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1657 // with the storage size (excluding the storage header size).\r
1658 //\r
1659 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
9071550e 1660\r
9071550e 1661 //\r
1662 // Harware error record variable needs larger size.\r
1663 //\r
a24b4043 1664 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
d4577dce 1665 *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r
1666 *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
a24b4043 1667 } else {\r
1668 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
d4577dce 1669 ASSERT (PcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
1670 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
a24b4043 1671 }\r
1672\r
1673 //\r
d4577dce 1674 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
a24b4043 1675 //\r
d4577dce 1676 *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
9071550e 1677 }\r
9071550e 1678 \r
1679 //\r
1680 // Point to the starting address of the variables.\r
1681 //\r
1682 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1683\r
1684 //\r
1685 // Now walk through the related variable store.\r
1686 //\r
a24b4043 1687 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
9071550e 1688 NextVariable = GetNextVariablePtr (Variable);\r
1689 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1690\r
1691 if (EfiAtRuntime ()) {\r
1692 //\r
1693 // we don't take the state of the variables in mind\r
1694 // when calculating RemainingVariableStorageSize,\r
1695 // since the space occupied by variables not marked with\r
1696 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
1697 //\r
452f0207 1698 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
a24b4043 1699 HwErrVariableTotalSize += VariableSize;\r
1700 } else {\r
1701 CommonVariableTotalSize += VariableSize;\r
1702 }\r
9071550e 1703 } else {\r
1704 //\r
1705 // Only care about Variables with State VAR_ADDED,because\r
1706 // the space not marked as VAR_ADDED is reclaimable now.\r
1707 //\r
a24b4043 1708 if ((Variable->State == VAR_ADDED) || (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
452f0207 1709 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
a24b4043 1710 HwErrVariableTotalSize += VariableSize;\r
1711 } else {\r
1712 CommonVariableTotalSize += VariableSize;\r
1713 }\r
9071550e 1714 }\r
1715 }\r
1716\r
1717 //\r
1718 // Go to the next one\r
1719 //\r
1720 Variable = NextVariable;\r
1721 }\r
1722 \r
a24b4043 1723 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
1724 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
1725 } else {\r
1726 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
9071550e 1727 }\r
1728\r
1729 return EFI_SUCCESS;\r
1730}\r
9071550e 1731\r
1732EFI_STATUS\r
1733EFIAPI\r
1734VariableServiceInitialize (\r
1735 IN EFI_HANDLE ImageHandle,\r
1736 IN EFI_SYSTEM_TABLE *SystemTable\r
1737 )\r
1738/*++\r
1739\r
1740Routine Description:\r
1741 This function does initialization for variable services\r
1742\r
1743Arguments:\r
1744\r
1745 ImageHandle - The firmware allocated handle for the EFI image.\r
1746 SystemTable - A pointer to the EFI System Table.\r
1747\r
1748Returns:\r
1749\r
1750 Status code.\r
1751\r
1752 EFI_NOT_FOUND - Variable store area not found.\r
1753 EFI_SUCCESS - Variable services successfully initialized.\r
1754\r
1755--*/\r
1756{\r
1757 EFI_STATUS Status;\r
1758 EFI_HANDLE NewHandle;\r
1759 VS_DEV *Dev;\r
eb16e240 1760 EFI_PEI_HOB_POINTERS GuidHob;\r
a24b4043 1761 VARIABLE_HEADER *Variable;\r
9071550e 1762 VARIABLE_HEADER *NextVariable;\r
1763 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1764 EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntryData;\r
1765 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;\r
9071550e 1766 UINT64 BaseAddress;\r
1767 UINT64 Length;\r
1768 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
1769\r
1770 Status = gBS->AllocatePool (\r
1771 EfiRuntimeServicesData,\r
1772 (UINTN) sizeof (VARIABLE_GLOBAL),\r
7c04a679 1773 (VOID**) &mGlobal\r
9071550e 1774 );\r
1775 if (EFI_ERROR (Status)) {\r
1776 return Status;\r
1777 }\r
1778\r
a24b4043 1779 ZeroMem (mGlobal, (UINTN) sizeof (VARIABLE_GLOBAL));\r
1780\r
eb16e240 1781 GuidHob.Raw = GetHobList ();\r
1782 FlashMapEntryData = NULL;\r
1783 while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {\r
1784 FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);\r
9071550e 1785 if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r
1786 break;\r
1787 }\r
eb16e240 1788 GuidHob.Raw = GET_NEXT_HOB (GuidHob); \r
9071550e 1789 }\r
1790\r
eb16e240 1791 if (FlashMapEntryData == NULL) {\r
1792 DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));\r
9071550e 1793 Status = EFI_NOT_FOUND;\r
1794 return Status;\r
1795 }\r
be768885 1796 \r
394bbc59 1797 CopyMem(\r
1798 (VOID*)&VariableStoreEntry,\r
1799 (VOID*)&FlashMapEntryData->Entries[0],\r
1800 sizeof(EFI_FLASH_SUBAREA_ENTRY)\r
1801 );\r
9071550e 1802\r
1803 //\r
1804 // Mark the variable storage region of the FLASH as RUNTIME\r
1805 //\r
1806 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);\r
1807 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);\r
1808 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
1809 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
1810 if (EFI_ERROR (Status)) {\r
1811 Status = EFI_UNSUPPORTED;\r
1812 return Status;\r
1813 }\r
1814 Status = gDS->SetMemorySpaceAttributes (\r
1815 BaseAddress,\r
1816 Length,\r
1817 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
1818 );\r
1819 if (EFI_ERROR (Status)) {\r
1820 Status = EFI_UNSUPPORTED;\r
1821 return Status;\r
1822 }\r
7c04a679 1823\r
9071550e 1824 Status = FileStorageConstructor (\r
1825 &mGlobal->VariableStore[NonVolatile], \r
1826 &mGlobal->GoVirtualChildEvent[NonVolatile],\r
1827 VariableStoreEntry.Base,\r
1828 (UINT32) VariableStoreEntry.Length,\r
1829 FlashMapEntryData->VolumeId,\r
1830 FlashMapEntryData->FilePath\r
1831 );\r
1832 ASSERT_EFI_ERROR (Status);\r
1833\r
1834 //\r
1835 // Volatile Storage\r
1836 //\r
1837 Status = MemStorageConstructor (\r
1838 &mGlobal->VariableStore[Volatile],\r
1839 &mGlobal->GoVirtualChildEvent[Volatile],\r
1840 VOLATILE_VARIABLE_STORE_SIZE\r
1841 );\r
1842 ASSERT_EFI_ERROR (Status);\r
1843\r
1844 //\r
1845 // Scratch\r
1846 //\r
1847 Status = gBS->AllocatePool (\r
1848 EfiRuntimeServicesData,\r
1849 VARIABLE_SCRATCH_SIZE,\r
1850 &mGlobal->Scratch\r
1851 );\r
1852 ASSERT_EFI_ERROR (Status);\r
1853\r
1854 //\r
1855 // 1. NV Storage\r
1856 //\r
1857 Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);\r
1858 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
1859 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
1860 if (~VariableStoreHeader->Size == 0) {\r
1861 VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;\r
1862 }\r
1863 }\r
1864 //\r
1865 // Calculate LastVariableOffset\r
1866 //\r
a24b4043 1867 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1868 while (IsValidVariableHeader (Variable)) {\r
1869 UINTN VariableSize = 0;\r
1870 NextVariable = GetNextVariablePtr (Variable);\r
1871 VariableSize = NextVariable - Variable;\r
1872 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1873 mGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
1874 } else {\r
1875 mGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
1876 }\r
1877 Variable = NextVariable;\r
9071550e 1878 }\r
a24b4043 1879\r
1880 mGlobal->LastVariableOffset[NonVolatile] = (UINTN) Variable - (UINTN) VariableStoreHeader;\r
1881 mGlobal->VariableBase[NonVolatile] = VariableStoreHeader;\r
9071550e 1882\r
1883 //\r
1884 // Reclaim if remaining space is too small\r
1885 //\r
1886 if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {\r
1887 Status = Reclaim (NonVolatile, NULL);\r
1888 if (EFI_ERROR (Status)) {\r
1889 //\r
1890 // Reclaim error\r
1891 // we cannot restore to original state\r
1892 //\r
a24b4043 1893 DEBUG ((EFI_D_ERROR, "FSVariable: Reclaim error (fatal error) - %r\n", Status));\r
9071550e 1894 ASSERT_EFI_ERROR (Status);\r
1895 }\r
1896 }\r
1897 \r
1898 //\r
1899 // 2. Volatile Storage\r
1900 //\r
1901 Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);\r
1902 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
1903 mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev); \r
1904 mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);\r
1905 //\r
1906 // init store_header & body in memory.\r
1907 //\r
1908 mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);\r
1909 mGlobal->VariableStore[Volatile]->Write (\r
1910 mGlobal->VariableStore[Volatile],\r
1911 0,\r
1912 sizeof (VARIABLE_STORE_HEADER),\r
1913 &mStoreHeaderTemplate\r
1914 );\r
1915\r
1916\r
3ffa0f1f 1917 SystemTable->RuntimeServices->GetVariable = DuetGetVariable;\r
9071550e 1918 SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;\r
1919 SystemTable->RuntimeServices->SetVariable = SetVariable;\r
1920\r
9071550e 1921 SystemTable->RuntimeServices->QueryVariableInfo = QueryVariableInfo;\r
9071550e 1922\r
1923 //\r
1924 // Now install the Variable Runtime Architectural Protocol on a new handle\r
1925 //\r
1926 NewHandle = NULL;\r
1927 Status = gBS->InstallMultipleProtocolInterfaces (\r
1928 &NewHandle,\r
1929 &gEfiVariableArchProtocolGuid,\r
1930 NULL,\r
1931 &gEfiVariableWriteArchProtocolGuid,\r
1932 NULL,\r
1933 NULL\r
1934 );\r
1935 ASSERT_EFI_ERROR (Status);\r
1936\r
1937 return Status;\r
9071550e 1938}\r
1939\r
1940\r
1941\r
9071550e 1942VOID\r
1943EFIAPI\r
e56dd2ce 1944OnVirtualAddressChangeFsv (\r
9071550e 1945 IN EFI_EVENT Event,\r
1946 IN VOID *Context\r
1947 )\r
1948{\r
1949 UINTN Index;\r
1950\r
1951 for (Index = 0; Index < MaxType; Index++) {\r
1952 mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);\r
7c04a679 1953 EfiConvertPointer (0, (VOID**) &mGlobal->VariableStore[Index]);\r
9071550e 1954 EfiConvertPointer (0, &mGlobal->VariableBase[Index]);\r
1955 }\r
d7f79118
RN
1956 EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLangCodes);\r
1957 EfiConvertPointer (0, (VOID **) &mGlobal->LangCodes);\r
1958 EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLang);\r
9071550e 1959 EfiConvertPointer (0, &mGlobal->Scratch);\r
7c04a679 1960 EfiConvertPointer (0, (VOID**) &mGlobal);\r
9071550e 1961}\r