dhcp4driver.c

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

C
699
字号
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 


Module Name:

  Dhcp4Driver.c

Abstract:

--*/
#include "Dhcp4Impl.h"
#include "Dhcp4Driver.h"

EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding = {
  Dhcp4DriverBindingSupported,
  Dhcp4DriverBindingStart,
  Dhcp4DriverBindingStop,
  0x10,
  NULL,
  NULL
};

EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplete = {
  Dhcp4ServiceBindingCreateChild,
  Dhcp4ServiceBindingDestroyChild
};

EFI_DRIVER_ENTRY_POINT (Dhcp4DriverEntryPoint)

EFI_STATUS
EFIAPI
Dhcp4DriverEntryPoint (
  IN EFI_HANDLE             ImageHandle,
  IN EFI_SYSTEM_TABLE       *SystemTable
  )
/*++

Routine Description:

  Entry point of the DHCP driver to install various protocols.

Arguments:

  ImageHandle - The driver's image handle
  SystemTable - The system table

Returns:

  EFI_SUCCESS - All the related protocols are installed.
  Others      - Failed to install the protocols.

--*/
{
  return NetLibInstallAllDriverProtocols (
           ImageHandle,
           SystemTable,
           &gDhcp4DriverBinding,
           ImageHandle,
           &gDhcp4ComponentName,
           NULL,
           NULL
           );
}

EFI_STATUS
EFIAPI
Dhcp4DriverBindingSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN EFI_HANDLE                   ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL
  )
/*++

  Routine Description:
    Test to see if DHCP driver supports the ControllerHandle. 

  Arguments:
    This                - Protocol instance pointer.
    ControllerHandle    - Handle of device to test
    RemainingDevicePath - Optional parameter use to pick a specific child 
                          device to start.

  Returns:
    EFI_SUCCES          - This driver supports this device
    other               - This driver does not support this device

--*/
{
  EFI_STATUS  Status;

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiUdp4ServiceBindingProtocolGuid,
                  NULL,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );

  return Status;
}


EFI_STATUS
DhcpConfigUdpIo (
  IN UDP_IO_PORT            *UdpIo,
  IN VOID                   *Context
  )
/*++

Routine Description:

  Configure the default UDP child to receive all the DHCP traffics
  on this network interface.

Arguments:

  UdpIo   - The UDP IO port to configure
  Context - The context to the function

Returns:

  EFI_SUCCESS - The UDP IO port is successfully configured.
  Others      - Failed to configure the UDP child.

--*/
{
  EFI_UDP4_CONFIG_DATA      UdpConfigData;

  UdpConfigData.AcceptBroadcast           = TRUE;
  UdpConfigData.AcceptPromiscuous         = FALSE;
  UdpConfigData.AcceptAnyPort             = FALSE;
  UdpConfigData.AllowDuplicatePort        = TRUE;
  UdpConfigData.TypeOfService             = 0;
  UdpConfigData.TimeToLive                = 64;
  UdpConfigData.DoNotFragment             = FALSE;
  UdpConfigData.ReceiveTimeout            = 0;
  UdpConfigData.TransmitTimeout           = 0;

  UdpConfigData.UseDefaultAddress         = FALSE;
  UdpConfigData.StationPort               = DHCP_CLIENT_PORT;
  UdpConfigData.RemotePort                = DHCP_SERVER_PORT;

  EFI_IP4 (UdpConfigData.StationAddress)  = 0;
  EFI_IP4 (UdpConfigData.SubnetMask)      = 0;
  EFI_IP4 (UdpConfigData.RemoteAddress)   = 0;

  return UdpIo->Udp->Configure (UdpIo->Udp, &UdpConfigData);;
}


EFI_STATUS
Dhcp4CloseService (
  IN DHCP_SERVICE           *DhcpSb
  )
