summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/arch/unix/plugins.c
blob: 855454b0eaf8eb9880e72f27fc3c3b0912df387e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* tag: plugin interface for openbios forth kernel
 *
 * Copyright (C) 2003, 2004 Stefan Reinauer
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */

#include "sysinclude.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#include "unix/plugins.h"

unsigned char *plugindir = "/usr/share/OpenBIOS/plugins";
#define PLUGINDIR  plugindir
#define PATHSIZE   256

#define CONFIG_DEBUG_PLUGINS

typedef struct iorange iorange_t;
struct iorange {
	const char *name;
	unsigned int start;
	unsigned int end;
	io_ops_t *ops;
	iorange_t *next;
};

static iorange_t *ioranges = NULL;

typedef struct plugin plugin_t;
struct plugin {
	const char *name;
	plugin_t *next;
};

static plugin_t *plugins = NULL;

io_ops_t *find_iorange(u32 reg)
{
	iorange_t *range = ioranges;
	while (range) {
		if (range->start <= reg && range->end >= reg)
			return range->ops;
		range = range->next;
	}
	return NULL;
}

int register_iorange(const char *name, io_ops_t * ops, unsigned int rstart,
		     unsigned int rend)
{
	iorange_t *newrange;

	/* intersection check */
	newrange = ioranges;
	while (newrange) {
		int fail = 0;
		/* new section swallows old section */
		if (newrange->start >= rstart && newrange->end <= rend)
			fail = -1;
		/* new section start or end point are within range */
		if (newrange->start <= rstart && newrange->end >= rstart)
			fail = -1;
		if (newrange->start <= rend && newrange->end >= rend)
			fail = -1;
		if (fail) {
			printf("Error: overlapping IO regions: %s and %s\n",
				newrange->name, name);
			return -1;
		}
		newrange = newrange->next;
	}

	newrange = malloc(sizeof(iorange_t));

	newrange->name = name;
	newrange->ops = ops;
	newrange->start = rstart;
	newrange->end = rend;
	newrange->next = ioranges;

	ioranges = newrange;

	return 0;
}

int is_loaded(const char *plugin_name)
{
	plugin_t *p = plugins;
	while (p) {
		if (!strcmp(plugin_name, p->name))
			return -1;
		p = p->next;
	}
	return 0;
}

int load_plugin(const char *plugin_name)
{
	void *handle;
	char *error;
	char path[PATHSIZE];

	int (*init_plugin) (void);
	char **deps;
	char **plugin_info;
	plugin_t *p;

	if (is_loaded(plugin_name)) {
		printf("Plugin %s already loaded.\n", plugin_name);
		return 0;
	}

	strncpy(path, PLUGINDIR, PATHSIZE);
	strncat(path, "/plugin_", PATHSIZE);
	strncat(path, plugin_name, PATHSIZE);
	strncat(path, ".so", PATHSIZE);

#if DEBUG
	printf("Opening plugin %s\n", path);
#endif

	handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
	if (!handle) {
		error = dlerror();
		printf("Error: Could not open plugin \"%s\": %s\n",
		       plugin_name, error);
		exit(1);
	}
#ifdef CONFIG_DEBUG_PLUGINS
	plugin_info = dlsym(handle, "plugin_author");
	if ((error = dlerror()) == NULL)
		printf("Plugin %s author:  %s\n", plugin_name, *plugin_info);
	plugin_info = dlsym(handle, "plugin_license");
	if ((error = dlerror()) == NULL)
		printf("Plugin %s license: %s\n", plugin_name, *plugin_info);
	plugin_info = dlsym(handle, "plugin_description");
	if ((error = dlerror()) == NULL)
		printf("Plugin %s descr.: %s\n", plugin_name, *plugin_info);
#endif
	p = malloc(sizeof(plugin_t));
	p->next = plugins;
	p->name = plugin_name;
	plugins = p;

	deps = dlsym(handle, "plugin_deps");
	if ((error = dlerror()) != NULL)
		deps = NULL;


	strncpy(path, "plugin_", PATHSIZE);
	strncat(path, plugin_name, PATHSIZE);
	strncat(path, "_init", PATHSIZE);

	init_plugin = dlsym(handle, path);
	if ((error = dlerror()) != NULL) {
		printf("error: %s\n", error);
		exit(1);
	}

	if (deps) {
		int i = 0;
		char *walk = deps[0];
#ifdef CONFIG_DEBUG_PLUGINS
		printf("\nPlugin %s dependencies:", plugin_name);
#endif
		while (walk) {
			printf(" %s", walk);
			if (!is_loaded(walk)) {
#ifdef CONFIG_DEBUG_PLUGINS
				printf("(loading)\n");
#endif
				load_plugin(walk);
			}
#ifdef CONFIG_DEBUG_PLUGINS
			else {
				printf("(loaded)");
			}
#endif
			walk = deps[++i];
		}
	}

	printf("\n");
#if DEBUG
	printf("Initializing module:\n");
#endif

	return init_plugin();

	// We don't dlclose the handle here since
	// we want to keep our symbols for later use.
}