]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/DynamicCommand/TftpDynamicCommand/Tftp.c
ShellPkg/TftpDynamicCommand: Clarify the retry count option in command.
[mirror_edk2.git] / ShellPkg / DynamicCommand / TftpDynamicCommand / Tftp.c
index e2491cd54cae5b5e970cb020bd8c3aed1493e121..ed081b5bad7cf5a4b5f16ee506d259a20ad6e178 100644 (file)
@@ -165,6 +165,7 @@ GetFileSize (
   @param[in]   AsciiFilePath  Path of the file, ASCII encoded\r
   @param[in]   FileSize       Size of the file in number of bytes\r
   @param[in]   BlockSize      Value of the TFTP blksize option\r
+  @param[in]   WindowSize     Value of the TFTP window size option\r
   @param[out]  Data           Address where to store the address of the buffer\r
                               where the data of the file were downloaded in\r
                               case of success.\r
@@ -183,6 +184,7 @@ DownloadFile (
   IN   CONST CHAR8          *AsciiFilePath,\r
   IN   UINTN                FileSize,\r
   IN   UINT16               BlockSize,\r
+  IN   UINT16               WindowSize,\r
   OUT  VOID                 **Data\r
   );\r
 \r
@@ -216,7 +218,7 @@ EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData = {
   { { 0, 0, 0, 0 } },               // GatewayIp         - Not relevant as UseDefaultSetting=TRUE\r
   { { 0, 0, 0, 0 } },               // ServerIp          - Not known yet\r
   69,                               // InitialServerPort - Standard TFTP server port\r
-  6,                                // TryCount          - Max number of retransmissions.\r
+  6,                                // TryCount          - The number of times to transmit request packets and wait for a response.\r
   4                                 // TimeoutValue      - Retransmission timeout in seconds.\r
 };\r
 \r
@@ -227,6 +229,7 @@ STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
   {L"-c", TypeValue},\r
   {L"-t", TypeValue},\r
   {L"-s", TypeValue},\r
+  {L"-w", TypeValue},\r
   {NULL , TypeMax}\r
   };\r
 \r
@@ -239,7 +242,17 @@ STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
 ///\r
 #define MTFTP_MIN_BLKSIZE          8\r
 #define MTFTP_MAX_BLKSIZE          65464\r
