]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Rrq.c
index da364329fcf721c9e508aa9e6dad77a0256dd843..897358e5f13378740820549eceedf875807c5ff7 100644 (file)
@@ -1,15 +1,9 @@
 /** @file\r
   Mtftp6 Rrq process functions implementation.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
 \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
-\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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -35,6 +29,9 @@ Mtftp6RrqSendAck (
 {\r
   EFI_MTFTP6_PACKET         *Ack;\r
   NET_BUF                   *Packet;\r
+  EFI_STATUS                Status;\r
+\r
+  Status = EFI_SUCCESS;\r
 \r
   //\r
   // Allocate net buffer to create ack packet.\r
@@ -59,8 +56,14 @@ Mtftp6RrqSendAck (
   // Reset current retry count of the instance.\r
   //\r
   Instance->CurRetry = 0;\r
+  Instance->LastPacket = Packet;\r
 \r
-  return Mtftp6TransmitPacket (Instance, Packet);\r
+  Status = Mtftp6TransmitPacket (Instance, Packet);\r
+  if (!EFI_ERROR (Status)) {\r
+    Instance->AckedBlock = Instance->TotalBlock;\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -93,7 +96,7 @@ Mtftp6RrqSaveBlock (
   UINT16                    Block;\r
   UINT64                    Start;\r
   UINT32                    DataLen;\r
-  UINT64                    TotalBlock;\r
+  UINT64                    BlockCounter;\r
   BOOLEAN                   Completed;\r
 \r
   Completed = FALSE;\r
@@ -105,7 +108,7 @@ Mtftp6RrqSaveBlock (
   // This is the last block, save the block num\r
   //\r
   if (DataLen < Instance->BlkSize) {\r
-       Completed = TRUE;\r
+    Completed = TRUE;\r
     Instance->LastBlk = Block;\r
     Mtftp6SetLastBlockNum (&Instance->BlkList, Block);\r
   }\r
@@ -114,10 +117,10 @@ Mtftp6RrqSaveBlock (
   // Remove this block number from the file hole. If Mtftp6RemoveBlockNum\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 = Mtftp6RemoveBlockNum (&Instance->BlkList, Block, Completed, &TotalBlock);\r
+  Status = Mtftp6RemoveBlockNum (&Instance->BlkList, Block, Completed, &BlockCounter);\r
 \r
   if (Status == EFI_NOT_FOUND) {\r
     return EFI_SUCCESS;\r
@@ -153,7 +156,7 @@ Mtftp6RrqSaveBlock (
 \r
   if (Token->Buffer != NULL) {\r
 \r
-    Start = MultU64x32 (TotalBlock - 1, Instance->BlkSize);\r
+    Start = MultU64x32 (BlockCounter - 1, Instance->BlkSize);\r
     if (Start + DataLen <= Token->BufferSize) {\r
       CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);\r
       //\r
@@ -223,15 +226,16 @@ Mtftp6RrqHandleData (
   INTN                      Expected;\r
 \r
   *IsCompleted = FALSE;\r
+  Status       = EFI_SUCCESS;\r
   BlockNum     = NTOHS (Packet->Data.Block);\r
   Expected     = Mtftp6GetNextBlockNum (&Instance->BlkList);\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->IsMaster && (Expected != BlockNum)) {\r
     //\r
@@ -241,8 +245,10 @@ Mtftp6RrqHandleData (
     NetbufFree (*UdpPacket);\r
     *UdpPacket = NULL;\r
 \r
-    Mtftp6TransmitPacket (Instance, Instance->LastPacket);\r
-    return EFI_SUCCESS;\r
+    //\r
+    // If Expected is 0, (UINT16) (Expected - 1) is also the expected Ack number (65535).\r
+    //\r
+    return Mtftp6RrqSendAck (Instance,  (UINT16) (Expected - 1));\r
   }\r
 \r
   Status = Mtftp6RrqSaveBlock (Instance, Packet, Len, UdpPacket);\r
@@ -251,6 +257,11 @@ Mtftp6RrqHandleData (
     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 valid data packet.\r
   //\r
@@ -287,10 +298,12 @@ Mtftp6RrqHandleData (
     NetbufFree (*UdpPacket);\r
     *UdpPacket = NULL;\r
 \r
-    Mtftp6RrqSendAck (Instance, BlockNum);\r
+    if (Instance->WindowSize == (Instance->TotalBlock - Instance->AckedBlock) || Expected < 0) {\r
+      Status = Mtftp6RrqSendAck (Instance, BlockNum);\r
+    }\r
   }\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 \r
@@ -325,12 +338,13 @@ Mtftp6RrqOackValid (
   }\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 windowsize to be used and\r
   // return the timeout matches that requested.\r
   //\r
   if ((((ReplyInfo->BitMap & MTFTP6_OPT_BLKSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) ||\r
+      (((ReplyInfo->BitMap & MTFTP6_OPT_WINDOWSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) ||\r
       (((ReplyInfo->BitMap & MTFTP6_OPT_TIMEOUT_BIT) != 0) && (ReplyInfo->Timeout != RequestInfo->Timeout))\r
-      ) {\r
+     ) {\r
     return FALSE;\r
   }\r
 \r
@@ -453,8 +467,10 @@ Mtftp6RrqHandleOack (
   MTFTP6_EXT_OPTION_INFO    ExtInfo;\r
   EFI_STATUS                Status;\r
   INTN                      Expected;\r
+  EFI_UDP6_PROTOCOL         *Udp6;\r
 \r
   *IsCompleted = FALSE;\r
+  Options = NULL;\r
 \r
   //\r
   // If already started the master download, don't change the\r
@@ -477,11 +493,12 @@ Mtftp6RrqHandleOack (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+  ASSERT (Options != NULL);\r
 \r
   //\r
   // Parse the extensive options in the packet.\r
   //\r
-  Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, &ExtInfo);\r
+  Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, Instance->Operation, &ExtInfo);\r
 \r
   if (EFI_ERROR (Status) || !Mtftp6RrqOackValid (Instance, &ExtInfo, &Instance->ExtInfo)) {\r
     //\r
@@ -511,7 +528,7 @@ Mtftp6RrqHandleOack (
 \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->IsMaster = ExtInfo.IsMaster;\r
@@ -546,13 +563,30 @@ Mtftp6RrqHandleOack (
         );\r
 \r
       Instance->McastPort  = ExtInfo.McastPort;\r
-      Instance->McastUdpIo = UdpIoCreateIo (\r
-                               Instance->Service->Controller,\r
-                               Instance->Service->Image,\r
-                               Mtftp6RrqConfigMcastUdpIo,\r
-                               UDP_IO_UDP6_VERSION,\r
-                               Instance\r
-                               );\r
+      if (Instance->McastUdpIo == NULL) {\r
+        Instance->McastUdpIo = UdpIoCreateIo (\r
+                                 Instance->Service->Controller,\r
+                                 Instance->Service->Image,\r
+                                 Mtftp6RrqConfigMcastUdpIo,\r
+                                 UDP_IO_UDP6_VERSION,\r
+                                 Instance\r
+                                 );\r
+        if (Instance->McastUdpIo != NULL) {\r
+          Status = gBS->OpenProtocol (\r
+                          Instance->McastUdpIo->UdpHandle,\r
+                          &gEfiUdp6ProtocolGuid,\r
+                          (VOID **) &Udp6,\r
+                          Instance->Service->Image,\r
+                          Instance->Handle,\r
+                          EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+                          );\r
+          if (EFI_ERROR (Status)) {\r
+            UdpIoFreeIo (Instance->McastUdpIo);\r
+            Instance->McastUdpIo = NULL;\r
+            return EFI_DEVICE_ERROR;\r
+          }\r
+        }\r
+      }\r
 \r
       if (Instance->McastUdpIo == NULL) {\r
         return EFI_DEVICE_ERROR;\r
@@ -591,6 +625,10 @@ Mtftp6RrqHandleOack (
         Instance->BlkSize = ExtInfo.BlkSize;\r
       }\r
 \r
+      if (ExtInfo.WindowSize != 0) {\r
+        Instance->WindowSize = ExtInfo.WindowSize;\r
+      }\r
+\r
       if (ExtInfo.Timeout != 0) {\r
         Instance->Timeout = ExtInfo.Timeout;\r
       }\r
@@ -604,6 +642,10 @@ Mtftp6RrqHandleOack (
       Instance->BlkSize = ExtInfo.BlkSize;\r
     }\r
 \r
+    if (ExtInfo.WindowSize != 0) {\r
+      Instance->WindowSize = ExtInfo.WindowSize;\r
+    }\r
+\r
     if (ExtInfo.Timeout != 0) {\r
       Instance->Timeout = ExtInfo.Timeout;\r
     }\r