Search code examples
uefi

OpenProtocol returned 2 When I used OpenProtocol to open a handle


OpenProtocol returned 2 When I used OpenProtocol to open a handle which had been returned by LocateHandleBuffer.

the main code was shown below:

//Entry Point for this application
EFI_STATUS UefiMain (IN EFI_HANDLE  ImageHandle,IN EFI_SYSTEM_TABLE  *SystemTable)//ImageHandle and system table
{
    //Define and initialize all the variables.
    EFI_STATUS Status=EFI_SUCCESS;//zero
    UINTN HandleIndex=0,HandleCount=0;//the index for handle buffer and the num of handle elem
    EFI_HANDLE* DiskControllerHandles=NULL;//NULL
    EFI_DISK_IO_PROTOCOL* DiskIOProtocol=NULL;//NULL
    EFI_DEVICE_PATH_PROTOCOL* DiskDevicePathProtocol=NULL;//NULL
    //the return result
    Status=gBS->LocateHandleBuffer(ByProtocol,&gEfiDiskIoProtocolGuid,NULL,&HandleCount,&DiskControllerHandles);//Get all handles which support EFI_DISK_IO_PROTOCOL into the buffer.
    if(EFI_ERROR(Status))//LocateHandleBuffer failed.
    {
        Print(L"UefiMain:gBS->LocateHandleBuffer failed,error=%d.\r\n",Status);
        return EFI_SUCCESS;//failed
    }

    //Open the handle which supports EFI_DEVICE_PATH_TO_TEXT_PROTOCOL and was returned by LocateHandleBuffer previously.
    //Loop  
    for(HandleIndex=0;HandleIndex<HandleCount;HandleIndex++)
    {
        DiskIOProtocol=NULL;
        DiskDevicePathProtocol=NULL;

        //Open each handle which supports EFI_DEVICE_PATH_PROTOCOL.
        Status=gBS->OpenProtocol(DiskControllerHandles[HandleIndex],&gEfiDevicePathProtocolGuid,(VOID**)DiskDevicePathProtocol,
        ImageHandle,NULL,EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);//EFI_OPEN_PROTOCOL_GET_PROTOCOL);//Here 2 was returned. I haven't known why 2 was returned here after looking up Unified Extensible Firmware Interface 2.6
    }

    return EFI_SUCCESS;//return
}

Solution

  • the third parameter was not correct,so the correct code should be as below:

     Status=gBS->OpenProtocol(DiskControllerHandles[HandleIndex],&gEfiDevicePathProtocolGuid,(VOID**)&DiskDevicePathProtocol,
            ImageHandle,NULL,EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);