]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Rrq.c
BaseTools:Change the path of the file that Binary Cache
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Rrq.c
index e983d7979192b453b726be4da6c8c96aa268bc51..24c965afb5807771b1bbb8b520d0fce5189171ef 100644 (file)
@@ -1,15 +1,9 @@
 /** @file\r
   Routines to process Rrq (download).\r
-  \r
-(C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
-Copyright (c) 2006 - 2014, 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<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
+(C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -37,9 +31,9 @@ Mtftp4RrqInput (
 \r
 \r
 /**\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
+  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
@@ -99,6 +93,9 @@ Mtftp4RrqSendAck (
 {\r
   EFI_MTFTP4_PACKET         *Ack;\r
   NET_BUF                   *Packet;\r
+  EFI_STATUS                Status;\r
+\r
+  Status = EFI_SUCCESS;\r
 \r
   Packet = NetbufAlloc (sizeof (EFI_MTFTP4_ACK_HEADER));\r
   if (Packet == NULL) {\r
@@ -115,7 +112,12 @@ Mtftp4RrqSendAck (
   Ack->Ack.OpCode   = HTONS (EFI_MTFTP4_OPCODE_ACK);\r
   Ack->Ack.Block[0] = HTONS (BlkNo);\r
 \r
-  return Mtftp4SendPacket (Instance, Packet);\r
+  Status = Mtftp4SendPacket (Instance, Packet);\r
+  if (!EFI_ERROR (Status)) {\r
+    Instance->AckedBlock = Instance->TotalBlock;\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -146,7 +148,7 @@ Mtftp4RrqSaveBlock (
   UINT16                    Block;\r
   UINT64                    Start;\r
   UINT32                    DataLen;\r
-  UINT64                    TotalBlock;\r
+  UINT64                    BlockCounter;\r
   BOOLEAN                   Completed;\r
 \r
   Completed = FALSE;\r
@@ -158,7 +160,7 @@ Mtftp4RrqSaveBlock (
   // This is the last block, save the block no\r
   //\r
   if (DataLen < Instance->BlkSize) {\r
-       Completed = TRUE;\r
+    Completed = TRUE;\r
     Instance->LastBlock = Block;\r
     Mtftp4SetLastBlockNum (&Instance->Blocks, Block);\r
   }\r
@@ -167,10 +169,10 @@ Mtftp4RrqSaveBlock (
   // 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
+  // to accept transfers of unlimited size. So BlockCounter is memorised as\r
   // continuous block counter.\r
   //\r
-  Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block, Completed, &TotalBlock);\r
+  Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block, Completed, &BlockCounter);\r
 \r
   if (Status == EFI_NOT_FOUND) {\r
     return EFI_SUCCESS;\r
@@ -193,7 +195,7 @@ Mtftp4RrqSaveBlock (
   }\r
 \r
   if (Token->Buffer != NULL) {\r
-     Start = MultU64x32 (TotalBlock - 1, Instance->BlkSize);\r
+     Start = MultU64x32 (BlockCounter - 1, Instance->BlkSize);\r
 \r
     if (Start + DataLen <= Token->BufferSize) {\r
       CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);\r
@@ -228,8 +230,8 @@ Mtftp4RrqSaveBlock (
 \r
 \r
 /**\r
-  Function to process the received data packets. \r
-  \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
@@ -257,19 +259,22 @@ Mtftp4RrqHandleData (
   INTN                      Expected;\r
 \r
   *Completed  = FALSE;\r
+  Status      = EFI_SUCCESS;\r
   BlockNum    = NTOHS (Packet->Data.Block);\r
   Expected    = Mtftp4GetNextBlockNum (&Instance->Blocks);\r
 \r
   ASSERT (Expected >= 0);\r
 \r
   //\r
-  // If we are active and received an unexpected packet, retransmit\r
-  // the last ACK then restart receiving. If we are passive, save\r
-  // the block.\r
+  // If we are active (Master) and received an unexpected packet, transmit\r
+  // the ACK for the block we received, then restart receiving the\r
+  // expected one. If we are passive (Slave), save the block.\r
   //\r
   if (Instance->Master && (Expected != BlockNum)) {\r
-    Mtftp4Retransmit (Instance);\r
-    return EFI_SUCCESS;\r
+    //\r
+    // If Expected is 0, (UINT16) (Expected - 1) is also the expected Ack number (65535).\r
+    //\r
+    return Mtftp4RrqSendAck (Instance,  (UINT16) (Expected - 1));\r
   }\r
 \r
   Status = Mtftp4RrqSaveBlock (Instance, Packet, Len);\r
@@ -278,6 +283,11 @@ Mtftp4RrqHandleData (
     return Status;\r
   }\r
 \r
+  //\r
+  // Record the total received and saved block number.\r
+  //\r
+  Instance->TotalBlock ++;\r
+\r
   //\r
   // Reset the passive client's timer whenever it received a\r
   // valid data packet.\r
@@ -309,16 +319,19 @@ Mtftp4RrqHandleData (
       BlockNum = (UINT16) (Expected - 1);\r
     }\r
 \r
-    Mtftp4RrqSendAck (Instance, BlockNum);\r
+    if (Instance->WindowSize == (Instance->TotalBlock - Instance->AckedBlock) || Expected < 0) {\r
+      Status = Mtftp4RrqSendAck (Instance, BlockNum);\r
+    }\r
+\r
   }\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 \r
 /**\r
   Validate whether the options received in the server's OACK packet is valid.\r
-  \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
@@ -349,11 +362,13 @@ Mtftp4RrqOackValid (
   }\r
 \r
   //\r
-  // Server can only specify a smaller block size to be used and\r
+  // Server can only specify a smaller block size and window size to be used and\r
   // return the timeout matches that requested.\r
   //\r
   if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0)&& (Reply->BlkSize > Request->BlkSize)) ||\r
-      (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout))) {\r
+      (((Reply->Exist & MTFTP4_WINDOWSIZE_EXIST) != 0)&& (Reply->WindowSize > Request->WindowSize)) ||\r
+      (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout))\r
+     ) {\r
     return FALSE;\r
   }\r
 \r
@@ -428,20 +443,20 @@ Mtftp4RrqConfigMcastPort (
     return Status;\r
   }\r
 \r
-  if (!Config->UseDefaultSetting && \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
+                                       McastIo->Protocol.Udp4,\r
                                        FALSE,\r
                                        &mZeroIp4Addr,\r
                                        &mZeroIp4Addr,\r
                                        &Config->GatewayIp\r
                                        );\r
-                             \r
+\r
     if (EFI_ERROR (Status)) {\r
       McastIo->Protocol.Udp4->Configure (McastIo->Protocol.Udp4, NULL);\r
       return Status;\r
@@ -459,8 +474,8 @@ Mtftp4RrqConfigMcastPort (
 \r
 \r
 /**\r
-  Function to process the OACK. \r
-  \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
@@ -507,7 +522,7 @@ Mtftp4RrqHandleOack (
   //\r
   ZeroMem (&Reply, sizeof (MTFTP4_OPTION));\r
 \r
-  Status = Mtftp4ParseOptionOack (Packet, Len, &Reply);\r
+  Status = Mtftp4ParseOptionOack (Packet, Len, Instance->Operation, &Reply);\r
 \r
   if (EFI_ERROR (Status) ||\r
       !Mtftp4RrqOackValid (Instance, &Reply, &Instance->RequestOption)) {\r
@@ -529,7 +544,7 @@ Mtftp4RrqHandleOack (
 \r
     //\r
     // Save the multicast info. Always update the Master, only update the\r
-    // multicast IP address, block size, timeoute at the first time. If IP\r
+    // multicast IP address, block size, window size, timeoute at the first time. If IP\r
     // address is updated, create a UDP child to receive the multicast.\r
     //\r
     Instance->Master = Reply.Master;\r
@@ -591,31 +606,39 @@ Mtftp4RrqHandleOack (
 \r
         return Status;\r
       }\r
-    \r
+\r
       //\r
       // Update the parameters used.\r
       //\r
       if (Reply.BlkSize != 0) {\r
         Instance->BlkSize = Reply.BlkSize;\r
       }\r
-      \r
+\r
+      if (Reply.WindowSize != 0) {\r
+        Instance->WindowSize = Reply.WindowSize;\r
+      }\r
+\r
       if (Reply.Timeout != 0) {\r
         Instance->Timeout = Reply.Timeout;\r
-      }  \r
-    }    \r
-    \r
+      }\r
+    }\r
+\r
   } else {\r
     Instance->Master = TRUE;\r
-    \r
+\r
     if (Reply.BlkSize != 0) {\r
       Instance->BlkSize = Reply.BlkSize;\r
     }\r
 \r
+    if (Reply.WindowSize != 0) {\r
+      Instance->WindowSize = Reply.WindowSize;\r
+    }\r
+\r
     if (Reply.Timeout != 0) {\r
       Instance->Timeout = Reply.Timeout;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Send an ACK to (Expected - 1) which is 0 for unicast download,\r
   // or tell the server we want to receive the Expected block.\r
@@ -762,7 +785,7 @@ Mtftp4RrqInput (
   case EFI_MTFTP4_OPCODE_ERROR:\r
     Status = EFI_TFTP_ERROR;\r
     break;\r
-    \r
+\r
   default:\r
     break;\r
   }\r