]>
Commit | Line | Data |
---|---|---|
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 | \r | |
b7bc11a4 | 6 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r |
8a2d4996 | 7 | This program and the accompanying materials \r |
8 | are licensed and made available under the terms and conditions of the BSD License \r | |
9 | which accompanies this distribution. The full text of the license may be found at \r | |
10 | http://opensource.org/licenses/bsd-license.php \r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "Variable.h"\r | |
18 | \r | |
19 | extern VARIABLE_STORE_HEADER *mNvVariableCache;\r | |
858939cb | 20 | extern VARIABLE_INFO_ENTRY *gVariableInfo;\r |
8a2d4996 | 21 | EFI_HANDLE mHandle = NULL;\r |
22 | EFI_EVENT mVirtualAddressChangeEvent = NULL;\r | |
32732a33 | 23 | EFI_EVENT mFtwRegistration = NULL;\r |
8a2d4996 | 24 | \r |
25 | /**\r | |
32732a33 | 26 | Return TRUE if ExitBootServices () has been called.\r |
8a2d4996 | 27 | \r |
32732a33 | 28 | @retval TRUE If ExitBootServices () has been called.\r |
8a2d4996 | 29 | **/\r |
30 | BOOLEAN\r | |
31 | AtRuntime (\r | |
32 | VOID\r | |
33 | )\r | |
34 | {\r | |
35 | return EfiAtRuntime ();\r | |
36 | }\r | |
37 | \r | |
38 | \r | |
39 | /**\r | |
40 | Initializes a basic mutual exclusion lock.\r | |
41 | \r | |
42 | This function initializes a basic mutual exclusion lock to the released state \r | |
43 | and returns the lock. Each lock provides mutual exclusion access at its task \r | |
44 | priority level. Since there is no preemption or multiprocessor support in EFI,\r | |
45 | acquiring the lock only consists of raising to the locks TPL.\r | |
46 | If Lock is NULL, then ASSERT().\r | |
47 | If Priority is not a valid TPL value, then ASSERT().\r | |
48 | \r | |
49 | @param Lock A pointer to the lock data structure to initialize.\r | |
50 | @param Priority EFI TPL is associated with the lock.\r | |
51 | \r | |
52 | @return The lock.\r | |
53 | \r | |
54 | **/\r | |
55 | EFI_LOCK *\r | |
56 | InitializeLock (\r | |
57 | IN OUT EFI_LOCK *Lock,\r | |
58 | IN EFI_TPL Priority\r | |
59 | )\r | |
60 | {\r | |
61 | return EfiInitializeLock (Lock, Priority);\r | |
62 | }\r | |
63 | \r | |
64 | \r | |
65 | /**\r | |
66 | Acquires lock only at boot time. Simply returns at runtime.\r | |
67 | \r | |
68 | This is a temperary function that will be removed when\r | |
69 | EfiAcquireLock() in UefiLib can handle the call in UEFI\r | |
70 | Runtimer driver in RT phase.\r | |
71 | It calls EfiAcquireLock() at boot time, and simply returns\r | |
72 | at runtime.\r | |
73 | \r | |
74 | @param Lock A pointer to the lock to acquire.\r | |
75 | \r | |
76 | **/\r | |
77 | VOID\r | |
78 | AcquireLockOnlyAtBootTime (\r | |
79 | IN EFI_LOCK *Lock\r | |
80 | )\r | |
81 | {\r | |
82 | if (!AtRuntime ()) {\r | |
83 | EfiAcquireLock (Lock);\r | |
84 | }\r | |
85 | }\r | |
86 | \r | |
87 | \r | |
88 | /**\r | |
89 | Releases lock only at boot time. Simply returns at runtime.\r | |
90 | \r | |
91 | This is a temperary function which will be removed when\r | |
92 | EfiReleaseLock() in UefiLib can handle the call in UEFI\r | |
93 | Runtimer driver in RT phase.\r | |
94 | It calls EfiReleaseLock() at boot time and simply returns\r | |
95 | at runtime.\r | |
96 | \r | |
97 | @param Lock A pointer to the lock to release.\r | |
98 | \r | |
99 | **/\r | |
100 | VOID\r | |
101 | ReleaseLockOnlyAtBootTime (\r | |
102 | IN EFI_LOCK *Lock\r | |
103 | )\r | |
104 | {\r | |
105 | if (!AtRuntime ()) {\r | |
106 | EfiReleaseLock (Lock);\r | |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | /**\r | |
111 | Retrive the Fault Tolerent Write protocol interface.\r | |
112 | \r | |
113 | @param[out] FtwProtocol The interface of Ftw protocol\r | |
114 | \r | |
115 | @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.\r | |
116 | @retval EFI_NOT_FOUND The FTW protocol instance was not found.\r | |
117 | @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r | |
118 | \r | |
119 | **/\r | |
120 | EFI_STATUS\r | |
121 | GetFtwProtocol (\r | |
122 | OUT VOID **FtwProtocol\r | |
123 | )\r | |
124 | {\r | |
125 | EFI_STATUS Status;\r | |
126 | \r | |
127 | //\r | |
128 | // Locate Fault Tolerent Write protocol\r | |
129 | //\r | |
130 | Status = gBS->LocateProtocol (\r | |
131 | &gEfiFaultTolerantWriteProtocolGuid,\r | |
132 | NULL,\r | |
133 | FtwProtocol\r | |
134 | ); \r | |
135 | return Status;\r | |
136 | }\r | |
137 | \r | |
138 | /**\r | |
139 | Retrive the FVB protocol interface by HANDLE.\r | |
140 | \r | |
141 | @param[in] FvBlockHandle The handle of FVB protocol that provides services for\r | |
142 | reading, writing, and erasing the target block.\r | |
143 | @param[out] FvBlock The interface of FVB protocol\r | |
144 | \r | |
145 | @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r | |
146 | @retval EFI_UNSUPPORTED The device does not support the FVB protocol.\r | |
147 | @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r | |
148 | \r | |
149 | **/\r | |
150 | EFI_STATUS\r | |
151 | GetFvbByHandle (\r | |
152 | IN EFI_HANDLE FvBlockHandle,\r | |
153 | OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r | |
154 | )\r | |
155 | {\r | |
156 | //\r | |
157 | // To get the FVB protocol interface on the handle\r | |
158 | //\r | |
159 | return gBS->HandleProtocol (\r | |
160 | FvBlockHandle,\r | |
161 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
162 | (VOID **) FvBlock\r | |
163 | );\r | |
164 | }\r | |
165 | \r | |
166 | \r | |
167 | /**\r | |
168 | Function returns an array of handles that support the FVB protocol\r | |
169 | in a buffer allocated from pool. \r | |
170 | \r | |
171 | @param[out] NumberHandles The number of handles returned in Buffer.\r | |
172 | @param[out] Buffer A pointer to the buffer to return the requested\r | |
173 | array of handles that support FVB protocol.\r | |
174 | \r | |
175 | @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r | |
176 | handles in Buffer was returned in NumberHandles.\r | |
177 | @retval EFI_NOT_FOUND No FVB handle was found.\r | |
178 | @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r | |
179 | @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r | |
180 | \r | |
181 | **/\r | |
182 | EFI_STATUS\r | |
183 | GetFvbCountAndBuffer (\r | |
184 | OUT UINTN *NumberHandles,\r | |
185 | OUT EFI_HANDLE **Buffer\r | |
186 | )\r | |
187 | {\r | |
188 | EFI_STATUS Status;\r | |
189 | \r | |
190 | //\r | |
191 | // Locate all handles of Fvb protocol\r | |
192 | //\r | |
193 | Status = gBS->LocateHandleBuffer (\r | |
194 | ByProtocol,\r | |
195 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
196 | NULL,\r | |
197 | NumberHandles,\r | |
198 | Buffer\r | |
199 | );\r | |
200 | return Status;\r | |
201 | }\r | |
202 | \r | |
203 | \r | |
204 | /**\r | |
205 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
206 | \r | |
207 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
208 | It convers pointer to new virtual address.\r | |
209 | \r | |
210 | @param Event Event whose notification function is being invoked.\r | |
211 | @param Context Pointer to the notification function's context.\r | |
212 | \r | |
213 | **/\r | |
214 | VOID\r | |
215 | EFIAPI\r | |
216 | VariableClassAddressChangeEvent (\r | |
217 | IN EFI_EVENT Event,\r | |
218 | IN VOID *Context\r | |
219 | )\r | |
220 | {\r | |
221 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r | |
222 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r | |
223 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r | |
224 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);\r | |
225 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);\r | |
226 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);\r | |
227 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);\r | |
228 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);\r | |
229 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);\r | |
230 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);\r | |
231 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);\r | |
232 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
233 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r | |
234 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r | |
235 | EfiConvertPointer (0x0, (VOID **) &mNvVariableCache); \r | |
236 | }\r | |
237 | \r | |
238 | \r | |
239 | /**\r | |
240 | Notification function of EVT_GROUP_READY_TO_BOOT event group.\r | |
241 | \r | |
242 | This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r | |
243 | When the Boot Manager is about to load and execute a boot option, it reclaims variable\r | |
244 | storage if free size is below the threshold.\r | |
245 | \r | |
246 | @param Event Event whose notification function is being invoked.\r | |
247 | @param Context Pointer to the notification function's context.\r | |
248 | \r | |
249 | **/\r | |
250 | VOID\r | |
251 | EFIAPI\r | |
252 | OnReadyToBoot (\r | |
253 | EFI_EVENT Event,\r | |
254 | VOID *Context\r | |
255 | )\r | |
256 | {\r | |
257 | ReclaimForOS ();\r | |
9ae0edbf | 258 | if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r |
259 | gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r | |
260 | }\r | |
8a2d4996 | 261 | }\r |
262 | \r | |
263 | \r | |
264 | /**\r | |
265 | Fault Tolerant Write protocol notification event handler.\r | |
266 | \r | |
267 | Non-Volatile variable write may needs FTW protocol to reclaim when \r | |
268 | writting variable.\r | |
269 | \r | |
270 | @param[in] Event Event whose notification function is being invoked.\r | |
271 | @param[in] Context Pointer to the notification function's context.\r | |
272 | \r | |
273 | **/\r | |
274 | VOID\r | |
275 | EFIAPI\r | |
276 | FtwNotificationEvent (\r | |
277 | IN EFI_EVENT Event,\r | |
278 | IN VOID *Context\r | |
279 | )\r | |
280 | {\r | |
281 | EFI_STATUS Status;\r | |
282 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r | |
283 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
284 | EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r | |
9ae0edbf | 285 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r |
286 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
287 | UINT64 Length;\r | |
288 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r | |
289 | UINT64 VariableStoreLength;\r | |
8a2d4996 | 290 | \r |
291 | //\r | |
292 | // Ensure FTW protocol is installed.\r | |
293 | //\r | |
5c54e905 | 294 | Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r |
8a2d4996 | 295 | if (EFI_ERROR (Status)) {\r |
296 | return ;\r | |
297 | }\r | |
298 | \r | |
299 | //\r | |
300 | // Find the proper FVB protocol for variable.\r | |
301 | //\r | |
302 | NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r | |
303 | if (NvStorageVariableBase == 0) {\r | |
304 | NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
305 | }\r | |
306 | Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r | |
307 | if (EFI_ERROR (Status)) {\r | |
308 | return ;\r | |
309 | }\r | |
310 | mVariableModuleGlobal->FvbInstance = FvbProtocol;\r | |
9ae0edbf | 311 | \r |
312 | //\r | |
313 | // Mark the variable storage region of the FLASH as RUNTIME.\r | |
314 | //\r | |
315 | VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r | |
316 | VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r | |
317 | BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r | |
318 | Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r | |
319 | Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r | |
320 | \r | |
321 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
322 | if (EFI_ERROR (Status)) {\r | |
323 | DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r | |
324 | } else {\r | |
325 | Status = gDS->SetMemorySpaceAttributes (\r | |
326 | BaseAddress,\r | |
327 | Length,\r | |
328 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
329 | );\r | |
330 | if (EFI_ERROR (Status)) {\r | |
331 | DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r | |
332 | }\r | |
333 | }\r | |
8a2d4996 | 334 | \r |
335 | Status = VariableWriteServiceInitialize ();\r | |
336 | ASSERT_EFI_ERROR (Status);\r | |
337 | \r | |
338 | //\r | |
5c54e905 | 339 | // Install the Variable Write Architectural protocol.\r |
340 | //\r | |
8a2d4996 | 341 | Status = gBS->InstallProtocolInterface (\r |
342 | &mHandle,\r | |
343 | &gEfiVariableWriteArchProtocolGuid, \r | |
344 | EFI_NATIVE_INTERFACE,\r | |
345 | NULL\r | |
346 | );\r | |
347 | ASSERT_EFI_ERROR (Status);\r | |
348 | \r | |
349 | //\r | |
350 | // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r | |
351 | //\r | |
352 | gBS->CloseEvent (Event);\r | |
353 | \r | |
354 | }\r | |
355 | \r | |
356 | \r | |
357 | /**\r | |
358 | Variable Driver main entry point. The Variable driver places the 4 EFI\r | |
359 | runtime services in the EFI System Table and installs arch protocols \r | |
360 | for variable read and write services being availible. It also registers\r | |
361 | a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
362 | \r | |
363 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
364 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
365 | \r | |
366 | @retval EFI_SUCCESS Variable service successfully initialized.\r | |
367 | \r | |
368 | **/\r | |
369 | EFI_STATUS\r | |
370 | EFIAPI\r | |
371 | VariableServiceInitialize (\r | |
372 | IN EFI_HANDLE ImageHandle,\r | |
373 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
374 | )\r | |
375 | {\r | |
376 | EFI_STATUS Status;\r | |
377 | EFI_EVENT ReadyToBootEvent; \r | |
378 | \r | |
379 | Status = VariableCommonInitialize ();\r | |
380 | ASSERT_EFI_ERROR (Status);\r | |
381 | \r | |
382 | SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r | |
383 | SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r | |
384 | SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r | |
385 | SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r | |
386 | \r | |
387 | //\r | |
388 | // Now install the Variable Runtime Architectural protocol on a new handle.\r | |
389 | //\r | |
390 | Status = gBS->InstallProtocolInterface (\r | |
391 | &mHandle,\r | |
392 | &gEfiVariableArchProtocolGuid, \r | |
393 | EFI_NATIVE_INTERFACE,\r | |
394 | NULL\r | |
395 | );\r | |
396 | ASSERT_EFI_ERROR (Status);\r | |
397 | \r | |
398 | //\r | |
399 | // Register FtwNotificationEvent () notify function.\r | |
400 | // \r | |
401 | EfiCreateProtocolNotifyEvent (\r | |
402 | &gEfiFaultTolerantWriteProtocolGuid,\r | |
403 | TPL_CALLBACK,\r | |
404 | FtwNotificationEvent,\r | |
405 | (VOID *)SystemTable,\r | |
406 | &mFtwRegistration\r | |
407 | );\r | |
408 | \r | |
409 | Status = gBS->CreateEventEx (\r | |
410 | EVT_NOTIFY_SIGNAL,\r | |
411 | TPL_NOTIFY,\r | |
412 | VariableClassAddressChangeEvent,\r | |
413 | NULL,\r | |
414 | &gEfiEventVirtualAddressChangeGuid,\r | |
415 | &mVirtualAddressChangeEvent\r | |
416 | );\r | |
417 | ASSERT_EFI_ERROR (Status);\r | |
418 | \r | |
419 | //\r | |
420 | // Register the event handling function to reclaim variable for OS usage.\r | |
421 | //\r | |
422 | Status = EfiCreateEventReadyToBootEx (\r | |
423 | TPL_NOTIFY, \r | |
424 | OnReadyToBoot, \r | |
425 | NULL, \r | |
426 | &ReadyToBootEvent\r | |
427 | );\r | |
428 | \r | |
8a2d4996 | 429 | return EFI_SUCCESS;\r |
430 | }\r | |
431 | \r |