9 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString);
10 int Module_LoadFile(char *Path, char *ArgString);
11 int Module_int_ResolveDeps(tModule *Info);
12 int Module_IsLoaded(char *Name);
15 extern void StartupPrint(char *Str);
16 extern tModule gKernelModules[];
17 extern void gKernelModulesEnd;
20 int giNumBuiltinModules = 0;
21 int giModuleSpinlock = 0;
22 tModule *gLoadedModules = NULL;
25 int Modules_LoadBuiltins()
32 giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
33 giNumBuiltinModules /= sizeof(tModule);
35 baIsLoaded = calloc( giNumBuiltinModules, sizeof(*baIsLoaded) );
37 // Pass 1 - Are the dependencies compiled in?
38 for( i = 0; i < giNumBuiltinModules; i++ )
40 deps = gKernelModules[i].Dependencies;
43 for( j = 0; deps[j]; j++ )
45 for( k = 0; k < giNumBuiltinModules; k++ ) {
46 if(strcmp(deps[j], gKernelModules[k].Name) == 0)
49 if(k == giNumBuiltinModules) {
50 Warning("Unable to find dependency '%s' for '%s' in kernel",
51 deps[j], gKernelModules[i].Name);
53 baIsLoaded[i] = -1; // Don't Load
64 for( i = 0; i < giNumBuiltinModules; i++ )
66 if( baIsLoaded[i] ) continue; // Ignore already loaded modules
70 for( j = 0; deps[j]; j++ )
72 for( k = 0; k < giNumBuiltinModules; k++ ) {
73 if(strcmp(deps[j], gKernelModules[k].Name) == 0)
76 // `k` is assumed to be less than `giNumBuiltinModules`
78 // If a dependency failed, skip and mark as failed
79 if( baIsLoaded[k] == -1 ) {
84 // If a dependency is not intialised, skip
85 if( !baIsLoaded[k] ) break;
87 // Check if we broke out
88 if( deps[j] ) continue;
91 // All Dependencies OK? Initialise
92 StartupPrint(gKernelModules[i].Name);
93 Log("Initialising %p '%s' v%i.%i...",
95 gKernelModules[i].Name,
96 gKernelModules[i].Version>>8, gKernelModules[i].Version & 0xFF
98 gKernelModules[i].Init(NULL);
109 * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
110 * \brief Load a module from a memory location
112 int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
114 char path[VFS_MEMPATH_SIZE];
116 VFS_GetMemPath(Buffer, Length, path);
118 return Module_LoadFile( path, ArgString );
122 * \fn int Module_LoadFile(char *Path, char *ArgString)
123 * \brief Load a module from a file
125 int Module_LoadFile(char *Path, char *ArgString)
131 base = Binary_LoadKernel(Path);
134 if(base == NULL) return 0;
136 // Check for Acess Driver
137 if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 )
140 // Check for EDI Driver
141 if( Binary_FindSymbol(base, "driver_init", NULL ) == 0 )
143 Binary_Relocate(base); // Relocate
144 return Module_InitEDI( base ); // And intialise
148 // Unknown module type?, return error
151 Warning("Module_LoadMem: Module has neither a Module Info struct, nor an EDI entrypoint");
153 Warning("Module_LoadMem: Module does not have a Module Info struct");
158 // Check magic number
159 if(info->Magic != MODULE_MAGIC)
161 Warning("Module_LoadMem: Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
165 // Check Architecture
166 if(info->Arch != MODULE_ARCH_ID)
168 Warning("Module_LoadMem: Module is for a different architecture");
172 // Resolve Dependencies
173 if( !Module_int_ResolveDeps(info) ) {
179 //if( info->Init( ArgString ) != 0 )
180 if( info->Init( NULL ) == 0 )
187 LOCK( &giModuleSpinlock );
188 info->Next = gLoadedModules;
189 gLoadedModules = info;
190 RELEASE( &giModuleSpinlock );
196 * \fn int Module_int_ResolveDeps(tModule *Info)
197 * \brief Resolves the dependencies
199 * \note Currently does not resolve the dependencies, just checks them
201 int Module_int_ResolveDeps(tModule *Info)
203 char **names = Info->Dependencies;
205 // Walk dependencies array
206 for( ; *names; names++ )
208 // Check if the module is loaded
209 if( !Module_IsLoaded(*names) ) {
210 Warning("Module `%s' requires `%s', which is not loaded\n", Info->Name, *names);
218 * \fn int Module_IsLoaded(char *Name)
219 * \brief Checks if a module is loaded
220 * \param Name Name of module to find
222 int Module_IsLoaded(char *Name)
224 tModule *mod = gLoadedModules;
227 for( ; mod; mod = mod->Next )
229 // If found, return true
230 if(strcmp(mod->Name, Name) == 0)
233 // not found - return false