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);
139 LOG("ret = %i", ret);
141 // Remove from loading list
142 gLoadingModules = gLoadingModules->Next;
144 // Add to loaded list
145 LOCK( &glModuleSpinlock );
146 Module->Next = gLoadedModules;
147 gLoadedModules = Module;
148 RELEASE( &glModuleSpinlock );
154 * \brief Initialises builtin modules
156 int Modules_LoadBuiltins()
161 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
162 giNumBuiltinModules /= sizeof(tModule);
164 for( i = 0; i < giNumBuiltinModules; i++ )
166 Module_int_Initialise( &gKernelModules[i] );
173 * \brief Registers a tModuleLoader with the kernel
174 * \param Loader Pointer to loader structure (must be persistent)
176 int Module_RegisterLoader(tModuleLoader *Loader)
178 if(!Loader) return 1;
180 Loader->Next = gModule_Loaders;
181 gModule_Loaders = Loader;
187 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
188 * \brief Load a module from a memory location
190 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
192 char path[VFS_MEMPATH_SIZE];
194 VFS_GetMemPath(path, Buffer, Length);
196 return Module_LoadFile( path, ArgString );
200 * \fn int Module_LoadFile(char *Path, char *ArgString)
201 * \brief Load a module from a file
203 int Module_LoadFile(char *Path, char *ArgString)
209 base = Binary_LoadKernel(Path);
213 Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path);
217 // Check for Acess Driver
218 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
221 for( tmp = gModule_Loaders; tmp; tmp = tmp->Next)
223 if( tmp->Detector(base) == 0 ) continue;
225 return tmp->Loader(base);
229 // Check for EDI Driver
230 if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 )
232 return Module_InitEDI( base ); // And intialise
236 // Unknown module type?, return error
239 Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path);
241 Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path);
246 // Check magic number
247 if(info->Magic != MODULE_MAGIC)
249 Log_Warning("Module", "Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
253 // Check Architecture
254 if(info->Arch != MODULE_ARCH_ID)
256 Log_Warning("Module", "Module is for a different architecture");
261 if( Module_int_Initialise( info ) )
267 // Resolve Dependencies
268 if( !Module_int_ResolveDeps(info) ) {
273 Log_Log("Module", "Initialising %p '%s' v%i.%i...",
276 info->Version>>8, info->Version & 0xFF
280 //if( info->Init( ArgString ) != 0 )
281 if( info->Init( NULL ) == 0 )
288 LOCK( &glModuleSpinlock );
289 info->Next = gLoadedModules;
290 gLoadedModules = info;
291 RELEASE( &glModuleSpinlock );
298 * \fn int Module_int_ResolveDeps(tModule *Info)
299 * \brief Resolves the dependencies
301 * \note Currently does not resolve the dependencies, just checks them
303 int Module_int_ResolveDeps(tModule *Info)
305 char **names = Info->Dependencies;
307 // Walk dependencies array
308 for( ; *names; names++ )
310 // Check if the module is loaded
311 if( !Module_IsLoaded(*names) ) {
312 Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
320 * \fn int Module_IsLoaded(char *Name)
321 * \brief Checks if a module is loaded
322 * \param Name Name of module to find
324 int Module_IsLoaded(char *Name)
326 tModule *mod = gLoadedModules;
329 for( ; mod; mod = mod->Next )
331 // If found, return true
332 if(strcmp(mod->Name, Name) == 0)
335 // not found - return false