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