]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Rrq.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Rrq.c
index 665114c7f8a3e2b415dfb9377fd900cab88d8a32..63115ba5195866f0220c3a869d2984ac79190548 100644 (file)
@@ -1,40 +1,46 @@
 /** @file\r
+  Routines to process Rrq (download).\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+(C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
+http://opensource.org/licenses/bsd-license.php<BR>\r
 \r
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-Module Name:\r
-\r
-  Mtftp4Rrq.c\r
+**/\r
 \r
-Abstract:\r
 \r
-  Routines to process Rrq (download)\r
+#include "Mtftp4Impl.h"\r
 \r
 \r
-**/\r
+/**\r
+  The packet process callback for MTFTP download.\r
 \r
-#include "Mtftp4Impl.h"\r
+  @param  UdpPacket             The packet received\r
+  @param  EndPoint              The local/remote access point of the packet\r
+  @param  IoStatus              The status of the receiving\r
+  @param  Context               Opaque parameter, which is the MTFTP session\r
 \r
+**/\r
 VOID\r
+EFIAPI\r
 Mtftp4RrqInput (\r
   IN NET_BUF                *UdpPacket,\r
-  IN UDP_POINTS             *Points,\r
+  IN UDP_END_POINT          *EndPoint,\r
   IN EFI_STATUS             IoStatus,\r
   IN VOID                   *Context\r
   );\r
 \r
 \r
 /**\r
-  Start the MTFTP session to download. It will first initialize some\r
-  of the internal states then build and send a RRQ reqeuest packet, at\r
-  last, it will start receive for the downloading.\r
+  Start the MTFTP session to download.\r
+\r
+  It will first initialize some of the internal states then build and send a RRQ\r
+  reqeuest packet, at last, it will start receive for the downloading.\r
 \r
   @param  Instance              The Mtftp session\r
   @param  Operation             The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ\r
@@ -95,7 +101,6 @@ Mtftp4RrqSendAck (
   NET_BUF                   *Packet;\r
 \r
   Packet = NetbufAlloc (sizeof (EFI_MTFTP4_ACK_HEADER));\r
-\r
   if (Packet == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -105,6 +110,7 @@ Mtftp4RrqSendAck (
                                 sizeof (EFI_MTFTP4_ACK_HEADER),\r
                                 FALSE\r
                                 );\r
+  ASSERT (Ack != NULL);\r
 \r
   Ack->Ack.OpCode   = HTONS (EFI_MTFTP4_OPCODE_ACK);\r
   Ack->Ack.Block[0] = HTONS (BlkNo);\r
@@ -130,9 +136,9 @@ Mtftp4RrqSendAck (
 **/\r
 EFI_STATUS\r
 Mtftp4RrqSaveBlock (\r
-  IN MTFTP4_PROTOCOL        *Instance,\r
-  IN EFI_MTFTP4_PACKET      *Packet,\r
-  IN UINT32                 Len\r
+  IN OUT MTFTP4_PROTOCOL        *Instance,\r
+  IN     EFI_MTFTP4_PACKET      *Packet,\r
+  IN     UINT32                 Len\r
   )\r
 {\r
   EFI_MTFTP4_TOKEN          *Token;\r
@@ -140,15 +146,19 @@ Mtftp4RrqSaveBlock (
   UINT16                    Block;\r
   UINT64                    Start;\r
   UINT32                    DataLen;\r
+  UINT64                    TotalBlock;\r
+  BOOLEAN                   Completed;\r
 \r
-  Token   = Instance->Token;\r
-  Block   = NTOHS (Packet->Data.Block);\r
-  DataLen = Len - MTFTP4_DATA_HEAD_LEN;\r
+  Completed = FALSE;\r
+  Token     = Instance->Token;\r
+  Block     = NTOHS (Packet->Data.Block);\r
+  DataLen   = Len - MTFTP4_DATA_HEAD_LEN;\r
 \r
   //\r
   // This is the last block, save the block no\r
   //\r
   if (DataLen < Instance->BlkSize) {\r
+  Completed = TRUE;\r
     Instance->LastBlock = Block;\r
     Mtftp4SetLastBlockNum (&Instance->Blocks, Block);\r
   }\r
@@ -156,8 +166,11 @@ Mtftp4RrqSaveBlock (
   //\r
   // Remove this block number from the file hole. If Mtftp4RemoveBlockNum\r
   // returns EFI_NOT_FOUND, the block has been saved, don't save it again.\r
+  // Note that : For bigger files, allowing the block counter to roll over\r
+  // to accept transfers of unlimited size. So TotalBlock is memorised as\r
+  // continuous block counter.\r
   //\r
-  Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block);\r
+  Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block, Completed, &TotalBlock);\r
 \r
   if (Status == EFI_NOT_FOUND) {\r
     return EFI_SUCCESS;\r
@@ -172,7 +185,7 @@ Mtftp4RrqSaveBlock (
       Mtftp4SendError (\r
         Instance,\r
         EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,\r
-        "User aborted download"\r
+        (UINT8 *) "User aborted download"\r
         );\r
 \r
       return EFI_ABORTED;\r
@@ -180,15 +193,15 @@ Mtftp4RrqSaveBlock (
   }\r
 \r
   if (Token->Buffer != NULL) {\r
-    Start = MultU64x32 (Block - 1, Instance->BlkSize);\r
+     Start = MultU64x32 (TotalBlock - 1, Instance->BlkSize);\r
 \r
     if (Start + DataLen <= Token->BufferSize) {\r
-      NetCopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);\r
+      CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);\r
 \r
       //\r
       // Update the file size when received the last block\r
       //\r
-      if (Instance->LastBlock == Block) {\r
+      if ((Instance->LastBlock == Block) && Completed) {\r
         Token->BufferSize = Start + DataLen;\r
       }\r
 \r
@@ -203,7 +216,7 @@ Mtftp4RrqSaveBlock (
       Mtftp4SendError (\r
         Instance,\r
         EFI_MTFTP4_ERRORCODE_DISK_FULL,\r
-        "User provided memory block is too small"\r
+        (UINT8 *) "User provided memory block is too small"\r
         );\r
 \r
       return EFI_BUFFER_TOO_SMALL;\r
@@ -215,8 +228,9 @@ Mtftp4RrqSaveBlock (
 \r
 \r
 /**\r
-  Function to process the received data packets. It will save the block\r
-  then send back an ACK if it is active.\r
+  Function to process the received data packets.\r
+\r
+  It will save the block then send back an ACK if it is active.\r
 \r
   @param  Instance              The downloading MTFTP session\r
   @param  Packet                The packet received\r
@@ -231,11 +245,11 @@ Mtftp4RrqSaveBlock (
 **/\r
 EFI_STATUS\r
 Mtftp4RrqHandleData (\r
-  IN  MTFTP4_PROTOCOL       *Instance,\r
-  IN  EFI_MTFTP4_PACKET     *Packet,\r
-  IN  UINT32                Len,\r
-  IN  BOOLEAN               Multicast,\r
-  OUT BOOLEAN               *Completed\r
+  IN     MTFTP4_PROTOCOL       *Instance,\r
+  IN     EFI_MTFTP4_PACKET     *Packet,\r
+  IN     UINT32                Len,\r
+  IN     BOOLEAN               Multicast,\r
+     OUT BOOLEAN               *Completed\r
   )\r
 {\r
   EFI_STATUS                Status;\r
@@ -304,6 +318,7 @@ Mtftp4RrqHandleData (
 \r
 /**\r
   Validate whether the options received in the server's OACK packet is valid.\r
+\r
   The options are valid only if:\r
   1. The server doesn't include options not requested by us\r
   2. The server can only use smaller blksize than that is requested\r
@@ -314,7 +329,8 @@ Mtftp4RrqHandleData (
   @param  Reply                 The options in the OACK packet\r
   @param  Request               The requested options\r
 \r
-  @return TRUE if the options in the OACK is OK, otherwise FALSE.\r
+  @retval TRUE                  The options in the OACK is OK.\r
+  @retval FALSE                 The options in the OACK is invalid.\r
 \r
 **/\r
 BOOLEAN\r
@@ -336,8 +352,8 @@ Mtftp4RrqOackValid (
   // Server can only specify a smaller block size to be used and\r
   // return the timeout matches that requested.\r
   //\r
-  if (((Reply->Exist & MTFTP4_BLKSIZE_EXIST) && (Reply->BlkSize > Request->BlkSize)) ||\r
-      ((Reply->Exist & MTFTP4_TIMEOUT_EXIST) && (Reply->Timeout != Request->Timeout))) {\r
+  if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0)&& (Reply->BlkSize > Request->BlkSize)) ||\r
+      (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout))) {\r
     return FALSE;\r
   }\r
 \r
@@ -346,7 +362,7 @@ Mtftp4RrqOackValid (
   // setting. But if it use the specific multicast channel, it can't\r
   // change the setting.\r
   //\r
-  if ((Reply->Exist & MTFTP4_MCAST_EXIST) && (This->McastIp != 0)) {\r
+  if (((Reply->Exist & MTFTP4_MCAST_EXIST) != 0) && (This->McastIp != 0)) {\r
     if ((Reply->McastIp != 0) && (Reply->McastIp != This->McastIp)) {\r
       return FALSE;\r
     }\r
@@ -363,18 +379,18 @@ Mtftp4RrqOackValid (
 /**\r
   Configure a UDP IO port to receive the multicast.\r
 \r
-  @param  McastIo               The UDP IO port to configure\r
+  @param  McastIo               The UDP IO to configure\r
   @param  Context               The opaque parameter to the function which is the\r
                                 MTFTP session.\r
 \r
-  @retval EFI_SUCCESS           The udp child is successfully configured.\r
+  @retval EFI_SUCCESS           The UDP child is successfully configured.\r
   @retval Others                Failed to configure the UDP child.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 Mtftp4RrqConfigMcastPort (\r
-  IN UDP_IO_PORT            *McastIo,\r
+  IN UDP_IO                 *McastIo,\r
   IN VOID                   *Context\r
   )\r
 {\r
@@ -398,33 +414,54 @@ Mtftp4RrqConfigMcastPort (
   UdpConfig.ReceiveTimeout     = 0;\r
   UdpConfig.TransmitTimeout    = 0;\r
   UdpConfig.UseDefaultAddress  = Config->UseDefaultSetting;\r
-  UdpConfig.StationAddress     = Config->StationIp;\r
-  UdpConfig.SubnetMask         = Config->SubnetMask;\r
+  IP4_COPY_ADDRESS (&UdpConfig.StationAddress, &Config->StationIp);\r
+  IP4_COPY_ADDRESS (&UdpConfig.SubnetMask, &Config->SubnetMask);\r
   UdpConfig.StationPort        = Instance->McastPort;\r
   UdpConfig.RemotePort         = 0;\r
 \r
   Ip = HTONL (Instance->ServerIp);\r
-  NetCopyMem (&UdpConfig.RemoteAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
+  IP4_COPY_ADDRESS (&UdpConfig.RemoteAddress, &Ip);\r
 \r
-  Status = McastIo->Udp->Configure (McastIo->Udp, &UdpConfig);\r
+  Status = McastIo->Protocol.Udp4->Configure (McastIo->Protocol.Udp4, &UdpConfig);\r
 \r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
+  if (!Config->UseDefaultSetting &&\r
+      !EFI_IP4_EQUAL (&mZeroIp4Addr, &Config->GatewayIp)) {\r
+    //\r
+    // The station IP address is manually configured and the Gateway IP is not 0.\r
+    // Add the default route for this UDP instance.\r
+    //\r
+    Status = McastIo->Protocol.Udp4->Routes (\r
+                                       McastIo->Protocol.Udp4,\r
+                                       FALSE,\r
+                                       &mZeroIp4Addr,\r
+                                       &mZeroIp4Addr,\r
+                                       &Config->GatewayIp\r
+                                       );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      McastIo->Protocol.Udp4->Configure (McastIo->Protocol.Udp4, NULL);\r
+      return Status;\r
+    }\r
+  }\r
+\r
   //\r
   // join the multicast group\r
   //\r
   Ip = HTONL (Instance->McastIp);\r
-  NetCopyMem (&Group, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
+  IP4_COPY_ADDRESS (&Group, &Ip);\r
 \r
-  return McastIo->Udp->Groups (McastIo->Udp, TRUE, &Group);\r
+  return McastIo->Protocol.Udp4->Groups (McastIo->Protocol.Udp4, TRUE, &Group);\r
 }\r
 \r
 \r
 /**\r
-  Function to process the OACK. It will first validate the OACK\r
-  packet, then update the various negotiated parameters.\r
+  Function to process the OACK.\r
+\r
+  It will first validate the OACK packet, then update the various negotiated parameters.\r
 \r
   @param  Instance              The download MTFTP session\r
   @param  Packet                The packet received\r
@@ -440,16 +477,17 @@ Mtftp4RrqConfigMcastPort (
 **/\r
 EFI_STATUS\r
 Mtftp4RrqHandleOack (\r
-  IN  MTFTP4_PROTOCOL       *Instance,\r
-  IN  EFI_MTFTP4_PACKET     *Packet,\r
-  IN  UINT32                Len,\r
-  IN  BOOLEAN               Multicast,\r
-  OUT BOOLEAN               *Completed\r
+  IN OUT MTFTP4_PROTOCOL       *Instance,\r
+  IN     EFI_MTFTP4_PACKET     *Packet,\r
+  IN     UINT32                Len,\r
+  IN     BOOLEAN               Multicast,\r
+     OUT BOOLEAN               *Completed\r
   )\r
 {\r
   MTFTP4_OPTION             Reply;\r
   EFI_STATUS                Status;\r
   INTN                      Expected;\r
+  EFI_UDP4_PROTOCOL         *Udp4;\r
 \r
   *Completed = FALSE;\r
 \r
@@ -467,7 +505,7 @@ Mtftp4RrqHandleOack (
   //\r
   // Parse and validate the options from server\r
   //\r
-  NetZeroMem (&Reply, sizeof (MTFTP4_OPTION));\r
+  ZeroMem (&Reply, sizeof (MTFTP4_OPTION));\r
 \r
   Status = Mtftp4ParseOptionOack (Packet, Len, &Reply);\r
 \r
@@ -480,14 +518,14 @@ Mtftp4RrqHandleOack (
       Mtftp4SendError (\r
         Instance,\r
         EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,\r
-        "Mal-formated OACK packet"\r
+        (UINT8 *) "Mal-formated OACK packet"\r
         );\r
     }\r
 \r
     return EFI_TFTP_ERROR;\r
   }\r
 \r
-  if (Reply.Exist & MTFTP4_MCAST_EXIST) {\r
+  if ((Reply.Exist & MTFTP4_MCAST_EXIST) != 0) {\r
 \r
     //\r
     // Save the multicast info. Always update the Master, only update the\r
@@ -501,7 +539,7 @@ Mtftp4RrqHandleOack (
         Mtftp4SendError (\r
           Instance,\r
           EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,\r
-          "Illegal multicast setting"\r
+          (UINT8 *) "Illegal multicast setting"\r
           );\r
 \r
         return EFI_TFTP_ERROR;\r
@@ -512,12 +550,31 @@ Mtftp4RrqHandleOack (
       //\r
       Instance->McastIp      = Reply.McastIp;\r
       Instance->McastPort    = Reply.McastPort;\r
-      Instance->McastUdpPort = UdpIoCreatePort (\r
-                                 Instance->Service->Controller,\r
-                                 Instance->Service->Image,\r
-                                 Mtftp4RrqConfigMcastPort,\r
-                                 Instance\r
-                                 );\r
+      if (Instance->McastUdpPort == NULL) {\r
+        Instance->McastUdpPort = UdpIoCreateIo (\r
+                                   Instance->Service->Controller,\r
+                                   Instance->Service->Image,\r
+                                   Mtftp4RrqConfigMcastPort,\r
+                                   UDP_IO_UDP4_VERSION,\r
+                                   Instance\r
+                                   );\r
+        if (Instance->McastUdpPort != NULL) {\r
+          Status = gBS->OpenProtocol (\r
+                          Instance->McastUdpPort->UdpHandle,\r
+                          &gEfiUdp4ProtocolGuid,\r
+                          (VOID **) &Udp4,\r
+                          Instance->Service->Image,\r
+                          Instance->Handle,\r
+                          EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+                          );\r
+          if (EFI_ERROR (Status)) {\r
+            UdpIoFreeIo (Instance->McastUdpPort);\r
+            Instance->McastUdpPort = NULL;\r
+            return EFI_DEVICE_ERROR;\r
+          }\r
+        }\r
+      }\r
+\r
 \r
       if (Instance->McastUdpPort == NULL) {\r
         return EFI_DEVICE_ERROR;\r
@@ -529,7 +586,7 @@ Mtftp4RrqHandleOack (
         Mtftp4SendError (\r
           Instance,\r
           EFI_MTFTP4_ERRORCODE_ACCESS_VIOLATION,\r
-          "Failed to create socket to receive multicast packet"\r
+          (UINT8 *) "Failed to create socket to receive multicast packet"\r
           );\r
 \r
         return Status;\r
@@ -571,17 +628,16 @@ Mtftp4RrqHandleOack (
   The packet process callback for MTFTP download.\r
 \r
   @param  UdpPacket             The packet received\r
-  @param  Points                The local/remote access point of the packet\r
+  @param  EndPoint              The local/remote access point of the packet\r
   @param  IoStatus              The status of the receiving\r
   @param  Context               Opaque parameter, which is the MTFTP session\r
 \r
-  @return None\r
-\r
 **/\r
 VOID\r
+EFIAPI\r
 Mtftp4RrqInput (\r
   IN NET_BUF                *UdpPacket,\r
-  IN UDP_POINTS             *Points,\r
+  IN UDP_END_POINT          *EndPoint,\r
   IN EFI_STATUS             IoStatus,\r
   IN VOID                   *Context\r
   )\r
@@ -612,7 +668,7 @@ Mtftp4RrqInput (
   //\r
   // Find the port this packet is from to restart receive correctly.\r
   //\r
-  Multicast = (BOOLEAN) (Points->LocalAddr == Instance->McastIp);\r
+  Multicast = (BOOLEAN) (EndPoint->LocalAddr.Addr[0] == Instance->McastIp);\r
 \r
   if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) {\r
     goto ON_EXIT;\r
@@ -624,11 +680,11 @@ Mtftp4RrqInput (
   // is required to use the same port as RemotePort to multicast the\r
   // data.\r
   //\r
-  if (Points->RemotePort != Instance->ConnectedPort) {\r
+  if (EndPoint->RemotePort != Instance->ConnectedPort) {\r
     if (Instance->ConnectedPort != 0) {\r
       goto ON_EXIT;\r
     } else {\r
-      Instance->ConnectedPort = Points->RemotePort;\r
+      Instance->ConnectedPort = EndPoint->RemotePort;\r
     }\r
   }\r
 \r
@@ -638,7 +694,7 @@ Mtftp4RrqInput (
   Len = UdpPacket->TotalSize;\r
 \r
   if (UdpPacket->BlockOpNum > 1) {\r
-    Packet = NetAllocatePool (Len);\r
+    Packet = AllocatePool (Len);\r
 \r
     if (Packet == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
@@ -649,6 +705,7 @@ Mtftp4RrqInput (
 \r
   } else {\r
     Packet = (EFI_MTFTP4_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);\r
+    ASSERT (Packet != NULL);\r
   }\r
 \r
   Opcode = NTOHS (Packet->OpCode);\r
@@ -675,7 +732,7 @@ Mtftp4RrqInput (
         Mtftp4SendError (\r
           Instance,\r
           EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,\r
-          "User aborted the transfer"\r
+          (UINT8 *) "User aborted the transfer"\r
           );\r
       }\r
 \r
@@ -705,6 +762,9 @@ Mtftp4RrqInput (
   case EFI_MTFTP4_OPCODE_ERROR:\r
     Status = EFI_TFTP_ERROR;\r
     break;\r
+\r
+  default:\r
+    break;\r
   }\r
 \r
 ON_EXIT:\r
@@ -714,7 +774,7 @@ ON_EXIT:
   // receive, otherwise end the session.\r
   //\r
   if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) {\r
-    NetFreePool (Packet);\r
+    FreePool (Packet);\r
   }\r
 \r
   if (UdpPacket != NULL) {\r