Fixing up doxygen comments
[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(const 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 tShortSpinlock  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         SHORTLOCK( &glModuleSpinlock );
184         Module->Next = gLoadedModules;
185         gLoadedModules = Module;
186         SHORTREL( &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 Initialise a builtin module given it's name
257  * 
258  * E.g. Used by VTerm to load an alternate video driver at runtime
259  */
260 int Modules_InitialiseBuiltin(const char *Name)
261 {
262          int    i;
263         
264         // Check if it's loaded
265         if( Module_IsLoaded(Name) )
266                 return 0;
267         
268         if( !gapBuiltinModules )
269                 Modules_int_GetBuiltinArray();
270         
271         for( i = 0; i < giNumBuiltinModules; i++ )
272         {
273                 if( strcmp(gapBuiltinModules[i]->Name, Name) == 0 ) {
274                         return Module_int_Initialise(gapBuiltinModules[i],
275                                 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
276                                 );
277                 }
278         }
279         return -1;
280 }
281
282 /**
283  * \brief Sets the parameters for a builtin module
284  */
285 void Modules_SetBuiltinParams(char *Name, char *ArgString)
286 {
287          int    i;
288         
289         if( gasBuiltinModuleArgs == NULL )
290         {
291                 Modules_int_GetBuiltinArray();
292         }
293         
294         // I hate expensive scans
295         for( i = 0; i < giNumBuiltinModules; i++ )
296         {
297                 if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
298                         gasBuiltinModuleArgs[i] = ArgString;
299                         return ;
300                 }
301         }
302         
303         Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
304 }
305
306 /**
307  * \brief Registers a tModuleLoader with the kernel
308  * \param Loader        Pointer to loader structure (must be persistent)
309  */
310 int Module_RegisterLoader(tModuleLoader *Loader)
311 {
312         if(!Loader)     return 1;
313         
314         Loader->Next = gModule_Loaders;
315         gModule_Loaders = Loader;
316         
317         return 0;
318 }
319
320 /**
321  * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
322  * \brief Load a module from a memory location
323  */
324 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
325 {
326         char    path[VFS_MEMPATH_SIZE];
327         
328         VFS_GetMemPath(path, Buffer, Length);
329         
330         return Module_LoadFile( path, ArgString );
331 }
332
333 /**
334  * \fn int Module_LoadFile(char *Path, char *ArgString)
335  * \brief Load a module from a file
336  */
337 int Module_LoadFile(char *Path, char *ArgString)
338 {
339         void    *base;
340         tModule *info;
341         
342         // Load Binary
343         base = Binary_LoadKernel(Path);
344         
345         // Error check
346         if(base == NULL) {
347                 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
348                 return 0;
349         }
350         
351         // Check for Acess Driver
352         if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
353         {
354                 tModuleLoader   *tmp;
355                 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
356                 {
357                         if( tmp->Detector(base) == 0 )  continue;
358                         
359                         return tmp->Loader(base);
360                 }
361                 
362                 #if USE_EDI
363                 // Check for EDI Driver
364                 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
365                 {
366                         return Module_InitEDI( base );  // And intialise
367                 }
368                 #endif
369                 
370                 // Unknown module type?, return error
371                 Binary_Unload(base);
372                 #if USE_EDI
373                 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
374                 #else
375                 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
376                 #endif
377                 return 0;
378         }
379         
380         // Initialise (and register)
381         if( Module_int_Initialise( info, ArgString ) )
382         {
383                 Binary_Unload(base);
384                 return 0;
385         }
386         
387         return 1;
388 }
389
390 /**
391  * \fn int Module_int_ResolveDeps(tModule *Info)
392  * \brief Resolves the dependencies
393  * \todo Implement
394  * \note Currently does not resolve the dependencies, just checks them
395  */
396 int Module_int_ResolveDeps(tModule *Info)
397 {
398         char    **names = Info->Dependencies;
399         
400         // Walk dependencies array
401         for( ; *names; names++ )
402         {
403                 // Check if the module is loaded
404                 if( !Module_IsLoaded(*names) ) {
405                         Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
406                         return 0;
407                 }
408         }
409         return 1;
410 }
411
412 /**
413  * \fn int Module_IsLoaded(const char *Name)
414  * \brief Checks if a module is loaded
415  * \param Name  Name of module to find
416  */
417 int Module_IsLoaded(const char *Name)
418 {
419         tModule *mod = gLoadedModules;
420         
421         // Scan loaded list
422         for( ; mod; mod = mod->Next )
423         {
424                 // If found, return true
425                 if(strcmp(mod->Name, Name) == 0)
426                         return 1;
427         }
428         // not found - return false
429         return 0;
430 }

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