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(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 tSpinlock 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 * \return Zero on success, eModuleErrors or -1 on error
47 * \retval -1 Returned if a dependency fails, or a circular dependency
49 * \retval 0 Returned on success
50 * \retval >0 Error code form the module's initialisation function
52 int Module_int_Initialise(tModule *Module, char *ArgString)
60 ENTER("pModule", Module);
61 LOG("Module->Magic = 0x%x", Module->Magic);
62 if(Module->Magic != MODULE_MAGIC) {
65 "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)",
66 Module, Module->Magic, MODULE_MAGIC
68 LEAVE('i', MODULE_ERR_BADMODULE);
69 return MODULE_ERR_BADMODULE;
71 LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name);
73 if(Module->Arch != MODULE_ARCH_ID) {
76 "Module %p (%s) is for another architecture (%i)",
77 Module, Module->Name, Module->Arch
81 deps = Module->Dependencies;
83 // Check if the module has been loaded
84 for( mod = gLoadedModules; mod; mod = mod->Next )
86 if(mod == Module) LEAVE_RET('i', 0);
89 // Add to the "loading" (prevents circular deps)
90 Module->Next = gLoadingModules;
91 gLoadingModules = Module;
93 // Scan dependency list
94 for( j = 0; deps && deps[j]; j++ )
96 // Check if the module is already loaded
97 for( mod = gLoadedModules; mod; mod = mod->Next )
99 if(strcmp(deps[j], mod->Name) == 0)
102 if( mod ) continue; // Dependency is loaded, check the rest
104 // Ok, check if it's loading
105 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
107 if(strcmp(deps[j], mod->Name) == 0)
111 Log_Warning("Module", "Circular dependency detected (%s and %s)",
112 mod->Name, Module->Name);
116 // So, if it's not loaded, we better load it then
117 for( i = 0; i < giNumBuiltinModules; i ++ )
119 if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 )
122 if( i == giNumBuiltinModules ) {
123 Log_Warning("Module", "Dependency '%s' for module '%s' failed",
124 deps[j], Module->Name);
128 // Dependency is not loaded, so load it
129 ret = Module_int_Initialise(
130 gapBuiltinModules[i],
131 gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
135 // The only "ok" error is NOTNEEDED
136 if(ret != MODULE_ERR_NOTNEEDED)
141 // All Dependencies OK? Initialise
142 StartupPrint(Module->Name);
143 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
144 Module, Module->Name,
145 Module->Version >> 8, Module->Version & 0xFF
149 args = str_split( ArgString, ',' );
153 ret = Module->Init(args);
157 // Remove from loading list
158 gLoadingModules = gLoadingModules->Next;
160 if( ret != MODULE_ERR_OK ) {
163 case MODULE_ERR_MISC:
164 Log_Warning("Module", "Unable to load, reason: Miscelanious");
166 case MODULE_ERR_NOTNEEDED:
167 Log_Warning("Module", "Unable to load, reason: Module not needed");
169 case MODULE_ERR_MALLOC:
170 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
173 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
179 LOG("ret = %i", ret);
181 // Add to loaded list
182 LOCK( &glModuleSpinlock );
183 Module->Next = gLoadedModules;
184 gLoadedModules = Module;
185 RELEASE( &glModuleSpinlock );
191 * \brief Scans the builtin modules and creates an array of them
193 void Modules_int_GetBuiltinArray(void)
199 module = &gKernelModules;
201 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
203 if(module->Magic == MODULE_MAGIC) {
208 module = (void*)( (tVAddr)module + 4 );
212 giNumBuiltinModules = i;
213 gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
214 gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) );
218 module = &gKernelModules;
220 while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
222 if(module->Magic == MODULE_MAGIC) {
223 gapBuiltinModules[i] = module;
228 module = (void*)( (tVAddr)module + 4 );
233 * \brief Initialises builtin modules
235 void Modules_LoadBuiltins()
239 if( !gapBuiltinModules )
240 Modules_int_GetBuiltinArray();
242 for( i = 0; i < giNumBuiltinModules; i++ )
244 Module_int_Initialise(
245 gapBuiltinModules[i],
246 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
250 if( gasBuiltinModuleArgs != NULL )
251 free(gasBuiltinModuleArgs);
255 * \brief Sets the parameters for a builtin module
257 void Modules_SetBuiltinParams(char *Name, char *ArgString)
261 if( gasBuiltinModuleArgs == NULL )
263 Modules_int_GetBuiltinArray();
266 // I hate expensive scans
267 for( i = 0; i < giNumBuiltinModules; i++ )
269 if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
270 gasBuiltinModuleArgs[i] = ArgString;
275 Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
279 * \brief Registers a tModuleLoader with the kernel
280 * \param Loader Pointer to loader structure (must be persistent)
282 int Module_RegisterLoader(tModuleLoader *Loader)
284 if(!Loader) return 1;
286 Loader->Next = gModule_Loaders;
287 gModule_Loaders = Loader;
293 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
294 * \brief Load a module from a memory location
296 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
298 char path[VFS_MEMPATH_SIZE];
300 VFS_GetMemPath(path, Buffer, Length);
302 return Module_LoadFile( path, ArgString );
306 * \fn int Module_LoadFile(char *Path, char *ArgString)
307 * \brief Load a module from a file
309 int Module_LoadFile(char *Path, char *ArgString)
315 base = Binary_LoadKernel(Path);
319 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
323 // Check for Acess Driver
324 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
327 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
329 if( tmp->Detector(base) == 0 ) continue;
331 return tmp->Loader(base);
335 // Check for EDI Driver
336 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
338 return Module_InitEDI( base ); // And intialise
342 // Unknown module type?, return error
345 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
347 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
352 // Initialise (and register)
353 if( Module_int_Initialise( info, ArgString ) )
363 * \fn int Module_int_ResolveDeps(tModule *Info)
364 * \brief Resolves the dependencies
366 * \note Currently does not resolve the dependencies, just checks them
368 int Module_int_ResolveDeps(tModule *Info)
370 char **names = Info->Dependencies;
372 // Walk dependencies array
373 for( ; *names; names++ )
375 // Check if the module is loaded
376 if( !Module_IsLoaded(*names) ) {
377 Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
385 * \fn int Module_IsLoaded(char *Name)
386 * \brief Checks if a module is loaded
387 * \param Name Name of module to find
389 int Module_IsLoaded(char *Name)
391 tModule *mod = gLoadedModules;
394 for( ; mod; mod = mod->Next )
396 // If found, return true
397 if(strcmp(mod->Name, Name) == 0)
400 // not found - return false