X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fmodules.c;h=689487395cc95c7956095514eee0e43a25ad636d;hb=e939fc0ced4d445c24696636fe660dddbe035b1c;hp=37f5418bcaa3587755b93b4f4552acfa94b34ef5;hpb=bd8dc898108f10c0498f4dc5d0164a50b5ff2e5c;p=tpg%2Facess2.git diff --git a/Kernel/modules.c b/Kernel/modules.c index 37f5418b..68948739 100644 --- a/Kernel/modules.c +++ b/Kernel/modules.c @@ -2,7 +2,7 @@ * Acess2 * - Module Loader */ -#define DEBUG 0 +#define DEBUG 1 #include #include @@ -10,7 +10,9 @@ #define USE_UDI 0 // === PROTOTYPES === - int Modules_LoadBuiltins(void); + int Module_int_Initialise(tModule *Module, char *ArgString); +void Modules_LoadBuiltins(void); +void Modules_SetBuiltinParams(char *Name, char *ArgString); int Module_RegisterLoader(tModuleLoader *Loader); int Module_LoadMem(void *Buffer, Uint Length, char *ArgString); int Module_LoadFile(char *Path, char *ArgString); @@ -25,7 +27,7 @@ EXPORT(Module_RegisterLoader); extern int UDI_LoadDriver(void *Base); #endif extern void StartupPrint(char *Str); -extern tModule gKernelModules[]; +extern void gKernelModules; extern void gKernelModulesEnd; // === GLOBALS === @@ -34,6 +36,8 @@ tSpinlock glModuleSpinlock; tModule *gLoadedModules = NULL; tModuleLoader *gModule_Loaders = NULL; tModule *gLoadingModules = NULL; +tModule **gapBuiltinModules = NULL; +char **gasBuiltinModuleArgs; // === CODE === /** @@ -45,14 +49,34 @@ tModule *gLoadingModules = NULL; * \retval 0 Returned on success * \retval >0 Error code form the module's initialisation function */ -int Module_int_Initialise(tModule *Module) +int Module_int_Initialise(tModule *Module, char *ArgString) { int i, j; int ret; char **deps; + char **args; tModule *mod; ENTER("pModule", Module); + LOG("Module->Magic = 0x%x", Module->Magic); + if(Module->Magic != MODULE_MAGIC) { + Log_Warning( + "Module", + "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)", + Module, Module->Magic, MODULE_MAGIC + ); + LEAVE('i', MODULE_ERR_BADMODULE); + return MODULE_ERR_BADMODULE; + } + LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name); + + if(Module->Arch != MODULE_ARCH_ID) { + Log_Warning( + "Module", + "Module %p (%s) is for another architecture (%i)", + Module, Module->Name, Module->Arch + ); + } deps = Module->Dependencies; @@ -84,23 +108,28 @@ int Module_int_Initialise(tModule *Module) break; } if( mod ) { - Warning("[MOD ] Circular dependency detected"); + Log_Warning("Module", "Circular dependency detected (%s and %s)", + mod->Name, Module->Name); 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 ) + if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 ) break; } if( i == giNumBuiltinModules ) { - Warning("[MOD ] Dependency '%s' for module '%s' failed"); + Log_Warning("Module", "Dependency '%s' for module '%s' failed", + deps[j], Module->Name); return -1; } // Dependency is not loaded, so load it - ret = Module_int_Initialise( &gKernelModules[i] ); + ret = Module_int_Initialise( + gapBuiltinModules[i], + gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL + ); if( ret ) { // The only "ok" error is NOTNEEDED @@ -111,34 +140,43 @@ int Module_int_Initialise(tModule *Module) // All Dependencies OK? Initialise StartupPrint(Module->Name); - Log("[MOD ] Initialising %p '%s' v%i.%i...", + Log_Log("Module", "Initialising %p '%s' v%i.%i...", Module, Module->Name, Module->Version >> 8, Module->Version & 0xFF ); - ret = Module->Init(NULL); + if( ArgString ) + args = str_split( ArgString, ',' ); + else + args = NULL; + + ret = Module->Init(args); + + if(args) free(args); + + // Remove from loading list + gLoadingModules = gLoadingModules->Next; + if( ret != MODULE_ERR_OK ) { switch(ret) { case MODULE_ERR_MISC: - Warning("[MOD ] Unable to load, reason: Miscelanious"); + Log_Warning("Module", "Unable to load, reason: Miscelanious"); break; case MODULE_ERR_NOTNEEDED: - Warning("[MOD ] Unable to load, reason: Module not needed (probably hardware not found)"); + Log_Warning("Module", "Unable to load, reason: Module not needed"); break; case MODULE_ERR_MALLOC: - Warning("[MOD ] Unable to load, reason: Error in malloc/realloc/calloc, probably not good"); + Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good"); break; default: - Warning("[MOD ] Unable to load reason - Unknown code %i", ret); + Log_Warning("Module", "Unable to load reason - Unknown code %i", ret); break; } LEAVE_RET('i', ret); return ret; } - - // Remove from loading list - gLoadingModules = gLoadingModules->Next; + LOG("ret = %i", ret); // Add to loaded list LOCK( &glModuleSpinlock ); @@ -149,23 +187,92 @@ int Module_int_Initialise(tModule *Module) LEAVE_RET('i', 0); } +/** + * \brief Scans the builtin modules and creates an array of them + */ +void Modules_int_GetBuiltinArray(void) +{ + int i; + tModule *module; + + // Count + module = &gKernelModules; + i = 0; + while( (tVAddr)module < (tVAddr)&gKernelModulesEnd ) + { + if(module->Magic == MODULE_MAGIC) { + i ++; + module ++; + } + else + module = (void*)( (tVAddr)module + 4 ); + } + + // Create + giNumBuiltinModules = i; + gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) ); + gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) ); + + + // Fill + module = &gKernelModules; + i = 0; + while( (tVAddr)module < (tVAddr)&gKernelModulesEnd ) + { + if(module->Magic == MODULE_MAGIC) { + gapBuiltinModules[i] = module; + i ++; + module ++; + } + else + module = (void*)( (tVAddr)module + 4 ); + } +} + /** * \brief Initialises builtin modules */ -int Modules_LoadBuiltins() +void Modules_LoadBuiltins() { int i; - // Count modules - giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules; - giNumBuiltinModules /= sizeof(tModule); + if( !gapBuiltinModules ) + Modules_int_GetBuiltinArray(); for( i = 0; i < giNumBuiltinModules; i++ ) { - Module_int_Initialise( &gKernelModules[i] ); + Module_int_Initialise( + gapBuiltinModules[i], + (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL) + ); } - return 0; + if( gasBuiltinModuleArgs != NULL ) + free(gasBuiltinModuleArgs); +} + +/** + * \brief Sets the parameters for a builtin module + */ +void Modules_SetBuiltinParams(char *Name, char *ArgString) +{ + int i; + + if( gasBuiltinModuleArgs == NULL ) + { + Modules_int_GetBuiltinArray(); + } + + // I hate expensive scans + for( i = 0; i < giNumBuiltinModules; i++ ) + { + if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) { + gasBuiltinModuleArgs[i] = ArgString; + return ; + } + } + + Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name); } /** @@ -209,7 +316,7 @@ int Module_LoadFile(char *Path, char *ArgString) // Error check if(base == NULL) { - Warning("Module_LoadFile: Unable to load '%s'", Path); + Log_Warning("Module", "Module_LoadFile - Unable to load '%s'", Path); return 0; } @@ -235,9 +342,9 @@ int Module_LoadFile(char *Path, char *ArgString) // Unknown module type?, return error Binary_Unload(base); #if USE_EDI - Warning("Module_LoadFile: Module has neither a Module Info struct, nor an EDI entrypoint"); + Log_Warning("Module", "Module '%s' has neither a Module Info struct, nor an EDI entrypoint", Path); #else - Warning("Module_LoadFile: Module does not have a Module Info struct"); + Log_Warning("Module", "Module '%s' does not have a Module Info struct", Path); #endif return 0; } @@ -245,19 +352,19 @@ int Module_LoadFile(char *Path, char *ArgString) // Check magic number if(info->Magic != MODULE_MAGIC) { - Warning("Module_LoadFile: Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC); + Log_Warning("Module", "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_LoadFile: Module is for a different architecture"); + Log_Warning("Module", "Module is for a different architecture"); return 0; } #if 1 - if( Module_int_Initialise( info ) ) + if( Module_int_Initialise( info, ArgString ) ) { Binary_Unload(base); return 0; @@ -269,7 +376,7 @@ int Module_LoadFile(char *Path, char *ArgString) return 0; } - Log("Initialising %p '%s' v%i.%i...", + Log_Log("Module", "Initialising %p '%s' v%i.%i...", info, info->Name, info->Version>>8, info->Version & 0xFF @@ -308,7 +415,7 @@ int Module_int_ResolveDeps(tModule *Info) { // Check if the module is loaded if( !Module_IsLoaded(*names) ) { - Warning("Module `%s' requires `%s', which is not loaded\n", Info->Name, *names); + Log_Warning("Module", "Module `%s' requires `%s', which is not loaded\n", Info->Name, *names); return 0; } }