]>
Commit | Line | Data |
---|---|---|
77874cee LE |
1 | /** @file\r |
2 | Register a status code handler for printing the Boot Manager's LoadImage()\r | |
3 | and StartImage() preparations, and return codes, to the UEFI console.\r | |
4 | \r | |
5 | This feature enables users that are not accustomed to analyzing the firmware\r | |
6 | log to glean some information about UEFI boot option processing (loading and\r | |
7 | starting).\r | |
8 | \r | |
9 | This library instance filters out (ignores) status codes that are not\r | |
10 | reported by the containing firmware module. The intent is to link this\r | |
11 | library instance into BdsDxe via PlatformBootManagerLib (which BdsDxe depends\r | |
12 | upon), then catch only those status codes that BdsDxe reports (which happens\r | |
13 | via UefiBootManagerLib). Status codes reported by other modules (such as\r | |
14 | UiApp), via UefiBootManagerLib or otherwise, are meant to be ignored.\r | |
15 | \r | |
16 | Copyright (C) 2019, Red Hat, Inc.\r | |
17 | \r | |
b26f0cf9 | 18 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
77874cee LE |
19 | **/\r |
20 | \r | |
21 | #include <Library/BaseMemoryLib.h>\r | |
22 | #include <Library/DebugLib.h>\r | |
23 | #include <Library/DevicePathLib.h>\r | |
24 | #include <Library/MemoryAllocationLib.h>\r | |
25 | #include <Library/PcdLib.h>\r | |
26 | #include <Library/PrintLib.h>\r | |
27 | #include <Library/UefiBootManagerLib.h>\r | |
28 | #include <Library/UefiBootServicesTableLib.h>\r | |
29 | #include <Library/UefiLib.h>\r | |
30 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
31 | \r | |
32 | #include <Protocol/ReportStatusCodeHandler.h>\r | |
33 | \r | |
34 | #include <Guid/GlobalVariable.h>\r | |
35 | #include <Guid/StatusCodeDataTypeId.h>\r | |
36 | \r | |
37 | #include <Pi/PiStatusCode.h>\r | |
38 | \r | |
39 | \r | |
40 | //\r | |
41 | // Convenience variables for the status codes that are relevant for LoadImage()\r | |
42 | // and StartImage() preparations and return codes.\r | |
43 | //\r | |
44 | STATIC EFI_STATUS_CODE_VALUE mLoadPrep;\r | |
45 | STATIC EFI_STATUS_CODE_VALUE mLoadFail;\r | |
46 | STATIC EFI_STATUS_CODE_VALUE mStartPrep;\r | |
47 | STATIC EFI_STATUS_CODE_VALUE mStartFail;\r | |
48 | \r | |
49 | \r | |
50 | /**\r | |
51 | Handle status codes reported through ReportStatusCodeLib /\r | |
52 | EFI_STATUS_CODE_PROTOCOL.ReportStatusCode(). Format matching status codes to\r | |
53 | the system console.\r | |
54 | \r | |
55 | The highest TPL at which this handler can be registered with\r | |
56 | EFI_RSC_HANDLER_PROTOCOL.Register() is TPL_CALLBACK. That's because\r | |
57 | HandleStatusCode() uses the UEFI variable services.\r | |
58 | \r | |
59 | The parameter list of this function precisely matches that of\r | |
60 | EFI_STATUS_CODE_PROTOCOL.ReportStatusCode().\r | |
61 | \r | |
62 | The return status of this function is ignored by the caller, but the function\r | |
63 | still returns sensible codes:\r | |
64 | \r | |
65 | @retval EFI_SUCCESS The status code has been processed; either\r | |
66 | as a no-op, due to filtering, or by\r | |
67 | formatting it to the system console.\r | |
68 | \r | |
69 | @retval EFI_INVALID_PARAMETER Unknown or malformed contents have been\r | |
70 | detected in Data.\r | |
71 | \r | |
72 | @retval EFI_INCOMPATIBLE_VERSION Unexpected UEFI variable behavior has been\r | |
73 | encountered.\r | |
74 | \r | |
75 | @return Error codes propagated from underlying\r | |
76 | services.\r | |
77 | **/\r | |
78 | STATIC\r | |
79 | EFI_STATUS\r | |
80 | EFIAPI\r | |
81 | HandleStatusCode (\r | |
82 | IN EFI_STATUS_CODE_TYPE CodeType,\r | |
83 | IN EFI_STATUS_CODE_VALUE Value,\r | |
84 | IN UINT32 Instance,\r | |
85 | IN EFI_GUID *CallerId,\r | |
86 | IN EFI_STATUS_CODE_DATA *Data\r | |
87 | )\r | |
88 | {\r | |
89 | UINTN VariableSize;\r | |
90 | UINT16 BootCurrent;\r | |
91 | EFI_STATUS Status;\r | |
92 | CHAR16 BootOptionName[ARRAY_SIZE (L"Boot####")];\r | |
93 | EFI_BOOT_MANAGER_LOAD_OPTION BmBootOption;\r | |
94 | BOOLEAN DevPathStringIsDynamic;\r | |
95 | CHAR16 *DevPathString;\r | |
96 | \r | |
97 | //\r | |
98 | // Ignore all status codes that are irrelevant for LoadImage() and\r | |
99 | // StartImage() preparations and return codes.\r | |
100 | //\r | |
101 | if (Value != mLoadPrep && Value != mLoadFail &&\r | |
102 | Value != mStartPrep && Value != mStartFail) {\r | |
103 | return EFI_SUCCESS;\r | |
104 | }\r | |
105 | //\r | |
106 | // Ignore status codes that are not reported by the same containing module.\r | |
107 | //\r | |
108 | if (!CompareGuid (CallerId, &gEfiCallerIdGuid)) {\r | |
109 | return EFI_SUCCESS;\r | |
110 | }\r | |
111 | \r | |
112 | //\r | |
113 | // Sanity-check Data in case of failure reports.\r | |
114 | //\r | |
115 | if ((Value == mLoadFail || Value == mStartFail) &&\r | |
116 | (Data == NULL ||\r | |
117 | Data->HeaderSize != sizeof *Data ||\r | |
118 | Data->Size != sizeof (EFI_RETURN_STATUS_EXTENDED_DATA) - sizeof *Data ||\r | |
119 | !CompareGuid (&Data->Type, &gEfiStatusCodeSpecificDataGuid))) {\r | |
120 | DEBUG ((DEBUG_ERROR, "%a:%a: malformed Data\n", gEfiCallerBaseName,\r | |
121 | __FUNCTION__));\r | |
122 | return EFI_INVALID_PARAMETER;\r | |
123 | }\r | |
124 | \r | |
125 | //\r | |
126 | // Get the number of the Boot#### option that the status code applies to.\r | |
127 | //\r | |
128 | VariableSize = sizeof BootCurrent;\r | |
129 | Status = gRT->GetVariable (EFI_BOOT_CURRENT_VARIABLE_NAME,\r | |
130 | &gEfiGlobalVariableGuid, NULL /* Attributes */,\r | |
131 | &VariableSize, &BootCurrent);\r | |
132 | if (EFI_ERROR (Status)) {\r | |
133 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to get %g:\"%s\": %r\n",\r | |
134 | gEfiCallerBaseName, __FUNCTION__, &gEfiGlobalVariableGuid,\r | |
135 | EFI_BOOT_CURRENT_VARIABLE_NAME, Status));\r | |
136 | return Status;\r | |
137 | }\r | |
138 | if (VariableSize != sizeof BootCurrent) {\r | |
139 | DEBUG ((DEBUG_ERROR, "%a:%a: got %Lu bytes for %g:\"%s\", expected %Lu\n",\r | |
140 | gEfiCallerBaseName, __FUNCTION__, (UINT64)VariableSize,\r | |
141 | &gEfiGlobalVariableGuid, EFI_BOOT_CURRENT_VARIABLE_NAME,\r | |
142 | (UINT64)sizeof BootCurrent));\r | |
143 | return EFI_INCOMPATIBLE_VERSION;\r | |
144 | }\r | |
145 | \r | |
146 | //\r | |
147 | // Get the Boot#### option that the status code applies to.\r | |
148 | //\r | |
149 | UnicodeSPrint (BootOptionName, sizeof BootOptionName, L"Boot%04x",\r | |
150 | BootCurrent);\r | |
151 | Status = EfiBootManagerVariableToLoadOption (BootOptionName, &BmBootOption);\r | |
152 | if (EFI_ERROR (Status)) {\r | |
153 | DEBUG ((DEBUG_ERROR,\r | |
154 | "%a:%a: EfiBootManagerVariableToLoadOption(\"%s\"): %r\n",\r | |
155 | gEfiCallerBaseName, __FUNCTION__, BootOptionName, Status));\r | |
156 | return Status;\r | |
157 | }\r | |
158 | \r | |
159 | //\r | |
160 | // Format the device path.\r | |
161 | //\r | |
162 | DevPathStringIsDynamic = TRUE;\r | |
163 | DevPathString = ConvertDevicePathToText (\r | |
164 | BmBootOption.FilePath,\r | |
165 | FALSE, // DisplayOnly\r | |
166 | FALSE // AllowShortcuts\r | |
167 | );\r | |
168 | if (DevPathString == NULL) {\r | |
169 | DevPathStringIsDynamic = FALSE;\r | |
170 | DevPathString = L"<out of memory while formatting device path>";\r | |
171 | }\r | |
172 | \r | |
173 | //\r | |
174 | // Print the message to the console.\r | |
175 | //\r | |
176 | if (Value == mLoadPrep || Value == mStartPrep) {\r | |
177 | Print (\r | |
178 | L"%a: %a %s \"%s\" from %s\n",\r | |
179 | gEfiCallerBaseName,\r | |
180 | Value == mLoadPrep ? "loading" : "starting",\r | |
181 | BootOptionName,\r | |
182 | BmBootOption.Description,\r | |
183 | DevPathString\r | |
184 | );\r | |
185 | } else {\r | |
186 | Print (\r | |
187 | L"%a: failed to %a %s \"%s\" from %s: %r\n",\r | |
188 | gEfiCallerBaseName,\r | |
189 | Value == mLoadFail ? "load" : "start",\r | |
190 | BootOptionName,\r | |
191 | BmBootOption.Description,\r | |
192 | DevPathString,\r | |
193 | ((EFI_RETURN_STATUS_EXTENDED_DATA *)Data)->ReturnStatus\r | |
194 | );\r | |
195 | }\r | |
196 | \r | |
197 | //\r | |
198 | // Done.\r | |
199 | //\r | |
200 | if (DevPathStringIsDynamic) {\r | |
201 | FreePool (DevPathString);\r | |
202 | }\r | |
203 | EfiBootManagerFreeLoadOption (&BmBootOption);\r | |
204 | return EFI_SUCCESS;\r | |
205 | }\r | |
206 | \r | |
207 | \r | |
208 | /**\r | |
209 | Unregister HandleStatusCode() at ExitBootServices().\r | |
210 | \r | |
211 | (See EFI_RSC_HANDLER_PROTOCOL in Volume 3 of the Platform Init spec.)\r | |
212 | \r | |
213 | @param[in] Event Event whose notification function is being invoked.\r | |
214 | \r | |
215 | @param[in] Context Pointer to EFI_RSC_HANDLER_PROTOCOL, originally looked up\r | |
216 | when HandleStatusCode() was registered.\r | |
217 | **/\r | |
218 | STATIC\r | |
219 | VOID\r | |
220 | EFIAPI\r | |
221 | UnregisterAtExitBootServices (\r | |
222 | IN EFI_EVENT Event,\r | |
223 | IN VOID *Context\r | |
224 | )\r | |
225 | {\r | |
226 | EFI_RSC_HANDLER_PROTOCOL *StatusCodeRouter;\r | |
227 | \r | |
228 | StatusCodeRouter = Context;\r | |
229 | StatusCodeRouter->Unregister (HandleStatusCode);\r | |
230 | }\r | |
231 | \r | |
232 | \r | |
233 | /**\r | |
234 | Register a status code handler for printing the Boot Manager's LoadImage()\r | |
235 | and StartImage() preparations, and return codes, to the UEFI console.\r | |
236 | \r | |
237 | @retval EFI_SUCCESS The status code handler has been successfully\r | |
238 | registered.\r | |
239 | \r | |
240 | @return Error codes propagated from boot services and from\r | |
241 | EFI_RSC_HANDLER_PROTOCOL.\r | |
242 | **/\r | |
243 | EFI_STATUS\r | |
244 | EFIAPI\r | |
245 | PlatformBmPrintScRegisterHandler (\r | |
246 | VOID\r | |
247 | )\r | |
248 | {\r | |
249 | EFI_STATUS Status;\r | |
250 | EFI_RSC_HANDLER_PROTOCOL *StatusCodeRouter;\r | |
251 | EFI_EVENT ExitBootEvent;\r | |
252 | \r | |
253 | Status = gBS->LocateProtocol (&gEfiRscHandlerProtocolGuid,\r | |
254 | NULL /* Registration */, (VOID **)&StatusCodeRouter);\r | |
255 | ASSERT_EFI_ERROR (Status);\r | |
256 | if (EFI_ERROR (Status)) {\r | |
257 | return Status;\r | |
258 | }\r | |
259 | \r | |
260 | //\r | |
261 | // Set the EFI_STATUS_CODE_VALUE convenience variables.\r | |
262 | //\r | |
263 | mLoadPrep = PcdGet32 (PcdProgressCodeOsLoaderLoad);\r | |
264 | mLoadFail = (EFI_SOFTWARE_DXE_BS_DRIVER |\r | |
265 | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR);\r | |
266 | mStartPrep = PcdGet32 (PcdProgressCodeOsLoaderStart);\r | |
267 | mStartFail = (EFI_SOFTWARE_DXE_BS_DRIVER |\r | |
268 | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED);\r | |
269 | \r | |
270 | //\r | |
271 | // Register the handler callback.\r | |
272 | //\r | |
273 | Status = StatusCodeRouter->Register (HandleStatusCode, TPL_CALLBACK);\r | |
274 | if (EFI_ERROR (Status)) {\r | |
275 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to register status code handler: %r\n",\r | |
276 | gEfiCallerBaseName, __FUNCTION__, Status));\r | |
277 | return Status;\r | |
278 | }\r | |
279 | \r | |
280 | //\r | |
281 | // Status code reporting and routing/handling extend into OS runtime. Since\r | |
282 | // we don't want our handler to survive the BDS phase, we have to unregister\r | |
283 | // the callback at ExitBootServices(). (See EFI_RSC_HANDLER_PROTOCOL in\r | |
284 | // Volume 3 of the Platform Init spec.)\r | |
285 | //\r | |
286 | Status = gBS->CreateEvent (\r | |
287 | EVT_SIGNAL_EXIT_BOOT_SERVICES, // Type\r | |
288 | TPL_CALLBACK, // NotifyTpl\r | |
289 | UnregisterAtExitBootServices, // NotifyFunction\r | |
290 | StatusCodeRouter, // NotifyContext\r | |
291 | &ExitBootEvent // Event\r | |
292 | );\r | |
293 | if (EFI_ERROR (Status)) {\r | |
294 | //\r | |
295 | // We have to unregister the callback right now, and fail the function.\r | |
296 | //\r | |
297 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to create ExitBootServices() event: "\r | |
298 | "%r\n", gEfiCallerBaseName, __FUNCTION__, Status));\r | |
299 | StatusCodeRouter->Unregister (HandleStatusCode);\r | |
300 | return Status;\r | |
301 | }\r | |
302 | \r | |
303 | return EFI_SUCCESS;\r | |
304 | }\r |