Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / core / module.c
blob5fd75167acbc9c9433dddf20d7ad0debf0358c44
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <system.h>
22 #include <string.h>
23 #include <module.h>
24 #include <file.h>
25 #include <task.h>
26 #include <fd.h>
28 module_t module_list;
30 void module_display ()
32 printf ("Module\t\tSize\n");
33 module_t *kmod;
34 for (kmod = module_list.next; kmod != &module_list; kmod = kmod->next)
35 if (kmod->name)
36 printf ("%s\t\t%u\n", kmod->name, kmod->mem_usage/1024);
37 else
38 printf ("?\t\t%u\n", kmod->mem_usage/1024);
41 module_t *module_find (task_t *task)
43 module_t *kmod;
44 for (kmod = module_list.next; kmod != &module_list; kmod = kmod->next) {
45 if (kmod->task == task)
46 return kmod;
49 return 0;
52 module_t *module_load (char *modname)
54 int f = open (modname, O_RDONLY);
56 if (!f)
57 return 0;
59 fd_t *fd = fd_get (f);
61 if (!fd)
62 return 0;
64 module_t *kmod = (module_t *) kmalloc (sizeof (module_t));
66 if (!kmod)
67 return 0;
69 kmod->mem_usage = fd->e + sizeof (module_t) + 1;
71 kmod->image = (char *) kmalloc (sizeof (char) * fd->e + 1);
73 if (!kmod->image)
74 return 0;
76 read (f, kmod->image, fd->e);
79 unsigned entry;
80 unsigned data, data_off, bss;
81 int err = exec_elf (kmod->image, &entry, &data, &data_off, &bss);
83 if (err != NULL) {
84 printf ("ERROR -> invalid module %s\n", modname);
85 kfree (kmod->image);
86 kfree (kmod);
87 close (f);
88 return 0;
91 task_t *task = (task_t *) task_create (modname, entry, KMOD_MODULE_PRIORITY);
93 if (!task) {
94 printf ("ERROR -> invalid module task\n");
95 kfree (kmod->image);
96 kfree (kmod);
97 close (f);
98 return 0;
101 kmod->task = task;
103 unsigned l = strlen (modname);
104 kmod->name = (char *) kmalloc (sizeof (char) * l + 1);
106 if (!kmod->name) {
107 task_done (task);
108 kfree (kmod->image);
109 kfree (kmod);
110 close (f);
111 return 0;
114 memcpy (kmod->name, modname, l);
115 kmod->name[l] = '\0';
117 /* add into list */
118 kmod->next = &module_list;
119 kmod->prev = module_list.prev;
120 kmod->prev->next = kmod;
121 kmod->next->prev = kmod;
123 return kmod;
126 unsigned int init_module (void)
128 module_list.next = &module_list;
129 module_list.prev = &module_list;
131 return 1;