EFI_STATUS LibLocateProtocol ( IN EFI_GUID *ProtocolGuid, OUT VOID **Interface ) // // Find the first instance of this Protocol in the system and return it's interface // { EFI_STATUS Status; UINTN NumberHandles, Index; EFI_HANDLE *Handles; *Interface = NULL; Status = LibLocateHandle (ByProtocol, ProtocolGuid, NULL, &NumberHandles, &Handles); if (EFI_ERROR(Status)) { DEBUG((D_INFO, "LibLocateProtocol: Handle not found\n")); return Status; } for (Index=0; Index < NumberHandles; Index++) { Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], ProtocolGuid, Interface); if (!EFI_ERROR(Status)) { break; } } if (Handles) { FreePool (Handles); } return Status; }
static EFI_STATUS egFindESP(OUT EFI_FILE_HANDLE *RootDir) { EFI_STATUS Status; UINTN HandleCount = 0; EFI_HANDLE *Handles; Status = LibLocateHandle(ByProtocol, &ESPGuid, NULL, &HandleCount, &Handles); if (!EFI_ERROR(Status) && HandleCount > 0) { *RootDir = LibOpenRoot(Handles[0]); if (*RootDir == NULL) Status = EFI_NOT_FOUND; FreePool(Handles); } return Status; }
int pxe_init(bool quiet) { EFI_HANDLE *handles; EFI_STATUS status; UINTN nr_handles; status = LibLocateHandle(ByProtocol, &PxeBaseCodeProtocol, NULL, &nr_handles, &handles); if (status != EFI_SUCCESS) { if (!quiet) Print(L"No PXE Base Code Protocol\n"); return -1; } return 0; }
UINT16 * DevicePathStrFromProtocol ( IN VOID *Protocol, IN EFI_GUID *Guid ) { EFI_STATUS Status; UINTN HandleNum; EFI_HANDLE *HandleBuffer; EFI_DEVICE_PATH_PROTOCOL *DevicePath; UINTN Index; UINT16 *Str; VOID *Interface; //BOOLEAN Found; //Found = FALSE; Str = NULL; HandleNum = 0; Status = LibLocateHandle( ByProtocol, Guid, NULL, &HandleNum, &HandleBuffer ); if (EFI_ERROR(Status) || HandleNum == 0) { return NULL; } for ( Index = 0 ; Index < HandleNum ; Index ++ ) { Status = tBS->HandleProtocol (HandleBuffer[Index], Guid, &Interface); if (EFI_ERROR(Status)) { continue; } if (Interface == Protocol) { Status = tBS->HandleProtocol (HandleBuffer[Index], &gEfiDevicePathProtocolGuid, &DevicePath); if (!EFI_ERROR(Status)) { //Found = TRUE; Str = DevicePathToStr(DevicePath); } break; } } return Str; }
/** * create all the Dev Io Devices in this system. * @return EFI_SUCCESS the all the devices were gotten successfully. */ EFI_STATUS CreateAllDevIoDevices ( VOID ) { EFI_STATUS Status; UINTN HandleNum; EFI_HANDLE *HandleBuffer; EFI_DEVICE_IO_PROTOCOL *DeviceIo; EFI_DEVICE_PATH_PROTOCOL *DevicePath; UINTN Index; // //get all Device Io Protocol Interface. // HandleNum = 0; HandleBuffer = NULL; Status = LibLocateHandle ( ByProtocol, &gEfiDeviceIoProtocolGuid, NULL, &HandleNum, &HandleBuffer ); if (EFI_ERROR(Status) || HandleNum == 0) { return EFI_ABORTED; } gDevIoDevices = (DEVIO_DEVICE *)AllocateZeroPool ( sizeof (DEVIO_DEVICE) * HandleNum ); if (gDevIoDevices == NULL) { FreePool (HandleBuffer); return EFI_OUT_OF_RESOURCES; } for (Index = 0; Index < HandleNum; Index++) { Status = gtBS->HandleProtocol ( HandleBuffer[Index], &gEfiDeviceIoProtocolGuid, &DeviceIo ); if (EFI_ERROR(Status)) { FreePool (HandleBuffer); FreePool (gDevIoDevices); return Status; } gDevIoDevices[Index].DeviceIo = DeviceIo; Status = gtBS->HandleProtocol ( HandleBuffer[Index], &gEfiDevicePathProtocolGuid, &DevicePath ); if (EFI_ERROR(Status)) { FreePool (HandleBuffer); FreePool (gDevIoDevices); return Status; } gDevIoDevices[Index].DevicePath = DevicePath; } Print (L"found %d DeviceIo interface.", HandleNum); gDevIoDevNumber = HandleNum; FreePool (HandleBuffer); // //done successfully. // return EFI_SUCCESS; }
EFI_STATUS LibInitializeShellApplication ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { EFI_STATUS Status; EFI_HANDLE *HandleBuffer; UINTN HandleNum; UINTN HandleIndex; // // Shell app lib is a super set of the default lib. // Initialize the default lib first // InitializeShellLib (ImageHandle, SystemTable); // // Connect to the shell interface // Status = BS->HandleProtocol (ImageHandle, &ShellInterfaceProtocol, (VOID *) &SI); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "InitShellApp: Application not started from Shell\n")); BS->Exit (ImageHandle, Status, 0, NULL); } // // Connect to the shell environment // Status = BS->HandleProtocol ( ImageHandle, &ShellEnvProtocol, (VOID *) &SE2 ); if (EFI_ERROR (Status) || !(CompareGuid (&SE2->SESGuid, &SESGuid) == 0 && (SE2->MajorVersion > EFI_SHELL_MAJOR_VER || (SE2->MajorVersion == EFI_SHELL_MAJOR_VER && SE2->MinorVersion >= EFI_SHELL_MINOR_VER)) ) ) { Status = LibLocateHandle ( ByProtocol, &ShellEnvProtocol, NULL, &HandleNum, &HandleBuffer ); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "InitShellApp: Shell environment interfaces not found\n")); BS->Exit (ImageHandle, Status, 0, NULL); } Status = EFI_NOT_FOUND; for (HandleIndex = 0; HandleIndex < HandleNum; HandleIndex++) { BS->HandleProtocol ( HandleBuffer[HandleIndex], &ShellEnvProtocol, (VOID *) &SE2 ); if (CompareGuid (&SE2->SESGuid, &SESGuid) == 0 && (SE2->MajorVersion > EFI_SHELL_MAJOR_VER || (SE2->MajorVersion == EFI_SHELL_MAJOR_VER && SE2->MinorVersion >= EFI_SHELL_MINOR_VER) ) ) { Status = EFI_SUCCESS; break; } } FreePool (HandleBuffer); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "InitShellApp: Shell environment interfaces not found\n")); BS->Exit (ImageHandle, Status, Status, NULL); } } SE = (EFI_SHELL_ENVIRONMENT *) SE2; // // Disable the page break output mode in default // DisablePageBreak (); // // Disable the tab key pause output mode in default DisableOutputTabPause (); // // Done with init // return Status; }
/** * get the device path and file path based on the loaded image name. * @param Name the iamge file name such as framework.efi * @param DevicePath the Device path of this file is loaded from. * @param FilePath the file path of this file. * @return EFI_SUCCESS the device path and file path was found successfully. * @return EFI_INVALID_PARAMETER the Parameter is invalid */ EFI_STATUS GetFilePathByName ( IN CHAR16 *Name, OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, OUT CHAR16 **FilePath ) { EFI_STATUS Status; UINTN Index; EFI_LOADED_IMAGE_PROTOCOL *Image; EFI_HANDLE *HandleBuffer; UINTN HandleNum; EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; EFI_DEVICE_PATH_PROTOCOL *TempDeviceNode; CHAR16 *TempFilePath; CHAR16 FullFilePath[MAX_FILE_PATH]; BOOLEAN Found; // //verify parameters. // if (Name == NULL || DevicePath == NULL || FilePath == NULL) { return EFI_INVALID_PARAMETER; } if (StrLen (Name) == 0) { return EFI_INVALID_PARAMETER; } // //get all the load image protocol instance. // Found = FALSE; HandleNum = 0; HandleBuffer = NULL; Status = LibLocateHandle( ByProtocol, &gEfiLoadedImageProtocolGuid, NULL, &HandleNum, &HandleBuffer ); if (EFI_ERROR(Status) || HandleNum == 0) { return EFI_ABORTED; } // //for all the LoadedImage protocol found the image file name to match the //given file name. // TempDevicePath = NULL; for (Index = 0; Index < HandleNum; Index ++ ) { FullFilePath[0] = '\0'; // // Get the image instance from the image handle // Status = tBS->HandleProtocol ( HandleBuffer[Index], &gEfiLoadedImageProtocolGuid, &Image ); if (EFI_ERROR(Status)) { return Status; } if (Image->FilePath == NULL) { continue; } // //get the file path and parse the file name. // TempDevicePath = UnpackDevicePath (Image->FilePath); TempFilePath = NULL; TempDeviceNode = TempDevicePath; while (!IsDevicePathEnd(TempDeviceNode)) { if ((DevicePathType(TempDeviceNode) == MEDIA_DEVICE_PATH) && (DevicePathSubType(TempDeviceNode) == MEDIA_FILEPATH_DP)) { StrCat(FullFilePath, L"\\"); TempFilePath = ((FILEPATH_DEVICE_PATH *)TempDeviceNode)->PathName; if (StrLen (TempFilePath) == 1 && TempFilePath[0] == '\\') { // //if this the "\\" path then we need not append it,or else there will //have 3 '\\' in the device path. // ; } else { StrCat(FullFilePath, TempFilePath); } } TempDeviceNode = NextDevicePathNode (TempDeviceNode); } tBS->FreePool (TempDevicePath); if (StrLen (FullFilePath) <= StrLen (Name)) { continue; } TempFilePath = FullFilePath + (StrLen(FullFilePath) - StrLen(Name)); if ((*(TempFilePath - 1)) == L'\\' && StriCmp (TempFilePath, Name) == 0) { TempFilePath[0] = '\0'; // // Get the device instance from the device handle // Status = tBS->HandleProtocol ( Image->DeviceHandle, &gEfiDevicePathProtocolGuid, &TempDevicePath ); if (EFI_ERROR(Status)) { return Status; } Found = TRUE; break; } } if (HandleBuffer != NULL) { tBS->FreePool (HandleBuffer); } if (!Found) { return EFI_NOT_FOUND; } // // If the file path is only a root directory "\\", remove it // if (StrLen(FullFilePath) > 1) { if (FullFilePath[StrLen(FullFilePath) - 1] == L'\\') { FullFilePath[StrLen(FullFilePath) - 1] = '\0'; } } *DevicePath = DuplicateDevicePath (TempDevicePath); if (*DevicePath == NULL) { return EFI_OUT_OF_RESOURCES; } // //skip the first '\\'. // *FilePath = StrDuplicate (FullFilePath + 1); if (*FilePath == NULL) { tBS->FreePool (*DevicePath); *DevicePath = NULL; return EFI_OUT_OF_RESOURCES; } // // Done, return status code EFI_SUCCESS // return EFI_SUCCESS; }