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);
26 EXPORT(Module_RegisterLoader);
30 extern int UDI_LoadDriver(void *Base);
32 extern void StartupPrint(const char *Str);
33 extern tModule gKernelModules;
34 extern tModule gKernelModulesEnd;
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;
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
53 * \retval 0 Returned on success
54 * \retval >0 Error code form the module's initialisation function
56 int Module_int_Initialise(tModule *Module, const char *ArgString)
64 ENTER("pModule", Module);
65 LOG("Module->Magic = 0x%x", Module->Magic);
66 if(Module->Magic != MODULE_MAGIC) {
69 "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)",
70 Module, Module->Magic, MODULE_MAGIC
72 LEAVE('i', MODULE_ERR_BADMODULE);
73 return MODULE_ERR_BADMODULE;
75 LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name);
77 if(Module->Arch != MODULE_ARCH_ID) {
80 "Module %p (%s) is for another architecture (%i)",
81 Module, Module->Name, Module->Arch
85 deps = Module->Dependencies;
87 // Check if the module has been loaded
88 for( mod = gLoadedModules; mod; mod = mod->Next )
90 if(mod == Module) LEAVE_RET('i', 0);
93 // Add to the "loading" (prevents circular deps)
94 Module->Next = gLoadingModules;
95 gLoadingModules = Module;
97 // Scan dependency list
98 for( j = 0; deps && deps[j]; j++ )
100 // Check if the module is already loaded
101 for( mod = gLoadedModules; mod; mod = mod->Next )
103 if(strcmp(deps[j], mod->Name) == 0)
106 if( mod ) continue; // Dependency is loaded, check the rest
108 // Ok, check if it's loading
109 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
111 if(strcmp(deps[j], mod->Name) == 0)
115 Log_Warning("Module", "Circular dependency detected (%s and %s)",
116 mod->Name, Module->Name);
120 // So, if it's not loaded, we better load it then
121 for( i = 0; i < giNumBuiltinModules; i ++ )
123 if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 )
126 if( i == giNumBuiltinModules ) {
127 Log_Warning("Module", "Dependency '%s' for module '%s' failed",
128 deps[j], Module->Name);
132 // Dependency is not loaded, so load it
133 ret = Module_int_Initialise(
134 gapBuiltinModules[i],
135 gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
139 // The only "ok" error is NOTNEEDED
140 if(ret != MODULE_ERR_NOTNEEDED)
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
153 args = str_split( ArgString, ',' );
157 ret = Module->Init(args);
161 // Remove from loading list
162 gLoadingModules = gLoadingModules->Next;
164 if( ret != MODULE_ERR_OK ) {
167 case MODULE_ERR_MISC:
168 Log_Warning("Module", "Unable to load, reason: Miscelanious");
170 case MODULE_ERR_NOTNEEDED:
171 Log_Warning("Module", "Unable to load, reason: Module not needed");
173 case MODULE_ERR_MALLOC:
174 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
177 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
183 LOG("ret = %i", ret);
185 // Add to loaded list
186 SHORTLOCK( &glModuleSpinlock );
187 Module->Next = gLoadedModules;
188 gLoadedModules = Module;
189 SHORTREL( &glModuleSpinlock );
195 * \brief Scans the builtin modules and creates an array of them
197 void Modules_int_GetBuiltinArray(void)
203 module = &gKernelModules;
205 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
207 if(module->Magic == MODULE_MAGIC) {
212 module = (void*)( (tVAddr)module + 4 );
216 giNumBuiltinModules = i;
217 gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
218 gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) );
222 module = &gKernelModules;
224 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
226 if(module->Magic == MODULE_MAGIC) {
227 gapBuiltinModules[i] = module;
232 module = (void*)( (tVAddr)module + 4 );
237 * \brief Initialises builtin modules
239 void Modules_LoadBuiltins()
243 if( !gapBuiltinModules )
244 Modules_int_GetBuiltinArray();
246 for( i = 0; i < giNumBuiltinModules; i++ )
248 Module_int_Initialise(
249 gapBuiltinModules[i],
250 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
254 if( gasBuiltinModuleArgs != NULL )
255 free(gasBuiltinModuleArgs);
259 * \brief Initialise a builtin module given it's name
261 * E.g. Used by VTerm to load an alternate video driver at runtime
263 int Modules_InitialiseBuiltin(const char *Name)
267 // Check if it's loaded
268 if( Module_IsLoaded(Name) )
271 if( !gapBuiltinModules )
272 Modules_int_GetBuiltinArray();
274 for( i = 0; i < giNumBuiltinModules; i++ )
276 if( strcmp(gapBuiltinModules[i]->Name, Name) == 0 ) {
277 return Module_int_Initialise(gapBuiltinModules[i],
278 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
286 * \brief Sets the parameters for a builtin module
288 void Modules_SetBuiltinParams(const char *Name, char *ArgString)
292 if( gasBuiltinModuleArgs == NULL )
294 Modules_int_GetBuiltinArray();
297 // I hate expensive scans
298 for( i = 0; i < giNumBuiltinModules; i++ )
300 if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
301 gasBuiltinModuleArgs[i] = ArgString;
306 Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
310 * \brief Registers a tModuleLoader with the kernel
311 * \param Loader Pointer to loader structure (must be persistent)
313 int Module_RegisterLoader(tModuleLoader *Loader)
315 if(!Loader) return 1;
317 Loader->Next = gModule_Loaders;
318 gModule_Loaders = Loader;
324 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
325 * \brief Load a module from a memory location
327 int Module_LoadMem(void *Buffer, Uint Length, const char *ArgString)
329 char path[VFS_MEMPATH_SIZE];
331 VFS_GetMemPath(path, Buffer, Length);
333 return Module_LoadFile( path, ArgString );
337 * \fn int Module_LoadFile(const char *Path, const char *ArgString)
338 * \brief Load a module from a file
340 int Module_LoadFile(const char *Path, const char *ArgString)
346 base = Binary_LoadKernel(Path);
350 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
354 // Check for Acess Driver
355 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
358 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
360 if( tmp->Detector(base) == 0 ) continue;
362 return tmp->Loader(base);
366 // Check for EDI Driver
367 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
369 return Module_InitEDI( base ); // And intialise
373 // Unknown module type?, return error
376 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
378 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
383 // Initialise (and register)
384 if( Module_int_Initialise( info, ArgString ) )
394 * \fn int Module_int_ResolveDeps(tModule *Info)
395 * \brief Resolves the dependencies
397 * \note Currently does not resolve the dependencies, just checks them
399 int Module_int_ResolveDeps(tModule *Info)
401 const char **names = Info->Dependencies;
403 // Walk dependencies array
404 for( ; *names; names++ )
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);
416 * \fn int Module_IsLoaded(const char *Name)
417 * \brief Checks if a module is loaded
418 * \param Name Name of module to find
420 int Module_IsLoaded(const char *Name)
422 tModule *mod = gLoadedModules;
425 for( ; mod; mod = mod->Next )
427 // If found, return true
428 if(strcmp(mod->Name, Name) == 0)
431 // not found - return false
436 * \brief Load a module if needed
438 int Module_EnsureLoaded(const char *Name)
440 if( Module_IsLoaded(Name) )
443 if( Modules_InitialiseBuiltin(Name) == 0 )
446 // TODO: Load from a file?