There's nothing bootloader spec specific about the current handling of
nfs:// URIs. Move it out of blspec to simplify it and allow its reuse by
other bootentry providers as well.

Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de>
---
 common/blspec.c | 118 +-----------------------------------------------
 common/boot.c   | 116 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+), 116 deletions(-)

diff --git a/common/blspec.c b/common/blspec.c
index 27ac6560b97b..740726a571d2 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -16,7 +16,6 @@
 #include <init.h>
 #include <bootm.h>
 #include <glob.h>
-#include <net.h>
 #include <fs.h>
 #include <of.h>
 #include <linux/stat.h>
@@ -298,111 +297,6 @@ static int blspec_have_entry(struct bootentries 
*bootentries, const char *path)
        return 0;
 }
 
-/*
- * nfs_find_mountpath - Check if a given url is already mounted
- */
-static const char *nfs_find_mountpath(const char *nfshostpath)
-{
-       struct fs_device *fsdev;
-
-       for_each_fs_device(fsdev) {
-               if (fsdev->backingstore && !strcmp(fsdev->backingstore, 
nfshostpath))
-                       return fsdev->path;
-       }
-
-       return NULL;
-}
-
-/*
- * parse_nfs_url - check for nfs:// style url
- *
- * Check if the passed string is a NFS url and if yes, mount the
- * NFS and return the path we have mounted to.
- */
-static char *parse_nfs_url(const char *url)
-{
-       char *sep, *str, *host, *port, *path;
-       char *mountpath = NULL, *hostpath = NULL, *options = NULL;
-       const char *prevpath;
-       IPaddr_t ip;
-       int ret;
-
-       if (!IS_ENABLED(CONFIG_FS_NFS))
-               return NULL;
-
-       if (strncmp(url, "nfs://", 6))
-               return NULL;
-
-       url += 6;
-
-       str = xstrdup(url);
-
-       host = str;
-
-       sep = strchr(str, '/');
-       if (!sep) {
-               ret = -EINVAL;
-               goto out;
-       }
-
-       *sep++ = 0;
-
-       path = sep;
-
-       port = strchr(host, ':');
-       if (port)
-               *port++ = 0;
-
-       ret = ifup_all(0);
-       if (ret) {
-               pr_err("Failed to bring up networking\n");
-               goto out;
-       }
-
-       ret = resolv(host, &ip);
-       if (ret) {
-               pr_err("Cannot resolve \"%s\": %s\n", host, strerror(-ret));
-               goto out;
-       }
-
-       hostpath = basprintf("%pI4:%s", &ip, path);
-
-       prevpath = nfs_find_mountpath(hostpath);
-
-       if (prevpath) {
-               mountpath = xstrdup(prevpath);
-       } else {
-               mountpath = basprintf("/mnt/nfs-%s-bootentries-%08x", host,
-                                       rand());
-               if (port)
-                       options = basprintf("mountport=%s,port=%s", port,
-                                             port);
-
-               ret = make_directory(mountpath);
-               if (ret)
-                       goto out;
-
-               pr_debug("host: %s port: %s path: %s\n", host, port, path);
-               pr_debug("hostpath: %s mountpath: %s options: %s\n", hostpath, 
mountpath, options);
-
-               ret = mount(hostpath, "nfs", mountpath, options);
-               if (ret)
-                       goto out;
-       }
-
-       ret = 0;
-
-out:
-       free(str);
-       free(hostpath);
-       free(options);
-
-       if (ret)
-               free(mountpath);
-
-       return ret ? NULL : mountpath;
-}
-
 /*
  * entry_is_of_compatible - check if a bootspec entry is compatible with
  *                          the current machine.
@@ -798,15 +692,10 @@ static int blspec_bootentry_generate(struct bootentries 
*bootentries,
        if (ret > 0)
                found += ret;
 
-       if (*name == '/' || !strncmp(name, "nfs://", 6)) {
-               char *nfspath = parse_nfs_url(name);
-
-               if (nfspath)
-                       name = nfspath;
-
+       if (*name == '/') {
                ret = stat(name, &s);
                if (ret)
-                       goto out;
+                       return found;
 
                if (S_ISDIR(s.st_mode))
                        ret = blspec_scan_directory(bootentries, name);
@@ -814,9 +703,6 @@ static int blspec_bootentry_generate(struct bootentries 
*bootentries,
                        ret = blspec_scan_file(bootentries, NULL, name);
                if (ret > 0)
                        found += ret;
-
-out:
-               free(nfspath);
        }
 
        return found;
diff --git a/common/boot.c b/common/boot.c
index e3bdfec581c1..bfb2eb13f67f 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -13,6 +13,9 @@
 #include <init.h>
 #include <menu.h>
 #include <unistd.h>
+#include <libfile.h>
+#include <net.h>
+#include <fs.h>
 
 #include <linux/stat.h>
 
@@ -273,6 +276,112 @@ int bootentry_register_provider(struct bootentry_provider 
*p)
        return 0;
 }
 
+/*
+ * nfs_find_mountpath - Check if a given url is already mounted
+ */
+static const char *nfs_find_mountpath(const char *nfshostpath)
+{
+       struct fs_device *fsdev;
+
+       for_each_fs_device(fsdev) {
+               if (fsdev->backingstore && !strcmp(fsdev->backingstore, 
nfshostpath))
+                       return fsdev->path;
+       }
+
+       return NULL;
+}
+
+/*
+ * parse_nfs_url - check for nfs:// style url
+ *
+ * Check if the passed string is a NFS url and if yes, mount the
+ * NFS and return the path we have mounted to.
+ */
+static char *parse_nfs_url(const char *url)
+{
+       char *sep, *str, *host, *port, *path;
+       char *mountpath = NULL, *hostpath = NULL, *options = NULL;
+       const char *prevpath;
+       IPaddr_t ip;
+       int ret;
+
+       if (!IS_ENABLED(CONFIG_FS_NFS))
+               return NULL;
+
+       if (strncmp(url, "nfs://", 6))
+               return NULL;
+
+       url += 6;
+
+       str = xstrdup(url);
+
+       host = str;
+
+       sep = strchr(str, '/');
+       if (!sep) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       *sep++ = 0;
+
+       path = sep;
+
+       port = strchr(host, ':');
+       if (port)
+               *port++ = 0;
+
+       ret = ifup_all(0);
+       if (ret) {
+               pr_err("Failed to bring up networking\n");
+               goto out;
+       }
+
+       ret = resolv(host, &ip);
+       if (ret) {
+               pr_err("Cannot resolve \"%s\": %s\n", host, strerror(-ret));
+               goto out;
+       }
+
+       hostpath = basprintf("%pI4:%s", &ip, path);
+
+       prevpath = nfs_find_mountpath(hostpath);
+
+       if (prevpath) {
+               mountpath = xstrdup(prevpath);
+       } else {
+               mountpath = basprintf("/mnt/nfs-%s-bootentries-%08x", host,
+                                       rand());
+               if (port)
+                       options = basprintf("mountport=%s,port=%s", port,
+                                             port);
+
+               ret = make_directory(mountpath);
+               if (ret)
+                       goto out;
+
+               pr_debug("host: %s port: %s path: %s\n", host, port, path);
+               pr_debug("hostpath: %s mountpath: %s options: %s\n", hostpath, 
mountpath, options);
+
+               ret = mount(hostpath, "nfs", mountpath, options);
+               if (ret)
+                       goto out;
+       }
+
+       ret = 0;
+
+out:
+       free(str);
+       free(hostpath);
+       free(options);
+
+       if (ret)
+               free(mountpath);
+
+       return ret ? NULL : mountpath;
+}
+
+
 /*
  * bootentry_create_from_name - create boot entries from a name
  *
@@ -293,6 +402,11 @@ int bootentry_create_from_name(struct bootentries 
*bootentries,
 {
        struct bootentry_provider *p;
        int found = 0, ret;
+       char *nfspath;
+
+       nfspath = parse_nfs_url(name);
+       if (nfspath)
+               name = nfspath;
 
        list_for_each_entry(p, &bootentry_providers, list) {
                ret = p->generate(bootentries, name);
@@ -300,6 +414,8 @@ int bootentry_create_from_name(struct bootentries 
*bootentries,
                        found += ret;
        }
 
+       free(nfspath);
+
        if (IS_ENABLED(CONFIG_COMMAND_SUPPORT) && !found) {
                const char *path;
 
-- 
2.39.5


Reply via email to