3 * Copyright (c) 2011, 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 #define EFI_SET_TIMER_TO_SECOND 10000000
24 EFI_HANDLE mImageHandle
;
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
= GetEnvironmentVariable (ConsoleVarName
, NULL
, NULL
, (VOID
**)&DevicePathInstances
);
43 if (EFI_ERROR(Status
)) {
44 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
45 ASSERT_EFI_ERROR(Status
);
47 DevicePathInstances
= NULL
;
49 // Extract the Device Path instances from the multi-device path string
50 while ((DefaultConsolePaths
!= NULL
) && (DefaultConsolePaths
[0] != L
'\0')) {
51 NextDevicePathStr
= StrStr (DefaultConsolePaths
, L
";");
52 if (NextDevicePathStr
== NULL
) {
53 DevicePathStr
= DefaultConsolePaths
;
54 DefaultConsolePaths
= NULL
;
56 DevicePathStr
= (CHAR16
*)AllocateCopyPool ((NextDevicePathStr
- DefaultConsolePaths
+ 1) * sizeof(CHAR16
), DefaultConsolePaths
);
57 *(DevicePathStr
+ (NextDevicePathStr
- DefaultConsolePaths
)) = L
'\0';
58 DefaultConsolePaths
= NextDevicePathStr
;
59 if (DefaultConsolePaths
[0] == L
';') {
60 DefaultConsolePaths
++;
64 DevicePathInstance
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath (DevicePathStr
);
65 ASSERT(DevicePathInstance
!= NULL
);
66 DevicePathInstances
= AppendDevicePathInstance (DevicePathInstances
, DevicePathInstance
);
68 if (NextDevicePathStr
!= NULL
) {
69 FreePool (DevicePathStr
);
71 FreePool (DevicePathInstance
);
74 // Set the environment variable with this device path multi-instances
75 Size
= GetDevicePathSize (DevicePathInstances
);
77 Status
= gRT
->SetVariable (
79 &gEfiGlobalVariableGuid
,
80 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
85 Status
= EFI_INVALID_PARAMETER
;
89 if (!EFI_ERROR(Status
)) {
90 *DevicePaths
= DevicePathInstances
;
97 InitializeConsolePipe (
98 IN EFI_DEVICE_PATH
*ConsoleDevicePaths
,
99 IN EFI_GUID
*Protocol
,
100 OUT EFI_HANDLE
*Handle
,
108 EFI_DEVICE_PATH_PROTOCOL
* DevicePath
;
110 // Connect all the Device Path Consoles
112 DevicePath
= GetNextDevicePathInstance (&ConsoleDevicePaths
, &Size
);
114 Status
= BdsConnectDevicePath (DevicePath
, Handle
, NULL
);
116 if (EFI_ERROR(Status
)) {
117 // We convert back to the text representation of the device Path
118 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
119 CHAR16
* DevicePathTxt
;
122 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
123 if (!EFI_ERROR(Status
)) {
124 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText (DevicePath
, TRUE
, TRUE
);
126 DEBUG((EFI_D_ERROR
,"Fail to start the console with the Device Path '%s'. (Error '%r')\n", DevicePathTxt
, Status
));
128 FreePool (DevicePathTxt
);
133 // If the console splitter driver is not supported by the platform then use the first Device Path
134 // instance for the console interface.
135 if (!EFI_ERROR(Status
) && (*Interface
== NULL
)) {
136 Status
= gBS
->HandleProtocol (*Handle
, Protocol
, Interface
);
138 } while (ConsoleDevicePaths
!= NULL
);
140 // No Device Path has been defined for this console interface. We take the first protocol implementation
141 if (*Interface
== NULL
) {
142 Status
= gBS
->LocateHandleBuffer (ByProtocol
, Protocol
, NULL
, &NoHandles
, &Buffer
);
143 if (EFI_ERROR (Status
)) {
144 BdsConnectAllDrivers();
145 Status
= gBS
->LocateHandleBuffer (ByProtocol
, Protocol
, NULL
, &NoHandles
, &Buffer
);
148 if (!EFI_ERROR(Status
)) {
150 Status
= gBS
->HandleProtocol (*Handle
, Protocol
, Interface
);
151 ASSERT_EFI_ERROR(Status
);
155 Status
= EFI_SUCCESS
;
167 EFI_DEVICE_PATH
* ConOutDevicePaths
;
168 EFI_DEVICE_PATH
* ConInDevicePaths
;
169 EFI_DEVICE_PATH
* ConErrDevicePaths
;
171 // By getting the Console Device Paths from the environment variables before initializing the console pipe, we
172 // create the 3 environment variables (ConIn, ConOut, ConErr) that allows to initialize all the console interface
173 // of newly installed console drivers
174 Status
= GetConsoleDevicePathFromVariable (L
"ConOut", (CHAR16
*)PcdGetPtr(PcdDefaultConOutPaths
),&ConOutDevicePaths
);
175 ASSERT_EFI_ERROR (Status
);
176 Status
= GetConsoleDevicePathFromVariable (L
"ConIn", (CHAR16
*)PcdGetPtr(PcdDefaultConInPaths
),&ConInDevicePaths
);
177 ASSERT_EFI_ERROR (Status
);
178 Status
= GetConsoleDevicePathFromVariable (L
"ConErr", (CHAR16
*)PcdGetPtr(PcdDefaultConOutPaths
),&ConErrDevicePaths
);
179 ASSERT_EFI_ERROR (Status
);
181 // Initialize the Consoles
182 Status
= InitializeConsolePipe (ConOutDevicePaths
, &gEfiSimpleTextOutProtocolGuid
, &gST
->ConsoleOutHandle
, (VOID
**)&gST
->ConOut
);
183 ASSERT_EFI_ERROR (Status
);
184 Status
= InitializeConsolePipe (ConInDevicePaths
, &gEfiSimpleTextInProtocolGuid
, &gST
->ConsoleInHandle
, (VOID
**)&gST
->ConIn
);
185 ASSERT_EFI_ERROR (Status
);
186 Status
= InitializeConsolePipe (ConErrDevicePaths
, &gEfiSimpleTextOutProtocolGuid
, &gST
->StandardErrorHandle
, (VOID
**)&gST
->StdErr
);
187 if (EFI_ERROR(Status
)) {
188 // In case of error, we reuse the console output for the error output
189 gST
->StandardErrorHandle
= gST
->ConsoleOutHandle
;
190 gST
->StdErr
= gST
->ConOut
;
197 DefineDefaultBootEntries (
201 BDS_LOAD_OPTION
* BdsLoadOption
;
204 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
* EfiDevicePathFromTextProtocol
;
205 EFI_DEVICE_PATH
* BootDevicePath
;
206 BDS_LOADER_ARGUMENTS BootArguments
;
207 BDS_LOADER_TYPE BootType
;
210 // If Boot Order does not exist then create a default entry
213 Status
= gRT
->GetVariable (L
"BootOrder", &gEfiGlobalVariableGuid
, NULL
, &Size
, NULL
);
214 if (Status
== EFI_NOT_FOUND
) {
215 if ((PcdGetPtr(PcdDefaultBootDevicePath
) == NULL
) || (StrLen ((CHAR16
*)PcdGetPtr(PcdDefaultBootDevicePath
)) == 0)) {
216 return EFI_UNSUPPORTED
;
219 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
220 if (EFI_ERROR(Status
)) {
221 // You must provide an implementation of DevicePathFromTextProtocol in your firmware (eg: DevicePathDxe)
222 DEBUG((EFI_D_ERROR
,"Error: Bds requires DevicePathFromTextProtocol\n"));
225 BootDevicePath
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdDefaultBootDevicePath
));
228 // We convert back to the text representation of the device Path to see if the initial text is correct
229 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
230 CHAR16
* DevicePathTxt
;
232 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
233 ASSERT_EFI_ERROR(Status
);
234 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText (BootDevicePath
, TRUE
, TRUE
);
236 ASSERT (StrCmp ((CHAR16
*)PcdGetPtr(PcdDefaultBootDevicePath
), DevicePathTxt
) == 0);
238 FreePool (DevicePathTxt
);
241 // Create the entry is the Default values are correct
242 if (BootDevicePath
!= NULL
) {
243 BootType
= (BDS_LOADER_TYPE
)PcdGet32 (PcdDefaultBootType
);
245 if (BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) {
246 BootArguments
.LinuxAtagArguments
.CmdLine
[0] = '\0';
247 AsciiStrnCpy (BootArguments
.LinuxAtagArguments
.CmdLine
,(CHAR8
*)PcdGetPtr(PcdDefaultBootArgument
),BOOT_DEVICE_OPTION_MAX
);
248 BootArguments
.LinuxAtagArguments
.InitrdPathList
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdDefaultBootInitrdPath
));
251 BootOptionCreate (LOAD_OPTION_ACTIVE
| LOAD_OPTION_CATEGORY_BOOT
,
252 (CHAR16
*)PcdGetPtr(PcdDefaultBootDescription
),
258 FreePool (BdsLoadOption
);
260 Status
= EFI_UNSUPPORTED
;
268 StartDefaultBootOnTimeout (
275 EFI_EVENT WaitList
[2];
280 CHAR16 BootVariableName
[9];
284 Size
= sizeof(UINT16
);
285 Timeout
= (UINT16
)PcdGet16 (PcdPlatformBootTimeOut
);
286 TimeoutPtr
= &Timeout
;
287 GetEnvironmentVariable (L
"Timeout", &Timeout
, &Size
, (VOID
**)&TimeoutPtr
);
289 if (Timeout
!= 0xFFFF) {
291 // Create the waiting events (keystroke and 1sec timer)
292 gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &WaitList
[0]);
293 gBS
->SetTimer (WaitList
[0], TimerPeriodic
, EFI_SET_TIMER_TO_SECOND
);
294 WaitList
[1] = gST
->ConIn
->WaitForKey
;
298 Print(L
"The default boot selection will start in ");
299 while ((Timeout
> 0) && (WaitIndex
== 0)) {
300 Print(L
"%3d seconds",Timeout
);
301 gBS
->WaitForEvent (2, WaitList
, &WaitIndex
);
302 if (WaitIndex
== 0) {
303 Print(L
"\b\b\b\b\b\b\b\b\b\b\b");
307 // Discard key in the buffer
309 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
310 } while(!EFI_ERROR(Status
));
311 gBS
->CloseEvent (WaitList
[0]);
315 // In case of Timeout we start the default boot selection
317 // Get the Boot Option Order from the environment variable (a default value should have been created)
318 GetEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
320 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
321 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", BootOrder
[Index
]);
322 Status
= BdsStartBootOption (BootVariableName
);
323 if(!EFI_ERROR(Status
)){
324 // Boot option returned successfully, hence don't need to start next boot option
327 // In case of success, we should not return from this call.
335 This function uses policy data from the platform to determine what operating
336 system or system utility should be loaded and invoked. This function call
337 also optionally make the use of user input to determine the operating system
338 or system utility to be loaded and invoked. When the DXE Core has dispatched
339 all the drivers on the dispatch queue, this function is called. This
340 function will attempt to connect the boot devices required to load and invoke
341 the selected operating system or system utility. During this process,
342 additional firmware volumes may be discovered that may contain addition DXE
343 drivers that can be dispatched by the DXE Core. If a boot device cannot be
344 fully connected, this function calls the DXE Service Dispatch() to allow the
345 DXE drivers from any newly discovered firmware volumes to be dispatched.
346 Then the boot device connection can be attempted again. If the same boot
347 device connection operation fails twice in a row, then that boot device has
348 failed, and should be skipped. This function should never return.
350 @param This The EFI_BDS_ARCH_PROTOCOL instance.
358 IN EFI_BDS_ARCH_PROTOCOL
*This
364 PERF_END (NULL
, "DXE", NULL
, 0);
367 // Declare the Firmware Vendor
369 if (FixedPcdGetPtr(PcdFirmwareVendor
) != NULL
) {
371 gST
->FirmwareVendor
= AllocateRuntimePool (Size
);
372 ASSERT (gST
->FirmwareVendor
!= NULL
);
373 UnicodeSPrint (gST
->FirmwareVendor
, Size
, L
"%a EFI %a %a", PcdGetPtr(PcdFirmwareVendor
), __DATE__
, __TIME__
);
376 // If BootNext environment variable is defined then we just load it !
377 Status
= BdsStartBootOption (L
"BootNext");
378 if (Status
!= EFI_NOT_FOUND
) {
379 // BootNext has not been succeeded launched
380 if (EFI_ERROR(Status
)) {
381 Print(L
"Fail to start BootNext.\n");
384 // Delete the BootNext environment variable
385 gRT
->SetVariable (L
"BootNext", &gEfiGlobalVariableGuid
,
386 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
390 // If Boot Order does not exist then create a default entry
391 DefineDefaultBootEntries ();
393 // Now we need to setup the EFI System Table with information about the console devices.
394 InitializeConsole ();
396 // Timer before initiating the default boot selection
397 StartDefaultBootOnTimeout ();
399 // Start the Boot Menu
400 Status
= BootMenuMain ();
401 ASSERT_EFI_ERROR (Status
);
405 EFI_BDS_ARCH_PROTOCOL gBdsProtocol
= {
412 IN EFI_HANDLE ImageHandle
,
413 IN EFI_SYSTEM_TABLE
*SystemTable
418 mImageHandle
= ImageHandle
;
420 Status
= gBS
->InstallMultipleProtocolInterfaces (
422 &gEfiBdsArchProtocolGuid
, &gBdsProtocol
,
425 ASSERT_EFI_ERROR (Status
);