X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fsystem.c;h=986734cd56b6cef686aceaa63f448fdfeb2faccc;hb=b7553e30c636f469ddceacbe541620cdab2dc433;hp=80af7d1c4d4d5503dfe55734a06bc245349ba572;hpb=a4ce2e60f783c9e71447edc03f20f937b8abf35a;p=tpg%2Facess2.git diff --git a/Kernel/system.c b/Kernel/system.c index 80af7d1c..986734cd 100644 --- a/Kernel/system.c +++ b/Kernel/system.c @@ -6,6 +6,9 @@ #define DEBUG 0 #include +#define N_VARIABLES 16 +#define N_MAX_ARGS BITS + // === TYPES === typedef struct { @@ -18,46 +21,83 @@ typedef struct int nLines; tConfigLine Lines[]; } tConfigFile; +typedef struct +{ + const char *Name; // Name + int MinArgs; // Minimum number of arguments + int MaxArgs; // Maximum number of arguments + Uint IntArgs; // Bitmap of arguments that should be treated as integers + int Index; // + const char *OptDefaults[N_MAX_ARGS]; // Default values for optional arguments +} tConfigCommand; // === IMPORTS === -extern int Modules_LoadBuiltins(); -extern int PCI_Install(); -extern void DMA_Install(); -extern void Debug_SetKTerminal(char *File); -extern void StartupPrint(char *Str); +extern void Arch_LoadBootModules(void); +extern int Modules_LoadBuiltins(void); +extern void Modules_SetBuiltinParams(char *Name, char *ArgString); +extern void Debug_SetKTerminal(const char *File); // === PROTOTYPES === -void System_Init(char *ArgString); +void System_Init(char *Commandline); void System_ParseCommandLine(char *ArgString); +void System_ExecuteCommandLine(void); void System_ParseVFS(char *Arg); +void System_ParseModuleArgs(char *Arg); void System_ParseSetting(char *Arg); -void System_ExecuteScript(); +void System_ExecuteScript(void); tConfigFile *System_Int_ParseFile(char *File); +// === CONSTANTS === +enum eConfigCommands { + CC_LOADMODULE, + CC_SPAWN, + CC_MOUNT, + CC_SYMLINK, + CC_MKDIR, + CC_OPEN, + CC_CLOSE, + CC_IOCTL +}; +const tConfigCommand caConfigCommands[] = { + {"module", 1,2, 00, CC_LOADMODULE, {"",NULL}}, // Load a module from a file + {"spawn", 1,1, 00, CC_SPAWN, {NULL}}, // Spawn a process + // --- VFS --- + {"mount", 3,4, 00, CC_MOUNT, {"",0}}, // Mount a device + {"symlink", 2,2, 00, CC_SYMLINK, {0}}, // Create a Symbolic Link + {"mkdir", 1,1, 00, CC_MKDIR, {0}}, // Create a Directory + {"open", 1,2, 00, CC_OPEN, {(void*)VFS_OPENFLAG_READ,0}}, // Open a file + {"close", 1,1, 01, CC_CLOSE, {0}}, // Close an open file + {"ioctl", 3,3, 03, CC_IOCTL, {0}}, // Call an IOCtl + + {"", 0,0, 0, 0, {0}} +}; +#define NUM_CONFIG_COMMANDS (sizeof(caConfigCommands)/sizeof(caConfigCommands[0])) + // === GLOBALS === -char *gsConfigScript = "/Acess/Conf/BootConf.cfg"; +const char *gsConfigScript = "/Acess/Conf/BootConf.cfg"; +char *argv[32]; + int argc; // === CODE === -void System_Init(char *ArgString) +void System_Init(char *CommandLine) { - // - Start Builtin Drivers & Filesystems - StartupPrint("Scanning PCI Bus..."); - PCI_Install(); - StartupPrint("Loading DMA..."); - DMA_Install(); - StartupPrint("Loading staticly compiled modules..."); - Modules_LoadBuiltins(); + // Parse Kernel's Command Line + System_ParseCommandLine(CommandLine); - // Set the debug to be echoed to the terminal - StartupPrint("Kernel now echoes to VT6 (Ctrl-Alt-F7)"); - Debug_SetKTerminal("/Devices/VTerm/6"); + // Initialise modules + Log_Log("Config", "Initialising builtin modules..."); + Modules_LoadBuiltins(); + Arch_LoadBootModules(); - // - Parse Kernel's Command Line - System_ParseCommandLine(ArgString); + System_ExecuteCommandLine(); // - Execute the Config Script - Log("Executing config script..."); + Log_Log("Config", "Executing config script '%s'", gsConfigScript); System_ExecuteScript(); + + // Set the debug to be echoed to the terminal + Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)"); + Debug_SetKTerminal("/Devices/VTerm/7"); } /** @@ -66,42 +106,74 @@ void System_Init(char *ArgString) */ void System_ParseCommandLine(char *ArgString) { - char *argv[32]; - int argc; int i; char *str; - Log("Kernel Command Line: \"%s\"", ArgString); + Log_Log("Config", "Kernel Invocation (%p) \"%s\"", ArgString, ArgString); // --- Get Arguments --- str = ArgString; for( argc = 0; argc < 32; argc++ ) { - while(*str == ' ') str++; // Eat Whitespace - if(*str == '\0') { argc--; break;} // End of string + // Eat Whitespace + while(*str == ' ') str++; + // Check for the end of the string + if(*str == '\0') { argc--; break;} argv[argc] = str; - while(*str && *str != ' ') - { - /*if(*str == '"') { - while(*str && !(*str == '"' && str[-1] != '\\')) - str ++; - }*/ - str++; + if(*str == '"') { + while(*str && !(*str == '"' && str[-1] != '\\')) + str ++; } - if(*str == '\0') break; // End of string + else { + while(*str && *str != ' ') + str++; + } + if(*str == '\0') break; // Check for EOS *str = '\0'; // Cap off argument string str ++; // and increment the string pointer } if(argc < 32) argc ++; // Count last argument - // --- Parse Arguments --- - for( i = 1; i < argc; i++ ) + // --- Parse Arguments (Pass 1) --- + for( i = 0; i < argc; i++ ) { - if( argv[i][0] == '/' ) - System_ParseVFS( argv[i] ); - else + switch(argv[i][0]) + { + // --- VFS --- + // Ignored on this pass + case '/': + break; + + // --- Module Paramaters --- + // -VTerm:Width=640,Height=480,Scrollback=2 + case '-': + System_ParseModuleArgs( argv[i] ); + break; + // --- Config Options --- + // SCRIPT=/Acess/Conf/BootConf.cfg + default: System_ParseSetting( argv[i] ); + break; + } + } +} + +void System_ExecuteCommandLine(void) +{ + int i; + for( i = 0; i < argc; i++ ) + { + LOG("argv[%i] = '%s'", i, argv[i]); + switch(argv[i][0]) + { + // --- VFS --- + // Mount /System=ext2:/Devices/ATA/A1 + // Symlink /Acess=/System/Acess2 + case '/': + System_ParseVFS( argv[i] ); + break; + } } } @@ -120,7 +192,7 @@ void System_ParseVFS(char *Arg) // Check if the equals was found if( *value == '\0' ) { - Warning("Expected '=' in the string '%s'", Arg); + Log_Warning("Config", "Expected '=' in the string '%s'", Arg); return ; } @@ -131,7 +203,7 @@ void System_ParseVFS(char *Arg) // - Symbolic Link = if(value[0] == '/') { - Log("Symbolic link '%s' pointing to '%s'", Arg, value); + Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value); VFS_Symlink(Arg, value); } // - Mount =: @@ -146,17 +218,49 @@ void System_ParseVFS(char *Arg) } // Create Mountpoint if( (fd = VFS_Open(Arg, 0)) == -1 ) { - Log("Creating directory '%s'", Arg, value); + Log_Log("Config", "Creating directory '%s'", Arg, value); VFS_MkDir( Arg ); } else { VFS_Close(fd); } // Mount - Log("Mounting '%s' to '%s' ('%s')", dev, Arg, value); + Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value); VFS_Mount(dev, Arg, value, ""); } } +/** + * \brief Parse a module argument string + * \param Arg Argument string + */ +void System_ParseModuleArgs(char *Arg) +{ + char *name, *args; + int i; + + // Remove '-' + name = Arg + 1; + + // Find the start of the args + i = strpos(name, ':'); + if( i == -1 ) { + Log_Warning("Config", "Module spec with no arguments"); + #if 1 + return ; + #else + i = strlen(name); + args = name + i; + #endif + } + else { + name[i] = '\0'; + args = name + i + 1; + } + + Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args); + Modules_SetBuiltinParams(name, args); +} + /** * \fn void System_ParseSetting(char *Arg) */ @@ -172,10 +276,10 @@ void System_ParseSetting(char *Arg) // Check for boolean/flag (no '=') if(*value == '\0') { - if(strcmp(Arg, "") == 0) { - } else { - Warning("Kernel flag '%s' is not recognised", Arg); - } + //if(strcmp(Arg, "") == 0) { + //} else { + Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg); + //} } else { @@ -183,10 +287,13 @@ void System_ParseSetting(char *Arg) value ++; // and eat it's position if(strcmp(Arg, "SCRIPT") == 0) { - Log("Config Script: '%s'", value); - gsConfigScript = value; + Log_Log("Config", "Config Script: '%s'", value); + if(strlen(value) == 0) + gsConfigScript = NULL; + else + gsConfigScript = value; } else { - Warning("Kernel config setting '%s' is not recognised", Arg); + Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg); } } @@ -194,120 +301,230 @@ void System_ParseSetting(char *Arg) /** * \fn void System_ExecuteScript() + * \brief Reads and parses the boot configuration script */ -void System_ExecuteScript() +void System_ExecuteScript(void) { int fp; int fLen = 0; - int i; + int i, j, k; + int val; + int result = 0; + int variables[N_VARIABLES]; + int bReplaced[N_MAX_ARGS]; char *fData; + char *jmpTarget; tConfigFile *file; tConfigLine *line; + ENTER(""); + // Open Script fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ); if(fp == -1) { - Warning("[CFG] Passed script '%s' does not exist", gsConfigScript); + Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript); + LEAVE('-'); return; } - // Read into memory buffer + // Get length VFS_Seek(fp, 0, SEEK_END); fLen = VFS_Tell(fp); + LOG("VFS_Tell(0x%x) = %i", fp, fLen); VFS_Seek(fp, 0, SEEK_SET); + // Read into memory buffer fData = malloc(fLen+1); VFS_Read(fp, fLen, fData); fData[fLen] = '\0'; VFS_Close(fp); - // Parse File file = System_Int_ParseFile(fData); - // Loop lines + // Parse each line for( i = 0; i < file->nLines; i++ ) { line = &file->Lines[i]; if( line->nParts == 0 ) continue; // Skip blank - // Mount - if( strcmp(line->Parts[0], "mount") == 0 ) { - if( line->nParts != 4 ) { - Warning("Configuration command 'mount' requires 3 arguments, %i given", - line->nParts-1); - continue; + if(line->Parts[0][0] == ':') continue; // Ignore labels + + // Prescan and eliminate variables + for( j = 1; j < line->nParts; j++ ) + { + LOG("Arg #%i is '%s'", j, line->Parts[j]); + bReplaced[j] = 0; + if( line->Parts[j][0] != '$' ) continue; + if( line->Parts[j][1] == '?' ) { + val = result; } - //Log("[CFG ] Mount '%s' to '%s' (%s)", - // line->Parts[1], line->Parts[2], line->Parts[3]); - //! \todo Use an optional 4th argument for the options string - VFS_Mount(line->Parts[1], line->Parts[2], line->Parts[3], ""); - } - // Module - else if(strcmp(line->Parts[0], "module") == 0) { - if( line->nParts < 2 || line->nParts > 3 ) { - Warning("Configuration command 'module' requires 1 or 2 arguments, %i given", - line->nParts-1); - continue; + else { + val = atoi( &line->Parts[j][1] ); + if( val < 0 || val > N_VARIABLES ) continue; + val = variables[ val ]; } - if( line->nParts == 3 ) - Module_LoadFile(line->Parts[1], line->Parts[2]); - else - Module_LoadFile(line->Parts[1], ""); + LOG("Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val); + line->Parts[j] = malloc( BITS/8+2+1 ); + sprintf(line->Parts[j], "0x%x", val); + bReplaced[j] = 1; } - // UDI Module - else if(strcmp(line->Parts[0], "udimod") == 0) { - if( line->nParts != 2 ) { - Warning("Configuration command 'udimod' requires 1 argument, %i given", - line->nParts-1); - continue; + + // Find the command name + for( j = 0; j < NUM_CONFIG_COMMANDS; j++ ) + { + const char *args[N_MAX_ARGS]; + + if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue; + + Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1); + + // Check against minimum argument count + if( line->nParts - 1 < caConfigCommands[j].MinArgs ) { + Log_Warning("Config", + "Configuration command '%s' requires at least %i arguments, %i given", + caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1 + ); + break; } - Log("[CFG ] Load UDI Module '%s'", line->Parts[1]); - Module_LoadFile(line->Parts[1], ""); - } - // EDI Module - else if(strcmp(line->Parts[0], "edimod") == 0) { - if( line->nParts != 2 ) { - Warning("Configuration command 'edimod' requires 1 argument, %i given", - line->nParts-1); - continue; + + // Check for extra arguments + if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) { + Log_Warning("Config", + "Configuration command '%s' takes at most %i arguments, %i given", + caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1 + ); + break; + } + + // Fill in defaults + for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) { + args[k] = caConfigCommands[j].OptDefaults[k]; + } + + // Convert arguments to integers + for( k = line->nParts-1; k--; ) + { + if( k < 32 && (caConfigCommands[j].IntArgs & (1 << k)) ) { + args[k] = (const char *)(Uint)atoi(line->Parts[k+1]); + } + else { + args[k] = (char *)line->Parts[k+1]; + } + LOG("args[%i] = %p", k, args[k]); + } + switch( (enum eConfigCommands) caConfigCommands[j].Index ) + { + case CC_LOADMODULE: + result = Module_LoadFile( args[0], args[1] ); + break; + case CC_SPAWN: + result = Proc_Spawn( args[0] ); + break; + case CC_MOUNT: + result = VFS_Mount( args[0], args[1], args[2], args[3] ); + break; + case CC_SYMLINK: + result = VFS_Symlink( args[0], args[1] ); + break; + case CC_OPEN: + result = VFS_Open( args[0], (Uint)args[1] ); + break; + case CC_CLOSE: + VFS_Close( (Uint)args[0] ); + result = 0; + break; + case CC_MKDIR: + result = VFS_MkDir( args[0] ); + break; + case CC_IOCTL: + result = VFS_IOCtl( (Uint)args[0], (Uint)args[1], (void *)args[2] ); + break; } - Log("[CFG ] Load EDI Module '%s'", line->Parts[1]); - Module_LoadFile(line->Parts[1], ""); + LOG("Config", "result = %i", result); + break; } - // Symbolic Link - else if(strcmp(line->Parts[0], "symlink") == 0) { - if( line->nParts != 3 ) { - Warning("Configuration command 'symlink' requires 2 arguments, %i given", + if( j < NUM_CONFIG_COMMANDS ) continue; + + // --- State and Variables --- + if(strcmp(line->Parts[0], "set") == 0) + { + int to, value; + if( line->nParts-1 != 2 ) { + Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given", line->nParts-1); continue; } - Log("[CFG ] Symlink '%s' pointing to '%s'", - line->Parts[1], line->Parts[2]); - VFS_Symlink(line->Parts[1], line->Parts[2]); + + to = atoi(line->Parts[1]); + value = atoi(line->Parts[2]); + + variables[to] = value; + result = value; } - // Create Directory - else if(strcmp(line->Parts[0], "mkdir") == 0) { - if( line->nParts != 2 ) { - Warning("Configuration command 'mkdir' requires 1 argument, %i given", + // if + else if(strcmp(line->Parts[0], "if") == 0) + { + if( line->nParts-1 != 4 ) { + Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given", line->nParts-1); - continue; } - Log("[CFG ] New Directory '%s'", line->Parts[1]); - VFS_MkDir(line->Parts[1]); + + result = atoi(line->Parts[1]); + val = atoi(line->Parts[3]); + + jmpTarget = line->Parts[4]; + + Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s", + result, line->Parts[2], val, jmpTarget); + + if( strcmp(line->Parts[2], "<" ) == 0 ) { + if( result < val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], "<=") == 0 ) { + if( result <= val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], ">" ) == 0 ) { + if (result > val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], ">=") == 0 ) { + if( result >= val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], "=") == 0 ) { + if( result == val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], "!=") == 0 ) { + if( result != val ) goto jumpToLabel; + } + else { + Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]); + } + } - // Spawn a process - else if(strcmp(line->Parts[0], "spawn") == 0) { - if( line->nParts != 2 ) { - Warning("Configuration command 'spawn' requires 1 argument, %i given", + else if(strcmp(line->Parts[0], "goto") == 0) { + if( line->nParts-1 != 1 ) { + Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given", line->nParts-1); - continue; } - Log("[CFG ] Starting '%s' as a new task", line->Parts[1]); - Proc_Spawn(line->Parts[1]); + jmpTarget = line->Parts[1]; + + jumpToLabel: + for( j = 0; j < file->nLines; j ++ ) + { + if(file->Lines[j].nParts == 0) + continue; + if(file->Lines[j].Parts[0][0] != ':') + continue; + if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0) + break; + } + if( j == file->nLines ) + Log_Warning("Config", "Unable to find label '%s'", jmpTarget); + else + i = j; } else { - Warning("Unknown configuration command '%s' on line %i", + Log_Warning("Config", "Unknown configuration command '%s' on line %i", line->Parts[0], line->TrueLine ); @@ -317,10 +534,18 @@ void System_ExecuteScript() // Clean up after ourselves for( i = 0; i < file->nLines; i++ ) { if( file->Lines[i].nParts == 0 ) continue; // Skip blank + for( j = 0; j < file->Lines[i].nParts; j++ ) { + if(IsHeap(file->Lines[i].Parts[j])) + free(file->Lines[i].Parts[j]); + } free( file->Lines[i].Parts ); } + + // Free data free( file ); free( fData ); + + LEAVE('-'); } /** @@ -330,7 +555,7 @@ void System_ExecuteScript() * \return ::tConfigFile structure that represents the original contents * of \a FileData */ -tConfigFile *System_Int_ParseFile(char *FileData) +tConfigFile *System_Int_ParseFile(char *FileData) { char *ptr; char *start; @@ -372,6 +597,7 @@ tConfigFile *System_Int_ParseFile(char *FileData) start = ptr; ret->Lines[i].nParts = 0; + ret->Lines[i].Parts = NULL; // Count parts for(;;) @@ -437,6 +663,7 @@ tConfigFile *System_Int_ParseFile(char *FileData) // Quoted if( *ptr == '"' ) { ptr ++; + ret->Lines[i].Parts[j] = ptr; while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' ) ptr++; } @@ -463,6 +690,12 @@ tConfigFile *System_Int_ParseFile(char *FileData) } } + if( i < ret->nLines ) { + ret->Lines[i].nParts = 0; + ret->Lines[i].Parts = NULL; + LOG("Cleaning up final empty line"); + } + LEAVE('p', ret); return ret; }