]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
Add the EDKII_VARIABLE_LOCK_PROTOCOL implementation in SecurityPkg variable drivers.
[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
5e5bb2a9 17Copyright (c) 2010 - 2013, 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
6ab9f441 32#include <Protocol/SmmEndOfDxe.h>\r
25a4e71a 33\r
0c18794e 34#include <Library/SmmServicesTableLib.h>\r
35\r
36#include <Guid/AuthenticatedVariableFormat.h>\r
37#include <Guid/SmmVariableCommon.h>\r
38#include "Variable.h"\r
39\r
25a4e71a 40EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r
41UINTN mSmramRangeCount;\r
42\r
0c18794e 43extern VARIABLE_INFO_ENTRY *gVariableInfo;\r
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
6ab9f441
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
0c18794e 98EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {\r
99 VariableServiceGetVariable,\r
100 VariableServiceGetNextVariableName,\r
6ab9f441 101 SmmVariableSetVariable,\r
0c18794e 102 VariableServiceQueryVariableInfo\r
103};\r
104\r
0c18794e 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
25a4e71a 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
25a4e71a 171\r
0c18794e 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
0c18794e 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
dc204d5a
JY
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
648f98d1 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
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 @retval EFI_INVALID_PARAMETER Input parameter is invalid.\r
0c18794e 380\r
381**/\r
382EFI_STATUS\r
383SmmVariableGetStatistics (\r
384 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,\r
385 IN OUT UINTN *InfoSize\r
386 )\r
387{\r
388 VARIABLE_INFO_ENTRY *VariableInfo;\r
389 UINTN NameLength;\r
390 UINTN StatisticsInfoSize;\r
391 CHAR16 *InfoName;\r
5e5bb2a9 392 EFI_GUID VendorGuid;\r
0c18794e 393 \r
648f98d1 394 if (InfoEntry == NULL) {\r
395 return EFI_INVALID_PARAMETER;\r
396 }\r
397 \r
0c18794e 398 VariableInfo = gVariableInfo; \r
399 if (VariableInfo == NULL) {\r
400 return EFI_UNSUPPORTED;\r
401 }\r
402\r
403 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
12373f2c 404 if (*InfoSize < StatisticsInfoSize) {\r
0c18794e 405 *InfoSize = StatisticsInfoSize;\r
406 return EFI_BUFFER_TOO_SMALL;\r
407 }\r
408 InfoName = (CHAR16 *)(InfoEntry + 1);\r
409\r
5e5bb2a9
SZ
410 CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);\r
411\r
412 if (CompareGuid (&VendorGuid, &mZeroGuid)) {\r
0c18794e 413 //\r
414 // Return the first variable info\r
415 //\r
416 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
417 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
418 *InfoSize = StatisticsInfoSize;\r
419 return EFI_SUCCESS;\r
420 }\r
421\r
422 //\r
423 // Get the next variable info\r
424 //\r
425 while (VariableInfo != NULL) {\r
5e5bb2a9 426 if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {\r
0c18794e 427 NameLength = StrSize (VariableInfo->Name);\r
428 if (NameLength == StrSize (InfoName)) {\r
429 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {\r
430 //\r
431 // Find the match one\r
432 //\r
433 VariableInfo = VariableInfo->Next;\r
434 break;\r
435 }\r
436 }\r
437 }\r
438 VariableInfo = VariableInfo->Next;\r
439 };\r
440 \r
441 if (VariableInfo == NULL) {\r
442 *InfoSize = 0;\r
443 return EFI_SUCCESS;\r
444 }\r
445\r
446 //\r
447 // Output the new variable info\r
448 //\r
449 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
450 if (*InfoSize < StatisticsInfoSize) {\r
451 *InfoSize = StatisticsInfoSize;\r
452 return EFI_BUFFER_TOO_SMALL;\r
453 }\r
454\r
455 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
456 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
457 *InfoSize = StatisticsInfoSize;\r
458 \r
459 return EFI_SUCCESS;\r
460}\r
461\r
462\r
463/**\r
464 Communication service SMI Handler entry.\r
465\r
466 This SMI handler provides services for the variable wrapper driver.\r
467\r
dc204d5a
JY
468 Caution: This function may receive untrusted input.\r
469 This variable data and communicate buffer are external input, so this function will do basic validation.\r
470 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
471 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
472 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
473\r
0c18794e 474 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
475 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
476 handler was registered.\r
477 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
478 be conveyed from a non-SMM environment into an SMM environment.\r
479 @param[in, out] CommBufferSize The size of the CommBuffer.\r
480\r
481 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
482 should still be called.\r
483 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
484 still be called.\r
485 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
486 be called.\r
487 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
648f98d1 488\r
0c18794e 489**/\r
490EFI_STATUS\r
491EFIAPI\r
492SmmVariableHandler (\r
493 IN EFI_HANDLE DispatchHandle,\r
494 IN CONST VOID *RegisterContext,\r
495 IN OUT VOID *CommBuffer,\r
496 IN OUT UINTN *CommBufferSize\r
497 )\r
498{\r
499 EFI_STATUS Status;\r
500 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
501 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
502 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;\r
503 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;\r
504 VARIABLE_INFO_ENTRY *VariableInfo;\r
6ab9f441 505 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r
0c18794e 506 UINTN InfoSize;\r
9d00d20e 507 UINTN NameBufferSize;\r
5e5bb2a9 508 UINTN CommBufferPayloadSize;\r
0c18794e 509\r
25a4e71a 510 //\r
511 // If input is invalid, stop processing this SMI\r
512 //\r
513 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
514 return EFI_SUCCESS;\r
648f98d1 515 }\r
0c18794e 516\r
ad84df72 517 if (*CommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
5e5bb2a9
SZ
518 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));\r
519 return EFI_SUCCESS;\r
520 }\r
521 CommBufferPayloadSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
522 if (CommBufferPayloadSize > mVariableBufferPayloadSize) {\r
523 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));\r
25a4e71a 524 return EFI_SUCCESS;\r
525 }\r
526\r
9d00d20e 527 if (!InternalIsAddressValid ((UINTN)CommBuffer, *CommBufferSize)) {\r
5e5bb2a9 528 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
25a4e71a 529 return EFI_SUCCESS;\r
530 }\r
531 \r
0c18794e 532 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
25a4e71a 533 \r
0c18794e 534 switch (SmmVariableFunctionHeader->Function) {\r
535 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
5e5bb2a9
SZ
536 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
537 DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));\r
538 return EFI_SUCCESS;\r
539 }\r
540 //\r
541 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
542 //\r
543 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
544 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
9d00d20e
SZ
545 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
546 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
547 //\r
548 // Prevent InfoSize overflow happen\r
549 //\r
550 Status = EFI_ACCESS_DENIED;\r
551 goto EXIT;\r
552 }\r
25a4e71a 553 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) \r
554 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
555\r
556 //\r
557 // SMRAM range check already covered before\r
558 //\r
5e5bb2a9
SZ
559 if (InfoSize > CommBufferPayloadSize) {\r
560 DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));\r
25a4e71a 561 Status = EFI_ACCESS_DENIED;\r
562 goto EXIT;\r
563 }\r
564\r
9d00d20e
SZ
565 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
566 //\r
567 // Make sure VariableName is A Null-terminated string.\r
568 //\r
569 Status = EFI_ACCESS_DENIED;\r
570 goto EXIT;\r
571 }\r
572\r
0c18794e 573 Status = VariableServiceGetVariable (\r
574 SmmVariableHeader->Name,\r
575 &SmmVariableHeader->Guid,\r
576 &SmmVariableHeader->Attributes,\r
577 &SmmVariableHeader->DataSize,\r
578 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
579 );\r
5e5bb2a9 580 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
0c18794e 581 break;\r
582 \r
583 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
5e5bb2a9
SZ
584 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
585 DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n"));\r
586 return EFI_SUCCESS;\r
587 }\r
588 //\r
589 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
590 //\r
591 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
592 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;\r
9d00d20e
SZ
593 if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
594 //\r
595 // Prevent InfoSize overflow happen\r
596 //\r
597 Status = EFI_ACCESS_DENIED;\r
598 goto EXIT;\r
599 }\r
25a4e71a 600 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
601\r
602 //\r
603 // SMRAM range check already covered before\r
604 //\r
5e5bb2a9
SZ
605 if (InfoSize > CommBufferPayloadSize) {\r
606 DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));\r
25a4e71a 607 Status = EFI_ACCESS_DENIED;\r
608 goto EXIT;\r
609 }\r
610\r
5e5bb2a9 611 NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
9d00d20e
SZ
612 if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {\r
613 //\r
614 // Make sure input VariableName is A Null-terminated string.\r
615 //\r
616 Status = EFI_ACCESS_DENIED;\r
617 goto EXIT;\r
618 }\r
619\r
0c18794e 620 Status = VariableServiceGetNextVariableName (\r
621 &GetNextVariableName->NameSize,\r
622 GetNextVariableName->Name,\r
623 &GetNextVariableName->Guid\r
624 );\r
5e5bb2a9 625 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
0c18794e 626 break;\r
627 \r
628 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
5e5bb2a9
SZ
629 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
630 DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));\r
631 return EFI_SUCCESS;\r
632 }\r
633 //\r
634 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
635 //\r
636 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
637 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
9d00d20e
SZ
638 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
639 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
640 //\r
641 // Prevent InfoSize overflow happen\r
642 //\r
643 Status = EFI_ACCESS_DENIED;\r
644 goto EXIT;\r
645 }\r
d17c4eac 646 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
647 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
648\r
649 //\r
650 // SMRAM range check already covered before\r
651 // Data buffer should not contain SMM range\r
652 //\r
5e5bb2a9
SZ
653 if (InfoSize > CommBufferPayloadSize) {\r
654 DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n"));\r
d17c4eac 655 Status = EFI_ACCESS_DENIED;\r
656 goto EXIT;\r
657 }\r
658\r
9d00d20e
SZ
659 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
660 //\r
661 // Make sure VariableName is A Null-terminated string.\r
662 //\r
663 Status = EFI_ACCESS_DENIED;\r
664 goto EXIT;\r
665 }\r
666\r
0c18794e 667 Status = VariableServiceSetVariable (\r
668 SmmVariableHeader->Name,\r
669 &SmmVariableHeader->Guid,\r
670 SmmVariableHeader->Attributes,\r
671 SmmVariableHeader->DataSize,\r
672 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
673 );\r
674 break;\r
675 \r
676 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
5e5bb2a9
SZ
677 if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) {\r
678 DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n"));\r
679 return EFI_SUCCESS;\r
25a4e71a 680 }\r
5e5bb2a9 681 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
25a4e71a 682 \r
0c18794e 683 Status = VariableServiceQueryVariableInfo (\r
684 QueryVariableInfo->Attributes,\r
685 &QueryVariableInfo->MaximumVariableStorageSize,\r
686 &QueryVariableInfo->RemainingVariableStorageSize,\r
687 &QueryVariableInfo->MaximumVariableSize\r
688 );\r
689 break;\r
690\r
691 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
6ab9f441 692 mEndOfDxe = TRUE;\r
876ac395 693 if (AtRuntime()) {\r
694 Status = EFI_UNSUPPORTED;\r
695 break;\r
696 }\r
0c18794e 697 ReclaimForOS ();\r
698 Status = EFI_SUCCESS;\r
699 break;\r
700 \r
701 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
702 mAtRuntime = TRUE;\r
703 Status = EFI_SUCCESS;\r
704 break;\r
705\r
706 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
707 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
3f5c168f 708 InfoSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
25a4e71a 709\r
710 //\r
711 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
712 // It is covered by previous CommBuffer check \r
713 //\r
714 \r
715 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
5e5bb2a9 716 DEBUG ((EFI_D_ERROR, "GetStatistics: SMM communication buffer in SMRAM!\n"));\r
25a4e71a 717 Status = EFI_ACCESS_DENIED;\r
718 goto EXIT;\r
719 } \r
720\r
0c18794e 721 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
3f5c168f 722 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
0c18794e 723 break;\r
724\r
6ab9f441
RN
725 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE:\r
726 if (mEndOfDxe) {\r
727 Status = EFI_ACCESS_DENIED;\r
728 } else {\r
729 VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) SmmVariableFunctionHeader->Data;\r
730 Status = VariableLockRequestToLock (\r
731 NULL,\r
732 VariableToLock->Name,\r
733 &VariableToLock->Guid\r
734 );\r
735 }\r
736 break;\r
737\r
0c18794e 738 default:\r
0c18794e 739 Status = EFI_UNSUPPORTED;\r
740 }\r
741\r
25a4e71a 742EXIT:\r
0c18794e 743\r
25a4e71a 744 SmmVariableFunctionHeader->ReturnStatus = Status;\r
0c18794e 745 return EFI_SUCCESS;\r
746}\r
747\r
6ab9f441
RN
748/**\r
749 SMM END_OF_DXE protocol notification event handler.\r
750\r
751 @param Protocol Points to the protocol's unique identifier\r
752 @param Interface Points to the interface instance\r
753 @param Handle The handle on which the interface was installed\r
754\r
755 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
756\r
757**/\r
758EFI_STATUS\r
759EFIAPI\r
760SmmEndOfDxeCallback (\r
761 IN CONST EFI_GUID *Protocol,\r
762 IN VOID *Interface,\r
763 IN EFI_HANDLE Handle\r
764 )\r
765{\r
766 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
767 mEndOfDxe = TRUE;\r
768 return EFI_SUCCESS;\r
769}\r
0c18794e 770\r
771/**\r
772 SMM Fault Tolerant Write protocol notification event handler.\r
773\r
774 Non-Volatile variable write may needs FTW protocol to reclaim when \r
775 writting variable.\r
776 \r
777 @param Protocol Points to the protocol's unique identifier\r
778 @param Interface Points to the interface instance\r
779 @param Handle The handle on which the interface was installed\r
780\r
781 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
782 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
783 \r
784 **/\r
785EFI_STATUS\r
786EFIAPI\r
787SmmFtwNotificationEvent (\r
788 IN CONST EFI_GUID *Protocol,\r
789 IN VOID *Interface,\r
790 IN EFI_HANDLE Handle\r
791 )\r
792{\r
793 EFI_STATUS Status;\r
794 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
795 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
796 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
797 \r
798 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
799 return EFI_SUCCESS;\r
800 }\r
801\r
802 //\r
803 // Ensure SMM FTW protocol is installed.\r
804 //\r
805 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
806 if (EFI_ERROR (Status)) {\r
807 return Status;\r
808 }\r
809\r
810 //\r
811 // Find the proper FVB protocol for variable.\r
812 //\r
813 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
814 if (NvStorageVariableBase == 0) {\r
815 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
816 }\r
817 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
818 if (EFI_ERROR (Status)) {\r
819 return EFI_NOT_FOUND;\r
820 }\r
821\r
822 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
823 \r
824 Status = VariableWriteServiceInitialize ();\r
825 ASSERT_EFI_ERROR (Status);\r
826 \r
827 //\r
828 // Notify the variable wrapper driver the variable write service is ready\r
829 //\r
830 Status = gBS->InstallProtocolInterface (\r
831 &mSmmVariableHandle,\r
832 &gSmmVariableWriteGuid,\r
833 EFI_NATIVE_INTERFACE,\r
834 NULL\r
835 );\r
836 ASSERT_EFI_ERROR (Status);\r
837 \r
838 return EFI_SUCCESS;\r
839}\r
840\r
841\r
842/**\r
843 Variable Driver main entry point. The Variable driver places the 4 EFI\r
844 runtime services in the EFI System Table and installs arch protocols \r
845 for variable read and write services being available. It also registers\r
846 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
847\r
848 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
849 @param[in] SystemTable A pointer to the EFI System Table.\r
850 \r
851 @retval EFI_SUCCESS Variable service successfully initialized.\r
852\r
853**/\r
854EFI_STATUS\r
855EFIAPI\r
856VariableServiceInitialize (\r
857 IN EFI_HANDLE ImageHandle,\r
858 IN EFI_SYSTEM_TABLE *SystemTable\r
859 )\r
860{\r
861 EFI_STATUS Status;\r
862 EFI_HANDLE VariableHandle;\r
863 VOID *SmmFtwRegistration;\r
25a4e71a 864 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
865 UINTN Size;\r
6ab9f441 866 VOID *SmmEndOfDxeRegistration;\r
25a4e71a 867\r
0c18794e 868 //\r
869 // Variable initialize.\r
870 //\r
871 Status = VariableCommonInitialize ();\r
872 ASSERT_EFI_ERROR (Status);\r
873\r
874 //\r
875 // Install the Smm Variable Protocol on a new handle.\r
876 //\r
877 VariableHandle = NULL;\r
878 Status = gSmst->SmmInstallProtocolInterface (\r
879 &VariableHandle,\r
880 &gEfiSmmVariableProtocolGuid,\r
881 EFI_NATIVE_INTERFACE,\r
882 &gSmmVariable\r
883 );\r
884 ASSERT_EFI_ERROR (Status);\r
885\r
25a4e71a 886 //\r
887 // Get SMRAM information\r
888 //\r
889 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
890 ASSERT_EFI_ERROR (Status);\r
891\r
892 Size = 0;\r
893 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
894 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
895\r
896 Status = gSmst->SmmAllocatePool (\r
897 EfiRuntimeServicesData,\r
898 Size,\r
899 (VOID **)&mSmramRanges\r
900 );\r
901 ASSERT_EFI_ERROR (Status);\r
902\r
903 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
904 ASSERT_EFI_ERROR (Status);\r
905\r
906 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
907\r
5e5bb2a9
SZ
908 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +\r
909 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);\r
910\r
911 Status = gSmst->SmmAllocatePool (\r
912 EfiRuntimeServicesData,\r
913 mVariableBufferPayloadSize,\r
914 (VOID **)&mVariableBufferPayload\r
915 );\r
916 ASSERT_EFI_ERROR (Status);\r
917\r
0c18794e 918 ///\r
919 /// Register SMM variable SMI handler\r
920 ///\r
921 VariableHandle = NULL;\r
922 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
923 ASSERT_EFI_ERROR (Status);\r
924 \r
925 //\r
926 // Notify the variable wrapper driver the variable service is ready\r
927 //\r
928 Status = SystemTable->BootServices->InstallProtocolInterface (\r
929 &mVariableHandle,\r
930 &gEfiSmmVariableProtocolGuid,\r
931 EFI_NATIVE_INTERFACE,\r
932 &gSmmVariable\r
933 );\r
934 ASSERT_EFI_ERROR (Status);\r
935 \r
6ab9f441
RN
936 //\r
937 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
938 //\r
939 Status = gSmst->SmmRegisterProtocolNotify (\r
940 &gEfiSmmEndOfDxeProtocolGuid,\r
941 SmmEndOfDxeCallback,\r
942 &SmmEndOfDxeRegistration\r
943 );\r
944 ASSERT_EFI_ERROR (Status);\r
945\r
0c18794e 946 //\r
947 // Register FtwNotificationEvent () notify function.\r
948 // \r
949 Status = gSmst->SmmRegisterProtocolNotify (\r
950 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
951 SmmFtwNotificationEvent,\r
952 &SmmFtwRegistration\r
953 );\r
954 ASSERT_EFI_ERROR (Status);\r
955\r
956 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
957 \r
958 return EFI_SUCCESS;\r
959}\r
960\r
961\r