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

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