netlib.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,442 行 · 第 1/3 页

C
1,442
字号
  
  //
  // Disconnect the driver specified by ImageHandle from all
  // the devices in the handle database.
  //
  for (Index = 0; Index < DeviceHandleCount; Index++) {
    Status = gBS->DisconnectController (
                    DeviceHandleBuffer[Index],
                    ImageHandle,
                    NULL
                    );
  }
  
  //
  // Uninstall all the protocols installed in the driver entry point
  //
  for (Index = 0; Index < DeviceHandleCount; Index++) {
    Status = gBS->HandleProtocol (
                    DeviceHandleBuffer[Index],
                    &gEfiDriverBindingProtocolGuid,
                    &DriverBinding
                    );
    
    if (EFI_ERROR (Status)) {
      continue;
    }

    if (DriverBinding->ImageHandle != ImageHandle) {
      continue;
    }

    gBS->UninstallProtocolInterface (
          ImageHandle,
          &gEfiDriverBindingProtocolGuid,
          DriverBinding
          );
    
    Status = gBS->HandleProtocol (
                    DeviceHandleBuffer[Index],
                    &gEfiComponentNameProtocolGuid,
                    &ComponentName
                    );
    
    if (!EFI_ERROR (Status)) {
      gBS->UninstallProtocolInterface (
            ImageHandle,
            &gEfiComponentNameProtocolGuid,
            ComponentName
            );
    }

    Status = gBS->HandleProtocol (
                    DeviceHandleBuffer[Index],
                    &gEfiDriverConfigurationProtocolGuid,
                    &DriverConfiguration
                    );
    
    if (!EFI_ERROR (Status)) {
      gBS->UninstallProtocolInterface (
            ImageHandle,
            &gEfiDriverConfigurationProtocolGuid,
            DriverConfiguration
            );
    }

    Status = gBS->HandleProtocol (
                    DeviceHandleBuffer[Index],
                    &gEfiDriverDiagnosticsProtocolGuid,
                    &DriverDiagnostics
                    );
    
    if (!EFI_ERROR (Status)) {
      gBS->UninstallProtocolInterface (
            ImageHandle,
            &gEfiDriverDiagnosticsProtocolGuid,
            DriverDiagnostics
            );
    }
  }
  
  //
  // Free the buffer containing the list of handles from the handle database
  //
  if (DeviceHandleBuffer != NULL) {
    gBS->FreePool (DeviceHandleBuffer);
  }

  return EFI_SUCCESS;
}


EFI_STATUS
NetLibCreateServiceChild (
  IN  EFI_HANDLE            Controller,
  IN  EFI_HANDLE            Image,
  IN  EFI_GUID              *ServiceBindingGuid,
  OUT EFI_HANDLE            *ChildHandle
  )
/*++

Routine Description:

  Create a child of the service that is identified by ServiceBindingGuid.

Arguments:

  Controller          - The controller which has the service installed.
  Image               - The image handle used to open service.
  ServiceBindingGuid  - The service's Guid.
  ChildHandle         - The handle to receive the create child

Returns:

  EFI_SUCCESS  - The child is successfully created.
  Others       - Failed to create the child.

--*/
{
  EFI_STATUS                    Status;
  EFI_SERVICE_BINDING_PROTOCOL  *Service;

  
  ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));
  
  //
  // Get the ServiceBinding Protocol
  //
  Status = gBS->OpenProtocol (
                  Controller,
                  ServiceBindingGuid,
                  (VOID **) &Service,
                  Image,
                  Controller,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );

  if (EFI_ERROR (Status)) {
    return Status;
  }
  
  //
  // Create a child
  //
  Status = Service->CreateChild (Service, ChildHandle);
  return Status;
}

EFI_STATUS
NetLibDestroyServiceChild (
  IN  EFI_HANDLE            Controller,
  IN  EFI_HANDLE            Image,
  IN  EFI_GUID              *ServiceBindingGuid,
  IN  EFI_HANDLE            ChildHandle
  )
