13 int Modules_LoadBuiltins(void);
14 int Module_RegisterLoader(tModuleLoader *Loader);
15 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString);
16 int Module_LoadFile(char *Path, char *ArgString);
17 int Module_int_ResolveDeps(tModule *Info);
18 int Module_IsLoaded(char *Name);
21 EXPORT(Module_RegisterLoader);
25 extern int UDI_LoadDriver(void *Base);
27 extern void StartupPrint(char *Str);
28 extern tModule gKernelModules[];
29 extern void gKernelModulesEnd;
32 int giNumBuiltinModules = 0;
33 tSpinlock glModuleSpinlock;
34 tModule *gLoadedModules = NULL;
35 tModuleLoader *gModule_Loaders = NULL;
36 tModule *gLoadingModules = NULL;
40 * \brief Initialises a module
41 * \param Module Pointer to the module header
42 * \return Zero on success, eModuleErrors or -1 on error
43 * \retval -1 Returned if a dependency fails, or a circular dependency
45 * \retval 0 Returned on success
46 * \retval >0 Error code form the module's initialisation function
48 int Module_int_Initialise(tModule *Module)
55 ENTER("pModule", Module);
57 deps = Module->Dependencies;
59 // Check if the module has been loaded
60 for( mod = gLoadedModules; mod; mod = mod->Next )
62 if(mod == Module) LEAVE_RET('i', 0);
65 // Add to the "loading" (prevents circular deps)
66 Module->Next = gLoadingModules;
67 gLoadingModules = Module;
69 // Scan dependency list
70 for( j = 0; deps && deps[j]; j++ )
72 // Check if the module is already loaded
73 for( mod = gLoadedModules; mod; mod = mod->Next )
75 if(strcmp(deps[j], mod->Name) == 0)
78 if( mod ) continue; // Dependency is loaded, check the rest
80 // Ok, check if it's loading
81 for( mod = gLoadingModules->Next; mod; mod = mod->Next )
83 if(strcmp(deps[j], mod->Name) == 0)
87 Log_Warning("Module", "Circular dependency detected");
91 // So, if it's not loaded, we better load it then
92 for( i = 0; i < giNumBuiltinModules; i ++ )
94 if( strcmp(deps[j], gKernelModules[i].Name) == 0 )
97 if( i == giNumBuiltinModules ) {
98 Log_Warning("Module", "Dependency '%s' for module '%s' failed");
102 // Dependency is not loaded, so load it
103 ret = Module_int_Initialise( &gKernelModules[i] );
106 // The only "ok" error is NOTNEEDED
107 if(ret != MODULE_ERR_NOTNEEDED)
112 // All Dependencies OK? Initialise
113 StartupPrint(Module->Name);
114 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
115 Module, Module->Name,
116 Module->Version >> 8, Module->Version & 0xFF
119 ret = Module->Init(NULL);
120 if( ret != MODULE_ERR_OK ) {
123 case MODULE_ERR_MISC:
124 Log_Warning("Module", "Unable to load, reason: Miscelanious");
126 case MODULE_ERR_NOTNEEDED:
127 Log_Warning("Module", "Unable to load, reason: Module not needed");
129 case MODULE_ERR_MALLOC:
130 Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
133 Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
140 // Remove from loading list
141 gLoadingModules = gLoadingModules->Next;
143 // Add to loaded list
144 LOCK( &glModuleSpinlock );
145 Module->Next = gLoadedModules;
146 gLoadedModules = Module;
147 RELEASE( &glModuleSpinlock );
153 * \brief Initialises builtin modules
155 int Modules_LoadBuiltins()
160 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
161 giNumBuiltinModules /= sizeof(tModule);
163 for( i = 0; i < giNumBuiltinModules; i++ )
165 Module_int_Initialise( &gKernelModules[i] );
172 * \brief Registers a tModuleLoader with the kernel
173 * \param Loader Pointer to loader structure (must be persistent)
175 int Module_RegisterLoader(tModuleLoader *Loader)
177 if(!Loader) return 1;
179 Loader->Next = gModule_Loaders;
180 gModule_Loaders = Loader;
186 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
187 * \brief Load a module from a memory location
189 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
191 char path[VFS_MEMPATH_SIZE];
193 VFS_GetMemPath(path, Buffer, Length);
195 return Module_LoadFile( path, ArgString );
199 * \fn int Module_LoadFile(char *Path, char *ArgString)
200 * \brief Load a module from a file
202 int Module_LoadFile(char *Path, char *ArgString)
208 base = Binary_LoadKernel(Path);
212 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
216 // Check for Acess Driver
217 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
220 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
222 if( tmp->Detector(base) == 0 ) continue;
224 return tmp->Loader(base);
228 // Check for EDI Driver
229 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
231 return Module_InitEDI( base ); // And intialise
235 // Unknown module type?, return error
238 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
240 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
245 // Check magic number
246 if(info->Magic != MODULE_MAGIC)
248 Log_Warning("Module", "Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
252 // Check Architecture
253 if(info->Arch != MODULE_ARCH_ID)
255 Log_Warning("Module", "Module is for a different architecture");
260 if( Module_int_Initialise( info ) )
266 // Resolve Dependencies
267 if( !Module_int_ResolveDeps(info) ) {
272 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
275 info->Version>>8, info->Version & 0xFF
279 //if( info->Init( ArgString ) != 0 )
280 if( info->Init( NULL ) == 0 )
287 LOCK( &glModuleSpinlock );
288 info->Next = gLoadedModules;
289 gLoadedModules = info;
290 RELEASE( &glModuleSpinlock );
297 * \fn int Module_int_ResolveDeps(tModule *Info)
298 * \brief Resolves the dependencies
300 * \note Currently does not resolve the dependencies, just checks them
302 int Module_int_ResolveDeps(tModule *Info)
304 char **names = Info->Dependencies;
306 // Walk dependencies array
307 for( ; *names; names++ )
309 // Check if the module is loaded
310 if( !Module_IsLoaded(*names) ) {
311 Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
319 * \fn int Module_IsLoaded(char *Name)
320 * \brief Checks if a module is loaded
321 * \param Name Name of module to find
323 int Module_IsLoaded(char *Name)
325 tModule *mod = gLoadedModules;
328 for( ; mod; mod = mod->Next )
330 // If found, return true
331 if(strcmp(mod->Name, Name) == 0)
334 // not found - return false