]>
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 | |
6 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
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 | |
20 | VARIABLE_INFO_ENTRY *gVariableInfo;\r | |
21 | EFI_HANDLE mHandle = NULL;\r | |
22 | EFI_EVENT mVirtualAddressChangeEvent = NULL;\r | |
23 | EFI_EVENT mFtwRegistration = NULL;\r | |
24 | \r | |
25 | /**\r | |
26 | Return TRUE if ExitBootServices () has been called\r | |
27 | \r | |
28 | @retval TRUE If ExitBootServices () has been called\r | |
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 | |
258 | }\r | |
259 | \r | |
260 | \r | |
261 | /**\r | |
262 | Fault Tolerant Write protocol notification event handler.\r | |
263 | \r | |
264 | Non-Volatile variable write may needs FTW protocol to reclaim when \r | |
265 | writting variable.\r | |
266 | \r | |
267 | @param[in] Event Event whose notification function is being invoked.\r | |
268 | @param[in] Context Pointer to the notification function's context.\r | |
269 | \r | |
270 | **/\r | |
271 | VOID\r | |
272 | EFIAPI\r | |
273 | FtwNotificationEvent (\r | |
274 | IN EFI_EVENT Event,\r | |
275 | IN VOID *Context\r | |
276 | )\r | |
277 | {\r | |
278 | EFI_STATUS Status;\r | |
279 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r | |
280 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
281 | EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r | |
282 | \r | |
283 | //\r | |
284 | // Ensure FTW protocol is installed.\r | |
285 | //\r | |
5c54e905 | 286 | Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r |
8a2d4996 | 287 | if (EFI_ERROR (Status)) {\r |
288 | return ;\r | |
289 | }\r | |
290 | \r | |
291 | //\r | |
292 | // Find the proper FVB protocol for variable.\r | |
293 | //\r | |
294 | NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r | |
295 | if (NvStorageVariableBase == 0) {\r | |
296 | NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
297 | }\r | |
298 | Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r | |
299 | if (EFI_ERROR (Status)) {\r | |
300 | return ;\r | |
301 | }\r | |
302 | mVariableModuleGlobal->FvbInstance = FvbProtocol;\r | |
303 | \r | |
304 | Status = VariableWriteServiceInitialize ();\r | |
305 | ASSERT_EFI_ERROR (Status);\r | |
306 | \r | |
307 | //\r | |
5c54e905 | 308 | // Install the Variable Write Architectural protocol.\r |
309 | //\r | |
8a2d4996 | 310 | Status = gBS->InstallProtocolInterface (\r |
311 | &mHandle,\r | |
312 | &gEfiVariableWriteArchProtocolGuid, \r | |
313 | EFI_NATIVE_INTERFACE,\r | |
314 | NULL\r | |
315 | );\r | |
316 | ASSERT_EFI_ERROR (Status);\r | |
317 | \r | |
318 | //\r | |
319 | // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r | |
320 | //\r | |
321 | gBS->CloseEvent (Event);\r | |
322 | \r | |
323 | }\r | |
324 | \r | |
325 | \r | |
326 | /**\r | |
327 | Variable Driver main entry point. The Variable driver places the 4 EFI\r | |
328 | runtime services in the EFI System Table and installs arch protocols \r | |
329 | for variable read and write services being availible. It also registers\r | |
330 | a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
331 | \r | |
332 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
333 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
334 | \r | |
335 | @retval EFI_SUCCESS Variable service successfully initialized.\r | |
336 | \r | |
337 | **/\r | |
338 | EFI_STATUS\r | |
339 | EFIAPI\r | |
340 | VariableServiceInitialize (\r | |
341 | IN EFI_HANDLE ImageHandle,\r | |
342 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
343 | )\r | |
344 | {\r | |
345 | EFI_STATUS Status;\r | |
346 | EFI_EVENT ReadyToBootEvent; \r | |
347 | \r | |
348 | Status = VariableCommonInitialize ();\r | |
349 | ASSERT_EFI_ERROR (Status);\r | |
350 | \r | |
351 | SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r | |
352 | SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r | |
353 | SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r | |
354 | SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r | |
355 | \r | |
356 | //\r | |
357 | // Now install the Variable Runtime Architectural protocol on a new handle.\r | |
358 | //\r | |
359 | Status = gBS->InstallProtocolInterface (\r | |
360 | &mHandle,\r | |
361 | &gEfiVariableArchProtocolGuid, \r | |
362 | EFI_NATIVE_INTERFACE,\r | |
363 | NULL\r | |
364 | );\r | |
365 | ASSERT_EFI_ERROR (Status);\r | |
366 | \r | |
367 | //\r | |
368 | // Register FtwNotificationEvent () notify function.\r | |
369 | // \r | |
370 | EfiCreateProtocolNotifyEvent (\r | |
371 | &gEfiFaultTolerantWriteProtocolGuid,\r | |
372 | TPL_CALLBACK,\r | |
373 | FtwNotificationEvent,\r | |
374 | (VOID *)SystemTable,\r | |
375 | &mFtwRegistration\r | |
376 | );\r | |
377 | \r | |
378 | Status = gBS->CreateEventEx (\r | |
379 | EVT_NOTIFY_SIGNAL,\r | |
380 | TPL_NOTIFY,\r | |
381 | VariableClassAddressChangeEvent,\r | |
382 | NULL,\r | |
383 | &gEfiEventVirtualAddressChangeGuid,\r | |
384 | &mVirtualAddressChangeEvent\r | |
385 | );\r | |
386 | ASSERT_EFI_ERROR (Status);\r | |
387 | \r | |
388 | //\r | |
389 | // Register the event handling function to reclaim variable for OS usage.\r | |
390 | //\r | |
391 | Status = EfiCreateEventReadyToBootEx (\r | |
392 | TPL_NOTIFY, \r | |
393 | OnReadyToBoot, \r | |
394 | NULL, \r | |
395 | &ReadyToBootEvent\r | |
396 | );\r | |
397 | \r | |
398 | if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r | |
399 | gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r | |
400 | }\r | |
401 | return EFI_SUCCESS;\r | |
402 | }\r | |
403 | \r |