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