]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
Update PCD description.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmm.c
CommitLineData
0c18794e 1/** @file\r
2 The sample implementation for SMM variable protocol. And this driver \r
3 implements an SMI handler to communicate with the DXE runtime driver \r
4 to provide variable services.\r
5\r
dc204d5a
JY
6 Caution: This module requires additional review when modified.\r
7 This driver will have external input - variable data and communicate buffer in SMM mode.\r
8 This external input must be validated carefully to avoid security issue like\r
9 buffer overflow, integer overflow.\r
10\r
11 SmmVariableHandler() will receive untrusted input and do basic validation.\r
12\r
13 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
14 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
15 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
16\r
17Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
0c18794e 18This program and the accompanying materials \r
19are licensed and made available under the terms and conditions of the BSD License \r
20which accompanies this distribution. The full text of the license may be found at \r
21http://opensource.org/licenses/bsd-license.php\r
22\r
23THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
24WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
25\r
26**/\r
27\r
28#include <Protocol/SmmVariable.h>\r
29#include <Protocol/SmmFirmwareVolumeBlock.h>\r
30#include <Protocol/SmmFaultTolerantWrite.h>\r
25a4e71a 31#include <Protocol/SmmAccess2.h>\r
32\r
0c18794e 33#include <Library/SmmServicesTableLib.h>\r
34\r
35#include <Guid/AuthenticatedVariableFormat.h>\r
36#include <Guid/SmmVariableCommon.h>\r
37#include "Variable.h"\r
38\r
25a4e71a 39EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r
40UINTN mSmramRangeCount;\r
41\r
0c18794e 42extern VARIABLE_INFO_ENTRY *gVariableInfo;\r
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
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
25a4e71a 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
0c18794e 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
dc204d5a
JY
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
648f98d1 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
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 @retval EFI_INVALID_PARAMETER Input parameter is invalid.\r
0c18794e 303\r
304**/\r
305EFI_STATUS\r
306SmmVariableGetStatistics (\r
307 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,\r
308 IN OUT UINTN *InfoSize\r
309 )\r
310{\r
311 VARIABLE_INFO_ENTRY *VariableInfo;\r
312 UINTN NameLength;\r
313 UINTN StatisticsInfoSize;\r
314 CHAR16 *InfoName;\r
315 \r
648f98d1 316 if (InfoEntry == NULL) {\r
317 return EFI_INVALID_PARAMETER;\r
318 }\r
319 \r
0c18794e 320 VariableInfo = gVariableInfo; \r
321 if (VariableInfo == NULL) {\r
322 return EFI_UNSUPPORTED;\r
323 }\r
324\r
325 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
12373f2c 326 if (*InfoSize < StatisticsInfoSize) {\r
0c18794e 327 *InfoSize = StatisticsInfoSize;\r
328 return EFI_BUFFER_TOO_SMALL;\r
329 }\r
330 InfoName = (CHAR16 *)(InfoEntry + 1);\r
331\r
332 if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) {\r
333 //\r
334 // Return the first variable info\r
335 //\r
336 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
337 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
338 *InfoSize = StatisticsInfoSize;\r
339 return EFI_SUCCESS;\r
340 }\r
341\r
342 //\r
343 // Get the next variable info\r
344 //\r
345 while (VariableInfo != NULL) {\r
346 if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) {\r
347 NameLength = StrSize (VariableInfo->Name);\r
348 if (NameLength == StrSize (InfoName)) {\r
349 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {\r
350 //\r
351 // Find the match one\r
352 //\r
353 VariableInfo = VariableInfo->Next;\r
354 break;\r
355 }\r
356 }\r
357 }\r
358 VariableInfo = VariableInfo->Next;\r
359 };\r
360 \r
361 if (VariableInfo == NULL) {\r
362 *InfoSize = 0;\r
363 return EFI_SUCCESS;\r
364 }\r
365\r
366 //\r
367 // Output the new variable info\r
368 //\r
369 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
370 if (*InfoSize < StatisticsInfoSize) {\r
371 *InfoSize = StatisticsInfoSize;\r
372 return EFI_BUFFER_TOO_SMALL;\r
373 }\r
374\r
375 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
376 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
377 *InfoSize = StatisticsInfoSize;\r
378 \r
379 return EFI_SUCCESS;\r
380}\r
381\r
382\r
383/**\r
384 Communication service SMI Handler entry.\r
385\r
386 This SMI handler provides services for the variable wrapper driver.\r
387\r
dc204d5a
JY
388 Caution: This function may receive untrusted input.\r
389 This variable data and communicate buffer are external input, so this function will do basic validation.\r
390 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
391 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
392 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
393\r
0c18794e 394 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
395 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
396 handler was registered.\r
397 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
398 be conveyed from a non-SMM environment into an SMM environment.\r
399 @param[in, out] CommBufferSize The size of the CommBuffer.\r
400\r
401 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
402 should still be called.\r
403 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
404 still be called.\r
405 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
406 be called.\r
407 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
648f98d1 408\r
0c18794e 409**/\r
410EFI_STATUS\r
411EFIAPI\r
412SmmVariableHandler (\r
413 IN EFI_HANDLE DispatchHandle,\r
414 IN CONST VOID *RegisterContext,\r
415 IN OUT VOID *CommBuffer,\r
416 IN OUT UINTN *CommBufferSize\r
417 )\r
418{\r
419 EFI_STATUS Status;\r
420 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
421 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
422 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;\r
423 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;\r
424 VARIABLE_INFO_ENTRY *VariableInfo;\r
425 UINTN InfoSize;\r
426\r
25a4e71a 427 //\r
428 // If input is invalid, stop processing this SMI\r
429 //\r
430 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
431 return EFI_SUCCESS;\r
648f98d1 432 }\r
0c18794e 433\r
ad84df72 434 if (*CommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
25a4e71a 435 return EFI_SUCCESS;\r
436 }\r
437\r
438 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) {\r
439 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));\r
440 return EFI_SUCCESS;\r
441 }\r
442 \r
0c18794e 443 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
25a4e71a 444 \r
0c18794e 445 switch (SmmVariableFunctionHeader->Function) {\r
446 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
25a4e71a 447 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;\r
448 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) \r
449 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
450\r
451 //\r
452 // SMRAM range check already covered before\r
453 //\r
454 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {\r
455 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
456 Status = EFI_ACCESS_DENIED;\r
457 goto EXIT;\r
458 }\r
459\r
0c18794e 460 Status = VariableServiceGetVariable (\r
461 SmmVariableHeader->Name,\r
462 &SmmVariableHeader->Guid,\r
463 &SmmVariableHeader->Attributes,\r
464 &SmmVariableHeader->DataSize,\r
465 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
466 );\r
467 break;\r
468 \r
469 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
470 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data;\r
25a4e71a 471 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
472\r
473 //\r
474 // SMRAM range check already covered before\r
475 //\r
476 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {\r
477 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
478 Status = EFI_ACCESS_DENIED;\r
479 goto EXIT;\r
480 }\r
481\r
0c18794e 482 Status = VariableServiceGetNextVariableName (\r
483 &GetNextVariableName->NameSize,\r
484 GetNextVariableName->Name,\r
485 &GetNextVariableName->Guid\r
486 );\r
487 break;\r
488 \r
489 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
490 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;\r
491 Status = VariableServiceSetVariable (\r
492 SmmVariableHeader->Name,\r
493 &SmmVariableHeader->Guid,\r
494 SmmVariableHeader->Attributes,\r
495 SmmVariableHeader->DataSize,\r
496 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
497 );\r
498 break;\r
499 \r
500 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
501 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
25a4e71a 502 InfoSize = sizeof(SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
503\r
504 //\r
505 // SMRAM range check already covered before\r
506 //\r
507 if (InfoSize > *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data)) {\r
508 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
509 Status = EFI_ACCESS_DENIED;\r
510 goto EXIT;\r
511 }\r
512 \r
0c18794e 513 Status = VariableServiceQueryVariableInfo (\r
514 QueryVariableInfo->Attributes,\r
515 &QueryVariableInfo->MaximumVariableStorageSize,\r
516 &QueryVariableInfo->RemainingVariableStorageSize,\r
517 &QueryVariableInfo->MaximumVariableSize\r
518 );\r
519 break;\r
520\r
521 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
876ac395 522 if (AtRuntime()) {\r
523 Status = EFI_UNSUPPORTED;\r
524 break;\r
525 }\r
0c18794e 526 ReclaimForOS ();\r
527 Status = EFI_SUCCESS;\r
528 break;\r
529 \r
530 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
531 mAtRuntime = TRUE;\r
532 Status = EFI_SUCCESS;\r
533 break;\r
534\r
535 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
536 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
537 InfoSize = *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);\r
25a4e71a 538\r
539 //\r
540 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
541 // It is covered by previous CommBuffer check \r
542 //\r
543 \r
544 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
545 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));\r
546 Status = EFI_ACCESS_DENIED;\r
547 goto EXIT;\r
548 } \r
549\r
0c18794e 550 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
551 *CommBufferSize = InfoSize + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);\r
552 break;\r
553\r
554 default:\r
0c18794e 555 Status = EFI_UNSUPPORTED;\r
556 }\r
557\r
25a4e71a 558EXIT:\r
0c18794e 559\r
25a4e71a 560 SmmVariableFunctionHeader->ReturnStatus = Status;\r
0c18794e 561 return EFI_SUCCESS;\r
562}\r
563\r
564\r
565/**\r
566 SMM Fault Tolerant Write protocol notification event handler.\r
567\r
568 Non-Volatile variable write may needs FTW protocol to reclaim when \r
569 writting variable.\r
570 \r
571 @param Protocol Points to the protocol's unique identifier\r
572 @param Interface Points to the interface instance\r
573 @param Handle The handle on which the interface was installed\r
574\r
575 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
576 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
577 \r
578 **/\r
579EFI_STATUS\r
580EFIAPI\r
581SmmFtwNotificationEvent (\r
582 IN CONST EFI_GUID *Protocol,\r
583 IN VOID *Interface,\r
584 IN EFI_HANDLE Handle\r
585 )\r
586{\r
587 EFI_STATUS Status;\r
588 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
589 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
590 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
591 \r
592 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
593 return EFI_SUCCESS;\r
594 }\r
595\r
596 //\r
597 // Ensure SMM FTW protocol is installed.\r
598 //\r
599 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
600 if (EFI_ERROR (Status)) {\r
601 return Status;\r
602 }\r
603\r
604 //\r
605 // Find the proper FVB protocol for variable.\r
606 //\r
607 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
608 if (NvStorageVariableBase == 0) {\r
609 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
610 }\r
611 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
612 if (EFI_ERROR (Status)) {\r
613 return EFI_NOT_FOUND;\r
614 }\r
615\r
616 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
617 \r
618 Status = VariableWriteServiceInitialize ();\r
619 ASSERT_EFI_ERROR (Status);\r
620 \r
621 //\r
622 // Notify the variable wrapper driver the variable write service is ready\r
623 //\r
624 Status = gBS->InstallProtocolInterface (\r
625 &mSmmVariableHandle,\r
626 &gSmmVariableWriteGuid,\r
627 EFI_NATIVE_INTERFACE,\r
628 NULL\r
629 );\r
630 ASSERT_EFI_ERROR (Status);\r
631 \r
632 return EFI_SUCCESS;\r
633}\r
634\r
635\r
636/**\r
637 Variable Driver main entry point. The Variable driver places the 4 EFI\r
638 runtime services in the EFI System Table and installs arch protocols \r
639 for variable read and write services being available. It also registers\r
640 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
641\r
642 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
643 @param[in] SystemTable A pointer to the EFI System Table.\r
644 \r
645 @retval EFI_SUCCESS Variable service successfully initialized.\r
646\r
647**/\r
648EFI_STATUS\r
649EFIAPI\r
650VariableServiceInitialize (\r
651 IN EFI_HANDLE ImageHandle,\r
652 IN EFI_SYSTEM_TABLE *SystemTable\r
653 )\r
654{\r
655 EFI_STATUS Status;\r
656 EFI_HANDLE VariableHandle;\r
657 VOID *SmmFtwRegistration;\r
25a4e71a 658 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
659 UINTN Size;\r
660\r
0c18794e 661 //\r
662 // Variable initialize.\r
663 //\r
664 Status = VariableCommonInitialize ();\r
665 ASSERT_EFI_ERROR (Status);\r
666\r
667 //\r
668 // Install the Smm Variable Protocol on a new handle.\r
669 //\r
670 VariableHandle = NULL;\r
671 Status = gSmst->SmmInstallProtocolInterface (\r
672 &VariableHandle,\r
673 &gEfiSmmVariableProtocolGuid,\r
674 EFI_NATIVE_INTERFACE,\r
675 &gSmmVariable\r
676 );\r
677 ASSERT_EFI_ERROR (Status);\r
678\r
25a4e71a 679 //\r
680 // Get SMRAM information\r
681 //\r
682 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
683 ASSERT_EFI_ERROR (Status);\r
684\r
685 Size = 0;\r
686 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
687 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
688\r
689 Status = gSmst->SmmAllocatePool (\r
690 EfiRuntimeServicesData,\r
691 Size,\r
692 (VOID **)&mSmramRanges\r
693 );\r
694 ASSERT_EFI_ERROR (Status);\r
695\r
696 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
697 ASSERT_EFI_ERROR (Status);\r
698\r
699 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
700\r
0c18794e 701 ///\r
702 /// Register SMM variable SMI handler\r
703 ///\r
704 VariableHandle = NULL;\r
705 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
706 ASSERT_EFI_ERROR (Status);\r
707 \r
708 //\r
709 // Notify the variable wrapper driver the variable service is ready\r
710 //\r
711 Status = SystemTable->BootServices->InstallProtocolInterface (\r
712 &mVariableHandle,\r
713 &gEfiSmmVariableProtocolGuid,\r
714 EFI_NATIVE_INTERFACE,\r
715 &gSmmVariable\r
716 );\r
717 ASSERT_EFI_ERROR (Status);\r
718 \r
719 //\r
720 // Register FtwNotificationEvent () notify function.\r
721 // \r
722 Status = gSmst->SmmRegisterProtocolNotify (\r
723 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
724 SmmFtwNotificationEvent,\r
725 &SmmFtwRegistration\r
726 );\r
727 ASSERT_EFI_ERROR (Status);\r
728\r
729 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
730 \r
731 return EFI_SUCCESS;\r
732}\r
733\r
734\r