2 Register a status code handler for printing the Boot Manager's LoadImage()
3 and StartImage() preparations, and return codes, to the UEFI console.
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
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.
16 Copyright (C) 2019, Red Hat, Inc.
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
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.
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>
38 #include <Protocol/ReportStatusCodeHandler.h>
40 #include <Guid/GlobalVariable.h>
41 #include <Guid/StatusCodeDataTypeId.h>
43 #include <Pi/PiStatusCode.h>
47 // Convenience variables for the status codes that are relevant for LoadImage()
48 // and StartImage() preparations and return codes.
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
;
57 Handle status codes reported through ReportStatusCodeLib /
58 EFI_STATUS_CODE_PROTOCOL.ReportStatusCode(). Format matching status codes to
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.
65 The parameter list of this function precisely matches that of
66 EFI_STATUS_CODE_PROTOCOL.ReportStatusCode().
68 The return status of this function is ignored by the caller, but the function
69 still returns sensible codes:
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.
75 @retval EFI_INVALID_PARAMETER Unknown or malformed contents have been
78 @retval EFI_INCOMPATIBLE_VERSION Unexpected UEFI variable behavior has been
81 @return Error codes propagated from underlying
88 IN EFI_STATUS_CODE_TYPE CodeType
,
89 IN EFI_STATUS_CODE_VALUE Value
,
91 IN EFI_GUID
*CallerId
,
92 IN EFI_STATUS_CODE_DATA
*Data
98 CHAR16 BootOptionName
[ARRAY_SIZE (L
"Boot####")];
99 EFI_BOOT_MANAGER_LOAD_OPTION BmBootOption
;
100 BOOLEAN DevPathStringIsDynamic
;
101 CHAR16
*DevPathString
;
104 // Ignore all status codes that are irrelevant for LoadImage() and
105 // StartImage() preparations and return codes.
107 if (Value
!= mLoadPrep
&& Value
!= mLoadFail
&&
108 Value
!= mStartPrep
&& Value
!= mStartFail
) {
112 // Ignore status codes that are not reported by the same containing module.
114 if (!CompareGuid (CallerId
, &gEfiCallerIdGuid
)) {
119 // Sanity-check Data in case of failure reports.
121 if ((Value
== mLoadFail
|| Value
== mStartFail
) &&
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
,
128 return EFI_INVALID_PARAMETER
;
132 // Get the number of the Boot#### option that the status code applies to.
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
));
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
;
153 // Get the Boot#### option that the status code applies to.
155 UnicodeSPrint (BootOptionName
, sizeof BootOptionName
, L
"Boot%04x",
157 Status
= EfiBootManagerVariableToLoadOption (BootOptionName
, &BmBootOption
);
158 if (EFI_ERROR (Status
)) {
160 "%a:%a: EfiBootManagerVariableToLoadOption(\"%s\"): %r\n",
161 gEfiCallerBaseName
, __FUNCTION__
, BootOptionName
, Status
));
166 // Format the device path.
168 DevPathStringIsDynamic
= TRUE
;
169 DevPathString
= ConvertDevicePathToText (
170 BmBootOption
.FilePath
,
171 FALSE
, // DisplayOnly
172 FALSE
// AllowShortcuts
174 if (DevPathString
== NULL
) {
175 DevPathStringIsDynamic
= FALSE
;
176 DevPathString
= L
"<out of memory while formatting device path>";
180 // Print the message to the console.
182 if (Value
== mLoadPrep
|| Value
== mStartPrep
) {
184 L
"%a: %a %s \"%s\" from %s\n",
186 Value
== mLoadPrep
? "loading" : "starting",
188 BmBootOption
.Description
,
193 L
"%a: failed to %a %s \"%s\" from %s: %r\n",
195 Value
== mLoadFail
? "load" : "start",
197 BmBootOption
.Description
,
199 ((EFI_RETURN_STATUS_EXTENDED_DATA
*)Data
)->ReturnStatus
206 if (DevPathStringIsDynamic
) {
207 FreePool (DevPathString
);
209 EfiBootManagerFreeLoadOption (&BmBootOption
);
215 Unregister HandleStatusCode() at ExitBootServices().
217 (See EFI_RSC_HANDLER_PROTOCOL in Volume 3 of the Platform Init spec.)
219 @param[in] Event Event whose notification function is being invoked.
221 @param[in] Context Pointer to EFI_RSC_HANDLER_PROTOCOL, originally looked up
222 when HandleStatusCode() was registered.
227 UnregisterAtExitBootServices (
232 EFI_RSC_HANDLER_PROTOCOL
*StatusCodeRouter
;
234 StatusCodeRouter
= Context
;
235 StatusCodeRouter
->Unregister (HandleStatusCode
);
240 Register a status code handler for printing the Boot Manager's LoadImage()
241 and StartImage() preparations, and return codes, to the UEFI console.
243 @retval EFI_SUCCESS The status code handler has been successfully
246 @return Error codes propagated from boot services and from
247 EFI_RSC_HANDLER_PROTOCOL.
251 PlatformBmPrintScRegisterHandler (
256 EFI_RSC_HANDLER_PROTOCOL
*StatusCodeRouter
;
257 EFI_EVENT ExitBootEvent
;
259 Status
= gBS
->LocateProtocol (&gEfiRscHandlerProtocolGuid
,
260 NULL
/* Registration */, (VOID
**)&StatusCodeRouter
);
261 ASSERT_EFI_ERROR (Status
);
262 if (EFI_ERROR (Status
)) {
267 // Set the EFI_STATUS_CODE_VALUE convenience variables.
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
);
277 // Register the handler callback.
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
));
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.)
292 Status
= gBS
->CreateEvent (
293 EVT_SIGNAL_EXIT_BOOT_SERVICES
, // Type
294 TPL_CALLBACK
, // NotifyTpl
295 UnregisterAtExitBootServices
, // NotifyFunction
296 StatusCodeRouter
, // NotifyContext
297 &ExitBootEvent
// Event
299 if (EFI_ERROR (Status
)) {
301 // We have to unregister the callback right now, and fail the function.
303 DEBUG ((DEBUG_ERROR
, "%a:%a: failed to create ExitBootServices() event: "
304 "%r\n", gEfiCallerBaseName
, __FUNCTION__
, Status
));
305 StatusCodeRouter
->Unregister (HandleStatusCode
);