ArmPlatformPkg/Bds: Fix GetConsoleDevicePathFromVariable() when SetVariable has failed

Return the Default Device Path even when SetVariable has failed.



git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12469 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
oliviermartin 2011-09-28 19:05:54 +00:00
parent 0bade1054d
commit 705b0c031f
1 changed files with 440 additions and 440 deletions

View File

@ -1,440 +1,440 @@
/** @file /** @file
* *
* Copyright (c) 2011, ARM Limited. All rights reserved. * Copyright (c) 2011, ARM Limited. All rights reserved.
* *
* This program and the accompanying materials * This program and the accompanying materials
* are licensed and made available under the terms and conditions of the BSD License * are licensed and made available under the terms and conditions of the BSD License
* which accompanies this distribution. The full text of the license may be found at * which accompanies this distribution. The full text of the license may be found at
* http://opensource.org/licenses/bsd-license.php * http://opensource.org/licenses/bsd-license.php
* *
* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
* *
**/ **/
#include "BdsInternal.h" #include "BdsInternal.h"
#include <Library/PcdLib.h> #include <Library/PcdLib.h>
#include <Library/PerformanceLib.h> #include <Library/PerformanceLib.h>
#include <Protocol/Bds.h> #include <Protocol/Bds.h>
#define EFI_SET_TIMER_TO_SECOND 10000000 #define EFI_SET_TIMER_TO_SECOND 10000000
EFI_HANDLE mImageHandle; EFI_HANDLE mImageHandle;
STATIC STATIC
EFI_STATUS EFI_STATUS
GetConsoleDevicePathFromVariable ( GetConsoleDevicePathFromVariable (
IN CHAR16* ConsoleVarName, IN CHAR16* ConsoleVarName,
IN CHAR16* DefaultConsolePaths, IN CHAR16* DefaultConsolePaths,
OUT EFI_DEVICE_PATH** DevicePaths OUT EFI_DEVICE_PATH** DevicePaths
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
UINTN Size; UINTN Size;
EFI_DEVICE_PATH_PROTOCOL* DevicePathInstances; EFI_DEVICE_PATH_PROTOCOL* DevicePathInstances;
EFI_DEVICE_PATH_PROTOCOL* DevicePathInstance; EFI_DEVICE_PATH_PROTOCOL* DevicePathInstance;
CHAR16* DevicePathStr; CHAR16* DevicePathStr;
CHAR16* NextDevicePathStr; CHAR16* NextDevicePathStr;
EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol; EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol;
Status = GetEnvironmentVariable (ConsoleVarName, NULL, NULL, (VOID**)&DevicePathInstances); Status = GetEnvironmentVariable (ConsoleVarName, NULL, NULL, (VOID**)&DevicePathInstances);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol); Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);
ASSERT_EFI_ERROR(Status); ASSERT_EFI_ERROR(Status);
DevicePathInstances = NULL; DevicePathInstances = NULL;
// Extract the Device Path instances from the multi-device path string // Extract the Device Path instances from the multi-device path string
while ((DefaultConsolePaths != NULL) && (DefaultConsolePaths[0] != L'\0')) { while ((DefaultConsolePaths != NULL) && (DefaultConsolePaths[0] != L'\0')) {
NextDevicePathStr = StrStr (DefaultConsolePaths, L";"); NextDevicePathStr = StrStr (DefaultConsolePaths, L";");
if (NextDevicePathStr == NULL) { if (NextDevicePathStr == NULL) {
DevicePathStr = DefaultConsolePaths; DevicePathStr = DefaultConsolePaths;
DefaultConsolePaths = NULL; DefaultConsolePaths = NULL;
} else { } else {
DevicePathStr = (CHAR16*)AllocateCopyPool ((NextDevicePathStr - DefaultConsolePaths + 1) * sizeof(CHAR16), DefaultConsolePaths); DevicePathStr = (CHAR16*)AllocateCopyPool ((NextDevicePathStr - DefaultConsolePaths + 1) * sizeof(CHAR16), DefaultConsolePaths);
*(DevicePathStr + (NextDevicePathStr - DefaultConsolePaths)) = L'\0'; *(DevicePathStr + (NextDevicePathStr - DefaultConsolePaths)) = L'\0';
DefaultConsolePaths = NextDevicePathStr; DefaultConsolePaths = NextDevicePathStr;
if (DefaultConsolePaths[0] == L';') { if (DefaultConsolePaths[0] == L';') {
DefaultConsolePaths++; DefaultConsolePaths++;
} }
} }
DevicePathInstance = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (DevicePathStr); DevicePathInstance = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (DevicePathStr);
ASSERT(DevicePathInstance != NULL); ASSERT(DevicePathInstance != NULL);
DevicePathInstances = AppendDevicePathInstance (DevicePathInstances, DevicePathInstance); DevicePathInstances = AppendDevicePathInstance (DevicePathInstances, DevicePathInstance);
if (NextDevicePathStr != NULL) { if (NextDevicePathStr != NULL) {
FreePool (DevicePathStr); FreePool (DevicePathStr);
} }
FreePool (DevicePathInstance); FreePool (DevicePathInstance);
} }
// Set the environment variable with this device path multi-instances // Set the environment variable with this device path multi-instances
Size = GetDevicePathSize (DevicePathInstances); Size = GetDevicePathSize (DevicePathInstances);
if (Size > 0) { if (Size > 0) {
Status = gRT->SetVariable ( gRT->SetVariable (
ConsoleVarName, ConsoleVarName,
&gEfiGlobalVariableGuid, &gEfiGlobalVariableGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
Size, Size,
DevicePathInstances DevicePathInstances
); );
} else { } else {
Status = EFI_INVALID_PARAMETER; Status = EFI_INVALID_PARAMETER;
} }
} }
if (!EFI_ERROR(Status)) { if (!EFI_ERROR(Status)) {
*DevicePaths = DevicePathInstances; *DevicePaths = DevicePathInstances;
} }
return EFI_SUCCESS; return EFI_SUCCESS;
} }
STATIC STATIC
EFI_STATUS EFI_STATUS
InitializeConsolePipe ( InitializeConsolePipe (
IN EFI_DEVICE_PATH *ConsoleDevicePaths, IN EFI_DEVICE_PATH *ConsoleDevicePaths,
IN EFI_GUID *Protocol, IN EFI_GUID *Protocol,
OUT EFI_HANDLE *Handle, OUT EFI_HANDLE *Handle,
OUT VOID* *Interface OUT VOID* *Interface
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
UINTN Size; UINTN Size;
UINTN NoHandles; UINTN NoHandles;
EFI_HANDLE *Buffer; EFI_HANDLE *Buffer;
EFI_DEVICE_PATH_PROTOCOL* DevicePath; EFI_DEVICE_PATH_PROTOCOL* DevicePath;
// Connect all the Device Path Consoles // Connect all the Device Path Consoles
do { do {
DevicePath = GetNextDevicePathInstance (&ConsoleDevicePaths, &Size); DevicePath = GetNextDevicePathInstance (&ConsoleDevicePaths, &Size);
Status = BdsConnectDevicePath (DevicePath, Handle, NULL); Status = BdsConnectDevicePath (DevicePath, Handle, NULL);
DEBUG_CODE_BEGIN(); DEBUG_CODE_BEGIN();
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
// We convert back to the text representation of the device Path // We convert back to the text representation of the device Path
EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol; EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
CHAR16* DevicePathTxt; CHAR16* DevicePathTxt;
EFI_STATUS Status; EFI_STATUS Status;
Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol); Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
if (!EFI_ERROR(Status)) { if (!EFI_ERROR(Status)) {
DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (DevicePath, TRUE, TRUE); DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (DevicePath, TRUE, TRUE);
DEBUG((EFI_D_ERROR,"Fail to start the console with the Device Path '%s'. (Error '%r')\n", DevicePathTxt, Status)); DEBUG((EFI_D_ERROR,"Fail to start the console with the Device Path '%s'. (Error '%r')\n", DevicePathTxt, Status));
FreePool (DevicePathTxt); FreePool (DevicePathTxt);
} }
} }
DEBUG_CODE_END(); DEBUG_CODE_END();
// If the console splitter driver is not supported by the platform then use the first Device Path // If the console splitter driver is not supported by the platform then use the first Device Path
// instance for the console interface. // instance for the console interface.
if (!EFI_ERROR(Status) && (*Interface == NULL)) { if (!EFI_ERROR(Status) && (*Interface == NULL)) {
Status = gBS->HandleProtocol (*Handle, Protocol, Interface); Status = gBS->HandleProtocol (*Handle, Protocol, Interface);
} }
} while (ConsoleDevicePaths != NULL); } while (ConsoleDevicePaths != NULL);
// No Device Path has been defined for this console interface. We take the first protocol implementation // No Device Path has been defined for this console interface. We take the first protocol implementation
if (*Interface == NULL) { if (*Interface == NULL) {
Status = gBS->LocateHandleBuffer (ByProtocol, Protocol, NULL, &NoHandles, &Buffer); Status = gBS->LocateHandleBuffer (ByProtocol, Protocol, NULL, &NoHandles, &Buffer);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
BdsConnectAllDrivers(); BdsConnectAllDrivers();
Status = gBS->LocateHandleBuffer (ByProtocol, Protocol, NULL, &NoHandles, &Buffer); Status = gBS->LocateHandleBuffer (ByProtocol, Protocol, NULL, &NoHandles, &Buffer);
} }
if (!EFI_ERROR(Status)) { if (!EFI_ERROR(Status)) {
*Handle = Buffer[0]; *Handle = Buffer[0];
Status = gBS->HandleProtocol (*Handle, Protocol, Interface); Status = gBS->HandleProtocol (*Handle, Protocol, Interface);
ASSERT_EFI_ERROR(Status); ASSERT_EFI_ERROR(Status);
} }
FreePool (Buffer); FreePool (Buffer);
} else { } else {
Status = EFI_SUCCESS; Status = EFI_SUCCESS;
} }
return Status; return Status;
} }
EFI_STATUS EFI_STATUS
InitializeConsole ( InitializeConsole (
VOID VOID
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_DEVICE_PATH* ConOutDevicePaths; EFI_DEVICE_PATH* ConOutDevicePaths;
EFI_DEVICE_PATH* ConInDevicePaths; EFI_DEVICE_PATH* ConInDevicePaths;
EFI_DEVICE_PATH* ConErrDevicePaths; EFI_DEVICE_PATH* ConErrDevicePaths;
// By getting the Console Device Paths from the environment variables before initializing the console pipe, we // By getting the Console Device Paths from the environment variables before initializing the console pipe, we
// create the 3 environment variables (ConIn, ConOut, ConErr) that allows to initialize all the console interface // create the 3 environment variables (ConIn, ConOut, ConErr) that allows to initialize all the console interface
// of newly installed console drivers // of newly installed console drivers
Status = GetConsoleDevicePathFromVariable (L"ConOut", (CHAR16*)PcdGetPtr(PcdDefaultConOutPaths),&ConOutDevicePaths); Status = GetConsoleDevicePathFromVariable (L"ConOut", (CHAR16*)PcdGetPtr(PcdDefaultConOutPaths),&ConOutDevicePaths);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
Status = GetConsoleDevicePathFromVariable (L"ConIn", (CHAR16*)PcdGetPtr(PcdDefaultConInPaths),&ConInDevicePaths); Status = GetConsoleDevicePathFromVariable (L"ConIn", (CHAR16*)PcdGetPtr(PcdDefaultConInPaths),&ConInDevicePaths);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
Status = GetConsoleDevicePathFromVariable (L"ConErr", (CHAR16*)PcdGetPtr(PcdDefaultConOutPaths),&ConErrDevicePaths); Status = GetConsoleDevicePathFromVariable (L"ConErr", (CHAR16*)PcdGetPtr(PcdDefaultConOutPaths),&ConErrDevicePaths);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
// Initialize the Consoles // Initialize the Consoles
Status = InitializeConsolePipe (ConOutDevicePaths, &gEfiSimpleTextOutProtocolGuid, &gST->ConsoleOutHandle, (VOID **)&gST->ConOut); Status = InitializeConsolePipe (ConOutDevicePaths, &gEfiSimpleTextOutProtocolGuid, &gST->ConsoleOutHandle, (VOID **)&gST->ConOut);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
Status = InitializeConsolePipe (ConInDevicePaths, &gEfiSimpleTextInProtocolGuid, &gST->ConsoleInHandle, (VOID **)&gST->ConIn); Status = InitializeConsolePipe (ConInDevicePaths, &gEfiSimpleTextInProtocolGuid, &gST->ConsoleInHandle, (VOID **)&gST->ConIn);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
Status = InitializeConsolePipe (ConErrDevicePaths, &gEfiSimpleTextOutProtocolGuid, &gST->StandardErrorHandle, (VOID **)&gST->StdErr); Status = InitializeConsolePipe (ConErrDevicePaths, &gEfiSimpleTextOutProtocolGuid, &gST->StandardErrorHandle, (VOID **)&gST->StdErr);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
// In case of error, we reuse the console output for the error output // In case of error, we reuse the console output for the error output
gST->StandardErrorHandle = gST->ConsoleOutHandle; gST->StandardErrorHandle = gST->ConsoleOutHandle;
gST->StdErr = gST->ConOut; gST->StdErr = gST->ConOut;
} }
return EFI_SUCCESS; return EFI_SUCCESS;
} }
EFI_STATUS EFI_STATUS
DefineDefaultBootEntries ( DefineDefaultBootEntries (
VOID VOID
) )
{ {
BDS_LOAD_OPTION* BdsLoadOption; BDS_LOAD_OPTION* BdsLoadOption;
UINTN Size; UINTN Size;
EFI_STATUS Status; EFI_STATUS Status;
EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL* EfiDevicePathFromTextProtocol; EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL* EfiDevicePathFromTextProtocol;
EFI_DEVICE_PATH* BootDevicePath; EFI_DEVICE_PATH* BootDevicePath;
ARM_BDS_LOADER_ARGUMENTS* BootArguments; ARM_BDS_LOADER_ARGUMENTS* BootArguments;
ARM_BDS_LOADER_TYPE BootType; ARM_BDS_LOADER_TYPE BootType;
EFI_DEVICE_PATH* InitrdPath; EFI_DEVICE_PATH* InitrdPath;
UINTN CmdLineSize; UINTN CmdLineSize;
UINTN InitrdSize; UINTN InitrdSize;
// //
// If Boot Order does not exist then create a default entry // If Boot Order does not exist then create a default entry
// //
Size = 0; Size = 0;
Status = gRT->GetVariable (L"BootOrder", &gEfiGlobalVariableGuid, NULL, &Size, NULL); Status = gRT->GetVariable (L"BootOrder", &gEfiGlobalVariableGuid, NULL, &Size, NULL);
if (Status == EFI_NOT_FOUND) { if (Status == EFI_NOT_FOUND) {
if ((PcdGetPtr(PcdDefaultBootDevicePath) == NULL) || (StrLen ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath)) == 0)) { if ((PcdGetPtr(PcdDefaultBootDevicePath) == NULL) || (StrLen ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath)) == 0)) {
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol); Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
// You must provide an implementation of DevicePathFromTextProtocol in your firmware (eg: DevicePathDxe) // You must provide an implementation of DevicePathFromTextProtocol in your firmware (eg: DevicePathDxe)
DEBUG((EFI_D_ERROR,"Error: Bds requires DevicePathFromTextProtocol\n")); DEBUG((EFI_D_ERROR,"Error: Bds requires DevicePathFromTextProtocol\n"));
return Status; return Status;
} }
BootDevicePath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath)); BootDevicePath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath));
DEBUG_CODE_BEGIN(); DEBUG_CODE_BEGIN();
// We convert back to the text representation of the device Path to see if the initial text is correct // We convert back to the text representation of the device Path to see if the initial text is correct
EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol; EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
CHAR16* DevicePathTxt; CHAR16* DevicePathTxt;
Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol); Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
ASSERT_EFI_ERROR(Status); ASSERT_EFI_ERROR(Status);
DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (BootDevicePath, TRUE, TRUE); DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (BootDevicePath, TRUE, TRUE);
ASSERT (StrCmp ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath), DevicePathTxt) == 0); ASSERT (StrCmp ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath), DevicePathTxt) == 0);
FreePool (DevicePathTxt); FreePool (DevicePathTxt);
DEBUG_CODE_END(); DEBUG_CODE_END();
// Create the entry is the Default values are correct // Create the entry is the Default values are correct
if (BootDevicePath != NULL) { if (BootDevicePath != NULL) {
BootType = (ARM_BDS_LOADER_TYPE)PcdGet32 (PcdDefaultBootType); BootType = (ARM_BDS_LOADER_TYPE)PcdGet32 (PcdDefaultBootType);
if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) { if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {
CmdLineSize = AsciiStrSize ((CHAR8*)PcdGetPtr(PcdDefaultBootArgument)); CmdLineSize = AsciiStrSize ((CHAR8*)PcdGetPtr(PcdDefaultBootArgument));
InitrdPath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdDefaultBootInitrdPath)); InitrdPath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdDefaultBootInitrdPath));
InitrdSize = GetDevicePathSize (InitrdPath); InitrdSize = GetDevicePathSize (InitrdPath);
BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize); BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);
BootArguments->LinuxArguments.CmdLineSize = CmdLineSize; BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;
BootArguments->LinuxArguments.InitrdSize = InitrdSize; BootArguments->LinuxArguments.InitrdSize = InitrdSize;
CopyMem ((VOID*)(BootArguments + 1), (CHAR8*)PcdGetPtr(PcdDefaultBootArgument), CmdLineSize); CopyMem ((VOID*)(BootArguments + 1), (CHAR8*)PcdGetPtr(PcdDefaultBootArgument), CmdLineSize);
CopyMem ((VOID*)((UINTN)(BootArguments + 1) + CmdLineSize), InitrdPath, InitrdSize); CopyMem ((VOID*)((UINTN)(BootArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);
} else { } else {
BootArguments = NULL; BootArguments = NULL;
} }
BootOptionCreate (LOAD_OPTION_ACTIVE | LOAD_OPTION_CATEGORY_BOOT, BootOptionCreate (LOAD_OPTION_ACTIVE | LOAD_OPTION_CATEGORY_BOOT,
(CHAR16*)PcdGetPtr(PcdDefaultBootDescription), (CHAR16*)PcdGetPtr(PcdDefaultBootDescription),
BootDevicePath, BootDevicePath,
BootType, BootType,
BootArguments, BootArguments,
&BdsLoadOption &BdsLoadOption
); );
FreePool (BdsLoadOption); FreePool (BdsLoadOption);
} else { } else {
Status = EFI_UNSUPPORTED; Status = EFI_UNSUPPORTED;
} }
} }
return EFI_SUCCESS; return EFI_SUCCESS;
} }
EFI_STATUS EFI_STATUS
StartDefaultBootOnTimeout ( StartDefaultBootOnTimeout (
VOID VOID
) )
{ {
UINTN Size; UINTN Size;
UINT16 Timeout; UINT16 Timeout;
UINT16 *TimeoutPtr; UINT16 *TimeoutPtr;
EFI_EVENT WaitList[2]; EFI_EVENT WaitList[2];
UINTN WaitIndex; UINTN WaitIndex;
UINT16 *BootOrder; UINT16 *BootOrder;
UINTN BootOrderSize; UINTN BootOrderSize;
UINTN Index; UINTN Index;
CHAR16 BootVariableName[9]; CHAR16 BootVariableName[9];
EFI_STATUS Status; EFI_STATUS Status;
EFI_INPUT_KEY Key; EFI_INPUT_KEY Key;
Size = sizeof(UINT16); Size = sizeof(UINT16);
Timeout = (UINT16)PcdGet16 (PcdPlatformBootTimeOut); Timeout = (UINT16)PcdGet16 (PcdPlatformBootTimeOut);
TimeoutPtr = &Timeout; TimeoutPtr = &Timeout;
GetEnvironmentVariable (L"Timeout", &Timeout, &Size, (VOID**)&TimeoutPtr); GetEnvironmentVariable (L"Timeout", &Timeout, &Size, (VOID**)&TimeoutPtr);
if (Timeout != 0xFFFF) { if (Timeout != 0xFFFF) {
if (Timeout > 0) { if (Timeout > 0) {
// Create the waiting events (keystroke and 1sec timer) // Create the waiting events (keystroke and 1sec timer)
gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &WaitList[0]); gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &WaitList[0]);
gBS->SetTimer (WaitList[0], TimerPeriodic, EFI_SET_TIMER_TO_SECOND); gBS->SetTimer (WaitList[0], TimerPeriodic, EFI_SET_TIMER_TO_SECOND);
WaitList[1] = gST->ConIn->WaitForKey; WaitList[1] = gST->ConIn->WaitForKey;
// Start the timer // Start the timer
WaitIndex = 0; WaitIndex = 0;
Print(L"The default boot selection will start in "); Print(L"The default boot selection will start in ");
while ((Timeout > 0) && (WaitIndex == 0)) { while ((Timeout > 0) && (WaitIndex == 0)) {
Print(L"%3d seconds",Timeout); Print(L"%3d seconds",Timeout);
gBS->WaitForEvent (2, WaitList, &WaitIndex); gBS->WaitForEvent (2, WaitList, &WaitIndex);
if (WaitIndex == 0) { if (WaitIndex == 0) {
Print(L"\b\b\b\b\b\b\b\b\b\b\b"); Print(L"\b\b\b\b\b\b\b\b\b\b\b");
Timeout--; Timeout--;
} }
} }
// Discard key in the buffer // Discard key in the buffer
do { do {
Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
} while(!EFI_ERROR(Status)); } while(!EFI_ERROR(Status));
gBS->CloseEvent (WaitList[0]); gBS->CloseEvent (WaitList[0]);
Print(L"\n\r"); Print(L"\n\r");
} }
// In case of Timeout we start the default boot selection // In case of Timeout we start the default boot selection
if (Timeout == 0) { if (Timeout == 0) {
// Get the Boot Option Order from the environment variable (a default value should have been created) // Get the Boot Option Order from the environment variable (a default value should have been created)
GetEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder); GetEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) { for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
UnicodeSPrint (BootVariableName, 9 * sizeof(CHAR16), L"Boot%04X", BootOrder[Index]); UnicodeSPrint (BootVariableName, 9 * sizeof(CHAR16), L"Boot%04X", BootOrder[Index]);
Status = BdsStartBootOption (BootVariableName); Status = BdsStartBootOption (BootVariableName);
if(!EFI_ERROR(Status)){ if(!EFI_ERROR(Status)){
// Boot option returned successfully, hence don't need to start next boot option // Boot option returned successfully, hence don't need to start next boot option
break; break;
} }
// In case of success, we should not return from this call. // In case of success, we should not return from this call.
} }
} }
} }
return EFI_SUCCESS; return EFI_SUCCESS;
} }
/** /**
This function uses policy data from the platform to determine what operating This function uses policy data from the platform to determine what operating
system or system utility should be loaded and invoked. This function call system or system utility should be loaded and invoked. This function call
also optionally make the use of user input to determine the operating system also optionally make the use of user input to determine the operating system
or system utility to be loaded and invoked. When the DXE Core has dispatched or system utility to be loaded and invoked. When the DXE Core has dispatched
all the drivers on the dispatch queue, this function is called. This all the drivers on the dispatch queue, this function is called. This
function will attempt to connect the boot devices required to load and invoke function will attempt to connect the boot devices required to load and invoke
the selected operating system or system utility. During this process, the selected operating system or system utility. During this process,
additional firmware volumes may be discovered that may contain addition DXE additional firmware volumes may be discovered that may contain addition DXE
drivers that can be dispatched by the DXE Core. If a boot device cannot be drivers that can be dispatched by the DXE Core. If a boot device cannot be
fully connected, this function calls the DXE Service Dispatch() to allow the fully connected, this function calls the DXE Service Dispatch() to allow the
DXE drivers from any newly discovered firmware volumes to be dispatched. DXE drivers from any newly discovered firmware volumes to be dispatched.
Then the boot device connection can be attempted again. If the same boot Then the boot device connection can be attempted again. If the same boot
device connection operation fails twice in a row, then that boot device has device connection operation fails twice in a row, then that boot device has
failed, and should be skipped. This function should never return. failed, and should be skipped. This function should never return.
@param This The EFI_BDS_ARCH_PROTOCOL instance. @param This The EFI_BDS_ARCH_PROTOCOL instance.
@return None. @return None.
**/ **/
VOID VOID
EFIAPI EFIAPI
BdsEntry ( BdsEntry (
IN EFI_BDS_ARCH_PROTOCOL *This IN EFI_BDS_ARCH_PROTOCOL *This
) )
{ {
UINTN Size; UINTN Size;
EFI_STATUS Status; EFI_STATUS Status;
PERF_END (NULL, "DXE", NULL, 0); PERF_END (NULL, "DXE", NULL, 0);
// //
// Declare the Firmware Vendor // Declare the Firmware Vendor
// //
if (FixedPcdGetPtr(PcdFirmwareVendor) != NULL) { if (FixedPcdGetPtr(PcdFirmwareVendor) != NULL) {
Size = 0x100; Size = 0x100;
gST->FirmwareVendor = AllocateRuntimePool (Size); gST->FirmwareVendor = AllocateRuntimePool (Size);
ASSERT (gST->FirmwareVendor != NULL); ASSERT (gST->FirmwareVendor != NULL);
UnicodeSPrint (gST->FirmwareVendor, Size, L"%a EFI %a %a", PcdGetPtr(PcdFirmwareVendor), __DATE__, __TIME__); UnicodeSPrint (gST->FirmwareVendor, Size, L"%a EFI %a %a", PcdGetPtr(PcdFirmwareVendor), __DATE__, __TIME__);
} }
// If BootNext environment variable is defined then we just load it ! // If BootNext environment variable is defined then we just load it !
Status = BdsStartBootOption (L"BootNext"); Status = BdsStartBootOption (L"BootNext");
if (Status != EFI_NOT_FOUND) { if (Status != EFI_NOT_FOUND) {
// BootNext has not been succeeded launched // BootNext has not been succeeded launched
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
Print(L"Fail to start BootNext.\n"); Print(L"Fail to start BootNext.\n");
} }
// Delete the BootNext environment variable // Delete the BootNext environment variable
gRT->SetVariable (L"BootNext", &gEfiGlobalVariableGuid, gRT->SetVariable (L"BootNext", &gEfiGlobalVariableGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
0, NULL); 0, NULL);
} }
// If Boot Order does not exist then create a default entry // If Boot Order does not exist then create a default entry
DefineDefaultBootEntries (); DefineDefaultBootEntries ();
// Now we need to setup the EFI System Table with information about the console devices. // Now we need to setup the EFI System Table with information about the console devices.
InitializeConsole (); InitializeConsole ();
// Timer before initiating the default boot selection // Timer before initiating the default boot selection
StartDefaultBootOnTimeout (); StartDefaultBootOnTimeout ();
// Start the Boot Menu // Start the Boot Menu
Status = BootMenuMain (); Status = BootMenuMain ();
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
} }
EFI_BDS_ARCH_PROTOCOL gBdsProtocol = { EFI_BDS_ARCH_PROTOCOL gBdsProtocol = {
BdsEntry, BdsEntry,
}; };
EFI_STATUS EFI_STATUS
EFIAPI EFIAPI
BdsInitialize ( BdsInitialize (
IN EFI_HANDLE ImageHandle, IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable IN EFI_SYSTEM_TABLE *SystemTable
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
mImageHandle = ImageHandle; mImageHandle = ImageHandle;
Status = gBS->InstallMultipleProtocolInterfaces ( Status = gBS->InstallMultipleProtocolInterfaces (
&ImageHandle, &ImageHandle,
&gEfiBdsArchProtocolGuid, &gBdsProtocol, &gEfiBdsArchProtocolGuid, &gBdsProtocol,
NULL NULL
); );
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
return Status; return Status;
} }