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);
24 EXPORT(Module_RegisterLoader);
28 extern int UDI_LoadDriver(void *Base);
30 extern void StartupPrint(const char *Str);
31 extern tModule gKernelModules;
32 extern tModule gKernelModulesEnd;
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;
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
51 * \retval 0 Returned on success
52 * \retval >0 Error code form the module's initialisation function
54 int Module_int_Initialise(tModule *Module, const char *ArgString)
62 ENTER("pModule", Module);
63 LOG("Module->Magic = 0x%x", Module->Magic);
64 if(Module->Magic != MODULE_MAGIC) {
67 "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)",
68 Module, Module->Magic, MODULE_MAGIC
70 LEAVE('i', MODULE_ERR_BADMODULE);
71 return MODULE_ERR_BADMODULE;
73 LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name);
75 if(Module->Arch != MODULE_ARCH_ID) {
78 "Module %p (%s) is for another architecture (%i)",
79 Module, Module->Name, Module->Arch
83 deps = Module->Dependencies;
85 // Check if the module has been loaded
86 for( mod = gLoadedModules; mod; mod = mod->Next )
88 if(mod == Module) LEAVE_RET('i', 0);
91 // Add to the "loading" (prevents circular deps)
92 Module->Next = gLoadingModules;
93 gLoadingModules = Module;
95 // Scan dependency list
96 for( j = 0; deps && deps[j]; j++ )
98 // Check if the module is already loaded
99 for( mod = gLoadedModules; mod; mod = mod->Next )
101 if(strcmp(deps[j], mod->Name) == 0)
104 if( mod ) continue; // Dependency is loaded, check the rest
106 // Ok, check if it's loading
107 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
109 if(strcmp(deps[j], mod->Name) == 0)
113 Log_Warning("Module", "Circular dependency detected (%s and %s)",
114 mod->Name, Module->Name);
118 // So, if it's not loaded, we better load it then
119 for( i = 0; i < giNumBuiltinModules; i ++ )
121 if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 )
124 if( i == giNumBuiltinModules ) {
125 Log_Warning("Module", "Dependency '%s' for module '%s' failed",
126 deps[j], Module->Name);
130 // Dependency is not loaded, so load it
131 ret = Module_int_Initialise(
132 gapBuiltinModules[i],
133 gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
137 // The only "ok" error is NOTNEEDED
138 if(ret != MODULE_ERR_NOTNEEDED)
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
151 args = str_split( ArgString, ',' );
155 ret = Module->Init(args);
159 // Remove from loading list
160 gLoadingModules = gLoadingModules->Next;
162 if( ret != MODULE_ERR_OK ) {
165 case MODULE_ERR_MISC:
166 Log_Warning("Module", "Unable to load, reason: Miscelanious");
168 case MODULE_ERR_NOTNEEDED:
169 Log_Warning("Module", "Unable to load, reason: Module not needed");
171 case MODULE_ERR_MALLOC:
172 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
175 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
181 LOG("ret = %i", ret);
183 // Add to loaded list
184 SHORTLOCK( &glModuleSpinlock );
185 Module->Next = gLoadedModules;
186 gLoadedModules = Module;
187 SHORTREL( &glModuleSpinlock );
193 * \brief Scans the builtin modules and creates an array of them
195 void Modules_int_GetBuiltinArray(void)
201 module = &gKernelModules;
203 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
205 if(module->Magic == MODULE_MAGIC) {
210 module = (void*)( (tVAddr)module + 4 );
214 giNumBuiltinModules = i;
215 gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
216 gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) );
220 module = &gKernelModules;
222 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
224 if(module->Magic == MODULE_MAGIC) {
225 gapBuiltinModules[i] = module;
230 module = (void*)( (tVAddr)module + 4 );
235 * \brief Initialises builtin modules
237 void Modules_LoadBuiltins()
241 if( !gapBuiltinModules )
242 Modules_int_GetBuiltinArray();
244 for( i = 0; i < giNumBuiltinModules; i++ )
246 Module_int_Initialise(
247 gapBuiltinModules[i],
248 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
252 if( gasBuiltinModuleArgs != NULL )
253 free(gasBuiltinModuleArgs);
257 * \brief Initialise a builtin module given it's name
259 * E.g. Used by VTerm to load an alternate video driver at runtime
261 int Modules_InitialiseBuiltin(const char *Name)
265 // Check if it's loaded
266 if( Module_IsLoaded(Name) )
269 if( !gapBuiltinModules )
270 Modules_int_GetBuiltinArray();
272 for( i = 0; i < giNumBuiltinModules; i++ )
274 if( strcmp(gapBuiltinModules[i]->Name, Name) == 0 ) {
275 return Module_int_Initialise(gapBuiltinModules[i],
276 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
284 * \brief Sets the parameters for a builtin module
286 void Modules_SetBuiltinParams(char *Name, char *ArgString)
290 if( gasBuiltinModuleArgs == NULL )
292 Modules_int_GetBuiltinArray();
295 // I hate expensive scans
296 for( i = 0; i < giNumBuiltinModules; i++ )
298 if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
299 gasBuiltinModuleArgs[i] = ArgString;
304 Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
308 * \brief Registers a tModuleLoader with the kernel
309 * \param Loader Pointer to loader structure (must be persistent)
311 int Module_RegisterLoader(tModuleLoader *Loader)
313 if(!Loader) return 1;
315 Loader->Next = gModule_Loaders;
316 gModule_Loaders = Loader;
322 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
323 * \brief Load a module from a memory location
325 int Module_LoadMem(void *Buffer, Uint Length, const char *ArgString)
327 char path[VFS_MEMPATH_SIZE];
329 VFS_GetMemPath(path, Buffer, Length);
331 return Module_LoadFile( path, ArgString );
335 * \fn int Module_LoadFile(const char *Path, const char *ArgString)
336 * \brief Load a module from a file
338 int Module_LoadFile(const char *Path, const char *ArgString)
344 base = Binary_LoadKernel(Path);
348 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
352 // Check for Acess Driver
353 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
356 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
358 if( tmp->Detector(base) == 0 ) continue;
360 return tmp->Loader(base);
364 // Check for EDI Driver
365 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
367 return Module_InitEDI( base ); // And intialise
371 // Unknown module type?, return error
374 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
376 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
381 // Initialise (and register)
382 if( Module_int_Initialise( info, ArgString ) )
392 * \fn int Module_int_ResolveDeps(tModule *Info)
393 * \brief Resolves the dependencies
395 * \note Currently does not resolve the dependencies, just checks them
397 int Module_int_ResolveDeps(tModule *Info)
399 const char **names = Info->Dependencies;
401 // Walk dependencies array
402 for( ; *names; names++ )
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);
414 * \fn int Module_IsLoaded(const char *Name)
415 * \brief Checks if a module is loaded
416 * \param Name Name of module to find
418 int Module_IsLoaded(const char *Name)
420 tModule *mod = gLoadedModules;
423 for( ; mod; mod = mod->Next )
425 // If found, return true
426 if(strcmp(mod->Name, Name) == 0)
429 // not found - return false