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