/*++

Routine Description:

  Destory a child of the service that is identified by ServiceBindingGuid.

Arguments:

  Controller          - The controller which has the service installed.
  Image               - The image handle used to open service.
  ServiceBindingGuid  - The service's Guid.
  ChildHandle         - The child to destory

Returns:

  EFI_SUCCESS  - The child is successfully destoried.
  Others       - Failed to destory the child.

--*/
{
  EFI_STATUS                    Status;
  EFI_SERVICE_BINDING_PROTOCOL  *Service;

  ASSERT (ServiceBindingGuid != NULL);

  //
  // Get the ServiceBinding Protocol
  //
  Status = gBS->OpenProtocol (
                  Controller,
                  ServiceBindingGuid,
                  (VOID **) &Service,
                  Image,
                  Controller,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );

  if (EFI_ERROR (Status)) {
    return Status;
  }
  
  //
  // destory the child
  //
  Status = Service->DestroyChild (Service, ChildHandle);
  return Status;
}

EFI_STATUS
NetLibGetMacString (
  IN           EFI_HANDLE  SnpHandle,
  IN           EFI_HANDLE  ImageHandle,
  IN OUT CONST CHAR16      **MacString
  )
/*++

Routine Description:

  Convert the mac address of the simple network protocol installed on
  SnpHandle to a unicode string. Callers are responsible for freeing the
  string storage.

Arguments:

  SnpHandle    - The handle where the simple network protocol is installed on.
  ImageHandle  - The image handle used to act as the agent handle to get the simple
                 network protocol.
  MacString    - The pointer to store the address of the string representation of 
                 the mac address.

Returns:

  EFI_OUT_OF_RESOURCES - There are not enough memory resource.
  other                - Failed to open the simple network protocol.

--*/
{
  EFI_STATUS                   Status;
  EFI_SIMPLE_NETWORK_PROTOCOL  *Snp;
  EFI_SIMPLE_NETWORK_MODE      *Mode;
  CHAR16                       *MacAddress;
  UINTN                        Index;

  *MacString = NULL;

  //
  // Get the Simple Network protocol from the SnpHandle.
  //
  Status = gBS->OpenProtocol (
                  SnpHandle,
                  &gEfiSimpleNetworkProtocolGuid,
                  (VOID **) &Snp,
                  ImageHandle,
                  SnpHandle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Mode = Snp->Mode;

  //
  // It takes 2 unicode characters to represent a 1 byte binary buffer.
  // Plus one unicode character for the null-terminator.
  //
  MacAddress = NetAllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));
  if (MacAddress == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  //
  // Convert the mac address into a unicode string.
  //
  for (Index = 0; Index < Mode->HwAddressSize; Index++) {
    MacAddress[Index * 2]     = NibbleToHexChar (Mode->CurrentAddress.Addr[Index] >> 4);
    MacAddress[Index * 2 + 1] = NibbleToHexChar (Mode->CurrentAddress.Addr[Index]);
  }

  MacAddress[Mode->HwAddressSize * 2] = L'\0';

  *MacString = MacAddress;

  return EFI_SUCCESS;
}

EFI_HANDLE
NetLibGetNicHandle (
  IN EFI_HANDLE             Controller,
  IN EFI_GUID               *ProtocolGuid
  )
/*++

Routine Description:

  Find the UNDI/SNP handle from controller and protocol GUID.
  For example, IP will open a MNP child to transmit/receive 
  packets, when MNP is stopped, IP should also be stopped. IP
  needs to find its own private data which is related the IP's 
  service binding instance that is install on UNDI/SNP handle. 
  Now, the controller is either a MNP or ARP child handle. But
  IP opens these handle BY_DRIVER, use that info, we can get the
  UNDI/SNP handle.

Arguments:

  Controller    - Then protocol handle to check
  ProtocolGuid  - The protocol that is related with the handle.

Returns:

  The UNDI/SNP handle or NULL.

--*/
{
  EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;
  EFI_HANDLE                          Handle;
  EFI_STATUS                          Status;
  UINTN                               OpenCount;
  UINTN                               Index;

  Status = gBS->OpenProtocolInformation (
                  Controller,
                  ProtocolGuid,
                  &OpenBuffer,
                  &OpenCount
                  );

  if (EFI_ERROR (Status)) {
    return NULL;
  }
  
  Handle = NULL;
  
  for (Index = 0; Index < OpenCount; Index++) {
    if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
      Handle = OpenBuffer[Index].ControllerHandle;
      break;
    }
  }

  gBS->FreePool (OpenBuffer);
  return Handle;
}

EFI_STATUS
NetLibInstallAllDriverProtocols (
  IN EFI_HANDLE                         ImageHandle,
  IN EFI_SYSTEM_TABLE                   *SystemTable,
  IN EFI_DRIVER_BINDING_PROTOCOL        *DriverBinding,
  IN EFI_HANDLE                         DriverBindingHandle,
  IN EFI_COMPONENT_NAME_PROTOCOL        *ComponentName,       OPTIONAL
  IN EFI_DRIVER_CONFIGURATION_PROTOCOL  *DriverConfiguration, OPTIONAL
  IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL    *DriverDiagnostics    OPTIONAL
  )
