Removed debug
[tpg/acess2.git] / Kernel / modules.c
1 /*
2  * Acess2
3  * - Module Loader
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <modules.h>
8
9 #define USE_EDI 0
10 #define USE_UDI 0
11
12 // === PROTOTYPES ===
13  int    Module_int_Initialise(tModule *Module, char *ArgString);
14 void    Modules_LoadBuiltins(void);
15 void    Modules_SetBuiltinParams(char *Name, char *ArgString);
16  int    Module_RegisterLoader(tModuleLoader *Loader);
17  int    Module_LoadMem(void *Buffer, Uint Length, char *ArgString);
18  int    Module_LoadFile(char *Path, char *ArgString);
19  int    Module_int_ResolveDeps(tModule *Info);
20  int    Module_IsLoaded(char *Name);
21
22 // === EXPORTS ===
23 EXPORT(Module_RegisterLoader);
24
25 // === IMPORTS ===
26 #if USE_UDI
27 extern int      UDI_LoadDriver(void *Base);
28 #endif
29 extern void     StartupPrint(char *Str);
30 extern void     gKernelModules;
31 extern void     gKernelModulesEnd;
32
33 // === GLOBALS ===
34  int    giNumBuiltinModules = 0;
35 tSpinlock       glModuleSpinlock;
36 tModule *gLoadedModules = NULL;
37 tModuleLoader   *gModule_Loaders = NULL;
38 tModule *gLoadingModules = NULL;
39 tModule **gapBuiltinModules = NULL;
40 char    **gasBuiltinModuleArgs;
41
42 // === CODE ===
43 /**
44  * \brief Initialises a module
45  * \param Module        Pointer to the module header
46  * \return Zero on success, eModuleErrors or -1 on error
47  * \retval -1   Returned if a dependency fails, or a circular dependency
48  *              exists.
49  * \retval 0    Returned on success
50  * \retval >0   Error code form the module's initialisation function
51  */
52 int Module_int_Initialise(tModule *Module, char *ArgString)
53 {
54          int    i, j;
55          int    ret;
56         char    **deps;
57         char    **args;
58         tModule *mod;
59         
60         ENTER("pModule", Module);
61         LOG("Module->Magic = 0x%x", Module->Magic);
62         if(Module->Magic != MODULE_MAGIC) {
63                 Log_Warning(
64                         "Module",
65                         "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)",
66                         Module, Module->Magic, MODULE_MAGIC
67                         );
68                 LEAVE('i', MODULE_ERR_BADMODULE);
69                 return MODULE_ERR_BADMODULE;
70         }
71         LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name);
72         
73         if(Module->Arch != MODULE_ARCH_ID) {
74                 Log_Warning(
75                         "Module",
76                         "Module %p (%s) is for another architecture (%i)",
77                         Module, Module->Name, Module->Arch
78                         );
79         }
80         
81         deps = Module->Dependencies;
82         
83         // Check if the module has been loaded
84         for( mod = gLoadedModules; mod; mod = mod->Next )
85         {
86                 if(mod == Module)       LEAVE_RET('i', 0);
87         }
88         
89         // Add to the "loading" (prevents circular deps)
90         Module->Next = gLoadingModules;
91         gLoadingModules = Module;
92         
93         // Scan dependency list
94         for( j = 0; deps && deps[j]; j++ )
95         {
96                 // Check if the module is already loaded
97                 for( mod = gLoadedModules; mod; mod = mod->Next )
98                 {
99                         if(strcmp(deps[j], mod->Name) == 0)
100                                 break;
101                 }
102                 if( mod )       continue;       // Dependency is loaded, check the rest
103                 
104                 // Ok, check if it's loading
105                 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
106                 {
107                         if(strcmp(deps[j], mod->Name) == 0)
108                                 break;
109                 }
110                 if( mod ) {
111                         Log_Warning("Module", "Circular dependency detected (%s and %s)",
112                                 mod->Name, Module->Name);
113                         LEAVE_RET('i', -1);
114                 }
115                 
116                 // So, if it's not loaded, we better load it then
117                 for( i = 0; i < giNumBuiltinModules; i ++ )
118                 {
119                         if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 )
120                                 break;
121                 }
122                 if( i == giNumBuiltinModules ) {
123                         Log_Warning("Module", "Dependency '%s' for module '%s' failed",
124                                 deps[j], Module->Name);
125                         return -1;
126                 }
127                 
128                 // Dependency is not loaded, so load it
129                 ret = Module_int_Initialise(
130                         gapBuiltinModules[i],
131                         gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
132                         );
133                 if( ret )
134                 {
135                         // The only "ok" error is NOTNEEDED
136                         if(ret != MODULE_ERR_NOTNEEDED)
137                                 LEAVE_RET('i', -1);
138                 }
139         }
140         
141         // All Dependencies OK? Initialise
142         StartupPrint(Module->Name);
143         Log_Log("Module", "Initialising %p '%s' v%i.%i...",
144                 Module, Module->Name,
145                 Module->Version >> 8, Module->Version & 0xFF
146                 );
147         
148         if( ArgString )
149                 args = str_split( ArgString, ',' );
150         else
151                 args = NULL;
152         
153         ret = Module->Init(args);
154         
155         if(args)        free(args);
156         
157         // Remove from loading list
158         gLoadingModules = gLoadingModules->Next;
159         
160         if( ret != MODULE_ERR_OK ) {
161                 switch(ret)
162                 {
163                 case MODULE_ERR_MISC:
164                         Log_Warning("Module", "Unable to load, reason: Miscelanious");
165                         break;
166                 case MODULE_ERR_NOTNEEDED:
167                         Log_Warning("Module", "Unable to load, reason: Module not needed");
168                         break;
169                 case MODULE_ERR_MALLOC:
170                         Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
171                         break;
172                 default:
173                         Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
174                         break;
175                 }
176                 LEAVE_RET('i', ret);
177                 return ret;
178         }
179         LOG("ret = %i", ret);
180         
181         // Add to loaded list
182         LOCK( &glModuleSpinlock );
183         Module->Next = gLoadedModules;
184         gLoadedModules = Module;
185         RELEASE( &glModuleSpinlock );
186         
187         LEAVE_RET('i', 0);
188 }
189
190 /**
191  * \brief Scans the builtin modules and creates an array of them
192  */
193 void Modules_int_GetBuiltinArray(void)
194 {
195          int    i;
196         tModule *module;
197         
198         // Count
199         module = &gKernelModules;
200         i = 0;
201         while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
202         {
203                 if(module->Magic == MODULE_MAGIC) {
204                         i ++;
205                         module ++;
206                 }
207                 else
208                         module = (void*)( (tVAddr)module + 4 );
209         }
210         
211         // Create
212         giNumBuiltinModules = i;
213         gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
214         gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) );
215         
216         
217         // Fill
218         module = &gKernelModules;
219         i = 0;
220         while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
221         {
222                 if(module->Magic == MODULE_MAGIC) {
223                         gapBuiltinModules[i] = module;
224                         i ++;
225                         module ++;
226                 }
227                 else
228                         module = (void*)( (tVAddr)module + 4 );
229         }
230 }
231
232 /**
233  * \brief Initialises builtin modules
234  */
235 void Modules_LoadBuiltins()
236 {
237          int    i;
238         
239         if( !gapBuiltinModules )
240                 Modules_int_GetBuiltinArray();
241         
242         for( i = 0; i < giNumBuiltinModules; i++ )
243         {
244                 Module_int_Initialise(
245                         gapBuiltinModules[i],
246                         (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
247                         );
248         }
249         
250         if( gasBuiltinModuleArgs != NULL )
251                 free(gasBuiltinModuleArgs);
252 }
253
254 /**
255  * \brief Sets the parameters for a builtin module
256  */
257 void Modules_SetBuiltinParams(char *Name, char *ArgString)
258 {
259          int    i;
260         
261         if( gasBuiltinModuleArgs == NULL )
262         {
263                 Modules_int_GetBuiltinArray();
264         }
265         
266         // I hate expensive scans
267         for( i = 0; i < giNumBuiltinModules; i++ )
268         {
269                 if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
270                         gasBuiltinModuleArgs[i] = ArgString;
271                         return ;
272                 }
273         }
274         
275         Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
276 }
277
278 /**
279  * \brief Registers a tModuleLoader with the kernel
280  * \param Loader        Pointer to loader structure (must be persistent)
281  */
282 int Module_RegisterLoader(tModuleLoader *Loader)
283 {
284         if(!Loader)     return 1;
285         
286         Loader->Next = gModule_Loaders;
287         gModule_Loaders = Loader;
288         
289         return 0;
290 }
291
292 /**
293  * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
294  * \brief Load a module from a memory location
295  */
296 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
297 {
298         char    path[VFS_MEMPATH_SIZE];
299         
300         VFS_GetMemPath(path, Buffer, Length);
301         
302         return Module_LoadFile( path, ArgString );
303 }
304
305 /**
306  * \fn int Module_LoadFile(char *Path, char *ArgString)
307  * \brief Load a module from a file
308  */
309 int Module_LoadFile(char *Path, char *ArgString)
310 {
311         void    *base;
312         tModule *info;
313         
314         // Load Binary
315         base = Binary_LoadKernel(Path);
316         
317         // Error check
318         if(base == NULL) {
319                 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
320                 return 0;
321         }
322         
323         // Check for Acess Driver
324         if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
325         {
326                 tModuleLoader   *tmp;
327                 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
328                 {
329                         if( tmp->Detector(base) == 0 )  continue;
330                         
331                         return tmp->Loader(base);
332                 }
333                 
334                 #if USE_EDI
335                 // Check for EDI Driver
336                 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
337                 {
338                         return Module_InitEDI( base );  // And intialise
339                 }
340                 #endif
341                 
342                 // Unknown module type?, return error
343                 Binary_Unload(base);
344                 #if USE_EDI
345                 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
346                 #else
347                 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
348                 #endif
349                 return 0;
350         }
351         
352         // Initialise (and register)
353         if( Module_int_Initialise( info, ArgString ) )
354         {
355                 Binary_Unload(base);
356                 return 0;
357         }
358         
359         return 1;
360 }
361
362 /**
363  * \fn int Module_int_ResolveDeps(tModule *Info)
364  * \brief Resolves the dependencies
365  * \todo Implement
366  * \note Currently does not resolve the dependencies, just checks them
367  */
368 int Module_int_ResolveDeps(tModule *Info)
369 {
370         char    **names = Info->Dependencies;
371         
372         // Walk dependencies array
373         for( ; *names; names++ )
374         {
375                 // Check if the module is loaded
376                 if( !Module_IsLoaded(*names) ) {
377                         Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
378                         return 0;
379                 }
380         }
381         return 1;
382 }
383
384 /**
385  * \fn int Module_IsLoaded(char *Name)
386  * \brief Checks if a module is loaded
387  * \param Name  Name of module to find
388  */
389 int Module_IsLoaded(char *Name)
390 {
391         tModule *mod = gLoadedModules;
392         
393         // Scan loaded list
394         for( ; mod; mod = mod->Next )
395         {
396                 // If found, return true
397                 if(strcmp(mod->Name, Name) == 0)
398                         return 1;
399         }
400         // not found - return false
401         return 0;
402 }

UCC git Repository :: git.ucc.asn.au