]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/VariableAuthenticated/EsalVariableDxeSal/Variable.c
Add security package to repository.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / EsalVariableDxeSal / Variable.c
... / ...
CommitLineData
1/** @file\r
2 The implementation of Extended SAL variable services.\r
3\r
4Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "Variable.h"\r
16#include "AuthService.h"\r
17\r
18//\r
19// Don't use module globals after the SetVirtualAddress map is signaled\r
20//\r
21ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;\r
22CHAR16 *mVariableName[NUM_VAR_NAME] = {\r
23 L"PlatformLangCodes",\r
24 L"LangCodes",\r
25 L"PlatformLang",\r
26 L"Lang",\r
27 L"HwErrRec",\r
28 AUTHVAR_KEYDB_NAME,\r
29 EFI_SETUP_MODE_NAME,\r
30 EFI_PLATFORM_KEY_NAME,\r
31 EFI_KEY_EXCHANGE_KEY_NAME\r
32};\r
33\r
34GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
35\r
36//\r
37// The current Hii implementation accesses this variable a larg # of times on every boot.\r
38// Other common variables are only accessed a single time. This is why this cache algorithm\r
39// only targets a single variable. Probably to get an performance improvement out of\r
40// a Cache you would need a cache that improves the search performance for a variable.\r
41//\r
42VARIABLE_CACHE_ENTRY mVariableCache[] = {\r
43 {\r
44 &gEfiGlobalVariableGuid,\r
45 L"Lang",\r
46 0x00000000,\r
47 0x00,\r
48 NULL\r
49 },\r
50 {\r
51 &gEfiGlobalVariableGuid,\r
52 L"PlatformLang",\r
53 0x00000000,\r
54 0x00,\r
55 NULL\r
56 }\r
57};\r
58\r
59/**\r
60 Acquires lock only at boot time. Simply returns at runtime.\r
61\r
62 This is a temperary function which will be removed when\r
63 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
64 Runtimer driver in RT phase.\r
65 It calls EfiAcquireLock() at boot time, and simply returns\r
66 at runtime.\r
67\r
68 @param[in] Lock A pointer to the lock to acquire.\r
69\r
70**/\r
71VOID\r
72AcquireLockOnlyAtBootTime (\r
73 IN EFI_LOCK *Lock\r
74 )\r
75{\r
76 if (!EfiAtRuntime ()) {\r
77 EfiAcquireLock (Lock);\r
78 }\r
79}\r
80\r
81/**\r
82 Releases lock only at boot time. Simply returns at runtime.\r
83\r
84 This is a temperary function which will be removed when\r
85 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
86 Runtimer driver in RT phase.\r
87 It calls EfiReleaseLock() at boot time, and simply returns\r
88 at runtime\r
89\r
90 @param[in] Lock A pointer to the lock to release.\r
91\r
92**/\r
93VOID\r
94ReleaseLockOnlyAtBootTime (\r
95 IN EFI_LOCK *Lock\r
96 )\r
97{\r
98 if (!EfiAtRuntime ()) {\r
99 EfiReleaseLock (Lock);\r
100 }\r
101}\r
102\r
103/**\r
104 Reads/Writes variable storage, volatile or non-volatile.\r
105\r
106 This function reads or writes volatile or non-volatile variable stroage.\r
107 For volatile storage, it performs memory copy.\r
108 For non-volatile storage, it accesses data on firmware storage. Data\r
109 area to access can span multiple firmware blocks.\r
110\r
111 @param[in] Write TRUE - Write variable store.\r
112 FALSE - Read variable store.\r
113 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
114 @param[in] Volatile TRUE - Variable is volatile.\r
115 FALSE - Variable is non-volatile.\r
116 @param[in] Instance Instance of FV Block services.\r
117 @param[in] StartAddress Start address of data to access.\r
118 @param[in] DataSize Size of data to access.\r
119 @param[in, out] Buffer For write, pointer to the buffer from which data is written.\r
120 For read, pointer to the buffer to hold the data read.\r
121\r
122 @retval EFI_SUCCESS Variable store successfully accessed.\r
123 @retval EFI_INVALID_PARAMETER Data area to access exceeds valid variable storage.\r
124\r
125**/\r
126EFI_STATUS\r
127AccessVariableStore (\r
128 IN BOOLEAN Write,\r
129 IN VARIABLE_GLOBAL *Global,\r
130 IN BOOLEAN Volatile,\r
131 IN UINTN Instance,\r
132 IN EFI_PHYSICAL_ADDRESS StartAddress,\r
133 IN UINT32 DataSize,\r
134 IN OUT VOID *Buffer\r
135 )\r
136{\r
137 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
138 UINTN BlockIndex;\r
139 UINTN LinearOffset;\r
140 UINTN CurrWriteSize;\r
141 UINTN CurrWritePtr;\r
142 UINT8 *CurrBuffer;\r
143 EFI_LBA LbaNumber;\r
144 UINTN Size;\r
145 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
146 VARIABLE_STORE_HEADER *VolatileBase;\r
147 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
148 EFI_STATUS Status;\r
149 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
150\r
151 FvVolHdr = 0;\r
152 FwVolHeader = NULL;\r
153\r
154 if (Volatile) {\r
155 //\r
156 // If data is volatile, simply calculate the data pointer and copy memory.\r
157 // Data pointer should point to the actual address where data is to be\r
158 // accessed.\r
159 //\r
160 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
161\r
162 if ((StartAddress + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
163 return EFI_INVALID_PARAMETER;\r
164 }\r
165 \r
166 //\r
167 // For volatile variable, a simple memory copy is enough.\r
168 //\r
169 if (Write) {\r
170 CopyMem ((VOID *) StartAddress, Buffer, DataSize);\r
171 } else {\r
172 CopyMem (Buffer, (VOID *) StartAddress, DataSize);\r
173 }\r
174\r
175 return EFI_SUCCESS;\r
176 }\r
177\r
178 //\r
179 // If data is non-volatile, calculate firmware volume header and data pointer.\r
180 //\r
181 Status = (EFI_STATUS) EsalCall (\r
182 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
183 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
184 GetPhysicalAddressFunctionId, \r
185 Instance, \r
186 (UINT64) &FvVolHdr, \r
187 0, \r
188 0, \r
189 0, \r
190 0, \r
191 0\r
192 ).Status;\r
193 ASSERT_EFI_ERROR (Status);\r
194\r
195 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
196 ASSERT (FwVolHeader != NULL);\r
197 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(FwVolHeader + 1);\r
198\r
199 if ((StartAddress + DataSize) > ((EFI_PHYSICAL_ADDRESS) (UINTN) ((CHAR8 *)VariableStoreHeader + VariableStoreHeader->Size))) {\r
200 return EFI_INVALID_PARAMETER;\r
201 }\r
202 \r
203 LinearOffset = (UINTN) FwVolHeader;\r
204 CurrWritePtr = StartAddress;\r
205 CurrWriteSize = DataSize;\r
206 CurrBuffer = Buffer;\r
207 LbaNumber = 0;\r
208\r
209 if (CurrWritePtr < LinearOffset) {\r
210 return EFI_INVALID_PARAMETER;\r
211 }\r
212\r
213 //\r
214 // Traverse data blocks of this firmware storage to find the one where CurrWritePtr locates\r
215 //\r
216 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
217 for (BlockIndex = 0; BlockIndex < PtrBlockMapEntry->NumBlocks; BlockIndex++) {\r
218 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
219 //\r
220 // Check to see if the data area to access spans multiple blocks.\r
221 //\r
222 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
223 //\r
224 // If data area to access is contained in one block, just access and return.\r
225 //\r
226 if (Write) {\r
227 Status = (EFI_STATUS) EsalCall (\r
228 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
229 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
230 WriteFunctionId, \r
231 Instance, \r
232 LbaNumber, \r
233 (CurrWritePtr - LinearOffset), \r
234 (UINT64) &CurrWriteSize, \r
235 (UINT64) CurrBuffer, \r
236 0, \r
237 0\r
238 ).Status;\r
239 } else {\r
240 Status = (EFI_STATUS) EsalCall (\r
241 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
242 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
243 ReadFunctionId, \r
244 Instance, \r
245 LbaNumber, \r
246 (CurrWritePtr - LinearOffset), \r
247 (UINT64) &CurrWriteSize, \r
248 (UINT64) CurrBuffer, \r
249 0, \r
250 0\r
251 ).Status;\r
252 }\r
253 return Status;\r
254 } else {\r
255 //\r
256 // If data area to access spans multiple blocks, access this one and adjust for the next one.\r
257 //\r
258 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
259 if (Write) {\r
260 Status = (EFI_STATUS) EsalCall (\r
261 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
262 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
263 WriteFunctionId, \r
264 Instance, \r
265 LbaNumber, \r
266 (CurrWritePtr - LinearOffset), \r
267 (UINT64) &Size, \r
268 (UINT64) CurrBuffer, \r
269 0, \r
270 0\r
271 ).Status;\r
272 } else {\r
273 Status = (EFI_STATUS) EsalCall (\r
274 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
275 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
276 ReadFunctionId, \r
277 Instance, \r
278 LbaNumber, \r
279 (CurrWritePtr - LinearOffset), \r
280 (UINT64) &Size, \r
281 (UINT64) CurrBuffer, \r
282 0, \r
283 0\r
284 ).Status;\r
285 }\r
286 if (EFI_ERROR (Status)) {\r
287 return Status;\r
288 }\r
289 //\r
290 // Adjust for the remaining data.\r
291 //\r
292 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
293 CurrBuffer = CurrBuffer + Size;\r
294 CurrWriteSize = CurrWriteSize - Size;\r
295 }\r
296 }\r
297\r
298 LinearOffset += PtrBlockMapEntry->Length;\r
299 LbaNumber++;\r
300 }\r
301 }\r
302\r
303 return EFI_SUCCESS;\r
304}\r
305\r
306/**\r
307 Retrieves header of volatile or non-volatile variable stroage.\r
308\r
309 @param[in] VarStoreAddress Start address of variable storage.\r
310 @param[in] Volatile TRUE - Variable storage is volatile.\r
311 FALSE - Variable storage is non-volatile.\r
312 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
313 @param[in] Instance Instance of FV Block services.\r
314 @param[out] VarStoreHeader Pointer to VARIABLE_STORE_HEADER for output.\r
315\r
316**/\r
317VOID\r
318GetVarStoreHeader (\r
319 IN EFI_PHYSICAL_ADDRESS VarStoreAddress,\r
320 IN BOOLEAN Volatile,\r
321 IN VARIABLE_GLOBAL *Global,\r
322 IN UINTN Instance,\r
323 OUT VARIABLE_STORE_HEADER *VarStoreHeader\r
324 )\r
325{\r
326 EFI_STATUS Status;\r
327\r
328 Status = AccessVariableStore (\r
329 FALSE,\r
330 Global,\r
331 Volatile,\r
332 Instance,\r
333 VarStoreAddress,\r
334 sizeof (VARIABLE_STORE_HEADER),\r
335 VarStoreHeader \r
336 );\r
337 ASSERT_EFI_ERROR (Status);\r
338}\r
339\r
340/**\r
341 Checks variable header.\r
342\r
343 This function checks if variable header is valid or not.\r
344\r
345 @param[in] VariableAddress Start address of variable header.\r
346 @param[in] Volatile TRUE - Variable is volatile.\r
347 FALSE - Variable is non-volatile.\r
348 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
349 @param[in] Instance Instance of FV Block services.\r
350 @param[out] VariableHeader Pointer to VARIABLE_HEADER for output.\r
351\r
352 @retval TRUE Variable header is valid.\r
353 @retval FALSE Variable header is not valid.\r
354\r
355**/\r
356BOOLEAN\r
357IsValidVariableHeader (\r
358 IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
359 IN BOOLEAN Volatile,\r
360 IN VARIABLE_GLOBAL *Global,\r
361 IN UINTN Instance,\r
362 OUT VARIABLE_HEADER *VariableHeader OPTIONAL\r
363 )\r
364{\r
365 EFI_STATUS Status;\r
366 VARIABLE_HEADER LocalVariableHeader;\r
367\r
368 Status = AccessVariableStore (\r
369 FALSE,\r
370 Global,\r
371 Volatile,\r
372 Instance,\r
373 VariableAddress,\r
374 sizeof (VARIABLE_HEADER),\r
375 &LocalVariableHeader \r
376 );\r
377\r
378 if (EFI_ERROR (Status) || LocalVariableHeader.StartId != VARIABLE_DATA) {\r
379 return FALSE;\r
380 }\r
381\r
382 if (VariableHeader != NULL) {\r
383 CopyMem (VariableHeader, &LocalVariableHeader, sizeof (VARIABLE_HEADER));\r
384 }\r
385\r
386 return TRUE;\r
387}\r
388\r
389/**\r
390 Gets status of variable store.\r
391\r
392 This function gets the current status of variable store.\r
393\r
394 @param[in] VarStoreHeader Pointer to header of variable store.\r
395\r
396 @retval EfiRaw Variable store status is raw.\r
397 @retval EfiValid Variable store status is valid.\r
398 @retval EfiInvalid Variable store status is invalid.\r
399\r
400**/\r
401VARIABLE_STORE_STATUS\r
402GetVariableStoreStatus (\r
403 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
404 )\r
405{\r
406\r
407 if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&\r
408 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
409 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
410 ) {\r
411\r
412 return EfiValid;\r
413 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
414 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
415 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
416 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
417 VarStoreHeader->Size == 0xffffffff &&\r
418 VarStoreHeader->Format == 0xff &&\r
419 VarStoreHeader->State == 0xff\r
420 ) {\r
421\r
422 return EfiRaw;\r
423 } else {\r
424 return EfiInvalid;\r
425 }\r
426}\r
427\r
428/**\r
429 Gets the size of variable name.\r
430\r
431 This function gets the size of variable name.\r
432 The variable is specified by its variable header.\r
433 If variable header contains raw data, just return 0.\r
434\r
435 @param[in] Variable Pointer to the variable header.\r
436\r
437 @return Size of variable name in bytes.\r
438\r
439**/\r
440UINTN\r
441NameSizeOfVariable (\r
442 IN VARIABLE_HEADER *Variable\r
443 )\r
444{\r
445 if (Variable->State == (UINT8) (-1) ||\r
446 Variable->DataSize == (UINT32) -1 ||\r
447 Variable->NameSize == (UINT32) -1 ||\r
448 Variable->Attributes == (UINT32) -1) {\r
449 return 0;\r
450 }\r
451 return (UINTN) Variable->NameSize;\r
452}\r
453\r
454/**\r
455 Gets the size of variable data area.\r
456\r
457 This function gets the size of variable data area.\r
458 The variable is specified by its variable header.\r
459 If variable header contains raw data, just return 0.\r
460\r
461 @param[in] Variable Pointer to the variable header.\r
462\r
463 @return Size of variable data area in bytes.\r
464\r
465**/\r
466UINTN\r
467DataSizeOfVariable (\r
468 IN VARIABLE_HEADER *Variable\r
469 )\r
470{\r
471 if (Variable->State == (UINT8) -1 ||\r
472 Variable->DataSize == (UINT32) -1 ||\r
473 Variable->NameSize == (UINT32) -1 ||\r
474 Variable->Attributes == (UINT32) -1) {\r
475 return 0;\r
476 }\r
477 return (UINTN) Variable->DataSize;\r
478}\r
479\r
480/**\r
481 Gets the pointer to variable name.\r
482\r
483 This function gets the pointer to variable name.\r
484 The variable is specified by its variable header.\r
485\r
486 @param[in] VariableAddress Start address of variable header.\r
487 @param[in] Volatile TRUE - Variable is volatile.\r
488 FALSE - Variable is non-volatile.\r
489 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
490 @param[in] Instance Instance of FV Block services.\r
491 @param[out] VariableName Buffer to hold variable name for output.\r
492\r
493**/\r
494VOID\r
495GetVariableNamePtr (\r
496 IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
497 IN BOOLEAN Volatile,\r
498 IN VARIABLE_GLOBAL *Global,\r
499 IN UINTN Instance,\r
500 OUT CHAR16 *VariableName\r
501 )\r
502{\r
503 EFI_STATUS Status;\r
504 EFI_PHYSICAL_ADDRESS Address;\r
505 VARIABLE_HEADER VariableHeader;\r
506 BOOLEAN IsValid;\r
507\r
508 IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);\r
509 ASSERT (IsValid);\r
510\r
511 //\r
512 // Name area follows variable header.\r
513 //\r
514 Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
515\r
516 Status = AccessVariableStore (\r
517 FALSE,\r
518 Global,\r
519 Volatile,\r
520 Instance,\r
521 Address,\r
522 VariableHeader.NameSize,\r
523 VariableName \r
524 );\r
525 ASSERT_EFI_ERROR (Status);\r
526}\r
527\r
528/**\r
529 Gets the pointer to variable data area.\r
530\r
531 This function gets the pointer to variable data area.\r
532 The variable is specified by its variable header.\r
533\r
534 @param[in] VariableAddress Start address of variable header.\r
535 @param[in] Volatile TRUE - Variable is volatile.\r
536 FALSE - Variable is non-volatile.\r
537 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
538 @param[in] Instance Instance of FV Block services.\r
539 @param[out] VariableData Buffer to hold variable data for output.\r
540\r
541**/\r
542VOID\r
543GetVariableDataPtr (\r
544 IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
545 IN BOOLEAN Volatile,\r
546 IN VARIABLE_GLOBAL *Global,\r
547 IN UINTN Instance,\r
548 OUT CHAR16 *VariableData\r
549 )\r
550{\r
551 EFI_STATUS Status;\r
552 EFI_PHYSICAL_ADDRESS Address;\r
553 VARIABLE_HEADER VariableHeader;\r
554 BOOLEAN IsValid;\r
555\r
556 IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);\r
557 ASSERT (IsValid);\r
558\r
559 //\r
560 // Data area follows variable name.\r
561 // Be careful about pad size for alignment\r
562 //\r
563 Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
564 Address += NameSizeOfVariable (&VariableHeader);\r
565 Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));\r
566\r
567 Status = AccessVariableStore (\r
568 FALSE,\r
569 Global,\r
570 Volatile,\r
571 Instance,\r
572 Address,\r
573 VariableHeader.DataSize,\r
574 VariableData \r
575 );\r
576 ASSERT_EFI_ERROR (Status);\r
577}\r
578\r
579\r
580/**\r
581 Gets the pointer to the next variable header.\r
582\r
583 This function gets the pointer to the next variable header.\r
584 The variable is specified by its variable header.\r
585\r
586 @param[in] VariableAddress Start address of variable header.\r
587 @param[in] Volatile TRUE - Variable is volatile.\r
588 FALSE - Variable is non-volatile.\r
589 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
590 @param[in] Instance Instance of FV Block services.\r
591\r
592 @return Pointer to the next variable header.\r
593 NULL if variable header is invalid.\r
594\r
595**/\r
596EFI_PHYSICAL_ADDRESS\r
597GetNextVariablePtr (\r
598 IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
599 IN BOOLEAN Volatile,\r
600 IN VARIABLE_GLOBAL *Global,\r
601 IN UINTN Instance\r
602 )\r
603{\r
604 EFI_PHYSICAL_ADDRESS Address;\r
605 VARIABLE_HEADER VariableHeader;\r
606\r
607 if (!IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader)) {\r
608 return 0x0;\r
609 }\r
610\r
611 //\r
612 // Header of next variable follows data area of this variable\r
613 //\r
614 Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
615 Address += NameSizeOfVariable (&VariableHeader);\r
616 Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));\r
617 Address += DataSizeOfVariable (&VariableHeader);\r
618 Address += GET_PAD_SIZE (DataSizeOfVariable (&VariableHeader));\r
619\r
620 //\r
621 // Be careful about pad size for alignment\r
622 //\r
623 return HEADER_ALIGN (Address);\r
624}\r
625\r
626/**\r
627 Gets the pointer to the first variable header in given variable store area.\r
628\r
629 This function gets the pointer to the first variable header in given variable \r
630 store area. The variable store area is given by its start address.\r
631\r
632 @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.\r
633\r
634 @return Pointer to the first variable header.\r
635\r
636**/\r
637EFI_PHYSICAL_ADDRESS\r
638GetStartPointer (\r
639 IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress\r
640 )\r
641{\r
642 return HEADER_ALIGN (VarStoreHeaderAddress + sizeof (VARIABLE_STORE_HEADER));\r
643}\r
644\r
645/**\r
646 Gets the pointer to the end of given variable store area.\r
647\r
648 This function gets the pointer to the end of given variable store area.\r
649 The variable store area is given by its start address.\r
650\r
651 @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.\r
652 @param[in] Volatile TRUE - Variable is volatile.\r
653 FALSE - Variable is non-volatile.\r
654 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
655 @param[in] Instance Instance of FV Block services.\r
656\r
657 @return Pointer to the end of given variable store area.\r
658\r
659**/\r
660EFI_PHYSICAL_ADDRESS\r
661GetEndPointer (\r
662 IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress,\r
663 IN BOOLEAN Volatile,\r
664 IN VARIABLE_GLOBAL *Global,\r
665 IN UINTN Instance\r
666 )\r
667{\r
668 EFI_STATUS Status;\r
669 VARIABLE_STORE_HEADER VariableStoreHeader;\r
670\r
671 Status = AccessVariableStore (\r
672 FALSE,\r
673 Global,\r
674 Volatile,\r
675 Instance,\r
676 VarStoreHeaderAddress,\r
677 sizeof (VARIABLE_STORE_HEADER),\r
678 &VariableStoreHeader \r
679 );\r
680\r
681 ASSERT_EFI_ERROR (Status);\r
682 return HEADER_ALIGN (VarStoreHeaderAddress + VariableStoreHeader.Size);\r
683}\r
684\r
685/**\r
686 Updates variable info entry in EFI system table for statistical information.\r
687\r
688 Routine used to track statistical information about variable usage. \r
689 The data is stored in the EFI system table so it can be accessed later.\r
690 VariableInfo.efi can dump out the table. Only Boot Services variable \r
691 accesses are tracked by this code. The PcdVariableCollectStatistics\r
692 build flag controls if this feature is enabled. \r
693 A read that hits in the cache will have Read and Cache true for \r
694 the transaction. Data is allocated by this routine, but never\r
695 freed.\r
696\r
697 @param[in] VariableName Name of the Variable to track.\r
698 @param[in] VendorGuid Guid of the Variable to track.\r
699 @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
700 @param[in] Read TRUE if GetVariable() was called.\r
701 @param[in] Write TRUE if SetVariable() was called.\r
702 @param[in] Delete TRUE if deleted via SetVariable().\r
703 @param[in] Cache TRUE for a cache hit.\r
704\r
705**/\r
706VOID\r
707UpdateVariableInfo (\r
708 IN CHAR16 *VariableName,\r
709 IN EFI_GUID *VendorGuid,\r
710 IN BOOLEAN Volatile,\r
711 IN BOOLEAN Read,\r
712 IN BOOLEAN Write,\r
713 IN BOOLEAN Delete,\r
714 IN BOOLEAN Cache\r
715 )\r
716{\r
717 VARIABLE_INFO_ENTRY *Entry;\r
718\r
719 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
720\r
721 if (EfiAtRuntime ()) {\r
722 //\r
723 // Don't collect statistics at runtime\r
724 //\r
725 return;\r
726 }\r
727\r
728 if (gVariableInfo == NULL) {\r
729 //\r
730 // on the first call allocate a entry and place a pointer to it in\r
731 // the EFI System Table\r
732 //\r
733 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
734 ASSERT (gVariableInfo != NULL);\r
735\r
736 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
737 gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
738 ASSERT (gVariableInfo->Name != NULL);\r
739 StrCpy (gVariableInfo->Name, VariableName);\r
740 gVariableInfo->Volatile = Volatile;\r
741\r
742 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
743 }\r
744\r
745 \r
746 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
747 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
748 if (StrCmp (VariableName, Entry->Name) == 0) {\r
749 //\r
750 // Find the entry matching both variable name and vender GUID,\r
751 // and update counters for all types.\r
752 //\r
753 if (Read) {\r
754 Entry->ReadCount++;\r
755 }\r
756 if (Write) {\r
757 Entry->WriteCount++;\r
758 }\r
759 if (Delete) {\r
760 Entry->DeleteCount++;\r
761 }\r
762 if (Cache) {\r
763 Entry->CacheCount++;\r
764 }\r
765\r
766 return;\r
767 }\r
768 }\r
769\r
770 if (Entry->Next == NULL) {\r
771 //\r
772 // If the entry is not in the table add it.\r
773 // Next iteration of the loop will fill in the data\r
774 //\r
775 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
776 ASSERT (Entry->Next != NULL);\r
777\r
778 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
779 Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
780 ASSERT (Entry->Next->Name != NULL);\r
781 StrCpy (Entry->Next->Name, VariableName);\r
782 Entry->Next->Volatile = Volatile;\r
783 }\r
784\r
785 }\r
786 }\r
787}\r
788\r
789/**\r
790 Updates variable in cache.\r
791\r
792 This function searches the variable cache. If the variable to set exists in the cache,\r
793 it updates the variable in cache. It has the same parameters with UEFI SetVariable()\r
794 service.\r
795\r
796 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
797 variable. Each VariableName is unique for each VendorGuid.\r
798 @param[in] VendorGuid A unique identifier for the vendor.\r
799 @param[in] Attributes Attributes bitmask to set for the variable.\r
800 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the\r
801 variable to be deleted.\r
802 @param[in] Data The contents for the variable.\r
803\r
804**/\r
805VOID\r
806UpdateVariableCache (\r
807 IN CHAR16 *VariableName,\r
808 IN EFI_GUID *VendorGuid,\r
809 IN UINT32 Attributes,\r
810 IN UINTN DataSize,\r
811 IN VOID *Data\r
812 )\r
813{\r
814 VARIABLE_CACHE_ENTRY *Entry;\r
815 UINTN Index;\r
816\r
817 if (EfiAtRuntime ()) {\r
818 //\r
819 // Don't use the cache at runtime\r
820 //\r
821 return;\r
822 }\r
823\r
824 //\r
825 // Searches cache for the variable to update. If it exists, update it.\r
826 //\r
827 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
828 if (CompareGuid (VendorGuid, Entry->Guid)) {\r
829 if (StrCmp (VariableName, Entry->Name) == 0) { \r
830 Entry->Attributes = Attributes;\r
831 if (DataSize == 0) {\r
832 //\r
833 // If DataSize is 0, delete the variable.\r
834 //\r
835 if (Entry->DataSize != 0) {\r
836 FreePool (Entry->Data);\r
837 }\r
838 Entry->DataSize = DataSize;\r
839 } else if (DataSize == Entry->DataSize) {\r
840 //\r
841 // If size of data does not change, simply copy data\r
842 //\r
843 CopyMem (Entry->Data, Data, DataSize);\r
844 } else {\r
845 //\r
846 // If size of data changes, allocate pool and copy data.\r
847 //\r
848 Entry->Data = AllocatePool (DataSize);\r
849 Entry->DataSize = DataSize;\r
850 CopyMem (Entry->Data, Data, DataSize);\r
851 }\r
852 }\r
853 }\r
854 }\r
855}\r
856\r
857\r
858/**\r
859 Search the cache to check if the variable is in it.\r
860\r
861 This function searches the variable cache. If the variable to find exists, return its data\r
862 and attributes.\r
863\r
864 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
865 variable. Each VariableName is unique for each VendorGuid.\r
866 @param[in] VendorGuid A unique identifier for the vendor\r
867 @param[out] Attributes Pointer to the attributes bitmask of the variable for output.\r
868 @param[in, out] DataSize On input, size of the buffer of Data.\r
869 On output, size of the variable's data.\r
870 @param[out] Data Pointer to the data buffer for output.\r
871\r
872 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.\r
873 @retval EFI_NOT_FOUND No matching variable found in cache.\r
874 @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.\r
875\r
876**/\r
877EFI_STATUS\r
878FindVariableInCache (\r
879 IN CHAR16 *VariableName,\r
880 IN EFI_GUID *VendorGuid,\r
881 OUT UINT32 *Attributes OPTIONAL,\r
882 IN OUT UINTN *DataSize,\r
883 OUT VOID *Data\r
884 )\r
885{\r
886 VARIABLE_CACHE_ENTRY *Entry;\r
887 UINTN Index;\r
888\r
889 if (EfiAtRuntime ()) {\r
890 //\r
891 // Don't use the cache at runtime\r
892 //\r
893 return EFI_NOT_FOUND;\r
894 }\r
895\r
896 //\r
897 // Searches cache for the variable\r
898 //\r
899 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
900 if (CompareGuid (VendorGuid, Entry->Guid)) {\r
901 if (StrCmp (VariableName, Entry->Name) == 0) {\r
902 if (Entry->DataSize == 0) {\r
903 //\r
904 // Variable has been deleted so return EFI_NOT_FOUND\r
905 //\r
906 return EFI_NOT_FOUND;\r
907 } else if (Entry->DataSize > *DataSize) {\r
908 //\r
909 // If buffer is too small, return the size needed and EFI_BUFFER_TOO_SMALL\r
910 //\r
911 *DataSize = Entry->DataSize;\r
912 return EFI_BUFFER_TOO_SMALL;\r
913 } else {\r
914 //\r
915 // If buffer is large enough, return the data\r
916 //\r
917 *DataSize = Entry->DataSize;\r
918 CopyMem (Data, Entry->Data, Entry->DataSize);\r
919 //\r
920 // If Attributes is not NULL, return the variable's attribute.\r
921 //\r
922 if (Attributes != NULL) {\r
923 *Attributes = Entry->Attributes;\r
924 }\r
925 return EFI_SUCCESS;\r
926 }\r
927 }\r
928 }\r
929 }\r
930 \r
931 return EFI_NOT_FOUND;\r
932}\r
933\r
934/**\r
935 Finds variable in volatile and non-volatile storage areas.\r
936\r
937 This code finds variable in volatile and non-volatile storage areas.\r
938 If VariableName is an empty string, then we just return the first\r
939 qualified variable without comparing VariableName and VendorGuid.\r
940 Otherwise, VariableName and VendorGuid are compared.\r
941\r
942 @param[in] VariableName Name of the variable to be found.\r
943 @param[in] VendorGuid Vendor GUID to be found.\r
944 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
945 including the range searched and the target position.\r
946 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
947 base of volatile variable storage area, base of\r
948 NV variable storage area, and a lock.\r
949 @param[in] Instance Instance of FV Block services.\r
950\r
951 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
952 VendorGuid is NULL.\r
953 @retval EFI_SUCCESS Variable successfully found.\r
954 @retval EFI_INVALID_PARAMETER Variable not found.\r
955\r
956**/\r
957EFI_STATUS\r
958FindVariable (\r
959 IN CHAR16 *VariableName,\r
960 IN EFI_GUID *VendorGuid,\r
961 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
962 IN VARIABLE_GLOBAL *Global,\r
963 IN UINTN Instance\r
964 )\r
965{\r
966 EFI_PHYSICAL_ADDRESS Variable[2];\r
967 EFI_PHYSICAL_ADDRESS InDeletedVariable;\r
968 EFI_PHYSICAL_ADDRESS VariableStoreHeader[2];\r
969 UINTN InDeletedStorageIndex;\r
970 UINTN Index;\r
971 CHAR16 LocalVariableName[MAX_NAME_SIZE];\r
972 BOOLEAN Volatile;\r
973 VARIABLE_HEADER VariableHeader;\r
974\r
975 //\r
976 // 0: Volatile, 1: Non-Volatile\r
977 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
978 // make use of this mapping to implement search algorithme.\r
979 //\r
980 VariableStoreHeader[0] = Global->VolatileVariableBase;\r
981 VariableStoreHeader[1] = Global->NonVolatileVariableBase;\r
982\r
983 //\r
984 // Start Pointers for the variable.\r
985 // Actual Data Pointer where data can be written.\r
986 //\r
987 Variable[0] = GetStartPointer (VariableStoreHeader[0]);\r
988 Variable[1] = GetStartPointer (VariableStoreHeader[1]);\r
989\r
990 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
991 return EFI_INVALID_PARAMETER;\r
992 }\r
993\r
994 //\r
995 // Find the variable by walk through volatile and then non-volatile variable store\r
996 //\r
997 InDeletedVariable = 0x0;\r
998 InDeletedStorageIndex = 0;\r
999 Volatile = TRUE;\r
1000 for (Index = 0; Index < 2; Index++) {\r
1001 if (Index == 1) {\r
1002 Volatile = FALSE;\r
1003 }\r
1004 while (IsValidVariableHeader (Variable[Index], Volatile, Global, Instance, &VariableHeader)) {\r
1005 if (VariableHeader.State == VAR_ADDED || \r
1006 VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
1007 ) {\r
1008 if (!EfiAtRuntime () || ((VariableHeader.Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
1009 if (VariableName[0] == 0) {\r
1010 //\r
1011 // If VariableName is an empty string, then we just find the first qualified variable\r
1012 // without comparing VariableName and VendorGuid\r
1013 //\r
1014 if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1015 //\r
1016 // If variable is in delete transition, record it.\r
1017 //\r
1018 InDeletedVariable = Variable[Index];\r
1019 InDeletedStorageIndex = Index;\r
1020 } else {\r
1021 //\r
1022 // If variable is not in delete transition, return it.\r
1023 //\r
1024 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r
1025 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index], Volatile, Global, Instance);\r
1026 PtrTrack->CurrPtr = Variable[Index];\r
1027 PtrTrack->Volatile = Volatile;\r
1028\r
1029 return EFI_SUCCESS;\r
1030 }\r
1031 } else {\r
1032 //\r
1033 // If VariableName is not an empty string, then VariableName and VendorGuid are compared.\r
1034 //\r
1035 if (CompareGuid (VendorGuid, &VariableHeader.VendorGuid)) {\r
1036 GetVariableNamePtr (\r
1037 Variable[Index],\r
1038 Volatile,\r
1039 Global,\r
1040 Instance,\r
1041 LocalVariableName\r
1042 );\r
1043\r
1044 ASSERT (NameSizeOfVariable (&VariableHeader) != 0);\r
1045 if (CompareMem (VariableName, LocalVariableName, NameSizeOfVariable (&VariableHeader)) == 0) {\r
1046 if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1047 //\r
1048 // If variable is in delete transition, record it.\r
1049 // We will use if only no VAR_ADDED variable is found.\r
1050 //\r
1051 InDeletedVariable = Variable[Index];\r
1052 InDeletedStorageIndex = Index;\r
1053 } else {\r
1054 //\r
1055 // If variable is not in delete transition, return it.\r
1056 //\r
1057 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r
1058 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index], Volatile, Global, Instance);\r
1059 PtrTrack->CurrPtr = Variable[Index];\r
1060 PtrTrack->Volatile = Volatile;\r
1061\r
1062 return EFI_SUCCESS;\r
1063 }\r
1064 }\r
1065 }\r
1066 }\r
1067 }\r
1068 }\r
1069\r
1070 Variable[Index] = GetNextVariablePtr (\r
1071 Variable[Index],\r
1072 Volatile,\r
1073 Global,\r
1074 Instance\r
1075 );\r
1076 }\r
1077 if (InDeletedVariable != 0x0) {\r
1078 //\r
1079 // If no VAR_ADDED variable is found, and only variable in delete transition, then use this one.\r
1080 //\r
1081 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);\r
1082 PtrTrack->EndPtr = GetEndPointer (\r
1083 VariableStoreHeader[InDeletedStorageIndex],\r
1084 (BOOLEAN)(InDeletedStorageIndex == 0),\r
1085 Global,\r
1086 Instance\r
1087 );\r
1088 PtrTrack->CurrPtr = InDeletedVariable;\r
1089 PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);\r
1090 return EFI_SUCCESS;\r
1091 }\r
1092 }\r
1093 PtrTrack->CurrPtr = 0x0;\r
1094 return EFI_NOT_FOUND;\r
1095}\r
1096\r
1097/**\r
1098 Variable store garbage collection and reclaim operation.\r
1099\r
1100 @param[in] VariableBase Base address of variable store area.\r
1101 @param[out] LastVariableOffset Offset of last variable.\r
1102 @param[in] IsVolatile The variable store is volatile or not,\r
1103 if it is non-volatile, need FTW.\r
1104 @param[in] VirtualMode Current calling mode for this function.\r
1105 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
1106 @param[in] UpdatingVariable Pointer to header of the variable that is being updated.\r
1107\r
1108 @retval EFI_SUCCESS Variable store successfully reclaimed.\r
1109 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory buffer to hold all valid variables.\r
1110\r
1111**/\r
1112EFI_STATUS\r
1113Reclaim (\r
1114 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
1115 OUT UINTN *LastVariableOffset,\r
1116 IN BOOLEAN IsVolatile,\r
1117 IN BOOLEAN VirtualMode,\r
1118 IN ESAL_VARIABLE_GLOBAL *Global,\r
1119 IN EFI_PHYSICAL_ADDRESS UpdatingVariable\r
1120 )\r
1121{\r
1122 EFI_PHYSICAL_ADDRESS Variable;\r
1123 EFI_PHYSICAL_ADDRESS AddedVariable;\r
1124 EFI_PHYSICAL_ADDRESS NextVariable;\r
1125 EFI_PHYSICAL_ADDRESS NextAddedVariable;\r
1126 VARIABLE_STORE_HEADER VariableStoreHeader;\r
1127 VARIABLE_HEADER VariableHeader;\r
1128 VARIABLE_HEADER AddedVariableHeader;\r
1129 CHAR16 VariableName[MAX_NAME_SIZE];\r
1130 CHAR16 AddedVariableName[MAX_NAME_SIZE];\r
1131 UINT8 *ValidBuffer;\r
1132 UINTN MaximumBufferSize;\r
1133 UINTN VariableSize;\r
1134 UINTN NameSize;\r
1135 UINT8 *CurrPtr;\r
1136 BOOLEAN FoundAdded;\r
1137 EFI_STATUS Status;\r
1138 VARIABLE_GLOBAL *VariableGlobal;\r
1139 UINT32 Instance;\r
1140\r
1141 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
1142 Instance = Global->FvbInstance;\r
1143\r
1144 GetVarStoreHeader (VariableBase, IsVolatile, VariableGlobal, Instance, &VariableStoreHeader);\r
1145 //\r
1146 // recaluate the total size of Common/HwErr type variables in non-volatile area.\r
1147 //\r
1148 if (!IsVolatile) {\r
1149 Global->CommonVariableTotalSize = 0;\r
1150 Global->HwErrVariableTotalSize = 0;\r
1151 }\r
1152\r
1153 //\r
1154 // Calculate the size of buffer needed to gather all valid variables\r
1155 //\r
1156 Variable = GetStartPointer (VariableBase);\r
1157 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
1158\r
1159 while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {\r
1160 NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);\r
1161 //\r
1162 // Collect VAR_ADDED variables, and variables in delete transition status.\r
1163 //\r
1164 if (VariableHeader.State == VAR_ADDED || \r
1165 VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
1166 ) {\r
1167 VariableSize = NextVariable - Variable;\r
1168 MaximumBufferSize += VariableSize;\r
1169 }\r
1170\r
1171 Variable = NextVariable;\r
1172 }\r
1173\r
1174 //\r
1175 // Reserve the 1 Bytes with Oxff to identify the \r
1176 // end of the variable buffer. \r
1177 // \r
1178 MaximumBufferSize += 1;\r
1179 ValidBuffer = AllocatePool (MaximumBufferSize);\r
1180 if (ValidBuffer == NULL) {\r
1181 return EFI_OUT_OF_RESOURCES;\r
1182 }\r
1183\r
1184 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
1185\r
1186 //\r
1187 // Copy variable store header\r
1188 //\r
1189 CopyMem (ValidBuffer, &VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
1190 CurrPtr = (UINT8 *) GetStartPointer ((EFI_PHYSICAL_ADDRESS) ValidBuffer);\r
1191\r
1192 //\r
1193 // Reinstall all ADDED variables\r
1194 // \r
1195 Variable = GetStartPointer (VariableBase);\r
1196 while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {\r
1197 NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);\r
1198 if (VariableHeader.State == VAR_ADDED) {\r
1199 VariableSize = NextVariable - Variable;\r
1200 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
1201 CurrPtr += VariableSize;\r
1202 if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1203 Global->HwErrVariableTotalSize += VariableSize;\r
1204 } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1205 Global->CommonVariableTotalSize += VariableSize;\r
1206 }\r
1207 }\r
1208 Variable = NextVariable;\r
1209 }\r
1210 //\r
1211 // Reinstall in delete transition variables\r
1212 // \r
1213 Variable = GetStartPointer (VariableBase);\r
1214 while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {\r
1215 NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);\r
1216 if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1217\r
1218 //\r
1219 // Buffer has cached all ADDED variable. \r
1220 // Per IN_DELETED variable, we have to guarantee that\r
1221 // no ADDED one in previous buffer. \r
1222 // \r
1223 FoundAdded = FALSE;\r
1224 AddedVariable = GetStartPointer ((EFI_PHYSICAL_ADDRESS) ValidBuffer);\r
1225 while (IsValidVariableHeader (AddedVariable, IsVolatile, VariableGlobal, Instance, &AddedVariableHeader)) {\r
1226 NextAddedVariable = GetNextVariablePtr (AddedVariable, IsVolatile, VariableGlobal, Instance);\r
1227 NameSize = NameSizeOfVariable (&AddedVariableHeader);\r
1228 if (CompareGuid (&AddedVariableHeader.VendorGuid, &VariableHeader.VendorGuid) &&\r
1229 NameSize == NameSizeOfVariable (&VariableHeader)\r
1230 ) {\r
1231 GetVariableNamePtr (Variable, IsVolatile, VariableGlobal, Instance, VariableName);\r
1232 GetVariableNamePtr (AddedVariable, IsVolatile, VariableGlobal, Instance, AddedVariableName);\r
1233 if (CompareMem (VariableName, AddedVariableName, NameSize) == 0) {\r
1234 //\r
1235 // If ADDED variable with the same name and vender GUID has been reinstalled,\r
1236 // then discard this IN_DELETED copy.\r
1237 //\r
1238 FoundAdded = TRUE;\r
1239 break;\r
1240 }\r
1241 }\r
1242 AddedVariable = NextAddedVariable;\r
1243 }\r
1244 //\r
1245 // Add IN_DELETE variables that have not been added to buffer\r
1246 //\r
1247 if (!FoundAdded) {\r
1248 VariableSize = NextVariable - Variable;\r
1249 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
1250 if (Variable != UpdatingVariable) {\r
1251 //\r
1252 // Make this IN_DELETE instance valid if:\r
1253 // 1. No valid instance of this variable exists.\r
1254 // 2. It is not the variable that is going to be updated.\r
1255 //\r
1256 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
1257 }\r
1258 CurrPtr += VariableSize;\r
1259 if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1260 Global->HwErrVariableTotalSize += VariableSize;\r
1261 } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1262 Global->CommonVariableTotalSize += VariableSize;\r
1263 }\r
1264 }\r
1265 }\r
1266 Variable = NextVariable;\r
1267 }\r
1268\r
1269 if (IsVolatile) {\r
1270 //\r
1271 // If volatile variable store, just copy valid buffer\r
1272 //\r
1273 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader.Size, 0xff);\r
1274 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r
1275 Status = EFI_SUCCESS;\r
1276 } else {\r
1277 //\r
1278 // If non-volatile variable store, perform FTW here.\r
1279 // Write ValidBuffer to destination specified by VariableBase.\r
1280 //\r
1281 Status = FtwVariableSpace (\r
1282 VariableBase,\r
1283 ValidBuffer,\r
1284 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r
1285 );\r
1286 }\r
1287 if (!EFI_ERROR (Status)) {\r
1288 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r
1289 } else {\r
1290 *LastVariableOffset = 0;\r
1291 }\r
1292\r
1293 FreePool (ValidBuffer);\r
1294\r
1295 return Status;\r
1296}\r
1297\r
1298/**\r
1299 Get index from supported language codes according to language string.\r
1300\r
1301 This code is used to get corresponding index in supported language codes. It can handle\r
1302 RFC4646 and ISO639 language tags.\r
1303 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
1304 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
1305\r
1306 For example:\r
1307 SupportedLang = "engfraengfra"\r
1308 Lang = "eng"\r
1309 Iso639Language = TRUE\r
1310 The return value is "0".\r
1311 Another example:\r
1312 SupportedLang = "en;fr;en-US;fr-FR"\r
1313 Lang = "fr-FR"\r
1314 Iso639Language = FALSE\r
1315 The return value is "3".\r
1316\r
1317 @param[in] SupportedLang Platform supported language codes.\r
1318 @param[in] Lang Configured language.\r
1319 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
1320\r
1321 @return The index of language in the language codes.\r
1322\r
1323**/\r
1324UINTN\r
1325GetIndexFromSupportedLangCodes(\r
1326 IN CHAR8 *SupportedLang,\r
1327 IN CHAR8 *Lang,\r
1328 IN BOOLEAN Iso639Language\r
1329 ) \r
1330{\r
1331 UINTN Index;\r
1332 UINTN CompareLength;\r
1333 UINTN LanguageLength;\r
1334\r
1335 if (Iso639Language) {\r
1336 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1337 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
1338 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
1339 //\r
1340 // Successfully find the index of Lang string in SupportedLang string.\r
1341 //\r
1342 Index = Index / CompareLength;\r
1343 return Index;\r
1344 }\r
1345 }\r
1346 ASSERT (FALSE);\r
1347 return 0;\r
1348 } else {\r
1349 //\r
1350 // Compare RFC4646 language code\r
1351 //\r
1352 Index = 0;\r
1353 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
1354\r
1355 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
1356 //\r
1357 // Skip ';' characters in SupportedLang\r
1358 //\r
1359 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
1360 //\r
1361 // Determine the length of the next language code in SupportedLang\r
1362 //\r
1363 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
1364 \r
1365 if ((CompareLength == LanguageLength) && \r
1366 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
1367 //\r
1368 // Successfully find the index of Lang string in SupportedLang string.\r
1369 //\r
1370 return Index;\r
1371 }\r
1372 }\r
1373 ASSERT (FALSE);\r
1374 return 0;\r
1375 }\r
1376}\r
1377\r
1378/**\r
1379 Get language string from supported language codes according to index.\r
1380\r
1381 This code is used to get corresponding language string in supported language codes. It can handle\r
1382 RFC4646 and ISO639 language tags.\r
1383 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
1384 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
1385\r
1386 For example:\r
1387 SupportedLang = "engfraengfra"\r
1388 Index = "1"\r
1389 Iso639Language = TRUE\r
1390 The return value is "fra".\r
1391 Another example:\r
1392 SupportedLang = "en;fr;en-US;fr-FR"\r
1393 Index = "1"\r
1394 Iso639Language = FALSE\r
1395 The return value is "fr".\r
1396\r
1397 @param[in] SupportedLang Platform supported language codes.\r
1398 @param[in] Index the index in supported language codes.\r
1399 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
1400 @param[in] VirtualMode Current calling mode for this function.\r
1401 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
1402\r
1403 @return The language string in the language codes.\r
1404\r
1405**/\r
1406CHAR8 *\r
1407GetLangFromSupportedLangCodes (\r
1408 IN CHAR8 *SupportedLang,\r
1409 IN UINTN Index,\r
1410 IN BOOLEAN Iso639Language,\r
1411 IN BOOLEAN VirtualMode,\r
1412 IN ESAL_VARIABLE_GLOBAL *Global\r
1413 )\r
1414{\r
1415 UINTN SubIndex;\r
1416 UINTN CompareLength;\r
1417 CHAR8 *Supported;\r
1418\r
1419 SubIndex = 0;\r
1420 Supported = SupportedLang;\r
1421 if (Iso639Language) {\r
1422 //\r
1423 // according to the index of Lang string in SupportedLang string to get the language.\r
1424 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1425 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1426 //\r
1427 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1428 Global->Lang[CompareLength] = '\0';\r
1429 return CopyMem (Global->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
1430\r
1431 } else {\r
1432 while (TRUE) {\r
1433 //\r
1434 // take semicolon as delimitation, sequentially traverse supported language codes.\r
1435 //\r
1436 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
1437 Supported++;\r
1438 }\r
1439 if ((*Supported == '\0') && (SubIndex != Index)) {\r
1440 //\r
1441 // Have completed the traverse, but not find corrsponding string.\r
1442 // This case is not allowed to happen.\r
1443 //\r
1444 ASSERT(FALSE);\r
1445 return NULL;\r
1446 }\r
1447 if (SubIndex == Index) {\r
1448 //\r
1449 // according to the index of Lang string in SupportedLang string to get the language.\r
1450 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1451 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1452 //\r
1453 Global->PlatformLang[VirtualMode][CompareLength] = '\0';\r
1454 return CopyMem (Global->PlatformLang[VirtualMode], Supported - CompareLength, CompareLength);\r
1455 }\r
1456 SubIndex++;\r
1457\r
1458 //\r
1459 // Skip ';' characters in Supported\r
1460 //\r
1461 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1462 }\r
1463 }\r
1464}\r
1465\r
1466/**\r
1467 Returns a pointer to an allocated buffer that contains the best matching language \r
1468 from a set of supported languages. \r
1469 \r
1470 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
1471 code types may not be mixed in a single call to this function. This function\r
1472 supports a variable argument list that allows the caller to pass in a prioritized\r
1473 list of language codes to test against all the language codes in SupportedLanguages.\r
1474\r
1475 If SupportedLanguages is NULL, then ASSERT().\r
1476\r
1477 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
1478 contains a set of language codes in the format \r
1479 specified by Iso639Language.\r
1480 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1481 in ISO 639-2 format. If FALSE, then all language\r
1482 codes are assumed to be in RFC 4646 language format.\r
1483 @param[in] VirtualMode Current calling mode for this function.\r
1484 @param[in] ... A variable argument list that contains pointers to \r
1485 Null-terminated ASCII strings that contain one or more\r
1486 language codes in the format specified by Iso639Language.\r
1487 The first language code from each of these language\r
1488 code lists is used to determine if it is an exact or\r
1489 close match to any of the language codes in \r
1490 SupportedLanguages. Close matches only apply to RFC 4646\r
1491 language codes, and the matching algorithm from RFC 4647\r
1492 is used to determine if a close match is present. If \r
1493 an exact or close match is found, then the matching\r
1494 language code from SupportedLanguages is returned. If\r
1495 no matches are found, then the next variable argument\r
1496 parameter is evaluated. The variable argument list \r
1497 is terminated by a NULL.\r
1498\r
1499 @retval NULL The best matching language could not be found in SupportedLanguages.\r
1500 @retval NULL There are not enough resources available to return the best matching \r
1501 language.\r
1502 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
1503 language in SupportedLanguages.\r
1504\r
1505**/\r
1506CHAR8 *\r
1507VariableGetBestLanguage (\r
1508 IN CONST CHAR8 *SupportedLanguages, \r
1509 IN BOOLEAN Iso639Language,\r
1510 IN BOOLEAN VirtualMode,\r
1511 ...\r
1512 )\r
1513{\r
1514 VA_LIST Args;\r
1515 CHAR8 *Language;\r
1516 UINTN CompareLength;\r
1517 UINTN LanguageLength;\r
1518 CONST CHAR8 *Supported;\r
1519 CHAR8 *Buffer;\r
1520\r
1521 ASSERT (SupportedLanguages != NULL);\r
1522\r
1523 VA_START (Args, VirtualMode);\r
1524 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1525 //\r
1526 // Default to ISO 639-2 mode\r
1527 //\r
1528 CompareLength = 3;\r
1529 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1530\r
1531 //\r
1532 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1533 //\r
1534 if (!Iso639Language) {\r
1535 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1536 }\r
1537\r
1538 //\r
1539 // Trim back the length of Language used until it is empty\r
1540 //\r
1541 while (LanguageLength > 0) {\r
1542 //\r
1543 // Loop through all language codes in SupportedLanguages\r
1544 //\r
1545 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1546 //\r
1547 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1548 //\r
1549 if (!Iso639Language) {\r
1550 //\r
1551 // Skip ';' characters in Supported\r
1552 //\r
1553 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1554 //\r
1555 // Determine the length of the next language code in Supported\r
1556 //\r
1557 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1558 //\r
1559 // If Language is longer than the Supported, then skip to the next language\r
1560 //\r
1561 if (LanguageLength > CompareLength) {\r
1562 continue;\r
1563 }\r
1564 }\r
1565 //\r
1566 // See if the first LanguageLength characters in Supported match Language\r
1567 //\r
1568 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1569 VA_END (Args);\r
1570\r
1571 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang[VirtualMode];\r
1572 Buffer[CompareLength] = '\0';\r
1573 return CopyMem (Buffer, Supported, CompareLength);\r
1574 }\r
1575 }\r
1576\r
1577 if (Iso639Language) {\r
1578 //\r
1579 // If ISO 639 mode, then each language can only be tested once\r
1580 //\r
1581 LanguageLength = 0;\r
1582 } else {\r
1583 //\r
1584 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1585 //\r
1586 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1587 }\r
1588 }\r
1589 }\r
1590 VA_END (Args);\r
1591\r
1592 //\r
1593 // No matches were found \r
1594 //\r
1595 return NULL;\r
1596}\r
1597\r
1598/**\r
1599 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
1600\r
1601 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
1602 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1603 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
1604\r
1605 @param[in] VariableName Name of variable.\r
1606 @param[in] Data Variable data.\r
1607 @param[in] DataSize Size of data. 0 means delete.\r
1608 @param[in] VirtualMode Current calling mode for this function.\r
1609 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
1610\r
1611**/\r
1612VOID\r
1613AutoUpdateLangVariable(\r
1614 IN CHAR16 *VariableName,\r
1615 IN VOID *Data,\r
1616 IN UINTN DataSize,\r
1617 IN BOOLEAN VirtualMode,\r
1618 IN ESAL_VARIABLE_GLOBAL *Global\r
1619 )\r
1620{\r
1621 EFI_STATUS Status;\r
1622 CHAR8 *BestPlatformLang;\r
1623 CHAR8 *BestLang;\r
1624 UINTN Index;\r
1625 UINT32 Attributes;\r
1626 VARIABLE_POINTER_TRACK Variable;\r
1627 BOOLEAN SetLanguageCodes;\r
1628 CHAR16 **PredefinedVariableName;\r
1629 VARIABLE_GLOBAL *VariableGlobal;\r
1630 UINT32 Instance;\r
1631\r
1632 //\r
1633 // Don't do updates for delete operation\r
1634 //\r
1635 if (DataSize == 0) {\r
1636 return;\r
1637 }\r
1638\r
1639 SetLanguageCodes = FALSE;\r
1640 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
1641 Instance = Global->FvbInstance;\r
1642\r
1643\r
1644 PredefinedVariableName = &Global->VariableName[VirtualMode][0];\r
1645 if (StrCmp (VariableName, PredefinedVariableName[VAR_PLATFORM_LANG_CODES]) == 0) {\r
1646 //\r
1647 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1648 //\r
1649 if (EfiAtRuntime ()) {\r
1650 return;\r
1651 }\r
1652\r
1653 SetLanguageCodes = TRUE;\r
1654\r
1655 //\r
1656 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1657 // Therefore, in variable driver, only store the original value for other use.\r
1658 //\r
1659 if (Global->PlatformLangCodes[VirtualMode] != NULL) {\r
1660 FreePool (Global->PlatformLangCodes[VirtualMode]);\r
1661 }\r
1662 Global->PlatformLangCodes[VirtualMode] = AllocateRuntimeCopyPool (DataSize, Data);\r
1663 ASSERT (mVariableModuleGlobal->PlatformLangCodes[VirtualMode] != NULL);\r
1664\r
1665 //\r
1666 // PlatformLang holds a single language from PlatformLangCodes, \r
1667 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
1668 //\r
1669 if (Global->PlatformLang[VirtualMode] != NULL) {\r
1670 FreePool (Global->PlatformLang[VirtualMode]);\r
1671 }\r
1672 Global->PlatformLang[VirtualMode] = AllocateRuntimePool (DataSize);\r
1673 ASSERT (Global->PlatformLang[VirtualMode] != NULL);\r
1674\r
1675 } else if (StrCmp (VariableName, PredefinedVariableName[VAR_LANG_CODES]) == 0) {\r
1676 //\r
1677 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
1678 //\r
1679 if (EfiAtRuntime ()) {\r
1680 return;\r
1681 }\r
1682\r
1683 SetLanguageCodes = TRUE;\r
1684\r
1685 //\r
1686 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1687 // Therefore, in variable driver, only store the original value for other use.\r
1688 //\r
1689 if (Global->LangCodes[VirtualMode] != NULL) {\r
1690 FreePool (Global->LangCodes[VirtualMode]);\r
1691 }\r
1692 Global->LangCodes[VirtualMode] = AllocateRuntimeCopyPool (DataSize, Data);\r
1693 ASSERT (Global->LangCodes[VirtualMode] != NULL);\r
1694 }\r
1695\r
1696 if (SetLanguageCodes \r
1697 && (Global->PlatformLangCodes[VirtualMode] != NULL)\r
1698 && (Global->LangCodes[VirtualMode] != NULL)) {\r
1699 //\r
1700 // Update Lang if PlatformLang is already set\r
1701 // Update PlatformLang if Lang is already set\r
1702 //\r
1703 Status = FindVariable (PredefinedVariableName[VAR_PLATFORM_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
1704 if (!EFI_ERROR (Status)) {\r
1705 //\r
1706 // Update Lang\r
1707 //\r
1708 VariableName = PredefinedVariableName[VAR_PLATFORM_LANG];\r
1709 } else {\r
1710 Status = FindVariable (PredefinedVariableName[VAR_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
1711 if (!EFI_ERROR (Status)) {\r
1712 //\r
1713 // Update PlatformLang\r
1714 //\r
1715 VariableName = PredefinedVariableName[VAR_LANG];\r
1716 } else {\r
1717 //\r
1718 // Neither PlatformLang nor Lang is set, directly return\r
1719 //\r
1720 return;\r
1721 }\r
1722 }\r
1723 Data = (VOID *) GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
1724 GetVariableDataPtr ((EFI_PHYSICAL_ADDRESS) Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, (CHAR16 *) Data);\r
1725\r
1726 Status = AccessVariableStore (\r
1727 FALSE,\r
1728 VariableGlobal,\r
1729 Variable.Volatile,\r
1730 Instance,\r
1731 (UINTN) &(((VARIABLE_HEADER *)Variable.CurrPtr)->DataSize),\r
1732 sizeof (DataSize),\r
1733 &DataSize\r
1734 ); \r
1735 ASSERT_EFI_ERROR (Status);\r
1736 }\r
1737\r
1738 //\r
1739 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1740 //\r
1741 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
1742\r
1743 if (StrCmp (VariableName, PredefinedVariableName[VAR_PLATFORM_LANG]) == 0) {\r
1744 //\r
1745 // Update Lang when PlatformLangCodes/LangCodes were set.\r
1746 //\r
1747 if ((Global->PlatformLangCodes[VirtualMode] != NULL) && (Global->LangCodes[VirtualMode] != NULL)) {\r
1748 //\r
1749 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1750 //\r
1751 BestPlatformLang = VariableGetBestLanguage (Global->PlatformLangCodes[VirtualMode], FALSE, VirtualMode, Data, NULL);\r
1752 if (BestPlatformLang != NULL) {\r
1753 //\r
1754 // Get the corresponding index in language codes.\r
1755 //\r
1756 Index = GetIndexFromSupportedLangCodes (Global->PlatformLangCodes[VirtualMode], BestPlatformLang, FALSE);\r
1757\r
1758 //\r
1759 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1760 //\r
1761 BestLang = GetLangFromSupportedLangCodes (Global->LangCodes[VirtualMode], Index, TRUE, VirtualMode, Global);\r
1762\r
1763 //\r
1764 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1765 //\r
1766 FindVariable (PredefinedVariableName[VAR_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
1767\r
1768 Status = UpdateVariable (\r
1769 PredefinedVariableName[VAR_LANG],\r
1770 Global->GlobalVariableGuid[VirtualMode],\r
1771 BestLang,\r
1772 ISO_639_2_ENTRY_SIZE + 1,\r
1773 Attributes,\r
1774 0,\r
1775 0,\r
1776 VirtualMode,\r
1777 Global,\r
1778 &Variable\r
1779 );\r
1780\r
1781 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
1782\r
1783 ASSERT_EFI_ERROR (Status);\r
1784 }\r
1785 }\r
1786\r
1787 } else if (StrCmp (VariableName, PredefinedVariableName[VAR_LANG]) == 0) {\r
1788 //\r
1789 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
1790 //\r
1791 if ((Global->PlatformLangCodes[VirtualMode] != NULL) && (Global->LangCodes[VirtualMode] != NULL)) {\r
1792 //\r
1793 // When setting Lang, firstly get most matched language string from supported language codes.\r
1794 //\r
1795 BestLang = VariableGetBestLanguage (Global->LangCodes[VirtualMode], TRUE, VirtualMode, Data, NULL);\r
1796 if (BestLang != NULL) {\r
1797 //\r
1798 // Get the corresponding index in language codes.\r
1799 //\r
1800 Index = GetIndexFromSupportedLangCodes (Global->LangCodes[VirtualMode], BestLang, TRUE);\r
1801\r
1802 //\r
1803 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1804 //\r
1805 BestPlatformLang = GetLangFromSupportedLangCodes (Global->PlatformLangCodes[VirtualMode], Index, FALSE, VirtualMode, Global);\r
1806\r
1807 //\r
1808 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1809 //\r
1810 FindVariable (PredefinedVariableName[VAR_PLATFORM_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
1811\r
1812 Status = UpdateVariable (\r
1813 PredefinedVariableName[VAR_PLATFORM_LANG], \r
1814 Global->GlobalVariableGuid[VirtualMode], \r
1815 BestPlatformLang, \r
1816 AsciiStrSize (BestPlatformLang), \r
1817 Attributes, \r
1818 0,\r
1819 0,\r
1820 VirtualMode, \r
1821 Global, \r
1822 &Variable\r
1823 );\r
1824\r
1825 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
1826 ASSERT_EFI_ERROR (Status);\r
1827 }\r
1828 }\r
1829 }\r
1830}\r
1831\r
1832/**\r
1833 Update the variable region with Variable information. These are the same \r
1834 arguments as the EFI Variable services.\r
1835\r
1836 @param[in] VariableName Name of variable.\r
1837 @param[in] VendorGuid Guid of variable.\r
1838 @param[in] Data Variable data.\r
1839 @param[in] DataSize Size of data. 0 means delete.\r
1840 @param[in] Attributes Attributes of the variable.\r
1841 @param[in] KeyIndex Index of associated public key.\r
1842 @param[in] MonotonicCount Value of associated monotonic count. \r
1843 @param[in] VirtualMode Current calling mode for this function.\r
1844 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
1845 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1846\r
1847 @retval EFI_SUCCESS The update operation is success.\r
1848 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
1849\r
1850**/\r
1851EFI_STATUS\r
1852EFIAPI\r
1853UpdateVariable (\r
1854 IN CHAR16 *VariableName,\r
1855 IN EFI_GUID *VendorGuid,\r
1856 IN VOID *Data,\r
1857 IN UINTN DataSize,\r
1858 IN UINT32 Attributes OPTIONAL, \r
1859 IN UINT32 KeyIndex OPTIONAL,\r
1860 IN UINT64 MonotonicCount OPTIONAL,\r
1861 IN BOOLEAN VirtualMode,\r
1862 IN ESAL_VARIABLE_GLOBAL *Global,\r
1863 IN VARIABLE_POINTER_TRACK *Variable\r
1864 )\r
1865{\r
1866 EFI_STATUS Status;\r
1867 VARIABLE_HEADER *NextVariable;\r
1868 UINTN VarNameOffset;\r
1869 UINTN VarDataOffset;\r
1870 UINTN VarNameSize;\r
1871 UINTN VarSize;\r
1872 BOOLEAN Volatile;\r
1873 UINT8 State;\r
1874 VARIABLE_HEADER VariableHeader;\r
1875 VARIABLE_HEADER *NextVariableHeader;\r
1876 BOOLEAN Valid;\r
1877 BOOLEAN Reclaimed;\r
1878 VARIABLE_STORE_HEADER VariableStoreHeader;\r
1879 UINTN ScratchSize;\r
1880 VARIABLE_GLOBAL *VariableGlobal;\r
1881 UINT32 Instance;\r
1882\r
1883 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
1884 Instance = Global->FvbInstance;\r
1885\r
1886 Reclaimed = FALSE;\r
1887\r
1888 if (Variable->CurrPtr != 0) {\r
1889\r
1890 Valid = IsValidVariableHeader (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, &VariableHeader);\r
1891 if (!Valid) {\r
1892 Status = EFI_NOT_FOUND;\r
1893 goto Done;\r
1894 }\r
1895\r
1896 //\r
1897 // Update/Delete existing variable\r
1898 //\r
1899 Volatile = Variable->Volatile;\r
1900 \r
1901 if (EfiAtRuntime ()) { \r
1902 //\r
1903 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
1904 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1905 // return EFI_WRITE_PROTECTED.\r
1906 //\r
1907 if (Variable->Volatile) {\r
1908 Status = EFI_WRITE_PROTECTED;\r
1909 goto Done;\r
1910 }\r
1911 //\r
1912 // Only variable have NV attribute can be updated/deleted in Runtime\r
1913 //\r
1914 if ((VariableHeader.Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1915 Status = EFI_INVALID_PARAMETER;\r
1916 goto Done; \r
1917 }\r
1918 }\r
1919 //\r
1920 // Setting a data variable with no access, or zero DataSize attributes\r
1921 // specified causes it to be deleted.\r
1922 //\r
1923 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
1924 State = VariableHeader.State;\r
1925 State &= VAR_DELETED;\r
1926\r
1927 Status = AccessVariableStore (\r
1928 TRUE,\r
1929 VariableGlobal,\r
1930 Variable->Volatile,\r
1931 Instance,\r
1932 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
1933 sizeof (UINT8),\r
1934 &State\r
1935 ); \r
1936 if (!EFI_ERROR (Status)) {\r
1937 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r
1938 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1939 }\r
1940 goto Done; \r
1941 }\r
1942 //\r
1943 // Logic comes here to update variable.\r
1944 // If the variable is marked valid and the same data has been passed in\r
1945 // then return to the caller immediately.\r
1946 //\r
1947 if (DataSizeOfVariable (&VariableHeader) == DataSize) {\r
1948 NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
1949 GetVariableDataPtr (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, (CHAR16 *) NextVariable);\r
1950 if (CompareMem (Data, (VOID *) NextVariable, DataSize) == 0) {\r
1951 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
1952 Status = EFI_SUCCESS;\r
1953 goto Done;\r
1954 }\r
1955 }\r
1956 if ((VariableHeader.State == VAR_ADDED) ||\r
1957 (VariableHeader.State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1958 //\r
1959 // If new data is different from the old one, mark the old one as VAR_IN_DELETED_TRANSITION.\r
1960 // It will be deleted if new variable is successfully written.\r
1961 //\r
1962 State = VariableHeader.State;\r
1963 State &= VAR_IN_DELETED_TRANSITION;\r
1964\r
1965 Status = AccessVariableStore (\r
1966 TRUE,\r
1967 VariableGlobal,\r
1968 Variable->Volatile,\r
1969 Instance,\r
1970 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
1971 sizeof (UINT8),\r
1972 &State\r
1973 ); \r
1974 if (EFI_ERROR (Status)) {\r
1975 goto Done; \r
1976 }\r
1977 } \r
1978 } else {\r
1979 //\r
1980 // Create a new variable\r
1981 // \r
1982 \r
1983 //\r
1984 // Make sure we are trying to create a new variable.\r
1985 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
1986 //\r
1987 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1988 Status = EFI_NOT_FOUND;\r
1989 goto Done;\r
1990 }\r
1991 \r
1992 //\r
1993 // Only variable have NV|RT attribute can be created in Runtime\r
1994 //\r
1995 if (EfiAtRuntime () &&\r
1996 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
1997 Status = EFI_INVALID_PARAMETER;\r
1998 goto Done;\r
1999 } \r
2000 }\r
2001\r
2002 //\r
2003 // Function part - create a new variable and copy the data.\r
2004 // Both update a variable and create a variable will come here.\r
2005 //\r
2006 // Tricky part: Use scratch data area at the end of volatile variable store\r
2007 // as a temporary storage.\r
2008 //\r
2009 NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
2010 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2011 NextVariableHeader = (VARIABLE_HEADER *) NextVariable;\r
2012\r
2013 SetMem (NextVariableHeader, ScratchSize, 0xff);\r
2014\r
2015 NextVariableHeader->StartId = VARIABLE_DATA;\r
2016 NextVariableHeader->Attributes = Attributes;\r
2017 NextVariableHeader->PubKeyIndex = KeyIndex;\r
2018 NextVariableHeader->MonotonicCount = MonotonicCount;\r
2019 NextVariableHeader->Reserved = 0;\r
2020 VarNameOffset = sizeof (VARIABLE_HEADER);\r
2021 VarNameSize = StrSize (VariableName);\r
2022 CopyMem (\r
2023 (UINT8 *) ((UINTN)NextVariable + VarNameOffset),\r
2024 VariableName,\r
2025 VarNameSize\r
2026 );\r
2027 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
2028 CopyMem (\r
2029 (UINT8 *) ((UINTN)NextVariable + VarDataOffset),\r
2030 Data,\r
2031 DataSize\r
2032 );\r
2033 CopyMem (&NextVariableHeader->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
2034 //\r
2035 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2036 // NextVariable->DataSize should not include pad size so that variable\r
2037 // service can get actual size in GetVariable.\r
2038 //\r
2039 NextVariableHeader->NameSize = (UINT32)VarNameSize;\r
2040 NextVariableHeader->DataSize = (UINT32)DataSize;\r
2041\r
2042 //\r
2043 // The actual size of the variable that stores in storage should\r
2044 // include pad size.\r
2045 //\r
2046 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
2047 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2048 //\r
2049 // Create a nonvolatile variable\r
2050 //\r
2051 Volatile = FALSE;\r
2052 \r
2053 GetVarStoreHeader (VariableGlobal->NonVolatileVariableBase, FALSE, VariableGlobal, Instance, &VariableStoreHeader);\r
2054 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
2055 && ((HEADER_ALIGN (VarSize) + Global->HwErrVariableTotalSize) > PcdGet32(PcdHwErrStorageSize)))\r
2056 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
2057 && ((HEADER_ALIGN (VarSize) + Global->CommonVariableTotalSize) > VariableStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize)))) {\r
2058 if (EfiAtRuntime ()) {\r
2059 Status = EFI_OUT_OF_RESOURCES;\r
2060 goto Done;\r
2061 }\r
2062 //\r
2063 // Perform garbage collection & reclaim operation\r
2064 //\r
2065 Status = Reclaim (VariableGlobal->NonVolatileVariableBase, &(Global->NonVolatileLastVariableOffset), FALSE, VirtualMode, Global, Variable->CurrPtr);\r
2066 if (EFI_ERROR (Status)) {\r
2067 goto Done;\r
2068 }\r
2069\r
2070 Reclaimed = TRUE;\r
2071 //\r
2072 // If still no enough space, return out of resources\r
2073 //\r
2074 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
2075 && ((HEADER_ALIGN (VarSize) + Global->HwErrVariableTotalSize) > PcdGet32(PcdHwErrStorageSize)))\r
2076 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
2077 && ((HEADER_ALIGN (VarSize) + Global->CommonVariableTotalSize) > VariableStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize)))) {\r
2078 Status = EFI_OUT_OF_RESOURCES;\r
2079 goto Done;\r
2080 }\r
2081 }\r
2082 //\r
2083 // Four steps\r
2084 // 1. Write variable header\r
2085 // 2. Set variable state to header valid \r
2086 // 3. Write variable data\r
2087 // 4. Set variable state to valid\r
2088 //\r
2089 //\r
2090 // Step 1:\r
2091 //\r
2092 Status = AccessVariableStore (\r
2093 TRUE,\r
2094 VariableGlobal,\r
2095 FALSE,\r
2096 Instance,\r
2097 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
2098 sizeof (VARIABLE_HEADER),\r
2099 (UINT8 *) NextVariable\r
2100 );\r
2101\r
2102 if (EFI_ERROR (Status)) {\r
2103 goto Done;\r
2104 }\r
2105\r
2106 //\r
2107 // Step 2:\r
2108 //\r
2109 NextVariableHeader->State = VAR_HEADER_VALID_ONLY;\r
2110 Status = AccessVariableStore (\r
2111 TRUE,\r
2112 VariableGlobal,\r
2113 FALSE,\r
2114 Instance,\r
2115 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
2116 sizeof (VARIABLE_HEADER),\r
2117 (UINT8 *) NextVariable\r
2118 );\r
2119\r
2120 if (EFI_ERROR (Status)) {\r
2121 goto Done;\r
2122 }\r
2123 //\r
2124 // Step 3:\r
2125 //\r
2126 Status = AccessVariableStore (\r
2127 TRUE,\r
2128 VariableGlobal,\r
2129 FALSE,\r
2130 Instance,\r
2131 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
2132 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
2133 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
2134 );\r
2135\r
2136 if (EFI_ERROR (Status)) {\r
2137 goto Done;\r
2138 }\r
2139 //\r
2140 // Step 4:\r
2141 //\r
2142 NextVariableHeader->State = VAR_ADDED;\r
2143 Status = AccessVariableStore (\r
2144 TRUE,\r
2145 VariableGlobal,\r
2146 FALSE,\r
2147 Instance,\r
2148 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
2149 sizeof (VARIABLE_HEADER),\r
2150 (UINT8 *) NextVariable\r
2151 );\r
2152\r
2153 if (EFI_ERROR (Status)) {\r
2154 goto Done;\r
2155 }\r
2156\r
2157 Global->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2158\r
2159 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2160 Global->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2161 } else {\r
2162 Global->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
2163 }\r
2164 } else {\r
2165 //\r
2166 // Create a volatile variable\r
2167 // \r
2168 Volatile = TRUE;\r
2169\r
2170 if ((UINT32) (HEADER_ALIGN(VarSize) + Global->VolatileLastVariableOffset) >\r
2171 ((VARIABLE_STORE_HEADER *) ((UINTN) (VariableGlobal->VolatileVariableBase)))->Size) {\r
2172 //\r
2173 // Perform garbage collection & reclaim operation\r
2174 //\r
2175 Status = Reclaim (VariableGlobal->VolatileVariableBase, &Global->VolatileLastVariableOffset, TRUE, VirtualMode, Global, Variable->CurrPtr);\r
2176 if (EFI_ERROR (Status)) {\r
2177 goto Done;\r
2178 }\r
2179 //\r
2180 // If still no enough space, return out of resources\r
2181 //\r
2182 if ((UINT32) (HEADER_ALIGN (VarSize) + Global->VolatileLastVariableOffset) >\r
2183 ((VARIABLE_STORE_HEADER *) ((UINTN) (VariableGlobal->VolatileVariableBase)))->Size\r
2184 ) {\r
2185 Status = EFI_OUT_OF_RESOURCES;\r
2186 goto Done;\r
2187 }\r
2188 Reclaimed = TRUE;\r
2189 }\r
2190\r
2191 NextVariableHeader->State = VAR_ADDED;\r
2192 Status = AccessVariableStore (\r
2193 TRUE,\r
2194 VariableGlobal,\r
2195 TRUE,\r
2196 Instance,\r
2197 VariableGlobal->VolatileVariableBase + Global->VolatileLastVariableOffset,\r
2198 (UINT32) VarSize,\r
2199 (UINT8 *) NextVariable\r
2200 );\r
2201\r
2202 if (EFI_ERROR (Status)) {\r
2203 goto Done;\r
2204 }\r
2205\r
2206 Global->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2207 }\r
2208 //\r
2209 // Mark the old variable as deleted\r
2210 // If storage has just been reclaimed, the old variable marked as VAR_IN_DELETED_TRANSITION\r
2211 // has already been eliminated, so no need to delete it.\r
2212 //\r
2213 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != 0) {\r
2214 State = ((VARIABLE_HEADER *)Variable->CurrPtr)->State;\r
2215 State &= VAR_DELETED;\r
2216\r
2217 Status = AccessVariableStore (\r
2218 TRUE,\r
2219 VariableGlobal,\r
2220 Variable->Volatile,\r
2221 Instance,\r
2222 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
2223 sizeof (UINT8),\r
2224 &State\r
2225 );\r
2226 }\r
2227\r
2228 if (!EFI_ERROR (Status)) {\r
2229 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
2230 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
2231 }\r
2232\r
2233Done:\r
2234 return Status;\r
2235}\r
2236\r
2237/**\r
2238 Implements EsalGetVariable function of Extended SAL Variable Services Class.\r
2239\r
2240 This function implements EsalGetVariable function of Extended SAL Variable Services Class.\r
2241 It is equivalent in functionality to the EFI Runtime Service GetVariable().\r
2242 \r
2243 @param[in] VariableName A Null-terminated Unicode string that is the name of\r
2244 the vendor's variable.\r
2245 @param[in] VendorGuid A unique identifier for the vendor.\r
2246 @param[out] Attributes If not NULL, a pointer to the memory location to return the \r
2247 attributes bitmask for the variable.\r
2248 @param[in, out] DataSize Size of Data found. If size is less than the\r
2249 data, this value contains the required size.\r
2250 @param[out] Data On input, the size in bytes of the return Data buffer. \r
2251 On output, the size of data returned in Data.\r
2252 @param[in] VirtualMode Current calling mode for this function.\r
2253 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
2254\r
2255 @retval EFI_SUCCESS The function completed successfully. \r
2256 @retval EFI_NOT_FOUND The variable was not found.\r
2257 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has \r
2258 been updated with the size needed to complete the request.\r
2259 @retval EFI_INVALID_PARAMETER VariableName is NULL.\r
2260 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r
2261 @retval EFI_INVALID_PARAMETER DataSize is NULL.\r
2262 @retval EFI_INVALID_PARAMETER DataSize is not too small and Data is NULL.\r
2263 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
2264 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.\r
2265\r
2266**/\r
2267EFI_STATUS\r
2268EFIAPI\r
2269EsalGetVariable (\r
2270 IN CHAR16 *VariableName,\r
2271 IN EFI_GUID *VendorGuid,\r
2272 OUT UINT32 *Attributes OPTIONAL,\r
2273 IN OUT UINTN *DataSize,\r
2274 OUT VOID *Data,\r
2275 IN BOOLEAN VirtualMode,\r
2276 IN ESAL_VARIABLE_GLOBAL *Global\r
2277 )\r
2278{\r
2279 VARIABLE_POINTER_TRACK Variable;\r
2280 UINTN VarDataSize;\r
2281 EFI_STATUS Status;\r
2282 VARIABLE_HEADER VariableHeader;\r
2283 BOOLEAN Valid;\r
2284 VARIABLE_GLOBAL *VariableGlobal;\r
2285 UINT32 Instance;\r
2286\r
2287 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2288 return EFI_INVALID_PARAMETER;\r
2289 }\r
2290\r
2291 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
2292 Instance = Global->FvbInstance;\r
2293\r
2294 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
2295\r
2296 //\r
2297 // Check if this variable exists in cache.\r
2298 //\r
2299 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
2300 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r
2301 //\r
2302 // If variable exists in cache, just update statistical information for it and finish.\r
2303 // Here UpdateVariableInfo() has already retrieved data & attributes for output.\r
2304 //\r
2305 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r
2306 goto Done;\r
2307 }\r
2308 //\r
2309 // If variable does not exist in cache, search for it in variable storage area.\r
2310 //\r
2311 Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);\r
2312 if (Variable.CurrPtr == 0x0 || EFI_ERROR (Status)) {\r
2313 //\r
2314 // If it cannot be found in variable storage area, goto Done.\r
2315 //\r
2316 goto Done;\r
2317 }\r
2318\r
2319 Valid = IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, &VariableHeader);\r
2320 if (!Valid) {\r
2321 Status = EFI_NOT_FOUND;\r
2322 goto Done;\r
2323 }\r
2324 //\r
2325 // If variable exists, but not in cache, get its data and attributes, update\r
2326 // statistical information, and update cache.\r
2327 //\r
2328 VarDataSize = DataSizeOfVariable (&VariableHeader);\r
2329 ASSERT (VarDataSize != 0);\r
2330\r
2331 if (*DataSize >= VarDataSize) {\r
2332 if (Data == NULL) {\r
2333 Status = EFI_INVALID_PARAMETER;\r
2334 goto Done;\r
2335 }\r
2336\r
2337 GetVariableDataPtr (\r
2338 Variable.CurrPtr,\r
2339 Variable.Volatile,\r
2340 VariableGlobal,\r
2341 Instance,\r
2342 Data\r
2343 );\r
2344 if (Attributes != NULL) {\r
2345 *Attributes = VariableHeader.Attributes;\r
2346 }\r
2347\r
2348 *DataSize = VarDataSize;\r
2349 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2350 UpdateVariableCache (VariableName, VendorGuid, VariableHeader.Attributes, VarDataSize, Data);\r
2351 \r
2352 Status = EFI_SUCCESS;\r
2353 goto Done;\r
2354 } else {\r
2355 //\r
2356 // If DataSize is too small for the result, return EFI_BUFFER_TOO_SMALL.\r
2357 //\r
2358 *DataSize = VarDataSize;\r
2359 Status = EFI_BUFFER_TOO_SMALL;\r
2360 goto Done;\r
2361 }\r
2362\r
2363Done:\r
2364 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
2365 return Status;\r
2366}\r
2367\r
2368/**\r
2369 Implements EsalGetNextVariableName function of Extended SAL Variable Services Class.\r
2370\r
2371 This function implements EsalGetNextVariableName function of Extended SAL Variable Services Class.\r
2372 It is equivalent in functionality to the EFI Runtime Service GetNextVariableName().\r
2373 \r
2374 @param[in, out] VariableNameSize Size of the variable\r
2375 @param[in, out] VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().\r
2376 On output, returns the Null-terminated Unicode string of the current variable.\r
2377 @param[in, out] VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
2378 On output, returns the VendorGuid of the current variable. \r
2379 @param[in] VirtualMode Current calling mode for this function.\r
2380 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
2381\r
2382 @retval EFI_SUCCESS The function completed successfully. \r
2383 @retval EFI_NOT_FOUND The next variable was not found.\r
2384 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result. \r
2385 VariableNameSize has been updated with the size needed to complete the request.\r
2386 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.\r
2387 @retval EFI_INVALID_PARAMETER VariableName is NULL.\r
2388 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r
2389 @retval EFI_DEVICE_ERROR The variable name could not be retrieved due to a hardware error.\r
2390\r
2391**/\r
2392EFI_STATUS\r
2393EFIAPI\r
2394EsalGetNextVariableName (\r
2395 IN OUT UINTN *VariableNameSize,\r
2396 IN OUT CHAR16 *VariableName,\r
2397 IN OUT EFI_GUID *VendorGuid,\r
2398 IN BOOLEAN VirtualMode,\r
2399 IN ESAL_VARIABLE_GLOBAL *Global\r
2400 )\r
2401{\r
2402 VARIABLE_POINTER_TRACK Variable;\r
2403 UINTN VarNameSize;\r
2404 EFI_STATUS Status;\r
2405 VARIABLE_HEADER VariableHeader;\r
2406 VARIABLE_GLOBAL *VariableGlobal;\r
2407 UINT32 Instance;\r
2408\r
2409 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2410 return EFI_INVALID_PARAMETER;\r
2411 }\r
2412\r
2413 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
2414 Instance = Global->FvbInstance;\r
2415\r
2416 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
2417\r
2418 Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);\r
2419 //\r
2420 // If the variable does not exist, goto Done and return.\r
2421 //\r
2422 if (Variable.CurrPtr == 0x0 || EFI_ERROR (Status)) {\r
2423 goto Done;\r
2424 }\r
2425\r
2426 if (VariableName[0] != 0) {\r
2427 //\r
2428 // If variable name is not NULL, get next variable\r
2429 //\r
2430 Variable.CurrPtr = GetNextVariablePtr (\r
2431 Variable.CurrPtr,\r
2432 Variable.Volatile,\r
2433 VariableGlobal,\r
2434 Instance\r
2435 );\r
2436 }\r
2437\r
2438 while (TRUE) {\r
2439 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == 0x0) {\r
2440 //\r
2441 // If fail to find a variable in current area, reverse the volatile attribute of area to search.\r
2442 //\r
2443 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
2444 //\r
2445 // Here we depend on the searching sequence of FindVariable().\r
2446 // It first searches volatile area, then NV area.\r
2447 // So if the volatile attribute after switching is non-volatile, it means that we have finished searching volatile area,\r
2448 // and EFI_NOT_FOUND is returnd.\r
2449 // Otherwise, it means that we have finished searchig non-volatile area, and we will continue to search volatile area.\r
2450 //\r
2451 if (!Variable.Volatile) {\r
2452 Variable.StartPtr = GetStartPointer (VariableGlobal->NonVolatileVariableBase);\r
2453 Variable.EndPtr = GetEndPointer (VariableGlobal->NonVolatileVariableBase, FALSE, VariableGlobal, Instance);\r
2454 } else {\r
2455 Status = EFI_NOT_FOUND;\r
2456 goto Done;\r
2457 }\r
2458\r
2459 Variable.CurrPtr = Variable.StartPtr;\r
2460 if (!IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, NULL)) {\r
2461 continue;\r
2462 }\r
2463 }\r
2464 //\r
2465 // Variable is found\r
2466 //\r
2467 if (IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, &VariableHeader)) {\r
2468 if ((VariableHeader.State == VAR_ADDED) &&\r
2469 (!(EfiAtRuntime () && ((VariableHeader.Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)))) {\r
2470 VarNameSize = NameSizeOfVariable (&VariableHeader);\r
2471 ASSERT (VarNameSize != 0);\r
2472\r
2473 if (VarNameSize <= *VariableNameSize) {\r
2474 GetVariableNamePtr (\r
2475 Variable.CurrPtr,\r
2476 Variable.Volatile,\r
2477 VariableGlobal,\r
2478 Instance,\r
2479 VariableName\r
2480 );\r
2481 CopyMem (\r
2482 VendorGuid,\r
2483 &VariableHeader.VendorGuid,\r
2484 sizeof (EFI_GUID)\r
2485 );\r
2486 Status = EFI_SUCCESS;\r
2487 } else {\r
2488 Status = EFI_BUFFER_TOO_SMALL;\r
2489 }\r
2490\r
2491 *VariableNameSize = VarNameSize;\r
2492 goto Done;\r
2493 }\r
2494 }\r
2495\r
2496 Variable.CurrPtr = GetNextVariablePtr (\r
2497 Variable.CurrPtr,\r
2498 Variable.Volatile,\r
2499 VariableGlobal,\r
2500 Instance\r
2501 );\r
2502 }\r
2503\r
2504Done:\r
2505 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
2506 return Status;\r
2507}\r
2508\r
2509/**\r
2510 Implements EsalSetVariable function of Extended SAL Variable Services Class.\r
2511\r
2512 This function implements EsalSetVariable function of Extended SAL Variable Services Class.\r
2513 It is equivalent in functionality to the EFI Runtime Service SetVariable().\r
2514 \r
2515 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
2516 variable. Each VariableName is unique for each \r
2517 VendorGuid. VariableName must contain 1 or more \r
2518 Unicode characters. If VariableName is an empty Unicode \r
2519 string, then EFI_INVALID_PARAMETER is returned.\r
2520 @param[in] VendorGuid A unique identifier for the vendor.\r
2521 @param[in] Attributes Attributes bitmask to set for the variable.\r
2522 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the\r
2523 variable to be deleted.\r
2524 @param[in] Data The contents for the variable.\r
2525 @param[in] VirtualMode Current calling mode for this function.\r
2526 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
2527\r
2528 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as \r
2529 defined by the Attributes.\r
2530 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the \r
2531 DataSize exceeds the maximum allowed.\r
2532 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.\r
2533 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
2534 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
2535 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
2536 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
2537 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.\r
2538 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
2539\r
2540**/\r
2541EFI_STATUS\r
2542EFIAPI\r
2543EsalSetVariable (\r
2544 IN CHAR16 *VariableName,\r
2545 IN EFI_GUID *VendorGuid,\r
2546 IN UINT32 Attributes,\r
2547 IN UINTN DataSize,\r
2548 IN VOID *Data,\r
2549 IN BOOLEAN VirtualMode,\r
2550 IN ESAL_VARIABLE_GLOBAL *Global\r
2551 )\r
2552{\r
2553 VARIABLE_POINTER_TRACK Variable;\r
2554 EFI_STATUS Status;\r
2555 EFI_PHYSICAL_ADDRESS NextVariable;\r
2556 EFI_PHYSICAL_ADDRESS Point;\r
2557 VARIABLE_GLOBAL *VariableGlobal;\r
2558 UINT32 Instance;\r
2559 UINT32 KeyIndex;\r
2560 UINT64 MonotonicCount;\r
2561 UINTN PayloadSize;\r
2562\r
2563 //\r
2564 // Check input parameters\r
2565 //\r
2566 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2567 return EFI_INVALID_PARAMETER;\r
2568 } \r
2569\r
2570 if (DataSize != 0 && Data == NULL) {\r
2571 return EFI_INVALID_PARAMETER;\r
2572 }\r
2573\r
2574 //\r
2575 // EFI_VARIABLE_RUNTIME_ACCESS bit cannot be set without EFI_VARIABLE_BOOTSERVICE_ACCESS bit.\r
2576 //\r
2577 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2578 return EFI_INVALID_PARAMETER;\r
2579 }\r
2580\r
2581 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
2582 if (DataSize < AUTHINFO_SIZE) {\r
2583 //\r
2584 // Try to write Authencated Variable without AuthInfo\r
2585 //\r
2586 return EFI_SECURITY_VIOLATION;\r
2587 } \r
2588 PayloadSize = DataSize - AUTHINFO_SIZE; \r
2589 } else {\r
2590 PayloadSize = DataSize; \r
2591 }\r
2592\r
2593 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
2594 Instance = Global->FvbInstance;\r
2595\r
2596 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2597 //\r
2598 // For variable for hardware error record, the size of the VariableName, including the Unicode Null\r
2599 // in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxHardwareErrorVariableSize) bytes.\r
2600 //\r
2601 if ((PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) || \r
2602 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
2603 return EFI_INVALID_PARAMETER;\r
2604 }\r
2605 //\r
2606 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
2607 //\r
2608 if (StrnCmp (VariableName, \\r
2609 Global->VariableName[VirtualMode][VAR_HW_ERR_REC], \\r
2610 StrLen(Global->VariableName[VirtualMode][VAR_HW_ERR_REC])) != 0) {\r
2611 return EFI_INVALID_PARAMETER;\r
2612 }\r
2613 } else {\r
2614 //\r
2615 // For variable not for hardware error record, the size of the VariableName, including the\r
2616 // Unicode Null in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxVariableSize) bytes.\r
2617 //\r
2618 if ((PayloadSize > PcdGet32(PcdMaxVariableSize)) ||\r
2619 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxVariableSize))) {\r
2620 return EFI_INVALID_PARAMETER;\r
2621 } \r
2622 } \r
2623\r
2624 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
2625\r
2626 //\r
2627 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;\r
2628 //\r
2629 if (InterlockedIncrement (&Global->ReentrantState) > 1) {\r
2630 Point = VariableGlobal->NonVolatileVariableBase;;\r
2631 //\r
2632 // Parse non-volatile variable data and get last variable offset\r
2633 //\r
2634 NextVariable = GetStartPointer (Point);\r
2635 while (IsValidVariableHeader (NextVariable, FALSE, VariableGlobal, Instance, NULL)) {\r
2636 NextVariable = GetNextVariablePtr (NextVariable, FALSE, VariableGlobal, Instance);\r
2637 }\r
2638 Global->NonVolatileLastVariableOffset = NextVariable - Point;\r
2639 }\r
2640\r
2641 //\r
2642 // Check whether the input variable exists\r
2643 //\r
2644\r
2645 Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);\r
2646\r
2647 //\r
2648 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
2649 //\r
2650 AutoUpdateLangVariable (VariableName, Data, PayloadSize, VirtualMode, Global);\r
2651\r
2652 //\r
2653 // Process PK, KEK, Sigdb seperately\r
2654 //\r
2655 if (CompareGuid (VendorGuid, Global->GlobalVariableGuid[VirtualMode]) && (StrCmp (VariableName, Global->VariableName[VirtualMode][VAR_PLATFORM_KEY]) == 0)) {\r
2656 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes, TRUE);\r
2657 } else if (CompareGuid (VendorGuid, Global->GlobalVariableGuid[VirtualMode]) && (StrCmp (VariableName, Global->VariableName[VirtualMode][VAR_KEY_EXCHANGE_KEY]) == 0)) {\r
2658 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes, FALSE);\r
2659 } else if (CompareGuid (VendorGuid, Global->ImageSecurityDatabaseGuid[VirtualMode])) {\r
2660 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes);\r
2661 } else {\r
2662 Status = VerifyVariable (Data, DataSize, VirtualMode, Global, &Variable, Attributes, &KeyIndex, &MonotonicCount);\r
2663 if (!EFI_ERROR(Status)) {\r
2664 //\r
2665 // Verification pass\r
2666 //\r
2667 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2668 //\r
2669 // Cut the certificate size before set\r
2670 //\r
2671 Status = UpdateVariable (\r
2672 VariableName, \r
2673 VendorGuid, \r
2674 (UINT8*)Data + AUTHINFO_SIZE, \r
2675 DataSize - AUTHINFO_SIZE, \r
2676 Attributes, \r
2677 KeyIndex, \r
2678 MonotonicCount, \r
2679 VirtualMode, \r
2680 Global, \r
2681 &Variable\r
2682 );\r
2683 } else {\r
2684 //\r
2685 // Update variable as usual \r
2686 //\r
2687 Status = UpdateVariable (\r
2688 VariableName, \r
2689 VendorGuid, \r
2690 Data, \r
2691 DataSize, \r
2692 Attributes, \r
2693 0, \r
2694 0, \r
2695 VirtualMode, \r
2696 Global, \r
2697 &Variable\r
2698 );\r
2699 }\r
2700 }\r
2701 }\r
2702\r
2703 InterlockedDecrement (&Global->ReentrantState);\r
2704 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
2705 return Status;\r
2706}\r
2707\r
2708/**\r
2709 Implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.\r
2710\r
2711 This function implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.\r
2712 It is equivalent in functionality to the EFI Runtime Service QueryVariableInfo().\r
2713\r
2714 @param[in] Attributes Attributes bitmask to specify the type of variables\r
2715 on which to return information.\r
2716 @param[out] MaximumVariableStorageSize On output the maximum size of the storage space available for \r
2717 the EFI variables associated with the attributes specified. \r
2718 @param[out] RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI \r
2719 variables associated with the attributes specified.\r
2720 @param[out] MaximumVariableSize Returns the maximum size of an individual EFI variable \r
2721 associated with the attributes specified.\r
2722 @param[in] VirtualMode Current calling mode for this function\r
2723 @param[in] Global Context of this Extended SAL Variable Services Class call\r
2724\r
2725 @retval EFI_SUCCESS Valid answer returned.\r
2726 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2727 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the \r
2728 MaximumVariableStorageSize, RemainingVariableStorageSize, \r
2729 MaximumVariableSize are undefined.\r
2730**/\r
2731EFI_STATUS\r
2732EFIAPI\r
2733EsalQueryVariableInfo (\r
2734 IN UINT32 Attributes,\r
2735 OUT UINT64 *MaximumVariableStorageSize,\r
2736 OUT UINT64 *RemainingVariableStorageSize,\r
2737 OUT UINT64 *MaximumVariableSize,\r
2738 IN BOOLEAN VirtualMode,\r
2739 IN ESAL_VARIABLE_GLOBAL *Global\r
2740 )\r
2741{\r
2742 EFI_PHYSICAL_ADDRESS Variable;\r
2743 EFI_PHYSICAL_ADDRESS NextVariable;\r
2744 UINT64 VariableSize;\r
2745 EFI_PHYSICAL_ADDRESS VariableStoreHeaderAddress;\r
2746 BOOLEAN Volatile;\r
2747 VARIABLE_STORE_HEADER VarStoreHeader;\r
2748 VARIABLE_HEADER VariableHeader;\r
2749 UINT64 CommonVariableTotalSize;\r
2750 UINT64 HwErrVariableTotalSize;\r
2751 VARIABLE_GLOBAL *VariableGlobal;\r
2752 UINT32 Instance;\r
2753\r
2754 CommonVariableTotalSize = 0;\r
2755 HwErrVariableTotalSize = 0;\r
2756\r
2757 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
2758 return EFI_INVALID_PARAMETER;\r
2759 }\r
2760 \r
2761 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
2762 //\r
2763 // Make sure the Attributes combination is supported by the platform.\r
2764 //\r
2765 return EFI_UNSUPPORTED; \r
2766 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2767 //\r
2768 // Make sure if runtime bit is set, boot service bit is set also.\r
2769 //\r
2770 return EFI_INVALID_PARAMETER;\r
2771 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
2772 //\r
2773 // Make sure RT Attribute is set if we are in Runtime phase.\r
2774 //\r
2775 return EFI_INVALID_PARAMETER;\r
2776 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2777 //\r
2778 // Make sure Hw Attribute is set with NV.\r
2779 //\r
2780 return EFI_INVALID_PARAMETER;\r
2781 }\r
2782\r
2783 VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
2784 Instance = Global->FvbInstance;\r
2785\r
2786 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
2787\r
2788 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2789 //\r
2790 // Query is Volatile related.\r
2791 //\r
2792 Volatile = TRUE;\r
2793 VariableStoreHeaderAddress = VariableGlobal->VolatileVariableBase;\r
2794 } else {\r
2795 //\r
2796 // Query is Non-Volatile related.\r
2797 //\r
2798 Volatile = FALSE;\r
2799 VariableStoreHeaderAddress = VariableGlobal->NonVolatileVariableBase;\r
2800 }\r
2801\r
2802 //\r
2803 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2804 // with the storage size (excluding the storage header size).\r
2805 //\r
2806 GetVarStoreHeader (VariableStoreHeaderAddress, Volatile, VariableGlobal, Instance, &VarStoreHeader);\r
2807\r
2808 *MaximumVariableStorageSize = VarStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER);\r
2809\r
2810 // Harware error record variable needs larger size.\r
2811 //\r
2812 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2813 *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r
2814 *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2815 } else {\r
2816 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2817 ASSERT (PcdGet32(PcdHwErrStorageSize) < VarStoreHeader.Size);\r
2818 *MaximumVariableStorageSize = VarStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
2819 }\r
2820\r
2821 //\r
2822 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
2823 //\r
2824 *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
2825 }\r
2826\r
2827 //\r
2828 // Point to the starting address of the variables.\r
2829 //\r
2830 Variable = GetStartPointer (VariableStoreHeaderAddress);\r
2831\r
2832 //\r
2833 // Now walk through the related variable store.\r
2834 //\r
2835 while (IsValidVariableHeader (Variable, Volatile, VariableGlobal, Instance, &VariableHeader) &&\r
2836 (Variable < GetEndPointer (VariableStoreHeaderAddress, Volatile, VariableGlobal, Instance))) {\r
2837 NextVariable = GetNextVariablePtr (Variable, Volatile, VariableGlobal, Instance);\r
2838 VariableSize = NextVariable - Variable;\r
2839\r
2840 if (EfiAtRuntime ()) {\r
2841 //\r
2842 // we don't take the state of the variables in mind\r
2843 // when calculating RemainingVariableStorageSize,\r
2844 // since the space occupied by variables not marked with\r
2845 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2846 //\r
2847 if ((VariableHeader.Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2848 HwErrVariableTotalSize += VariableSize;\r
2849 } else {\r
2850 CommonVariableTotalSize += VariableSize;\r
2851 }\r
2852 } else {\r
2853 //\r
2854 // Only care about Variables with State VAR_ADDED,because\r
2855 // the space not marked as VAR_ADDED is reclaimable now.\r
2856 //\r
2857 if (VariableHeader.State == VAR_ADDED) {\r
2858 if ((VariableHeader.Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2859 HwErrVariableTotalSize += VariableSize;\r
2860 } else {\r
2861 CommonVariableTotalSize += VariableSize;\r
2862 }\r
2863 }\r
2864 }\r
2865\r
2866 //\r
2867 // Go to the next one\r
2868 //\r
2869 Variable = NextVariable;\r
2870 }\r
2871\r
2872 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2873 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2874 }else {\r
2875 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2876 }\r
2877\r
2878 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2879 *MaximumVariableSize = 0;\r
2880 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2881 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2882 }\r
2883\r
2884 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
2885 return EFI_SUCCESS;\r
2886}\r
2887\r
2888/**\r
2889 Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
2890\r
2891 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
2892 When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
2893 storage if free size is below the threshold.\r
2894\r
2895 @param[in] Event Event whose notification function is being invoked.\r
2896 @param[in] Context Pointer to the notification function's context.\r
2897\r
2898**/\r
2899VOID\r
2900EFIAPI\r
2901ReclaimForOS(\r
2902 IN EFI_EVENT Event,\r
2903 IN VOID *Context\r
2904 )\r
2905{\r
2906 UINT32 VarSize;\r
2907 EFI_STATUS Status;\r
2908 UINTN CommonVariableSpace;\r
2909 UINTN RemainingCommonVariableSpace;\r
2910 UINTN RemainingHwErrVariableSpace;\r
2911\r
2912 VarSize = ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase))->Size;\r
2913 Status = EFI_SUCCESS; \r
2914 //\r
2915 //Allowable max size of common variable storage space\r
2916 //\r
2917 CommonVariableSpace = VarSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
2918\r
2919 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2920 \r
2921 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
2922 //\r
2923 // If the free area is below a threshold, then performs reclaim operation.\r
2924 //\r
2925 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
2926 || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
2927 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
2928 Status = Reclaim (\r
2929 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
2930 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2931 FALSE,\r
2932 Physical,\r
2933 mVariableModuleGlobal,\r
2934 0x0\r
2935 );\r
2936 ASSERT_EFI_ERROR (Status);\r
2937 }\r
2938}\r
2939\r
2940/**\r
2941 Initializes variable store area for non-volatile and volatile variable.\r
2942\r
2943 This function allocates and initializes memory space for global context of ESAL\r
2944 variable service and variable store area for non-volatile and volatile variable.\r
2945\r
2946 @param[in] ImageHandle The Image handle of this driver.\r
2947 @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.\r
2948\r
2949 @retval EFI_SUCCESS Function successfully executed.\r
2950 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2951\r
2952**/\r
2953EFI_STATUS\r
2954VariableCommonInitialize (\r
2955 IN EFI_HANDLE ImageHandle,\r
2956 IN EFI_SYSTEM_TABLE *SystemTable\r
2957 )\r
2958{\r
2959 EFI_STATUS Status;\r
2960 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
2961 EFI_PHYSICAL_ADDRESS CurrPtr;\r
2962 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2963 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2964 EFI_PHYSICAL_ADDRESS Variable;\r
2965 EFI_PHYSICAL_ADDRESS NextVariable;\r
2966 UINTN VariableSize;\r
2967 UINT32 Instance;\r
2968 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
2969 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
2970 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
2971 UINT64 BaseAddress;\r
2972 UINT64 Length;\r
2973 UINTN Index;\r
2974 UINT8 Data;\r
2975 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
2976 UINT64 VariableStoreLength;\r
2977 EFI_EVENT ReadyToBootEvent;\r
2978 UINTN ScratchSize;\r
2979\r
2980 //\r
2981 // Allocate memory for mVariableModuleGlobal\r
2982 //\r
2983 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (ESAL_VARIABLE_GLOBAL));\r
2984 if (mVariableModuleGlobal == NULL) {\r
2985 return EFI_OUT_OF_RESOURCES;\r
2986 }\r
2987\r
2988 mVariableModuleGlobal->GlobalVariableGuid[Physical] = &gEfiGlobalVariableGuid;\r
2989 CopyMem (\r
2990 mVariableModuleGlobal->VariableName[Physical],\r
2991 mVariableName,\r
2992 sizeof (mVariableName)\r
2993 );\r
2994\r
2995 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);\r
2996\r
2997 //\r
2998 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2999 // is stored with common variable in the same NV region. So the platform integrator should\r
3000 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
3001 // PcdFlashNvStorageVariableSize.\r
3002 //\r
3003 ASSERT (PcdGet32(PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
3004\r
3005 //\r
3006 // Allocate memory for volatile variable store\r
3007 //\r
3008 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3009 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
3010 if (VolatileVariableStore == NULL) {\r
3011 FreePool (mVariableModuleGlobal);\r
3012 return EFI_OUT_OF_RESOURCES;\r
3013 }\r
3014\r
3015 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
3016\r
3017 //\r
3018 // Variable Specific Data\r
3019 //\r
3020 mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
3021 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer ((EFI_PHYSICAL_ADDRESS) VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
3022\r
3023 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
3024 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
3025 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
3026 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
3027 VolatileVariableStore->Reserved = 0;\r
3028 VolatileVariableStore->Reserved1 = 0;\r
3029\r
3030 //\r
3031 // Get non volatile varaible store\r
3032 //\r
3033 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3034 VariableStoreBase = TempVariableStoreHeader + \\r
3035 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
3036 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
3037 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
3038 //\r
3039 // Mark the variable storage region of the FLASH as RUNTIME\r
3040 //\r
3041 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
3042 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
3043 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
3044\r
3045 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
3046 if (EFI_ERROR (Status)) {\r
3047 goto Done;\r
3048 }\r
3049\r
3050 Status = gDS->SetMemorySpaceAttributes (\r
3051 BaseAddress,\r
3052 Length,\r
3053 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
3054 );\r
3055 if (EFI_ERROR (Status)) {\r
3056 goto Done;\r
3057 }\r
3058 //\r
3059 // Get address of non volatile variable store base.\r
3060 //\r
3061 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = VariableStoreBase;\r
3062\r
3063 //\r
3064 // Check Integrity\r
3065 //\r
3066 //\r
3067 // Find the Correct Instance of the FV Block Service.\r
3068 //\r
3069 Instance = 0;\r
3070 CurrPtr = mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase;\r
3071\r
3072 do {\r
3073 FvVolHdr = 0;\r
3074 Status = (EFI_STATUS) EsalCall (\r
3075 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
3076 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
3077 GetPhysicalAddressFunctionId, \r
3078 Instance, \r
3079 (UINT64) &FvVolHdr, \r
3080 0, \r
3081 0, \r
3082 0, \r
3083 0, \r
3084 0\r
3085 ).Status;\r
3086 if (EFI_ERROR (Status)) {\r
3087 break;\r
3088 }\r
3089 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
3090 ASSERT (FwVolHeader != NULL);\r
3091 if (CurrPtr >= (EFI_PHYSICAL_ADDRESS) FwVolHeader &&\r
3092 CurrPtr < ((EFI_PHYSICAL_ADDRESS) FwVolHeader + FwVolHeader->FvLength)) {\r
3093 mVariableModuleGlobal->FvbInstance = Instance;\r
3094 break;\r
3095 }\r
3096\r
3097 Instance++;\r
3098 } while (Status == EFI_SUCCESS);\r
3099\r
3100 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r
3101 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
3102 if (~VariableStoreHeader->Size == 0) {\r
3103 Status = AccessVariableStore (\r
3104 TRUE,\r
3105 &mVariableModuleGlobal->VariableGlobal[Physical],\r
3106 FALSE,\r
3107 mVariableModuleGlobal->FvbInstance,\r
3108 (UINTN) &VariableStoreHeader->Size,\r
3109 sizeof (UINT32),\r
3110 (UINT8 *) &VariableStoreLength\r
3111 );\r
3112 //\r
3113 // As Variables are stored in NV storage, which are slow devices,such as flash.\r
3114 // Variable operation may skip checking variable program result to improve performance,\r
3115 // We can assume Variable program is OK through some check point.\r
3116 // Variable Store Size Setting should be the first Variable write operation,\r
3117 // We can assume all Read/Write is OK if we can set Variable store size successfully.\r
3118 // If write fail, we will assert here.\r
3119 //\r
3120 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
3121\r
3122 if (EFI_ERROR (Status)) {\r
3123 goto Done;\r
3124 }\r
3125 }\r
3126\r
3127 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r
3128 //\r
3129 // Parse non-volatile variable data and get last variable offset.\r
3130 //\r
3131 Variable = GetStartPointer (CurrPtr);\r
3132 Status = EFI_SUCCESS;\r
3133\r
3134 while (IsValidVariableHeader (Variable, FALSE, &(mVariableModuleGlobal->VariableGlobal[Physical]), Instance, NULL)) {\r
3135 NextVariable = GetNextVariablePtr (\r
3136 Variable,\r
3137 FALSE,\r
3138 &(mVariableModuleGlobal->VariableGlobal[Physical]),\r
3139 Instance\r
3140 );\r
3141 VariableSize = NextVariable - Variable;\r
3142 if ((((VARIABLE_HEADER *)Variable)->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3143 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
3144 } else {\r
3145 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
3146 }\r
3147\r
3148 Variable = NextVariable;\r
3149 }\r
3150\r
3151 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) CurrPtr;\r
3152\r
3153 //\r
3154 // Check if the free area is really free.\r
3155 //\r
3156 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
3157 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase)[Index];\r
3158 if (Data != 0xff) {\r
3159 //\r
3160 // There must be something wrong in variable store, do reclaim operation.\r
3161 //\r
3162 Status = Reclaim (\r
3163 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
3164 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3165 FALSE,\r
3166 Physical,\r
3167 mVariableModuleGlobal,\r
3168 0x0\r
3169 );\r
3170 if (EFI_ERROR (Status)) {\r
3171 goto Done;\r
3172 }\r
3173 break;\r
3174 }\r
3175 }\r
3176\r
3177 //\r
3178 // Register the event handling function to reclaim variable for OS usage.\r
3179 //\r
3180 Status = EfiCreateEventReadyToBootEx (\r
3181 TPL_NOTIFY, \r
3182 ReclaimForOS, \r
3183 NULL, \r
3184 &ReadyToBootEvent\r
3185 );\r
3186 } else {\r
3187 Status = EFI_VOLUME_CORRUPTED;\r
3188 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
3189 }\r
3190\r
3191Done:\r
3192 if (EFI_ERROR (Status)) {\r
3193 FreePool (mVariableModuleGlobal);\r
3194 FreePool (VolatileVariableStore);\r
3195 }\r
3196\r
3197 return Status;\r
3198}\r