--- /dev/null
+/*++\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
+\r
+Module Name:\r
+\r
+ BdsBoot.c\r
+\r
+Abstract:\r
+\r
+ BDS Lib functions which relate with create or process the boot\r
+ option.\r
+\r
+--*/\r
+#include "Performance.h"\r
+\r
+BOOLEAN mEnumBootDevice = FALSE;\r
+\r
+EFI_STATUS\r
+BdsLibDoLegacyBoot (\r
+ IN BDS_COMMON_OPTION *Option\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ \r
+ Boot the legacy system with the boot option\r
+\r
+Arguments:\r
+\r
+ Option - The legacy boot option which have BBS device path\r
+\r
+Returns:\r
+\r
+ EFI_UNSUPPORTED - There is no legacybios protocol, do not support\r
+ legacy boot.\r
+ \r
+ EFI_STATUS - Return the status of LegacyBios->LegacyBoot ().\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
+\r
+ Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL,
+ (void **)&LegacyBios);\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // If no LegacyBios protocol we do not support legacy boot\r
+ //\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ //\r
+ // Notes: if we seperate the int 19, then we don't need to refresh BBS\r
+ //\r
+ BdsRefreshBbsTableForBoot (Option);\r
+\r
+ //\r
+ // Write boot to OS performance data to a file\r
+ //\r
+ PERF_CODE (\r
+ WriteBootToOsPerformanceData ();\r
+ );\r
+\r
+\r
+ DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Legacy Boot: %S\n", Option->Description));\r
+ return LegacyBios->LegacyBoot (\r
+ LegacyBios,\r
+ (BBS_BBS_DEVICE_PATH *) Option->DevicePath,\r
+ Option->LoadOptionsSize,\r
+ Option->LoadOptions\r
+ );\r
+}\r
+\r
+EFI_STATUS\r
+BdsLibBootViaBootOption (\r
+ IN BDS_COMMON_OPTION * Option,\r
+ IN EFI_DEVICE_PATH_PROTOCOL * DevicePath,\r
+ OUT UINTN *ExitDataSize,\r
+ OUT CHAR16 **ExitData OPTIONAL\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\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
+ to boot the legacy OS\r
+\r
+ ExitDataSize - Returned directly from gBS->StartImage ()\r
+\r
+ ExitData - Returned directly from gBS->StartImage ()\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Status from gBS->StartImage (),\r
+ or BdsBootByDiskSignatureAndPartition ()\r
+\r
+ EFI_NOT_FOUND - If the Device Path is not found in the system\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE Handle;\r
+ EFI_HANDLE ImageHandle;\r
+ EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\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
+ *ExitDataSize = 0;\r
+ *ExitData = NULL;\r
+\r
+ //\r
+ // Notes: put EFI64 ROM Shadow Solution\r
+ //\r
+ EFI64_SHADOW_ALL_LEGACY_ROM ();\r
+\r
+ //\r
+ // Notes: this code can be remove after the s3 script table\r
+ // hook on the event EFI_EVENT_SIGNAL_READY_TO_BOOT or\r
+ // EFI_EVENT_SIGNAL_LEGACY_BOOT\r
+ //\r
+ Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL,
+ (VOID **)&AcpiS3Save);\r
+ if (!EFI_ERROR (Status)) {\r
+ AcpiS3Save->S3Save (AcpiS3Save, NULL);\r
+ }\r
+ //\r
+ // If it's Device Path that starts with a hard drive path,\r
+ // this routine will do the booting.\r
+ //\r
+ Status = BdsBootByDiskSignatureAndPartition (\r
+ Option,\r
+ (HARDDRIVE_DEVICE_PATH *) DevicePath,\r
+ Option->LoadOptionsSize,\r
+ Option->LoadOptions,\r
+ ExitDataSize,\r
+ ExitData\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // If we found a disk signature and partition device path return success\r
+ //\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ EfiSignalEventReadyToBoot ();\r
+\r
+ //\r
+ // Set Boot Current\r
+ //\r
+ gRT->SetVariable (\r
+ L"BootCurrent",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+ sizeof (UINT16),\r
+ &Option->BootCurrent\r
+ );\r
+\r
+ if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r
+ (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r
+ ) {\r
+ //\r
+ // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
+ //\r
+ return BdsLibDoLegacyBoot (Option);\r
+ }\r
+\r
+ DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Booting EFI 1.1 way %S\n", Option->Description));\r
+\r
+ Status = gBS->LoadImage (\r
+ TRUE,\r
+ mBdsImageHandle,\r
+ DevicePath,\r
+ NULL,\r
+ 0,\r
+ &ImageHandle\r
+ );\r
+\r
+ //\r
+ // If we didn't find an image, we may need to load the default\r
+ // boot behavior for the device.\r
+ //\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // Find a Simple File System protocol on the device path. If the remaining\r
+ // device path is set to end then no Files are being specified, so try\r
+ // the removable media file name.\r
+ //\r
+ TempDevicePath = DevicePath;\r
+ Status = gBS->LocateDevicePath (\r
+ &gEfiSimpleFileSystemProtocolGuid,\r
+ &TempDevicePath,\r
+ &Handle\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
+ gBS->FreePool (Buffer);\r
+ }\r
+ }\r
+\r
+ Status = gBS->LoadImage (\r
+ TRUE,\r
+ mBdsImageHandle,\r
+ FilePath,\r
+ NULL,\r
+ 0,\r
+ &ImageHandle\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // The DevicePath failed, and it's not a valid\r
+ // removable media device.\r
+ //\r
+ goto Done;\r
+ }\r
+ }\r
+ } else {\r
+ Status = EFI_NOT_FOUND;\r
+ }\r
+ }\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // It there is any error from the Boot attempt exit now.\r
+ //\r
+ goto Done;\r
+ }\r
+ //\r
+ // Provide the image with it's load options\r
+ //\r
+ Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid,
+ (VOID **) &ImageInfo);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ if (Option->LoadOptionsSize != 0) {\r
+ ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
+ ImageInfo->LoadOptions = Option->LoadOptions;\r
+ }\r
+ //\r
+ // Before calling the image, enable the Watchdog Timer for\r
+ // the 5 Minute period\r
+ //\r
+ gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
+\r
+ Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
+ DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Image Return Status = %r\n", Status));\r
+\r
+ //\r
+ // Clear the Watchdog Timer after the image returns\r
+ //\r
+ gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
+\r
+Done:\r
+ //\r
+ // Clear Boot Current\r
+ //\r
+ gRT->SetVariable (\r
+ L"BootCurrent",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+ 0,\r
+ &Option->BootCurrent\r
+ );\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+BdsBootByDiskSignatureAndPartition (\r
+ IN BDS_COMMON_OPTION * Option,\r
+ IN HARDDRIVE_DEVICE_PATH * HardDriveDevicePath,\r
+ IN UINT32 LoadOptionsSize,\r
+ IN VOID *LoadOptions,\r
+ OUT UINTN *ExitDataSize,\r
+ OUT CHAR16 **ExitData OPTIONAL\r
+ )\r
+/*++\r
+\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
+Arguments:\r
+\r
+ Option - The current processing boot option.\r
+\r
+ HardDriveDevicePath - EFI Device Path to boot, if it starts with a hard\r
+ drive device path.\r
+\r
+ LoadOptionsSize - Passed into gBS->StartImage ()\r
+ via the loaded image protocol.\r
+\r
+ LoadOptions - Passed into gBS->StartImage ()\r
+ via the loaded image protocol.\r
+\r
+ ExitDataSize - returned directly from gBS->StartImage ()\r
+\r
+ ExitData - returned directly from gBS->StartImage ()\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Status from gBS->StartImage (),\r
+ or BootByDiskSignatureAndPartition ()\r
+ \r
+ EFI_NOT_FOUND - If the Device Path is not found in the system\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN BlockIoHandleCount;\r
+ EFI_HANDLE *BlockIoBuffer;\r
+ EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePath;\r
+ HARDDRIVE_DEVICE_PATH *TmpHdPath;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
+ UINTN Index;\r
+ BOOLEAN DevicePathMatch;\r
+ HARDDRIVE_DEVICE_PATH *TempPath;\r
+\r
+ *ExitDataSize = 0;\r
+ *ExitData = NULL;\r
+\r
+ if ( !((DevicePathType (&HardDriveDevicePath->Header) == MEDIA_DEVICE_PATH) &&\r
+ (DevicePathSubType (&HardDriveDevicePath->Header) == MEDIA_HARDDRIVE_DP))\r
+ ) {\r
+ //\r
+ // If the HardDriveDevicePath does not start with a Hard Drive Device Path\r
+ // exit.\r
+ //\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ //\r
+ // The boot device have already been connected\r
+ //\r
+ Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
+ if (EFI_ERROR (Status) || BlockIoHandleCount == 0) {\r
+ //\r
+ // If there was an error or there are no device handles that support\r
+ // the BLOCK_IO Protocol, then return.\r
+ //\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ //\r
+ // Loop through all the device handles that support the BLOCK_IO Protocol\r
+ //\r
+ for (Index = 0; Index < BlockIoHandleCount; Index++) {\r
+\r
+ Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r
+ if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r
+ continue;\r
+ }\r
+ //\r
+ // Make PreviousDevicePath == the device path node before the end node\r
+ //\r
+ DevicePath = BlockIoDevicePath;\r
+ BlockIoHdDevicePath = NULL;\r
+\r
+ //\r
+ // find HardDriver device path node\r
+ //\r
+ while (!IsDevicePathEnd (DevicePath)) {\r
+ if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && \r
+ (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
+ ) {\r
+ BlockIoHdDevicePath = DevicePath;\r
+ break;\r
+ }\r
+\r
+ DevicePath = NextDevicePathNode (DevicePath);\r
+ }\r
+\r
+ if (BlockIoHdDevicePath == NULL) {\r
+ continue;\r
+ }\r
+ //\r
+ // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
+ //\r
+ DevicePathMatch = FALSE;\r
+\r
+ TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePath;\r
+ TempPath = (HARDDRIVE_DEVICE_PATH *) BdsLibUnpackDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
+\r
+ //\r
+ // Only several fields will be checked. NOT whole NODE\r
+ //\r
+ if ( TmpHdPath->PartitionNumber == TempPath->PartitionNumber &&\r
+ TmpHdPath->MBRType == TempPath->MBRType &&\r
+ TmpHdPath->SignatureType == TempPath->SignatureType &&\r
+ CompareGuid ((EFI_GUID *) TmpHdPath->Signature, (EFI_GUID *) TempPath->Signature)) {\r
+ //\r
+ // Get the matched device path\r
+ //\r
+ DevicePathMatch = TRUE;\r
+ }\r
+ //\r
+ // Only do the boot, when devicepath match\r
+ //\r
+ if (DevicePathMatch) {\r
+ //\r
+ // Combine the Block IO and Hard Drive Device path together and try\r
+ // to boot from it.\r
+ //\r
+ DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
+ NewDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
+\r
+ //\r
+ // Recursive boot with new device path\r
+ //\r
+ Status = BdsLibBootViaBootOption (Option, NewDevicePath, ExitDataSize, ExitData);\r
+ if (!EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ gBS->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
+ gBS->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
+ gBS->FreePool (BootOptionVar);\r
+ break;\r
+ }\r
+\r
+ gBS->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
+ gBS->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
+ gBS->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
+ gBS->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
+ gBS->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
+ gBS->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
+ gBS->FreePool (BootOrder);\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+BdsLibEnumerateAllBootOption (\r
+ IN OUT LIST_ENTRY *BdsBootOptionList\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ This function will enumerate all possible boot device in the system,\r
+ it will only excute once of every boot.\r
+\r
+Arguments:\r
+\r
+ BdsBootOptionList - The header of the link list which indexed all\r
+ current boot options\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Finished all the boot device enumerate and create\r
+ the boot option base on that boot device\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ UINT16 BootOptionNumber;\r
+ UINTN NumberFileSystemHandles;\r
+ EFI_HANDLE *FileSystemHandles;\r
+ EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
+ UINTN Index;\r
+ UINTN NumberLoadFileHandles;\r
+ EFI_HANDLE *LoadFileHandles;\r
+ VOID *ProtocolInstance;\r
+ EFI_FIRMWARE_VOLUME_PROTOCOL *Fv;\r
+ UINTN FvHandleCount;\r
+ EFI_HANDLE *FvHandleBuffer;\r
+ EFI_FV_FILETYPE Type;\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
+ //\r
+ // If the boot device enumerate happened, just get the boot\r
+ // device from the boot order variable\r
+ //\r
+ if (mEnumBootDevice) {\r
+ BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
+ return EFI_SUCCESS;\r
+ }\r
+ //\r
+ // Notes: this dirty code is to get the legacy boot option from the\r
+ // BBS table and create to variable as the EFI boot option, it should\r
+ // be removed after the CSM can provide legacy boot option directly\r
+ //\r
+ REFRESH_LEGACY_BOOT_OPTIONS;\r
+\r
+ //\r
+ // Delete invalid boot option\r
+ //\r
+ BdsDeleteAllInvalidEfiBootOption ();\r
+ //\r
+ // Parse removable media\r
+ //\r
+ gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiSimpleFileSystemProtocolGuid,\r
+ NULL,\r
+ &NumberFileSystemHandles,\r
+ &FileSystemHandles\r
+ );\r
+ for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
+ Status = gBS->HandleProtocol (\r
+ FileSystemHandles[Index],\r
+ &gEfiBlockIoProtocolGuid,\r
+ (VOID **) &BlkIo\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ if (!BlkIo->Media->RemovableMedia) {\r
+ //\r
+ // If the file system handle supports a BlkIo protocol,\r
+ // skip the removable media devices\r
+ //\r
+ continue;\r
+ }\r
+ }\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
+ 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
+ }\r
+ //\r
+ // Parse Network Boot Device\r
+ //\r
+ gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiSimpleNetworkProtocolGuid,\r
+ NULL,\r
+ &NumberLoadFileHandles,\r
+ &LoadFileHandles\r
+ );\r
+ for (Index = 0; Index < NumberLoadFileHandles; Index++) {\r
+ Status = gBS->HandleProtocol (\r
+ LoadFileHandles[Index],\r
+ &gEfiLoadFileProtocolGuid,\r
+ (VOID **) &ProtocolInstance\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList);\r
+ BootOptionNumber++;\r
+ }\r
+\r
+ if (NumberLoadFileHandles) {\r
+ gBS->FreePool (LoadFileHandles);\r
+ }\r
+ //\r
+ // Check if we have on flash shell\r
+ //\r
+ gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiFirmwareVolumeProtocolGuid,\r
+ NULL,\r
+ &FvHandleCount,\r
+ &FvHandleBuffer\r
+ );\r
+ for (Index = 0; Index < FvHandleCount; Index++) {\r
+ gBS->HandleProtocol (\r
+ FvHandleBuffer[Index],\r
+ &gEfiFirmwareVolumeProtocolGuid,\r
+ (VOID **) &Fv\r
+ );\r
+\r
+ Status = Fv->ReadFile (\r
+ Fv,\r
+ &gEfiShellFileGuid,\r
+ NULL,\r
+ &Size,\r
+ &Type,\r
+ &Attributes,\r
+ &AuthenticationStatus\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // Skip if no shell file in the FV\r
+ //\r
+ continue;\r
+ }\r
+ //\r
+ // Build the shell boot option\r
+ //\r
+ BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
+ BootOptionNumber++;\r
+ }\r
+\r
+ if (FvHandleCount) {\r
+ gBS->FreePool (FvHandleBuffer);\r
+ }\r
+ //\r
+ // Make sure every boot only have one time\r
+ // boot device enumerate\r
+ //\r
+ BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
+ mEnumBootDevice = TRUE;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+VOID\r
+BdsLibBuildOptionFromHandle (\r
+ IN EFI_HANDLE Handle,\r
+ IN LIST_ENTRY *BdsBootOptionList\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ \r
+ Build the boot option with the handle parsed in\r
+ \r
+Arguments:\r
+\r
+ Handle - The handle which present the device path to create boot option\r
+ \r
+ BdsBootOptionList - The header of the link list which indexed all current\r
+ boot options\r
+\r
+Returns:\r
+\r
+ VOID\r
+\r
+--*/\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ CHAR16 *TempString;\r
+\r
+ DevicePath = DevicePathFromHandle (Handle);\r
+ TempString = DevicePathToStr (DevicePath);\r
+\r
+ //\r
+ // Create and register new boot option\r
+ //\r
+ BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, TempString, L"BootOrder");\r
+}\r
+\r
+VOID\r
+BdsLibBuildOptionFromShell (\r
+ IN EFI_HANDLE Handle,\r
+ IN OUT LIST_ENTRY *BdsBootOptionList\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ \r
+ Build the on flash shell boot option with the handle parsed in\r
+ \r
+Arguments:\r
+\r
+ Handle - The handle which present the device path to create on flash shell\r
+ boot option\r
+ \r
+ BdsBootOptionList - The header of the link list which indexed all current\r
+ boot options\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
+\r
+ DevicePath = DevicePathFromHandle (Handle);\r
+\r
+ //\r
+ // Build the shell device path\r
+ //\r
+ EfiInitializeFwVolDevicepathNode (&ShellNode, &gEfiShellFileGuid);\r
+ DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
+\r
+ //\r
+ // Create and register the shell boot option\r
+ //\r
+ BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"Internal EFI Shell", L"BootOrder");\r
+\r
+}\r
+\r
+VOID\r
+BdsLibBootNext (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ \r
+ Boot from the EFI1.1 spec defined "BootNext" variable\r
+ \r
+Arguments:\r
+\r
+ None\r
+ \r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+{\r
+ UINT16 *BootNext;\r
+ UINTN BootNextSize;\r
+ CHAR16 Buffer[20];\r
+ BDS_COMMON_OPTION *BootOption;\r
+ LIST_ENTRY TempList;\r
+ UINTN ExitDataSize;\r
+ CHAR16 *ExitData;\r
+\r
+ //\r
+ // Init the boot option name buffer and temp link list\r
+ //\r
+ InitializeListHead (&TempList);\r
+ ZeroMem (Buffer, sizeof (Buffer));\r
+\r
+ BootNext = BdsLibGetVariableAndSize (\r
+ L"BootNext",\r
+ &gEfiGlobalVariableGuid,\r
+ &BootNextSize\r
+ );\r
+\r
+ //\r
+ // Clear the boot next variable first\r
+ //\r
+ if (BootNext != NULL) {\r
+ gRT->SetVariable (\r
+ L"BootNext",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ 0,\r
+ BootNext\r
+ );\r
+\r
+ //\r
+ // Start to build the boot option and try to boot\r
+ //\r
+ UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
+ BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
+ BdsLibConnectDevicePath (BootOption->DevicePath);\r
+ BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
+ }\r
+\r
+}\r