X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fmodules.c;h=5b5a906a100d2dee4ead24f24143694918a95c8d;hb=ca05044548d5c1de87c030d625a305731a6cc665;hp=b1d858c7d0d2672a39fe61e8460b815239e06759;hpb=77ed20ce9d7e25654215980d0f89e63b8dd366f0;p=tpg%2Facess2.git diff --git a/Kernel/modules.c b/Kernel/modules.c index b1d858c7..5b5a906a 100644 --- a/Kernel/modules.c +++ b/Kernel/modules.c @@ -2,16 +2,24 @@ * Acess2 * - Module Loader */ -#include +#define DEBUG 0 +#include #include +#define USE_EDI 0 +#define USE_UDI 1 + // === PROTOTYPES === + int Modules_LoadBuiltins(); int Module_LoadMem(void *Buffer, Uint Length, char *ArgString); int Module_LoadFile(char *Path, char *ArgString); int Module_int_ResolveDeps(tModule *Info); int Module_IsLoaded(char *Name); // === IMPORTS === +#if USE_UDI +extern int UDI_LoadDriver(void *Base); +#endif extern void StartupPrint(char *Str); extern tModule gKernelModules[]; extern void gKernelModulesEnd; @@ -20,91 +28,154 @@ extern void gKernelModulesEnd; int giNumBuiltinModules = 0; int giModuleSpinlock = 0; tModule *gLoadedModules = NULL; +tModuleLoader *gModule_Loaders = NULL; +tModule *gLoadingModules = NULL; // === CODE === -int Modules_LoadBuiltins() +/** + * \brief Initialises a module + * \param Module Pointer to the module header + * \return Zero on success, eModuleErrors or -1 on error + * \retval -1 Returned if a dependency fails, or a circular dependency + * exists. + * \retval 0 Returned on success + * \retval >0 Error code form the module's initialisation function + */ +int Module_int_Initialise(tModule *Module) { - int i, j, k; - int numToInit = 0; - Uint8 *baIsLoaded; + int i, j; + int ret; char **deps; + tModule *mod; - giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules; - giNumBuiltinModules /= sizeof(tModule); + ENTER("pModule", Module); - baIsLoaded = calloc( giNumBuiltinModules, sizeof(*baIsLoaded) ); + deps = Module->Dependencies; - // Pass 1 - Are the dependencies compiled in? - for( i = 0; i < giNumBuiltinModules; i++ ) + // Check if the module has been loaded + for( mod = gLoadedModules; mod; mod = mod->Next ) { - deps = gKernelModules[i].Dependencies; - if(deps) + if(mod == Module) LEAVE_RET('i', 0); + } + + // Add to the "loading" (prevents circular deps) + Module->Next = gLoadingModules; + gLoadingModules = Module; + + // Scan dependency list + for( j = 0; deps && deps[j]; j++ ) + { + // Check if the module is already loaded + for( mod = gLoadedModules; mod; mod = mod->Next ) { - for( j = 0; deps[j]; j++ ) - { - for( k = 0; k < giNumBuiltinModules; k++ ) { - if(strcmp(deps[j], gKernelModules[k].Name) == 0) - break; - } - if(k == giNumBuiltinModules) { - Warning("Unable to find dependency '%s' for '%s' in kernel", - deps[j], gKernelModules[i].Name); - - baIsLoaded[i] = -1; // Don't Load - break; - } - } + if(strcmp(deps[j], mod->Name) == 0) + break; + } + if( mod ) continue; // Dependency is loaded, check the rest + + // Ok, check if it's loading + for( mod = gLoadingModules->Next; mod; mod = mod->Next ) + { + if(strcmp(deps[j], mod->Name) == 0) + break; + } + if( mod ) { + Warning("[MOD ] Circular dependency detected"); + LEAVE_RET('i', -1); + } + + // So, if it's not loaded, we better load it then + for( i = 0; i < giNumBuiltinModules; i ++ ) + { + if( strcmp(deps[j], gKernelModules[i].Name) == 0 ) + break; + } + if( i == giNumBuiltinModules ) { + Warning("[MOD ] Dependency '%s' for module '%s' failed"); + return -1; + } + + // Dependency is not loaded, so load it + ret = Module_int_Initialise( &gKernelModules[i] ); + if( ret ) + { + // The only "ok" error is NOTNEEDED + if(ret != MODULE_ERR_NOTNEEDED) + LEAVE_RET('i', -1); } - numToInit ++; } - // Pass 2 - Intialise - while(numToInit) - { - for( i = 0; i < giNumBuiltinModules; i++ ) + // All Dependencies OK? Initialise + StartupPrint(Module->Name); + Log("[MOD ] Initialising %p '%s' v%i.%i...", + Module, Module->Name, + Module->Version >> 8, Module->Version & 0xFF + ); + + ret = Module->Init(NULL); + if( ret != MODULE_ERR_OK ) { + Log("[MOD ] Loading Failed, all modules that depend on this will also fail"); + switch(ret) { - if( baIsLoaded[i] ) continue; // Ignore already loaded modules - - if( deps ) - { - for( j = 0; deps[j]; j++ ) - { - for( k = 0; k < giNumBuiltinModules; k++ ) { - if(strcmp(deps[j], gKernelModules[k].Name) == 0) - break; - } - // `k` is assumed to be less than `giNumBuiltinModules` - - // If a dependency failed, skip and mark as failed - if( baIsLoaded[k] == -1 ) { - baIsLoaded[i] = -1; - numToInit --; - break; - } - // If a dependency is not intialised, skip - if( !baIsLoaded[k] ) break; - } - // Check if we broke out - if( deps[j] ) continue; - } - - // All Dependencies OK? Initialise - StartupPrint(gKernelModules[i].Name); - Log("Initialising %p '%s' v%i.%i...", - &gKernelModules[i], - gKernelModules[i].Name, - gKernelModules[i].Version>>8, gKernelModules[i].Version & 0xFF - ); - gKernelModules[i].Init(NULL); - // Mark as loaded - baIsLoaded[i] = 1; - numToInit --; + case MODULE_ERR_MISC: + Log("[MOD ] Reason: Miscelanious"); + break; + case MODULE_ERR_NOTNEEDED: + Log("[MOD ] Reason: Module not needed (probably hardware not found)"); + break; + case MODULE_ERR_MALLOC: + Log("[MOD ] Reason: Error in malloc/realloc/calloc, probably not good"); + break; + default: + Log("[MOD ] Reason - Unknown code %i", ret); + break; } + LEAVE_RET('i', ret); + } + + // Remove from loading list + gLoadingModules = gLoadingModules->Next; + + // Add to loaded list + Module->Next = gLoadedModules; + gLoadedModules = Module; + + LEAVE_RET('i', 0); +} + +/** + * \brief Initialises builtin modules + */ +int Modules_LoadBuiltins() +{ + int i; + + // Count modules + giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules; + giNumBuiltinModules /= sizeof(tModule); + + for( i = 0; i < giNumBuiltinModules; i++ ) + { + Module_int_Initialise( &gKernelModules[i] ); } return 0; } +/** + * \brief Registers a tModuleLoader with the kernel + * \param Loader Pointer to loader structure (must be persistent) + */ +int Module_RegisterLoader(tModuleLoader *Loader) +{ + if(!Loader) return 1; + + Loader->Next = gModule_Loaders; + gModule_Loaders = Loader; + + return 0; +} + /** * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString) * \brief Load a module from a memory location @@ -113,7 +184,7 @@ int Module_LoadMem(void *Buffer, Uint Length, char *ArgString) { char path[VFS_MEMPATH_SIZE]; - VFS_GetMemPath(Buffer, Length, path); + VFS_GetMemPath(path, Buffer, Length); return Module_LoadFile( path, ArgString ); } @@ -131,16 +202,26 @@ int Module_LoadFile(char *Path, char *ArgString) base = Binary_LoadKernel(Path); // Error check - if(base == NULL) return 0; + if(base == NULL) { + Warning("Module_LoadFile: Unable to load '%s'", Path); + return 0; + } // Check for Acess Driver if( Binary_FindSymbol(base, "DriverInfo", (Uint*)&info ) == 0 ) { + tModuleLoader *tmp; + for( tmp = gModule_Loaders; tmp; tmp = tmp->Next) + { + if( tmp->Detector(base) == 0 ) continue; + + return tmp->Loader(base); + } + #if USE_EDI // Check for EDI Driver - if( Binary_FindSymbol(base, "driver_init", NULL ) == 0 ) + if( Binary_FindSymbol(base, "driver_init", NULL ) != 0 ) { - Binary_Relocate(base); // Relocate return Module_InitEDI( base ); // And intialise } #endif @@ -148,9 +229,9 @@ int Module_LoadFile(char *Path, char *ArgString) // Unknown module type?, return error Binary_Unload(base); #if USE_EDI - Warning("Module_LoadMem: Module has neither a Module Info struct, nor an EDI entrypoint"); + Warning("Module_LoadFile: Module has neither a Module Info struct, nor an EDI entrypoint"); #else - Warning("Module_LoadMem: Module does not have a Module Info struct"); + Warning("Module_LoadFile: Module does not have a Module Info struct"); #endif return 0; } @@ -158,14 +239,14 @@ int Module_LoadFile(char *Path, char *ArgString) // Check magic number if(info->Magic != MODULE_MAGIC) { - Warning("Module_LoadMem: Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC); + Warning("Module_LoadFile: Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC); return 0; } // Check Architecture if(info->Arch != MODULE_ARCH_ID) { - Warning("Module_LoadMem: Module is for a different architecture"); + Warning("Module_LoadFile: Module is for a different architecture"); return 0; }