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

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