]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c
Update secure boot UI driver to handle “reset to default” hot key.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmmRuntimeDxe.c
CommitLineData
0c18794e 1/** @file\r
2 Implement all four UEFI Runtime Variable services for the nonvolatile\r
3 and volatile storage space and install variable architecture protocol\r
4 based on SMM variable module.\r
5\r
dc204d5a
JY
6 Caution: This module requires additional review when modified.\r
7 This driver will have external input - variable data.\r
8 This external input must be validated carefully to avoid security issue like\r
9 buffer overflow, integer overflow.\r
10\r
11 RuntimeServiceGetVariable() and RuntimeServiceSetVariable() are external API\r
12 to receive data buffer. The size should be checked carefully.\r
13\r
14 InitCommunicateBuffer() is really function to check the variable data size.\r
15\r
aab9212f 16Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r
0c18794e 17This program and the accompanying materials \r
18are licensed and made available under the terms and conditions of the BSD License \r
19which accompanies this distribution. The full text of the license may be found at \r
20http://opensource.org/licenses/bsd-license.php\r
21\r
22THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
23WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
24\r
25**/\r
26\r
27#include <PiDxe.h>\r
28#include <Protocol/VariableWrite.h>\r
29#include <Protocol/Variable.h>\r
30#include <Protocol/SmmCommunication.h>\r
31#include <Protocol/SmmVariable.h>\r
32\r
33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/UefiRuntimeServicesTableLib.h>\r
35#include <Library/MemoryAllocationLib.h>\r
36#include <Library/UefiDriverEntryPoint.h>\r
37#include <Library/UefiRuntimeLib.h>\r
38#include <Library/BaseMemoryLib.h>\r
39#include <Library/DebugLib.h>\r
40#include <Library/PcdLib.h>\r
41#include <Library/UefiLib.h>\r
42#include <Library/BaseLib.h>\r
43\r
44#include <Guid/EventGroup.h>\r
45#include <Guid/AuthenticatedVariableFormat.h>\r
46#include <Guid/SmmVariableCommon.h>\r
47\r
48EFI_HANDLE mHandle = NULL; \r
49EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;\r
50EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
51EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
52UINT8 *mVariableBuffer = NULL;\r
53UINT8 *mVariableBufferPhysical = NULL;\r
54UINTN mVariableBufferSize;\r
6ed1ec59 55EFI_LOCK mVariableServicesLock;\r
0c18794e 56\r
6ed1ec59
SZ
57/**\r
58 Acquires lock only at boot time. Simply returns at runtime.\r
59\r
60 This is a temperary function that will be removed when\r
61 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
62 Runtimer driver in RT phase.\r
63 It calls EfiAcquireLock() at boot time, and simply returns\r
64 at runtime.\r
65\r
66 @param Lock A pointer to the lock to acquire.\r
67\r
68**/\r
69VOID\r
70AcquireLockOnlyAtBootTime (\r
71 IN EFI_LOCK *Lock\r
72 )\r
73{\r
74 if (!EfiAtRuntime ()) {\r
75 EfiAcquireLock (Lock);\r
76 }\r
77}\r
78\r
79/**\r
80 Releases lock only at boot time. Simply returns at runtime.\r
81\r
82 This is a temperary function which will be removed when\r
83 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
84 Runtimer driver in RT phase.\r
85 It calls EfiReleaseLock() at boot time and simply returns\r
86 at runtime.\r
87\r
88 @param Lock A pointer to the lock to release.\r
89\r
90**/\r
91VOID\r
92ReleaseLockOnlyAtBootTime (\r
93 IN EFI_LOCK *Lock\r
94 )\r
95{\r
96 if (!EfiAtRuntime ()) {\r
97 EfiReleaseLock (Lock);\r
98 }\r
99}\r
0c18794e 100\r
101/**\r
102 Initialize the communicate buffer using DataSize and Function.\r
103\r
104 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +\r
105 DataSize.\r
106\r
dc204d5a
JY
107 Caution: This function may receive untrusted input.\r
108 The data size external input, so this function will validate it carefully to avoid buffer overflow.\r
109\r
0c18794e 110 @param[out] DataPtr Points to the data in the communicate buffer.\r
111 @param[in] DataSize The data size to send to SMM.\r
112 @param[in] Function The function number to initialize the communicate header.\r
113 \r
114 @retval EFI_INVALID_PARAMETER The data size is too big.\r
115 @retval EFI_SUCCESS Find the specified variable.\r
116\r
117**/\r
118EFI_STATUS\r
119InitCommunicateBuffer (\r
120 OUT VOID **DataPtr OPTIONAL,\r
121 IN UINTN DataSize,\r
122 IN UINTN Function\r
123 )\r
124{\r
125 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; \r
126 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader; \r
127\r
128 \r
129 if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {\r
130 return EFI_INVALID_PARAMETER;\r
131 }\r
132\r
133 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
134 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
135 SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
136 \r
137 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
138 SmmVariableFunctionHeader->Function = Function;\r
139 if (DataPtr != NULL) {\r
140 *DataPtr = SmmVariableFunctionHeader->Data;\r
141 }\r
142\r
143 return EFI_SUCCESS;\r
144}\r
145\r
146\r
147/**\r
148 Send the data in communicate buffer to SMM.\r
149\r
150 @param[in] DataSize This size of the function header and the data.\r
151\r
152 @retval EFI_SUCCESS Success is returned from the functin in SMM.\r
153 @retval Others Failure is returned from the function in SMM. \r
154 \r
155**/\r
156EFI_STATUS\r
157SendCommunicateBuffer (\r
158 IN UINTN DataSize\r
159 )\r
160{\r
161 EFI_STATUS Status;\r
162 UINTN CommSize;\r
163 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; \r
164 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r
165 \r
166 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
167 Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);\r
168 ASSERT_EFI_ERROR (Status);\r
169\r
170 SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
171 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;\r
172 return SmmVariableFunctionHeader->ReturnStatus;\r
173}\r
174\r
175\r
176/**\r
177 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
178\r
dc204d5a
JY
179 Caution: This function may receive untrusted input.\r
180 The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
181\r
0c18794e 182 @param[in] VariableName Name of Variable to be found.\r
183 @param[in] VendorGuid Variable vendor GUID.\r
184 @param[out] Attributes Attribute value of the variable found.\r
185 @param[in, out] DataSize Size of Data found. If size is less than the\r
186 data, this value contains the required size.\r
187 @param[out] Data Data pointer.\r
188 \r
189 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
190 @retval EFI_SUCCESS Find the specified variable.\r
191 @retval EFI_NOT_FOUND Not found.\r
192 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
193\r
194**/\r
195EFI_STATUS\r
196EFIAPI\r
197RuntimeServiceGetVariable (\r
198 IN CHAR16 *VariableName,\r
199 IN EFI_GUID *VendorGuid,\r
200 OUT UINT32 *Attributes OPTIONAL,\r
201 IN OUT UINTN *DataSize,\r
202 OUT VOID *Data\r
203 )\r
204{\r
205 EFI_STATUS Status;\r
206 UINTN PayloadSize;\r
207 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
208\r
209 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
210 return EFI_INVALID_PARAMETER;\r
211 }\r
212\r
213 if ((*DataSize != 0) && (Data == NULL)) {\r
214 return EFI_INVALID_PARAMETER;\r
215 }\r
6ed1ec59 216\r
3588bb35
SZ
217 if (*DataSize >= mVariableBufferSize) {\r
218 //\r
219 // DataSize may be near MAX_ADDRESS incorrectly, this can cause the computed PayLoadSize to\r
220 // overflow to a small value and pass the check in InitCommunicateBuffer().\r
221 // To protect against this vulnerability, return EFI_INVALID_PARAMETER if DataSize is >= mVariableBufferSize.\r
222 // And there will be further check to ensure the total size is also not > mVariableBufferSize.\r
223 //\r
224 return EFI_INVALID_PARAMETER;\r
225 }\r
226\r
6ed1ec59
SZ
227 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
228\r
0c18794e 229 //\r
230 // Init the communicate buffer. The buffer data size is:\r
6ed1ec59 231 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
0c18794e 232 //\r
96277f74 233 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + *DataSize;\r
0c18794e 234 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);\r
235 if (EFI_ERROR (Status)) {\r
6ed1ec59 236 goto Done;\r
0c18794e 237 }\r
238 ASSERT (SmmVariableHeader != NULL);\r
239\r
240 CopyGuid (&SmmVariableHeader->Guid, VendorGuid);\r
241 SmmVariableHeader->DataSize = *DataSize;\r
242 SmmVariableHeader->NameSize = StrSize (VariableName);\r
243 if (Attributes == NULL) {\r
244 SmmVariableHeader->Attributes = 0;\r
245 } else {\r
246 SmmVariableHeader->Attributes = *Attributes;\r
247 }\r
248 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
249\r
250 //\r
251 // Send data to SMM.\r
252 //\r
253 Status = SendCommunicateBuffer (PayloadSize);\r
254\r
255 //\r
256 // Get data from SMM.\r
257 //\r
258 *DataSize = SmmVariableHeader->DataSize;\r
259 if (Attributes != NULL) {\r
260 *Attributes = SmmVariableHeader->Attributes;\r
261 }\r
262\r
263 if (EFI_ERROR (Status)) {\r
6ed1ec59 264 goto Done;\r
0c18794e 265 }\r
266\r
267 CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);\r
268\r
6ed1ec59
SZ
269Done:\r
270 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
0c18794e 271 return Status;\r
272}\r
273\r
274\r
275/**\r
276 This code Finds the Next available variable.\r
277\r
278 @param[in, out] VariableNameSize Size of the variable name.\r
279 @param[in, out] VariableName Pointer to variable name.\r
280 @param[in, out] VendorGuid Variable Vendor Guid.\r
281\r
282 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
283 @retval EFI_SUCCESS Find the specified variable.\r
284 @retval EFI_NOT_FOUND Not found.\r
285 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
286\r
287**/\r
288EFI_STATUS\r
289EFIAPI\r
290RuntimeServiceGetNextVariableName (\r
291 IN OUT UINTN *VariableNameSize,\r
292 IN OUT CHAR16 *VariableName,\r
293 IN OUT EFI_GUID *VendorGuid\r
294 )\r
295{\r
296 EFI_STATUS Status;\r
297 UINTN PayloadSize;\r
298 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;\r
299\r
300 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
301 return EFI_INVALID_PARAMETER;\r
302 }\r
6ed1ec59 303\r
3588bb35
SZ
304 if (*VariableNameSize >= mVariableBufferSize) {\r
305 //\r
306 // VariableNameSize may be near MAX_ADDRESS incorrectly, this can cause the computed PayLoadSize to\r
307 // overflow to a small value and pass the check in InitCommunicateBuffer().\r
308 // To protect against this vulnerability, return EFI_INVALID_PARAMETER if VariableNameSize is >= mVariableBufferSize.\r
309 // And there will be further check to ensure the total size is also not > mVariableBufferSize.\r
310 //\r
311 return EFI_INVALID_PARAMETER;\r
312 }\r
313\r
6ed1ec59
SZ
314 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
315\r
0c18794e 316 //\r
317 // Init the communicate buffer. The buffer data size is:\r
318 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
319 //\r
320 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + *VariableNameSize; \r
321 Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);\r
322 if (EFI_ERROR (Status)) {\r
6ed1ec59 323 goto Done;\r
0c18794e 324 }\r
325 ASSERT (SmmGetNextVariableName != NULL);\r
326\r
327 SmmGetNextVariableName->NameSize = *VariableNameSize;\r
328 CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);\r
329 CopyMem (SmmGetNextVariableName->Name, VariableName, *VariableNameSize);\r
330\r
331 //\r
332 // Send data to SMM\r
333 //\r
334 Status = SendCommunicateBuffer (PayloadSize);\r
335\r
336 //\r
337 // Get data from SMM.\r
338 //\r
339 *VariableNameSize = SmmGetNextVariableName->NameSize; \r
340 if (EFI_ERROR (Status)) {\r
6ed1ec59 341 goto Done;\r
0c18794e 342 }\r
343 \r
344 CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);\r
345 CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize); \r
346\r
6ed1ec59
SZ
347Done:\r
348 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
0c18794e 349 return Status;\r
350}\r
351\r
352/**\r
353 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
354\r
dc204d5a
JY
355 Caution: This function may receive untrusted input.\r
356 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.\r
357\r
0c18794e 358 @param[in] VariableName Name of Variable to be found.\r
359 @param[in] VendorGuid Variable vendor GUID.\r
360 @param[in] Attributes Attribute value of the variable found\r
361 @param[in] DataSize Size of Data found. If size is less than the\r
362 data, this value contains the required size.\r
363 @param[in] Data Data pointer.\r
364\r
365 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
366 @retval EFI_SUCCESS Set successfully.\r
367 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
368 @retval EFI_NOT_FOUND Not found.\r
369 @retval EFI_WRITE_PROTECTED Variable is read-only.\r
370\r
371**/\r
372EFI_STATUS\r
373EFIAPI\r
374RuntimeServiceSetVariable (\r
375 IN CHAR16 *VariableName,\r
376 IN EFI_GUID *VendorGuid,\r
377 IN UINT32 Attributes,\r
378 IN UINTN DataSize,\r
379 IN VOID *Data\r
380 )\r
381{\r
382 EFI_STATUS Status;\r
383 UINTN PayloadSize; \r
384 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r
385 \r
386 //\r
387 // Check input parameters.\r
388 //\r
389 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
390 return EFI_INVALID_PARAMETER;\r
391 } \r
392\r
393 if (DataSize != 0 && Data == NULL) {\r
394 return EFI_INVALID_PARAMETER;\r
395 }\r
6ed1ec59 396\r
3588bb35
SZ
397 if (DataSize >= mVariableBufferSize) {\r
398 //\r
399 // DataSize may be near MAX_ADDRESS incorrectly, this can cause the computed PayLoadSize to\r
400 // overflow to a small value and pass the check in InitCommunicateBuffer().\r
401 // To protect against this vulnerability, return EFI_INVALID_PARAMETER if DataSize is >= mVariableBufferSize.\r
402 // And there will be further check to ensure the total size is also not > mVariableBufferSize.\r
403 //\r
404 return EFI_INVALID_PARAMETER;\r
405 }\r
406\r
6ed1ec59
SZ
407 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
408 \r
0c18794e 409 //\r
410 // Init the communicate buffer. The buffer data size is:\r
411 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
412 //\r
413 PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + DataSize;\r
414 Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);\r
415 if (EFI_ERROR (Status)) {\r
6ed1ec59 416 goto Done;\r
0c18794e 417 }\r
418 ASSERT (SmmVariableHeader != NULL);\r
419\r
420 CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);\r
421 SmmVariableHeader->DataSize = DataSize;\r
422 SmmVariableHeader->NameSize = StrSize (VariableName);\r
423 SmmVariableHeader->Attributes = Attributes;\r
424 CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
425 CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);\r
426\r
427 //\r
428 // Send data to SMM.\r
429 //\r
430 Status = SendCommunicateBuffer (PayloadSize);\r
6ed1ec59
SZ
431\r
432Done:\r
433 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
0c18794e 434 return Status;\r
435}\r
436\r
437\r
438/**\r
439 This code returns information about the EFI variables.\r
440\r
441 @param[in] Attributes Attributes bitmask to specify the type of variables\r
442 on which to return information.\r
443 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
444 for the EFI variables associated with the attributes specified.\r
445 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
446 for EFI variables associated with the attributes specified.\r
447 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
448 associated with the attributes specified.\r
449\r
450 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
451 @retval EFI_SUCCESS Query successfully.\r
452 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.\r
453\r
454**/\r
455EFI_STATUS\r
456EFIAPI\r
457RuntimeServiceQueryVariableInfo (\r
458 IN UINT32 Attributes,\r
459 OUT UINT64 *MaximumVariableStorageSize,\r
460 OUT UINT64 *RemainingVariableStorageSize,\r
461 OUT UINT64 *MaximumVariableSize\r
462 )\r
463{\r
464 EFI_STATUS Status;\r
465 UINTN PayloadSize;\r
466 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;\r
467\r
468 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
469 return EFI_INVALID_PARAMETER;\r
470 }\r
6ed1ec59
SZ
471\r
472 AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
473\r
0c18794e 474 //\r
475 // Init the communicate buffer. The buffer data size is:\r
476 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;\r
477 //\r
478 PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
479 Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);\r
480 if (EFI_ERROR (Status)) {\r
6ed1ec59 481 goto Done;\r
0c18794e 482 }\r
483 ASSERT (SmmQueryVariableInfo != NULL);\r
484\r
485 SmmQueryVariableInfo->Attributes = Attributes;\r
486\r
487 //\r
488 // Send data to SMM.\r
489 //\r
490 Status = SendCommunicateBuffer (PayloadSize);\r
491 if (EFI_ERROR (Status)) {\r
6ed1ec59 492 goto Done;\r
0c18794e 493 }\r
494\r
495 //\r
496 // Get data from SMM.\r
497 //\r
498 *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;\r
499 *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;\r
500 *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize; \r
6ed1ec59
SZ
501\r
502Done:\r
503 ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
aab9212f 504 return Status;\r
0c18794e 505}\r
506\r
507\r
508/**\r
509 Exit Boot Services Event notification handler.\r
510\r
511 Notify SMM variable driver about the event.\r
512\r
513 @param[in] Event Event whose notification function is being invoked.\r
514 @param[in] Context Pointer to the notification function's context.\r
515\r
516**/\r
517VOID\r
518EFIAPI\r
519OnExitBootServices (\r
520 IN EFI_EVENT Event,\r
521 IN VOID *Context\r
522 )\r
523{\r
524 //\r
525 // Init the communicate buffer. The buffer data size is:\r
526 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
527 //\r
528 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE); \r
529\r
530 //\r
531 // Send data to SMM.\r
532 //\r
533 SendCommunicateBuffer (0);\r
534}\r
535\r
536\r
537/**\r
538 On Ready To Boot Services Event notification handler.\r
539\r
540 Notify SMM variable driver about the event.\r
541\r
542 @param[in] Event Event whose notification function is being invoked\r
543 @param[in] Context Pointer to the notification function's context\r
544\r
545**/\r
546VOID\r
547EFIAPI\r
548OnReadyToBoot (\r
549 IN EFI_EVENT Event,\r
550 IN VOID *Context\r
551 )\r
552{\r
553 //\r
554 // Init the communicate buffer. The buffer data size is:\r
555 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
556 //\r
557 InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);\r
558 \r
559 //\r
560 // Send data to SMM.\r
561 //\r
562 SendCommunicateBuffer (0);\r
563}\r
564\r
565\r
566/**\r
567 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
568\r
569 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
570 It convers pointer to new virtual address.\r
571\r
572 @param[in] Event Event whose notification function is being invoked.\r
573 @param[in] Context Pointer to the notification function's context.\r
574\r
575**/\r
576VOID\r
577EFIAPI\r
578VariableAddressChangeEvent (\r
579 IN EFI_EVENT Event,\r
580 IN VOID *Context\r
581 )\r
582{\r
583 EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);\r
584 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
585}\r
586\r
587\r
588/**\r
589 Initialize variable service and install Variable Architectural protocol.\r
590\r
591 @param[in] Event Event whose notification function is being invoked.\r
592 @param[in] Context Pointer to the notification function's context.\r
593 \r
594**/\r
595VOID\r
596EFIAPI\r
597SmmVariableReady (\r
598 IN EFI_EVENT Event,\r
599 IN VOID *Context\r
600 )\r
601{\r
602 EFI_STATUS Status;\r
603\r
604 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);\r
605 if (EFI_ERROR (Status)) {\r
606 return;\r
607 }\r
608 \r
609 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
610 ASSERT_EFI_ERROR (Status);\r
611 \r
612 //\r
613 // Allocate memory for variable store.\r
614 //\r
615 mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
616 mVariableBufferSize += MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
617 mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);\r
618 ASSERT (mVariableBuffer != NULL);\r
619\r
620 //\r
621 // Save the buffer physical address used for SMM conmunication.\r
622 //\r
623 mVariableBufferPhysical = mVariableBuffer;\r
624\r
625 gRT->GetVariable = RuntimeServiceGetVariable;\r
626 gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
627 gRT->SetVariable = RuntimeServiceSetVariable;\r
628 gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
629 \r
630 //\r
631 // Install the Variable Architectural Protocol on a new handle.\r
632 //\r
633 Status = gBS->InstallProtocolInterface (\r
634 &mHandle,\r
635 &gEfiVariableArchProtocolGuid, \r
636 EFI_NATIVE_INTERFACE,\r
637 NULL\r
638 );\r
639 ASSERT_EFI_ERROR (Status);\r
640}\r
641\r
642\r
643/**\r
644 SMM Non-Volatile variable write service is ready notify event handler.\r
645\r
646 @param[in] Event Event whose notification function is being invoked.\r
647 @param[in] Context Pointer to the notification function's context.\r
648 \r
649**/\r
650VOID\r
651EFIAPI\r
652SmmVariableWriteReady (\r
653 IN EFI_EVENT Event,\r
654 IN VOID *Context\r
655 )\r
656{\r
657 EFI_STATUS Status;\r
658 VOID *ProtocolOps;\r
659\r
660 //\r
661 // Check whether the protocol is installed or not.\r
662 //\r
663 Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);\r
664 if (EFI_ERROR (Status)) {\r
665 return;\r
666 }\r
667 \r
668 Status = gBS->InstallProtocolInterface (\r
669 &mHandle,\r
670 &gEfiVariableWriteArchProtocolGuid, \r
671 EFI_NATIVE_INTERFACE,\r
672 NULL\r
673 );\r
674 ASSERT_EFI_ERROR (Status); \r
675}\r
676\r
677\r
678/**\r
679 Variable Driver main entry point. The Variable driver places the 4 EFI\r
680 runtime services in the EFI System Table and installs arch protocols \r
681 for variable read and write services being available. It also registers\r
682 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
683\r
684 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
685 @param[in] SystemTable A pointer to the EFI System Table.\r
686 \r
687 @retval EFI_SUCCESS Variable service successfully initialized.\r
688\r
689**/\r
690EFI_STATUS\r
691EFIAPI\r
692VariableSmmRuntimeInitialize (\r
693 IN EFI_HANDLE ImageHandle,\r
694 IN EFI_SYSTEM_TABLE *SystemTable\r
695 )\r
696{\r
697 VOID *SmmVariableRegistration;\r
698 VOID *SmmVariableWriteRegistration;\r
699 EFI_EVENT OnReadyToBootEvent;\r
700 EFI_EVENT ExitBootServiceEvent;\r
6ed1ec59
SZ
701\r
702 EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);\r
703\r
0c18794e 704 //\r
705 // Smm variable service is ready\r
706 //\r
707 EfiCreateProtocolNotifyEvent (\r
708 &gEfiSmmVariableProtocolGuid, \r
709 TPL_CALLBACK, \r
710 SmmVariableReady, \r
711 NULL, \r
712 &SmmVariableRegistration\r
713 );\r
714\r
715 //\r
716 // Smm Non-Volatile variable write service is ready\r
717 //\r
718 EfiCreateProtocolNotifyEvent (\r
719 &gSmmVariableWriteGuid, \r
720 TPL_CALLBACK, \r
721 SmmVariableWriteReady, \r
722 NULL, \r
723 &SmmVariableWriteRegistration\r
724 );\r
725\r
726 //\r
727 // Register the event to reclaim variable for OS usage.\r
728 //\r
729 EfiCreateEventReadyToBootEx (\r
730 TPL_NOTIFY, \r
731 OnReadyToBoot, \r
732 NULL, \r
733 &OnReadyToBootEvent\r
734 ); \r
735\r
736 //\r
737 // Register the event to inform SMM variable that it is at runtime.\r
738 //\r
739 gBS->CreateEventEx (\r
740 EVT_NOTIFY_SIGNAL,\r
741 TPL_NOTIFY,\r
742 OnExitBootServices,\r
743 NULL,\r
744 &gEfiEventExitBootServicesGuid,\r
745 &ExitBootServiceEvent\r
746 ); \r
747\r
748 //\r
749 // Register the event to convert the pointer for runtime.\r
750 //\r
751 gBS->CreateEventEx (\r
752 EVT_NOTIFY_SIGNAL,\r
753 TPL_NOTIFY,\r
754 VariableAddressChangeEvent,\r
755 NULL,\r
756 &gEfiEventVirtualAddressChangeGuid,\r
757 &mVirtualAddressChangeEvent\r
758 );\r
759 \r
760 return EFI_SUCCESS;\r
761}\r
762\r