1 /* tag: plugin interface for openbios forth kernel
3 * Copyright (C) 2003, 2004 Stefan Reinauer
5 * See the file "COPYING" for further information about
6 * the copyright and warranty status of this work.
9 #include "sysinclude.h"
15 #include "unix/plugins.h"
17 unsigned char *plugindir = "/usr/share/OpenBIOS/plugins";
18 #define PLUGINDIR plugindir
21 #define CONFIG_DEBUG_PLUGINS
23 typedef struct iorange iorange_t;
32 static iorange_t *ioranges = NULL;
34 typedef struct plugin plugin_t;
40 static plugin_t *plugins = NULL;
42 io_ops_t *find_iorange(u32 reg)
44 iorange_t *range = ioranges;
46 if (range->start <= reg && range->end >= reg)
53 int register_iorange(const char *name, io_ops_t * ops, unsigned int rstart,
58 /* intersection check */
62 /* new section swallows old section */
63 if (newrange->start >= rstart && newrange->end <= rend)
65 /* new section start or end point are within range */
66 if (newrange->start <= rstart && newrange->end >= rstart)
68 if (newrange->start <= rend && newrange->end >= rend)
71 printf("Error: overlapping IO regions: %s and %s\n",
72 newrange->name, name);
75 newrange = newrange->next;
78 newrange = malloc(sizeof(iorange_t));
80 newrange->name = name;
82 newrange->start = rstart;
84 newrange->next = ioranges;
91 int is_loaded(const char *plugin_name)
93 plugin_t *p = plugins;
95 if (!strcmp(plugin_name, p->name))
102 int load_plugin(const char *plugin_name)
108 int (*init_plugin) (void);
113 if (is_loaded(plugin_name)) {
114 printf("Plugin %s already loaded.\n", plugin_name);
118 strncpy(path, PLUGINDIR, PATHSIZE);
119 strncat(path, "/plugin_", PATHSIZE);
120 strncat(path, plugin_name, PATHSIZE);
121 strncat(path, ".so", PATHSIZE);
124 printf("Opening plugin %s\n", path);
127 handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
130 printf("Error: Could not open plugin \"%s\": %s\n",
134 #ifdef CONFIG_DEBUG_PLUGINS
135 plugin_info = dlsym(handle, "plugin_author");
136 if ((error = dlerror()) == NULL)
137 printf("Plugin %s author: %s\n", plugin_name, *plugin_info);
138 plugin_info = dlsym(handle, "plugin_license");
139 if ((error = dlerror()) == NULL)
140 printf("Plugin %s license: %s\n", plugin_name, *plugin_info);
141 plugin_info = dlsym(handle, "plugin_description");
142 if ((error = dlerror()) == NULL)
143 printf("Plugin %s descr.: %s\n", plugin_name, *plugin_info);
145 p = malloc(sizeof(plugin_t));
147 p->name = plugin_name;
150 deps = dlsym(handle, "plugin_deps");
151 if ((error = dlerror()) != NULL)
155 strncpy(path, "plugin_", PATHSIZE);
156 strncat(path, plugin_name, PATHSIZE);
157 strncat(path, "_init", PATHSIZE);
159 init_plugin = dlsym(handle, path);
160 if ((error = dlerror()) != NULL) {
161 printf("error: %s\n", error);
167 char *walk = deps[0];
168 #ifdef CONFIG_DEBUG_PLUGINS
169 printf("\nPlugin %s dependencies:", plugin_name);
173 if (!is_loaded(walk)) {
174 #ifdef CONFIG_DEBUG_PLUGINS
175 printf("(loading)\n");
179 #ifdef CONFIG_DEBUG_PLUGINS
190 printf("Initializing module:\n");
193 return init_plugin();
195 // We don't dlclose the handle here since
196 // we want to keep our symbols for later use.