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

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