123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732 |
- /** @file
- Implementation of Mtftp drivers.
- Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
- **/
- #include "Mtftp4Impl.h"
- EFI_DRIVER_BINDING_PROTOCOL gMtftp4DriverBinding = {
- Mtftp4DriverBindingSupported,
- Mtftp4DriverBindingStart,
- Mtftp4DriverBindingStop,
- 0xa,
- NULL,
- NULL
- };
- EFI_SERVICE_BINDING_PROTOCOL gMtftp4ServiceBindingTemplete = {
- Mtftp4ServiceBindingCreateChild,
- Mtftp4ServiceBindingDestroyChild
- };
- /**
- The driver entry point which installs multiple protocols to the ImageHandle.
- @param ImageHandle The MTFTP's image handle.
- @param SystemTable The system table.
- @retval EFI_SUCCESS The handles are successfully installed on the image.
- @retval others some EFI_ERROR occurred.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4DriverEntryPoint (
- IN EFI_HANDLE ImageHandle,
- IN EFI_SYSTEM_TABLE *SystemTable
- )
- {
- return EfiLibInstallDriverBindingComponentName2 (
- ImageHandle,
- SystemTable,
- &gMtftp4DriverBinding,
- ImageHandle,
- &gMtftp4ComponentName,
- &gMtftp4ComponentName2
- );
- }
- /**
- Test whether MTFTP driver support this controller.
- @param This The MTFTP driver binding instance
- @param Controller The controller to test
- @param RemainingDevicePath The remaining device path
- @retval EFI_SUCCESS The controller has UDP service binding protocol
- installed, MTFTP can support it.
- @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
- RemainingDevicePath is already being managed by
- the driver specified by This.
- @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
- RemainingDevicePath is already being managed by a
- different driver or an application that requires
- exclusive access.
- @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
- RemainingDevicePath is not supported by the driver
- specified by This.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4DriverBindingSupported (
- IN EFI_DRIVER_BINDING_PROTOCOL *This,
- IN EFI_HANDLE Controller,
- IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
- )
- {
- EFI_STATUS Status;
- Status = gBS->OpenProtocol (
- Controller,
- &gEfiUdp4ServiceBindingProtocolGuid,
- NULL,
- This->DriverBindingHandle,
- Controller,
- EFI_OPEN_PROTOCOL_TEST_PROTOCOL
- );
- return Status;
- }
- /**
- Config a NULL UDP that is used to keep the connection between UDP and MTFTP.
- Just leave the Udp child unconfigured. When UDP is unloaded,
- MTFTP will be informed with DriverBinding Stop.
- @param UdpIo The UDP_IO to configure
- @param Context The opaque parameter to the callback
- @retval EFI_SUCCESS It always return EFI_SUCCESS directly.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4ConfigNullUdp (
- IN UDP_IO *UdpIo,
- IN VOID *Context
- )
- {
- return EFI_SUCCESS;
- }
- /**
- Create then initialize a MTFTP service binding instance.
- @param Controller The controller to install the MTFTP service
- binding on
- @param Image The driver binding image of the MTFTP driver
- @param Service The variable to receive the created service
- binding instance.
- @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the instance
- @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep
- connection with UDP.
- @retval EFI_SUCCESS The service instance is created for the
- controller.
- **/
- EFI_STATUS
- Mtftp4CreateService (
- IN EFI_HANDLE Controller,
- IN EFI_HANDLE Image,
- OUT MTFTP4_SERVICE **Service
- )
- {
- MTFTP4_SERVICE *MtftpSb;
- EFI_STATUS Status;
- *Service = NULL;
- MtftpSb = AllocatePool (sizeof (MTFTP4_SERVICE));
- if (MtftpSb == NULL) {
- return EFI_OUT_OF_RESOURCES;
- }
- MtftpSb->Signature = MTFTP4_SERVICE_SIGNATURE;
- MtftpSb->ServiceBinding = gMtftp4ServiceBindingTemplete;
- MtftpSb->ChildrenNum = 0;
- InitializeListHead (&MtftpSb->Children);
- MtftpSb->Timer = NULL;
- MtftpSb->TimerNotifyLevel = NULL;
- MtftpSb->TimerToGetMap = NULL;
- MtftpSb->Controller = Controller;
- MtftpSb->Image = Image;
- MtftpSb->ConnectUdp = NULL;
- //
- // Create the timer and a udp to be notified when UDP is uninstalled
- //
- Status = gBS->CreateEvent (
- EVT_NOTIFY_SIGNAL | EVT_TIMER,
- TPL_CALLBACK,
- Mtftp4OnTimerTick,
- MtftpSb,
- &MtftpSb->Timer
- );
- if (EFI_ERROR (Status)) {
- FreePool (MtftpSb);
- return Status;
- }
- Status = gBS->CreateEvent (
- EVT_NOTIFY_SIGNAL | EVT_TIMER,
- TPL_NOTIFY,
- Mtftp4OnTimerTickNotifyLevel,
- MtftpSb,
- &MtftpSb->TimerNotifyLevel
- );
- if (EFI_ERROR (Status)) {
- gBS->CloseEvent (MtftpSb->Timer);
- FreePool (MtftpSb);
- return Status;
- }
- //
- // Create the timer used to time out the procedure which is used to
- // get the default IP address.
- //
- Status = gBS->CreateEvent (
- EVT_TIMER,
- TPL_CALLBACK,
- NULL,
- NULL,
- &MtftpSb->TimerToGetMap
- );
- if (EFI_ERROR (Status)) {
- gBS->CloseEvent (MtftpSb->TimerNotifyLevel);
- gBS->CloseEvent (MtftpSb->Timer);
- FreePool (MtftpSb);
- return Status;
- }
- MtftpSb->ConnectUdp = UdpIoCreateIo (
- Controller,
- Image,
- Mtftp4ConfigNullUdp,
- UDP_IO_UDP4_VERSION,
- NULL
- );
- if (MtftpSb->ConnectUdp == NULL) {
- gBS->CloseEvent (MtftpSb->TimerToGetMap);
- gBS->CloseEvent (MtftpSb->TimerNotifyLevel);
- gBS->CloseEvent (MtftpSb->Timer);
- FreePool (MtftpSb);
- return EFI_DEVICE_ERROR;
- }
- *Service = MtftpSb;
- return EFI_SUCCESS;
- }
- /**
- Release all the resource used the MTFTP service binding instance.
- @param MtftpSb The MTFTP service binding instance.
- **/
- VOID
- Mtftp4CleanService (
- IN MTFTP4_SERVICE *MtftpSb
- )
- {
- UdpIoFreeIo (MtftpSb->ConnectUdp);
- gBS->CloseEvent (MtftpSb->TimerToGetMap);
- gBS->CloseEvent (MtftpSb->TimerNotifyLevel);
- gBS->CloseEvent (MtftpSb->Timer);
- }
- /**
- Start the MTFTP driver on this controller.
- MTFTP driver will install a MTFTP SERVICE BINDING protocol on the supported
- controller, which can be used to create/destroy MTFTP children.
- @param This The MTFTP driver binding protocol.
- @param Controller The controller to manage.
- @param RemainingDevicePath Remaining device path.
- @retval EFI_ALREADY_STARTED The MTFTP service binding protocol has been
- started on the controller.
- @retval EFI_SUCCESS The MTFTP service binding is installed on the
- controller.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4DriverBindingStart (
- IN EFI_DRIVER_BINDING_PROTOCOL *This,
- IN EFI_HANDLE Controller,
- IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
- )
- {
- MTFTP4_SERVICE *MtftpSb;
- EFI_STATUS Status;
- //
- // Directly return if driver is already running.
- //
- Status = gBS->OpenProtocol (
- Controller,
- &gEfiMtftp4ServiceBindingProtocolGuid,
- NULL,
- This->DriverBindingHandle,
- Controller,
- EFI_OPEN_PROTOCOL_TEST_PROTOCOL
- );
- if (Status == EFI_SUCCESS) {
- return EFI_ALREADY_STARTED;
- }
- Status = Mtftp4CreateService (Controller, This->DriverBindingHandle, &MtftpSb);
- if (EFI_ERROR (Status)) {
- return Status;
- }
- ASSERT (MtftpSb != NULL);
- Status = gBS->SetTimer (MtftpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
- if (EFI_ERROR (Status)) {
- goto ON_ERROR;
- }
- Status = gBS->SetTimer (MtftpSb->TimerNotifyLevel, TimerPeriodic, TICKS_PER_SECOND);
- if (EFI_ERROR (Status)) {
- goto ON_ERROR;
- }
- //
- // Install the Mtftp4ServiceBinding Protocol onto Controller
- //
- Status = gBS->InstallMultipleProtocolInterfaces (
- &Controller,
- &gEfiMtftp4ServiceBindingProtocolGuid,
- &MtftpSb->ServiceBinding,
- NULL
- );
- if (EFI_ERROR (Status)) {
- goto ON_ERROR;
- }
- return EFI_SUCCESS;
- ON_ERROR:
- Mtftp4CleanService (MtftpSb);
- FreePool (MtftpSb);
- return Status;
- }
- /**
- Callback function which provided by user to remove one node in NetDestroyLinkList process.
- @param[in] Entry The entry to be removed.
- @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
- @retval EFI_SUCCESS The entry has been removed successfully.
- @retval Others Fail to remove the entry.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4DestroyChildEntryInHandleBuffer (
- IN LIST_ENTRY *Entry,
- IN VOID *Context
- )
- {
- MTFTP4_PROTOCOL *Instance;
- EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
- UINTN NumberOfChildren;
- EFI_HANDLE *ChildHandleBuffer;
- if ((Entry == NULL) || (Context == NULL)) {
- return EFI_INVALID_PARAMETER;
- }
- Instance = NET_LIST_USER_STRUCT_S (Entry, MTFTP4_PROTOCOL, Link, MTFTP4_PROTOCOL_SIGNATURE);
- ServiceBinding = ((MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ServiceBinding;
- NumberOfChildren = ((MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->NumberOfChildren;
- ChildHandleBuffer = ((MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ChildHandleBuffer;
- if (!NetIsInHandleBuffer (Instance->Handle, NumberOfChildren, ChildHandleBuffer)) {
- return EFI_SUCCESS;
- }
- return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
- }
- /**
- Stop the MTFTP driver on controller. The controller is a UDP
- child handle.
- @param This The MTFTP driver binding protocol
- @param Controller The controller to stop
- @param NumberOfChildren The number of children
- @param ChildHandleBuffer The array of the child handle.
- @retval EFI_SUCCESS The driver is stopped on the controller.
- @retval EFI_DEVICE_ERROR Failed to stop the driver on the controller.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4DriverBindingStop (
- IN EFI_DRIVER_BINDING_PROTOCOL *This,
- IN EFI_HANDLE Controller,
- IN UINTN NumberOfChildren,
- IN EFI_HANDLE *ChildHandleBuffer
- )
- {
- EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
- MTFTP4_SERVICE *MtftpSb;
- EFI_HANDLE NicHandle;
- EFI_STATUS Status;
- LIST_ENTRY *List;
- MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
- //
- // MTFTP driver opens UDP child, So, Controller is a UDP
- // child handle. Locate the Nic handle first. Then get the
- // MTFTP private data back.
- //
- NicHandle = NetLibGetNicHandle (Controller, &gEfiUdp4ProtocolGuid);
- if (NicHandle == NULL) {
- return EFI_SUCCESS;
- }
- Status = gBS->OpenProtocol (
- NicHandle,
- &gEfiMtftp4ServiceBindingProtocolGuid,
- (VOID **)&ServiceBinding,
- This->DriverBindingHandle,
- NicHandle,
- EFI_OPEN_PROTOCOL_GET_PROTOCOL
- );
- if (EFI_ERROR (Status)) {
- return EFI_DEVICE_ERROR;
- }
- MtftpSb = MTFTP4_SERVICE_FROM_THIS (ServiceBinding);
- if (!IsListEmpty (&MtftpSb->Children)) {
- //
- // Destroy the Mtftp4 child instance in ChildHandleBuffer.
- //
- List = &MtftpSb->Children;
- Context.ServiceBinding = ServiceBinding;
- Context.NumberOfChildren = NumberOfChildren;
- Context.ChildHandleBuffer = ChildHandleBuffer;
- Status = NetDestroyLinkList (
- List,
- Mtftp4DestroyChildEntryInHandleBuffer,
- &Context,
- NULL
- );
- }
- if ((NumberOfChildren == 0) && IsListEmpty (&MtftpSb->Children)) {
- gBS->UninstallProtocolInterface (
- NicHandle,
- &gEfiMtftp4ServiceBindingProtocolGuid,
- ServiceBinding
- );
- Mtftp4CleanService (MtftpSb);
- if (gMtftp4ControllerNameTable != NULL) {
- FreeUnicodeStringTable (gMtftp4ControllerNameTable);
- gMtftp4ControllerNameTable = NULL;
- }
- FreePool (MtftpSb);
- Status = EFI_SUCCESS;
- }
- return Status;
- }
- /**
- Initialize a MTFTP protocol instance which is the child of MtftpSb.
- @param MtftpSb The MTFTP service binding protocol.
- @param Instance The MTFTP instance to initialize.
- **/
- VOID
- Mtftp4InitProtocol (
- IN MTFTP4_SERVICE *MtftpSb,
- OUT MTFTP4_PROTOCOL *Instance
- )
- {
- ZeroMem (Instance, sizeof (MTFTP4_PROTOCOL));
- Instance->Signature = MTFTP4_PROTOCOL_SIGNATURE;
- InitializeListHead (&Instance->Link);
- CopyMem (&Instance->Mtftp4, &gMtftp4ProtocolTemplate, sizeof (Instance->Mtftp4));
- Instance->State = MTFTP4_STATE_UNCONFIGED;
- Instance->Service = MtftpSb;
- InitializeListHead (&Instance->Blocks);
- }
- /**
- Create a MTFTP child for the service binding instance, then
- install the MTFTP protocol to the ChildHandle.
- @param This The MTFTP service binding instance.
- @param ChildHandle The Child handle to install the MTFTP protocol.
- @retval EFI_INVALID_PARAMETER The parameter is invalid.
- @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the new child.
- @retval EFI_SUCCESS The child is successfully create.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4ServiceBindingCreateChild (
- IN EFI_SERVICE_BINDING_PROTOCOL *This,
- IN EFI_HANDLE *ChildHandle
- )
- {
- MTFTP4_SERVICE *MtftpSb;
- MTFTP4_PROTOCOL *Instance;
- EFI_STATUS Status;
- EFI_TPL OldTpl;
- VOID *Udp4;
- if ((This == NULL) || (ChildHandle == NULL)) {
- return EFI_INVALID_PARAMETER;
- }
- Instance = AllocatePool (sizeof (*Instance));
- if (Instance == NULL) {
- return EFI_OUT_OF_RESOURCES;
- }
- MtftpSb = MTFTP4_SERVICE_FROM_THIS (This);
- Mtftp4InitProtocol (MtftpSb, Instance);
- Instance->UnicastPort = UdpIoCreateIo (
- MtftpSb->Controller,
- MtftpSb->Image,
- Mtftp4ConfigNullUdp,
- UDP_IO_UDP4_VERSION,
- Instance
- );
- if (Instance->UnicastPort == NULL) {
- FreePool (Instance);
- return EFI_OUT_OF_RESOURCES;
- }
- //
- // Install the MTFTP protocol onto ChildHandle
- //
- Status = gBS->InstallMultipleProtocolInterfaces (
- ChildHandle,
- &gEfiMtftp4ProtocolGuid,
- &Instance->Mtftp4,
- NULL
- );
- if (EFI_ERROR (Status)) {
- UdpIoFreeIo (Instance->UnicastPort);
- FreePool (Instance);
- return Status;
- }
- Instance->Handle = *ChildHandle;
- //
- // Open the Udp4 protocol BY_CHILD.
- //
- Status = gBS->OpenProtocol (
- MtftpSb->ConnectUdp->UdpHandle,
- &gEfiUdp4ProtocolGuid,
- (VOID **)&Udp4,
- gMtftp4DriverBinding.DriverBindingHandle,
- Instance->Handle,
- EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
- );
- if (EFI_ERROR (Status)) {
- goto ON_ERROR;
- }
- //
- // Open the Udp4 protocol by child.
- //
- Status = gBS->OpenProtocol (
- Instance->UnicastPort->UdpHandle,
- &gEfiUdp4ProtocolGuid,
- (VOID **)&Udp4,
- gMtftp4DriverBinding.DriverBindingHandle,
- Instance->Handle,
- EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
- );
- if (EFI_ERROR (Status)) {
- //
- // Close the Udp4 protocol.
- //
- gBS->CloseProtocol (
- MtftpSb->ConnectUdp->UdpHandle,
- &gEfiUdp4ProtocolGuid,
- gMtftp4DriverBinding.DriverBindingHandle,
- *ChildHandle
- );
- goto ON_ERROR;
- }
- //
- // Add it to the parent's child list.
- //
- OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
- InsertTailList (&MtftpSb->Children, &Instance->Link);
- MtftpSb->ChildrenNum++;
- gBS->RestoreTPL (OldTpl);
- return EFI_SUCCESS;
- ON_ERROR:
- if (Instance->Handle != NULL) {
- gBS->UninstallMultipleProtocolInterfaces (
- Instance->Handle,
- &gEfiMtftp4ProtocolGuid,
- &Instance->Mtftp4,
- NULL
- );
- }
- UdpIoFreeIo (Instance->UnicastPort);
- FreePool (Instance);
- return Status;
- }
- /**
- Destroy one of the service binding's child.
- @param This The service binding instance
- @param ChildHandle The child handle to destroy
- @retval EFI_INVALID_PARAMETER The parameter is invalid.
- @retval EFI_UNSUPPORTED The child may have already been destroyed.
- @retval EFI_SUCCESS The child is destroyed and removed from the
- parent's child list.
- **/
- EFI_STATUS
- EFIAPI
- Mtftp4ServiceBindingDestroyChild (
- IN EFI_SERVICE_BINDING_PROTOCOL *This,
- IN EFI_HANDLE ChildHandle
- )
- {
- MTFTP4_SERVICE *MtftpSb;
- MTFTP4_PROTOCOL *Instance;
- EFI_MTFTP4_PROTOCOL *Mtftp4;
- EFI_STATUS Status;
- EFI_TPL OldTpl;
- if ((This == NULL) || (ChildHandle == NULL)) {
- return EFI_INVALID_PARAMETER;
- }
- //
- // Retrieve the private context data structures
- //
- Status = gBS->OpenProtocol (
- ChildHandle,
- &gEfiMtftp4ProtocolGuid,
- (VOID **)&Mtftp4,
- gMtftp4DriverBinding.DriverBindingHandle,
- ChildHandle,
- EFI_OPEN_PROTOCOL_GET_PROTOCOL
- );
- if (EFI_ERROR (Status)) {
- return EFI_UNSUPPORTED;
- }
- Instance = MTFTP4_PROTOCOL_FROM_THIS (Mtftp4);
- MtftpSb = MTFTP4_SERVICE_FROM_THIS (This);
- if (Instance->Service != MtftpSb) {
- return EFI_INVALID_PARAMETER;
- }
- if (Instance->InDestroy) {
- return EFI_SUCCESS;
- }
- Instance->InDestroy = TRUE;
- //
- // Close the Udp4 protocol.
- //
- gBS->CloseProtocol (
- MtftpSb->ConnectUdp->UdpHandle,
- &gEfiUdp4ProtocolGuid,
- gMtftp4DriverBinding.DriverBindingHandle,
- ChildHandle
- );
- gBS->CloseProtocol (
- Instance->UnicastPort->UdpHandle,
- &gEfiUdp4ProtocolGuid,
- gMtftp4DriverBinding.DriverBindingHandle,
- ChildHandle
- );
- if (Instance->McastUdpPort != NULL) {
- gBS->CloseProtocol (
- Instance->McastUdpPort->UdpHandle,
- &gEfiUdp4ProtocolGuid,
- gMtftp4DriverBinding.DriverBindingHandle,
- ChildHandle
- );
- }
- //
- // Uninstall the MTFTP4 protocol first to enable a top down destruction.
- //
- Status = gBS->UninstallProtocolInterface (
- ChildHandle,
- &gEfiMtftp4ProtocolGuid,
- Mtftp4
- );
- if (EFI_ERROR (Status)) {
- Instance->InDestroy = FALSE;
- return Status;
- }
- OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
- Mtftp4CleanOperation (Instance, EFI_DEVICE_ERROR);
- UdpIoFreeIo (Instance->UnicastPort);
- RemoveEntryList (&Instance->Link);
- MtftpSb->ChildrenNum--;
- gBS->RestoreTPL (OldTpl);
- FreePool (Instance);
- return EFI_SUCCESS;
- }
|