/*++

Routine Description:

  Intialize a driver by installing the Driver Binding Protocol onto the 
  driver's DriverBindingHandle.  This is typically the same as the driver's
  ImageHandle, but it can be different if the driver produces multiple
  DriverBinding Protocols.  This function also initializes the EFI Driver
  Library that initializes the global variables gST, gBS, gRT.

Arguments:

  ImageHandle         - The image handle of the driver
  SystemTable         - The EFI System Table that was passed to the driver's
                        entry point
  DriverBinding       - A Driver Binding Protocol instance that this driver
                        is producing.
  DriverBindingHandle - The handle that DriverBinding is to be installe onto.
                        If this parameter is NULL, then a new handle is created.
  ComponentName       - A Component Name Protocol instance that this driver is
                        producing.
  DriverConfiguration - A Driver Configuration Protocol instance that this
                        driver is producing.  
  DriverDiagnostics   - A Driver Diagnostics Protocol instance that this
                        driver is producing.

Returns: 

  EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
  Otherwise, then return status from gBS->InstallProtocolInterface()

--*/
{
  return NetLibInstallAllDriverProtocolsWithUnload (
           ImageHandle,
           SystemTable,
           DriverBinding,
           DriverBindingHandle,
           ComponentName,
           DriverConfiguration,
           DriverDiagnostics,
           NetLibDefaultUnload
           );
}

EFI_STATUS
NetLibInstallAllDriverProtocolsWithUnload (
  IN EFI_HANDLE                         ImageHandle,
  IN EFI_SYSTEM_TABLE                   *SystemTable,
  IN EFI_DRIVER_BINDING_PROTOCOL        *DriverBinding,
  IN EFI_HANDLE                         DriverBindingHandle,
  IN EFI_COMPONENT_NAME_PROTOCOL        *ComponentName,         OPTIONAL
  IN EFI_DRIVER_CONFIGURATION_PROTOCOL  *DriverConfiguration,   OPTIONAL
  IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL    *DriverDiagnostics,     OPTIONAL
  IN NET_LIB_DRIVER_UNLOAD              Unload
  )
/*++

Routine Description:

  Intialize a driver by installing the Driver Binding Protocol onto the 
  driver's DriverBindingHandle.  This is typically the same as the driver's
  ImageHandle, but it can be different if the driver produces multiple
  DriverBinding Protocols.  This function also initializes the EFI Driver
  Library that initializes the global variables gST, gBS, gRT.

Arguments:

  ImageHandle         - The image handle of the driver
  SystemTable         - The EFI System Table that was passed to the driver's
                        entry point
  DriverBinding       - A Driver Binding Protocol instance that this driver
                        is producing.
  DriverBindingHandle - The handle that DriverBinding is to be installe onto.
                        If this parameter is NULL, then a new handle is created.
  ComponentName       - A Component Name Protocol instance that this driver is
                        producing.
  DriverConfiguration - A Driver Configuration Protocol instance that this
                        driver is producing.  
  DriverDiagnostics   - A Driver Diagnostics Protocol instance that this
                        driver is producing.
  Unload    - The customized unload to install.
  
Returns: 

  EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
  Otherwise, then return status from gBS->InstallProtocolInterface()

--*/
{
  EFI_STATUS                Status;
  EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;

  Status = EfiLibInstallAllDriverProtocols (
             ImageHandle,
             SystemTable,
             DriverBinding,
             DriverBindingHandle,
             ComponentName,
             DriverConfiguration,
             DriverDiagnostics
             );
  
  if (EFI_ERROR (Status)) {
    return Status;
  }
  
  //
  // Retrieve the Loaded Image Protocol from Image Handle
  //
  Status = gBS->OpenProtocol (
                  ImageHandle,
                  &gEfiLoadedImageProtocolGuid,
                  (VOID **) &LoadedImage,
                  ImageHandle,
                  ImageHandle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  
  if (EFI_ERROR (Status)) {
    return Status;
  }
  
  //
  // Fill in the Unload() service of the Loaded Image Protocol
  //
  LoadedImage->Unload = (Unload == NULL) ? NetLibDefaultUnload : Unload;
  return EFI_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?