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 tModule 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 char **gasBuiltinModuleArgs;
43 * \brief Initialises a module
44 * \param Module Pointer to the module header
45 * \return Zero on success, eModuleErrors or -1 on error
46 * \retval -1 Returned if a dependency fails, or a circular dependency
48 * \retval 0 Returned on success
49 * \retval >0 Error code form the module's initialisation function
51 int Module_int_Initialise(tModule *Module, char *ArgString)
59 ENTER("pModule", Module);
61 deps = Module->Dependencies;
63 // Check if the module has been loaded
64 for( mod = gLoadedModules; mod; mod = mod->Next )
66 if(mod == Module) LEAVE_RET('i', 0);
69 // Add to the "loading" (prevents circular deps)
70 Module->Next = gLoadingModules;
71 gLoadingModules = Module;
73 // Scan dependency list
74 for( j = 0; deps && deps[j]; j++ )
76 // Check if the module is already loaded
77 for( mod = gLoadedModules; mod; mod = mod->Next )
79 if(strcmp(deps[j], mod->Name) == 0)
82 if( mod ) continue; // Dependency is loaded, check the rest
84 // Ok, check if it's loading
85 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
87 if(strcmp(deps[j], mod->Name) == 0)
91 Log_Warning("Module", "Circular dependency detected");
95 // So, if it's not loaded, we better load it then
96 for( i = 0; i < giNumBuiltinModules; i ++ )
98 if( strcmp(deps[j], gKernelModules[i].Name) == 0 )
101 if( i == giNumBuiltinModules ) {
102 Log_Warning("Module", "Dependency '%s' for module '%s' failed");
106 // Dependency is not loaded, so load it
107 ret = Module_int_Initialise(
109 gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
113 // The only "ok" error is NOTNEEDED
114 if(ret != MODULE_ERR_NOTNEEDED)
119 // All Dependencies OK? Initialise
120 StartupPrint(Module->Name);
121 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
122 Module, Module->Name,
123 Module->Version >> 8, Module->Version & 0xFF
127 args = str_split( ArgString, ',' );
131 ret = Module->Init(args);
135 if( ret != MODULE_ERR_OK ) {
138 case MODULE_ERR_MISC:
139 Log_Warning("Module", "Unable to load, reason: Miscelanious");
141 case MODULE_ERR_NOTNEEDED:
142 Log_Warning("Module", "Unable to load, reason: Module not needed");
144 case MODULE_ERR_MALLOC:
145 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
148 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
154 LOG("ret = %i", ret);
156 // Remove from loading list
157 gLoadingModules = gLoadingModules->Next;
159 // Add to loaded list
160 LOCK( &glModuleSpinlock );
161 Module->Next = gLoadedModules;
162 gLoadedModules = Module;
163 RELEASE( &glModuleSpinlock );
169 * \brief Initialises builtin modules
171 void Modules_LoadBuiltins()
176 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
177 giNumBuiltinModules /= sizeof(tModule);
179 for( i = 0; i < giNumBuiltinModules; i++ )
181 Module_int_Initialise(
183 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
187 if( gasBuiltinModuleArgs != NULL )
188 free(gasBuiltinModuleArgs);
192 * \brief Sets the parameters for a builtin module
194 void Modules_SetBuiltinParams(char *Name, char *ArgString)
197 if( gasBuiltinModuleArgs == NULL ) {
198 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
199 giNumBuiltinModules /= sizeof(tModule);
200 gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
203 for( i = 0; i < giNumBuiltinModules; i ++ )
205 if(strcmp( gKernelModules[i].Name, Name ) == 0) {
206 gasBuiltinModuleArgs[i] = ArgString;
211 Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
215 * \brief Registers a tModuleLoader with the kernel
216 * \param Loader Pointer to loader structure (must be persistent)
218 int Module_RegisterLoader(tModuleLoader *Loader)
220 if(!Loader) return 1;
222 Loader->Next = gModule_Loaders;
223 gModule_Loaders = Loader;
229 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
230 * \brief Load a module from a memory location
232 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
234 char path[VFS_MEMPATH_SIZE];
236 VFS_GetMemPath(path, Buffer, Length);
238 return Module_LoadFile( path, ArgString );
242 * \fn int Module_LoadFile(char *Path, char *ArgString)
243 * \brief Load a module from a file
245 int Module_LoadFile(char *Path, char *ArgString)
251 base = Binary_LoadKernel(Path);
255 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
259 // Check for Acess Driver
260 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
263 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
265 if( tmp->Detector(base) == 0 ) continue;
267 return tmp->Loader(base);
271 // Check for EDI Driver
272 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
274 return Module_InitEDI( base ); // And intialise
278 // Unknown module type?, return error
281 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
283 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
288 // Check magic number
289 if(info->Magic != MODULE_MAGIC)
291 Log_Warning("Module", "Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
295 // Check Architecture
296 if(info->Arch != MODULE_ARCH_ID)
298 Log_Warning("Module", "Module is for a different architecture");
303 if( Module_int_Initialise( info, ArgString ) )
309 // Resolve Dependencies
310 if( !Module_int_ResolveDeps(info) ) {
315 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
318 info->Version>>8, info->Version & 0xFF
322 //if( info->Init( ArgString ) != 0 )
323 if( info->Init( NULL ) == 0 )
330 LOCK( &glModuleSpinlock );
331 info->Next = gLoadedModules;
332 gLoadedModules = info;
333 RELEASE( &glModuleSpinlock );
340 * \fn int Module_int_ResolveDeps(tModule *Info)
341 * \brief Resolves the dependencies
343 * \note Currently does not resolve the dependencies, just checks them
345 int Module_int_ResolveDeps(tModule *Info)
347 char **names = Info->Dependencies;
349 // Walk dependencies array
350 for( ; *names; names++ )
352 // Check if the module is loaded
353 if( !Module_IsLoaded(*names) ) {
354 Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
362 * \fn int Module_IsLoaded(char *Name)
363 * \brief Checks if a module is loaded
364 * \param Name Name of module to find
366 int Module_IsLoaded(char *Name)
368 tModule *mod = gLoadedModules;
371 for( ; mod; mod = mod->Next )
373 // If found, return true
374 if(strcmp(mod->Name, Name) == 0)
377 // not found - return false