+/**
+ * \brief Initialises a module
+ * \param Module Pointer to the module header
+ * \param ArgString Comma separated list of module arguments
+ * \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, const char *ArgString)
+{
+ int i, j;
+ int ret;
+ const 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;
+
+ // Check if the module has been loaded
+ for( mod = gLoadedModules; mod; mod = mod->Next )
+ {
+ 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 )
+ {
+ 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 ) {
+ 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], gapBuiltinModules[i]->Name) == 0 )
+ break;
+ }
+ if( i == giNumBuiltinModules ) {
+ 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(
+ gapBuiltinModules[i],
+ gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
+ );
+ if( ret )
+ {
+ // The only "ok" error is NOTNEEDED
+ if(ret != MODULE_ERR_NOTNEEDED)
+ LEAVE_RET('i', -1);
+ }
+ }
+
+ // All Dependencies OK? Initialise
+ StartupPrint(Module->Name);
+ Log_Log("Module", "Starting %p '%s' v%i.%i",
+ Module, Module->Name,
+ Module->Version >> 8, Module->Version & 0xFF
+ );
+
+ 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:
+ Log_Warning("Module", "Unable to load, reason: Miscelanious");
+ break;
+ case MODULE_ERR_NOTNEEDED:
+ Log_Warning("Module", "Unable to load, reason: Module not needed");
+ break;
+ case MODULE_ERR_MALLOC:
+ Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
+ break;
+ default:
+ Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
+ break;
+ }
+ LEAVE_RET('i', ret);
+ return ret;
+ }
+ LOG("ret = %i", ret);
+
+ // Add to loaded list
+ SHORTLOCK( &glModuleSpinlock );
+ Module->Next = gLoadedModules;
+ gLoadedModules = Module;
+ SHORTREL( &glModuleSpinlock );
+
+ 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
+ */
+void Modules_LoadBuiltins()