]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
Fix a buffer overflow bug in VariableSmm driver.
[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
429 if (*CommBufferSize < sizeof(SMM_VARIABLE_COMMUNICATE_HEADER) - 1) {\r
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
448 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {\r
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
470 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {\r
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
485 Status = VariableServiceSetVariable (\r
486 SmmVariableHeader->Name,\r
487 &SmmVariableHeader->Guid,\r
488 SmmVariableHeader->Attributes,\r
489 SmmVariableHeader->DataSize,\r
490 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
491 );\r
492 break;\r
493 \r
494 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
495 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
2445a70e 496 InfoSize = sizeof(SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
497\r
498 //\r
499 // SMRAM range check already covered before\r
500 //\r
501 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {\r
502 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
503 Status = EFI_ACCESS_DENIED;\r
504 goto EXIT;\r
505 }\r
506\r
8a2d4996 507 Status = VariableServiceQueryVariableInfo (\r
508 QueryVariableInfo->Attributes,\r
509 &QueryVariableInfo->MaximumVariableStorageSize,\r
510 &QueryVariableInfo->RemainingVariableStorageSize,\r
511 &QueryVariableInfo->MaximumVariableSize\r
512 );\r
513 break;\r
514\r
515 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
516 ReclaimForOS ();\r
517 Status = EFI_SUCCESS;\r
518 break;\r
519 \r
520 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
521 mAtRuntime = TRUE;\r
522 Status = EFI_SUCCESS;\r
523 break;\r
524\r
525 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
d00ed85e 526 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
8a2d4996 527 InfoSize = *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);\r
2445a70e 528\r
529 //\r
530 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
531 // It is covered by previous CommBuffer check \r
532 //\r
533 \r
534 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
535 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));\r
536 Status = EFI_ACCESS_DENIED;\r
537 goto EXIT;\r
538 } \r
539\r
8a2d4996 540 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
541 *CommBufferSize = InfoSize + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);\r
542 break;\r
543\r
544 default:\r
8a2d4996 545 Status = EFI_UNSUPPORTED;\r
546 }\r
547\r
2445a70e 548EXIT:\r
549\r
8a2d4996 550 SmmVariableFunctionHeader->ReturnStatus = Status;\r
551\r
552 return EFI_SUCCESS;\r
553}\r
554\r
555\r
556/**\r
557 SMM Fault Tolerant Write protocol notification event handler.\r
558\r
559 Non-Volatile variable write may needs FTW protocol to reclaim when \r
560 writting variable.\r
561 \r
562 @param Protocol Points to the protocol's unique identifier\r
563 @param Interface Points to the interface instance\r
564 @param Handle The handle on which the interface was installed\r
565\r
566 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
567 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
568 \r
569 **/\r
570EFI_STATUS\r
571EFIAPI\r
572SmmFtwNotificationEvent (\r
573 IN CONST EFI_GUID *Protocol,\r
574 IN VOID *Interface,\r
575 IN EFI_HANDLE Handle\r
576 )\r
577{\r
578 EFI_STATUS Status;\r
579 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
580 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
581 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
582 \r
583 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
584 return EFI_SUCCESS;\r
585 }\r
586\r
587 //\r
588 // Ensure SMM FTW protocol is installed.\r
589 //\r
5c7fa429 590 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
8a2d4996 591 if (EFI_ERROR (Status)) {\r
592 return Status;\r
593 }\r
594\r
595 //\r
596 // Find the proper FVB protocol for variable.\r
597 //\r
598 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
599 if (NvStorageVariableBase == 0) {\r
600 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
601 }\r
602 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
603 if (EFI_ERROR (Status)) {\r
604 return EFI_NOT_FOUND;\r
605 }\r
606\r
607 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
608 \r
609 Status = VariableWriteServiceInitialize ();\r
610 ASSERT_EFI_ERROR (Status);\r
611 \r
612 //\r
613 // Notify the variable wrapper driver the variable write service is ready\r
614 //\r
615 Status = gBS->InstallProtocolInterface (\r
616 &mSmmVariableHandle,\r
d00ed85e 617 &gSmmVariableWriteGuid,\r
8a2d4996 618 EFI_NATIVE_INTERFACE,\r
619 NULL\r
620 );\r
621 ASSERT_EFI_ERROR (Status);\r
622 \r
623 return EFI_SUCCESS;\r
624}\r
625\r
626\r
627/**\r
628 Variable Driver main entry point. The Variable driver places the 4 EFI\r
629 runtime services in the EFI System Table and installs arch protocols \r
d00ed85e 630 for variable read and write services being available. It also registers\r
8a2d4996 631 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
632\r
633 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
634 @param[in] SystemTable A pointer to the EFI System Table.\r
635 \r
636 @retval EFI_SUCCESS Variable service successfully initialized.\r
637\r
638**/\r
639EFI_STATUS\r
640EFIAPI\r
641VariableServiceInitialize (\r
642 IN EFI_HANDLE ImageHandle,\r
643 IN EFI_SYSTEM_TABLE *SystemTable\r
644 )\r
645{\r
646 EFI_STATUS Status;\r
647 EFI_HANDLE VariableHandle;\r
648 VOID *SmmFtwRegistration;\r
2445a70e 649 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
650 UINTN Size;\r
651\r
8a2d4996 652 //\r
653 // Variable initialize.\r
654 //\r
655 Status = VariableCommonInitialize ();\r
656 ASSERT_EFI_ERROR (Status);\r
657\r
658 //\r
659 // Install the Smm Variable Protocol on a new handle.\r
660 //\r
661 VariableHandle = NULL;\r
662 Status = gSmst->SmmInstallProtocolInterface (\r
663 &VariableHandle,\r
664 &gEfiSmmVariableProtocolGuid,\r
665 EFI_NATIVE_INTERFACE,\r
666 &gSmmVariable\r
667 );\r
668 ASSERT_EFI_ERROR (Status);\r
669\r
2445a70e 670 //\r
671 // Get SMRAM information\r
672 //\r
673 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
674 ASSERT_EFI_ERROR (Status);\r
675\r
676 Size = 0;\r
677 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
678 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
679\r
680 Status = gSmst->SmmAllocatePool (\r
681 EfiRuntimeServicesData,\r
682 Size,\r
683 (VOID **)&mSmramRanges\r
684 );\r
685 ASSERT_EFI_ERROR (Status);\r
686\r
687 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
688 ASSERT_EFI_ERROR (Status);\r
689\r
690 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
691\r
8a2d4996 692 ///\r
693 /// Register SMM variable SMI handler\r
694 ///\r
695 VariableHandle = NULL;\r
696 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
697 ASSERT_EFI_ERROR (Status);\r
698 \r
699 //\r
700 // Notify the variable wrapper driver the variable service is ready\r
701 //\r
702 Status = SystemTable->BootServices->InstallProtocolInterface (\r
703 &mVariableHandle,\r
704 &gEfiSmmVariableProtocolGuid,\r
705 EFI_NATIVE_INTERFACE,\r
706 &gSmmVariable\r
707 );\r
708 ASSERT_EFI_ERROR (Status);\r
709 \r
710 //\r
711 // Register FtwNotificationEvent () notify function.\r
712 // \r
713 Status = gSmst->SmmRegisterProtocolNotify (\r
714 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
715 SmmFtwNotificationEvent,\r
716 &SmmFtwRegistration\r
717 );\r
718 ASSERT_EFI_ERROR (Status);\r
719\r
720 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
721 \r
722 return EFI_SUCCESS;\r
723}\r
724\r
725\r