]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / EmuRuntimeDxe / EmuVariable.c
CommitLineData
504214c4 1/** @file\r
8d3a5c82 2\r
504214c4
LG
3 Emulation Variable services operate on the runtime volatile memory.\r
4 The nonvolatile variable space doesn't exist.\r
5\r
051bf6e0 6Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 7This program and the accompanying materials\r
8d3a5c82 8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
504214c4 15**/\r
8d3a5c82 16\r
17#include "Variable.h"\r
18\r
1794e506 19///\r
20/// Don't use module globals after the SetVirtualAddress map is signaled\r
21///\r
8d3a5c82 22ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;\r
23\r
e4ddc008 24VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
1794e506 25\r
72399dae 26///\r
27/// The size of a 3 character ISO639 language code.\r
28///\r
29#define ISO_639_2_ENTRY_SIZE 3\r
30\r
31/**\r
d1102dba 32 Update the variable region with Variable information. These are the same\r
72399dae 33 arguments as the EFI Variable services.\r
34\r
35 @param[in] VariableName Name of variable\r
36\r
37 @param[in] VendorGuid Guid of variable\r
38\r
39 @param[in] Data Variable data\r
40\r
41 @param[in] DataSize Size of data. 0 means delete\r
42\r
d1102dba 43 @param[in] Attributes Attribues of the variable\r
72399dae 44\r
45 @param[in] Variable The variable information which is used to keep track of variable usage.\r
46\r
47 @retval EFI_SUCCESS The update operation is success.\r
48\r
49 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
50\r
51**/\r
52EFI_STATUS\r
53EFIAPI\r
54UpdateVariable (\r
55 IN CHAR16 *VariableName,\r
56 IN EFI_GUID *VendorGuid,\r
57 IN VOID *Data,\r
58 IN UINTN DataSize,\r
59 IN UINT32 Attributes OPTIONAL,\r
60 IN VARIABLE_POINTER_TRACK *Variable\r
61 );\r
62\r
63/**\r
64 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
65\r
66 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
67 If VariableName is an empty string, then we just return the first\r
68 qualified variable without comparing VariableName and VendorGuid.\r
69 Otherwise, VariableName and VendorGuid are compared.\r
70\r
71 @param VariableName Name of the variable to be found.\r
72 @param VendorGuid Vendor GUID to be found.\r
73 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
74 including the range searched and the target position.\r
75 @param Global Pointer to VARIABLE_GLOBAL structure, including\r
76 base of volatile variable storage area, base of\r
77 NV variable storage area, and a lock.\r
78\r
79 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
80 VendorGuid is NULL.\r
81 @retval EFI_SUCCESS Variable successfully found.\r
82 @retval EFI_NOT_FOUND Variable not found.\r
83\r
84**/\r
85EFI_STATUS\r
86FindVariable (\r
87 IN CHAR16 *VariableName,\r
88 IN EFI_GUID *VendorGuid,\r
89 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
90 IN VARIABLE_GLOBAL *Global\r
91 );\r
92\r
1794e506 93/**\r
94 Acquires lock only at boot time. Simply returns at runtime.\r
95\r
96 This is a temperary function which will be removed when\r
97 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
98 Runtimer driver in RT phase.\r
99 It calls EfiAcquireLock() at boot time, and simply returns\r
100 at runtime\r
101\r
102 @param Lock A pointer to the lock to acquire\r
103\r
104**/\r
8d3a5c82 105VOID\r
106AcquireLockOnlyAtBootTime (\r
107 IN EFI_LOCK *Lock\r
108 )\r
109{\r
110 if (!EfiAtRuntime ()) {\r
111 EfiAcquireLock (Lock);\r
112 }\r
113}\r
114\r
1794e506 115/**\r
116 Releases lock only at boot time. Simply returns at runtime.\r
117\r
118 This is a temperary function which will be removed when\r
119 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
120 Runtimer driver in RT phase.\r
121 It calls EfiReleaseLock() at boot time, and simply returns\r
122 at runtime\r
123\r
124 @param Lock A pointer to the lock to release\r
125\r
126**/\r
8d3a5c82 127VOID\r
128ReleaseLockOnlyAtBootTime (\r
129 IN EFI_LOCK *Lock\r
130 )\r
131{\r
132 if (!EfiAtRuntime ()) {\r
133 EfiReleaseLock (Lock);\r
134 }\r
135}\r
136\r
1794e506 137/**\r
138 Gets pointer to the variable data.\r
8d3a5c82 139\r
1794e506 140 This function gets the pointer to the variable data according\r
141 to the input pointer to the variable header.\r
8d3a5c82 142\r
1794e506 143 @param Variable Pointer to the variable header.\r
8d3a5c82 144\r
1794e506 145 @return Pointer to variable data\r
8d3a5c82 146\r
1794e506 147**/\r
148UINT8 *\r
149GetVariableDataPtr (\r
150 IN VARIABLE_HEADER *Variable\r
151 )\r
8d3a5c82 152{\r
153 if (Variable->StartId != VARIABLE_DATA) {\r
154 return NULL;\r
155 }\r
156 //\r
157 // Be careful about pad size for alignment\r
158 //\r
159 return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
160}\r
161\r
1794e506 162/**\r
02328a57 163 Gets pointer to header of the next potential variable.\r
8d3a5c82 164\r
02328a57 165 This function gets the pointer to the next potential variable header\r
166 according to the input point to the variable header. The return value\r
167 is not a valid variable if the input variable was the last variable\r
168 in the variabl store.\r
8d3a5c82 169\r
1794e506 170 @param Variable Pointer to header of the next variable\r
8d3a5c82 171\r
1794e506 172 @return Pointer to next variable header.\r
02328a57 173 @retval NULL Input was not a valid variable header.\r
8d3a5c82 174\r
1794e506 175**/\r
176VARIABLE_HEADER *\r
02328a57 177GetNextPotentialVariablePtr (\r
1794e506 178 IN VARIABLE_HEADER *Variable\r
179 )\r
8d3a5c82 180{\r
181 VARIABLE_HEADER *VarHeader;\r
182\r
183 if (Variable->StartId != VARIABLE_DATA) {\r
184 return NULL;\r
185 }\r
186 //\r
187 // Be careful about pad size for alignment\r
188 //\r
f68af18e 189 VarHeader = (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
8d3a5c82 190\r
02328a57 191 return VarHeader;\r
192}\r
193\r
194/**\r
195 Gets pointer to header of the next variable.\r
196\r
197 This function gets the pointer to the next variable header according\r
198 to the input point to the variable header.\r
199\r
200 @param Variable Pointer to header of the next variable\r
201\r
202 @return Pointer to next variable header.\r
203\r
204**/\r
205VARIABLE_HEADER *\r
206GetNextVariablePtr (\r
207 IN VARIABLE_HEADER *Variable\r
208 )\r
209{\r
210 VARIABLE_HEADER *VarHeader;\r
211\r
212 VarHeader = GetNextPotentialVariablePtr (Variable);\r
213\r
214 if ((VarHeader == NULL) || (VarHeader->StartId != VARIABLE_DATA)) {\r
8d3a5c82 215 return NULL;\r
216 }\r
217\r
218 return VarHeader;\r
219}\r
220\r
02328a57 221/**\r
222 Updates LastVariableOffset variable for the given variable store.\r
223\r
224 LastVariableOffset points to the offset to use for the next variable\r
225 when updating the variable store.\r
226\r
227 @param[in] VariableStore Pointer to the start of the variable store\r
228 @param[out] LastVariableOffset Offset to put the next new variable in\r
229\r
230**/\r
231VOID\r
232InitializeLocationForLastVariableOffset (\r
233 IN VARIABLE_STORE_HEADER *VariableStore,\r
234 OUT UINTN *LastVariableOffset\r
235 )\r
236{\r
237 VARIABLE_HEADER *VarHeader;\r
238\r
239 *LastVariableOffset = sizeof (VARIABLE_STORE_HEADER);\r
240 VarHeader = (VARIABLE_HEADER*) ((UINT8*)VariableStore + *LastVariableOffset);\r
241 while (VarHeader->StartId == VARIABLE_DATA) {\r
242 VarHeader = GetNextPotentialVariablePtr (VarHeader);\r
243\r
244 if (VarHeader != NULL) {\r
245 *LastVariableOffset = (UINTN) VarHeader - (UINTN) VariableStore;\r
246 } else {\r
247 return;\r
248 }\r
249 }\r
250}\r
251\r
1794e506 252/**\r
253 Gets pointer to the end of the variable storage area.\r
8d3a5c82 254\r
1794e506 255 This function gets pointer to the end of the variable storage\r
256 area, according to the input variable store header.\r
8d3a5c82 257\r
1794e506 258 @param VolHeader Pointer to the variale store header\r
8d3a5c82 259\r
1794e506 260 @return Pointer to the end of the variable storage area.\r
8d3a5c82 261\r
1794e506 262**/\r
263VARIABLE_HEADER *\r
264GetEndPointer (\r
265 IN VARIABLE_STORE_HEADER *VolHeader\r
266 )\r
8d3a5c82 267{\r
268 //\r
269 // The end of variable store\r
270 //\r
fb849d0d 271 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VolHeader + VolHeader->Size);\r
8d3a5c82 272}\r
273\r
e4ddc008 274/**\r
d1102dba 275 Routine used to track statistical information about variable usage.\r
e4ddc008 276 The data is stored in the EFI system table so it can be accessed later.\r
d1102dba 277 VariableInfo.efi can dump out the table. Only Boot Services variable\r
e4ddc008 278 accesses are tracked by this code. The PcdVariableCollectStatistics\r
d1102dba 279 build flag controls if this feature is enabled.\r
e4ddc008 280\r
d1102dba 281 A read that hits in the cache will have Read and Cache true for\r
e4ddc008 282 the transaction. Data is allocated by this routine, but never\r
283 freed.\r
284\r
285 @param[in] VariableName Name of the Variable to track\r
286 @param[in] VendorGuid Guid of the Variable to track\r
287 @param[in] Volatile TRUE if volatile FALSE if non-volatile\r
288 @param[in] Read TRUE if GetVariable() was called\r
289 @param[in] Write TRUE if SetVariable() was called\r
290 @param[in] Delete TRUE if deleted via SetVariable()\r
291 @param[in] Cache TRUE for a cache hit.\r
292\r
293**/\r
294VOID\r
295UpdateVariableInfo (\r
296 IN CHAR16 *VariableName,\r
297 IN EFI_GUID *VendorGuid,\r
298 IN BOOLEAN Volatile,\r
299 IN BOOLEAN Read,\r
300 IN BOOLEAN Write,\r
301 IN BOOLEAN Delete,\r
302 IN BOOLEAN Cache\r
303 )\r
304{\r
305 VARIABLE_INFO_ENTRY *Entry;\r
306\r
307 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
308\r
309 if (EfiAtRuntime ()) {\r
310 // Don't collect statistics at runtime\r
311 return;\r
312 }\r
313\r
314 if (gVariableInfo == NULL) {\r
315 //\r
316 // on the first call allocate a entry and place a pointer to it in\r
317 // the EFI System Table\r
318 //\r
319 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
320 ASSERT (gVariableInfo != NULL);\r
321\r
322 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
6e1e5405 323 gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r
e4ddc008 324 ASSERT (gVariableInfo->Name != NULL);\r
568a5119 325 StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
e4ddc008 326 gVariableInfo->Volatile = Volatile;\r
327\r
328 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
329 }\r
330\r
d1102dba 331\r
e4ddc008 332 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
333 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
334 if (StrCmp (VariableName, Entry->Name) == 0) {\r
335 if (Read) {\r
336 Entry->ReadCount++;\r
337 }\r
338 if (Write) {\r
339 Entry->WriteCount++;\r
340 }\r
341 if (Delete) {\r
342 Entry->DeleteCount++;\r
343 }\r
344 if (Cache) {\r
345 Entry->CacheCount++;\r
346 }\r
347\r
348 return;\r
349 }\r
350 }\r
351\r
352 if (Entry->Next == NULL) {\r
353 //\r
354 // If the entry is not in the table add it.\r
355 // Next iteration of the loop will fill in the data\r
356 //\r
357 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
358 ASSERT (Entry->Next != NULL);\r
359\r
360 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
6e1e5405 361 Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r
e4ddc008 362 ASSERT (Entry->Next->Name != NULL);\r
568a5119 363 StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
e4ddc008 364 Entry->Next->Volatile = Volatile;\r
365 }\r
366\r
367 }\r
368 }\r
369}\r
370\r
72399dae 371/**\r
372 Get index from supported language codes according to language string.\r
373\r
374 This code is used to get corresponding index in supported language codes. It can handle\r
0254efc0 375 RFC4646 and ISO639 language tags.\r
72399dae 376 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
0254efc0 377 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
72399dae 378\r
379 For example:\r
380 SupportedLang = "engfraengfra"\r
381 Lang = "eng"\r
382 Iso639Language = TRUE\r
383 The return value is "0".\r
384 Another example:\r
385 SupportedLang = "en;fr;en-US;fr-FR"\r
386 Lang = "fr-FR"\r
387 Iso639Language = FALSE\r
388 The return value is "3".\r
389\r
390 @param SupportedLang Platform supported language codes.\r
391 @param Lang Configured language.\r
0254efc0 392 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 393\r
394 @retval the index of language in the language codes.\r
395\r
396**/\r
397UINTN\r
72399dae 398GetIndexFromSupportedLangCodes(\r
399 IN CHAR8 *SupportedLang,\r
400 IN CHAR8 *Lang,\r
401 IN BOOLEAN Iso639Language\r
d1102dba 402 )\r
72399dae 403{\r
404 UINTN Index;\r
1623ce4a
RN
405 UINTN CompareLength;\r
406 UINTN LanguageLength;\r
72399dae 407\r
72399dae 408 if (Iso639Language) {\r
1623ce4a 409 CompareLength = ISO_639_2_ENTRY_SIZE;\r
72399dae 410 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
411 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
412 //\r
413 // Successfully find the index of Lang string in SupportedLang string.\r
414 //\r
415 Index = Index / CompareLength;\r
416 return Index;\r
417 }\r
418 }\r
419 ASSERT (FALSE);\r
420 return 0;\r
421 } else {\r
422 //\r
0254efc0 423 // Compare RFC4646 language code\r
72399dae 424 //\r
1623ce4a
RN
425 Index = 0;\r
426 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
427\r
428 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
72399dae 429 //\r
1623ce4a 430 // Skip ';' characters in SupportedLang\r
72399dae 431 //\r
1623ce4a
RN
432 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
433 //\r
434 // Determine the length of the next language code in SupportedLang\r
435 //\r
436 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
d1102dba
LG
437\r
438 if ((CompareLength == LanguageLength) &&\r
1623ce4a 439 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
72399dae 440 //\r
441 // Successfully find the index of Lang string in SupportedLang string.\r
442 //\r
443 return Index;\r
444 }\r
72399dae 445 }\r
446 ASSERT (FALSE);\r
447 return 0;\r
448 }\r
449}\r
450\r
451/**\r
452 Get language string from supported language codes according to index.\r
453\r
454 This code is used to get corresponding language string in supported language codes. It can handle\r
0254efc0 455 RFC4646 and ISO639 language tags.\r
72399dae 456 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
0254efc0 457 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
72399dae 458\r
459 For example:\r
460 SupportedLang = "engfraengfra"\r
461 Index = "1"\r
462 Iso639Language = TRUE\r
463 The return value is "fra".\r
464 Another example:\r
465 SupportedLang = "en;fr;en-US;fr-FR"\r
466 Index = "1"\r
467 Iso639Language = FALSE\r
468 The return value is "fr".\r
469\r
470 @param SupportedLang Platform supported language codes.\r
471 @param Index the index in supported language codes.\r
0254efc0 472 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 473\r
474 @retval the language string in the language codes.\r
475\r
476**/\r
477CHAR8 *\r
72399dae 478GetLangFromSupportedLangCodes (\r
479 IN CHAR8 *SupportedLang,\r
480 IN UINTN Index,\r
481 IN BOOLEAN Iso639Language\r
482)\r
483{\r
484 UINTN SubIndex;\r
1623ce4a 485 UINTN CompareLength;\r
72399dae 486 CHAR8 *Supported;\r
487\r
488 SubIndex = 0;\r
489 Supported = SupportedLang;\r
490 if (Iso639Language) {\r
491 //\r
492 // according to the index of Lang string in SupportedLang string to get the language.\r
493 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
494 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
495 //\r
1623ce4a
RN
496 CompareLength = ISO_639_2_ENTRY_SIZE;\r
497 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
72399dae 498 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
1623ce4a 499\r
72399dae 500 } else {\r
501 while (TRUE) {\r
502 //\r
503 // take semicolon as delimitation, sequentially traverse supported language codes.\r
504 //\r
505 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
506 Supported++;\r
507 }\r
508 if ((*Supported == '\0') && (SubIndex != Index)) {\r
509 //\r
510 // Have completed the traverse, but not find corrsponding string.\r
511 // This case is not allowed to happen.\r
512 //\r
513 ASSERT(FALSE);\r
514 return NULL;\r
515 }\r
516 if (SubIndex == Index) {\r
517 //\r
518 // according to the index of Lang string in SupportedLang string to get the language.\r
519 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
520 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
521 //\r
1623ce4a 522 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
72399dae 523 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
524 }\r
1623ce4a 525 SubIndex++;\r
d1102dba 526\r
5c033766
RN
527 //\r
528 // Skip ';' characters in Supported\r
529 //\r
530 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
72399dae 531 }\r
532 }\r
533}\r
534\r
1623ce4a 535/**\r
d1102dba
LG
536 Returns a pointer to an allocated buffer that contains the best matching language\r
537 from a set of supported languages.\r
538\r
539 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
1623ce4a
RN
540 code types may not be mixed in a single call to this function. This function\r
541 supports a variable argument list that allows the caller to pass in a prioritized\r
542 list of language codes to test against all the language codes in SupportedLanguages.\r
543\r
544 If SupportedLanguages is NULL, then ASSERT().\r
545\r
546 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
d1102dba 547 contains a set of language codes in the format\r
1623ce4a 548 specified by Iso639Language.\r
051bf6e0
LG
549 @param[in] Iso639Language If not zero, then all language codes are assumed to be\r
550 in ISO 639-2 format. If zero, then all language\r
1623ce4a 551 codes are assumed to be in RFC 4646 language format\r
d1102dba 552 @param[in] ... A variable argument list that contains pointers to\r
1623ce4a
RN
553 Null-terminated ASCII strings that contain one or more\r
554 language codes in the format specified by Iso639Language.\r
555 The first language code from each of these language\r
556 code lists is used to determine if it is an exact or\r
d1102dba 557 close match to any of the language codes in\r
1623ce4a
RN
558 SupportedLanguages. Close matches only apply to RFC 4646\r
559 language codes, and the matching algorithm from RFC 4647\r
d1102dba 560 is used to determine if a close match is present. If\r
1623ce4a
RN
561 an exact or close match is found, then the matching\r
562 language code from SupportedLanguages is returned. If\r
563 no matches are found, then the next variable argument\r
d1102dba 564 parameter is evaluated. The variable argument list\r
1623ce4a
RN
565 is terminated by a NULL.\r
566\r
567 @retval NULL The best matching language could not be found in SupportedLanguages.\r
d1102dba 568 @retval NULL There are not enough resources available to return the best matching\r
1623ce4a 569 language.\r
d1102dba 570 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
1623ce4a
RN
571 language in SupportedLanguages.\r
572\r
573**/\r
574CHAR8 *\r
e1adae60 575EFIAPI\r
1623ce4a 576VariableGetBestLanguage (\r
d1102dba 577 IN CONST CHAR8 *SupportedLanguages,\r
180ac200 578 IN UINTN Iso639Language,\r
1623ce4a
RN
579 ...\r
580 )\r
581{\r
582 VA_LIST Args;\r
583 CHAR8 *Language;\r
584 UINTN CompareLength;\r
585 UINTN LanguageLength;\r
586 CONST CHAR8 *Supported;\r
587 CHAR8 *Buffer;\r
588\r
589 ASSERT (SupportedLanguages != NULL);\r
590\r
591 VA_START (Args, Iso639Language);\r
592 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
593 //\r
594 // Default to ISO 639-2 mode\r
595 //\r
596 CompareLength = 3;\r
597 LanguageLength = MIN (3, AsciiStrLen (Language));\r
598\r
599 //\r
600 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
601 //\r
051bf6e0 602 if (Iso639Language == 0) {\r
1623ce4a
RN
603 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
604 }\r
605\r
606 //\r
607 // Trim back the length of Language used until it is empty\r
608 //\r
609 while (LanguageLength > 0) {\r
610 //\r
611 // Loop through all language codes in SupportedLanguages\r
612 //\r
613 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
614 //\r
615 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
616 //\r
051bf6e0 617 if (Iso639Language == 0) {\r
1623ce4a
RN
618 //\r
619 // Skip ';' characters in Supported\r
620 //\r
621 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
622 //\r
623 // Determine the length of the next language code in Supported\r
624 //\r
625 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
626 //\r
627 // If Language is longer than the Supported, then skip to the next language\r
628 //\r
629 if (LanguageLength > CompareLength) {\r
630 continue;\r
631 }\r
632 }\r
633 //\r
634 // See if the first LanguageLength characters in Supported match Language\r
635 //\r
636 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
637 VA_END (Args);\r
638\r
051bf6e0 639 Buffer = (Iso639Language != 0) ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1623ce4a
RN
640 Buffer[CompareLength] = '\0';\r
641 return CopyMem (Buffer, Supported, CompareLength);\r
642 }\r
643 }\r
644\r
051bf6e0 645 if (Iso639Language != 0) {\r
1623ce4a
RN
646 //\r
647 // If ISO 639 mode, then each language can only be tested once\r
648 //\r
649 LanguageLength = 0;\r
650 } else {\r
651 //\r
d1102dba 652 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
1623ce4a
RN
653 //\r
654 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
655 }\r
656 }\r
657 }\r
658 VA_END (Args);\r
659\r
660 //\r
d1102dba 661 // No matches were found\r
1623ce4a
RN
662 //\r
663 return NULL;\r
664}\r
665\r
72399dae 666/**\r
667 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
668\r
669 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
670\r
671 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
672 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
673\r
674 @param[in] VariableName Name of variable\r
675\r
676 @param[in] Data Variable data\r
677\r
678 @param[in] DataSize Size of data. 0 means delete\r
679\r
72399dae 680**/\r
1623ce4a 681VOID\r
72399dae 682AutoUpdateLangVariable(\r
683 IN CHAR16 *VariableName,\r
684 IN VOID *Data,\r
685 IN UINTN DataSize\r
686 )\r
687{\r
1623ce4a
RN
688 EFI_STATUS Status;\r
689 CHAR8 *BestPlatformLang;\r
690 CHAR8 *BestLang;\r
691 UINTN Index;\r
692 UINT32 Attributes;\r
72399dae 693 VARIABLE_POINTER_TRACK Variable;\r
1623ce4a 694 BOOLEAN SetLanguageCodes;\r
72399dae 695\r
696 //\r
1623ce4a 697 // Don't do updates for delete operation\r
72399dae 698 //\r
1623ce4a
RN
699 if (DataSize == 0) {\r
700 return;\r
701 }\r
702\r
703 SetLanguageCodes = FALSE;\r
72399dae 704\r
705 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
1623ce4a
RN
706 //\r
707 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
708 //\r
709 if (EfiAtRuntime ()) {\r
710 return;\r
711 }\r
712\r
713 SetLanguageCodes = TRUE;\r
714\r
72399dae 715 //\r
716 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
717 // Therefore, in variable driver, only store the original value for other use.\r
718 //\r
1623ce4a
RN
719 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
720 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
721 }\r
722 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
723 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
724\r
72399dae 725 //\r
d1102dba 726 // PlatformLang holds a single language from PlatformLangCodes,\r
1623ce4a 727 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 728 //\r
1623ce4a
RN
729 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
730 FreePool (mVariableModuleGlobal->PlatformLang);\r
731 }\r
732 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
733 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
72399dae 734\r
1623ce4a 735 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
72399dae 736 //\r
1623ce4a 737 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 738 //\r
1623ce4a
RN
739 if (EfiAtRuntime ()) {\r
740 return;\r
741 }\r
742\r
743 SetLanguageCodes = TRUE;\r
72399dae 744\r
745 //\r
1623ce4a
RN
746 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
747 // Therefore, in variable driver, only store the original value for other use.\r
72399dae 748 //\r
1623ce4a
RN
749 if (mVariableModuleGlobal->LangCodes != NULL) {\r
750 FreePool (mVariableModuleGlobal->LangCodes);\r
751 }\r
752 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
753 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
754 }\r
72399dae 755\r
d1102dba 756 if (SetLanguageCodes\r
1623ce4a
RN
757 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
758 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
72399dae 759 //\r
1623ce4a
RN
760 // Update Lang if PlatformLang is already set\r
761 // Update PlatformLang if Lang is already set\r
72399dae 762 //\r
1623ce4a
RN
763 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r
764 if (!EFI_ERROR (Status)) {\r
765 //\r
766 // Update Lang\r
767 //\r
768 VariableName = L"PlatformLang";\r
769 Data = GetVariableDataPtr (Variable.CurrPtr);\r
770 DataSize = Variable.CurrPtr->DataSize;\r
771 } else {\r
772 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r
773 if (!EFI_ERROR (Status)) {\r
774 //\r
775 // Update PlatformLang\r
776 //\r
777 VariableName = L"Lang";\r
778 Data = GetVariableDataPtr (Variable.CurrPtr);\r
779 DataSize = Variable.CurrPtr->DataSize;\r
780 } else {\r
781 //\r
782 // Neither PlatformLang nor Lang is set, directly return\r
783 //\r
784 return;\r
785 }\r
786 }\r
787 }\r
d1102dba 788\r
1623ce4a
RN
789 //\r
790 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
791 //\r
792 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
72399dae 793\r
1623ce4a 794 if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
72399dae 795 //\r
1623ce4a 796 // Update Lang when PlatformLangCodes/LangCodes were set.\r
72399dae 797 //\r
1623ce4a
RN
798 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
799 //\r
800 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
801 //\r
802 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
803 if (BestPlatformLang != NULL) {\r
804 //\r
805 // Get the corresponding index in language codes.\r
806 //\r
807 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
72399dae 808\r
1623ce4a
RN
809 //\r
810 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
811 //\r
812 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
72399dae 813\r
1623ce4a
RN
814 //\r
815 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
816 //\r
817 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
72399dae 818\r
1623ce4a 819 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
72399dae 820\r
1623ce4a 821 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
72399dae 822\r
1623ce4a
RN
823 ASSERT_EFI_ERROR(Status);\r
824 }\r
825 }\r
72399dae 826\r
1623ce4a 827 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
72399dae 828 //\r
1623ce4a 829 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 830 //\r
1623ce4a
RN
831 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
832 //\r
833 // When setting Lang, firstly get most matched language string from supported language codes.\r
834 //\r
835 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
836 if (BestLang != NULL) {\r
837 //\r
838 // Get the corresponding index in language codes.\r
839 //\r
840 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 841\r
1623ce4a
RN
842 //\r
843 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
844 //\r
845 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
72399dae 846\r
1623ce4a
RN
847 //\r
848 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
849 //\r
850 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
72399dae 851\r
d1102dba 852 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang,\r
1623ce4a 853 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
72399dae 854\r
1623ce4a
RN
855 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
856 ASSERT_EFI_ERROR (Status);\r
857 }\r
858 }\r
72399dae 859 }\r
72399dae 860}\r
861\r
862/**\r
d1102dba 863 Update the variable region with Variable information. These are the same\r
72399dae 864 arguments as the EFI Variable services.\r
865\r
866 @param[in] VariableName Name of variable\r
867\r
868 @param[in] VendorGuid Guid of variable\r
869\r
870 @param[in] Data Variable data\r
871\r
872 @param[in] DataSize Size of data. 0 means delete\r
873\r
874 @param[in] Attributes Attribues of the variable\r
875\r
876 @param[in] Variable The variable information which is used to keep track of variable usage.\r
877\r
878 @retval EFI_SUCCESS The update operation is success.\r
879\r
880 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
881\r
882**/\r
883EFI_STATUS\r
884EFIAPI\r
885UpdateVariable (\r
886 IN CHAR16 *VariableName,\r
887 IN EFI_GUID *VendorGuid,\r
888 IN VOID *Data,\r
889 IN UINTN DataSize,\r
890 IN UINT32 Attributes OPTIONAL,\r
891 IN VARIABLE_POINTER_TRACK *Variable\r
892 )\r
893{\r
894 EFI_STATUS Status;\r
895 VARIABLE_HEADER *NextVariable;\r
896 UINTN VarNameSize;\r
897 UINTN VarNameOffset;\r
898 UINTN VarDataOffset;\r
899 UINTN VarSize;\r
900 VARIABLE_GLOBAL *Global;\r
1d5b25b7 901 UINTN NonVolatileVarableStoreSize;\r
72399dae 902\r
903 Global = &mVariableModuleGlobal->VariableGlobal[Physical];\r
904\r
905 if (Variable->CurrPtr != NULL) {\r
906 //\r
907 // Update/Delete existing variable\r
908 //\r
909\r
d1102dba 910 if (EfiAtRuntime ()) {\r
72399dae 911 //\r
d1102dba
LG
912 // If EfiAtRuntime and the variable is Volatile and Runtime Access,\r
913 // the volatile is ReadOnly, and SetVariable should be aborted and\r
72399dae 914 // return EFI_WRITE_PROTECTED.\r
915 //\r
916 if (Variable->Volatile) {\r
917 Status = EFI_WRITE_PROTECTED;\r
918 goto Done;\r
919 }\r
920 //\r
921 // Only variable have NV attribute can be updated/deleted in Runtime\r
922 //\r
923 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
924 Status = EFI_INVALID_PARAMETER;\r
925 goto Done;\r
926 }\r
927 }\r
928\r
929 //\r
930 // Setting a data variable with no access, or zero DataSize attributes\r
931 // specified causes it to be deleted.\r
932 //\r
933 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
934 Variable->CurrPtr->State &= VAR_DELETED;\r
935 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
936 Status = EFI_SUCCESS;\r
937 goto Done;\r
938 }\r
939\r
940 //\r
941 // If the variable is marked valid and the same data has been passed in\r
942 // then return to the caller immediately.\r
943 //\r
944 if (Variable->CurrPtr->DataSize == DataSize &&\r
945 CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0\r
946 ) {\r
947 Status = EFI_SUCCESS;\r
948 goto Done;\r
949 } else if (Variable->CurrPtr->State == VAR_ADDED) {\r
950 //\r
951 // Mark the old variable as in delete transition\r
952 //\r
953 Variable->CurrPtr->State &= VAR_IN_DELETED_TRANSITION;\r
954 }\r
d1102dba 955\r
72399dae 956 } else {\r
957 //\r
958 // No found existing variable, Create a new variable\r
d1102dba
LG
959 //\r
960\r
72399dae 961 //\r
962 // Make sure we are trying to create a new variable.\r
d1102dba 963 // Setting a data variable with no access, or zero DataSize attributes means to delete it.\r
72399dae 964 //\r
965 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
966 Status = EFI_NOT_FOUND;\r
967 goto Done;\r
968 }\r
d1102dba 969\r
72399dae 970 //\r
971 // Only variable have NV|RT attribute can be created in Runtime\r
972 //\r
973 if (EfiAtRuntime () &&\r
974 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
975 Status = EFI_INVALID_PARAMETER;\r
976 goto Done;\r
d1102dba 977 }\r
72399dae 978 }\r
d1102dba 979\r
72399dae 980 //\r
981 // Function part - create a new variable and copy the data.\r
982 // Both update a variable and create a variable will come here.\r
983 //\r
d1102dba 984\r
72399dae 985 VarNameOffset = sizeof (VARIABLE_HEADER);\r
986 VarNameSize = StrSize (VariableName);\r
987 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
988 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
989\r
990 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
991 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(Global->NonVolatileVariableBase))->Size;\r
d1102dba 992 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
d26727de 993 && ((HEADER_ALIGN (VarSize) + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
d1102dba 994 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)\r
d26727de 995 && ((HEADER_ALIGN (VarSize) + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
72399dae 996 Status = EFI_OUT_OF_RESOURCES;\r
997 goto Done;\r
998 }\r
999\r
1000 NextVariable = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->NonVolatileLastVariableOffset\r
1001 + (UINTN) Global->NonVolatileVariableBase);\r
f68af18e 1002 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
72399dae 1003\r
1004 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
f68af18e 1005 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
72399dae 1006 } else {\r
f68af18e 1007 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
72399dae 1008 }\r
1009 } else {\r
d26727de 1010 if ((UINT32) (HEADER_ALIGN (VarSize) + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
72399dae 1011 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
1012 ) {\r
1013 Status = EFI_OUT_OF_RESOURCES;\r
1014 goto Done;\r
1015 }\r
1016\r
1017 NextVariable = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->VolatileLastVariableOffset\r
1018 + (UINTN) Global->VolatileVariableBase);\r
f68af18e 1019 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
72399dae 1020 }\r
1021\r
1022 NextVariable->StartId = VARIABLE_DATA;\r
1023 NextVariable->Attributes = Attributes;\r
1024 NextVariable->State = VAR_ADDED;\r
1025 NextVariable->Reserved = 0;\r
1026\r
1027 //\r
1028 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1029 // NextVariable->NameSize should not include pad size so that variable\r
1030 // service can get actual size in GetVariable\r
1031 //\r
1032 NextVariable->NameSize = (UINT32)VarNameSize;\r
1033 NextVariable->DataSize = (UINT32)DataSize;\r
1034\r
1035 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1036 CopyMem (\r
1037 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1038 VariableName,\r
1039 VarNameSize\r
1040 );\r
1041 CopyMem (\r
1042 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1043 Data,\r
1044 DataSize\r
1045 );\r
1046\r
1047 //\r
1048 // Mark the old variable as deleted\r
1049 //\r
1d5b25b7 1050 if (Variable->CurrPtr != NULL) {\r
1051 Variable->CurrPtr->State &= VAR_DELETED;\r
1052 }\r
72399dae 1053\r
1054 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
1055\r
1056 Status = EFI_SUCCESS;\r
1057\r
1058Done:\r
1059 return Status;\r
1060}\r
1061\r
1794e506 1062/**\r
1063 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
1064\r
1065 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
1066 If VariableName is an empty string, then we just return the first\r
1067 qualified variable without comparing VariableName and VendorGuid.\r
1068 Otherwise, VariableName and VendorGuid are compared.\r
1069\r
1070 @param VariableName Name of the variable to be found.\r
1071 @param VendorGuid Vendor GUID to be found.\r
1072 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
1073 including the range searched and the target position.\r
1074 @param Global Pointer to VARIABLE_GLOBAL structure, including\r
1075 base of volatile variable storage area, base of\r
1076 NV variable storage area, and a lock.\r
1077\r
1078 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
1079 VendorGuid is NULL.\r
1080 @retval EFI_SUCCESS Variable successfully found.\r
1081 @retval EFI_NOT_FOUND Variable not found.\r
1082\r
1083**/\r
8d3a5c82 1084EFI_STATUS\r
8d3a5c82 1085FindVariable (\r
1086 IN CHAR16 *VariableName,\r
1087 IN EFI_GUID *VendorGuid,\r
1088 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
1089 IN VARIABLE_GLOBAL *Global\r
1090 )\r
8d3a5c82 1091{\r
1092 VARIABLE_HEADER *Variable[2];\r
1093 VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r
1094 UINTN Index;\r
1095\r
8d3a5c82 1096 //\r
1097 // 0: Non-Volatile, 1: Volatile\r
1098 //\r
1099 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
1100 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
1101\r
1102 //\r
1103 // Start Pointers for the variable.\r
1104 // Actual Data Pointer where data can be written.\r
1105 //\r
d26727de
RN
1106 Variable[0] = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader[0] + 1);\r
1107 Variable[1] = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader[1] + 1);\r
8d3a5c82 1108\r
1109 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
1110 return EFI_INVALID_PARAMETER;\r
1111 }\r
1112 //\r
1113 // Find the variable by walk through non-volatile and volatile variable store\r
1114 //\r
1115 for (Index = 0; Index < 2; Index++) {\r
d26727de 1116 PtrTrack->StartPtr = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader[Index] + 1);\r
8d3a5c82 1117 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r
1118\r
fba0ee1f 1119 while ((Variable[Index] < GetEndPointer (VariableStoreHeader[Index])) && (Variable[Index] != NULL)) {\r
8d3a5c82 1120 if (Variable[Index]->StartId == VARIABLE_DATA && Variable[Index]->State == VAR_ADDED) {\r
2374b973 1121 if (!(EfiAtRuntime () && ((Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {\r
8d3a5c82 1122 if (VariableName[0] == 0) {\r
1123 PtrTrack->CurrPtr = Variable[Index];\r
1124 PtrTrack->Volatile = (BOOLEAN) Index;\r
1125 return EFI_SUCCESS;\r
1126 } else {\r
1127 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r
c24b392c 1128 if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize) == 0) {\r
8d3a5c82 1129 PtrTrack->CurrPtr = Variable[Index];\r
1130 PtrTrack->Volatile = (BOOLEAN) Index;\r
1131 return EFI_SUCCESS;\r
1132 }\r
1133 }\r
1134 }\r
1135 }\r
1136 }\r
1137\r
1138 Variable[Index] = GetNextVariablePtr (Variable[Index]);\r
1139 }\r
1140 }\r
1141 PtrTrack->CurrPtr = NULL;\r
1142 return EFI_NOT_FOUND;\r
1143}\r
1144\r
1794e506 1145/**\r
1146 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
d1102dba 1147\r
1794e506 1148 @param VariableName A Null-terminated Unicode string that is the name of\r
1149 the vendor's variable.\r
1150 @param VendorGuid A unique identifier for the vendor.\r
d1102dba 1151 @param Attributes If not NULL, a pointer to the memory location to return the\r
1794e506 1152 attributes bitmask for the variable.\r
1153 @param DataSize Size of Data found. If size is less than the\r
1154 data, this value contains the required size.\r
d1102dba 1155 @param Data On input, the size in bytes of the return Data buffer.\r
1794e506 1156 On output, the size of data returned in Data.\r
1157 @param Global Pointer to VARIABLE_GLOBAL structure\r
1794e506 1158\r
d1102dba 1159 @retval EFI_SUCCESS The function completed successfully.\r
1794e506 1160 @retval EFI_NOT_FOUND The variable was not found.\r
d1102dba 1161 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has\r
1794e506 1162 been updated with the size needed to complete the request.\r
1163 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid or DataSize is NULL.\r
1164\r
1165**/\r
8d3a5c82 1166EFI_STATUS\r
1167EFIAPI\r
bd682470 1168EmuGetVariable (\r
8d3a5c82 1169 IN CHAR16 *VariableName,\r
72399dae 1170 IN EFI_GUID *VendorGuid,\r
8d3a5c82 1171 OUT UINT32 *Attributes OPTIONAL,\r
1172 IN OUT UINTN *DataSize,\r
1173 OUT VOID *Data,\r
72399dae 1174 IN VARIABLE_GLOBAL *Global\r
8d3a5c82 1175 )\r
8d3a5c82 1176{\r
1177 VARIABLE_POINTER_TRACK Variable;\r
1178 UINTN VarDataSize;\r
1179 EFI_STATUS Status;\r
04eb20aa 1180 UINT8 *VariableDataPtr;\r
8d3a5c82 1181\r
1182 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1183 return EFI_INVALID_PARAMETER;\r
1184 }\r
51967a53 1185\r
e19eab61
SZ
1186 if (VariableName[0] == 0) {\r
1187 return EFI_NOT_FOUND;\r
1188 }\r
1189\r
51967a53 1190 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1191\r
8d3a5c82 1192 //\r
1193 // Find existing variable\r
1194 //\r
1195 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
1196\r
1197 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1198 goto Done;\r
1199 }\r
1200 //\r
1201 // Get data size\r
1202 //\r
1203 VarDataSize = Variable.CurrPtr->DataSize;\r
1204 if (*DataSize >= VarDataSize) {\r
1205 if (Data == NULL) {\r
1206 Status = EFI_INVALID_PARAMETER;\r
1207 goto Done;\r
1208 }\r
04eb20aa
ED
1209 VariableDataPtr = GetVariableDataPtr (Variable.CurrPtr);\r
1210 ASSERT (VariableDataPtr != NULL);\r
d1102dba 1211\r
04eb20aa 1212 CopyMem (Data, VariableDataPtr, VarDataSize);\r
8d3a5c82 1213 if (Attributes != NULL) {\r
1214 *Attributes = Variable.CurrPtr->Attributes;\r
1215 }\r
1216\r
1217 *DataSize = VarDataSize;\r
e4ddc008 1218 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
8d3a5c82 1219 Status = EFI_SUCCESS;\r
1220 goto Done;\r
1221 } else {\r
1222 *DataSize = VarDataSize;\r
1223 Status = EFI_BUFFER_TOO_SMALL;\r
1224 goto Done;\r
1225 }\r
1226\r
1227Done:\r
1228 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1229 return Status;\r
1230}\r
1231\r
1794e506 1232/**\r
1233\r
1234 This code Finds the Next available variable.\r
1235\r
6f817f9b
SZ
1236 @param VariableNameSize The size of the VariableName buffer. The size must be large enough to fit input\r
1237 string supplied in VariableName buffer.\r
1794e506 1238 @param VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().\r
1239 On output, returns the Null-terminated Unicode string of the current variable.\r
1240 @param VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
6f817f9b 1241 On output, returns the VendorGuid of the current variable.\r
1794e506 1242 @param Global Pointer to VARIABLE_GLOBAL structure.\r
1794e506 1243\r
6f817f9b 1244 @retval EFI_SUCCESS The function completed successfully.\r
1794e506 1245 @retval EFI_NOT_FOUND The next variable was not found.\r
6f817f9b 1246 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.\r
1794e506 1247 VariableNameSize has been updated with the size needed to complete the request.\r
1248 @retval EFI_INVALID_PARAMETER VariableNameSize or VariableName or VendorGuid is NULL.\r
6f817f9b
SZ
1249 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
1250 GUID of an existing variable.\r
1251 @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of\r
1252 the input VariableName buffer.\r
1794e506 1253\r
1254**/\r
8d3a5c82 1255EFI_STATUS\r
1256EFIAPI\r
bd682470 1257EmuGetNextVariableName (\r
8d3a5c82 1258 IN OUT UINTN *VariableNameSize,\r
1259 IN OUT CHAR16 *VariableName,\r
1260 IN OUT EFI_GUID *VendorGuid,\r
72399dae 1261 IN VARIABLE_GLOBAL *Global\r
8d3a5c82 1262 )\r
8d3a5c82 1263{\r
1264 VARIABLE_POINTER_TRACK Variable;\r
1265 UINTN VarNameSize;\r
1266 EFI_STATUS Status;\r
6f817f9b 1267 UINTN MaxLen;\r
8d3a5c82 1268\r
1269 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1270 return EFI_INVALID_PARAMETER;\r
1271 }\r
1272\r
6f817f9b
SZ
1273 //\r
1274 // Calculate the possible maximum length of name string, including the Null terminator.\r
1275 //\r
1276 MaxLen = *VariableNameSize / sizeof (CHAR16);\r
1277 if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r
1278 //\r
1279 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r
1280 // follow spec to return EFI_INVALID_PARAMETER.\r
1281 //\r
1282 return EFI_INVALID_PARAMETER;\r
1283 }\r
1284\r
51967a53 1285 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1286\r
8d3a5c82 1287 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
1288\r
1289 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
6f817f9b
SZ
1290 //\r
1291 // For VariableName is an empty string, FindVariable() will try to find and return\r
1292 // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r
1293 // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r
1294 //\r
1295 if (VariableName[0] != 0) {\r
1296 //\r
1297 // For VariableName is not an empty string, and FindVariable() returns error as\r
1298 // VariableName and VendorGuid are not a name and GUID of an existing variable,\r
1299 // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r
1300 //\r
1301 Status = EFI_INVALID_PARAMETER;\r
1302 }\r
8d3a5c82 1303 goto Done;\r
1304 }\r
1305\r
1306 while (TRUE) {\r
1307 if (VariableName[0] != 0) {\r
1308 //\r
1309 // If variable name is not NULL, get next variable\r
1310 //\r
1311 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1312 }\r
1313 //\r
1314 // If both volatile and non-volatile variable store are parsed,\r
1315 // return not found\r
1316 //\r
1317 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
1318 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
1319 if (Variable.Volatile) {\r
d26727de 1320 Variable.StartPtr = (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r
8d3a5c82 1321 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
1322 } else {\r
1323 Status = EFI_NOT_FOUND;\r
1324 goto Done;\r
1325 }\r
1326\r
1327 Variable.CurrPtr = Variable.StartPtr;\r
1328 if (Variable.CurrPtr->StartId != VARIABLE_DATA) {\r
1329 continue;\r
1330 }\r
1331 }\r
1332 //\r
1333 // Variable is found\r
1334 //\r
1335 if (Variable.CurrPtr->StartId == VARIABLE_DATA && Variable.CurrPtr->State == VAR_ADDED) {\r
ba5e88f9 1336 if (!(EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {\r
8d3a5c82 1337 VarNameSize = Variable.CurrPtr->NameSize;\r
1338 if (VarNameSize <= *VariableNameSize) {\r
1339 CopyMem (\r
1340 VariableName,\r
1341 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
1342 VarNameSize\r
1343 );\r
1344 CopyMem (\r
1345 VendorGuid,\r
1346 &Variable.CurrPtr->VendorGuid,\r
1347 sizeof (EFI_GUID)\r
1348 );\r
1349 Status = EFI_SUCCESS;\r
1350 } else {\r
1351 Status = EFI_BUFFER_TOO_SMALL;\r
1352 }\r
1353\r
1354 *VariableNameSize = VarNameSize;\r
1355 goto Done;\r
1356 }\r
1357 }\r
1358 }\r
1359\r
1360Done:\r
1361 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1362 return Status;\r
1363\r
1364}\r
1365\r
1794e506 1366/**\r
1367\r
1368 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
1369\r
1370 @param VariableName A Null-terminated Unicode string that is the name of the vendor's\r
d1102dba
LG
1371 variable. Each VariableName is unique for each\r
1372 VendorGuid. VariableName must contain 1 or more\r
1373 Unicode characters. If VariableName is an empty Unicode\r
1794e506 1374 string, then EFI_INVALID_PARAMETER is returned.\r
1375 @param VendorGuid A unique identifier for the vendor\r
1376 @param Attributes Attributes bitmask to set for the variable\r
1377 @param DataSize The size in bytes of the Data buffer. A size of zero causes the\r
1378 variable to be deleted.\r
1379 @param Data The contents for the variable\r
1380 @param Global Pointer to VARIABLE_GLOBAL structure\r
1381 @param VolatileOffset The offset of last volatile variable\r
1382 @param NonVolatileOffset The offset of last non-volatile variable\r
1794e506 1383\r
d1102dba 1384 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
1794e506 1385 defined by the Attributes.\r
d1102dba
LG
1386 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the\r
1387 DataSize exceeds the maximum allowed, or VariableName is an empty\r
1794e506 1388 Unicode string, or VendorGuid is NULL.\r
1389 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
1390 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
1391 @retval EFI_WRITE_PROTECTED The variable in question is read-only or cannot be deleted.\r
1392 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
1393\r
1394**/\r
8d3a5c82 1395EFI_STATUS\r
1396EFIAPI\r
bd682470 1397EmuSetVariable (\r
8d3a5c82 1398 IN CHAR16 *VariableName,\r
1399 IN EFI_GUID *VendorGuid,\r
1400 IN UINT32 Attributes,\r
1401 IN UINTN DataSize,\r
1402 IN VOID *Data,\r
1403 IN VARIABLE_GLOBAL *Global,\r
1404 IN UINTN *VolatileOffset,\r
72399dae 1405 IN UINTN *NonVolatileOffset\r
8d3a5c82 1406 )\r
8d3a5c82 1407{\r
1408 VARIABLE_POINTER_TRACK Variable;\r
1409 EFI_STATUS Status;\r
8d3a5c82 1410\r
c6492839 1411 //\r
1412 // Check input parameters\r
1413 //\r
8d3a5c82 1414 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1415 return EFI_INVALID_PARAMETER;\r
d1102dba 1416 }\r
48a0e6bf 1417\r
1418 if (DataSize != 0 && Data == NULL) {\r
1419 return EFI_INVALID_PARAMETER;\r
1420 }\r
1421\r
8e38f18e 1422 //\r
1423 // Not support authenticated variable write yet.\r
1424 //\r
1425 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1426 return EFI_INVALID_PARAMETER;\r
1427 }\r
1428\r
c6492839 1429 //\r
1430 // Make sure if runtime bit is set, boot service bit is set also\r
1431 //\r
1432 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1433 return EFI_INVALID_PARAMETER;\r
8d3a5c82 1434 }\r
56251c66 1435\r
d1102dba 1436\r
56251c66 1437 if ((UINTN)(~0) - DataSize < StrSize(VariableName)){\r
1438 //\r
d1102dba
LG
1439 // Prevent whole variable size overflow\r
1440 //\r
56251c66 1441 return EFI_INVALID_PARAMETER;\r
1442 }\r
1443\r
c6492839 1444 //\r
1445 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 1446 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
1447 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
c6492839 1448 //\r
1449 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
56251c66 1450 if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
c6492839 1451 return EFI_INVALID_PARAMETER;\r
0c420e26 1452 }\r
1453 //\r
1454 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
1455 //\r
1456 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
1457 return EFI_INVALID_PARAMETER;\r
1458 }\r
c6492839 1459 } else {\r
1460 //\r
1461 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 1462 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
c6492839 1463 //\r
56251c66 1464 if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
c6492839 1465 return EFI_INVALID_PARAMETER;\r
d1102dba 1466 }\r
0c420e26 1467 }\r
1468\r
51967a53 1469 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1470\r
c6492839 1471 //\r
1472 // Check whether the input variable is already existed\r
1473 //\r
d1102dba 1474\r
8d3a5c82 1475 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
1476\r
c6492839 1477 //\r
72399dae 1478 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
c6492839 1479 //\r
72399dae 1480 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
c6492839 1481\r
72399dae 1482 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
c6492839 1483\r
8d3a5c82 1484 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1485 return Status;\r
1486}\r
1487\r
1794e506 1488/**\r
1489\r
1490 This code returns information about the EFI variables.\r
1491\r
1492 @param Attributes Attributes bitmask to specify the type of variables\r
1493 on which to return information.\r
d1102dba
LG
1494 @param MaximumVariableStorageSize On output the maximum size of the storage space available for\r
1495 the EFI variables associated with the attributes specified.\r
1496 @param RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI\r
1794e506 1497 variables associated with the attributes specified.\r
d1102dba 1498 @param MaximumVariableSize Returns the maximum size of an individual EFI variable\r
1794e506 1499 associated with the attributes specified.\r
1500 @param Global Pointer to VARIABLE_GLOBAL structure.\r
1794e506 1501\r
1502 @retval EFI_SUCCESS Valid answer returned.\r
1503 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied\r
d1102dba
LG
1504 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the\r
1505 MaximumVariableStorageSize, RemainingVariableStorageSize,\r
1794e506 1506 MaximumVariableSize are undefined.\r
1507\r
1508**/\r
8d3a5c82 1509EFI_STATUS\r
1510EFIAPI\r
bd682470 1511EmuQueryVariableInfo (\r
8d3a5c82 1512 IN UINT32 Attributes,\r
1513 OUT UINT64 *MaximumVariableStorageSize,\r
1514 OUT UINT64 *RemainingVariableStorageSize,\r
1515 OUT UINT64 *MaximumVariableSize,\r
72399dae 1516 IN VARIABLE_GLOBAL *Global\r
8d3a5c82 1517 )\r
8d3a5c82 1518{\r
1519 VARIABLE_HEADER *Variable;\r
1520 VARIABLE_HEADER *NextVariable;\r
1521 UINT64 VariableSize;\r
1522 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
72399dae 1523 UINT64 CommonVariableTotalSize;\r
1524 UINT64 HwErrVariableTotalSize;\r
1525\r
1526 CommonVariableTotalSize = 0;\r
1527 HwErrVariableTotalSize = 0;\r
8d3a5c82 1528\r
c6492839 1529 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 1530 return EFI_INVALID_PARAMETER;\r
1531 }\r
d1102dba 1532\r
c6492839 1533 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 1534 //\r
1535 // Make sure the Attributes combination is supported by the platform.\r
1536 //\r
d1102dba 1537 return EFI_UNSUPPORTED;\r
8d3a5c82 1538 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1539 //\r
1540 // Make sure if runtime bit is set, boot service bit is set also.\r
1541 //\r
1542 return EFI_INVALID_PARAMETER;\r
1794e506 1543 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
8d3a5c82 1544 //\r
1545 // Make sure RT Attribute is set if we are in Runtime phase.\r
1546 //\r
1547 return EFI_INVALID_PARAMETER;\r
72399dae 1548 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1549 //\r
1550 // Make sure Hw Attribute is set with NV.\r
1551 //\r
1552 return EFI_INVALID_PARAMETER;\r
8e38f18e 1553 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
1554 //\r
1555 // Not support authentiated variable write yet.\r
1556 //\r
1557 return EFI_UNSUPPORTED;\r
8d3a5c82 1558 }\r
1559\r
1560 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1561\r
1562 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1563 //\r
1564 // Query is Volatile related.\r
1565 //\r
1566 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
1567 } else {\r
1568 //\r
1569 // Query is Non-Volatile related.\r
1570 //\r
1571 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
1572 }\r
1573\r
1574 //\r
1575 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1576 // with the storage size (excluding the storage header size)\r
1577 //\r
1578 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 1579\r
1580 //\r
1581 // Harware error record variable needs larger size.\r
1582 //\r
72399dae 1583 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 1584 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
1585 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
72399dae 1586 } else {\r
1587 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
188e4e84 1588 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
1589 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
72399dae 1590 }\r
1591\r
1592 //\r
188e4e84 1593 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
72399dae 1594 //\r
188e4e84 1595 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 1596 }\r
8d3a5c82 1597\r
1598 //\r
1599 // Point to the starting address of the variables.\r
1600 //\r
d26727de 1601 Variable = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader + 1);\r
8d3a5c82 1602\r
1603 //\r
1604 // Now walk through the related variable store.\r
1605 //\r
1606 while (Variable < GetEndPointer (VariableStoreHeader)) {\r
72399dae 1607 NextVariable = GetNextVariablePtr(Variable);\r
1608 if (NextVariable == NULL) {\r
8d3a5c82 1609 break;\r
1610 }\r
8d3a5c82 1611 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1612\r
3b425367 1613 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
72399dae 1614 HwErrVariableTotalSize += VariableSize;\r
1615 } else {\r
1616 CommonVariableTotalSize += VariableSize;\r
8d3a5c82 1617 }\r
1618\r
1619 //\r
1620 // Go to the next one.\r
1621 //\r
1622 Variable = NextVariable;\r
1623 }\r
1624\r
72399dae 1625 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
1626 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
1627 } else {\r
1628 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
1629 }\r
1630\r
c6492839 1631 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
1632 *MaximumVariableSize = 0;\r
1633 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
1634 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
1635 }\r
d1102dba 1636\r
8d3a5c82 1637 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1638 return EFI_SUCCESS;\r
1639}\r
1640\r
1794e506 1641/**\r
1642 Initializes variable store area.\r
1643\r
1644 This function allocates memory space for variable store area and initializes its attributes.\r
1645\r
aa75dfec 1646 @param VolatileStore Indicates if the variable store is volatile.\r
1794e506 1647\r
1648**/\r
8d3a5c82 1649EFI_STATUS\r
8d3a5c82 1650InitializeVariableStore (\r
02328a57 1651 IN BOOLEAN VolatileStore\r
8d3a5c82 1652 )\r
8d3a5c82 1653{\r
f68af18e 1654 EFI_STATUS Status;\r
8d3a5c82 1655 VARIABLE_STORE_HEADER *VariableStore;\r
02328a57 1656 BOOLEAN FullyInitializeStore;\r
1657 EFI_PHYSICAL_ADDRESS *VariableBase;\r
1658 UINTN *LastVariableOffset;\r
f68af18e
RN
1659 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1660 VARIABLE_HEADER *Variable;\r
1661 VOID *VariableData;\r
1662 EFI_HOB_GUID_TYPE *GuidHob;\r
02328a57 1663\r
1664 FullyInitializeStore = TRUE;\r
1665\r
1666 if (VolatileStore) {\r
1667 VariableBase = &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase;\r
1668 LastVariableOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r
1669 } else {\r
1670 VariableBase = &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase;\r
1671 LastVariableOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
1672 }\r
8d3a5c82 1673\r
48cd992a 1674 //\r
1675 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
1676 // is stored with common variable in the same NV region. So the platform integrator should\r
d1102dba 1677 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of\r
93c288f2 1678 // PcdVariableStoreSize.\r
48cd992a 1679 //\r
188e4e84 1680 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdVariableStoreSize));\r
48cd992a 1681\r
8d3a5c82 1682 //\r
02328a57 1683 // Allocate memory for variable store.\r
8d3a5c82 1684 //\r
02328a57 1685 if (VolatileStore || (PcdGet64 (PcdEmuVariableNvStoreReserved) == 0)) {\r
188e4e84 1686 VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize));\r
02328a57 1687 } else {\r
1688 //\r
1689 // A memory location has been reserved for the NV variable store. Certain\r
1690 // platforms may be able to preserve a memory range across system resets,\r
1691 // thereby providing better NV variable emulation.\r
1692 //\r
1693 VariableStore =\r
1694 (VARIABLE_STORE_HEADER *)(VOID*)(UINTN)\r
1695 PcdGet64 (PcdEmuVariableNvStoreReserved);\r
1696 if (\r
188e4e84 1697 (VariableStore->Size == PcdGet32 (PcdVariableStoreSize)) &&\r
02328a57 1698 (VariableStore->Format == VARIABLE_STORE_FORMATTED) &&\r
1699 (VariableStore->State == VARIABLE_STORE_HEALTHY)\r
1700 ) {\r
1701 DEBUG((\r
1702 EFI_D_INFO,\r
1703 "Variable Store reserved at %p appears to be valid\n",\r
1704 VariableStore\r
1705 ));\r
1706 FullyInitializeStore = FALSE;\r
1707 }\r
1708 }\r
1709\r
8d3a5c82 1710 if (NULL == VariableStore) {\r
1711 return EFI_OUT_OF_RESOURCES;\r
1712 }\r
1713\r
02328a57 1714 if (FullyInitializeStore) {\r
188e4e84 1715 SetMem (VariableStore, PcdGet32 (PcdVariableStoreSize), 0xff);\r
02328a57 1716 }\r
8d3a5c82 1717\r
1718 //\r
1719 // Variable Specific Data\r
1720 //\r
1721 *VariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStore;\r
02328a57 1722 InitializeLocationForLastVariableOffset (VariableStore, LastVariableOffset);\r
8d3a5c82 1723\r
3709c4cd 1724 CopyGuid (&VariableStore->Signature, &gEfiVariableGuid);\r
188e4e84 1725 VariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
8d3a5c82 1726 VariableStore->Format = VARIABLE_STORE_FORMATTED;\r
1727 VariableStore->State = VARIABLE_STORE_HEALTHY;\r
1728 VariableStore->Reserved = 0;\r
1729 VariableStore->Reserved1 = 0;\r
1730\r
f68af18e
RN
1731 if (!VolatileStore) {\r
1732 //\r
1733 // Get HOB variable store.\r
1734 //\r
1735 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
1736 if (GuidHob != NULL) {\r
1737 VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob);\r
1738 if (CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&\r
1739 (VariableStoreHeader->Format == VARIABLE_STORE_FORMATTED) &&\r
1740 (VariableStoreHeader->State == VARIABLE_STORE_HEALTHY)\r
1741 ) {\r
1742 DEBUG ((EFI_D_INFO, "HOB Variable Store appears to be valid.\n"));\r
1743 //\r
1744 // Flush the HOB variable to Emulation Variable storage.\r
1745 //\r
d26727de 1746 for ( Variable = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader + 1)\r
f68af18e
RN
1747 ; (Variable < GetEndPointer (VariableStoreHeader) && (Variable != NULL))\r
1748 ; Variable = GetNextVariablePtr (Variable)\r
1749 ) {\r
1750 ASSERT (Variable->State == VAR_ADDED);\r
1751 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
1752 VariableData = GetVariableDataPtr (Variable);\r
1753 Status = EmuSetVariable (\r
1754 GET_VARIABLE_NAME_PTR (Variable),\r
1755 &Variable->VendorGuid,\r
1756 Variable->Attributes,\r
1757 Variable->DataSize,\r
1758 VariableData,\r
1759 &mVariableModuleGlobal->VariableGlobal[Physical],\r
1760 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
1761 &mVariableModuleGlobal->NonVolatileLastVariableOffset\r
1762 );\r
1763 ASSERT_EFI_ERROR (Status);\r
1764 }\r
1765 }\r
1766 }\r
1767 }\r
1768\r
8d3a5c82 1769 return EFI_SUCCESS;\r
1770}\r
1771\r
1794e506 1772/**\r
1773 Initializes variable store area for non-volatile and volatile variable.\r
1774\r
1775 This function allocates and initializes memory space for global context of ESAL\r
1776 variable service and variable store area for non-volatile and volatile variable.\r
1777\r
1778 @param ImageHandle The Image handle of this driver.\r
1779 @param SystemTable The pointer of EFI_SYSTEM_TABLE.\r
1780\r
1781 @retval EFI_SUCCESS Function successfully executed.\r
1782 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
1783\r
1784**/\r
8d3a5c82 1785EFI_STATUS\r
1786EFIAPI\r
1787VariableCommonInitialize (\r
1788 IN EFI_HANDLE ImageHandle,\r
1789 IN EFI_SYSTEM_TABLE *SystemTable\r
1790 )\r
8d3a5c82 1791{\r
1792 EFI_STATUS Status;\r
1793\r
1794 //\r
1795 // Allocate memory for mVariableModuleGlobal\r
1796 //\r
72399dae 1797 mVariableModuleGlobal = (ESAL_VARIABLE_GLOBAL *) AllocateRuntimeZeroPool (\r
8d3a5c82 1798 sizeof (ESAL_VARIABLE_GLOBAL)\r
1799 );\r
1800 if (NULL == mVariableModuleGlobal) {\r
1801 return EFI_OUT_OF_RESOURCES;\r
1802 }\r
1803\r
1804 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);\r
1805\r
1806 //\r
1807 // Intialize volatile variable store\r
1808 //\r
02328a57 1809 Status = InitializeVariableStore (TRUE);\r
8d3a5c82 1810 if (EFI_ERROR (Status)) {\r
5bb820af 1811 FreePool(mVariableModuleGlobal);\r
8d3a5c82 1812 return Status;\r
1813 }\r
1814 //\r
1815 // Intialize non volatile variable store\r
1816 //\r
02328a57 1817 Status = InitializeVariableStore (FALSE);\r
8d3a5c82 1818\r
1819 return Status;\r
1820}\r