]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellTftpCommandLib/Tftp.c
IntelFsp2WrapperPkg/FspmWrapperPeim: Update debug message match code.
[mirror_edk2.git] / ShellPkg / Library / UefiShellTftpCommandLib / Tftp.c
index b872afdb8952ac4611aeb192374eaa55f2125ac5..831b9c3d0250c342a4c19ee0073d5be65fe90981 100644 (file)
@@ -2,6 +2,8 @@
   The implementation for the 'tftp' Shell command.\r
 \r
   Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2015, Intel Corporation. All rights reserved. <BR>\r
+  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<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
@@ -14,6 +16,8 @@
 \r
 #include "UefiShellTftpCommandLib.h"\r
 \r
+#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32\r
+\r
 /*\r
    Constant strings and definitions related to the message indicating the amount of\r
    progress in the dowloading of a TFTP file.\r
@@ -36,18 +40,70 @@ STATIC CONST CHAR16 mTftpProgressFrame[] = L"[
 // (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'\r
 STATIC CONST CHAR16 mTftpProgressDelete[] = L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";\r
 \r
-STATIC BOOLEAN StringToUint16 (\r
+/**\r
+  Check and convert the UINT16 option values of the 'tftp' command\r
+\r
+  @param[in]  ValueStr  Value as an Unicode encoded string\r
+  @param[out] Value     UINT16 value\r
+\r
+  @return     TRUE      The value was returned.\r
+  @return     FALSE     A parsing error occured.\r
+**/\r
+STATIC \r
+BOOLEAN \r
+StringToUint16 (\r
   IN   CONST CHAR16  *ValueStr,\r
   OUT  UINT16        *Value\r
   );\r
 \r
