EFI_STATUS SelectAndBootDevice()

in PcBdsPkg/MsBootPolicy/MsBootPolicy.c [244:324]


EFI_STATUS SelectAndBootDevice(EFI_GUID *ByGuid, FILTER_ROUTINE ByFilter) {
    EFI_STATUS                   Status;
    EFI_HANDLE                  *Handles;
    UINTN                        HandleCount;
    UINTN                        Index;
    EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
    CHAR16                      *TmpStr;
    EFI_DEVICE_PATH_PROTOCOL    *DevicePath;

    Status = gBS->LocateHandleBuffer(
        ByProtocol,
        ByGuid,
        NULL,
        &HandleCount,
        &Handles
        );
    if (EFI_ERROR(Status)) {
        DEBUG((DEBUG_ERROR,"Unable to locate any %g handles - code=%r\n",ByGuid,Status));
        return Status;
    }
    DEBUG((DEBUG_INFO,"Found %d handles\n",HandleCount));
    DisplayDevicePaths(Handles, HandleCount);
    FilterHandles(Handles, &HandleCount, ByFilter);
    DEBUG((DEBUG_INFO,"%d handles survived filtering\n",HandleCount));
    if (HandleCount == 0) {
        DEBUG((DEBUG_WARN, "No handles survived filtering!\n"));
        return EFI_NOT_FOUND;
    }

    SortHandles(Handles, HandleCount);

    Status = EFI_DEVICE_ERROR;
    for (Index = 0; Index < HandleCount; Index++) {
        DevicePath = DevicePathFromHandle(Handles[Index]);
        if (DevicePath == NULL) {
          DEBUG((DEBUG_ERROR, "DevicePathFromHandle(%p) FAILED\n", Handles[Index]));
          continue;
        }
        TmpStr = ConvertDevicePathToText (DevicePath,TRUE,TRUE);
        if (TmpStr == NULL) {
          DEBUG((DEBUG_ERROR,"ConvertDevicePathToText(%p) FAILED ",DevicePath));
          continue;
        }
        DEBUG((DEBUG_INFO,"Selecting device %s",TmpStr));
        DEBUG((DEBUG_INFO,"\n"));
        if (MsBootPolicyLibIsDeviceBootable(Handles[Index])) {
            EfiBootManagerInitializeLoadOption(
                &BootOption,
                LoadOptionNumberUnassigned,
                LoadOptionTypeBoot,
                LOAD_OPTION_ACTIVE,
                L"MsTemp",
                DevicePathFromHandle(Handles[Index]),
                NULL,
                0
                );

            if (ByFilter == FilterOnlyIPv4 || ByFilter == FilterOnlyIPv6 || ByFilter == FilterOnlyUSB) {
                DEBUG((DEBUG_INFO, "Attempting alternate boot...\n"));
                Status = SetAltBoot();
                if (EFI_ERROR(Status) != FALSE) {
                  DEBUG((DEBUG_ERROR, "Alternate boot set failed %r...\n", Status));
                }
            }

            EfiBootManagerBoot(&BootOption);
            Status = BootOption.Status;

            EfiBootManagerFreeLoadOption(&BootOption);
            // if EFI_SUCCESS, device was booted, and the return is back to setup
            if (Status == EFI_SUCCESS) {
                break;
            }
        } else {
            DEBUG((DEBUG_WARN,"Device %s\n",TmpStr));
            DEBUG((DEBUG_WARN," was blocked from booting\n"));
        }
        FreePool(TmpStr);
    }
    return Status;
}