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