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 (%s and %s)",
92 mod->Name, Module->Name);
96 // So, if it's not loaded, we better load it then
97 for( i = 0; i < giNumBuiltinModules; i ++ )
99 if( strcmp(deps[j], gKernelModules[i].Name) == 0 )
102 if( i == giNumBuiltinModules ) {
103 Log_Warning("Module", "Dependency '%s' for module '%s' failed",
104 deps[j], Module->Name);
108 // Dependency is not loaded, so load it
109 ret = Module_int_Initialise(
111 gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
115 // The only "ok" error is NOTNEEDED
116 if(ret != MODULE_ERR_NOTNEEDED)
121 // All Dependencies OK? Initialise
122 StartupPrint(Module->Name);
123 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
124 Module, Module->Name,
125 Module->Version >> 8, Module->Version & 0xFF
129 args = str_split( ArgString, ',' );
133 ret = Module->Init(args);
137 // Remove from loading list
138 gLoadingModules = gLoadingModules->Next;
140 if( ret != MODULE_ERR_OK ) {
143 case MODULE_ERR_MISC:
144 Log_Warning("Module", "Unable to load, reason: Miscelanious");
146 case MODULE_ERR_NOTNEEDED:
147 Log_Warning("Module", "Unable to load, reason: Module not needed");
149 case MODULE_ERR_MALLOC:
150 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
153 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
159 LOG("ret = %i", ret);
161 // Add to loaded list
162 LOCK( &glModuleSpinlock );
163 Module->Next = gLoadedModules;
164 gLoadedModules = Module;
165 RELEASE( &glModuleSpinlock );
171 * \brief Initialises builtin modules
173 void Modules_LoadBuiltins()
178 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
179 giNumBuiltinModules /= sizeof(tModule);
181 for( i = 0; i < giNumBuiltinModules; i++ )
183 Module_int_Initialise(
185 (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
189 if( gasBuiltinModuleArgs != NULL )
190 free(gasBuiltinModuleArgs);
194 * \brief Sets the parameters for a builtin module
196 void Modules_SetBuiltinParams(char *Name, char *ArgString)
199 if( gasBuiltinModuleArgs == NULL ) {
200 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
201 giNumBuiltinModules /= sizeof(tModule);
202 gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
205 for( i = 0; i < giNumBuiltinModules; i ++ )
207 if(strcmp( gKernelModules[i].Name, Name ) == 0) {
208 gasBuiltinModuleArgs[i] = ArgString;
213 Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
217 * \brief Registers a tModuleLoader with the kernel
218 * \param Loader Pointer to loader structure (must be persistent)
220 int Module_RegisterLoader(tModuleLoader *Loader)
222 if(!Loader) return 1;
224 Loader->Next = gModule_Loaders;
225 gModule_Loaders = Loader;
231 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
232 * \brief Load a module from a memory location
234 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
236 char path[VFS_MEMPATH_SIZE];
238 VFS_GetMemPath(path, Buffer, Length);
240 return Module_LoadFile( path, ArgString );
244 * \fn int Module_LoadFile(char *Path, char *ArgString)
245 * \brief Load a module from a file
247 int Module_LoadFile(char *Path, char *ArgString)
253 base = Binary_LoadKernel(Path);
257 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
261 // Check for Acess Driver
262 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
265 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
267 if( tmp->Detector(base) == 0 ) continue;
269 return tmp->Loader(base);
273 // Check for EDI Driver
274 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
276 return Module_InitEDI( base ); // And intialise
280 // Unknown module type?, return error
283 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
285 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
290 // Check magic number
291 if(info->Magic != MODULE_MAGIC)
293 Log_Warning("Module", "Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
297 // Check Architecture
298 if(info->Arch != MODULE_ARCH_ID)
300 Log_Warning("Module", "Module is for a different architecture");
305 if( Module_int_Initialise( info, ArgString ) )
311 // Resolve Dependencies
312 if( !Module_int_ResolveDeps(info) ) {
317 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
320 info->Version>>8, info->Version & 0xFF
324 //if( info->Init( ArgString ) != 0 )
325 if( info->Init( NULL ) == 0 )
332 LOCK( &glModuleSpinlock );
333 info->Next = gLoadedModules;
334 gLoadedModules = info;
335 RELEASE( &glModuleSpinlock );
342 * \fn int Module_int_ResolveDeps(tModule *Info)
343 * \brief Resolves the dependencies
345 * \note Currently does not resolve the dependencies, just checks them
347 int Module_int_ResolveDeps(tModule *Info)
349 char **names = Info->Dependencies;
351 // Walk dependencies array
352 for( ; *names; names++ )
354 // Check if the module is loaded
355 if( !Module_IsLoaded(*names) ) {
356 Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
364 * \fn int Module_IsLoaded(char *Name)
365 * \brief Checks if a module is loaded
366 * \param Name Name of module to find
368 int Module_IsLoaded(char *Name)
370 tModule *mod = gLoadedModules;
373 for( ; mod; mod = mod->Next )
375 // If found, return true
376 if(strcmp(mod->Name, Name) == 0)
379 // not found - return false