ldlinux: PATH-based module lookup
authorMatt Fleming <matt.fleming@linux.intel.com>
Mon, 6 Jun 2011 10:09:42 +0000 (11:09 +0100)
committerMatt Fleming <matt.fleming@linux.intel.com>
Mon, 6 Jun 2011 10:09:42 +0000 (11:09 +0100)
Add support for specifying directories to search when loading
modules. A new config directive, "PATH", instructs the module loading
code to search the directories listed in a colon-separated list when
loading a module.

Signed-off-by: Matt Fleming <matt.fleming@linux.intel.com>
com32/elflink/ldlinux/readconfig.c
com32/lib/sys/module/common.c
core/fs/fs.c
core/include/fs.h
mk/lib.mk

index 8ded92a..067b370 100644 (file)
@@ -23,6 +23,7 @@
 #include <syslinux/adv.h>
 #include <syslinux/config.h>
 #include <dprintf.h>
+#include <fs.h>
 
 #include "menu.h"
 #include "config.h"
@@ -1132,6 +1133,23 @@ do_include:
 
        } else if (looking_at(p, "say")) {
                printf("%s\n", p + 4);
+       } else if (looking_at(p, "path")) {
+               /* PATH-based lookup */
+               char *new_path, *_p;
+               size_t len, new_len;
+
+               new_path = refstrdup(skipspace(p + 4));
+               len = strlen(PATH);
+               new_len = strlen(new_path);
+               _p = realloc(PATH, len + new_len + 2);
+               if (_p) {
+                       strncpy(_p, PATH, len);
+                       _p[len++] = ':';
+                       strncpy(_p + len, new_path, new_len);
+                       _p[len + new_len] = '\0';
+                       PATH = _p;
+               } else
+                       printf("Failed to realloc PATH\n");
        }
     }
 }
index 431f5cd..1afdbf5 100644 (file)
@@ -8,6 +8,7 @@
 #include <stdio.h>
 #include <elf.h>
 #include <string.h>
+#include <fs.h>
 
 #include <linux/list.h>
 #include <sys/module.h>
@@ -56,13 +57,45 @@ void print_elf_symbols(struct elf_module *module) {
 }
 #endif //ELF_DEBUG
 
+static FILE *findpath(char *name)
+{
+       char path[FILENAME_MAX];
+       FILE *f;
+       char *p, *n;
+       int i;
+
+       p = PATH;
+again:
+       i = 0;
+       while (*p && *p != ':' && i < FILENAME_MAX) {
+               path[i++] = *p++;
+       }
+
+       if (*p == ':')
+               p++;
+
+       n = name;
+       while (*n && i < FILENAME_MAX)
+               path[i++] = *n++;
+       path[i] = '\0';
+
+       f = fopen(path, "rb");
+       if (f)
+               return f;
+
+       if (p >= PATH && p < PATH + strlen(PATH))
+               goto again;
+
+       return NULL;
+}
+
 /*
  * Image files manipulation routines
  */
 
 int image_load(struct elf_module *module)
 {
-       module->u.l._file = fopen(module->name, "rb");
+       module->u.l._file = findpath(module->name);
 
        if (module->u.l._file == NULL) {
                DBG_PRINT("Could not open object file '%s'\n", module->name);
index 39ba09e..c743dca 100644 (file)
@@ -6,6 +6,8 @@
 #include "fs.h"
 #include "cache.h"
 
+char *PATH = "/:/bin/";
+
 /* The currently mounted filesystem */
 struct fs_info *this_fs = NULL;                /* Root filesystem */
 
index e9e4548..04451ed 100644 (file)
@@ -178,6 +178,8 @@ static inline struct file *handle_to_file(uint16_t handle)
     return handle ? &files[handle-1] : NULL;
 }
 
+extern char *PATH;
+
 /* fs.c */
 void pm_mangle_name(com32sys_t *);
 void pm_searchdir(com32sys_t *);
index 1792ea1..604b91a 100644 (file)
--- a/mk/lib.mk
+++ b/mk/lib.mk
@@ -32,7 +32,8 @@ LIBFLAGS = -DDYNAMIC_CRC_TABLE -DPNG_NO_CONSOLE_IO \
 # LIBFLAGS += -DPNG_NO_FLOATING_POINT_SUPPORTED
 
 REQFLAGS  = $(GCCOPT) -g -mregparm=3 -DREGPARM=3 -D__COM32__ \
-           -nostdinc -iwithprefix include -I. -I./sys -I../include
+           -nostdinc -iwithprefix include -I. -I./sys -I../include \
+           -I../../core/include
 OPTFLAGS  = -Os -march=i386 -falign-functions=0 -falign-jumps=0 \
            -falign-labels=0 -ffast-math -fomit-frame-pointer
 WARNFLAGS = $(GCCWARN) -Wpointer-arith -Wwrite-strings -Wstrict-prototypes -Winline