net: Consolidate the parsing of bootfile
authorJoe Hershberger <joe.hershberger@ni.com>
Wed, 4 Jul 2018 00:36:43 +0000 (19:36 -0500)
committerJoe Hershberger <joe.hershberger@ni.com>
Thu, 26 Jul 2018 19:08:20 +0000 (14:08 -0500)
The same basic parsing was implemented in tftp and nfs, so add a helper
function to do the work once.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
include/net.h
net/net.c
net/nfs.c
net/tftp.c

index de2d7bba19d3937181a42f4d2c72c7e9adb2d50c..62f82c4dca72c5f1d64a1bdf7386e03bfab7a7bc 100644 (file)
@@ -842,6 +842,17 @@ void copy_filename(char *dst, const char *src, int size);
 /* check if serverip is specified in filename from the command line */
 int is_serverip_in_cmd(void);
 
+/**
+ * net_parse_bootfile - Parse the bootfile env var / cmd line param
+ *
+ * @param ipaddr - a pointer to the ipaddr to populate if included in bootfile
+ * @param filename - a pointer to the string to save the filename part
+ * @param max_len - The longest - 1 that the filename part can be
+ *
+ * return 1 if parsed, 0 if bootfile is empty
+ */
+int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len);
+
 /* get a random source port */
 unsigned int random_port(void);
 
index 1b6781d358f04fdc5d286f43776ae70023b737c5..31cf306ae71e11cf0f5ad65e992a1380d84faeba 100644 (file)
--- a/net/net.c
+++ b/net/net.c
@@ -1517,6 +1517,26 @@ int is_serverip_in_cmd(void)
        return !!strchr(net_boot_file_name, ':');
 }
 
+int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
+{
+       char *colon;
+
+       if (net_boot_file_name[0] == '\0')
+               return 0;
+
+       colon = strchr(net_boot_file_name, ':');
+       if (colon) {
+               if (ipaddr)
+                       *ipaddr = string_to_ip(net_boot_file_name);
+               strncpy(filename, colon + 1, max_len);
+       } else {
+               strncpy(filename, net_boot_file_name, max_len);
+       }
+       filename[max_len - 1] = '\0';
+
+       return 1;
+}
+
 #if    defined(CONFIG_CMD_NFS)         || \
        defined(CONFIG_CMD_SNTP)        || \
        defined(CONFIG_CMD_DNS)
index 86dfe9a494a79fbb61b94c22b96f17da92516fec..d6a7f8e827a1ce47712daca250f45c1ccc24c531 100644 (file)
--- a/net/nfs.c
+++ b/net/nfs.c
@@ -859,7 +859,8 @@ void nfs_start(void)
                return;
        }
 
-       if (net_boot_file_name[0] == '\0') {
+       if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
+                               sizeof(nfs_path_buff))) {
                sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
                        net_ip.s_addr & 0xFF,
                        (net_ip.s_addr >>  8) & 0xFF,
@@ -868,18 +869,6 @@ void nfs_start(void)
 
                printf("*** Warning: no boot file name; using '%s'\n",
                       nfs_path);
-       } else {
-               char *p = net_boot_file_name;
-
-               p = strchr(p, ':');
-
-               if (p != NULL) {
-                       nfs_server_ip = string_to_ip(net_boot_file_name);
-                       ++p;
-                       strcpy(nfs_path, p);
-               } else {
-                       strcpy(nfs_path, net_boot_file_name);
-               }
        }
 
        nfs_filename = basename(nfs_path);
index 6671b1f7ca726acf44b6f18f41f9f017c10fd1ec..68ffd814146c8ded1ec8c613980f4c2d45ba8f2c 100644 (file)
@@ -735,7 +735,7 @@ void tftp_start(enum proto_t protocol)
              tftp_block_size_option, timeout_ms);
 
        tftp_remote_ip = net_server_ip;
-       if (net_boot_file_name[0] == '\0') {
+       if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
                sprintf(default_filename, "%02X%02X%02X%02X.img",
                        net_ip.s_addr & 0xFF,
                        (net_ip.s_addr >>  8) & 0xFF,
@@ -747,17 +747,6 @@ void tftp_start(enum proto_t protocol)
 
                printf("*** Warning: no boot file name; using '%s'\n",
                       tftp_filename);
-       } else {
-               char *p = strchr(net_boot_file_name, ':');
-
-               if (p == NULL) {
-                       strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
-                       tftp_filename[MAX_LEN - 1] = 0;
-               } else {
-                       tftp_remote_ip = string_to_ip(net_boot_file_name);
-                       strncpy(tftp_filename, p + 1, MAX_LEN);
-                       tftp_filename[MAX_LEN - 1] = 0;
-               }
        }
 
        printf("Using %s device\n", eth_get_name());