/*++

Routine Description:

  Destory the DHCP service. The Dhcp4 service may be partly initialized, 
  or partly destoried. If a resource is destoried, it is marked as so in
  case the destory failed and being called again later.

Arguments:

  DhcpSb  - The DHCP service instance to destory.

Returns:

  EFI_SUCCESS - The DHCP service is successfully closed.

--*/
{
  DhcpCleanLease (DhcpSb);

  if (DhcpSb->UdpIo != NULL) {
    UdpIoFreePort (DhcpSb->UdpIo);
    DhcpSb->UdpIo = NULL;
  }

  if (DhcpSb->Timer != NULL) {
    gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0);
    gBS->CloseEvent (DhcpSb->Timer);

    DhcpSb->Timer = NULL;
  }

  return EFI_SUCCESS;
}


EFI_STATUS
Dhcp4CreateService (
  IN  EFI_HANDLE            Controller,
  IN  EFI_HANDLE            ImageHandle,
  OUT DHCP_SERVICE          **Service
  )
/*++

Routine Description:

  Create a new DHCP service binding instance for the controller.

Arguments:

  Controller  - The controller to install DHCP service binding protocol onto
  ImageHandle - The driver's image handle
  Service     - The variable to receive the created DHCP service instance.

Returns:

  EFI_OUT_OF_RESOURCES - Failed to allocate resource .
  EFI_SUCCESS          - The DHCP service instance is created.

--*/
{
  DHCP_SERVICE              *DhcpSb;
  EFI_STATUS                Status;

  *Service  = NULL;
  DhcpSb    = NetAllocateZeroPool (sizeof (DHCP_SERVICE));
  
  if (DhcpSb == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  DhcpSb->Signature       = DHCP_SERVICE_SIGNATURE;
  DhcpSb->ServiceBinding  = mDhcp4ServiceBindingTemplete;
  DhcpSb->ServiceState    = DHCP_UNCONFIGED;
  DhcpSb->InDestory       = FALSE;
  DhcpSb->Controller      = Controller;
  DhcpSb->Image           = ImageHandle;
  NetListInit (&DhcpSb->Children);
  DhcpSb->DhcpState       = Dhcp4Stopped;
  DhcpSb->Xid             = NET_RANDOM (NetRandomInitSeed ());

  //
  // Create various resources, UdpIo, Timer, and get Mac address
  //
  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL | EFI_EVENT_TIMER,
                  EFI_TPL_CALLBACK,
                  DhcpOnTimerTick,
                  DhcpSb,
                  &DhcpSb->Timer
                  );

  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  DhcpSb->UdpIo = UdpIoCreatePort (Controller, ImageHandle, DhcpConfigUdpIo, NULL);

  if (DhcpSb->UdpIo == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto ON_ERROR;
  }

  DhcpSb->HwLen  = (UINT8) DhcpSb->UdpIo->SnpMode.HwAddressSize;
  DhcpSb->HwType = DhcpSb->UdpIo->SnpMode.IfType;
  DhcpSb->Mac    = DhcpSb->UdpIo->SnpMode.CurrentAddress;

  *Service       = DhcpSb;
  return EFI_SUCCESS;

ON_ERROR:
  Dhcp4CloseService (DhcpSb);
  NetFreePool (DhcpSb);

  return Status;
}

EFI_STATUS
EFIAPI
Dhcp4DriverBindingStart (
  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN EFI_HANDLE                   ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL
  )
/*++

  Routine Description:
    Start this driver on ControllerHandle.

  Arguments:
    This                - Protocol instance pointer.
    ControllerHandle    - Handle of device to bind driver to
    RemainingDevicePath - Optional parameter use to pick a specific child 
                          device to start.

  Returns:
    EFI_SUCCES          - This driver is added to ControllerHandle
    EFI_ALREADY_STARTED - This driver is already running on ControllerHandle
    other               - This driver does not support this device

--*/
{
  DHCP_SERVICE              *DhcpSb;
  EFI_STATUS                Status;

  //
  // First: test for the DHCP4 Protocol
  //
  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiDhcp4ServiceBindingProtocolGuid,
                  NULL,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );

  if (Status == EFI_SUCCESS) {
    return EFI_ALREADY_STARTED;
  }

  Status = Dhcp4CreateService (ControllerHandle, This->DriverBindingHandle, &DhcpSb);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);

  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }
  
  //
  // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &ControllerHandle,
                  &gEfiDhcp4ServiceBindingProtocolGuid,
                  &DhcpSb->ServiceBinding,
                  NULL
                  );

  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

⌨️ 快捷键说明

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