]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
Fix a potential SMM memory dump issue. If pass communication buffer with DataBuffer...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmm.c
CommitLineData
8a2d4996 1/** @file\r
2\r
3 The sample implementation for SMM variable protocol. And this driver \r
4 implements an SMI handler to communicate with the DXE runtime driver \r
5 to provide variable services.\r
6\r
2445a70e 7 Caution: This module requires additional review when modified.\r
8 This driver will have external input - variable data and communicate buffer in SMM mode.\r
9 This external input must be validated carefully to avoid security issue like\r
10 buffer overflow, integer overflow.\r
11\r
12 SmmVariableHandler() will receive untrusted input and do basic validation.\r
13\r
14 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
15 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
16 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
17\r
18Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
19This program and the accompanying materials \r
20are licensed and made available under the terms and conditions of the BSD License \r
21which accompanies this distribution. The full text of the license may be found at \r
22http://opensource.org/licenses/bsd-license.php\r
8a2d4996 23\r
24THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
25WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
26\r
27**/\r
d00ed85e 28#include <Protocol/SmmVariable.h>\r
29#include <Protocol/SmmFirmwareVolumeBlock.h>\r
8a2d4996 30#include <Protocol/SmmFaultTolerantWrite.h>\r
2445a70e 31#include <Protocol/SmmAccess2.h>\r
32\r
8a2d4996 33#include <Library/SmmServicesTableLib.h>\r
34\r
d00ed85e 35#include <Guid/VariableFormat.h>\r
36#include <Guid/SmmVariableCommon.h>\r
8a2d4996 37#include "Variable.h"\r
8a2d4996 38\r
2445a70e 39EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r
40UINTN mSmramRangeCount;\r
41\r
d00ed85e 42extern VARIABLE_INFO_ENTRY *gVariableInfo;\r
8a2d4996 43EFI_HANDLE mSmmVariableHandle = NULL;\r
44EFI_HANDLE mVariableHandle = NULL;\r
45BOOLEAN mAtRuntime = FALSE;\r
46EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};\r
8a2d4996 47 \r
48EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {\r
49 VariableServiceGetVariable,\r
50 VariableServiceGetNextVariableName,\r
51 VariableServiceSetVariable,\r
52 VariableServiceQueryVariableInfo\r
53};\r
54\r
55\r
56/**\r
57 Return TRUE if ExitBootServices () has been called.\r
58 \r
59 @retval TRUE If ExitBootServices () has been called.\r
60**/\r
61BOOLEAN\r
62AtRuntime (\r
63 VOID\r
64 )\r
65{\r
66 return mAtRuntime;\r
67}\r
68\r
2445a70e 69/**\r
70 This function check if the address is in SMRAM.\r
71\r
72 @param Buffer the buffer address to be checked.\r
73 @param Length the buffer length to be checked.\r
74\r
75 @retval TRUE this address is in SMRAM.\r
76 @retval FALSE this address is NOT in SMRAM.\r
77**/\r
78BOOLEAN\r
79InternalIsAddressInSmram (\r
80 IN EFI_PHYSICAL_ADDRESS Buffer,\r
81 IN UINT64 Length\r
82 )\r
83{\r
84 UINTN Index;\r
85\r
86 for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
87 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r
88 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r
89 return TRUE;\r
90 }\r
91 }\r
92\r
93 return FALSE;\r
94}\r
95\r
96\r
8a2d4996 97/**\r
98 Initializes a basic mutual exclusion lock.\r
99\r
100 This function initializes a basic mutual exclusion lock to the released state \r
101 and returns the lock. Each lock provides mutual exclusion access at its task \r
102 priority level. Since there is no preemption or multiprocessor support in EFI,\r
103 acquiring the lock only consists of raising to the locks TPL.\r
104 If Lock is NULL, then ASSERT().\r
105 If Priority is not a valid TPL value, then ASSERT().\r
106\r
107 @param Lock A pointer to the lock data structure to initialize.\r
108 @param Priority EFI TPL is associated with the lock.\r
109\r
110 @return The lock.\r
111\r
112**/\r
113EFI_LOCK *\r
114InitializeLock (\r
115 IN OUT EFI_LOCK *Lock,\r
116 IN EFI_TPL Priority\r
117 )\r
118{\r
119 return Lock;\r
120}\r
121\r
122/**\r
123 Acquires lock only at boot time. Simply returns at runtime.\r
124\r
125 This is a temperary function that will be removed when\r
126 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
127 Runtimer driver in RT phase.\r
128 It calls EfiAcquireLock() at boot time, and simply returns\r
129 at runtime.\r
130\r
131 @param Lock A pointer to the lock to acquire.\r
132\r
133**/\r
134VOID\r
135AcquireLockOnlyAtBootTime (\r
136 IN EFI_LOCK *Lock\r
137 )\r
138{\r
139\r
140}\r
141\r
142\r
143/**\r
144 Releases lock only at boot time. Simply returns at runtime.\r
145\r
146 This is a temperary function which will be removed when\r
147 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
148 Runtimer driver in RT phase.\r
149 It calls EfiReleaseLock() at boot time and simply returns\r
150 at runtime.\r
151\r
152 @param Lock A pointer to the lock to release.\r
153\r
154**/\r
155VOID\r
156ReleaseLockOnlyAtBootTime (\r
157 IN EFI_LOCK *Lock\r
158 )\r
159{\r
160\r
161}\r
162\r
163/**\r
164 Retrive the SMM Fault Tolerent Write protocol interface.\r
165\r
166 @param[out] FtwProtocol The interface of SMM Ftw protocol\r
167\r
168 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.\r
169 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.\r
170 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
171\r
172**/\r
173EFI_STATUS\r
174GetFtwProtocol (\r
175 OUT VOID **FtwProtocol\r
176 )\r
177{\r
178 EFI_STATUS Status;\r
179\r
180 //\r
181 // Locate Smm Fault Tolerent Write protocol\r
182 //\r
183 Status = gSmst->SmmLocateProtocol (\r
184 &gEfiSmmFaultTolerantWriteProtocolGuid, \r
185 NULL, \r
186 FtwProtocol\r
187 );\r
188 return Status;\r
189}\r
190\r
191\r
192/**\r
193 Retrive the SMM FVB protocol interface by HANDLE.\r
194\r
195 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for\r
196 reading, writing, and erasing the target block.\r
197 @param[out] FvBlock The interface of SMM FVB protocol\r
198\r
199 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
200 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.\r
201 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
202\r
203**/\r
204EFI_STATUS\r
205GetFvbByHandle (\r
206 IN EFI_HANDLE FvBlockHandle,\r
207 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
208 )\r
209{\r
210 //\r
211 // To get the SMM FVB protocol interface on the handle\r
212 //\r
213 return gSmst->SmmHandleProtocol (\r
214 FvBlockHandle,\r
215 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
216 (VOID **) FvBlock\r
217 );\r
218}\r
219\r
220\r
221/**\r
222 Function returns an array of handles that support the SMM FVB protocol\r
223 in a buffer allocated from pool. \r
224\r
225 @param[out] NumberHandles The number of handles returned in Buffer.\r
226 @param[out] Buffer A pointer to the buffer to return the requested\r
227 array of handles that support SMM FVB protocol.\r
228\r
229 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
230 handles in Buffer was returned in NumberHandles.\r
231 @retval EFI_NOT_FOUND No SMM FVB handle was found.\r
232 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
233 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
234\r
235**/\r
236EFI_STATUS\r
237GetFvbCountAndBuffer (\r
238 OUT UINTN *NumberHandles,\r
239 OUT EFI_HANDLE **Buffer\r
240 )\r
241{\r
242 EFI_STATUS Status;\r
243 UINTN BufferSize;\r
244\r
245 if ((NumberHandles == NULL) || (Buffer == NULL)) {\r
246 return EFI_INVALID_PARAMETER;\r
247 }\r
248\r
249 BufferSize = 0;\r
250 *NumberHandles = 0;\r
251 *Buffer = NULL;\r
252 Status = gSmst->SmmLocateHandle (\r
253 ByProtocol,\r
254 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
255 NULL,\r
256 &BufferSize,\r
257 *Buffer\r
258 );\r
259 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
260 return EFI_NOT_FOUND;\r
261 }\r
262\r
263 *Buffer = AllocatePool (BufferSize);\r
264 if (*Buffer == NULL) {\r
265 return EFI_OUT_OF_RESOURCES;\r
266 }\r
267\r
268 Status = gSmst->SmmLocateHandle (\r
269 ByProtocol,\r
270 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
271 NULL,\r
272 &BufferSize,\r
273 *Buffer\r
274 );\r
275\r
276 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);\r
277 if (EFI_ERROR(Status)) {\r
278 *NumberHandles = 0;\r
279 }\r
280\r
281 return Status;\r
282}\r
283\r
284\r
285/**\r
286 Get the variable statistics information from the information buffer pointed by gVariableInfo.\r
287\r
2445a70e 288 Caution: This function may be invoked at SMM runtime.\r
289 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.\r
290\r
291 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.\r
292 On input, point to the variable information returned last time. if \r
293 InfoEntry->VendorGuid is zero, return the first information.\r
294 On output, point to the next variable information.\r
295 @param[in, out] InfoSize On input, the size of the variable information buffer.\r
296 On output, the returned variable information size.\r
8a2d4996 297\r
298 @retval EFI_SUCCESS The variable information is found and returned successfully.\r
299 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The \r
300 PcdVariableCollectStatistics should be set TRUE to support it.\r
301 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.\r
302\r
303**/\r
304EFI_STATUS\r
305SmmVariableGetStatistics (\r
d00ed85e 306 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,\r
8a2d4996 307 IN OUT UINTN *InfoSize\r
308 )\r
309{\r
d00ed85e 310 VARIABLE_INFO_ENTRY *VariableInfo;\r
8a2d4996 311 UINTN NameLength;\r
312 UINTN StatisticsInfoSize;\r
313 CHAR16 *InfoName;\r
314 \r
315 ASSERT (InfoEntry != NULL);\r
316 VariableInfo = gVariableInfo; \r
317 if (VariableInfo == NULL) {\r
318 return EFI_UNSUPPORTED;\r
319 }\r
320\r
d00ed85e 321 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
eb96e4f2 322 if (*InfoSize < StatisticsInfoSize) {\r
8a2d4996 323 *InfoSize = StatisticsInfoSize;\r
324 return EFI_BUFFER_TOO_SMALL;\r
325 }\r
326 InfoName = (CHAR16 *)(InfoEntry + 1);\r
327\r
328 if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) {\r
329 //\r
330 // Return the first variable info\r
331 //\r
d00ed85e 332 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
8a2d4996 333 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
334 *InfoSize = StatisticsInfoSize;\r
335 return EFI_SUCCESS;\r
336 }\r
337\r
338 //\r
339 // Get the next variable info\r
340 //\r
341 while (VariableInfo != NULL) {\r
342 if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) {\r
343 NameLength = StrSize (VariableInfo->Name);\r
344 if (NameLength == StrSize (InfoName)) {\r
345 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {\r
346 //\r
347 // Find the match one\r
348 //\r
349 VariableInfo = VariableInfo->Next;\r
350 break;\r
351 }\r
352 }\r
353 }\r
354 VariableInfo = VariableInfo->Next;\r
355 };\r
356 \r
357 if (VariableInfo == NULL) {\r
358 *InfoSize = 0;\r
359 return EFI_SUCCESS;\r
360 }\r
361\r
362 //\r
363 // Output the new variable info\r
364 //\r
d00ed85e 365 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
8a2d4996 366 if (*InfoSize < StatisticsInfoSize) {\r
367 *InfoSize = StatisticsInfoSize;\r
368 return EFI_BUFFER_TOO_SMALL;\r
369 }\r
370\r
d00ed85e 371 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
8a2d4996 372 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
373 *InfoSize = StatisticsInfoSize;\r
374 \r
375 return EFI_SUCCESS;\r
376}\r
377\r
378\r
379/**\r
380 Communication service SMI Handler entry.\r
381\r
382 This SMI handler provides services for the variable wrapper driver.\r
383\r
2445a70e 384 Caution: This function may receive untrusted input.\r
385 This variable data and communicate buffer are external input, so this function will do basic validation.\r
386 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
387 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
388 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
389\r
8a2d4996 390 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
391 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
392 handler was registered.\r
393 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
394 be conveyed from a non-SMM environment into an SMM environment.\r
395 @param[in, out] CommBufferSize The size of the CommBuffer.\r
396\r
397 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
398 should still be called.\r
399 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
400 still be called.\r
401 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
402 be called.\r
403 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
404**/\r
405EFI_STATUS\r
406EFIAPI\r
407SmmVariableHandler (\r
408 IN EFI_HANDLE DispatchHandle,\r
409 IN CONST VOID *RegisterContext,\r
410 IN OUT VOID *CommBuffer,\r
411 IN OUT UINTN *CommBufferSize\r
412 )\r
413{\r
414 EFI_STATUS Status;\r
415 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
416 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
417 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;\r
418 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;\r
d00ed85e 419 VARIABLE_INFO_ENTRY *VariableInfo;\r
8a2d4996 420 UINTN InfoSize;\r
421\r
2445a70e 422 //\r
423 // If input is invalid, stop processing this SMI\r
424 //\r
425 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
426 return EFI_SUCCESS;\r
427 }\r
428\r
d294b9a4 429 if (*CommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
2445a70e 430 return EFI_SUCCESS;\r
431 }\r
432\r
433 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) {\r
434 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));\r
435 return EFI_SUCCESS;\r
436 }\r
8a2d4996 437\r
438 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
439 switch (SmmVariableFunctionHeader->Function) {\r
440 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
2445a70e 441 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;\r
442 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) \r
443 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
444\r
445 //\r
446 // SMRAM range check already covered before\r
447 //\r
3f5c168f 448 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
2445a70e 449 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
450 Status = EFI_ACCESS_DENIED;\r
451 goto EXIT;\r
452 }\r
453\r
8a2d4996 454 Status = VariableServiceGetVariable (\r
455 SmmVariableHeader->Name,\r
456 &SmmVariableHeader->Guid,\r
457 &SmmVariableHeader->Attributes,\r
458 &SmmVariableHeader->DataSize,\r
459 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
460 );\r
461 break;\r
462 \r
463 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
464 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data;\r
2445a70e 465 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
466\r
467 //\r
468 // SMRAM range check already covered before\r
469 //\r
3f5c168f 470 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
2445a70e 471 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
472 Status = EFI_ACCESS_DENIED;\r
473 goto EXIT;\r
474 }\r
475\r
8a2d4996 476 Status = VariableServiceGetNextVariableName (\r
477 &GetNextVariableName->NameSize,\r
478 GetNextVariableName->Name,\r
479 &GetNextVariableName->Guid\r
480 );\r
481 break;\r
482 \r
483 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
484 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;\r
d17c4eac 485 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
486 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
487\r
488 //\r
489 // SMRAM range check already covered before\r
490 // Data buffer should not contain SMM range\r
491 //\r
492 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
493 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
494 Status = EFI_ACCESS_DENIED;\r
495 goto EXIT;\r
496 }\r
497\r
8a2d4996 498 Status = VariableServiceSetVariable (\r
499 SmmVariableHeader->Name,\r
500 &SmmVariableHeader->Guid,\r
501 SmmVariableHeader->Attributes,\r
502 SmmVariableHeader->DataSize,\r
503 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
504 );\r
505 break;\r
506 \r
507 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
508 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
2445a70e 509 InfoSize = sizeof(SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
510\r
511 //\r
512 // SMRAM range check already covered before\r
513 //\r
3f5c168f 514 if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
2445a70e 515 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
516 Status = EFI_ACCESS_DENIED;\r
517 goto EXIT;\r
518 }\r
519\r
8a2d4996 520 Status = VariableServiceQueryVariableInfo (\r
521 QueryVariableInfo->Attributes,\r
522 &QueryVariableInfo->MaximumVariableStorageSize,\r
523 &QueryVariableInfo->RemainingVariableStorageSize,\r
524 &QueryVariableInfo->MaximumVariableSize\r
525 );\r
526 break;\r
527\r
528 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
876ac395 529 if (AtRuntime()) {\r
530 Status = EFI_UNSUPPORTED;\r
531 break;\r
532 }\r
8a2d4996 533 ReclaimForOS ();\r
534 Status = EFI_SUCCESS;\r
535 break;\r
536 \r
537 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
538 mAtRuntime = TRUE;\r
539 Status = EFI_SUCCESS;\r
540 break;\r
541\r
542 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
d00ed85e 543 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
3f5c168f 544 InfoSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
2445a70e 545\r
546 //\r
547 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
548 // It is covered by previous CommBuffer check \r
549 //\r
550 \r
551 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
552 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));\r
553 Status = EFI_ACCESS_DENIED;\r
554 goto EXIT;\r
555 } \r
556\r
8a2d4996 557 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
3f5c168f 558 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
8a2d4996 559 break;\r
560\r
561 default:\r
8a2d4996 562 Status = EFI_UNSUPPORTED;\r
563 }\r
564\r
2445a70e 565EXIT:\r
566\r
8a2d4996 567 SmmVariableFunctionHeader->ReturnStatus = Status;\r
568\r
569 return EFI_SUCCESS;\r
570}\r
571\r
572\r
573/**\r
574 SMM Fault Tolerant Write protocol notification event handler.\r
575\r
576 Non-Volatile variable write may needs FTW protocol to reclaim when \r
577 writting variable.\r
578 \r
579 @param Protocol Points to the protocol's unique identifier\r
580 @param Interface Points to the interface instance\r
581 @param Handle The handle on which the interface was installed\r
582\r
583 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
584 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
585 \r
586 **/\r
587EFI_STATUS\r
588EFIAPI\r
589SmmFtwNotificationEvent (\r
590 IN CONST EFI_GUID *Protocol,\r
591 IN VOID *Interface,\r
592 IN EFI_HANDLE Handle\r
593 )\r
594{\r
595 EFI_STATUS Status;\r
596 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
597 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
598 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
599 \r
600 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
601 return EFI_SUCCESS;\r
602 }\r
603\r
604 //\r
605 // Ensure SMM FTW protocol is installed.\r
606 //\r
5c7fa429 607 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
8a2d4996 608 if (EFI_ERROR (Status)) {\r
609 return Status;\r
610 }\r
611\r
612 //\r
613 // Find the proper FVB protocol for variable.\r
614 //\r
615 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
616 if (NvStorageVariableBase == 0) {\r
617 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
618 }\r
619 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
620 if (EFI_ERROR (Status)) {\r
621 return EFI_NOT_FOUND;\r
622 }\r
623\r
624 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
625 \r
626 Status = VariableWriteServiceInitialize ();\r
627 ASSERT_EFI_ERROR (Status);\r
628 \r
629 //\r
630 // Notify the variable wrapper driver the variable write service is ready\r
631 //\r
632 Status = gBS->InstallProtocolInterface (\r
633 &mSmmVariableHandle,\r
d00ed85e 634 &gSmmVariableWriteGuid,\r
8a2d4996 635 EFI_NATIVE_INTERFACE,\r
636 NULL\r
637 );\r
638 ASSERT_EFI_ERROR (Status);\r
639 \r
640 return EFI_SUCCESS;\r
641}\r
642\r
643\r
644/**\r
645 Variable Driver main entry point. The Variable driver places the 4 EFI\r
646 runtime services in the EFI System Table and installs arch protocols \r
d00ed85e 647 for variable read and write services being available. It also registers\r
8a2d4996 648 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
649\r
650 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
651 @param[in] SystemTable A pointer to the EFI System Table.\r
652 \r
653 @retval EFI_SUCCESS Variable service successfully initialized.\r
654\r
655**/\r
656EFI_STATUS\r
657EFIAPI\r
658VariableServiceInitialize (\r
659 IN EFI_HANDLE ImageHandle,\r
660 IN EFI_SYSTEM_TABLE *SystemTable\r
661 )\r
662{\r
663 EFI_STATUS Status;\r
664 EFI_HANDLE VariableHandle;\r
665 VOID *SmmFtwRegistration;\r
2445a70e 666 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
667 UINTN Size;\r
668\r
8a2d4996 669 //\r
670 // Variable initialize.\r
671 //\r
672 Status = VariableCommonInitialize ();\r
673 ASSERT_EFI_ERROR (Status);\r
674\r
675 //\r
676 // Install the Smm Variable Protocol on a new handle.\r
677 //\r
678 VariableHandle = NULL;\r
679 Status = gSmst->SmmInstallProtocolInterface (\r
680 &VariableHandle,\r
681 &gEfiSmmVariableProtocolGuid,\r
682 EFI_NATIVE_INTERFACE,\r
683 &gSmmVariable\r
684 );\r
685 ASSERT_EFI_ERROR (Status);\r
686\r
2445a70e 687 //\r
688 // Get SMRAM information\r
689 //\r
690 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
691 ASSERT_EFI_ERROR (Status);\r
692\r
693 Size = 0;\r
694 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
695 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
696\r
697 Status = gSmst->SmmAllocatePool (\r
698 EfiRuntimeServicesData,\r
699 Size,\r
700 (VOID **)&mSmramRanges\r
701 );\r
702 ASSERT_EFI_ERROR (Status);\r
703\r
704 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
705 ASSERT_EFI_ERROR (Status);\r
706\r
707 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
708\r
8a2d4996 709 ///\r
710 /// Register SMM variable SMI handler\r
711 ///\r
712 VariableHandle = NULL;\r
713 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
714 ASSERT_EFI_ERROR (Status);\r
715 \r
716 //\r
717 // Notify the variable wrapper driver the variable service is ready\r
718 //\r
719 Status = SystemTable->BootServices->InstallProtocolInterface (\r
720 &mVariableHandle,\r
721 &gEfiSmmVariableProtocolGuid,\r
722 EFI_NATIVE_INTERFACE,\r
723 &gSmmVariable\r
724 );\r
725 ASSERT_EFI_ERROR (Status);\r
726 \r
727 //\r
728 // Register FtwNotificationEvent () notify function.\r
729 // \r
730 Status = gSmst->SmmRegisterProtocolNotify (\r
731 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
732 SmmFtwNotificationEvent,\r
733 &SmmFtwRegistration\r
734 );\r
735 ASSERT_EFI_ERROR (Status);\r
736\r
737 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
738 \r
739 return EFI_SUCCESS;\r
740}\r
741\r
742\r