]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
MdeModulePkg: Add the alignment check for FTW spare area address and length, and...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableSmm.c
... / ...
CommitLineData
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
7 Caution: This module requires additional review when modified.\r
8 This driver will have external input - variable data and communicate buffer in SMM mode.\r
9 This external input must be validated carefully to avoid security issue like\r
10 buffer overflow, integer overflow.\r
11\r
12 SmmVariableHandler() will receive untrusted input and do basic validation.\r
13\r
14 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
15 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
16 SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
17\r
18Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r
19This program and the accompanying materials \r
20are licensed and made available under the terms and conditions of the BSD License \r
21which accompanies this distribution. The full text of the license may be found at \r
22http://opensource.org/licenses/bsd-license.php\r
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
28#include <Protocol/SmmVariable.h>\r
29#include <Protocol/SmmFirmwareVolumeBlock.h>\r
30#include <Protocol/SmmFaultTolerantWrite.h>\r
31#include <Protocol/SmmAccess2.h>\r
32#include <Protocol/SmmEndOfDxe.h>\r
33\r
34#include <Library/SmmServicesTableLib.h>\r
35\r
36#include <Guid/VariableFormat.h>\r
37#include <Guid/SmmVariableCommon.h>\r
38#include "Variable.h"\r
39\r
40EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r
41UINTN mSmramRangeCount;\r
42\r
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
48UINT8 *mVariableBufferPayload = NULL;\r
49UINTN mVariableBufferPayloadSize;\r
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
97\r
98EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {\r
99 VariableServiceGetVariable,\r
100 VariableServiceGetNextVariableName,\r
101 SmmVariableSetVariable,\r
102 VariableServiceQueryVariableInfo\r
103};\r
104\r
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
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
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
171\r
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
354 FreePool (*Buffer);\r
355 *Buffer = NULL;\r
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
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
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
383 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,\r
384 IN OUT UINTN *InfoSize\r
385 )\r
386{\r
387 VARIABLE_INFO_ENTRY *VariableInfo;\r
388 UINTN NameLength;\r
389 UINTN StatisticsInfoSize;\r
390 CHAR16 *InfoName;\r
391 EFI_GUID VendorGuid;\r
392\r
393 ASSERT (InfoEntry != NULL);\r
394 VariableInfo = gVariableInfo; \r
395 if (VariableInfo == NULL) {\r
396 return EFI_UNSUPPORTED;\r
397 }\r
398\r
399 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
400 if (*InfoSize < StatisticsInfoSize) {\r
401 *InfoSize = StatisticsInfoSize;\r
402 return EFI_BUFFER_TOO_SMALL;\r
403 }\r
404 InfoName = (CHAR16 *)(InfoEntry + 1);\r
405\r
406 CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);\r
407\r
408 if (CompareGuid (&VendorGuid, &mZeroGuid)) {\r
409 //\r
410 // Return the first variable info\r
411 //\r
412 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
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
422 if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {\r
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
445 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
446 if (*InfoSize < StatisticsInfoSize) {\r
447 *InfoSize = StatisticsInfoSize;\r
448 return EFI_BUFFER_TOO_SMALL;\r
449 }\r
450\r
451 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
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
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
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
499 VARIABLE_INFO_ENTRY *VariableInfo;\r
500 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r
501 UINTN InfoSize;\r
502 UINTN NameBufferSize;\r
503 UINTN CommBufferPayloadSize;\r
504 UINTN TempCommBufferSize;\r
505\r
506 //\r
507 // If input is invalid, stop processing this SMI\r
508 //\r
509 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
510 return EFI_SUCCESS;\r
511 }\r
512\r
513 TempCommBufferSize = *CommBufferSize;\r
514\r
515 if (TempCommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
516 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));\r
517 return EFI_SUCCESS;\r
518 }\r
519 CommBufferPayloadSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
520 if (CommBufferPayloadSize > mVariableBufferPayloadSize) {\r
521 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));\r
522 return EFI_SUCCESS;\r
523 }\r
524\r
525 if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
526 DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
527 return EFI_SUCCESS;\r
528 }\r
529\r
530 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
531 switch (SmmVariableFunctionHeader->Function) {\r
532 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
533 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
534 DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));\r
535 return EFI_SUCCESS;\r
536 }\r
537 //\r
538 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
539 //\r
540 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
541 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
542 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
543 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
544 //\r
545 // Prevent InfoSize overflow happen\r
546 //\r
547 Status = EFI_ACCESS_DENIED;\r
548 goto EXIT;\r
549 }\r
550 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) \r
551 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
552\r
553 //\r
554 // SMRAM range check already covered before\r
555 //\r
556 if (InfoSize > CommBufferPayloadSize) {\r
557 DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));\r
558 Status = EFI_ACCESS_DENIED;\r
559 goto EXIT;\r
560 }\r
561\r
562 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
563 //\r
564 // Make sure VariableName is A Null-terminated string.\r
565 //\r
566 Status = EFI_ACCESS_DENIED;\r
567 goto EXIT;\r
568 }\r
569\r
570 Status = VariableServiceGetVariable (\r
571 SmmVariableHeader->Name,\r
572 &SmmVariableHeader->Guid,\r
573 &SmmVariableHeader->Attributes,\r
574 &SmmVariableHeader->DataSize,\r
575 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
576 );\r
577 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
578 break;\r
579 \r
580 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
581 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
582 DEBUG ((EFI_D_ERROR, "GetNextVariableName: 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 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;\r
590 if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
591 //\r
592 // Prevent InfoSize overflow happen\r
593 //\r
594 Status = EFI_ACCESS_DENIED;\r
595 goto EXIT;\r
596 }\r
597 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
598\r
599 //\r
600 // SMRAM range check already covered before\r
601 //\r
602 if (InfoSize > CommBufferPayloadSize) {\r
603 DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));\r
604 Status = EFI_ACCESS_DENIED;\r
605 goto EXIT;\r
606 }\r
607\r
608 NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
609 if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {\r
610 //\r
611 // Make sure input VariableName is A Null-terminated string.\r
612 //\r
613 Status = EFI_ACCESS_DENIED;\r
614 goto EXIT;\r
615 }\r
616\r
617 Status = VariableServiceGetNextVariableName (\r
618 &GetNextVariableName->NameSize,\r
619 GetNextVariableName->Name,\r
620 &GetNextVariableName->Guid\r
621 );\r
622 CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
623 break;\r
624 \r
625 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
626 if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
627 DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));\r
628 return EFI_SUCCESS;\r
629 }\r
630 //\r
631 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
632 //\r
633 CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
634 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
635 if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
636 ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
637 //\r
638 // Prevent InfoSize overflow happen\r
639 //\r
640 Status = EFI_ACCESS_DENIED;\r
641 goto EXIT;\r
642 }\r
643 InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
644 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
645\r
646 //\r
647 // SMRAM range check already covered before\r
648 // Data buffer should not contain SMM range\r
649 //\r
650 if (InfoSize > CommBufferPayloadSize) {\r
651 DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n"));\r
652 Status = EFI_ACCESS_DENIED;\r
653 goto EXIT;\r
654 }\r
655\r
656 if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
657 //\r
658 // Make sure VariableName is A Null-terminated string.\r
659 //\r
660 Status = EFI_ACCESS_DENIED;\r
661 goto EXIT;\r
662 }\r
663\r
664 Status = VariableServiceSetVariable (\r
665 SmmVariableHeader->Name,\r
666 &SmmVariableHeader->Guid,\r
667 SmmVariableHeader->Attributes,\r
668 SmmVariableHeader->DataSize,\r
669 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
670 );\r
671 break;\r
672 \r
673 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
674 if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) {\r
675 DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n"));\r
676 return EFI_SUCCESS;\r
677 }\r
678 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
679\r
680 Status = VariableServiceQueryVariableInfo (\r
681 QueryVariableInfo->Attributes,\r
682 &QueryVariableInfo->MaximumVariableStorageSize,\r
683 &QueryVariableInfo->RemainingVariableStorageSize,\r
684 &QueryVariableInfo->MaximumVariableSize\r
685 );\r
686 break;\r
687\r
688 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
689 mEndOfDxe = TRUE;\r
690 if (AtRuntime()) {\r
691 Status = EFI_UNSUPPORTED;\r
692 break;\r
693 }\r
694 ReclaimForOS ();\r
695 Status = EFI_SUCCESS;\r
696 break;\r
697 \r
698 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
699 mAtRuntime = TRUE;\r
700 Status = EFI_SUCCESS;\r
701 break;\r
702\r
703 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
704 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
705 InfoSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
706\r
707 //\r
708 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
709 // It is covered by previous CommBuffer check \r
710 //\r
711 \r
712 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
713 DEBUG ((EFI_D_ERROR, "GetStatistics: SMM communication buffer in SMRAM!\n"));\r
714 Status = EFI_ACCESS_DENIED;\r
715 goto EXIT;\r
716 } \r
717\r
718 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
719 *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
720 break;\r
721\r
722 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE:\r
723 if (mEndOfDxe) {\r
724 Status = EFI_ACCESS_DENIED;\r
725 } else {\r
726 VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) SmmVariableFunctionHeader->Data;\r
727 Status = VariableLockRequestToLock (\r
728 NULL,\r
729 VariableToLock->Name,\r
730 &VariableToLock->Guid\r
731 );\r
732 }\r
733 break;\r
734\r
735 default:\r
736 Status = EFI_UNSUPPORTED;\r
737 }\r
738\r
739EXIT:\r
740\r
741 SmmVariableFunctionHeader->ReturnStatus = Status;\r
742\r
743 return EFI_SUCCESS;\r
744}\r
745\r
746/**\r
747 SMM END_OF_DXE protocol notification event handler.\r
748\r
749 @param Protocol Points to the protocol's unique identifier\r
750 @param Interface Points to the interface instance\r
751 @param Handle The handle on which the interface was installed\r
752\r
753 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
754\r
755**/\r
756EFI_STATUS\r
757EFIAPI\r
758SmmEndOfDxeCallback (\r
759 IN CONST EFI_GUID *Protocol,\r
760 IN VOID *Interface,\r
761 IN EFI_HANDLE Handle\r
762 )\r
763{\r
764 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
765 mEndOfDxe = TRUE;\r
766 return EFI_SUCCESS;\r
767}\r
768\r
769/**\r
770 SMM Fault Tolerant Write protocol notification event handler.\r
771\r
772 Non-Volatile variable write may needs FTW protocol to reclaim when \r
773 writting variable.\r
774 \r
775 @param Protocol Points to the protocol's unique identifier\r
776 @param Interface Points to the interface instance\r
777 @param Handle The handle on which the interface was installed\r
778\r
779 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
780 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
781 \r
782 **/\r
783EFI_STATUS\r
784EFIAPI\r
785SmmFtwNotificationEvent (\r
786 IN CONST EFI_GUID *Protocol,\r
787 IN VOID *Interface,\r
788 IN EFI_HANDLE Handle\r
789 )\r
790{\r
791 EFI_STATUS Status;\r
792 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
793 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
794 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
795 UINTN FtwMaxBlockSize;\r
796 \r
797 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
798 return EFI_SUCCESS;\r
799 }\r
800\r
801 //\r
802 // Ensure SMM FTW protocol is installed.\r
803 //\r
804 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
805 if (EFI_ERROR (Status)) {\r
806 return Status;\r
807 }\r
808\r
809 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
810 if (!EFI_ERROR (Status)) {\r
811 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
812 }\r
813\r
814 //\r
815 // Find the proper FVB protocol for variable.\r
816 //\r
817 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
818 if (NvStorageVariableBase == 0) {\r
819 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
820 }\r
821 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
822 if (EFI_ERROR (Status)) {\r
823 return EFI_NOT_FOUND;\r
824 }\r
825\r
826 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
827 \r
828 Status = VariableWriteServiceInitialize ();\r
829 ASSERT_EFI_ERROR (Status);\r
830 \r
831 //\r
832 // Notify the variable wrapper driver the variable write service is ready\r
833 //\r
834 Status = gBS->InstallProtocolInterface (\r
835 &mSmmVariableHandle,\r
836 &gSmmVariableWriteGuid,\r
837 EFI_NATIVE_INTERFACE,\r
838 NULL\r
839 );\r
840 ASSERT_EFI_ERROR (Status);\r
841 \r
842 return EFI_SUCCESS;\r
843}\r
844\r
845\r
846/**\r
847 Variable Driver main entry point. The Variable driver places the 4 EFI\r
848 runtime services in the EFI System Table and installs arch protocols \r
849 for variable read and write services being available. It also registers\r
850 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
851\r
852 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
853 @param[in] SystemTable A pointer to the EFI System Table.\r
854 \r
855 @retval EFI_SUCCESS Variable service successfully initialized.\r
856\r
857**/\r
858EFI_STATUS\r
859EFIAPI\r
860VariableServiceInitialize (\r
861 IN EFI_HANDLE ImageHandle,\r
862 IN EFI_SYSTEM_TABLE *SystemTable\r
863 )\r
864{\r
865 EFI_STATUS Status;\r
866 EFI_HANDLE VariableHandle;\r
867 VOID *SmmFtwRegistration;\r
868 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
869 UINTN Size;\r
870 VOID *SmmEndOfDxeRegistration;\r
871\r
872 //\r
873 // Variable initialize.\r
874 //\r
875 Status = VariableCommonInitialize ();\r
876 ASSERT_EFI_ERROR (Status);\r
877\r
878 //\r
879 // Install the Smm Variable Protocol on a new handle.\r
880 //\r
881 VariableHandle = NULL;\r
882 Status = gSmst->SmmInstallProtocolInterface (\r
883 &VariableHandle,\r
884 &gEfiSmmVariableProtocolGuid,\r
885 EFI_NATIVE_INTERFACE,\r
886 &gSmmVariable\r
887 );\r
888 ASSERT_EFI_ERROR (Status);\r
889\r
890 //\r
891 // Get SMRAM information\r
892 //\r
893 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
894 ASSERT_EFI_ERROR (Status);\r
895\r
896 Size = 0;\r
897 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
898 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
899\r
900 Status = gSmst->SmmAllocatePool (\r
901 EfiRuntimeServicesData,\r
902 Size,\r
903 (VOID **)&mSmramRanges\r
904 );\r
905 ASSERT_EFI_ERROR (Status);\r
906\r
907 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
908 ASSERT_EFI_ERROR (Status);\r
909\r
910 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
911\r
912 mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +\r
913 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);\r
914\r
915 Status = gSmst->SmmAllocatePool (\r
916 EfiRuntimeServicesData,\r
917 mVariableBufferPayloadSize,\r
918 (VOID **)&mVariableBufferPayload\r
919 );\r
920 ASSERT_EFI_ERROR (Status);\r
921\r
922 ///\r
923 /// Register SMM variable SMI handler\r
924 ///\r
925 VariableHandle = NULL;\r
926 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
927 ASSERT_EFI_ERROR (Status);\r
928 \r
929 //\r
930 // Notify the variable wrapper driver the variable service is ready\r
931 //\r
932 Status = SystemTable->BootServices->InstallProtocolInterface (\r
933 &mVariableHandle,\r
934 &gEfiSmmVariableProtocolGuid,\r
935 EFI_NATIVE_INTERFACE,\r
936 &gSmmVariable\r
937 );\r
938 ASSERT_EFI_ERROR (Status);\r
939 \r
940 //\r
941 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
942 //\r
943 Status = gSmst->SmmRegisterProtocolNotify (\r
944 &gEfiSmmEndOfDxeProtocolGuid,\r
945 SmmEndOfDxeCallback,\r
946 &SmmEndOfDxeRegistration\r
947 );\r
948 ASSERT_EFI_ERROR (Status);\r
949\r
950 //\r
951 // Register FtwNotificationEvent () notify function.\r
952 // \r
953 Status = gSmst->SmmRegisterProtocolNotify (\r
954 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
955 SmmFtwNotificationEvent,\r
956 &SmmFtwRegistration\r
957 );\r
958 ASSERT_EFI_ERROR (Status);\r
959\r
960 SmmFtwNotificationEvent (NULL, NULL, NULL);\r
961 \r
962 return EFI_SUCCESS;\r
963}\r
964\r
965\r