-\r
+///\r
+/// The default windowsize (1) of tftp.\r
+///\r
+#define MTFTP_DEFAULT_WINDOWSIZE   1\r
+///\r
+/// The valid range of window size option.\r
+/// Note that: RFC 7440 does not mention max window size value, but for the\r
+/// stability reason, the value is limited to 64.\r
+///\r
+#define MTFTP_MIN_WINDOWSIZE       1\r
+#define MTFTP_MAX_WINDOWSIZE       64\r
 \r
 /**\r
   Function for 'tftp' command.\r
@@ -288,6 +301,7 @@ RunTftp (
   VOID                    *Data;\r
   SHELL_FILE_HANDLE       FileHandle;\r
   UINT16                  BlockSize;\r
+  UINT16                  WindowSize;\r
 \r
   ShellStatus         = SHELL_INVALID_PARAMETER;\r
   ProblemParam        = NULL;\r
@@ -297,6 +311,7 @@ RunTftp (
   FileSize            = 0;\r
   DataSize            = 0;\r
   BlockSize           = MTFTP_DEFAULT_BLKSIZE;\r
+  WindowSize          = MTFTP_DEFAULT_WINDOWSIZE;\r
 \r
   //\r
   // Initialize the Shell library (we must be in non-auto-init...)\r
@@ -406,6 +421,10 @@ RunTftp (
     if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TryCount)) {\r
       goto Error;\r
     }\r
+\r
+    if (Mtftp4ConfigData.TryCount == 0) {\r
+      Mtftp4ConfigData.TryCount = 6;\r
+    }\r
   }\r
 \r
   ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");\r
@@ -436,6 +455,20 @@ RunTftp (
     }\r
   }\r
 \r
+  ValueStr = ShellCommandLineGetValue (CheckPackage, L"-w");\r
+  if (ValueStr != NULL) {\r
+    if (!StringToUint16 (ValueStr, &WindowSize)) {\r
+      goto Error;\r
+    }\r
+    if (WindowSize < MTFTP_MIN_WINDOWSIZE || WindowSize > MTFTP_MAX_WINDOWSIZE) {\r
+      ShellPrintHiiEx (\r
+        -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
+        mTftpHiiHandle, L"tftp", ValueStr\r
+      );\r
+      goto Error;\r
+    }\r
+  }\r
+\r
   //\r
   // Locate all MTFTP4 Service Binding protocols\r
   //\r
@@ -510,7 +543,7 @@ RunTftp (
       goto NextHandle;\r
     }\r
 \r
-    Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, &Data);\r
+    Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, WindowSize, &Data);\r
     if (EFI_ERROR (Status)) {\r
       ShellPrintHiiEx (\r
         -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD),\r
@@ -519,6 +552,8 @@ RunTftp (
       goto NextHandle;\r
     }\r
 \r
+    DataSize = FileSize;\r
+\r
     if (!EFI_ERROR (ShellFileExists (LocalFilePath))) {\r
       ShellDeleteFileByName (LocalFilePath);\r
     }\r
@@ -539,7 +574,6 @@ RunTftp (
       goto NextHandle;\r
     }\r
 \r
-    DataSize = FileSize;\r
     Status = ShellWriteFile (FileHandle, &FileSize, Data);\r
     if (!EFI_ERROR (Status)) {\r
       ShellStatus = SHELL_SUCCESS;\r
@@ -877,6 +911,7 @@ Error :
   @param[in]   AsciiFilePath  Path of the file, ASCII encoded\r
   @param[in]   FileSize       Size of the file in number of bytes\r
   @param[in]   BlockSize      Value of the TFTP blksize option\r
+  @param[in]   WindowSize     Value of the TFTP window size option\r
   @param[out]  Data           Address where to store the address of the buffer\r
                               where the data of the file were downloaded in\r
                               case of success.\r
@@ -895,6 +930,7 @@ DownloadFile (
   IN   CONST CHAR8          *AsciiFilePath,\r
   IN   UINTN                FileSize,\r
   IN   UINT16               BlockSize,\r
+  IN   UINT16               WindowSize,\r
   OUT  VOID                 **Data\r
   )\r
 {\r
@@ -903,8 +939,10 @@ DownloadFile (
   VOID                  *Buffer;\r
   DOWNLOAD_CONTEXT      *TftpContext;\r
   EFI_MTFTP4_TOKEN      Mtftp4Token;\r
-  EFI_MTFTP4_OPTION     ReqOpt;\r
-  UINT8                 OptBuf[10];\r
+  UINT8                 BlksizeBuf[10];\r
+  UINT8                 WindowsizeBuf[10];\r
+\r
+  ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));\r
 \r
   // Downloaded file can be large. BS.AllocatePages() is more faster\r
   // than AllocatePool() and avoid fragmentation.\r
@@ -931,19 +969,30 @@ DownloadFile (
   TftpContext->DownloadedNbOfBytes   = 0;\r
   TftpContext->LastReportedNbOfBytes = 0;\r
 \r
-  ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));\r
   Mtftp4Token.Filename    = (UINT8*)AsciiFilePath;\r
   Mtftp4Token.BufferSize  = FileSize;\r
   Mtftp4Token.Buffer      = Buffer;\r
   Mtftp4Token.CheckPacket = CheckPacket;\r
   Mtftp4Token.Context     = (VOID*)TftpContext;\r
+  Mtftp4Token.OptionCount = 0;\r
+  Mtftp4Token.OptionList  = AllocatePool (sizeof (EFI_MTFTP4_OPTION) * 2);\r
+  if (Mtftp4Token.OptionList == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Error;\r
+  }\r
+\r
   if (BlockSize != MTFTP_DEFAULT_BLKSIZE) {\r
-    ReqOpt.OptionStr = (UINT8 *) "blksize";\r
-    AsciiSPrint ((CHAR8 *)OptBuf, sizeof (OptBuf), "%d", BlockSize);\r
-    ReqOpt.ValueStr  = OptBuf;\r
+    Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *) "blksize";\r
+    AsciiSPrint ((CHAR8 *) BlksizeBuf, sizeof (BlksizeBuf), "%d", BlockSize);\r
+    Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr  = BlksizeBuf;\r
+    Mtftp4Token.OptionCount ++;\r
+  }\r
 \r
-    Mtftp4Token.OptionCount = 1;\r
-    Mtftp4Token.OptionList  = &ReqOpt;\r
+  if (WindowSize != MTFTP_DEFAULT_WINDOWSIZE) {\r
+    Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *) "windowsize";\r
+    AsciiSPrint ((CHAR8 *) WindowsizeBuf, sizeof (WindowsizeBuf), "%d", WindowSize);\r
+    Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr  = WindowsizeBuf;\r
+    Mtftp4Token.OptionCount ++;\r
   }\r
 \r
   ShellPrintHiiEx (\r
@@ -959,10 +1008,14 @@ DownloadFile (
 \r
 Error :\r
 \r
-  if (TftpContext == NULL) {\r
+  if (TftpContext != NULL) {\r
     FreePool (TftpContext);\r
   }\r
 \r
+  if (Mtftp4Token.OptionList != NULL) {\r
+    FreePool (Mtftp4Token.OptionList);\r
+  }\r
+\r
   if (EFI_ERROR (Status)) {\r
     gBS->FreePages (PagesAddress, EFI_SIZE_TO_PAGES (FileSize));\r
     return Status;\r