]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Impl.c
MdeModulePkg/Network: Fix potential ASSERT if NetIp4IsUnicast is called
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Impl.c
index 5329e51863c335586af0a3d42cb802f9df28a95b..54942311660d4b4f4490edd951c62c1f236c5297 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Interface routine for Mtftp4.\r
   \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
+Copyright (c) 2006 - 2016, 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
@@ -58,6 +59,12 @@ Mtftp4CleanOperation (
   }\r
 \r
   if (Instance->McastUdpPort != NULL) {\r
+    gBS->CloseProtocol (\r
+           Instance->McastUdpPort->UdpHandle,\r
+           &gEfiUdp4ProtocolGuid,\r
+           gMtftp4DriverBinding.DriverBindingHandle,\r
+           Instance->Handle\r
+           );\r
     UdpIoFreeIo (Instance->McastUdpPort);\r
     Instance->McastUdpPort = NULL;\r
   }\r
@@ -114,6 +121,7 @@ Mtftp4GetInfoCheckPacket (
   MTFTP4_GETINFO_STATE      *State;\r
   EFI_STATUS                Status;\r
   UINT16                    OpCode;\r
+  EFI_MTFTP4_ERROR_HEADER  *ErrorHeader;\r
 \r
   State   = (MTFTP4_GETINFO_STATE *) Token->Context;\r
   OpCode   = NTOHS (Packet->OpCode);\r
@@ -123,6 +131,12 @@ Mtftp4GetInfoCheckPacket (
   //\r
   switch (OpCode) {\r
   case EFI_MTFTP4_OPCODE_ERROR:\r
+    ErrorHeader = (EFI_MTFTP4_ERROR_HEADER *) Packet;\r
+    if (ErrorHeader->ErrorCode == EFI_MTFTP4_ERRORCODE_FILE_NOT_FOUND) {\r
+      DEBUG ((EFI_D_ERROR, "TFTP error code 1 (File Not Found)\n"));\r
+    } else {\r
+      DEBUG ((EFI_D_ERROR, "TFTP error code %d\n", ErrorHeader->ErrorCode));\r
+    }\r
     State->Status = EFI_TFTP_ERROR;\r
     break;\r
 \r
@@ -178,7 +192,7 @@ Mtftp4OverrideValid (
   IP4_ADDR                  Gateway;\r
 \r
   CopyMem (&Ip, &Override->ServerIp, sizeof (IP4_ADDR));\r
-  if (!NetIp4IsUnicast (NTOHL (Ip), 0)) {\r
+  if (IP4_IS_UNSPECIFIED (NTOHL (Ip)) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) {\r
     return FALSE;\r
   }\r
 \r
@@ -194,7 +208,7 @@ Mtftp4OverrideValid (
     Netmask = NTOHL (Netmask);\r
     Ip      = NTOHL (Ip);\r
 \r
-    if (!NetIp4IsUnicast (Gateway, Netmask) || !IP4_NET_EQUAL (Gateway, Ip, Netmask)) {\r
+    if ((Netmask != 0 && !NetIp4IsUnicast (Gateway, Netmask)) || !IP4_NET_EQUAL (Gateway, Ip, Netmask)) {\r
       return FALSE;\r
     }\r
   }\r
@@ -244,7 +258,7 @@ Mtftp4GetMapping (
     return FALSE;\r
   }\r
 \r
-  while (!EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) {\r
+  while (EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) {\r
     Udp->Poll (Udp);\r
 \r
     if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip4Mode, NULL, NULL)) &&\r
@@ -292,13 +306,13 @@ Mtftp4ConfigUnicastPort (
   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        = 0;\r
   UdpConfig.RemotePort         = 0;\r
 \r
   Ip = HTONL (Instance->ServerIp);\r
-  CopyMem (&UdpConfig.RemoteAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
+  IP4_COPY_ADDRESS (&UdpConfig.RemoteAddress, &Ip);\r
 \r
   Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfig);\r
 \r
@@ -653,18 +667,14 @@ EfiMtftp4Configure (
     Gateway  = NTOHL (Gateway);\r
     ServerIp = NTOHL (ServerIp);\r
 \r
-    if (!NetIp4IsUnicast (ServerIp, 0)) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-\r
     if (!ConfigData->UseDefaultSetting &&\r
-       ((!IP4_IS_VALID_NETMASK (Netmask) || !NetIp4IsUnicast (Ip, Netmask)))) {\r
+        ((!IP4_IS_VALID_NETMASK (Netmask) || (Netmask != 0 && !NetIp4IsUnicast (Ip, Netmask))))) {\r
 \r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
-    if ((Gateway != 0) &&\r
-        (!IP4_NET_EQUAL (Gateway, Ip, Netmask) || !NetIp4IsUnicast (Gateway, Netmask))) {\r
+    if ((Gateway != 0) && \r
+        (!IP4_NET_EQUAL (Gateway, Ip, Netmask) || (Netmask != 0 && !NetIp4IsUnicast (Gateway, Netmask)))) {\r
 \r
       return EFI_INVALID_PARAMETER;\r
     }\r
@@ -936,8 +946,8 @@ EfiMtftp4ReadDirectory (
                                  parameters. If NULL, the default parameters that \r
                                  were set in the EFI_MTFTP4_PROTOCOL.Configure() \r
                                  function are used\r
-  @param  Filename               Pointer to ASCIIZ file name string\r
-  @param  ModeStr                Pointer to ASCIIZ mode string. If NULL, "octet" \r
+  @param  Filename               Pointer to null-terminated ASCII file name string\r
+  @param  ModeStr                Pointer to null-terminated ASCII mode string. If NULL, "octet" \r
                                  will be used\r
   @param  OptionCount            Number of option/value string pairs in OptionList\r
   @param  OptionList             Pointer to array of option/value string pairs. \r
@@ -1073,7 +1083,7 @@ EfiMtftp4Poll (
 \r
   if (Instance->State == MTFTP4_STATE_UNCONFIGED) {\r
     return EFI_NOT_STARTED;\r
-  } else if (Instance->State == MTFTP4_STATE_DESTORY) {\r
+  } else if (Instance->State == MTFTP4_STATE_DESTROY) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r