3 * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BdsInternal.h"
17 #include <Library/PcdLib.h>
18 #include <Library/PerformanceLib.h>
20 #include <Protocol/Bds.h>
22 #include <Guid/EventGroup.h>
24 #define EFI_SET_TIMER_TO_SECOND 10000000
28 GetConsoleDevicePathFromVariable (
29 IN CHAR16
* ConsoleVarName
,
30 IN CHAR16
* DefaultConsolePaths
,
31 OUT EFI_DEVICE_PATH
** DevicePaths
36 EFI_DEVICE_PATH_PROTOCOL
* DevicePathInstances
;
37 EFI_DEVICE_PATH_PROTOCOL
* DevicePathInstance
;
38 CHAR16
* DevicePathStr
;
39 CHAR16
* NextDevicePathStr
;
40 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
*EfiDevicePathFromTextProtocol
;
42 Status
= GetGlobalEnvironmentVariable (ConsoleVarName
, NULL
, NULL
, (VOID
**)&DevicePathInstances
);
43 if (EFI_ERROR(Status
)) {
44 // In case no default console device path has been defined we assume a driver handles the console (eg: SimpleTextInOutSerial)
45 if ((DefaultConsolePaths
== NULL
) || (DefaultConsolePaths
[0] == L
'\0')) {
50 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
51 ASSERT_EFI_ERROR(Status
);
53 DevicePathInstances
= NULL
;
55 // Extract the Device Path instances from the multi-device path string
56 while ((DefaultConsolePaths
!= NULL
) && (DefaultConsolePaths
[0] != L
'\0')) {
57 NextDevicePathStr
= StrStr (DefaultConsolePaths
, L
";");
58 if (NextDevicePathStr
== NULL
) {
59 DevicePathStr
= DefaultConsolePaths
;
60 DefaultConsolePaths
= NULL
;
62 DevicePathStr
= (CHAR16
*)AllocateCopyPool ((NextDevicePathStr
- DefaultConsolePaths
+ 1) * sizeof(CHAR16
), DefaultConsolePaths
);
63 *(DevicePathStr
+ (NextDevicePathStr
- DefaultConsolePaths
)) = L
'\0';
64 DefaultConsolePaths
= NextDevicePathStr
;
65 if (DefaultConsolePaths
[0] == L
';') {
66 DefaultConsolePaths
++;
70 DevicePathInstance
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath (DevicePathStr
);
71 ASSERT(DevicePathInstance
!= NULL
);
72 DevicePathInstances
= AppendDevicePathInstance (DevicePathInstances
, DevicePathInstance
);
74 if (NextDevicePathStr
!= NULL
) {
75 FreePool (DevicePathStr
);
77 FreePool (DevicePathInstance
);
80 // Set the environment variable with this device path multi-instances
81 Size
= GetDevicePathSize (DevicePathInstances
);
85 &gEfiGlobalVariableGuid
,
86 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
91 Status
= EFI_INVALID_PARAMETER
;
95 if (!EFI_ERROR(Status
)) {
96 *DevicePaths
= DevicePathInstances
;
103 InitializeConsolePipe (
104 IN EFI_DEVICE_PATH
*ConsoleDevicePaths
,
105 IN EFI_GUID
*Protocol
,
106 OUT EFI_HANDLE
*Handle
,
114 EFI_DEVICE_PATH_PROTOCOL
* DevicePath
;
116 // Connect all the Device Path Consoles
117 while (ConsoleDevicePaths
!= NULL
) {
118 DevicePath
= GetNextDevicePathInstance (&ConsoleDevicePaths
, &Size
);
120 Status
= BdsConnectDevicePath (DevicePath
, Handle
, NULL
);
122 if (EFI_ERROR(Status
)) {
123 // We convert back to the text representation of the device Path
124 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
125 CHAR16
* DevicePathTxt
;
128 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
129 if (!EFI_ERROR(Status
)) {
130 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText (DevicePath
, TRUE
, TRUE
);
132 DEBUG((EFI_D_ERROR
,"Fail to start the console with the Device Path '%s'. (Error '%r')\n", DevicePathTxt
, Status
));
134 FreePool (DevicePathTxt
);
139 // If the console splitter driver is not supported by the platform then use the first Device Path
140 // instance for the console interface.
141 if (!EFI_ERROR(Status
) && (*Interface
== NULL
)) {
142 Status
= gBS
->HandleProtocol (*Handle
, Protocol
, Interface
);
146 // No Device Path has been defined for this console interface. We take the first protocol implementation
147 if (*Interface
== NULL
) {
148 Status
= gBS
->LocateHandleBuffer (ByProtocol
, Protocol
, NULL
, &NoHandles
, &Buffer
);
149 if (EFI_ERROR (Status
)) {
150 BdsConnectAllDrivers();
151 Status
= gBS
->LocateHandleBuffer (ByProtocol
, Protocol
, NULL
, &NoHandles
, &Buffer
);
154 if (!EFI_ERROR(Status
)) {
156 Status
= gBS
->HandleProtocol (*Handle
, Protocol
, Interface
);
157 ASSERT_EFI_ERROR(Status
);
161 Status
= EFI_SUCCESS
;
173 EFI_DEVICE_PATH
* ConOutDevicePaths
;
174 EFI_DEVICE_PATH
* ConInDevicePaths
;
175 EFI_DEVICE_PATH
* ConErrDevicePaths
;
177 // By getting the Console Device Paths from the environment variables before initializing the console pipe, we
178 // create the 3 environment variables (ConIn, ConOut, ConErr) that allows to initialize all the console interface
179 // of newly installed console drivers
180 Status
= GetConsoleDevicePathFromVariable (L
"ConOut", (CHAR16
*)PcdGetPtr(PcdDefaultConOutPaths
), &ConOutDevicePaths
);
181 ASSERT_EFI_ERROR (Status
);
182 Status
= GetConsoleDevicePathFromVariable (L
"ConIn", (CHAR16
*)PcdGetPtr(PcdDefaultConInPaths
), &ConInDevicePaths
);
183 ASSERT_EFI_ERROR (Status
);
184 Status
= GetConsoleDevicePathFromVariable (L
"ErrOut", (CHAR16
*)PcdGetPtr(PcdDefaultConOutPaths
), &ConErrDevicePaths
);
185 ASSERT_EFI_ERROR (Status
);
187 // Initialize the Consoles
188 Status
= InitializeConsolePipe (ConOutDevicePaths
, &gEfiSimpleTextOutProtocolGuid
, &gST
->ConsoleOutHandle
, (VOID
**)&gST
->ConOut
);
189 ASSERT_EFI_ERROR (Status
);
190 Status
= InitializeConsolePipe (ConInDevicePaths
, &gEfiSimpleTextInProtocolGuid
, &gST
->ConsoleInHandle
, (VOID
**)&gST
->ConIn
);
191 ASSERT_EFI_ERROR (Status
);
192 Status
= InitializeConsolePipe (ConErrDevicePaths
, &gEfiSimpleTextOutProtocolGuid
, &gST
->StandardErrorHandle
, (VOID
**)&gST
->StdErr
);
193 if (EFI_ERROR(Status
)) {
194 // In case of error, we reuse the console output for the error output
195 gST
->StandardErrorHandle
= gST
->ConsoleOutHandle
;
196 gST
->StdErr
= gST
->ConOut
;
199 // Free Memory allocated for reading the UEFI Variables
200 if (ConOutDevicePaths
) {
201 FreePool (ConOutDevicePaths
);
203 if (ConInDevicePaths
) {
204 FreePool (ConInDevicePaths
);
206 if (ConErrDevicePaths
) {
207 FreePool (ConErrDevicePaths
);
214 DefineDefaultBootEntries (
218 BDS_LOAD_OPTION
* BdsLoadOption
;
221 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
* EfiDevicePathFromTextProtocol
;
222 EFI_DEVICE_PATH
* BootDevicePath
;
224 UINTN OptionalDataSize
;
225 ARM_BDS_LOADER_ARGUMENTS
* BootArguments
;
226 ARM_BDS_LOADER_TYPE BootType
;
227 EFI_DEVICE_PATH
* InitrdPath
;
230 UINTN CmdLineAsciiSize
;
231 CHAR16
* DefaultBootArgument
;
232 CHAR8
* AsciiDefaultBootArgument
;
235 // If Boot Order does not exist then create a default entry
238 Status
= gRT
->GetVariable (L
"BootOrder", &gEfiGlobalVariableGuid
, NULL
, &Size
, NULL
);
239 if (Status
== EFI_NOT_FOUND
) {
240 if ((PcdGetPtr(PcdDefaultBootDevicePath
) == NULL
) || (StrLen ((CHAR16
*)PcdGetPtr(PcdDefaultBootDevicePath
)) == 0)) {
241 return EFI_UNSUPPORTED
;
244 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
245 if (EFI_ERROR(Status
)) {
246 // You must provide an implementation of DevicePathFromTextProtocol in your firmware (eg: DevicePathDxe)
247 DEBUG((EFI_D_ERROR
,"Error: Bds requires DevicePathFromTextProtocol\n"));
250 BootDevicePath
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdDefaultBootDevicePath
));
253 // We convert back to the text representation of the device Path to see if the initial text is correct
254 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
255 CHAR16
* DevicePathTxt
;
257 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
258 ASSERT_EFI_ERROR(Status
);
259 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText (BootDevicePath
, TRUE
, TRUE
);
261 if (StrCmp ((CHAR16
*)PcdGetPtr (PcdDefaultBootDevicePath
), DevicePathTxt
) != 0) {
262 DEBUG ((EFI_D_ERROR
, "Device Path given: '%s' Device Path expected: '%s'\n",
263 (CHAR16
*)PcdGetPtr (PcdDefaultBootDevicePath
), DevicePathTxt
));
264 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
267 FreePool (DevicePathTxt
);
270 // Create the entry is the Default values are correct
271 if (BootDevicePath
!= NULL
) {
272 BootType
= (ARM_BDS_LOADER_TYPE
)PcdGet32 (PcdDefaultBootType
);
274 // We do not support NULL pointer
275 ASSERT (PcdGetPtr (PcdDefaultBootArgument
) != NULL
);
278 // Logic to handle ASCII or Unicode default parameters
280 if (*(CHAR8
*)PcdGetPtr (PcdDefaultBootArgument
) == '\0') {
282 CmdLineAsciiSize
= 0;
283 DefaultBootArgument
= NULL
;
284 AsciiDefaultBootArgument
= NULL
;
285 } else if (IsUnicodeString ((CHAR16
*)PcdGetPtr (PcdDefaultBootArgument
))) {
286 // The command line is a Unicode string
287 DefaultBootArgument
= (CHAR16
*)PcdGetPtr (PcdDefaultBootArgument
);
288 CmdLineSize
= StrSize (DefaultBootArgument
);
290 // Initialize ASCII variables
291 CmdLineAsciiSize
= CmdLineSize
/ 2;
292 AsciiDefaultBootArgument
= AllocatePool (CmdLineAsciiSize
);
293 if (AsciiDefaultBootArgument
== NULL
) {
294 return EFI_OUT_OF_RESOURCES
;
296 UnicodeStrToAsciiStr ((CHAR16
*)PcdGetPtr (PcdDefaultBootArgument
), AsciiDefaultBootArgument
);
298 // The command line is a ASCII string
299 AsciiDefaultBootArgument
= (CHAR8
*)PcdGetPtr (PcdDefaultBootArgument
);
300 CmdLineAsciiSize
= AsciiStrSize (AsciiDefaultBootArgument
);
302 // Initialize ASCII variables
303 CmdLineSize
= CmdLineAsciiSize
* 2;
304 DefaultBootArgument
= AllocatePool (CmdLineSize
);
305 if (DefaultBootArgument
== NULL
) {
306 return EFI_OUT_OF_RESOURCES
;
308 AsciiStrToUnicodeStr (AsciiDefaultBootArgument
, DefaultBootArgument
);
311 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
312 InitrdPath
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdDefaultBootInitrdPath
));
313 InitrdSize
= GetDevicePathSize (InitrdPath
);
315 OptionalDataSize
= sizeof(ARM_BDS_LOADER_ARGUMENTS
) + CmdLineAsciiSize
+ InitrdSize
;
316 BootArguments
= (ARM_BDS_LOADER_ARGUMENTS
*)AllocatePool (OptionalDataSize
);
317 if (BootArguments
== NULL
) {
318 return EFI_OUT_OF_RESOURCES
;
320 BootArguments
->LinuxArguments
.CmdLineSize
= CmdLineAsciiSize
;
321 BootArguments
->LinuxArguments
.InitrdSize
= InitrdSize
;
323 CopyMem ((VOID
*)(BootArguments
+ 1), AsciiDefaultBootArgument
, CmdLineAsciiSize
);
324 CopyMem ((VOID
*)((UINTN
)(BootArguments
+ 1) + CmdLineAsciiSize
), InitrdPath
, InitrdSize
);
326 OptionalData
= (UINT8
*)BootArguments
;
328 OptionalData
= (UINT8
*)DefaultBootArgument
;
329 OptionalDataSize
= CmdLineSize
;
332 BootOptionCreate (LOAD_OPTION_ACTIVE
| LOAD_OPTION_CATEGORY_BOOT
,
333 (CHAR16
*)PcdGetPtr(PcdDefaultBootDescription
),
340 FreePool (BdsLoadOption
);
342 if (DefaultBootArgument
== (CHAR16
*)PcdGetPtr (PcdDefaultBootArgument
)) {
343 FreePool (AsciiDefaultBootArgument
);
344 } else if (DefaultBootArgument
!= NULL
) {
345 FreePool (DefaultBootArgument
);
348 Status
= EFI_UNSUPPORTED
;
356 StartDefaultBootOnTimeout (
363 EFI_EVENT WaitList
[2];
368 CHAR16 BootVariableName
[9];
372 Size
= sizeof(UINT16
);
373 Timeout
= (UINT16
)PcdGet16 (PcdPlatformBootTimeOut
);
374 Status
= GetGlobalEnvironmentVariable (L
"Timeout", &Timeout
, &Size
, (VOID
**)&TimeoutPtr
);
375 if (!EFI_ERROR (Status
)) {
376 Timeout
= *TimeoutPtr
;
377 FreePool (TimeoutPtr
);
380 if (Timeout
!= 0xFFFF) {
382 // Create the waiting events (keystroke and 1sec timer)
383 gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &WaitList
[0]);
384 gBS
->SetTimer (WaitList
[0], TimerPeriodic
, EFI_SET_TIMER_TO_SECOND
);
385 WaitList
[1] = gST
->ConIn
->WaitForKey
;
389 Print(L
"The default boot selection will start in ");
390 while ((Timeout
> 0) && (WaitIndex
== 0)) {
391 Print(L
"%3d seconds",Timeout
);
392 gBS
->WaitForEvent (2, WaitList
, &WaitIndex
);
393 if (WaitIndex
== 0) {
394 Print(L
"\b\b\b\b\b\b\b\b\b\b\b");
398 // Discard key in the buffer
400 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
401 } while(!EFI_ERROR(Status
));
402 gBS
->CloseEvent (WaitList
[0]);
406 // In case of Timeout we start the default boot selection
408 // Get the Boot Option Order from the environment variable (a default value should have been created)
409 GetGlobalEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
411 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
412 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", BootOrder
[Index
]);
413 Status
= BdsStartBootOption (BootVariableName
);
414 if(!EFI_ERROR(Status
)){
415 // Boot option returned successfully, hence don't need to start next boot option
418 // In case of success, we should not return from this call.
420 FreePool (BootOrder
);
427 An empty function to pass error checking of CreateEventEx ().
429 @param Event Event whose notification function is being invoked.
430 @param Context Pointer to the notification function's context,
431 which is implementation-dependent.
436 EmptyCallbackFunction (
445 This function uses policy data from the platform to determine what operating
446 system or system utility should be loaded and invoked. This function call
447 also optionally make the use of user input to determine the operating system
448 or system utility to be loaded and invoked. When the DXE Core has dispatched
449 all the drivers on the dispatch queue, this function is called. This
450 function will attempt to connect the boot devices required to load and invoke
451 the selected operating system or system utility. During this process,
452 additional firmware volumes may be discovered that may contain addition DXE
453 drivers that can be dispatched by the DXE Core. If a boot device cannot be
454 fully connected, this function calls the DXE Service Dispatch() to allow the
455 DXE drivers from any newly discovered firmware volumes to be dispatched.
456 Then the boot device connection can be attempted again. If the same boot
457 device connection operation fails twice in a row, then that boot device has
458 failed, and should be skipped. This function should never return.
460 @param This The EFI_BDS_ARCH_PROTOCOL instance.
468 IN EFI_BDS_ARCH_PROTOCOL
*This
475 CHAR16 BootVariableName
[9];
476 EFI_EVENT EndOfDxeEvent
;
479 // Signal EndOfDxe PI Event
481 Status
= gBS
->CreateEventEx (
484 EmptyCallbackFunction
,
486 &gEfiEndOfDxeEventGroupGuid
,
489 if (!EFI_ERROR (Status
)) {
490 gBS
->SignalEvent (EndOfDxeEvent
);
493 PERF_END (NULL
, "DXE", NULL
, 0);
496 // Declare the Firmware Vendor
498 if (FixedPcdGetPtr(PcdFirmwareVendor
) != NULL
) {
500 gST
->FirmwareVendor
= AllocateRuntimePool (Size
);
501 ASSERT (gST
->FirmwareVendor
!= NULL
);
502 UnicodeSPrint (gST
->FirmwareVendor
, Size
, L
"%a EFI %a %a", PcdGetPtr(PcdFirmwareVendor
), __DATE__
, __TIME__
);
506 // Fixup Table CRC after we updated Firmware Vendor
509 Status
= gBS
->CalculateCrc32 ((VOID
*)gST
, gST
->Hdr
.HeaderSize
, &gST
->Hdr
.CRC32
);
510 ASSERT_EFI_ERROR (Status
);
512 // If BootNext environment variable is defined then we just load it !
513 BootNextSize
= sizeof(UINT16
);
514 Status
= GetGlobalEnvironmentVariable (L
"BootNext", NULL
, &BootNextSize
, (VOID
**)&BootNext
);
515 if (!EFI_ERROR(Status
)) {
516 ASSERT(BootNextSize
== sizeof(UINT16
));
518 // Generate the requested Boot Entry variable name
519 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", *BootNext
);
521 // Set BootCurrent variable
522 gRT
->SetVariable (L
"BootCurrent", &gEfiGlobalVariableGuid
,
523 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
524 BootNextSize
, BootNext
);
528 // Start the requested Boot Entry
529 Status
= BdsStartBootOption (BootVariableName
);
530 if (Status
!= EFI_NOT_FOUND
) {
531 // BootNext has not been succeeded launched
532 if (EFI_ERROR(Status
)) {
533 Print(L
"Fail to start BootNext.\n");
536 // Delete the BootNext environment variable
537 gRT
->SetVariable (L
"BootNext", &gEfiGlobalVariableGuid
,
538 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
542 // Clear BootCurrent variable
543 gRT
->SetVariable (L
"BootCurrent", &gEfiGlobalVariableGuid
,
544 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
548 // If Boot Order does not exist then create a default entry
549 DefineDefaultBootEntries ();
551 // Now we need to setup the EFI System Table with information about the console devices.
552 InitializeConsole ();
555 // Update the CRC32 in the EFI System Table header
558 Status
= gBS
->CalculateCrc32 ((VOID
*)gST
, gST
->Hdr
.HeaderSize
, &gST
->Hdr
.CRC32
);
559 ASSERT_EFI_ERROR (Status
);
561 // Timer before initiating the default boot selection
562 StartDefaultBootOnTimeout ();
564 // Start the Boot Menu
565 Status
= BootMenuMain ();
566 ASSERT_EFI_ERROR (Status
);
570 EFI_BDS_ARCH_PROTOCOL gBdsProtocol
= {
577 IN EFI_HANDLE ImageHandle
,
578 IN EFI_SYSTEM_TABLE
*SystemTable
583 Status
= gBS
->InstallMultipleProtocolInterfaces (
585 &gEfiBdsArchProtocolGuid
, &gBdsProtocol
,
588 ASSERT_EFI_ERROR (Status
);