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];
323 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
325 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
331 VFS_Seek(fp, 0, SEEK_END);
333 LOG("VFS_Tell(0x%x) = %i", fp, fLen);
334 VFS_Seek(fp, 0, SEEK_SET);
335 // Read into memory buffer
336 fData = malloc(fLen+1);
337 VFS_Read(fp, fLen, fData);
343 file = System_Int_ParseFile(fData);
346 for( i = 0; i < file->nLines; i++ )
348 line = &file->Lines[i];
349 if( line->nParts == 0 ) continue; // Skip blank
351 if(line->Parts[0][0] == ':') continue; // Ignore labels
353 // Prescan and eliminate variables
354 for( j = 1; j < line->nParts; j++ )
356 LOG("Arg #%i is '%s'", j, line->Parts[j]);
358 if( line->Parts[j][0] != '$' ) continue;
359 if( line->Parts[j][1] == '?' ) {
363 val = atoi( &line->Parts[j][1] );
364 if( val < 0 || val > N_VARIABLES ) continue;
365 val = variables[ val ];
367 LOG("Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
368 line->Parts[j] = malloc( BITS/8+2+1 );
369 sprintf(line->Parts[j], "0x%x", val);
373 // Find the command name
374 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
376 const char *args[N_MAX_ARGS];
378 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
380 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
382 // Check against minimum argument count
383 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
384 Log_Warning("Config",
385 "Configuration command '%s' requires at least %i arguments, %i given",
386 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
391 // Check for extra arguments
392 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
393 Log_Warning("Config",
394 "Configuration command '%s' takes at most %i arguments, %i given",
395 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
401 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
402 args[k] = caConfigCommands[j].OptDefaults[k];
405 // Convert arguments to integers
406 for( k = line->nParts-1; k--; )
408 if( k < 32 && (caConfigCommands[j].IntArgs & (1 << k)) ) {
409 args[k] = (const char *)(Uint)atoi(line->Parts[k+1]);
412 args[k] = (char *)line->Parts[k+1];
414 LOG("args[%i] = %p", k, args[k]);
416 switch( (enum eConfigCommands) caConfigCommands[j].Index )
419 result = Module_LoadFile( args[0], args[1] );
422 result = Proc_Spawn( args[0] );
425 result = VFS_Mount( args[0], args[1], args[2], args[3] );
428 result = VFS_Symlink( args[0], args[1] );
431 result = VFS_Open( args[0], (Uint)args[1] );
434 VFS_Close( (Uint)args[0] );
438 result = VFS_MkDir( args[0] );
441 result = VFS_IOCtl( (Uint)args[0], (Uint)args[1], (void *)args[2] );
444 LOG("Config", "result = %i", result);
447 if( j < NUM_CONFIG_COMMANDS ) continue;
449 // --- State and Variables ---
450 if(strcmp(line->Parts[0], "set") == 0)
453 if( line->nParts-1 != 2 ) {
454 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
459 to = atoi(line->Parts[1]);
460 value = atoi(line->Parts[2]);
462 variables[to] = value;
465 // if <val1> <op> <val2> <dest>
466 else if(strcmp(line->Parts[0], "if") == 0)
468 if( line->nParts-1 != 4 ) {
469 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
473 result = atoi(line->Parts[1]);
474 val = atoi(line->Parts[3]);
476 jmpTarget = line->Parts[4];
478 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
479 result, line->Parts[2], val, jmpTarget);
481 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;
496 else if( strcmp(line->Parts[2], "!=") == 0 ) {
497 if( result != val ) goto jumpToLabel;
500 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
504 else if(strcmp(line->Parts[0], "goto") == 0) {
505 if( line->nParts-1 != 1 ) {
506 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
509 jmpTarget = line->Parts[1];
512 for( j = 0; j < file->nLines; j ++ )
514 if(file->Lines[j].nParts == 0)
516 if(file->Lines[j].Parts[0][0] != ':')
518 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
521 if( j == file->nLines )
522 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
527 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
534 // Clean up after ourselves
535 for( i = 0; i < file->nLines; i++ ) {
536 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
537 for( j = 0; j < file->Lines[i].nParts; j++ ) {
538 if(IsHeap(file->Lines[i].Parts[j]))
539 free(file->Lines[i].Parts[j]);
541 free( file->Lines[i].Parts );
552 * \brief Parses a config file
553 * \param FileData Read/Write buffer containing the config file data
555 * \return ::tConfigFile structure that represents the original contents
558 tConfigFile *System_Int_ParseFile(char *FileData)
566 ENTER("pFileData", FileData);
568 // Prescan and count the number of lines
569 for(ptr = FileData; *ptr; ptr++)
571 if(*ptr != '\n') continue;
573 if(ptr == FileData) {
579 if(ptr[-1] == '\\') continue;
584 LOG("nLines = %i", nLines);
586 // Ok so we have `nLines` lines, now to allocate our return
587 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
588 ret->nLines = nLines;
590 // Read the file for real
592 ptr = FileData, i = 0;
599 ret->Lines[i].nParts = 0;
600 ret->Lines[i].Parts = NULL;
605 // Read leading whitespace
606 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
609 if( *ptr == '\0' || *ptr == '\n' ) {
610 if(*ptr == '\n') ptr ++;
614 if( *ptr == '#' || *ptr == ';' ) {
615 while( *ptr && *ptr != '\n' ) ptr ++;
616 if(*ptr == '\n') ptr ++;
620 ret->Lines[i].nParts ++;
624 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
629 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
633 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
635 if( ret->Lines[i].nParts == 0 ) {
636 ret->Lines[i].Parts = NULL;
640 // Allocate part list
641 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
644 for( ptr = start, j = 0; ; j++ )
646 // Read leading whitespace
647 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
650 if( *ptr == '\0' || *ptr == '\n' ) {
651 if(*ptr == '\n') ptr ++;
655 if( *ptr == '#' || *ptr == ';' ) {
656 while( *ptr && *ptr != '\n' ) ptr ++;
657 if(*ptr == '\n') ptr ++;
661 ret->Lines[i].Parts[j] = ptr;
666 ret->Lines[i].Parts[j] = ptr;
667 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
672 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
676 // Break if we have reached NULL
678 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
683 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
687 *ptr = '\0'; // Cap off string
688 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
689 ptr ++; // And increment for the next round
693 if( i < ret->nLines ) {
694 ret->Lines[i].nParts = 0;
695 ret->Lines[i].Parts = NULL;
696 LOG("Cleaning up final empty line");