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