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);
23 EXPORT(Module_RegisterLoader);
27 extern int UDI_LoadDriver(void *Base);
29 extern void StartupPrint(char *Str);
30 extern void gKernelModules;
31 extern void gKernelModulesEnd;
34 int giNumBuiltinModules = 0;
35 tShortSpinlock glModuleSpinlock;
36 tModule *gLoadedModules = NULL;
37 tModuleLoader *gModule_Loaders = NULL;
38 tModule *gLoadingModules = NULL;
39 tModule **gapBuiltinModules = NULL;
40 char **gasBuiltinModuleArgs;
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
50 * \retval 0 Returned on success
51 * \retval >0 Error code form the module's initialisation function
53 int Module_int_Initialise(tModule *Module, char *ArgString)
61 ENTER("pModule", Module);
62 LOG("Module->Magic = 0x%x", Module->Magic);
63 if(Module->Magic != MODULE_MAGIC) {
66 "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)",
67 Module, Module->Magic, MODULE_MAGIC
69 LEAVE('i', MODULE_ERR_BADMODULE);
70 return MODULE_ERR_BADMODULE;
72 LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name);
74 if(Module->Arch != MODULE_ARCH_ID) {
77 "Module %p (%s) is for another architecture (%i)",
78 Module, Module->Name, Module->Arch
82 deps = Module->Dependencies;
84 // Check if the module has been loaded
85 for( mod = gLoadedModules; mod; mod = mod->Next )
87 if(mod == Module) LEAVE_RET('i', 0);
90 // Add to the "loading" (prevents circular deps)
91 Module->Next = gLoadingModules;
92 gLoadingModules = Module;
94 // Scan dependency list
95 for( j = 0; deps && deps[j]; j++ )
97 // Check if the module is already loaded
98 for( mod = gLoadedModules; mod; mod = mod->Next )
100 if(strcmp(deps[j], mod->Name) == 0)
103 if( mod ) continue; // Dependency is loaded, check the rest
105 // Ok, check if it's loading
106 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
108 if(strcmp(deps[j], mod->Name) == 0)
112 Log_Warning("Module", "Circular dependency detected (%s and %s)",
113 mod->Name, Module->Name);
117 // So, if it's not loaded, we better load it then
118 for( i = 0; i < giNumBuiltinModules; i ++ )
120 if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 )
123 if( i == giNumBuiltinModules ) {
124 Log_Warning("Module", "Dependency '%s' for module '%s' failed",
125 deps[j], Module->Name);
129 // Dependency is not loaded, so load it
130 ret = Module_int_Initialise(
131 gapBuiltinModules[i],
132 gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
136 // The only "ok" error is NOTNEEDED
137 if(ret != MODULE_ERR_NOTNEEDED)
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
150 args = str_split( ArgString, ',' );
154 ret = Module->Init(args);
158 // Remove from loading list
159 gLoadingModules = gLoadingModules->Next;
161 if( ret != MODULE_ERR_OK ) {
164 case MODULE_ERR_MISC:
165 Log_Warning("Module", "Unable to load, reason: Miscelanious");
167 case MODULE_ERR_NOTNEEDED:
168 Log_Warning("Module", "Unable to load, reason: Module not needed");
170 case MODULE_ERR_MALLOC:
171 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
174 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
180 LOG("ret = %i", ret);
182 // Add to loaded list
183 SHORTLOCK( &glModuleSpinlock );
184 Module->Next = gLoadedModules;
185 gLoadedModules = Module;
186 SHORTREL( &glModuleSpinlock );
192 * \brief Scans the builtin modules and creates an array of them
194 void Modules_int_GetBuiltinArray(void)
200 module = &gKernelModules;
202 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
204 if(module->Magic == MODULE_MAGIC) {
209 module = (void*)( (tVAddr)module + 4 );
213 giNumBuiltinModules = i;
214 gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
215 gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) );
219 module = &gKernelModules;
221 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
223 if(module->Magic == MODULE_MAGIC) {
224 gapBuiltinModules[i] = module;
229 module = (void*)( (tVAddr)module + 4 );
234 * \brief Initialises builtin modules
236 void Modules_LoadBuiltins()
240 if( !gapBuiltinModules )
241 Modules_int_GetBuiltinArray();
243 for( i = 0; i < giNumBuiltinModules; i++ )
245 Module_int_Initialise(
246 gapBuiltinModules[i],
247 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
251 if( gasBuiltinModuleArgs != NULL )
252 free(gasBuiltinModuleArgs);
256 * \brief Initialise a builtin module given it's name
257 * \example Used by VTerm to load an alternate video driver at runtime
259 int Modules_InitialiseBuiltin(const char *Name)
263 // Check if it's loaded
264 if( Module_IsLoaded(Name) )
267 if( !gapBuiltinModules )
268 Modules_int_GetBuiltinArray();
270 for( i = 0; i < giNumBuiltinModules; i++ )
272 if( strcmp(gapBuiltinModules[i]->Name, Name) == 0 ) {
273 return Module_int_Initialise(gapBuiltinModules[i],
274 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
282 * \brief Sets the parameters for a builtin module
284 void Modules_SetBuiltinParams(char *Name, char *ArgString)
288 if( gasBuiltinModuleArgs == NULL )
290 Modules_int_GetBuiltinArray();
293 // I hate expensive scans
294 for( i = 0; i < giNumBuiltinModules; i++ )
296 if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
297 gasBuiltinModuleArgs[i] = ArgString;
302 Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
306 * \brief Registers a tModuleLoader with the kernel
307 * \param Loader Pointer to loader structure (must be persistent)
309 int Module_RegisterLoader(tModuleLoader *Loader)
311 if(!Loader) return 1;
313 Loader->Next = gModule_Loaders;
314 gModule_Loaders = Loader;
320 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
321 * \brief Load a module from a memory location
323 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
325 char path[VFS_MEMPATH_SIZE];
327 VFS_GetMemPath(path, Buffer, Length);
329 return Module_LoadFile( path, ArgString );
333 * \fn int Module_LoadFile(char *Path, char *ArgString)
334 * \brief Load a module from a file
336 int Module_LoadFile(char *Path, char *ArgString)
342 base = Binary_LoadKernel(Path);
346 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
350 // Check for Acess Driver
351 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
354 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
356 if( tmp->Detector(base) == 0 ) continue;
358 return tmp->Loader(base);
362 // Check for EDI Driver
363 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
365 return Module_InitEDI( base ); // And intialise
369 // Unknown module type?, return error
372 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
374 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
379 // Initialise (and register)
380 if( Module_int_Initialise( info, ArgString ) )
390 * \fn int Module_int_ResolveDeps(tModule *Info)
391 * \brief Resolves the dependencies
393 * \note Currently does not resolve the dependencies, just checks them
395 int Module_int_ResolveDeps(tModule *Info)
397 char **names = Info->Dependencies;
399 // Walk dependencies array
400 for( ; *names; names++ )
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);
412 * \fn int Module_IsLoaded(const char *Name)
413 * \brief Checks if a module is loaded
414 * \param Name Name of module to find
416 int Module_IsLoaded(const char *Name)
418 tModule *mod = gLoadedModules;
421 for( ; mod; mod = mod->Next )
423 // If found, return true
424 if(strcmp(mod->Name, Name) == 0)
427 // not found - return false