]>
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 | |
17409b7a | 16 | Copyright (c) 2010 - 2015, 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 | |
6ab9f441 | 32 | #include <Protocol/VariableLock.h>\r |
17409b7a | 33 | #include <Protocol/VarCheck.h>\r |
0c18794e | 34 | \r |
35 | #include <Library/UefiBootServicesTableLib.h>\r | |
36 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
37 | #include <Library/MemoryAllocationLib.h>\r | |
38 | #include <Library/UefiDriverEntryPoint.h>\r | |
39 | #include <Library/UefiRuntimeLib.h>\r | |
40 | #include <Library/BaseMemoryLib.h>\r | |
41 | #include <Library/DebugLib.h>\r | |
42 | #include <Library/PcdLib.h>\r | |
43 | #include <Library/UefiLib.h>\r | |
44 | #include <Library/BaseLib.h>\r | |
45 | \r | |
46 | #include <Guid/EventGroup.h>\r | |
47 | #include <Guid/AuthenticatedVariableFormat.h>\r | |
48 | #include <Guid/SmmVariableCommon.h>\r | |
49 | \r | |
50 | EFI_HANDLE mHandle = NULL; \r | |
51 | EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL;\r | |
52 | EFI_EVENT mVirtualAddressChangeEvent = NULL;\r | |
53 | EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r | |
54 | UINT8 *mVariableBuffer = NULL;\r | |
55 | UINT8 *mVariableBufferPhysical = NULL;\r | |
56 | UINTN mVariableBufferSize;\r | |
5e5bb2a9 | 57 | UINTN mVariableBufferPayloadSize;\r |
6ed1ec59 | 58 | EFI_LOCK mVariableServicesLock;\r |
6ab9f441 | 59 | EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock;\r |
17409b7a | 60 | EDKII_VAR_CHECK_PROTOCOL mVarCheck;\r |
0c18794e | 61 | \r |
13af4ab0 SZ |
62 | #define MAX_NV_VARIABLE_SIZE (MAX (MAX (PcdGet32 (PcdMaxVariableSize), \\r |
63 | PcdGet32 (PcdMaxAuthVariableSize)), \\r | |
64 | PcdGet32 (PcdMaxHardwareErrorVariableSize)))\r | |
65 | \r | |
c1d93242 JY |
66 | /**\r |
67 | SecureBoot Hook for SetVariable.\r | |
68 | \r | |
69 | @param[in] VariableName Name of Variable to be found.\r | |
70 | @param[in] VendorGuid Variable vendor GUID.\r | |
71 | \r | |
72 | **/\r | |
73 | VOID\r | |
74 | EFIAPI\r | |
75 | SecureBootHook (\r | |
76 | IN CHAR16 *VariableName,\r | |
77 | IN EFI_GUID *VendorGuid\r | |
78 | );\r | |
79 | \r | |
6ed1ec59 SZ |
80 | /**\r |
81 | Acquires lock only at boot time. Simply returns at runtime.\r | |
82 | \r | |
83 | This is a temperary function that will be removed when\r | |
84 | EfiAcquireLock() in UefiLib can handle the call in UEFI\r | |
85 | Runtimer driver in RT phase.\r | |
86 | It calls EfiAcquireLock() at boot time, and simply returns\r | |
87 | at runtime.\r | |
88 | \r | |
89 | @param Lock A pointer to the lock to acquire.\r | |
90 | \r | |
91 | **/\r | |
92 | VOID\r | |
93 | AcquireLockOnlyAtBootTime (\r | |
94 | IN EFI_LOCK *Lock\r | |
95 | )\r | |
96 | {\r | |
97 | if (!EfiAtRuntime ()) {\r | |
98 | EfiAcquireLock (Lock);\r | |
99 | }\r | |
100 | }\r | |
101 | \r | |
102 | /**\r | |
103 | Releases lock only at boot time. Simply returns at runtime.\r | |
104 | \r | |
105 | This is a temperary function which will be removed when\r | |
106 | EfiReleaseLock() in UefiLib can handle the call in UEFI\r | |
107 | Runtimer driver in RT phase.\r | |
108 | It calls EfiReleaseLock() at boot time and simply returns\r | |
109 | at runtime.\r | |
110 | \r | |
111 | @param Lock A pointer to the lock to release.\r | |
112 | \r | |
113 | **/\r | |
114 | VOID\r | |
115 | ReleaseLockOnlyAtBootTime (\r | |
116 | IN EFI_LOCK *Lock\r | |
117 | )\r | |
118 | {\r | |
119 | if (!EfiAtRuntime ()) {\r | |
120 | EfiReleaseLock (Lock);\r | |
121 | }\r | |
122 | }\r | |
0c18794e | 123 | \r |
124 | /**\r | |
125 | Initialize the communicate buffer using DataSize and Function.\r | |
126 | \r | |
127 | The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +\r | |
128 | DataSize.\r | |
129 | \r | |
dc204d5a JY |
130 | Caution: This function may receive untrusted input.\r |
131 | The data size external input, so this function will validate it carefully to avoid buffer overflow.\r | |
132 | \r | |
0c18794e | 133 | @param[out] DataPtr Points to the data in the communicate buffer.\r |
134 | @param[in] DataSize The data size to send to SMM.\r | |
135 | @param[in] Function The function number to initialize the communicate header.\r | |
136 | \r | |
137 | @retval EFI_INVALID_PARAMETER The data size is too big.\r | |
138 | @retval EFI_SUCCESS Find the specified variable.\r | |
139 | \r | |
140 | **/\r | |
141 | EFI_STATUS\r | |
142 | InitCommunicateBuffer (\r | |
143 | OUT VOID **DataPtr OPTIONAL,\r | |
144 | IN UINTN DataSize,\r | |
145 | IN UINTN Function\r | |
146 | )\r | |
147 | {\r | |
148 | EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; \r | |
149 | SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader; \r | |
150 | \r | |
151 | \r | |
152 | if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {\r | |
153 | return EFI_INVALID_PARAMETER;\r | |
154 | }\r | |
155 | \r | |
156 | SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r | |
157 | CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r | |
158 | SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r | |
159 | \r | |
160 | SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r | |
161 | SmmVariableFunctionHeader->Function = Function;\r | |
162 | if (DataPtr != NULL) {\r | |
163 | *DataPtr = SmmVariableFunctionHeader->Data;\r | |
164 | }\r | |
165 | \r | |
166 | return EFI_SUCCESS;\r | |
167 | }\r | |
168 | \r | |
169 | \r | |
170 | /**\r | |
171 | Send the data in communicate buffer to SMM.\r | |
172 | \r | |
173 | @param[in] DataSize This size of the function header and the data.\r | |
174 | \r | |
175 | @retval EFI_SUCCESS Success is returned from the functin in SMM.\r | |
176 | @retval Others Failure is returned from the function in SMM. \r | |
177 | \r | |
178 | **/\r | |
179 | EFI_STATUS\r | |
180 | SendCommunicateBuffer (\r | |
181 | IN UINTN DataSize\r | |
182 | )\r | |
183 | {\r | |
184 | EFI_STATUS Status;\r | |
185 | UINTN CommSize;\r | |
186 | EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; \r | |
187 | SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;\r | |
188 | \r | |
189 | CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r | |
190 | Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);\r | |
191 | ASSERT_EFI_ERROR (Status);\r | |
192 | \r | |
193 | SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r | |
194 | SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;\r | |
195 | return SmmVariableFunctionHeader->ReturnStatus;\r | |
196 | }\r | |
197 | \r | |
6ab9f441 RN |
198 | /**\r |
199 | Mark a variable that will become read-only after leaving the DXE phase of execution.\r | |
200 | \r | |
201 | @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r | |
202 | @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r | |
203 | @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r | |
204 | \r | |
205 | @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r | |
206 | as pending to be read-only.\r | |
207 | @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r | |
208 | Or VariableName is an empty string.\r | |
209 | @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r | |
210 | already been signaled.\r | |
211 | @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r | |
212 | **/\r | |
213 | EFI_STATUS\r | |
214 | EFIAPI\r | |
215 | VariableLockRequestToLock (\r | |
216 | IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r | |
217 | IN CHAR16 *VariableName,\r | |
218 | IN EFI_GUID *VendorGuid\r | |
219 | )\r | |
220 | {\r | |
221 | EFI_STATUS Status;\r | |
222 | UINTN VariableNameSize;\r | |
223 | UINTN PayloadSize;\r | |
224 | SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock;\r | |
225 | \r | |
226 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r | |
227 | return EFI_INVALID_PARAMETER;\r | |
228 | }\r | |
229 | \r | |
230 | VariableNameSize = StrSize (VariableName);\r | |
d4193108 | 231 | VariableToLock = NULL;\r |
6ab9f441 RN |
232 | \r |
233 | //\r | |
234 | // If VariableName exceeds SMM payload limit. Return failure\r | |
235 | //\r | |
236 | if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {\r | |
237 | return EFI_INVALID_PARAMETER;\r | |
238 | }\r | |
239 | \r | |
240 | AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r | |
241 | \r | |
242 | //\r | |
243 | // Init the communicate buffer. The buffer data size is:\r | |
244 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r | |
245 | //\r | |
246 | PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name) + VariableNameSize;\r | |
247 | Status = InitCommunicateBuffer ((VOID **) &VariableToLock, PayloadSize, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE);\r | |
248 | if (EFI_ERROR (Status)) {\r | |
249 | goto Done;\r | |
250 | }\r | |
251 | ASSERT (VariableToLock != NULL);\r | |
252 | \r | |
253 | CopyGuid (&VariableToLock->Guid, VendorGuid);\r | |
254 | VariableToLock->NameSize = VariableNameSize;\r | |
255 | CopyMem (VariableToLock->Name, VariableName, VariableToLock->NameSize);\r | |
256 | \r | |
257 | //\r | |
258 | // Send data to SMM.\r | |
259 | //\r | |
260 | Status = SendCommunicateBuffer (PayloadSize);\r | |
261 | \r | |
262 | Done:\r | |
263 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
264 | return Status;\r | |
265 | }\r | |
0c18794e | 266 | \r |
17409b7a SZ |
267 | /**\r |
268 | Register SetVariable check handler.\r | |
269 | \r | |
270 | @param[in] Handler Pointer to check handler.\r | |
271 | \r | |
272 | @retval EFI_SUCCESS The SetVariable check handler was registered successfully.\r | |
273 | @retval EFI_INVALID_PARAMETER Handler is NULL.\r | |
274 | @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r | |
275 | already been signaled.\r | |
276 | @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.\r | |
277 | @retval EFI_UNSUPPORTED This interface is not implemented.\r | |
278 | For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.\r | |
279 | \r | |
280 | **/\r | |
281 | EFI_STATUS\r | |
282 | EFIAPI\r | |
283 | VarCheckRegisterSetVariableCheckHandler (\r | |
284 | IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler\r | |
285 | )\r | |
286 | {\r | |
287 | return EFI_UNSUPPORTED;\r | |
288 | }\r | |
289 | \r | |
290 | /**\r | |
291 | Variable property set.\r | |
292 | \r | |
293 | @param[in] Name Pointer to the variable name.\r | |
294 | @param[in] Guid Pointer to the vendor GUID.\r | |
295 | @param[in] VariableProperty Pointer to the input variable property.\r | |
296 | \r | |
297 | @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.\r | |
298 | @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,\r | |
299 | or the fields of VariableProperty are not valid.\r | |
300 | @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r | |
301 | already been signaled.\r | |
302 | @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.\r | |
303 | \r | |
304 | **/\r | |
305 | EFI_STATUS\r | |
306 | EFIAPI\r | |
307 | VarCheckVariablePropertySet (\r | |
308 | IN CHAR16 *Name,\r | |
309 | IN EFI_GUID *Guid,\r | |
310 | IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty\r | |
311 | )\r | |
312 | {\r | |
313 | EFI_STATUS Status;\r | |
314 | UINTN VariableNameSize;\r | |
315 | UINTN PayloadSize;\r | |
316 | SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r | |
317 | \r | |
318 | if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r | |
319 | return EFI_INVALID_PARAMETER;\r | |
320 | }\r | |
321 | \r | |
322 | if (VariableProperty == NULL) {\r | |
323 | return EFI_INVALID_PARAMETER;\r | |
324 | }\r | |
325 | \r | |
326 | if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {\r | |
327 | return EFI_INVALID_PARAMETER;\r | |
328 | }\r | |
329 | \r | |
330 | VariableNameSize = StrSize (Name);\r | |
331 | CommVariableProperty = NULL;\r | |
332 | \r | |
333 | //\r | |
334 | // If VariableName exceeds SMM payload limit. Return failure\r | |
335 | //\r | |
336 | if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r | |
337 | return EFI_INVALID_PARAMETER;\r | |
338 | }\r | |
339 | \r | |
340 | AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r | |
341 | \r | |
342 | //\r | |
343 | // Init the communicate buffer. The buffer data size is:\r | |
344 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r | |
345 | //\r | |
346 | PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r | |
347 | Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET);\r | |
348 | if (EFI_ERROR (Status)) {\r | |
349 | goto Done;\r | |
350 | }\r | |
351 | ASSERT (CommVariableProperty != NULL);\r | |
352 | \r | |
353 | CopyGuid (&CommVariableProperty->Guid, Guid);\r | |
354 | CopyMem (&CommVariableProperty->VariableProperty, VariableProperty, sizeof (*VariableProperty));\r | |
355 | CommVariableProperty->NameSize = VariableNameSize;\r | |
356 | CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r | |
357 | \r | |
358 | //\r | |
359 | // Send data to SMM.\r | |
360 | //\r | |
361 | Status = SendCommunicateBuffer (PayloadSize);\r | |
362 | \r | |
363 | Done:\r | |
364 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
365 | return Status;\r | |
366 | }\r | |
367 | \r | |
368 | /**\r | |
369 | Variable property get.\r | |
370 | \r | |
371 | @param[in] Name Pointer to the variable name.\r | |
372 | @param[in] Guid Pointer to the vendor GUID.\r | |
373 | @param[out] VariableProperty Pointer to the output variable property.\r | |
374 | \r | |
375 | @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.\r | |
376 | @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.\r | |
377 | @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.\r | |
378 | \r | |
379 | **/\r | |
380 | EFI_STATUS\r | |
381 | EFIAPI\r | |
382 | VarCheckVariablePropertyGet (\r | |
383 | IN CHAR16 *Name,\r | |
384 | IN EFI_GUID *Guid,\r | |
385 | OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty\r | |
386 | )\r | |
387 | {\r | |
388 | EFI_STATUS Status;\r | |
389 | UINTN VariableNameSize;\r | |
390 | UINTN PayloadSize;\r | |
391 | SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r | |
392 | \r | |
393 | if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r | |
394 | return EFI_INVALID_PARAMETER;\r | |
395 | }\r | |
396 | \r | |
397 | if (VariableProperty == NULL) {\r | |
398 | return EFI_INVALID_PARAMETER;\r | |
399 | }\r | |
400 | \r | |
401 | VariableNameSize = StrSize (Name);\r | |
402 | CommVariableProperty = NULL;\r | |
403 | \r | |
404 | //\r | |
405 | // If VariableName exceeds SMM payload limit. Return failure\r | |
406 | //\r | |
407 | if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r | |
408 | return EFI_INVALID_PARAMETER;\r | |
409 | }\r | |
410 | \r | |
411 | AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r | |
412 | \r | |
413 | //\r | |
414 | // Init the communicate buffer. The buffer data size is:\r | |
415 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r | |
416 | //\r | |
417 | PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r | |
418 | Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET);\r | |
419 | if (EFI_ERROR (Status)) {\r | |
420 | goto Done;\r | |
421 | }\r | |
422 | ASSERT (CommVariableProperty != NULL);\r | |
423 | \r | |
424 | CopyGuid (&CommVariableProperty->Guid, Guid);\r | |
425 | CommVariableProperty->NameSize = VariableNameSize;\r | |
426 | CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r | |
427 | \r | |
428 | //\r | |
429 | // Send data to SMM.\r | |
430 | //\r | |
431 | Status = SendCommunicateBuffer (PayloadSize);\r | |
432 | if (Status == EFI_SUCCESS) {\r | |
433 | CopyMem (VariableProperty, &CommVariableProperty->VariableProperty, sizeof (*VariableProperty));\r | |
434 | }\r | |
435 | \r | |
436 | Done:\r | |
437 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
438 | return Status;\r | |
439 | }\r | |
440 | \r | |
0c18794e | 441 | /**\r |
442 | This code finds variable in storage blocks (Volatile or Non-Volatile).\r | |
443 | \r | |
dc204d5a JY |
444 | Caution: This function may receive untrusted input.\r |
445 | The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r | |
446 | \r | |
0c18794e | 447 | @param[in] VariableName Name of Variable to be found.\r |
448 | @param[in] VendorGuid Variable vendor GUID.\r | |
449 | @param[out] Attributes Attribute value of the variable found.\r | |
450 | @param[in, out] DataSize Size of Data found. If size is less than the\r | |
451 | data, this value contains the required size.\r | |
452 | @param[out] Data Data pointer.\r | |
453 | \r | |
454 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
455 | @retval EFI_SUCCESS Find the specified variable.\r | |
456 | @retval EFI_NOT_FOUND Not found.\r | |
457 | @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r | |
458 | \r | |
459 | **/\r | |
460 | EFI_STATUS\r | |
461 | EFIAPI\r | |
462 | RuntimeServiceGetVariable (\r | |
463 | IN CHAR16 *VariableName,\r | |
464 | IN EFI_GUID *VendorGuid,\r | |
465 | OUT UINT32 *Attributes OPTIONAL,\r | |
466 | IN OUT UINTN *DataSize,\r | |
467 | OUT VOID *Data\r | |
468 | )\r | |
469 | {\r | |
470 | EFI_STATUS Status;\r | |
471 | UINTN PayloadSize;\r | |
472 | SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r | |
3a146f2a | 473 | UINTN TempDataSize;\r |
9d00d20e | 474 | UINTN VariableNameSize;\r |
0c18794e | 475 | \r |
476 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
477 | return EFI_INVALID_PARAMETER;\r | |
478 | }\r | |
479 | \r | |
3a146f2a | 480 | TempDataSize = *DataSize;\r |
9d00d20e | 481 | VariableNameSize = StrSize (VariableName);\r |
d4193108 | 482 | SmmVariableHeader = NULL;\r |
3a146f2a | 483 | \r |
484 | //\r | |
485 | // If VariableName exceeds SMM payload limit. Return failure\r | |
486 | //\r | |
5e5bb2a9 | 487 | if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r |
3588bb35 SZ |
488 | return EFI_INVALID_PARAMETER;\r |
489 | }\r | |
490 | \r | |
6ed1ec59 SZ |
491 | AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r |
492 | \r | |
0c18794e | 493 | //\r |
494 | // Init the communicate buffer. The buffer data size is:\r | |
6ed1ec59 | 495 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r |
0c18794e | 496 | //\r |
5e5bb2a9 | 497 | if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {\r |
3a146f2a | 498 | //\r |
499 | // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r | |
500 | //\r | |
5e5bb2a9 | 501 | TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;\r |
3a146f2a | 502 | }\r |
9d00d20e | 503 | PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;\r |
3a146f2a | 504 | \r |
0c18794e | 505 | Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);\r |
506 | if (EFI_ERROR (Status)) {\r | |
6ed1ec59 | 507 | goto Done;\r |
0c18794e | 508 | }\r |
509 | ASSERT (SmmVariableHeader != NULL);\r | |
510 | \r | |
511 | CopyGuid (&SmmVariableHeader->Guid, VendorGuid);\r | |
3a146f2a | 512 | SmmVariableHeader->DataSize = TempDataSize;\r |
9d00d20e | 513 | SmmVariableHeader->NameSize = VariableNameSize;\r |
0c18794e | 514 | if (Attributes == NULL) {\r |
515 | SmmVariableHeader->Attributes = 0;\r | |
516 | } else {\r | |
517 | SmmVariableHeader->Attributes = *Attributes;\r | |
518 | }\r | |
519 | CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r | |
520 | \r | |
521 | //\r | |
522 | // Send data to SMM.\r | |
523 | //\r | |
524 | Status = SendCommunicateBuffer (PayloadSize);\r | |
525 | \r | |
526 | //\r | |
527 | // Get data from SMM.\r | |
528 | //\r | |
3a146f2a | 529 | if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r |
530 | //\r | |
531 | // SMM CommBuffer DataSize can be a trimed value\r | |
532 | // Only update DataSize when needed\r | |
533 | //\r | |
534 | *DataSize = SmmVariableHeader->DataSize;\r | |
535 | }\r | |
0c18794e | 536 | if (Attributes != NULL) {\r |
537 | *Attributes = SmmVariableHeader->Attributes;\r | |
538 | }\r | |
539 | \r | |
540 | if (EFI_ERROR (Status)) {\r | |
6ed1ec59 | 541 | goto Done;\r |
0c18794e | 542 | }\r |
543 | \r | |
52e764f7 SZ |
544 | if (Data != NULL) {\r |
545 | CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);\r | |
546 | } else {\r | |
547 | Status = EFI_INVALID_PARAMETER;\r | |
548 | }\r | |
0c18794e | 549 | \r |
6ed1ec59 SZ |
550 | Done:\r |
551 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
0c18794e | 552 | return Status;\r |
553 | }\r | |
554 | \r | |
555 | \r | |
556 | /**\r | |
557 | This code Finds the Next available variable.\r | |
558 | \r | |
559 | @param[in, out] VariableNameSize Size of the variable name.\r | |
560 | @param[in, out] VariableName Pointer to variable name.\r | |
561 | @param[in, out] VendorGuid Variable Vendor Guid.\r | |
562 | \r | |
563 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
564 | @retval EFI_SUCCESS Find the specified variable.\r | |
565 | @retval EFI_NOT_FOUND Not found.\r | |
566 | @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r | |
567 | \r | |
568 | **/\r | |
569 | EFI_STATUS\r | |
570 | EFIAPI\r | |
571 | RuntimeServiceGetNextVariableName (\r | |
572 | IN OUT UINTN *VariableNameSize,\r | |
573 | IN OUT CHAR16 *VariableName,\r | |
574 | IN OUT EFI_GUID *VendorGuid\r | |
575 | )\r | |
576 | {\r | |
577 | EFI_STATUS Status;\r | |
578 | UINTN PayloadSize;\r | |
579 | SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;\r | |
9d00d20e SZ |
580 | UINTN OutVariableNameSize;\r |
581 | UINTN InVariableNameSize;\r | |
0c18794e | 582 | \r |
583 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
584 | return EFI_INVALID_PARAMETER;\r | |
585 | }\r | |
6ed1ec59 | 586 | \r |
9d00d20e SZ |
587 | OutVariableNameSize = *VariableNameSize;\r |
588 | InVariableNameSize = StrSize (VariableName);\r | |
d4193108 | 589 | SmmGetNextVariableName = NULL;\r |
0c55190f | 590 | \r |
591 | //\r | |
592 | // If input string exceeds SMM payload limit. Return failure\r | |
593 | //\r | |
5e5bb2a9 | 594 | if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r |
3588bb35 SZ |
595 | return EFI_INVALID_PARAMETER;\r |
596 | }\r | |
597 | \r | |
6ed1ec59 SZ |
598 | AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r |
599 | \r | |
0c18794e | 600 | //\r |
601 | // Init the communicate buffer. The buffer data size is:\r | |
602 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r | |
603 | //\r | |
5e5bb2a9 | 604 | if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r |
0c55190f | 605 | //\r |
606 | // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r | |
607 | //\r | |
5e5bb2a9 | 608 | OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r |
0c55190f | 609 | }\r |
610 | //\r | |
611 | // Payload should be Guid + NameSize + MAX of Input & Output buffer\r | |
612 | //\r | |
9d00d20e | 613 | PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);\r |
0c55190f | 614 | \r |
0c18794e | 615 | Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);\r |
616 | if (EFI_ERROR (Status)) {\r | |
6ed1ec59 | 617 | goto Done;\r |
0c18794e | 618 | }\r |
619 | ASSERT (SmmGetNextVariableName != NULL);\r | |
620 | \r | |
0c55190f | 621 | //\r |
622 | // SMM comm buffer->NameSize is buffer size for return string\r | |
623 | //\r | |
9d00d20e | 624 | SmmGetNextVariableName->NameSize = OutVariableNameSize;\r |
0c55190f | 625 | \r |
0c18794e | 626 | CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);\r |
0c55190f | 627 | //\r |
628 | // Copy whole string\r | |
629 | //\r | |
9d00d20e SZ |
630 | CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);\r |
631 | if (OutVariableNameSize > InVariableNameSize) {\r | |
632 | ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);\r | |
633 | }\r | |
0c18794e | 634 | \r |
635 | //\r | |
636 | // Send data to SMM\r | |
637 | //\r | |
638 | Status = SendCommunicateBuffer (PayloadSize);\r | |
639 | \r | |
640 | //\r | |
641 | // Get data from SMM.\r | |
642 | //\r | |
9d00d20e SZ |
643 | if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r |
644 | //\r | |
645 | // SMM CommBuffer NameSize can be a trimed value\r | |
646 | // Only update VariableNameSize when needed\r | |
647 | //\r | |
648 | *VariableNameSize = SmmGetNextVariableName->NameSize;\r | |
649 | }\r | |
0c18794e | 650 | if (EFI_ERROR (Status)) {\r |
6ed1ec59 | 651 | goto Done;\r |
0c18794e | 652 | }\r |
653 | \r | |
654 | CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);\r | |
655 | CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize); \r | |
656 | \r | |
6ed1ec59 SZ |
657 | Done:\r |
658 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
0c18794e | 659 | return Status;\r |
660 | }\r | |
661 | \r | |
662 | /**\r | |
663 | This code sets variable in storage blocks (Volatile or Non-Volatile).\r | |
664 | \r | |
dc204d5a JY |
665 | Caution: This function may receive untrusted input.\r |
666 | The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.\r | |
667 | \r | |
0c18794e | 668 | @param[in] VariableName Name of Variable to be found.\r |
669 | @param[in] VendorGuid Variable vendor GUID.\r | |
670 | @param[in] Attributes Attribute value of the variable found\r | |
671 | @param[in] DataSize Size of Data found. If size is less than the\r | |
672 | data, this value contains the required size.\r | |
673 | @param[in] Data Data pointer.\r | |
674 | \r | |
675 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
676 | @retval EFI_SUCCESS Set successfully.\r | |
677 | @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r | |
678 | @retval EFI_NOT_FOUND Not found.\r | |
679 | @retval EFI_WRITE_PROTECTED Variable is read-only.\r | |
680 | \r | |
681 | **/\r | |
682 | EFI_STATUS\r | |
683 | EFIAPI\r | |
684 | RuntimeServiceSetVariable (\r | |
685 | IN CHAR16 *VariableName,\r | |
686 | IN EFI_GUID *VendorGuid,\r | |
687 | IN UINT32 Attributes,\r | |
688 | IN UINTN DataSize,\r | |
689 | IN VOID *Data\r | |
690 | )\r | |
691 | {\r | |
692 | EFI_STATUS Status;\r | |
693 | UINTN PayloadSize; \r | |
694 | SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;\r | |
9d00d20e | 695 | UINTN VariableNameSize;\r |
0c18794e | 696 | \r |
697 | //\r | |
698 | // Check input parameters.\r | |
699 | //\r | |
700 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r | |
701 | return EFI_INVALID_PARAMETER;\r | |
702 | } \r | |
703 | \r | |
704 | if (DataSize != 0 && Data == NULL) {\r | |
705 | return EFI_INVALID_PARAMETER;\r | |
706 | }\r | |
6ed1ec59 | 707 | \r |
9d00d20e | 708 | VariableNameSize = StrSize (VariableName);\r |
d4193108 | 709 | SmmVariableHeader = NULL;\r |
3588bb35 | 710 | \r |
5e5bb2a9 SZ |
711 | //\r |
712 | // If VariableName or DataSize exceeds SMM payload limit. Return failure\r | |
713 | //\r | |
714 | if ((VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r | |
715 | (DataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize)){\r | |
56251c66 | 716 | return EFI_INVALID_PARAMETER;\r |
717 | }\r | |
718 | \r | |
6ed1ec59 | 719 | AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r |
56251c66 | 720 | \r |
0c18794e | 721 | //\r |
722 | // Init the communicate buffer. The buffer data size is:\r | |
723 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r | |
724 | //\r | |
9d00d20e | 725 | PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;\r |
0c18794e | 726 | Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);\r |
727 | if (EFI_ERROR (Status)) {\r | |
6ed1ec59 | 728 | goto Done;\r |
0c18794e | 729 | }\r |
730 | ASSERT (SmmVariableHeader != NULL);\r | |
731 | \r | |
732 | CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);\r | |
733 | SmmVariableHeader->DataSize = DataSize;\r | |
9d00d20e | 734 | SmmVariableHeader->NameSize = VariableNameSize;\r |
0c18794e | 735 | SmmVariableHeader->Attributes = Attributes;\r |
736 | CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r | |
737 | CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);\r | |
738 | \r | |
739 | //\r | |
740 | // Send data to SMM.\r | |
741 | //\r | |
742 | Status = SendCommunicateBuffer (PayloadSize);\r | |
6ed1ec59 SZ |
743 | \r |
744 | Done:\r | |
745 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
c1d93242 JY |
746 | \r |
747 | if (!EfiAtRuntime ()) {\r | |
748 | if (!EFI_ERROR (Status)) {\r | |
749 | SecureBootHook (\r | |
750 | VariableName,\r | |
751 | VendorGuid\r | |
752 | );\r | |
753 | }\r | |
754 | }\r | |
0c18794e | 755 | return Status;\r |
756 | }\r | |
757 | \r | |
758 | \r | |
759 | /**\r | |
760 | This code returns information about the EFI variables.\r | |
761 | \r | |
762 | @param[in] Attributes Attributes bitmask to specify the type of variables\r | |
763 | on which to return information.\r | |
764 | @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
765 | for the EFI variables associated with the attributes specified.\r | |
766 | @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
767 | for EFI variables associated with the attributes specified.\r | |
768 | @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
769 | associated with the attributes specified.\r | |
770 | \r | |
771 | @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r | |
772 | @retval EFI_SUCCESS Query successfully.\r | |
773 | @retval EFI_UNSUPPORTED The attribute is not supported on this platform.\r | |
774 | \r | |
775 | **/\r | |
776 | EFI_STATUS\r | |
777 | EFIAPI\r | |
778 | RuntimeServiceQueryVariableInfo (\r | |
779 | IN UINT32 Attributes,\r | |
780 | OUT UINT64 *MaximumVariableStorageSize,\r | |
781 | OUT UINT64 *RemainingVariableStorageSize,\r | |
782 | OUT UINT64 *MaximumVariableSize\r | |
783 | )\r | |
784 | {\r | |
785 | EFI_STATUS Status;\r | |
786 | UINTN PayloadSize;\r | |
787 | SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;\r | |
788 | \r | |
d4193108 ED |
789 | SmmQueryVariableInfo = NULL;\r |
790 | \r | |
0c18794e | 791 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r |
792 | return EFI_INVALID_PARAMETER;\r | |
793 | }\r | |
6ed1ec59 SZ |
794 | \r |
795 | AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r | |
796 | \r | |
0c18794e | 797 | //\r |
798 | // Init the communicate buffer. The buffer data size is:\r | |
799 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;\r | |
800 | //\r | |
801 | PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r | |
802 | Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);\r | |
803 | if (EFI_ERROR (Status)) {\r | |
6ed1ec59 | 804 | goto Done;\r |
0c18794e | 805 | }\r |
806 | ASSERT (SmmQueryVariableInfo != NULL);\r | |
807 | \r | |
808 | SmmQueryVariableInfo->Attributes = Attributes;\r | |
809 | \r | |
810 | //\r | |
811 | // Send data to SMM.\r | |
812 | //\r | |
813 | Status = SendCommunicateBuffer (PayloadSize);\r | |
814 | if (EFI_ERROR (Status)) {\r | |
6ed1ec59 | 815 | goto Done;\r |
0c18794e | 816 | }\r |
817 | \r | |
818 | //\r | |
819 | // Get data from SMM.\r | |
820 | //\r | |
821 | *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize;\r | |
822 | *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize;\r | |
823 | *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize; \r | |
6ed1ec59 SZ |
824 | \r |
825 | Done:\r | |
826 | ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r | |
aab9212f | 827 | return Status;\r |
0c18794e | 828 | }\r |
829 | \r | |
830 | \r | |
831 | /**\r | |
832 | Exit Boot Services Event notification handler.\r | |
833 | \r | |
834 | Notify SMM variable driver about the event.\r | |
835 | \r | |
836 | @param[in] Event Event whose notification function is being invoked.\r | |
837 | @param[in] Context Pointer to the notification function's context.\r | |
838 | \r | |
839 | **/\r | |
840 | VOID\r | |
841 | EFIAPI\r | |
842 | OnExitBootServices (\r | |
843 | IN EFI_EVENT Event,\r | |
844 | IN VOID *Context\r | |
845 | )\r | |
846 | {\r | |
847 | //\r | |
848 | // Init the communicate buffer. The buffer data size is:\r | |
849 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r | |
850 | //\r | |
851 | InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE); \r | |
852 | \r | |
853 | //\r | |
854 | // Send data to SMM.\r | |
855 | //\r | |
856 | SendCommunicateBuffer (0);\r | |
857 | }\r | |
858 | \r | |
859 | \r | |
860 | /**\r | |
861 | On Ready To Boot Services Event notification handler.\r | |
862 | \r | |
863 | Notify SMM variable driver about the event.\r | |
864 | \r | |
865 | @param[in] Event Event whose notification function is being invoked\r | |
866 | @param[in] Context Pointer to the notification function's context\r | |
867 | \r | |
868 | **/\r | |
869 | VOID\r | |
870 | EFIAPI\r | |
871 | OnReadyToBoot (\r | |
872 | IN EFI_EVENT Event,\r | |
873 | IN VOID *Context\r | |
874 | )\r | |
875 | {\r | |
876 | //\r | |
877 | // Init the communicate buffer. The buffer data size is:\r | |
878 | // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r | |
879 | //\r | |
880 | InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);\r | |
881 | \r | |
882 | //\r | |
883 | // Send data to SMM.\r | |
884 | //\r | |
885 | SendCommunicateBuffer (0);\r | |
886 | }\r | |
887 | \r | |
888 | \r | |
889 | /**\r | |
890 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
891 | \r | |
892 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
893 | It convers pointer to new virtual address.\r | |
894 | \r | |
895 | @param[in] Event Event whose notification function is being invoked.\r | |
896 | @param[in] Context Pointer to the notification function's context.\r | |
897 | \r | |
898 | **/\r | |
899 | VOID\r | |
900 | EFIAPI\r | |
901 | VariableAddressChangeEvent (\r | |
902 | IN EFI_EVENT Event,\r | |
903 | IN VOID *Context\r | |
904 | )\r | |
905 | {\r | |
906 | EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);\r | |
907 | EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r | |
908 | }\r | |
909 | \r | |
910 | \r | |
911 | /**\r | |
912 | Initialize variable service and install Variable Architectural protocol.\r | |
913 | \r | |
914 | @param[in] Event Event whose notification function is being invoked.\r | |
915 | @param[in] Context Pointer to the notification function's context.\r | |
916 | \r | |
917 | **/\r | |
918 | VOID\r | |
919 | EFIAPI\r | |
920 | SmmVariableReady (\r | |
921 | IN EFI_EVENT Event,\r | |
922 | IN VOID *Context\r | |
923 | )\r | |
924 | {\r | |
925 | EFI_STATUS Status;\r | |
926 | \r | |
927 | Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);\r | |
928 | if (EFI_ERROR (Status)) {\r | |
929 | return;\r | |
930 | }\r | |
931 | \r | |
932 | Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r | |
933 | ASSERT_EFI_ERROR (Status);\r | |
934 | \r | |
935 | //\r | |
5e5bb2a9 | 936 | // Allocate memory for variable communicate buffer.\r |
0c18794e | 937 | //\r |
13af4ab0 | 938 | mVariableBufferPayloadSize = MAX_NV_VARIABLE_SIZE +\r |
17409b7a | 939 | OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - sizeof (VARIABLE_HEADER);\r |
5e5bb2a9 | 940 | mVariableBufferSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + mVariableBufferPayloadSize;\r |
0c18794e | 941 | mVariableBuffer = AllocateRuntimePool (mVariableBufferSize);\r |
942 | ASSERT (mVariableBuffer != NULL);\r | |
943 | \r | |
944 | //\r | |
945 | // Save the buffer physical address used for SMM conmunication.\r | |
946 | //\r | |
947 | mVariableBufferPhysical = mVariableBuffer;\r | |
948 | \r | |
949 | gRT->GetVariable = RuntimeServiceGetVariable;\r | |
950 | gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;\r | |
951 | gRT->SetVariable = RuntimeServiceSetVariable;\r | |
952 | gRT->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r | |
953 | \r | |
954 | //\r | |
955 | // Install the Variable Architectural Protocol on a new handle.\r | |
956 | //\r | |
957 | Status = gBS->InstallProtocolInterface (\r | |
958 | &mHandle,\r | |
959 | &gEfiVariableArchProtocolGuid, \r | |
960 | EFI_NATIVE_INTERFACE,\r | |
961 | NULL\r | |
962 | );\r | |
963 | ASSERT_EFI_ERROR (Status);\r | |
fe9dfae5 SZ |
964 | \r |
965 | mVariableLock.RequestToLock = VariableLockRequestToLock;\r | |
966 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
967 | &mHandle,\r | |
968 | &gEdkiiVariableLockProtocolGuid,\r | |
969 | &mVariableLock,\r | |
970 | NULL\r | |
971 | );\r | |
972 | ASSERT_EFI_ERROR (Status);\r | |
973 | \r | |
974 | mVarCheck.RegisterSetVariableCheckHandler = VarCheckRegisterSetVariableCheckHandler;\r | |
975 | mVarCheck.VariablePropertySet = VarCheckVariablePropertySet;\r | |
976 | mVarCheck.VariablePropertyGet = VarCheckVariablePropertyGet;\r | |
977 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
978 | &mHandle,\r | |
979 | &gEdkiiVarCheckProtocolGuid,\r | |
980 | &mVarCheck,\r | |
981 | NULL\r | |
982 | );\r | |
983 | ASSERT_EFI_ERROR (Status);\r | |
0c18794e | 984 | }\r |
985 | \r | |
986 | \r | |
987 | /**\r | |
988 | SMM Non-Volatile variable write service is ready notify event handler.\r | |
989 | \r | |
990 | @param[in] Event Event whose notification function is being invoked.\r | |
991 | @param[in] Context Pointer to the notification function's context.\r | |
992 | \r | |
993 | **/\r | |
994 | VOID\r | |
995 | EFIAPI\r | |
996 | SmmVariableWriteReady (\r | |
997 | IN EFI_EVENT Event,\r | |
998 | IN VOID *Context\r | |
999 | )\r | |
1000 | {\r | |
1001 | EFI_STATUS Status;\r | |
1002 | VOID *ProtocolOps;\r | |
1003 | \r | |
1004 | //\r | |
1005 | // Check whether the protocol is installed or not.\r | |
1006 | //\r | |
1007 | Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);\r | |
1008 | if (EFI_ERROR (Status)) {\r | |
1009 | return;\r | |
1010 | }\r | |
1011 | \r | |
1012 | Status = gBS->InstallProtocolInterface (\r | |
1013 | &mHandle,\r | |
1014 | &gEfiVariableWriteArchProtocolGuid, \r | |
1015 | EFI_NATIVE_INTERFACE,\r | |
1016 | NULL\r | |
1017 | );\r | |
1018 | ASSERT_EFI_ERROR (Status); \r | |
1019 | }\r | |
1020 | \r | |
1021 | \r | |
1022 | /**\r | |
1023 | Variable Driver main entry point. The Variable driver places the 4 EFI\r | |
1024 | runtime services in the EFI System Table and installs arch protocols \r | |
1025 | for variable read and write services being available. It also registers\r | |
1026 | a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
1027 | \r | |
1028 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
1029 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
1030 | \r | |
1031 | @retval EFI_SUCCESS Variable service successfully initialized.\r | |
1032 | \r | |
1033 | **/\r | |
1034 | EFI_STATUS\r | |
1035 | EFIAPI\r | |
1036 | VariableSmmRuntimeInitialize (\r | |
1037 | IN EFI_HANDLE ImageHandle,\r | |
1038 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1039 | )\r | |
1040 | {\r | |
1041 | VOID *SmmVariableRegistration;\r | |
1042 | VOID *SmmVariableWriteRegistration;\r | |
1043 | EFI_EVENT OnReadyToBootEvent;\r | |
1044 | EFI_EVENT ExitBootServiceEvent;\r | |
c7b89751 | 1045 | EFI_EVENT LegacyBootEvent;\r |
6ed1ec59 SZ |
1046 | \r |
1047 | EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);\r | |
1048 | \r | |
0c18794e | 1049 | //\r |
1050 | // Smm variable service is ready\r | |
1051 | //\r | |
1052 | EfiCreateProtocolNotifyEvent (\r | |
1053 | &gEfiSmmVariableProtocolGuid, \r | |
1054 | TPL_CALLBACK, \r | |
1055 | SmmVariableReady, \r | |
1056 | NULL, \r | |
1057 | &SmmVariableRegistration\r | |
1058 | );\r | |
1059 | \r | |
1060 | //\r | |
1061 | // Smm Non-Volatile variable write service is ready\r | |
1062 | //\r | |
1063 | EfiCreateProtocolNotifyEvent (\r | |
1064 | &gSmmVariableWriteGuid, \r | |
1065 | TPL_CALLBACK, \r | |
1066 | SmmVariableWriteReady, \r | |
1067 | NULL, \r | |
1068 | &SmmVariableWriteRegistration\r | |
1069 | );\r | |
1070 | \r | |
1071 | //\r | |
1072 | // Register the event to reclaim variable for OS usage.\r | |
1073 | //\r | |
1074 | EfiCreateEventReadyToBootEx (\r | |
1075 | TPL_NOTIFY, \r | |
1076 | OnReadyToBoot, \r | |
1077 | NULL, \r | |
1078 | &OnReadyToBootEvent\r | |
1079 | ); \r | |
1080 | \r | |
1081 | //\r | |
1082 | // Register the event to inform SMM variable that it is at runtime.\r | |
1083 | //\r | |
1084 | gBS->CreateEventEx (\r | |
1085 | EVT_NOTIFY_SIGNAL,\r | |
1086 | TPL_NOTIFY,\r | |
1087 | OnExitBootServices,\r | |
1088 | NULL,\r | |
1089 | &gEfiEventExitBootServicesGuid,\r | |
1090 | &ExitBootServiceEvent\r | |
1091 | ); \r | |
1092 | \r | |
c7b89751 SZ |
1093 | //\r |
1094 | // Register the event to inform SMM variable that it is at runtime for legacy boot.\r | |
1095 | // Reuse OnExitBootServices() here.\r | |
1096 | //\r | |
1097 | EfiCreateEventLegacyBootEx(\r | |
1098 | TPL_NOTIFY,\r | |
1099 | OnExitBootServices,\r | |
1100 | NULL,\r | |
1101 | &LegacyBootEvent\r | |
1102 | );\r | |
1103 | \r | |
0c18794e | 1104 | //\r |
1105 | // Register the event to convert the pointer for runtime.\r | |
1106 | //\r | |
1107 | gBS->CreateEventEx (\r | |
1108 | EVT_NOTIFY_SIGNAL,\r | |
1109 | TPL_NOTIFY,\r | |
1110 | VariableAddressChangeEvent,\r | |
1111 | NULL,\r | |
1112 | &gEfiEventVirtualAddressChangeGuid,\r | |
1113 | &mVirtualAddressChangeEvent\r | |
1114 | );\r | |
1115 | \r | |
1116 | return EFI_SUCCESS;\r | |
1117 | }\r | |
1118 | \r |