-STATIC EFI_STATUS GetNicName (\r
+/**\r
+  Get the name of the NIC.\r
+\r
+  @param[in]   ControllerHandle  The network physical device handle.\r
+  @param[in]   NicNumber         The network physical device number.\r
+  @param[out]  NicName           Address where to store the NIC name.\r
+                                 The memory area has to be at least\r
+                                 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH \r
+                                 double byte wide.\r
+\r
+  @return  EFI_SUCCESS  The name of the NIC was returned.\r
+  @return  Others       The creation of the child for the Managed\r
+                        Network Service failed or the opening of\r
+                        the Managed Network Protocol failed or\r
+                        the operational parameters for the\r
+                        Managed Network Protocol could not be\r
+                        read.\r
+**/\r
+STATIC \r
+EFI_STATUS \r
+GetNicName (\r
   IN   EFI_HANDLE  ControllerHandle,\r
-  IN   UINTN       Index,\r
+  IN   UINTN       NicNumber,\r
   OUT  CHAR16      *NicName\r
   );\r
 \r
-STATIC EFI_STATUS CreateServiceChildAndOpenProtocol (\r
+/**\r
+  Create a child for the service identified by its service binding protocol GUID\r
+  and get from the child the interface of the protocol identified by its GUID.\r
+\r
+  @param[in]   ControllerHandle            Controller handle.\r
+  @param[in]   ServiceBindingProtocolGuid  Service binding protocol GUID of the\r
+                                           service to be created.\r
+  @param[in]   ProtocolGuid                GUID of the protocol to be open.\r
+  @param[out]  ChildHandle                 Address where the handler of the\r
+                                           created child is returned. NULL is\r
+                                           returned in case of error.\r
+  @param[out]  Interface                   Address where a pointer to the\r
+                                           protocol interface is returned in\r
+                                           case of success.\r
+\r
+  @return  EFI_SUCCESS  The child was created and the protocol opened.\r
+  @return  Others       Either the creation of the child or the opening\r
+                        of the protocol failed.\r
+**/\r
+STATIC \r
+EFI_STATUS \r
+CreateServiceChildAndOpenProtocol (\r
   IN   EFI_HANDLE  ControllerHandle,\r
   IN   EFI_GUID    *ServiceBindingProtocolGuid,\r
   IN   EFI_GUID    *ProtocolGuid,\r
@@ -55,20 +111,71 @@ STATIC EFI_STATUS CreateServiceChildAndOpenProtocol (
   OUT  VOID        **Interface\r
   );\r
 \r
-STATIC VOID CloseProtocolAndDestroyServiceChild (\r
+/**\r
+  Close the protocol identified by its GUID on the child handle of the service\r
+  identified by its service binding protocol GUID, then destroy the child\r
+  handle.\r
+\r
+  @param[in]  ControllerHandle            Controller handle.\r
+  @param[in]  ServiceBindingProtocolGuid  Service binding protocol GUID of the\r
+                                          service to be destroyed.\r
+  @param[in]  ProtocolGuid                GUID of the protocol to be closed.\r
+  @param[in]  ChildHandle                 Handle of the child to be destroyed.\r
+\r
+**/\r
+STATIC \r
+VOID \r
+CloseProtocolAndDestroyServiceChild (\r
   IN  EFI_HANDLE  ControllerHandle,\r
   IN  EFI_GUID    *ServiceBindingProtocolGuid,\r
   IN  EFI_GUID    *ProtocolGuid,\r
   IN  EFI_HANDLE  ChildHandle\r
   );\r
 \r
-STATIC EFI_STATUS GetFileSize (\r
+/**\r
+  Worker function that gets the size in numbers of bytes of a file from a TFTP\r
+  server before to download the file.\r
+\r
+  @param[in]   Mtftp4    MTFTP4 protocol interface\r
+  @param[in]   FilePath  Path of the file, ASCII encoded\r
+  @param[out]  FileSize  Address where to store the file size in number of\r
+                         bytes.\r
+\r
+  @retval  EFI_SUCCESS      The size of the file was returned.\r
+  @retval  EFI_UNSUPPORTED  The server does not support the "tsize" option.\r
+  @retval  Others           Error when retrieving the information from the server\r
+                            (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r
+                            or error when parsing the response of the server.\r
+**/\r
+STATIC \r
+EFI_STATUS \r
+GetFileSize (\r
   IN   EFI_MTFTP4_PROTOCOL  *Mtftp4,\r
   IN   CONST CHAR8          *FilePath,\r
   OUT  UINTN                *FileSize\r
   );\r
 \r
-STATIC EFI_STATUS DownloadFile (\r
+/**\r
+  Worker function that download the data of a file from a TFTP server given\r
+  the path of the file and its size.\r
+\r
+  @param[in]   Mtftp4         MTFTP4 protocol interface\r
+  @param[in]   FilePath       Path of the file, Unicode encoded\r
+  @param[in]   AsciiFilePath  Path of the file, ASCII encoded\r
+  @param[in]   FileSize       Size of the file in number of bytes\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
+\r
+  @retval  EFI_SUCCESS           The file was downloaded.\r
+  @retval  EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval  Others                The downloading of the file from the server failed\r
+                                 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r
+\r
+**/\r
+STATIC \r
+EFI_STATUS \r
+DownloadFile (\r
   IN   EFI_MTFTP4_PROTOCOL  *Mtftp4,\r
   IN   CONST CHAR16         *FilePath,\r
   IN   CONST CHAR8          *AsciiFilePath,\r
@@ -76,7 +183,22 @@ STATIC EFI_STATUS DownloadFile (
   OUT  VOID                 **Data\r
   );\r
 \r
-STATIC EFI_STATUS CheckPacket (\r
+/**\r
+  Update the progress of a file download\r
+  This procedure is called each time a new TFTP packet is received.\r
+\r
+  @param[in]  This       MTFTP4 protocol interface\r
+  @param[in]  Token      Parameters for the download of the file\r
+  @param[in]  PacketLen  Length of the packet\r
+  @param[in]  Packet     Address of the packet\r
+\r
+  @retval  EFI_SUCCESS  All packets are accepted.\r
+\r
+**/\r
+STATIC \r
+EFI_STATUS \r
+EFIAPI\r
+CheckPacket (\r
   IN EFI_MTFTP4_PROTOCOL  *This,\r
   IN EFI_MTFTP4_TOKEN     *Token,\r
   IN UINT16               PacketLen,\r
@@ -142,7 +264,7 @@ ShellCommandRunTftp (
   EFI_HANDLE              *Handles;\r
   UINTN                   HandleCount;\r
   UINTN                   NicNumber;\r
-  CHAR16                  NicName[IP4_NIC_NAME_LENGTH];\r
+  CHAR16                  NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];\r
   EFI_HANDLE              ControllerHandle;\r
   EFI_HANDLE              Mtftp4ChildHandle;\r
   EFI_MTFTP4_PROTOCOL     *Mtftp4;\r
@@ -155,6 +277,7 @@ ShellCommandRunTftp (
   NicFound            = FALSE;\r
   AsciiRemoteFilePath = NULL;\r
   Handles             = NULL;\r
+  FileSize            = 0;\r
 \r
   //\r
   // Initialize the Shell library (we must be in non-auto-init...)\r
@@ -218,6 +341,7 @@ ShellCommandRunTftp (
   }\r
 \r
   RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);\r
+  ASSERT(RemoteFilePath != NULL);\r
   AsciiRemoteFilePath = AllocatePool (\r
                           (StrLen (RemoteFilePath) + 1) * sizeof (CHAR8)\r
                           );\r
@@ -408,7 +532,7 @@ ShellCommandRunTftp (
       );\r
   }\r
 \r
-  if ((UserNicName != NULL) && (NicFound == FALSE)) {\r
+  if ((UserNicName != NULL) && (!NicFound)) {\r
     ShellPrintHiiEx (\r
       -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),\r
       gShellTftpHiiHandle, UserNicName\r
@@ -455,7 +579,7 @@ StringToUint16 (
     return FALSE;\r
   }\r
 \r
-  *Value = Val;\r
+  *Value = (UINT16)Val;\r
   return TRUE;\r
 }\r
 \r
@@ -466,7 +590,8 @@ StringToUint16 (
   @param[in]   NicNumber         The network physical device number.\r
   @param[out]  NicName           Address where to store the NIC name.\r
                                  The memory area has to be at least\r
-                                 IP4_NIC_NAME_LENGTH bytes wide.\r
+                                 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH \r
+                                 double byte wide.\r
 \r
   @return  EFI_SUCCESS  The name of the NIC was returned.\r
   @return  Others       The creation of the child for the Managed\r
@@ -507,7 +632,7 @@ GetNicName (
 \r
   UnicodeSPrint (\r
     NicName,\r
-    IP4_NIC_NAME_LENGTH,\r
+    IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,\r
     SnpMode.IfType == NET_IFTYPE_ETHERNET ?\r
     L"eth%d" :\r
     L"unk%d" ,\r
@@ -817,6 +942,7 @@ Error :
 **/\r
 STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 CheckPacket (\r
   IN EFI_MTFTP4_PROTOCOL  *This,\r
   IN EFI_MTFTP4_TOKEN     *Token,\r
@@ -826,10 +952,11 @@ CheckPacket (
 {\r
   DOWNLOAD_CONTEXT  *Context;\r
   CHAR16            Progress[TFTP_PROGRESS_MESSAGE_SIZE];\r
-  UINT64            NbOfKb;\r
+  UINT            NbOfKb;\r
   UINTN             Index;\r
   UINTN             LastStep;\r
   UINTN             Step;\r
+  EFI_STATUS        Status;\r
 \r
   if ((NTOHS (Packet->OpCode)) != EFI_MTFTP4_OPCODE_DATA) {\r
     return EFI_SUCCESS;\r
@@ -850,17 +977,19 @@ CheckPacket (
   NbOfKb = Context->DownloadedNbOfBytes / 1024;\r
 \r
   Progress[0] = L'\0';\r
-  LastStep  = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) /\r
-              Context->FileSize;\r
-  Step      = (Context->DownloadedNbOfBytes   * TFTP_PROGRESS_SLIDER_STEPS) /\r
-              Context->FileSize;\r
+  LastStep  = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
+  Step      = (Context->DownloadedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
+\r
   if (Step <= LastStep) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
   ShellPrintEx (-1, -1, L"%s", mTftpProgressDelete);\r
 \r
-  StrCpy (Progress, mTftpProgressFrame);\r
+  Status = StrCpyS (Progress, TFTP_PROGRESS_MESSAGE_SIZE, mTftpProgressFrame);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
   for (Index = 1; Index < Step; Index++) {\r
     Progress[Index] = L'=';\r
   }\r