]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
Add EDKII_VARIABLE_LOCK_PROTOCOL and the implementation in MdeModulePkg variable...
[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
5e5bb2a9 18Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r
2445a70e 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
ff843847 32#include <Protocol/SmmEndOfDxe.h>\r
2445a70e 33\r
8a2d4996 34#include <Library/SmmServicesTableLib.h>\r
35\r
d00ed85e 36#include <Guid/VariableFormat.h>\r
37#include <Guid/SmmVariableCommon.h>\r
8a2d4996 38#include "Variable.h"\r
8a2d4996 39\r
2445a70e 40EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r
41UINTN mSmramRangeCount;\r
42\r
d00ed85e 43extern VARIABLE_INFO_ENTRY *gVariableInfo;\r
8a2d4996 44EFI_HANDLE mSmmVariableHandle = NULL;\r
45EFI_HANDLE mVariableHandle = NULL;\r
46BOOLEAN mAtRuntime = FALSE;\r
47EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};\r
5e5bb2a9
SZ
48UINT8 *mVariableBufferPayload = NULL;\r
49UINTN mVariableBufferPayloadSize;\r
ff843847
RN
50extern BOOLEAN mEndOfDxe;\r
51extern BOOLEAN mEnableLocking;\r
52\r
53/**\r
54\r
55 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
56\r
57 @param VariableName Name of Variable to be found.\r
58 @param VendorGuid Variable vendor GUID.\r
59 @param Attributes Attribute value of the variable found\r
60 @param DataSize Size of Data found. If size is less than the\r
61 data, this value contains the required size.\r
62 @param Data Data pointer.\r
63\r
64 @return EFI_INVALID_PARAMETER Invalid parameter.\r
65 @return EFI_SUCCESS Set successfully.\r
66 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
67 @return EFI_NOT_FOUND Not found.\r
68 @return EFI_WRITE_PROTECTED Variable is read-only.\r
69\r
70**/\r
71EFI_STATUS\r
72EFIAPI\r
73SmmVariableSetVariable (\r
74 IN CHAR16 *VariableName,\r
75 IN EFI_GUID *VendorGuid,\r
76 IN UINT32 Attributes,\r
77 IN UINTN DataSize,\r
78 IN VOID *Data\r
79 )\r
80{\r
81 EFI_STATUS Status;\r
82\r
83 //\r
84 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.\r
85 //\r
86 mEnableLocking = FALSE;\r
87 Status = VariableServiceSetVariable (\r
88 VariableName,\r
89 VendorGuid,\r
90 Attributes,\r
91 DataSize,\r
92 Data\r
93 );\r
94 mEnableLocking = TRUE;\r
95 return Status;\r
96}\r
5e5bb2a9 97\r
8a2d4996 98EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {\r
99 VariableServiceGetVariable,\r
100 VariableServiceGetNextVariableName,\r
ff843847 101 SmmVariableSetVariable,\r
8a2d4996 102 VariableServiceQueryVariableInfo\r
103};\r
104\r
8a2d4996 105/**\r
106 Return TRUE if ExitBootServices () has been called.\r
107 \r
108 @retval TRUE If ExitBootServices () has been called.\r
109**/\r
110BOOLEAN\r
111AtRuntime (\r
112 VOID\r
113 )\r
114{\r
115 return mAtRuntime;\r
116}\r
117\r
2445a70e 118/**\r
119 This function check if the address is in SMRAM.\r
120\r
121 @param Buffer the buffer address to be checked.\r
122 @param Length the buffer length to be checked.\r
123\r
124 @retval TRUE this address is in SMRAM.\r
125 @retval FALSE this address is NOT in SMRAM.\r
126**/\r
127BOOLEAN\r
128InternalIsAddressInSmram (\r
129 IN EFI_PHYSICAL_ADDRESS Buffer,\r
130 IN UINT64 Length\r
131 )\r
132{\r
133 UINTN Index;\r
134\r
135 for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
136 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r
137 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r
138 return TRUE;\r
139 }\r
140 }\r
141\r
142 return FALSE;\r
143}\r
144\r
9d00d20e
SZ
145/**\r
146 This function check if the address refered by Buffer and Length is valid.\r
147\r
148 @param Buffer the buffer address to be checked.\r
149 @param Length the buffer length to be checked.\r
150\r
151 @retval TRUE this address is valid.\r
152 @retval FALSE this address is NOT valid.\r
153**/\r
154BOOLEAN\r
155InternalIsAddressValid (\r
156 IN UINTN Buffer,\r
157 IN UINTN Length\r
158 )\r
159{\r
160 if (Buffer > (MAX_ADDRESS - Length)) {\r
161 //\r
162 // Overflow happen\r
163 //\r
164 return FALSE;\r
165 }\r
166 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {\r
167 return FALSE;\r
168 }\r
169 return TRUE;\r
170}\r
2445a70e 171\r
8a2d4996 172/**\r
173 Initializes a basic mutual exclusion lock.\r
174\r
175 This function initializes a basic mutual exclusion lock to the released state \r
176 and returns the lock. Each lock provides mutual exclusion access at its task \r
177 priority level. Since there is no preemption or multiprocessor support in EFI,\r
178 acquiring the lock only consists of raising to the locks TPL.\r
179 If Lock is NULL, then ASSERT().\r
180 If Priority is not a valid TPL value, then ASSERT().\r
181\r
182 @param Lock A pointer to the lock data structure to initialize.\r
183 @param Priority EFI TPL is associated with the lock.\r
184\r
185 @return The lock.\r
186\r
187**/\r
188EFI_LOCK *\r
189InitializeLock (\r
190 IN OUT EFI_LOCK *Lock,\r
191 IN EFI_TPL Priority\r
192 )\r
193{\r
194 return Lock;\r
195}\r
196\r
197/**\r
198 Acquires lock only at boot time. Simply returns at runtime.\r
199\r
200 This is a temperary function that will be removed when\r
201 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
202 Runtimer driver in RT phase.\r
203 It calls EfiAcquireLock() at boot time, and simply returns\r
204 at runtime.\r
205\r
206 @param Lock A pointer to the lock to acquire.\r
207\r
208**/\r
209VOID\r
210AcquireLockOnlyAtBootTime (\r
211 IN EFI_LOCK *Lock\r
212 )\r
213{\r
214\r
215}\r
216\r
217\r
218/**\r
219 Releases lock only at boot time. Simply returns at runtime.\r
220\r
221 This is a temperary function which will be removed when\r
222 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
223 Runtimer driver in RT phase.\r
224 It calls EfiReleaseLock() at boot time and simply returns\r
225 at runtime.\r
226\r
227 @param Lock A pointer to the lock to release.\r
228\r
229**/\r
230VOID\r
231ReleaseLockOnlyAtBootTime (\r
232 IN EFI_LOCK *Lock\r
233 )\r
234{\r
235\r
236}\r
237\r
238/**\r
239 Retrive the SMM Fault Tolerent Write protocol interface.\r
240\r
241 @param[out] FtwProtocol The interface of SMM Ftw protocol\r
242\r
243 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.\r
244 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.\r
245 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
246\r
247**/\r
248EFI_STATUS\r
249GetFtwProtocol (\r
250 OUT VOID **FtwProtocol\r
251 )\r
252{\r
253 EFI_STATUS Status;\r
254\r
255 //\r
256 // Locate Smm Fault Tolerent Write protocol\r
257 //\r
258 Status = gSmst->SmmLocateProtocol (\r
259 &gEfiSmmFaultTolerantWriteProtocolGuid, \r
260 NULL, \r
261 FtwProtocol\r
262 );\r
263 return Status;\r
264}\r
265\r
266\r
267/**\r
268 Retrive the SMM FVB protocol interface by HANDLE.\r
269\r
270 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for\r
271 reading, writing, and erasing the target block.\r
272 @param[out] FvBlock The interface of SMM FVB protocol\r
273\r
274 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
275 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.\r
276 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
277\r
278**/\r
279EFI_STATUS\r
280GetFvbByHandle (\r
281 IN EFI_HANDLE FvBlockHandle,\r
282 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
283 )\r
284{\r
285 //\r
286 // To get the SMM FVB protocol interface on the handle\r
287 //\r
288 return gSmst->SmmHandleProtocol (\r
289 FvBlockHandle,\r
290 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
291 (VOID **) FvBlock\r
292 );\r
293}\r
294\r
295\r
296/**\r
297 Function returns an array of handles that support the SMM FVB protocol\r
298 in a buffer allocated from pool. \r
299\r
300 @param[out] NumberHandles The number of handles returned in Buffer.\r
301 @param[out] Buffer A pointer to the buffer to return the requested\r
302 array of handles that support SMM FVB protocol.\r
303\r
304 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
305 handles in Buffer was returned in NumberHandles.\r
306 @retval EFI_NOT_FOUND No SMM FVB handle was found.\r
307 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
308 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
309\r
310**/\r
311EFI_STATUS\r
312GetFvbCountAndBuffer (\r
313 OUT UINTN *NumberHandles,\r
314 OUT EFI_HANDLE **Buffer\r
315 )\r
316{\r
317 EFI_STATUS Status;\r
318 UINTN BufferSize;\r
319\r
320 if ((NumberHandles == NULL) || (Buffer == NULL)) {\r
321 return EFI_INVALID_PARAMETER;\r
322 }\r
323\r
324 BufferSize = 0;\r
325 *NumberHandles = 0;\r
326 *Buffer = NULL;\r
327 Status = gSmst->SmmLocateHandle (\r
328 ByProtocol,\r
329 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
330 NULL,\r
331 &BufferSize,\r
332 *Buffer\r
333 );\r
334 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
335 return EFI_NOT_FOUND;\r
336 }\r
337\r
338 *Buffer = AllocatePool (BufferSize);\r
339 if (*Buffer == NULL) {\r
340 return EFI_OUT_OF_RESOURCES;\r
341 }\r
342\r
343 Status = gSmst->SmmLocateHandle (\r
344 ByProtocol,\r
345 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
346 NULL,\r
347 &BufferSize,\r
348 *Buffer\r
349 );\r
350\r
351 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);\r
352 if (EFI_ERROR(Status)) {\r
353 *NumberHandles = 0;\r
5e5bb2a9
SZ
354 FreePool (*Buffer);\r
355 *Buffer = NULL;\r
8a2d4996 356 }\r
357\r
358 return Status;\r
359}\r
360\r
361\r
362/**\r
363 Get the variable statistics information from the information buffer pointed by gVariableInfo.\r
364\r
2445a70e 365 Caution: This function may be invoked at SMM runtime.\r
366 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.\r
367\r
368 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.\r
369 On input, point to the variable information returned last time. if \r
370 InfoEntry->VendorGuid is zero, return the first information.\r
371 On output, point to the next variable information.\r
372 @param[in, out] InfoSize On input, the size of the variable information buffer.\r
373 On output, the returned variable information size.\r
8a2d4996 374\r
375 @retval EFI_SUCCESS The variable information is found and returned successfully.\r
376 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The \r
377 PcdVariableCollectStatistics should be set TRUE to support it.\r
378 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.\r
379\r
380**/\r
381EFI_STATUS\r
382SmmVariableGetStatistics (\r
d00ed85e 383 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,\r
8a2d4996 384 IN OUT UINTN *InfoSize\r
385 )\r
386{\r
d00ed85e 387 VARIABLE_INFO_ENTRY *VariableInfo;\r
8a2d4996 388 UINTN NameLength;\r
389 UINTN StatisticsInfoSize;\r
390 CHAR16 *InfoName;\r
5e5bb2a9
SZ
391 EFI_GUID VendorGuid;\r
392\r
8a2d4996 393 ASSERT (InfoEntry != NULL);\r
394 VariableInfo = gVariableInfo; \r
395 if (VariableInfo == NULL) {\r
396 return EFI_UNSUPPORTED;\r
397 }\r
398\r
d00ed85e 399 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
eb96e4f2 400 if (*InfoSize < StatisticsInfoSize) {\r
8a2d4996 401 *InfoSize = StatisticsInfoSize;\r
402 return EFI_BUFFER_TOO_SMALL;\r
403 }\r
404 InfoName = (CHAR16 *)(InfoEntry + 1);\r
405\r
5e5bb2a9
SZ
406 CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);\r
407\r
408 if (CompareGuid (&VendorGuid, &mZeroGuid)) {\r
8a2d4996 409 //\r
410 // Return the first variable info\r
411 //\r
d00ed85e 412 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
8a2d4996 413 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
414 *InfoSize = StatisticsInfoSize;\r
415 return EFI_SUCCESS;\r
416 }\r
417\r
418 //\r
419 // Get the next variable info\r
420 //\r
421 while (VariableInfo != NULL) {\r
5e5bb2a9 422 if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {\r
8a2d4996 423 NameLength = StrSize (VariableInfo->Name);\r
424 if (NameLength == StrSize (InfoName)) {\r
425 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {\r
426 //\r
427 // Find the match one\r
428 //\r
429 VariableInfo = VariableInfo->Next;\r
430 break;\r
431 }\r
432 }\r
433 }\r
434 VariableInfo = VariableInfo->Next;\r
435 };\r
436 \r
437 if (VariableInfo == NULL) {\r
438 *InfoSize = 0;\r
439 return EFI_SUCCESS;\r
440 }\r
441\r
442 //\r
443 // Output the new variable info\r
444 //\r
d00ed85e 445 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
8a2d4996 446 if (*InfoSize < StatisticsInfoSize) {\r
447 *InfoSize = StatisticsInfoSize;\r
448 return EFI_BUFFER_TOO_SMALL;\r
449 }\r
450\r
d00ed85e 451 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
8a2d4996 452 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
453 *InfoSize = StatisticsInfoSize;\r
454 \r
455 return EFI_SUCCESS;\r
456}\r
457\r
458\r
459/**\r
460 Communication service SMI Handler entry.\r
461\r
462 This SMI handler provides services for the variable wrapper driver.\r
463\r
2445a70e 464 Caution: This function may receive untrusted input.\r
465 This variable data and communicate buffer are external input, so this function will do basic validation.\r
466 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
467 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
468 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
469\r
8a2d4996 470 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
471 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
472 handler was registered.\r
473 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
474 be conveyed from a non-SMM environment into an SMM environment.\r
475 @param[in, out] CommBufferSize The size of the CommBuffer.\r
476\r
477 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
478 should still be called.\r
479 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
480 still be called.\r
481 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
482 be called.\r
483 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
484**/\r
485EFI_STATUS\r
486EFIAPI\r
487SmmVariableHandler (\r
488 IN EFI_HANDLE DispatchHandle,\r
489 IN CONST VOID *RegisterContext,\r
490 IN OUT VOID *CommBuffer,\r
491 IN OUT UINTN *CommBufferSize\r
492 )\r
493{\r
494 EFI_STATUS Status;\r
495 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
496 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
497 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;\r
498 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;\r
d00ed85e 499 VARIABLE_INFO_ENTRY *VariableInfo;\r
ff843847 500 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r
8a2d4996 501 UINTN InfoSize;\r
9d00d20e 502 UINTN NameBufferSize;\r
5e5bb2a9 503 UINTN CommBufferPayloadSize;\r
8a2d4996 504\r
2445a70e 505 //\r
506 // If input is invalid, stop processing this SMI\r
507 //\r
508 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
509 return EFI_SUCCESS;\r
510 }\r
511\r
d294b9a4 512 if (*CommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
5e5bb2a9
SZ
513 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));\r
514 return EFI_SUCCESS;\r
515 }\r
516 CommBufferPayloadSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
517 if (CommBufferPayloadSize > mVariableBufferPayloadSize) {\r
518 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));\r
2445a70e 519 return EFI_SUCCESS;\r
520 }\r
521\r
9d00d20e 522 if (!InternalIsAddressValid ((UINTN)CommBuffer, *CommBufferSize)) {\r
5e5bb2a9 523 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
2445a70e 524 return EFI_SUCCESS;\r
525 }\r
8a2d4996 526\r
527 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
528 switch (SmmVariableFunctionHeader->Function) {\r
529 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
5e5bb2a9
SZ
530 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
531 DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));\r
532 return EFI_SUCCESS;\r
533 }\r
534 //\r
535 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
536 //\r
537 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
538 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
9d00d20e
SZ
539 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
540 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
541 //\r
542 // Prevent InfoSize overflow happen\r
543 //\r
544 Status = EFI_ACCESS_DENIED;\r
545 goto EXIT;\r
546 }\r
2445a70e 547 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) \r
548 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
549\r
550 //\r
551 // SMRAM range check already covered before\r
552 //\r
5e5bb2a9
SZ
553 if (InfoSize > CommBufferPayloadSize) {\r
554 DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));\r
2445a70e 555 Status = EFI_ACCESS_DENIED;\r
556 goto EXIT;\r
557 }\r
558\r
9d00d20e
SZ
559 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
560 //\r
561 // Make sure VariableName is A Null-terminated string.\r
562 //\r
563 Status = EFI_ACCESS_DENIED;\r
564 goto EXIT;\r
565 }\r
566\r
8a2d4996 567 Status = VariableServiceGetVariable (\r
568 SmmVariableHeader->Name,\r
569 &SmmVariableHeader->Guid,\r
570 &SmmVariableHeader->Attributes,\r
571 &SmmVariableHeader->DataSize,\r
572 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
573 );\r
5e5bb2a9 574 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
8a2d4996 575 break;\r
576 \r
577 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
5e5bb2a9
SZ
578 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
579 DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n"));\r
580 return EFI_SUCCESS;\r
581 }\r
582 //\r
583 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
584 //\r
585 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
586 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;\r
9d00d20e
SZ
587 if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
588 //\r
589 // Prevent InfoSize overflow happen\r
590 //\r
591 Status = EFI_ACCESS_DENIED;\r
592 goto EXIT;\r
593 }\r
2445a70e 594 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
595\r
596 //\r
597 // SMRAM range check already covered before\r
598 //\r
5e5bb2a9
SZ
599 if (InfoSize > CommBufferPayloadSize) {\r
600 DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));\r
2445a70e 601 Status = EFI_ACCESS_DENIED;\r
602 goto EXIT;\r
603 }\r
604\r
5e5bb2a9 605 NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
9d00d20e
SZ
606 if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {\r
607 //\r
608 // Make sure input VariableName is A Null-terminated string.\r
609 //\r
610 Status = EFI_ACCESS_DENIED;\r
611 goto EXIT;\r
612 }\r
613\r
8a2d4996 614 Status = VariableServiceGetNextVariableName (\r
615 &GetNextVariableName->NameSize,\r
616 GetNextVariableName->Name,\r
617 &GetNextVariableName->Guid\r
618 );\r
5e5bb2a9 619 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
8a2d4996 620 break;\r
621 \r
622 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
5e5bb2a9
SZ
623 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
624 DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));\r
625 return EFI_SUCCESS;\r
626 }\r
627 //\r
628 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
629 //\r
630 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
631 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
9d00d20e
SZ
632 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
633 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
634 //\r
635 // Prevent InfoSize overflow happen\r
636 //\r
637 Status = EFI_ACCESS_DENIED;\r
638 goto EXIT;\r
639 }\r
d17c4eac 640 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
641 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
642\r
643 //\r
644 // SMRAM range check already covered before\r
645 // Data buffer should not contain SMM range\r
646 //\r
5e5bb2a9
SZ
647 if (InfoSize > CommBufferPayloadSize) {\r
648 DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n"));\r
d17c4eac 649 Status = EFI_ACCESS_DENIED;\r
650 goto EXIT;\r
651 }\r
652\r
9d00d20e
SZ
653 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
654 //\r
655 // Make sure VariableName is A Null-terminated string.\r
656 //\r
657 Status = EFI_ACCESS_DENIED;\r
658 goto EXIT;\r
659 }\r
660\r
8a2d4996 661 Status = VariableServiceSetVariable (\r
662 SmmVariableHeader->Name,\r
663 &SmmVariableHeader->Guid,\r
664 SmmVariableHeader->Attributes,\r
665 SmmVariableHeader->DataSize,\r
666 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
667 );\r
668 break;\r
669 \r
670 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
5e5bb2a9
SZ
671 if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) {\r
672 DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n"));\r
673 return EFI_SUCCESS;\r
2445a70e 674 }\r
5e5bb2a9 675 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
2445a70e 676\r
8a2d4996 677 Status = VariableServiceQueryVariableInfo (\r
678 QueryVariableInfo->Attributes,\r
679 &QueryVariableInfo->MaximumVariableStorageSize,\r
680 &QueryVariableInfo->RemainingVariableStorageSize,\r
681 &QueryVariableInfo->MaximumVariableSize\r
682 );\r
683 break;\r
684\r
685 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
ff843847 686 mEndOfDxe = TRUE;\r
876ac395 687 if (AtRuntime()) {\r
688 Status = EFI_UNSUPPORTED;\r
689 break;\r
690 }\r
8a2d4996 691 ReclaimForOS ();\r
692 Status = EFI_SUCCESS;\r
693 break;\r
694 \r
695 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
696 mAtRuntime = TRUE;\r
697 Status = EFI_SUCCESS;\r
698 break;\r
699\r
700 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
d00ed85e 701 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
3f5c168f 702 InfoSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
2445a70e 703\r
704 //\r
705 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
706 // It is covered by previous CommBuffer check \r
707 //\r
708 \r
709 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
5e5bb2a9 710 DEBUG ((EFI_D_ERROR, "GetStatistics: SMM communication buffer in SMRAM!\n"));\r
2445a70e 711 Status = EFI_ACCESS_DENIED;\r
712 goto EXIT;\r
713 } \r
714\r
8a2d4996 715 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
3f5c168f 716 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
8a2d4996 717 break;\r
718\r
ff843847
RN
719 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE:\r
720 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {\r
721 DEBUG ((EFI_D_ERROR, "RequestToLock: SMM communication buffer size invalid!\n"));\r
722 return EFI_SUCCESS;\r
723 }\r
724 //\r
725 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
726 //\r
727 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
728 VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) mVariableBufferPayload;\r
729\r
730 if (VariableToLock->NameSize > MAX_ADDRESS - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {\r
731 //\r
732 // Prevent InfoSize overflow happen\r
733 //\r
734 Status = EFI_ACCESS_DENIED;\r
735 goto EXIT;\r
736 }\r
737\r
738 if (VariableToLock->NameSize < sizeof (CHAR16) || VariableToLock->Name[VariableToLock->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
739 //\r
740 // Make sure VariableName is A Null-terminated string.\r
741 //\r
742 Status = EFI_ACCESS_DENIED;\r
743 goto EXIT;\r
744 }\r
745 \r
746 InfoSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name) + VariableToLock->NameSize;\r
747 \r
748 //\r
749 // SMRAM range check already covered before\r
750 //\r
751 if (InfoSize > CommBufferPayloadSize) {\r
752 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
753 Status = EFI_ACCESS_DENIED;\r
754 goto EXIT;\r
755 }\r
756\r
757 Status = VariableLockRequestToLock (\r
758 NULL,\r
759 VariableToLock->Name,\r
760 &VariableToLock->Guid\r
761 );\r
762 break;\r
763\r
8a2d4996 764 default:\r
8a2d4996 765 Status = EFI_UNSUPPORTED;\r
766 }\r
767\r
2445a70e 768EXIT:\r
769\r
8a2d4996 770 SmmVariableFunctionHeader->ReturnStatus = Status;\r
771\r
772 return EFI_SUCCESS;\r
773}\r
774\r
ff843847
RN
775/**\r
776 SMM END_OF_DXE protocol notification event handler.\r
777\r
778 @param Protocol Points to the protocol's unique identifier\r
779 @param Interface Points to the interface instance\r
780 @param Handle The handle on which the interface was installed\r
781\r
782 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
783\r
784**/\r
785EFI_STATUS\r
786EFIAPI\r
787SmmEndOfDxeCallback (\r
788 IN CONST EFI_GUID *Protocol,\r
789 IN VOID *Interface,\r
790 IN EFI_HANDLE Handle\r
791 )\r
792{\r
793 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
794 mEndOfDxe = TRUE;\r
795 return EFI_SUCCESS;\r
796}\r
8a2d4996 797\r
798/**\r
799 SMM Fault Tolerant Write protocol notification event handler.\r
800\r
801 Non-Volatile variable write may needs FTW protocol to reclaim when \r
802 writting variable.\r
803 \r
804 @param Protocol Points to the protocol's unique identifier\r
805 @param Interface Points to the interface instance\r
806 @param Handle The handle on which the interface was installed\r
807\r
808 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
809 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
810 \r
811 **/\r
812EFI_STATUS\r
813EFIAPI\r
814SmmFtwNotificationEvent (\r
815 IN CONST EFI_GUID *Protocol,\r
816 IN VOID *Interface,\r
817 IN EFI_HANDLE Handle\r
818 )\r
819{\r
820 EFI_STATUS Status;\r
821 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
822 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
823 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
824 \r
825 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
826 return EFI_SUCCESS;\r
827 }\r
828\r
829 //\r
830 // Ensure SMM FTW protocol is installed.\r
831 //\r
5c7fa429 832 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
8a2d4996 833 if (EFI_ERROR (Status)) {\r
834 return Status;\r
835 }\r
836\r
837 //\r
838 // Find the proper FVB protocol for variable.\r
839 //\r
840 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
841 if (NvStorageVariableBase == 0) {\r
842 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
843 }\r
844 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
845 if (EFI_ERROR (Status)) {\r
846 return EFI_NOT_FOUND;\r
847 }\r
848\r
849 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
850 \r
851 Status = VariableWriteServiceInitialize ();\r
852 ASSERT_EFI_ERROR (Status);\r
853 \r
854 //\r
855 // Notify the variable wrapper driver the variable write service is ready\r
856 //\r
857 Status = gBS->InstallProtocolInterface (\r
858 &mSmmVariableHandle,\r
d00ed85e 859 &gSmmVariableWriteGuid,\r
8a2d4996 860 EFI_NATIVE_INTERFACE,\r
861 NULL\r
862 );\r
863 ASSERT_EFI_ERROR (Status);\r
864 \r
865 return EFI_SUCCESS;\r
866}\r
867\r
868\r
869/**\r
870 Variable Driver main entry point. The Variable driver places the 4 EFI\r
871 runtime services in the EFI System Table and installs arch protocols \r
d00ed85e 872 for variable read and write services being available. It also registers\r
8a2d4996 873 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
874\r
875 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
876 @param[in] SystemTable A pointer to the EFI System Table.\r
877 \r
878 @retval EFI_SUCCESS Variable service successfully initialized.\r
879\r
880**/\r
881EFI_STATUS\r
882EFIAPI\r
883VariableServiceInitialize (\r
884 IN EFI_HANDLE ImageHandle,\r
885 IN EFI_SYSTEM_TABLE *SystemTable\r
886 )\r
887{\r
888 EFI_STATUS Status;\r
889 EFI_HANDLE VariableHandle;\r
890 VOID *SmmFtwRegistration;\r
2445a70e 891 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
892 UINTN Size;\r
ff843847 893 VOID *SmmEndOfDxeRegistration;\r
2445a70e 894\r
8a2d4996 895 //\r
896 // Variable initialize.\r
897 //\r
898 Status = VariableCommonInitialize ();\r
899 ASSERT_EFI_ERROR (Status);\r
900\r
901 //\r
902 // Install the Smm Variable Protocol on a new handle.\r
903 //\r
904 VariableHandle = NULL;\r
905 Status = gSmst->SmmInstallProtocolInterface (\r
906 &VariableHandle,\r
907 &gEfiSmmVariableProtocolGuid,\r
908 EFI_NATIVE_INTERFACE,\r
909 &gSmmVariable\r
910 );\r
911 ASSERT_EFI_ERROR (Status);\r
912\r
2445a70e 913 //\r
914 // Get SMRAM information\r
915 //\r
916 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
917 ASSERT_EFI_ERROR (Status);\r
918\r
919 Size = 0;\r
920 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
921 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
922\r
923 Status = gSmst->SmmAllocatePool (\r
924 EfiRuntimeServicesData,\r
925 Size,\r
926 (VOID **)&mSmramRanges\r
927 );\r
928 ASSERT_EFI_ERROR (Status);\r
929\r
930 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
931 ASSERT_EFI_ERROR (Status);\r
932\r
933 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
934\r
5e5bb2a9
SZ
935 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +\r
936 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);\r
937\r
938 Status = gSmst->SmmAllocatePool (\r
939 EfiRuntimeServicesData,\r
940 mVariableBufferPayloadSize,\r
941 (VOID **)&mVariableBufferPayload\r
942 );\r
943 ASSERT_EFI_ERROR (Status);\r
944\r
8a2d4996 945 ///\r
946 /// Register SMM variable SMI handler\r
947 ///\r
948 VariableHandle = NULL;\r
949 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
950 ASSERT_EFI_ERROR (Status);\r
951 \r
952 //\r
953 // Notify the variable wrapper driver the variable service is ready\r
954 //\r
955 Status = SystemTable->BootServices->InstallProtocolInterface (\r
956 &mVariableHandle,\r
957 &gEfiSmmVariableProtocolGuid,\r
958 EFI_NATIVE_INTERFACE,\r
959 &gSmmVariable\r
960 );\r
961 ASSERT_EFI_ERROR (Status);\r
962 \r
ff843847
RN
963 //\r
964 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
965 //\r
966 Status = gSmst->SmmRegisterProtocolNotify (\r
967 &gEfiSmmEndOfDxeProtocolGuid,\r
968 SmmEndOfDxeCallback,\r
969 &SmmEndOfDxeRegistration\r
970 );\r
971 ASSERT_EFI_ERROR (Status);\r
972\r
8a2d4996 973 //\r
974 // Register FtwNotificationEvent () notify function.\r
975 // \r
976 Status = gSmst->SmmRegisterProtocolNotify (\r
977 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
978 SmmFtwNotificationEvent,\r
979 &SmmFtwRegistration\r
980 );\r
981 ASSERT_EFI_ERROR (Status);\r
982\r
983 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
984 \r
985 return EFI_SUCCESS;\r
986}\r
987\r
988\r