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