]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Bds/BdsHelper.c
ArmPlatformPkg/Bds: Getting and editing IP addresses
[mirror_edk2.git] / ArmPlatformPkg / Bds / BdsHelper.c
index 152061d0f717e369148277e972dc332f2d82d233..8553577cf1b1272f63b297033c390a25546a56e4 100644 (file)
@@ -12,6 +12,7 @@
 *\r
 **/\r
 \r
+#include <Library/NetLib.h>\r
 #include "BdsInternal.h"\r
 \r
 EFI_STATUS\r
@@ -137,43 +138,87 @@ GetHIInputInteger (
   return Status;\r
 }\r
 \r
+/**\r
+  Get an IPv4 address\r
+\r
+  The function asks the user for an IPv4 address. If the input\r
+  string defines a valid IPv4 address, the four bytes of the\r
+  corresponding IPv4 address are extracted from the string and returned by\r
+  the function.\r
+\r
+  @param[out]  EFI_IP_ADDRESS  OutIpAddr  Returned IPv4 address. Valid if\r
+                                          and only if the returned value\r
+                                          is equal to EFI_SUCCESS\r
+
+  @retval  EFI_SUCCESS            Input completed\r
+  @retval  EFI_ABORTED            Editing aborted by the user\r
+  @retval  EFI_INVALID_PARAMETER  The string returned by the user is\r
+                                  mal-formated\r
+  @retval  EFI_OUT_OF_RESOURCES   Fail to perform the operation due to\r
+                                  lack of resource\r
+**/\r
 EFI_STATUS\r
 GetHIInputIP (\r
-  OUT EFI_IP_ADDRESS   *Ip\r
+  OUT  EFI_IP_ADDRESS  *OutIpAddr\r
   )\r
 {\r
-  CHAR16  CmdLine[255];\r
-  CHAR16  *Str;\r
   EFI_STATUS  Status;\r
+  CHAR16      CmdLine[48];\r
 \r
   CmdLine[0] = '\0';\r
-  Status = EditHIInputStr (CmdLine,255);\r
-  if (!EFI_ERROR(Status)) {\r
-    Str = CmdLine;\r
-    Ip->v4.Addr[0] = (UINT8)StrDecimalToUintn (Str);\r
-\r
-    Str = StrStr (Str, L".");\r
-    if (Str == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
+  Status = EditHIInputStr (CmdLine, 48);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
 \r
-    Ip->v4.Addr[1] = (UINT8)StrDecimalToUintn (++Str);\r
+  Status = NetLibStrToIp4 (CmdLine, &OutIpAddr->v4);\r
 \r
-    Str = StrStr (Str, L".");\r
-    if (Str == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
+  return Status;\r
+}\r
 \r
-    Ip->v4.Addr[2] = (UINT8)StrDecimalToUintn (++Str);\r
+/**\r
+  Edit an IPv4 address\r
+\r
+  The function displays as a string following the "%d.%d.%d.%d" format the\r
+  IPv4 address that is passed in and asks the user to modify it. If the\r
+  resulting string defines a valid IPv4 address, the four bytes of the\r
+  corresponding IPv4 address are extracted from the string and returned by\r
+  the function.\r
+\r
+  @param[in ]  EFI_IP_ADDRESS  InIpAddr   Input IPv4 address\r
+  @param[out]  EFI_IP_ADDRESS  OutIpAddr  Returned IPv4 address. Valid if\r
+                                          and only if the returned value\r
+                                          is equal to EFI_SUCCESS\r
+
+  @retval  EFI_SUCCESS            Update completed\r
+  @retval  EFI_ABORTED            Editing aborted by the user\r
+  @retval  EFI_INVALID_PARAMETER  The string returned by the user is\r
+                                  mal-formated\r
+  @retval  EFI_OUT_OF_RESOURCES   Fail to perform the operation due to\r
+                                  lack of resource\r
+**/\r
+EFI_STATUS\r
+EditHIInputIP (\r
+  IN   EFI_IP_ADDRESS  *InIpAddr,\r
+  OUT  EFI_IP_ADDRESS  *OutIpAddr\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  CHAR16      CmdLine[48];\r
 \r
-    Str = StrStr (Str, L".");\r
-    if (Str == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
+  UnicodeSPrint (
+    CmdLine, 48, L"%d.%d.%d.%d",\r
+    InIpAddr->v4.Addr[0], InIpAddr->v4.Addr[1],\r
+    InIpAddr->v4.Addr[2], InIpAddr->v4.Addr[3]\r
+    );\r
 \r
-    Ip->v4.Addr[3] = (UINT8)StrDecimalToUintn (++Str);\r
+  Status = EditHIInputStr (CmdLine, 48);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
   }\r
 \r
+  Status = NetLibStrToIp4 (CmdLine, &OutIpAddr->v4);\r
+\r
   return Status;\r
 }\r
 \r