]>
Commit | Line | Data |
---|---|---|
25126a05 | 1 | /** @file\r |
2 | Report Status Code Router Driver which produces Report Stataus Code Handler Protocol\r | |
3 | and Status Code Runtime Protocol.\r | |
4 | \r | |
d1102dba | 5 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r |
7a7bedce | 6 | Copyright (c) Microsoft Corporation.<BR>\r |
9d510e61 | 7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
25126a05 | 8 | \r |
9 | **/\r | |
10 | \r | |
11 | #include "ReportStatusCodeRouterRuntimeDxe.h"\r | |
12 | \r | |
1436aea4 MK |
13 | EFI_HANDLE mHandle = NULL;\r |
14 | LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);\r | |
15 | EFI_EVENT mVirtualAddressChangeEvent = NULL;\r | |
25126a05 | 16 | \r |
17 | //\r | |
18 | // Report operation nest status.\r | |
19 | // If it is set, then the report operation has nested.\r | |
20 | //\r | |
1436aea4 | 21 | UINT32 mStatusCodeNestStatus = 0;\r |
25126a05 | 22 | \r |
1436aea4 | 23 | EFI_STATUS_CODE_PROTOCOL mStatusCodeProtocol = {\r |
25126a05 | 24 | ReportDispatcher\r |
25 | };\r | |
26 | \r | |
27 | EFI_RSC_HANDLER_PROTOCOL mRscHandlerProtocol = {\r | |
28 | Register,\r | |
29 | Unregister\r | |
1436aea4 | 30 | };\r |
25126a05 | 31 | \r |
cfc2ba61 | 32 | /**\r |
33 | Event callback function to invoke status code handler in list.\r | |
34 | \r | |
35 | @param Event Event whose notification function is being invoked.\r | |
36 | @param Context Pointer to the notification function's context, which is\r | |
37 | always zero in current implementation.\r | |
38 | \r | |
39 | **/\r | |
40 | VOID\r | |
41 | EFIAPI\r | |
42 | RscHandlerNotification (\r | |
1436aea4 MK |
43 | IN EFI_EVENT Event,\r |
44 | IN VOID *Context\r | |
cfc2ba61 | 45 | )\r |
46 | {\r | |
47 | RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r | |
48 | EFI_PHYSICAL_ADDRESS Address;\r | |
49 | RSC_DATA_ENTRY *RscData;\r | |
50 | \r | |
1436aea4 | 51 | CallbackEntry = (RSC_HANDLER_CALLBACK_ENTRY *)Context;\r |
cfc2ba61 | 52 | \r |
53 | //\r | |
54 | // Traverse the status code data buffer to parse all\r | |
55 | // data to report.\r | |
56 | //\r | |
57 | Address = CallbackEntry->StatusCodeDataBuffer;\r | |
58 | while (Address < CallbackEntry->EndPointer) {\r | |
1436aea4 | 59 | RscData = (RSC_DATA_ENTRY *)(UINTN)Address;\r |
cfc2ba61 | 60 | CallbackEntry->RscHandlerCallback (\r |
61 | RscData->Type,\r | |
62 | RscData->Value,\r | |
63 | RscData->Instance,\r | |
64 | &RscData->CallerId,\r | |
65 | &RscData->Data\r | |
66 | );\r | |
67 | \r | |
05db9401 | 68 | Address += (OFFSET_OF (RSC_DATA_ENTRY, Data) + RscData->Data.HeaderSize + RscData->Data.Size);\r |
cfc2ba61 | 69 | Address = ALIGN_VARIABLE (Address);\r |
70 | }\r | |
71 | \r | |
72 | CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;\r | |
73 | }\r | |
74 | \r | |
25126a05 | 75 | /**\r |
76 | Register the callback function for ReportStatusCode() notification.\r | |
d1102dba | 77 | \r |
25126a05 | 78 | When this function is called the function pointer is added to an internal list and any future calls to\r |
79 | ReportStatusCode() will be forwarded to the Callback function. During the bootservices,\r | |
80 | this is the callback for which this service can be invoked. The report status code router\r | |
81 | will create an event such that the callback function is only invoked at the TPL for which it was\r | |
82 | registered. The entity that registers for the callback should also register for an event upon\r | |
83 | generation of exit boot services and invoke the unregister service.\r | |
84 | If the handler does not have a TPL dependency, it should register for a callback at TPL high. The\r | |
85 | router infrastructure will support making callbacks at runtime, but the caller for runtime invocation\r | |
86 | must meet the following criteria:\r | |
87 | 1. must be a runtime driver type so that its memory is not reclaimed\r | |
88 | 2. not unregister at exit boot services so that the router will still have its callback address\r | |
89 | 3. the caller must be self-contained (eg. Not call out into any boot-service interfaces) and be\r | |
90 | runtime safe, in general.\r | |
d1102dba | 91 | \r |
25126a05 | 92 | @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is called when\r |
93 | a call to ReportStatusCode() occurs.\r | |
d1102dba LG |
94 | @param[in] Tpl TPL at which callback can be safely invoked.\r |
95 | \r | |
25126a05 | 96 | @retval EFI_SUCCESS Function was successfully registered.\r |
97 | @retval EFI_INVALID_PARAMETER The callback function was NULL.\r | |
98 | @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be\r | |
99 | registered.\r | |
100 | @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.\r | |
101 | \r | |
102 | **/\r | |
103 | EFI_STATUS\r | |
104 | EFIAPI\r | |
105 | Register (\r | |
1436aea4 MK |
106 | IN EFI_RSC_HANDLER_CALLBACK Callback,\r |
107 | IN EFI_TPL Tpl\r | |
25126a05 | 108 | )\r |
109 | {\r | |
1436aea4 MK |
110 | EFI_STATUS Status;\r |
111 | LIST_ENTRY *Link;\r | |
112 | RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r | |
25126a05 | 113 | \r |
114 | if (Callback == NULL) {\r | |
115 | return EFI_INVALID_PARAMETER;\r | |
116 | }\r | |
117 | \r | |
118 | for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r | |
119 | CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r | |
120 | if (CallbackEntry->RscHandlerCallback == Callback) {\r | |
121 | //\r | |
122 | // If the function was already registered. It can't be registered again.\r | |
123 | //\r | |
124 | return EFI_ALREADY_STARTED;\r | |
125 | }\r | |
126 | }\r | |
127 | \r | |
4e03ff80 | 128 | CallbackEntry = AllocateRuntimeZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY));\r |
25126a05 | 129 | ASSERT (CallbackEntry != NULL);\r |
130 | \r | |
131 | CallbackEntry->Signature = RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;\r | |
132 | CallbackEntry->RscHandlerCallback = Callback;\r | |
133 | CallbackEntry->Tpl = Tpl;\r | |
134 | \r | |
cfc2ba61 | 135 | //\r |
136 | // If TPL of registered callback funtion is not TPL_HIGH_LEVEL, then event should be created\r | |
137 | // for it, and related buffer for status code data should be prepared.\r | |
138 | // Here the data buffer must be prepared in advance, because Report Status Code Protocol might\r | |
139 | // be invoked under TPL_HIGH_LEVEL and no memory allocation is allowed then.\r | |
140 | // If TPL is TPL_HIGH_LEVEL, then all status code will be reported immediately, without data\r | |
141 | // buffer and event trigger.\r | |
142 | //\r | |
143 | if (Tpl != TPL_HIGH_LEVEL) {\r | |
1436aea4 | 144 | CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (EFI_PAGE_SIZE);\r |
cfc2ba61 | 145 | CallbackEntry->BufferSize = EFI_PAGE_SIZE;\r |
146 | CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;\r | |
1436aea4 MK |
147 | Status = gBS->CreateEvent (\r |
148 | EVT_NOTIFY_SIGNAL,\r | |
149 | Tpl,\r | |
150 | RscHandlerNotification,\r | |
151 | CallbackEntry,\r | |
152 | &CallbackEntry->Event\r | |
153 | );\r | |
cfc2ba61 | 154 | ASSERT_EFI_ERROR (Status);\r |
155 | }\r | |
156 | \r | |
25126a05 | 157 | InsertTailList (&mCallbackListHead, &CallbackEntry->Node);\r |
158 | \r | |
159 | return EFI_SUCCESS;\r | |
160 | }\r | |
161 | \r | |
162 | /**\r | |
163 | Remove a previously registered callback function from the notification list.\r | |
d1102dba | 164 | \r |
25126a05 | 165 | A callback function must be unregistered before it is deallocated. It is important that any registered\r |
166 | callbacks that are not runtime complaint be unregistered when ExitBootServices() is called.\r | |
d1102dba | 167 | \r |
25126a05 | 168 | @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be\r |
169 | unregistered.\r | |
d1102dba | 170 | \r |
25126a05 | 171 | @retval EFI_SUCCESS The function was successfully unregistered.\r |
172 | @retval EFI_INVALID_PARAMETER The callback function was NULL.\r | |
173 | @retval EFI_NOT_FOUND The callback function was not found to be unregistered.\r | |
174 | \r | |
175 | **/\r | |
176 | EFI_STATUS\r | |
177 | EFIAPI\r | |
178 | Unregister (\r | |
1436aea4 | 179 | IN EFI_RSC_HANDLER_CALLBACK Callback\r |
25126a05 | 180 | )\r |
181 | {\r | |
1436aea4 MK |
182 | LIST_ENTRY *Link;\r |
183 | RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r | |
25126a05 | 184 | \r |
185 | if (Callback == NULL) {\r | |
186 | return EFI_INVALID_PARAMETER;\r | |
187 | }\r | |
188 | \r | |
189 | for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r | |
190 | CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r | |
191 | if (CallbackEntry->RscHandlerCallback == Callback) {\r | |
192 | //\r | |
193 | // If the function is found in list, delete it and return.\r | |
194 | //\r | |
cfc2ba61 | 195 | if (CallbackEntry->Tpl != TPL_HIGH_LEVEL) {\r |
1436aea4 | 196 | FreePool ((VOID *)(UINTN)CallbackEntry->StatusCodeDataBuffer);\r |
cfc2ba61 | 197 | gBS->CloseEvent (CallbackEntry->Event);\r |
198 | }\r | |
1436aea4 | 199 | \r |
25126a05 | 200 | RemoveEntryList (&CallbackEntry->Node);\r |
201 | FreePool (CallbackEntry);\r | |
202 | return EFI_SUCCESS;\r | |
203 | }\r | |
204 | }\r | |
205 | \r | |
206 | return EFI_NOT_FOUND;\r | |
207 | }\r | |
208 | \r | |
25126a05 | 209 | /**\r |
210 | Provides an interface that a software module can call to report a status code.\r | |
211 | \r | |
212 | @param Type Indicates the type of status code being reported.\r | |
213 | @param Value Describes the current status of a hardware or software entity.\r | |
214 | This included information about the class and subclass that is used to\r | |
215 | classify the entity as well as an operation.\r | |
216 | @param Instance The enumeration of a hardware or software entity within\r | |
217 | the system. Valid instance numbers start with 1.\r | |
218 | @param CallerId This optional parameter may be used to identify the caller.\r | |
219 | This parameter allows the status code driver to apply different rules to\r | |
220 | different callers.\r | |
221 | @param Data This optional parameter may be used to pass additional data.\r | |
222 | \r | |
223 | @retval EFI_SUCCESS The function completed successfully\r | |
224 | @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.\r | |
225 | \r | |
226 | **/\r | |
227 | EFI_STATUS\r | |
228 | EFIAPI\r | |
229 | ReportDispatcher (\r | |
1436aea4 MK |
230 | IN EFI_STATUS_CODE_TYPE Type,\r |
231 | IN EFI_STATUS_CODE_VALUE Value,\r | |
232 | IN UINT32 Instance,\r | |
233 | IN EFI_GUID *CallerId OPTIONAL,\r | |
234 | IN EFI_STATUS_CODE_DATA *Data OPTIONAL\r | |
25126a05 | 235 | )\r |
236 | {\r | |
1436aea4 MK |
237 | LIST_ENTRY *Link;\r |
238 | RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r | |
239 | RSC_DATA_ENTRY *RscData;\r | |
240 | EFI_STATUS Status;\r | |
241 | VOID *NewBuffer;\r | |
242 | EFI_PHYSICAL_ADDRESS FailSafeEndPointer;\r | |
25126a05 | 243 | \r |
244 | //\r | |
245 | // Use atom operation to avoid the reentant of report.\r | |
246 | // If current status is not zero, then the function is reentrancy.\r | |
247 | //\r | |
248 | if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {\r | |
249 | return EFI_DEVICE_ERROR;\r | |
250 | }\r | |
251 | \r | |
04bbf1f3 | 252 | for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link);) {\r |
25126a05 | 253 | CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r |
04bbf1f3 | 254 | //\r |
255 | // The handler may remove itself, so get the next handler in advance.\r | |
256 | //\r | |
257 | Link = GetNextNode (&mCallbackListHead, Link);\r | |
25126a05 | 258 | if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {\r |
259 | CallbackEntry->RscHandlerCallback (\r | |
260 | Type,\r | |
261 | Value,\r | |
262 | Instance,\r | |
263 | CallerId,\r | |
264 | Data\r | |
265 | );\r | |
266 | continue;\r | |
267 | }\r | |
268 | \r | |
cfc2ba61 | 269 | //\r |
270 | // If callback is registered with TPL lower than TPL_HIGH_LEVEL, event must be signaled at boot time to possibly wait for\r | |
271 | // allowed TPL to report status code. Related data should also be stored in data buffer.\r | |
272 | //\r | |
1436aea4 | 273 | FailSafeEndPointer = CallbackEntry->EndPointer;\r |
cfc2ba61 | 274 | CallbackEntry->EndPointer = ALIGN_VARIABLE (CallbackEntry->EndPointer);\r |
1436aea4 | 275 | RscData = (RSC_DATA_ENTRY *)(UINTN)CallbackEntry->EndPointer;\r |
cfc2ba61 | 276 | CallbackEntry->EndPointer += sizeof (RSC_DATA_ENTRY);\r |
277 | if (Data != NULL) {\r | |
05db9401 | 278 | CallbackEntry->EndPointer += (Data->Size + Data->HeaderSize - sizeof (EFI_STATUS_CODE_DATA));\r |
cfc2ba61 | 279 | }\r |
25126a05 | 280 | \r |
cfc2ba61 | 281 | //\r |
282 | // If data buffer is about to be used up (7/8 here), try to reallocate a buffer with double size, if not at TPL_HIGH_LEVEL.\r | |
283 | //\r | |
284 | if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + (CallbackEntry->BufferSize / 8) * 7)) {\r | |
285 | if (EfiGetCurrentTpl () < TPL_HIGH_LEVEL) {\r | |
286 | NewBuffer = ReallocatePool (\r | |
287 | CallbackEntry->BufferSize,\r | |
288 | CallbackEntry->BufferSize * 2,\r | |
1436aea4 | 289 | (VOID *)(UINTN)CallbackEntry->StatusCodeDataBuffer\r |
cfc2ba61 | 290 | );\r |
291 | if (NewBuffer != NULL) {\r | |
1436aea4 MK |
292 | FailSafeEndPointer = (EFI_PHYSICAL_ADDRESS)(UINTN)NewBuffer + (FailSafeEndPointer - CallbackEntry->StatusCodeDataBuffer);\r |
293 | CallbackEntry->EndPointer = (EFI_PHYSICAL_ADDRESS)(UINTN)NewBuffer + (CallbackEntry->EndPointer - CallbackEntry->StatusCodeDataBuffer);\r | |
294 | RscData = (RSC_DATA_ENTRY *)(UINTN)((UINTN)NewBuffer + ((UINTN)RscData - CallbackEntry->StatusCodeDataBuffer));\r | |
295 | CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS)(UINTN)NewBuffer;\r | |
296 | CallbackEntry->BufferSize *= 2;\r | |
cfc2ba61 | 297 | }\r |
298 | }\r | |
299 | }\r | |
25126a05 | 300 | \r |
cfc2ba61 | 301 | //\r |
302 | // If data buffer is used up, do not report for this time.\r | |
303 | //\r | |
304 | if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + CallbackEntry->BufferSize)) {\r | |
1a258c77 | 305 | CallbackEntry->EndPointer = FailSafeEndPointer;\r |
cfc2ba61 | 306 | continue;\r |
307 | }\r | |
308 | \r | |
1436aea4 MK |
309 | RscData->Type = Type;\r |
310 | RscData->Value = Value;\r | |
311 | RscData->Instance = Instance;\r | |
cfc2ba61 | 312 | if (CallerId != NULL) {\r |
313 | CopyGuid (&RscData->CallerId, CallerId);\r | |
314 | }\r | |
1436aea4 | 315 | \r |
cfc2ba61 | 316 | if (Data != NULL) {\r |
317 | CopyMem (&RscData->Data, Data, Data->HeaderSize + Data->Size);\r | |
7a7bedce MK |
318 | } else {\r |
319 | ZeroMem (&RscData->Data, sizeof (RscData->Data));\r | |
320 | RscData->Data.HeaderSize = sizeof (RscData->Data);\r | |
cfc2ba61 | 321 | }\r |
25126a05 | 322 | \r |
cfc2ba61 | 323 | Status = gBS->SignalEvent (CallbackEntry->Event);\r |
25126a05 | 324 | ASSERT_EFI_ERROR (Status);\r |
325 | }\r | |
326 | \r | |
327 | //\r | |
328 | // Restore the nest status of report\r | |
329 | //\r | |
330 | InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);\r | |
331 | \r | |
332 | return EFI_SUCCESS;\r | |
333 | }\r | |
334 | \r | |
335 | /**\r | |
336 | Virtual address change notification call back. It converts global pointer\r | |
337 | to virtual address.\r | |
338 | \r | |
339 | @param Event Event whose notification function is being invoked.\r | |
340 | @param Context Pointer to the notification function's context, which is\r | |
341 | always zero in current implementation.\r | |
342 | \r | |
343 | **/\r | |
344 | VOID\r | |
345 | EFIAPI\r | |
346 | VirtualAddressChangeCallBack (\r | |
1436aea4 MK |
347 | IN EFI_EVENT Event,\r |
348 | IN VOID *Context\r | |
25126a05 | 349 | )\r |
350 | {\r | |
1436aea4 MK |
351 | EFI_STATUS Status;\r |
352 | LIST_ENTRY *Link;\r | |
353 | RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r | |
25126a05 | 354 | \r |
355 | for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r | |
356 | CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r | |
1436aea4 | 357 | Status = EfiConvertFunctionPointer (0, (VOID **)&CallbackEntry->RscHandlerCallback);\r |
25126a05 | 358 | ASSERT_EFI_ERROR (Status);\r |
359 | }\r | |
360 | \r | |
361 | Status = EfiConvertList (\r | |
362 | 0,\r | |
363 | &mCallbackListHead\r | |
364 | );\r | |
365 | ASSERT_EFI_ERROR (Status);\r | |
366 | }\r | |
367 | \r | |
368 | /**\r | |
369 | Entry point of Generic Status Code Driver.\r | |
370 | \r | |
371 | This function is the entry point of this Generic Status Code Driver.\r | |
372 | It installs eport Stataus Code Handler Protocol and Status Code Runtime Protocol,\r | |
373 | and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
374 | \r | |
375 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
376 | @param SystemTable A pointer to the EFI System Table.\r | |
d1102dba | 377 | \r |
25126a05 | 378 | @retval EFI_SUCCESS The entry point is executed successfully.\r |
379 | \r | |
380 | **/\r | |
381 | EFI_STATUS\r | |
382 | EFIAPI\r | |
383 | GenericStatusCodeRuntimeDxeEntry (\r | |
1436aea4 MK |
384 | IN EFI_HANDLE ImageHandle,\r |
385 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
25126a05 | 386 | )\r |
387 | {\r | |
1436aea4 | 388 | EFI_STATUS Status;\r |
25126a05 | 389 | \r |
390 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
391 | &mHandle,\r | |
392 | &gEfiRscHandlerProtocolGuid,\r | |
393 | &mRscHandlerProtocol,\r | |
394 | &gEfiStatusCodeRuntimeProtocolGuid,\r | |
395 | &mStatusCodeProtocol,\r | |
396 | NULL\r | |
397 | );\r | |
398 | ASSERT_EFI_ERROR (Status);\r | |
399 | \r | |
400 | Status = gBS->CreateEventEx (\r | |
401 | EVT_NOTIFY_SIGNAL,\r | |
402 | TPL_NOTIFY,\r | |
403 | VirtualAddressChangeCallBack,\r | |
404 | NULL,\r | |
405 | &gEfiEventVirtualAddressChangeGuid,\r | |
406 | &mVirtualAddressChangeEvent\r | |
407 | );\r | |
408 | ASSERT_EFI_ERROR (Status);\r | |
409 | \r | |
410 | return EFI_SUCCESS;\r | |
411 | }\r |