Direct support for serial mouse in kernel - it is recognized as /dev/mousecom; added...
[ZeXOS.git] / kernel / core / module.c
blob76a27ef999ee7ef4cfd43de1f0a7c58635cd4038
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <module.h>
23 #include <file.h>
24 #include <task.h>
26 module_t module_list;
28 void module_display ()
30 printf ("Module\t\tSize\n");
31 module_t *kmod;
32 for (kmod = module_list.next; kmod != &module_list; kmod = kmod->next)
33 if (kmod->name)
34 printf ("%s\t\t%u\n", kmod->name, kmod->mem_usage/1024);
35 else
36 printf ("?\t\t%u\n", kmod->mem_usage/1024);
39 module_t *module_find (task_t *task)
41 module_t *kmod;
42 for (kmod = module_list.next; kmod != &module_list; kmod = kmod->next) {
43 if (kmod->task == task)
44 return kmod;
47 return 0;
50 module_t *module_load (char *modname)
52 FILE *f = fopen (modname, "r");
54 if (!f)
55 return 0;
57 module_t *kmod = (module_t *) kmalloc (sizeof (module_t));
59 if (!kmod)
60 return 0;
62 kmod->mem_usage = f->e + sizeof (module_t) +1;
64 kmod->image = (char *) kmalloc (sizeof (char) * f->e + 1);
66 if (!kmod->image)
67 return 0;
69 fgets (kmod->image, f->e, f);
72 unsigned entry;
73 unsigned data, data_off, bss;
74 int err = exec_elf (kmod->image, &entry, &data, &data_off, &bss);
76 if (err != NULL) {
77 printf ("ERROR -> invalid module %s\n", modname);
78 return 0;
81 task_t *task = (task_t *) task_create (modname, entry, KMOD_MODULE_PRIORITY);
83 if (!task) {
84 printf ("ERROR -> invalid module task\n");
85 return 0;
88 kmod->task = task;
90 unsigned l = strlen (modname);
91 kmod->name = (char *) kmalloc (sizeof (char) * l + 1);
93 if (kmod->name) {
94 memcpy (kmod->name, modname, l);
95 kmod->name[l] = '\0';
98 /* add into list */
99 kmod->next = &module_list;
100 kmod->prev = module_list.prev;
101 kmod->prev->next = kmod;
102 kmod->next->prev = kmod;
104 //fclose (f); // FIXME: uninitialized pointer of FILE *f in free () ??
106 return kmod;
109 unsigned int init_module (void)
111 module_list.next = &module_list;
112 module_list.prev = &module_list;
114 return 1;