Random fixes, disabled debug in vterm, random crash disappeared, also doing fixes...
[tpg/acess2.git] / Kernel / modules.c
index 5b5a906..68d5ba0 100644 (file)
@@ -7,15 +7,19 @@
 #include <modules.h>
 
 #define        USE_EDI 0
-#define        USE_UDI 1
+#define        USE_UDI 0
 
 // === PROTOTYPES ===
- int   Modules_LoadBuiltins();
+ int   Modules_LoadBuiltins(void);
+ int   Module_RegisterLoader(tModuleLoader *Loader);
  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);
 
+// === EXPORTS ===
+EXPORT(Module_RegisterLoader);
+
 // === IMPORTS ===
 #if USE_UDI
 extern int     UDI_LoadDriver(void *Base);
@@ -26,7 +30,7 @@ extern void   gKernelModulesEnd;
 
 // === GLOBALS ===
  int   giNumBuiltinModules = 0;
- int   giModuleSpinlock = 0;
+tSpinlock      glModuleSpinlock;
 tModule        *gLoadedModules = NULL;
 tModuleLoader  *gModule_Loaders = NULL;
 tModule        *gLoadingModules = NULL;
@@ -80,7 +84,7 @@ int Module_int_Initialise(tModule *Module)
                                break;
                }
                if( mod ) {
-                       Warning("[MOD  ] Circular dependency detected");
+                       Log_Warning("Module", "Circular dependency detected");
                        LEAVE_RET('i', -1);
                }
                
@@ -91,7 +95,7 @@ int Module_int_Initialise(tModule *Module)
                                break;
                }
                if( i == giNumBuiltinModules ) {
-                       Warning("[MOD  ] Dependency '%s' for module '%s' failed");
+                       Log_Warning("Module", "Dependency '%s' for module '%s' failed");
                        return -1;
                }
                
@@ -107,38 +111,41 @@ 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( ret != MODULE_ERR_OK ) {
-               Log("[MOD  ] Loading Failed, all modules that depend on this will also fail");
                switch(ret)
                {
                case MODULE_ERR_MISC:
-                       Log("[MOD  ] Reason: Miscelanious");
+                       Log_Warning("Module", "Unable to load, reason: Miscelanious");
                        break;
                case MODULE_ERR_NOTNEEDED:
-                       Log("[MOD  ] Reason: Module not needed (probably hardware not found)");
+                       Log_Warning("Module", "Unable to load, reason: Module not needed");
                        break;
                case MODULE_ERR_MALLOC:
-                       Log("[MOD  ] 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:
-                       Log("[MOD  ] Reason - Unknown code %i", ret);
+                       Log_Warning("Module", "Unable to load reason - Unknown code %i", ret);
                        break;
                }
                LEAVE_RET('i', ret);
+               return ret;
        }
+       LOG("ret = %i", ret);
        
        // Remove from loading list
        gLoadingModules = gLoadingModules->Next;
        
        // Add to loaded list
+       LOCK( &glModuleSpinlock );
        Module->Next = gLoadedModules;
        gLoadedModules = Module;
+       RELEASE( &glModuleSpinlock );
        
        LEAVE_RET('i', 0);
 }
@@ -203,7 +210,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;
        }
        
@@ -229,9 +236,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;
        }
@@ -239,24 +246,31 @@ 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 ) )
+       {
+               Binary_Unload(base);
+               return 0;
+       }
+       #else
        // Resolve Dependencies
        if( !Module_int_ResolveDeps(info) ) {
                Binary_Unload(base);
                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
@@ -271,10 +285,11 @@ int Module_LoadFile(char *Path, char *ArgString)
        }
        
        // Add to list
-       LOCK( &giModuleSpinlock );
+       LOCK( &glModuleSpinlock );
        info->Next = gLoadedModules;
        gLoadedModules = info;
-       RELEASE( &giModuleSpinlock );
+       RELEASE( &glModuleSpinlock );
+       #endif
        
        return 1;
 }
@@ -294,7 +309,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;
                }
        }

UCC git Repository :: git.ucc.asn.au