3 * Architecture Independent System Init
10 #define N_MAX_ARGS BITS
26 const char *Name; // Name
27 int MinArgs; // Minimum number of arguments
28 int MaxArgs; // Maximum number of arguments
29 Uint IntArgs; // Bitmap of arguments that should be treated as integers
31 const char *OptDefaults[N_MAX_ARGS]; // Default values for optional arguments
35 extern void Arch_LoadBootModules(void);
36 extern int Modules_LoadBuiltins(void);
37 extern void Modules_SetBuiltinParams(char *Name, char *ArgString);
38 extern void Debug_SetKTerminal(const char *File);
41 void System_Init(char *Commandline);
42 void System_ParseCommandLine(char *ArgString);
43 void System_ExecuteCommandLine(void);
44 void System_ParseVFS(char *Arg);
45 void System_ParseModuleArgs(char *Arg);
46 void System_ParseSetting(char *Arg);
47 void System_ExecuteScript(void);
48 tConfigFile *System_Int_ParseFile(char *File);
51 enum eConfigCommands {
61 const tConfigCommand caConfigCommands[] = {
62 {"module", 1,2, 00, CC_LOADMODULE, {"",NULL}}, // Load a module from a file
63 {"spawn", 1,1, 00, CC_SPAWN, {NULL}}, // Spawn a process
65 {"mount", 3,4, 00, CC_MOUNT, {"",0}}, // Mount a device
66 {"symlink", 2,2, 00, CC_SYMLINK, {0}}, // Create a Symbolic Link
67 {"mkdir", 1,1, 00, CC_MKDIR, {0}}, // Create a Directory
68 {"open", 1,2, 00, CC_OPEN, {(void*)VFS_OPENFLAG_READ,0}}, // Open a file
69 {"close", 1,1, 01, CC_CLOSE, {0}}, // Close an open file
70 {"ioctl", 3,3, 03, CC_IOCTL, {0}}, // Call an IOCtl
74 #define NUM_CONFIG_COMMANDS (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
77 const char *gsConfigScript = "/Acess/Conf/BootConf.cfg";
82 void System_Init(char *CommandLine)
84 // Parse Kernel's Command Line
85 System_ParseCommandLine(CommandLine);
88 Log_Log("Config", "Initialising builtin modules...");
89 Modules_LoadBuiltins();
90 Arch_LoadBootModules();
92 System_ExecuteCommandLine();
94 // - Execute the Config Script
95 Log_Log("Config", "Executing config script '%s'", gsConfigScript);
96 System_ExecuteScript();
98 // Set the debug to be echoed to the terminal
99 Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
100 Debug_SetKTerminal("/Devices/VTerm/7");
104 * \fn void System_ParseCommandLine(char *ArgString)
105 * \brief Parses the kernel's command line and sets the environment
107 void System_ParseCommandLine(char *ArgString)
112 Log_Log("Config", "Kernel Invocation (%p) \"%s\"", ArgString, ArgString);
114 // --- Get Arguments ---
116 for( argc = 0; argc < 32; argc++ )
119 while(*str == ' ') str++;
120 // Check for the end of the string
121 if(*str == '\0') { argc--; break;}
124 while(*str && !(*str == '"' && str[-1] != '\\'))
128 while(*str && *str != ' ')
131 if(*str == '\0') break; // Check for EOS
132 *str = '\0'; // Cap off argument string
133 str ++; // and increment the string pointer
136 argc ++; // Count last argument
138 // --- Parse Arguments (Pass 1) ---
139 for( i = 0; i < argc; i++ )
144 // Ignored on this pass
148 // --- Module Paramaters ---
149 // -VTerm:Width=640,Height=480,Scrollback=2
151 System_ParseModuleArgs( argv[i] );
153 // --- Config Options ---
154 // SCRIPT=/Acess/Conf/BootConf.cfg
156 System_ParseSetting( argv[i] );
162 void System_ExecuteCommandLine(void)
165 for( i = 0; i < argc; i++ )
167 Log("argv[%i] = '%s'", i, argv[i]);
171 // Mount /System=ext2:/Devices/ATA/A1
172 // Symlink /Acess=/System/Acess2
174 System_ParseVFS( argv[i] );
181 * \fn void System_ParseVFS(char *Arg)
183 void System_ParseVFS(char *Arg)
189 // Search for the '=' token
190 while( *value && *value != '=' )
193 // Check if the equals was found
194 if( *value == '\0' ) {
195 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
200 *value = '\0'; value ++;
202 // Check assignment type
203 // - Symbolic Link <link>=<destination>
206 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
207 VFS_Symlink(Arg, value);
209 // - Mount <mountpoint>=<fs>:<device>
214 while(*dev && *dev != ':') dev++;
220 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
221 Log_Log("Config", "Creating directory '%s'", Arg, value);
227 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
228 VFS_Mount(dev, Arg, value, "");
233 * \brief Parse a module argument string
234 * \param Arg Argument string
236 void System_ParseModuleArgs(char *Arg)
244 // Find the start of the args
245 i = strpos(name, ':');
247 Log_Warning("Config", "Module spec with no arguments");
260 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
261 Modules_SetBuiltinParams(name, args);
265 * \fn void System_ParseSetting(char *Arg)
267 void System_ParseSetting(char *Arg)
272 // Search for the '=' token
273 while( *value && *value != '=' )
276 // Check for boolean/flag (no '=')
279 //if(strcmp(Arg, "") == 0) {
281 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
286 *value = '\0'; // Remove '='
287 value ++; // and eat it's position
289 if(strcmp(Arg, "SCRIPT") == 0) {
290 Log_Log("Config", "Config Script: '%s'", value);
291 if(strlen(value) == 0)
292 gsConfigScript = NULL;
294 gsConfigScript = value;
296 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
303 * \fn void System_ExecuteScript()
304 * \brief Reads and parses the boot configuration script
306 void System_ExecuteScript(void)
313 int variables[N_VARIABLES];
314 int bReplaced[N_MAX_ARGS];
321 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
323 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
328 VFS_Seek(fp, 0, SEEK_END);
330 Log_Debug("System", "VFS_Tell(%i) = %i", fp, fLen);
331 VFS_Seek(fp, 0, SEEK_SET);
332 // Read into memory buffer
333 fData = malloc(fLen+1);
334 VFS_Read(fp, fLen, fData);
340 file = System_Int_ParseFile(fData);
343 for( i = 0; i < file->nLines; i++ )
345 line = &file->Lines[i];
346 if( line->nParts == 0 ) continue; // Skip blank
348 if(line->Parts[0][0] == ':') continue; // Ignore labels
350 // Prescan and eliminate variables
351 for( j = 1; j < line->nParts; j++ )
353 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
355 if( line->Parts[j][0] != '$' ) continue;
356 if( line->Parts[j][1] == '?' ) {
360 val = atoi( &line->Parts[j][1] );
361 if( val < 0 || val > N_VARIABLES ) continue;
362 val = variables[ val ];
364 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
365 line->Parts[j] = malloc( BITS/8+2+1 );
366 sprintf(line->Parts[j], "0x%x", val);
370 // Find the command name
371 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
373 const char *args[N_MAX_ARGS];
375 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
377 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
379 // Check against minimum argument count
380 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
381 Log_Warning("Config",
382 "Configuration command '%s' requires at least %i arguments, %i given",
383 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
388 // Check for extra arguments
389 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
390 Log_Warning("Config",
391 "Configuration command '%s' takes at most %i arguments, %i given",
392 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
398 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
399 args[k] = caConfigCommands[j].OptDefaults[k];
402 // Convert arguments to integers
403 for( k = line->nParts-1; k--; )
405 if( k < 32 && (caConfigCommands[j].IntArgs & (1 << k)) ) {
406 args[k] = (const char *)(Uint)atoi(line->Parts[k+1]);
409 args[k] = (char *)line->Parts[k+1];
411 Log_Debug("Config", "args[%i] = %p", k, args[k]);
413 switch( (enum eConfigCommands) caConfigCommands[j].Index )
416 result = Module_LoadFile( args[0], args[1] );
419 result = Proc_Spawn( args[0] );
422 result = VFS_Mount( args[0], args[1], args[2], args[3] );
425 result = VFS_Symlink( args[0], args[1] );
428 result = VFS_Open( args[0], (Uint)args[1] );
431 VFS_Close( (Uint)args[0] );
435 result = VFS_MkDir( args[0] );
438 result = VFS_IOCtl( (Uint)args[0], (Uint)args[1], (void *)args[2] );
441 Log_Debug("Config", "result = %i", result);
444 if( j < NUM_CONFIG_COMMANDS ) continue;
446 // --- State and Variables ---
447 if(strcmp(line->Parts[0], "set") == 0)
450 if( line->nParts-1 != 2 ) {
451 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
456 to = atoi(line->Parts[1]);
457 value = atoi(line->Parts[2]);
459 variables[to] = value;
462 // if <val1> <op> <val2> <dest>
463 else if(strcmp(line->Parts[0], "if") == 0)
465 if( line->nParts-1 != 4 ) {
466 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
470 result = atoi(line->Parts[1]);
471 val = atoi(line->Parts[3]);
473 jmpTarget = line->Parts[4];
475 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
476 result, line->Parts[2], val, jmpTarget);
478 if( strcmp(line->Parts[2], "<" ) == 0 ) {
479 if( result < val ) goto jumpToLabel;
481 else if( strcmp(line->Parts[2], "<=") == 0 ) {
482 if( result <= val ) goto jumpToLabel;
484 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
485 if (result > val ) goto jumpToLabel;
487 else if( strcmp(line->Parts[2], ">=") == 0 ) {
488 if( result >= val ) goto jumpToLabel;
490 else if( strcmp(line->Parts[2], "=") == 0 ) {
491 if( result == val ) goto jumpToLabel;
493 else if( strcmp(line->Parts[2], "!=") == 0 ) {
494 if( result != val ) goto jumpToLabel;
497 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
501 else if(strcmp(line->Parts[0], "goto") == 0) {
502 if( line->nParts-1 != 1 ) {
503 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
506 jmpTarget = line->Parts[1];
509 for( j = 0; j < file->nLines; j ++ )
511 if(file->Lines[j].nParts == 0)
513 if(file->Lines[j].Parts[0][0] != ':')
515 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
518 if( j == file->nLines )
519 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
524 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
531 // Clean up after ourselves
532 for( i = 0; i < file->nLines; i++ ) {
533 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
534 for( j = 0; j < file->Lines[i].nParts; j++ ) {
535 if(IsHeap(file->Lines[i].Parts[j]))
536 free(file->Lines[i].Parts[j]);
538 free( file->Lines[i].Parts );
547 * \brief Parses a config file
548 * \param FileData Read/Write buffer containing the config file data
550 * \return ::tConfigFile structure that represents the original contents
553 tConfigFile *System_Int_ParseFile(char *FileData)
561 ENTER("pFileData", FileData);
563 // Prescan and count the number of lines
564 for(ptr = FileData; *ptr; ptr++)
566 if(*ptr != '\n') continue;
568 if(ptr == FileData) {
574 if(ptr[-1] == '\\') continue;
579 LOG("nLines = %i", nLines);
581 // Ok so we have `nLines` lines, now to allocate our return
582 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
583 ret->nLines = nLines;
585 // Read the file for real
587 ptr = FileData, i = 0;
594 ret->Lines[i].nParts = 0;
595 ret->Lines[i].Parts = NULL;
600 // Read leading whitespace
601 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
604 if( *ptr == '\0' || *ptr == '\n' ) {
605 if(*ptr == '\n') ptr ++;
609 if( *ptr == '#' || *ptr == ';' ) {
610 while( *ptr && *ptr != '\n' ) ptr ++;
611 if(*ptr == '\n') ptr ++;
615 ret->Lines[i].nParts ++;
619 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
624 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
628 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
630 if( ret->Lines[i].nParts == 0 ) {
631 ret->Lines[i].Parts = NULL;
635 // Allocate part list
636 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
639 for( ptr = start, j = 0; ; j++ )
641 // Read leading whitespace
642 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
645 if( *ptr == '\0' || *ptr == '\n' ) {
646 if(*ptr == '\n') ptr ++;
650 if( *ptr == '#' || *ptr == ';' ) {
651 while( *ptr && *ptr != '\n' ) ptr ++;
652 if(*ptr == '\n') ptr ++;
656 ret->Lines[i].Parts[j] = ptr;
661 ret->Lines[i].Parts[j] = ptr;
662 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
667 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
671 // Break if we have reached NULL
673 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
678 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
682 *ptr = '\0'; // Cap off string
683 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
684 ptr ++; // And increment for the next round
688 if( i < ret->nLines ) {
689 ret->Lines[i].nParts = 0;
690 ret->Lines[i].Parts = NULL;
691 Log_Log("System", "Cleaning up final empty line");