/*++\r
\r
-Copyright (c) 2006, Intel Corporation \r
-All rights reserved. This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
\r
Module Name:\r
\r
/*++\r
\r
Routine Description:\r
- \r
+\r
Boot the legacy system with the boot option\r
\r
Arguments:\r
\r
EFI_UNSUPPORTED - There is no legacybios protocol, do not support\r
legacy boot.\r
- \r
+\r
EFI_STATUS - Return the status of LegacyBios->LegacyBoot ().\r
\r
--*/\r
\r
Routine Description:\r
\r
- Process the boot option follow the EFI 1.1 specification and \r
+ Process the boot option follow the EFI 1.1 specification and\r
special treat the legacy boot option with BBS_DEVICE_PATH.\r
\r
Arguments:\r
\r
Option - The boot option need to be processed\r
- \r
- DevicePath - The device path which describe where to load \r
- the boot image or the legcy BBS device path \r
+\r
+ DevicePath - The device path which describe where to load\r
+ the boot image or the legcy BBS device path\r
to boot the legacy OS\r
\r
ExitDataSize - Returned directly from gBS->StartImage ()\r
EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;\r
+ EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
+ VOID *Buffer;\r
+\r
+ //\r
+ // Record the performance data for End of BDS\r
+ //\r
+ PERF_END (0, BDS_TOK, NULL, 0);\r
\r
*ExitDataSize = 0;\r
*ExitData = NULL;\r
//\r
return EFI_SUCCESS;\r
}\r
-\r
+ //\r
+ // Signal the EFI_EVENT_SIGNAL_READY_TO_BOOT event\r
+ //\r
EfiSignalEventReadyToBoot ();\r
\r
//\r
if (!EFI_ERROR (Status) && IsDevicePathEnd (TempDevicePath)) {\r
FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
if (FilePath) {\r
+ //\r
+ // Issue a dummy read to the device to check for media change.\r
+ // When the removable media is changed, any Block IO read/write will\r
+ // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
+ // returned. After the Block IO protocol is reinstalled, subsequent\r
+ // Block IO read/write will success.\r
+ //\r
+ Status = gBS->HandleProtocol (\r
+ Handle,\r
+ &gEfiBlockIoProtocolGuid,\r
+ (VOID **) &BlkIo\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ Buffer = AllocatePool (BlkIo->Media->BlockSize);\r
+ if (Buffer != NULL) {\r
+ BlkIo->ReadBlocks (\r
+ BlkIo,\r
+ BlkIo->Media->MediaId,\r
+ 0,\r
+ BlkIo->Media->BlockSize,\r
+ Buffer\r
+ );\r
+ FreePool (Buffer);\r
+ }\r
+ }\r
+\r
Status = gBS->LoadImage (\r
TRUE,\r
mBdsImageHandle,\r
Routine Description:\r
\r
Check to see if a hard ware device path was passed in. If it was then search\r
- all the block IO devices for the passed in hard drive device path. \r
- \r
+ all the block IO devices for the passed in hard drive device path.\r
+\r
Arguments:\r
\r
Option - The current processing boot option.\r
\r
EFI_SUCCESS - Status from gBS->StartImage (),\r
or BootByDiskSignatureAndPartition ()\r
- \r
+\r
EFI_NOT_FOUND - If the Device Path is not found in the system\r
\r
--*/\r
// find HardDriver device path node\r
//\r
while (!IsDevicePathEnd (DevicePath)) {\r
- if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && \r
+ if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
(DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
) {\r
BlockIoHdDevicePath = DevicePath;\r
}\r
}\r
\r
- gBS->FreePool (BlockIoBuffer);\r
+ FreePool (BlockIoBuffer);\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+BdsLibDeleteOptionFromHandle (\r
+ IN EFI_HANDLE Handle\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Delete the boot option associated with the handle passed in\r
+\r
+Arguments:\r
+\r
+ Handle - The handle which present the device path to create boot option\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Delete the boot option success\r
+\r
+ EFI_NOT_FOUND - If the Device Path is not found in the system\r
+\r
+ EFI_OUT_OF_RESOURCES - Lack of memory resource\r
+\r
+ Other - Error return value from SetVariable()\r
+\r
+--*/\r
+{\r
+ UINT16 *BootOrder;\r
+ UINT8 *BootOptionVar;\r
+ UINTN BootOrderSize;\r
+ UINTN BootOptionSize;\r
+ EFI_STATUS Status;\r
+ UINTN Index;\r
+ UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
+ UINTN DevicePathSize;\r
+ UINTN OptionDevicePathSize;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
+ UINT8 *TempPtr;\r
+ CHAR16 *Description;\r
+\r
+ Status = EFI_SUCCESS;\r
+ BootOrder = NULL;\r
+ BootOrderSize = 0;\r
+\r
+ BootOrder = BdsLibGetVariableAndSize (\r
+ L"BootOrder",\r
+ &gEfiGlobalVariableGuid,\r
+ &BootOrderSize\r
+ );\r
+ if (NULL == BootOrder) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ DevicePath = DevicePathFromHandle (Handle);\r
+ if (DevicePath == NULL) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ DevicePathSize = GetDevicePathSize (DevicePath);\r
+\r
+ Index = 0;\r
+ while (Index < BootOrderSize / sizeof (UINT16)) {\r
+ UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
+ BootOptionVar = BdsLibGetVariableAndSize (\r
+ BootOption,\r
+ &gEfiGlobalVariableGuid,\r
+ &BootOptionSize\r
+ );\r
+ if (NULL == BootOptionVar) {\r
+ FreePool (BootOrder);\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ TempPtr = BootOptionVar;\r
+ TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
+ Description = (CHAR16 *) TempPtr;\r
+ TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+ OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+ OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
+\r
+ //\r
+ // Check whether the device path match\r
+ //\r
+ if ((OptionDevicePathSize == DevicePathSize) &&\r
+ (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
+ BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
+ FreePool (BootOptionVar);\r
+ break;\r
+ }\r
+\r
+ FreePool (BootOptionVar);\r
+ Index++;\r
+ }\r
+\r
+ Status = gRT->SetVariable (\r
+ L"BootOrder",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ BootOrderSize,\r
+ BootOrder\r
+ );\r
+\r
+ FreePool (BootOrder);\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+BdsDeleteAllInvalidEfiBootOption (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Delete all invalid EFI boot options. The probable invalid boot option could\r
+ be Removable media or Network boot device.\r
+\r
+Arguments:\r
+\r
+ VOID\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Delete all invalid boot option success\r
+\r
+ EFI_NOT_FOUND - Variable "BootOrder" is not found\r
+\r
+ EFI_OUT_OF_RESOURCES - Lack of memory resource\r
+\r
+ Other - Error return value from SetVariable()\r
+\r
+--*/\r
+{\r
+ UINT16 *BootOrder;\r
+ UINT8 *BootOptionVar;\r
+ UINTN BootOrderSize;\r
+ UINTN BootOptionSize;\r
+ EFI_STATUS Status;\r
+ UINTN Index;\r
+ UINTN Index2;\r
+ UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
+ UINTN OptionDevicePathSize;\r
+ EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
+ EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
+ UINT8 *TempPtr;\r
+ CHAR16 *Description;\r
+ EFI_HANDLE Handle;\r
+ BOOLEAN NeedDelete;\r
+\r
+ Status = EFI_SUCCESS;\r
+ BootOrder = NULL;\r
+ BootOrderSize = 0;\r
+\r
+ BootOrder = BdsLibGetVariableAndSize (\r
+ L"BootOrder",\r
+ &gEfiGlobalVariableGuid,\r
+ &BootOrderSize\r
+ );\r
+ if (NULL == BootOrder) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ Index = 0;\r
+ while (Index < BootOrderSize / sizeof (UINT16)) {\r
+ UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
+ BootOptionVar = BdsLibGetVariableAndSize (\r
+ BootOption,\r
+ &gEfiGlobalVariableGuid,\r
+ &BootOptionSize\r
+ );\r
+ if (NULL == BootOptionVar) {\r
+ FreePool (BootOrder);\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ TempPtr = BootOptionVar;\r
+ TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
+ Description = (CHAR16 *) TempPtr;\r
+ TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+ OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+ OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
+\r
+ //\r
+ // Skip legacy boot option (BBS boot device)\r
+ //\r
+ if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
+ (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
+ FreePool (BootOptionVar);\r
+ Index++;\r
+ continue;\r
+ }\r
+\r
+ TempDevicePath = OptionDevicePath;\r
+ LastDeviceNode = OptionDevicePath;\r
+ while (!EfiIsDevicePathEnd (TempDevicePath)) {\r
+ LastDeviceNode = TempDevicePath;\r
+ TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
+ }\r
+ //\r
+ // Skip the boot option that point to a file, since the device path in\r
+ // removable media boot option doesn't contains a file name.\r
+ //\r
+ if (((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
+ (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) ||\r
+ //\r
+ // Skip boot option for internal Shell, it's always valid\r
+ //\r
+ (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL)) {\r
+ FreePool (BootOptionVar);\r
+ Index++;\r
+ continue;\r
+ }\r
+\r
+ NeedDelete = TRUE;\r
+ //\r
+ // Check if it's a valid boot option for removable media\r
+ //\r
+ TempDevicePath = OptionDevicePath;\r
+ Status = gBS->LocateDevicePath (\r
+ &gEfiSimpleFileSystemProtocolGuid,\r
+ &TempDevicePath,\r
+ &Handle\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ NeedDelete = FALSE;\r
+ }\r
+ //\r
+ // Check if it's a valid boot option for network boot device\r
+ //\r
+ TempDevicePath = OptionDevicePath;\r
+ Status = gBS->LocateDevicePath (\r
+ &gEfiLoadFileProtocolGuid,\r
+ &TempDevicePath,\r
+ &Handle\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ NeedDelete = FALSE;\r
+ }\r
+\r
+ if (NeedDelete) {\r
+ //\r
+ // Delete this invalid boot option "Boot####"\r
+ //\r
+ Status = gRT->SetVariable (\r
+ BootOption,\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ 0,\r
+ NULL\r
+ );\r
+ //\r
+ // Mark this boot option in boot order as deleted\r
+ //\r
+ BootOrder[Index] = 0xffff;\r
+ }\r
+\r
+ FreePool (BootOptionVar);\r
+ Index++;\r
+ }\r
+\r
+ //\r
+ // Adjust boot order array\r
+ //\r
+ Index2 = 0;\r
+ for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
+ if (BootOrder[Index] != 0xffff) {\r
+ BootOrder[Index2] = BootOrder[Index];\r
+ Index2 ++;\r
+ }\r
+ }\r
+ Status = gRT->SetVariable (\r
+ L"BootOrder",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ Index2 * sizeof (UINT16),\r
+ BootOrder\r
+ );\r
+\r
+ FreePool (BootOrder);\r
+\r
return Status;\r
}\r
\r
UINT16 BootOptionNumber;\r
UINTN NumberFileSystemHandles;\r
EFI_HANDLE *FileSystemHandles;\r
- UINTN NumberBlkIoHandles;\r
- EFI_HANDLE *BlkIoHandles;\r
EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
UINTN Index;\r
- EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
UINTN NumberLoadFileHandles;\r
EFI_HANDLE *LoadFileHandles;\r
VOID *ProtocolInstance;\r
UINTN Size;\r
EFI_FV_FILE_ATTRIBUTES Attributes;\r
UINT32 AuthenticationStatus;\r
+ EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
+ EFI_HANDLE ImageHandle;\r
+ EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
+ BOOLEAN NeedDelete;\r
\r
BootOptionNumber = 0;\r
\r
REFRESH_LEGACY_BOOT_OPTIONS;\r
\r
//\r
- // Check all the block IO to create boot option\r
+ // Delete invalid boot option\r
//\r
- gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiBlockIoProtocolGuid,\r
- NULL,\r
- &NumberBlkIoHandles,\r
- &BlkIoHandles\r
- );\r
- for (Index = 0; Index < NumberBlkIoHandles; Index++) {\r
- Status = gBS->HandleProtocol (\r
- BlkIoHandles[Index],\r
- &gEfiBlockIoProtocolGuid,\r
- (VOID **) &BlkIo\r
- );\r
- if (EFI_ERROR (Status)) {\r
- continue;\r
- }\r
-\r
- if (!BlkIo->Media->RemovableMedia) {\r
- //\r
- // Skip fixed Media device on first loop interration\r
- //\r
- continue;\r
- }\r
-\r
- DevicePath = DevicePathFromHandle (BlkIoHandles[Index]);\r
- if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && \r
- (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
- ) {\r
- //\r
- // Build the boot option\r
- //\r
- BdsLibBuildOptionFromHandle (BlkIoHandles[Index], BdsBootOptionList);\r
- BootOptionNumber++;\r
- }\r
- }\r
-\r
- if (NumberBlkIoHandles) {\r
- gBS->FreePool (BlkIoHandles);\r
- }\r
+ BdsDeleteAllInvalidEfiBootOption ();\r
//\r
- // Parse Fixed Disk Devices.\r
+ // Parse removable media\r
//\r
gBS->LocateHandleBuffer (\r
ByProtocol,\r
(VOID **) &BlkIo\r
);\r
if (!EFI_ERROR (Status)) {\r
- if (BlkIo->Media->RemovableMedia) {\r
+ if (!BlkIo->Media->RemovableMedia) {\r
//\r
// If the file system handle supports a BlkIo protocol,\r
// skip the removable media devices\r
}\r
}\r
\r
- DevicePath = DevicePathFromHandle (FileSystemHandles[Index]);\r
- if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && \r
- (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
- ) {\r
+ //\r
+ // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
+ // machinename is ia32, ia64, x64, ...\r
+ //\r
+ FilePath = FileDevicePath (FileSystemHandles[Index], EFI_REMOVABLE_MEDIA_FILE_NAME);\r
+ NeedDelete = TRUE;\r
+ Status = gBS->LoadImage (\r
+ TRUE,\r
+ mBdsImageHandle,\r
+ FilePath,\r
+ NULL,\r
+ 0,\r
+ &ImageHandle\r
+ );\r
+ if (!EFI_ERROR(Status)) {\r
+ //\r
+ // Verify the image is a EFI application (and not a driver)\r
//\r
- // If the FileSystem protocol does not contain a BlkIo protocol,\r
- // then build it\r
+ Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
+ ASSERT (!EFI_ERROR(Status));\r
+\r
+ if (ImageInfo->ImageCodeType == EfiLoaderCode) {\r
+ NeedDelete = FALSE;\r
+ }\r
+ }\r
+\r
+ if (NeedDelete) {\r
//\r
+ // No such file or the file is not a EFI application, delete this boot option\r
+ //\r
+ BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
+ } else {\r
BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList);\r
BootOptionNumber++;\r
}\r
}\r
\r
if (NumberFileSystemHandles) {\r
- gBS->FreePool (FileSystemHandles);\r
+ FreePool (FileSystemHandles);\r
}\r
//\r
// Parse Network Boot Device\r
}\r
\r
if (NumberLoadFileHandles) {\r
- gBS->FreePool (LoadFileHandles);\r
+ FreePool (LoadFileHandles);\r
}\r
//\r
// Check if we have on flash shell\r
}\r
\r
if (FvHandleCount) {\r
- gBS->FreePool (FvHandleBuffer);\r
+ FreePool (FvHandleBuffer);\r
}\r
//\r
// Make sure every boot only have one time\r
/*++\r
\r
Routine Description:\r
- \r
+\r
Build the boot option with the handle parsed in\r
- \r
+\r
Arguments:\r
\r
Handle - The handle which present the device path to create boot option\r
- \r
+\r
BdsBootOptionList - The header of the link list which indexed all current\r
boot options\r
\r
/*++\r
\r
Routine Description:\r
- \r
+\r
Build the on flash shell boot option with the handle parsed in\r
- \r
+\r
Arguments:\r
\r
Handle - The handle which present the device path to create on flash shell\r
boot option\r
- \r
+\r
BdsBootOptionList - The header of the link list which indexed all current\r
boot options\r
\r
/*++\r
\r
Routine Description:\r
- \r
+\r
Boot from the EFI1.1 spec defined "BootNext" variable\r
- \r
+\r
Arguments:\r
\r
None\r
- \r
+\r
Returns:\r
\r
None\r