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