- deps = gKernelModules[i].Dependencies;
-
- 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
+ // 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", "Initialising %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()
+{
+ int i;
+
+ if( !gapBuiltinModules )
+ Modules_int_GetBuiltinArray();
+
+ for( i = 0; i < giNumBuiltinModules; i++ )
+ {
+ Module_int_Initialise(
+ gapBuiltinModules[i],
+ (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
+ );
+ }
+
+ if( gasBuiltinModuleArgs != NULL )
+ free(gasBuiltinModuleArgs);
+}
+
+/**
+ * \brief Initialise a builtin module given it's name
+ *
+ * E.g. Used by VTerm to load an alternate video driver at runtime
+ */
+int Modules_InitialiseBuiltin(const char *Name)
+{
+ int i;
+
+ // Check if it's loaded
+ if( Module_IsLoaded(Name) )
+ return 0;
+
+ if( !gapBuiltinModules )
+ Modules_int_GetBuiltinArray();
+
+ for( i = 0; i < giNumBuiltinModules; i++ )
+ {
+ if( strcmp(gapBuiltinModules[i]->Name, Name) == 0 ) {
+ return Module_int_Initialise(gapBuiltinModules[i],
+ (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)