9 int Modules_LoadBuiltins();
10 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString);
11 int Module_LoadFile(char *Path, char *ArgString);
12 int Module_int_ResolveDeps(tModule *Info);
13 int Module_IsLoaded(char *Name);
16 extern void StartupPrint(char *Str);
17 extern tModule gKernelModules[];
18 extern void gKernelModulesEnd;
21 int giNumBuiltinModules = 0;
22 int giModuleSpinlock = 0;
23 tModule *gLoadedModules = NULL;
26 int Modules_LoadBuiltins()
33 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
34 giNumBuiltinModules /= sizeof(tModule);
36 baIsLoaded = calloc( giNumBuiltinModules, sizeof(*baIsLoaded) );
38 // Pass 1 - Are the dependencies compiled in?
39 for( i = 0; i < giNumBuiltinModules; i++ )
41 deps = gKernelModules[i].Dependencies;
44 for( j = 0; deps[j]; j++ )
46 for( k = 0; k < giNumBuiltinModules; k++ ) {
47 if(strcmp(deps[j], gKernelModules[k].Name) == 0)
50 if(k == giNumBuiltinModules) {
51 Warning("Unable to find dependency '%s' for '%s' in kernel",
52 deps[j], gKernelModules[i].Name);
54 baIsLoaded[i] = -1; // Don't Load
65 for( i = 0; i < giNumBuiltinModules; i++ )
67 if( baIsLoaded[i] ) continue; // Ignore already loaded modules
69 deps = gKernelModules[i].Dependencies;
73 for( j = 0; deps[j]; j++ )
75 for( k = 0; k < giNumBuiltinModules; k++ ) {
76 if(strcmp(deps[j], gKernelModules[k].Name) == 0)
79 // `k` is assumed to be less than `giNumBuiltinModules`
81 // If a dependency failed, skip and mark as failed
82 if( baIsLoaded[k] == -1 ) {
87 // If a dependency is not intialised, skip
88 if( !baIsLoaded[k] ) break;
90 // Check if we broke out
91 if( deps[j] ) continue;
94 // All Dependencies OK? Initialise
95 StartupPrint(gKernelModules[i].Name);
96 Log("Initialising %p '%s' v%i.%i...",
98 gKernelModules[i].Name,
99 gKernelModules[i].Version>>8, gKernelModules[i].Version & 0xFF
101 gKernelModules[i].Init(NULL);
112 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
113 * \brief Load a module from a memory location
115 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
117 char path[VFS_MEMPATH_SIZE];
119 VFS_GetMemPath(Buffer, Length, path);
121 return Module_LoadFile( path, ArgString );
125 * \fn int Module_LoadFile(char *Path, char *ArgString)
126 * \brief Load a module from a file
128 int Module_LoadFile(char *Path, char *ArgString)
134 base = Binary_LoadKernel(Path);
137 if(base == NULL) return 0;
139 // Check for Acess Driver
140 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
143 // Check for EDI Driver
144 if( Binary_FindSymbol(base, "driver_init", NULL ) == 0 )
146 Binary_Relocate(base); // Relocate
147 return Module_InitEDI( base ); // And intialise
151 // Unknown module type?, return error
154 Warning("Module_LoadMem: Module has neither a Module Info struct, nor an EDI entrypoint");
156 Warning("Module_LoadMem: Module does not have a Module Info struct");
161 // Check magic number
162 if(info->Magic != MODULE_MAGIC)
164 Warning("Module_LoadMem: Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
168 // Check Architecture
169 if(info->Arch != MODULE_ARCH_ID)
171 Warning("Module_LoadMem: Module is for a different architecture");
175 // Resolve Dependencies
176 if( !Module_int_ResolveDeps(info) ) {
181 Log("Initialising %p '%s' v%i.%i...",
184 info->Version>>8, info->Version & 0xFF
188 //if( info->Init( ArgString ) != 0 )
189 if( info->Init( NULL ) == 0 )
196 LOCK( &giModuleSpinlock );
197 info->Next = gLoadedModules;
198 gLoadedModules = info;
199 RELEASE( &giModuleSpinlock );
205 * \fn int Module_int_ResolveDeps(tModule *Info)
206 * \brief Resolves the dependencies
208 * \note Currently does not resolve the dependencies, just checks them
210 int Module_int_ResolveDeps(tModule *Info)
212 char **names = Info->Dependencies;
214 // Walk dependencies array
215 for( ; *names; names++ )
217 // Check if the module is loaded
218 if( !Module_IsLoaded(*names) ) {
219 Warning("Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
227 * \fn int Module_IsLoaded(char *Name)
228 * \brief Checks if a module is loaded
229 * \param Name Name of module to find
231 int Module_IsLoaded(char *Name)
233 tModule *mod = gLoadedModules;
236 for( ; mod; mod = mod->Next )
238 // If found, return true
239 if(strcmp(mod->Name, Name) == 0)
242 // not found - return false