Changed the x86 architecture to have tPAddr be 64-bits always
[tpg/acess2.git] / Kernel / modules.c
index b7209ff..ba92905 100644 (file)
@@ -5,15 +5,23 @@
 #include <common.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;
+extern void    gKernelModulesEnd;
 
 // === GLOBALS ===
  int   giNumBuiltinModules = 0;
@@ -23,18 +31,88 @@ tModule     *gLoadedModules = NULL;
 // === CODE ===
 int Modules_LoadBuiltins()
 {
-        int    i;
+        int    i, j, k;
+        int    numToInit = 0;
+       Uint8   *baIsLoaded;
+       char    **deps;
+       
        giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
        giNumBuiltinModules /= sizeof(tModule);
        
+       baIsLoaded = calloc( giNumBuiltinModules, sizeof(*baIsLoaded) );
+       
+       // Pass 1 - Are the dependencies compiled in?
        for( i = 0; i < giNumBuiltinModules; i++ )
        {
-               Log("Initialising %p '%s' v%i.%i...",
-                       &gKernelModules[i],
-                       gKernelModules[i].Name,
-                       gKernelModules[i].Version>>8, gKernelModules[i].Version & 0xFF
-                       );
-               gKernelModules[i].Init(NULL);
+               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;
+                               }
+                               if(k == giNumBuiltinModules) {
+                                       Warning("Unable to find dependency '%s' for '%s' in kernel",
+                                               deps[j], gKernelModules[i].Name);
+                                       
+                                       baIsLoaded[i] = -1;     // Don't Load
+                                       break;
+                               }
+                       }
+               }
+               numToInit ++;
+       }
+       
+       // Pass 2 - Intialise
+       while(numToInit)
+       {
+               for( i = 0; i < giNumBuiltinModules; i++ )
+               {
+                       if( baIsLoaded[i] )     continue;       // Ignore already loaded modules
+               
+                       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
+                               );
+                       if( gKernelModules[i].Init(NULL) == 0 ) {
+                               Log("Loading Failed, all modules that depend on this will also fail");
+                               baIsLoaded[i] = -1;
+                       }
+                       // Mark as loaded
+                       else
+                               baIsLoaded[i] = 1;
+                       numToInit --;
+               }
        }
        
        return 0;
@@ -48,7 +126,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 );
 }
@@ -80,6 +158,14 @@ int Module_LoadFile(char *Path, char *ArgString)
                }
                #endif
                
+               #if USE_UDI
+               if( Binary_FindSymbol(base, "udi_init_info", NULL ) == 0 )
+               {
+                       Binary_Relocate(base);  // Relocate
+                       return UDI_LoadDriver( base );  // And intialise
+               }
+               #endif
+               
                // Unknown module type?, return error
                Binary_Unload(base);
                #if USE_EDI
@@ -110,6 +196,12 @@ int Module_LoadFile(char *Path, char *ArgString)
                return 0;
        }
        
+       Log("Initialising %p '%s' v%i.%i...",
+                               info,
+                               info->Name,
+                               info->Version>>8, info->Version & 0xFF
+                               );
+       
        // Call Initialiser
        //if( info->Init( ArgString ) != 0 )
        if( info->Init( NULL ) == 0 )

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