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

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