common/env_flags.c: Add function to validate a MAC address
authorCodrin Ciubotariu <codrin.ciubotariu@freescale.com>
Wed, 9 Sep 2015 15:00:51 +0000 (18:00 +0300)
committerYork Sun <yorksun@freescale.com>
Mon, 21 Sep 2015 15:29:47 +0000 (08:29 -0700)
The code that checks if a string has the format of a MAC address has been
moved to a separate function called eth_validate_ethaddr_str().

This has been done to allow other components (such as vsc9953 driver)
to validate a MAC address.

Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@freescale.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Reviewed-by: York Sun <yorksun@freescale.com>
common/env_flags.c
include/env_flags.h

index 5189f5b2ddd54e0245a4455035a220383c1e3052..e682d8517890cca27d87d812244e16330071e783 100644 (file)
@@ -187,6 +187,31 @@ static void skip_num(int hex, const char *value, const char **end,
                *end = value;
 }
 
+#ifdef CONFIG_CMD_NET
+int eth_validate_ethaddr_str(const char *addr)
+{
+       const char *end;
+       const char *cur;
+       int i;
+
+       cur = addr;
+       for (i = 0; i < 6; i++) {
+               skip_num(1, cur, &end, 2);
+               if (cur == end)
+                       return -1;
+               if (cur + 2 == end && is_hex_prefix(cur))
+                       return -1;
+               if (i != 5 && *end != ':')
+                       return -1;
+               if (i == 5 && *end != '\0')
+                       return -1;
+               cur = end + 1;
+       }
+
+       return 0;
+}
+#endif
+
 /*
  * Based on the declared type enum, validate that the value string complies
  * with that format
@@ -239,19 +264,8 @@ static int _env_flags_validate_type(const char *value,
                }
                break;
        case env_flags_vartype_macaddr:
-               cur = value;
-               for (i = 0; i < 6; i++) {
-                       skip_num(1, cur, &end, 2);
-                       if (cur == end)
-                               return -1;
-                       if (cur + 2 == end && is_hex_prefix(cur))
-                               return -1;
-                       if (i != 5 && *end != ':')
-                               return -1;
-                       if (i == 5 && *end != '\0')
-                               return -1;
-                       cur = end + 1;
-               }
+               if (eth_validate_ethaddr_str(value))
+                       return -1;
                break;
 #endif
        case env_flags_vartype_end:
index 2d2de88fc04fdd187caf5dd32799a56b6ac3ede0..8823fb9602eaa78fdffc161004b003bfc65bff4d 100644 (file)
@@ -109,6 +109,13 @@ enum env_flags_varaccess env_flags_parse_varaccess(const char *flags);
  */
 enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags);
 
+#ifdef CONFIG_CMD_NET
+/*
+ * Check if a string has the format of an Ethernet MAC address
+ */
+int eth_validate_ethaddr_str(const char *addr);
+#endif
+
 #ifdef USE_HOSTCC
 /*
  * Look up the type of a variable directly from the .flags var.