Changes to the module loader to handle specific errors from modules
[tpg/acess2.git] / Kernel / modules.c
index a4e639b..39c9966 100644 (file)
@@ -2,16 +2,23 @@
  * Acess2
  * - Module Loader
  */
-#include <common.h>
+#include <acess.h>
 #include <modules.h>
 
+#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,21 +27,25 @@ extern void gKernelModulesEnd;
  int   giNumBuiltinModules = 0;
  int   giModuleSpinlock = 0;
 tModule        *gLoadedModules = NULL;
+tModuleLoader  *gModule_Loaders = NULL;
 
 // === CODE ===
 int Modules_LoadBuiltins()
 {
         int    i, j, k;
+        int    ret;
         int    numToInit = 0;
        Uint8   *baIsLoaded;
        char    **deps;
        
+       // Count modules
        giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
        giNumBuiltinModules /= sizeof(tModule);
        
+       // Allocate loaded array
        baIsLoaded = calloc( giNumBuiltinModules, sizeof(*baIsLoaded) );
        
-       // Pass 1 - Are the dependencies compiled in?
+       // Pass 1 - Check for dependencies
        for( i = 0; i < giNumBuiltinModules; i++ )
        {
                deps = gKernelModules[i].Dependencies;
@@ -46,10 +57,11 @@ int Modules_LoadBuiltins()
                                        if(strcmp(deps[j], gKernelModules[k].Name) == 0)
                                                break;
                                }
-                               Log("%s requires %s\n", gKernelModules[i].Name, deps[j]);
                                if(k == giNumBuiltinModules) {
-                                       Warning("Unable to find dependency '%s' for '%s' in kernel",
-                                               deps[j], gKernelModules[i].Name);
+                                       Warning(
+                                               "[MOD  ] Dependency '%s' for module '%s' was not compiled in",
+                                               deps[j], gKernelModules[i].Name
+                                               );
                                        
                                        baIsLoaded[i] = -1;     // Don't Load
                                        break;
@@ -59,7 +71,7 @@ int Modules_LoadBuiltins()
                numToInit ++;
        }
        
-       // Pass 2 - Intialise
+       // Pass 2 - Intialise modules in order
        while(numToInit)
        {
                for( i = 0; i < giNumBuiltinModules; i++ )
@@ -77,35 +89,73 @@ int Modules_LoadBuiltins()
                                                        break;
                                        }
                                        // `k` is assumed to be less than `giNumBuiltinModules`
+                                       // We checked this in pass 1
                                        
-                                       Log("baIsLoaded[%i(%s)] = %i\n", k, deps[j], baIsLoaded[k]);
                                        // 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 a dependency is not intialised, skip this module
+                                       // and come back later
                                        if( !baIsLoaded[k] )    break;
                                }
-                               // Check if we broke out
+                               // Check for breakouts
                                if( deps[j] )   continue;
                        }
                        
                        // All Dependencies OK? Initialise
                        StartupPrint(gKernelModules[i].Name);
-                       Log("Initialising %p '%s' v%i.%i...",
+                       Log("[MOD  ] Initialising %p '%s' v%i.%i...",
                                &gKernelModules[i],
                                gKernelModules[i].Name,
                                gKernelModules[i].Version>>8, gKernelModules[i].Version & 0xFF
                                );
-                       gKernelModules[i].Init(NULL);
+                       
+                       ret = gKernelModules[i].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");
+                                       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;
+                               }
+                               baIsLoaded[i] = -1;
+                       }
                        // Mark as loaded
-                       baIsLoaded[i] = 1;
+                       else
+                               baIsLoaded[i] = 1;
                        numToInit --;
                }
        }
        
+       free(baIsLoaded);
+       
+       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;
 }
 
@@ -117,7 +167,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 );
 }
@@ -135,16 +185,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
@@ -152,9 +212,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;
        }
@@ -162,14 +222,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;
        }
        

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