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 = 1; 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)
166 LOG("Invocation '%s'", argv[0]);
167 for( i = 1; i < argc; i++ )
169 LOG("argv[%i] = '%s'", i, argv[i]);
173 // Mount /System=ext2:/Devices/ATA/A1
174 // Symlink /Acess=/System/Acess2
176 System_ParseVFS( argv[i] );
183 * \fn void System_ParseVFS(char *Arg)
185 void System_ParseVFS(char *Arg)
191 // Search for the '=' token
192 while( *value && *value != '=' )
195 // Check if the equals was found
196 if( *value == '\0' ) {
197 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
202 *value = '\0'; value ++;
204 // Check assignment type
205 // - Symbolic Link <link>=<destination>
208 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
209 VFS_Symlink(Arg, value);
211 // - Mount <mountpoint>=<fs>:<device>
216 while(*dev && *dev != ':') dev++;
222 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
223 Log_Log("Config", "Creating directory '%s'", Arg, value);
229 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
230 VFS_Mount(dev, Arg, value, "");
235 * \brief Parse a module argument string
236 * \param Arg Argument string
238 void System_ParseModuleArgs(char *Arg)
246 // Find the start of the args
247 i = strpos(name, ':');
249 Log_Warning("Config", "Module spec with no arguments");
262 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
263 Modules_SetBuiltinParams(name, args);
267 * \fn void System_ParseSetting(char *Arg)
269 void System_ParseSetting(char *Arg)
274 // Search for the '=' token
275 while( *value && *value != '=' )
278 // Check for boolean/flag (no '=')
281 //if(strcmp(Arg, "") == 0) {
283 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
288 *value = '\0'; // Remove '='
289 value ++; // and eat it's position
291 if(strcmp(Arg, "SCRIPT") == 0) {
292 Log_Log("Config", "Config Script: '%s'", value);
293 if(strlen(value) == 0)
294 gsConfigScript = NULL;
296 gsConfigScript = value;
298 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
305 * \fn void System_ExecuteScript()
306 * \brief Reads and parses the boot configuration script
308 void System_ExecuteScript(void)
315 int variables[N_VARIABLES];
316 int bReplaced[N_MAX_ARGS];
325 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
327 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
333 VFS_Seek(fp, 0, SEEK_END);
335 LOG("VFS_Tell(0x%x) = %i", fp, fLen);
336 VFS_Seek(fp, 0, SEEK_SET);
337 // Read into memory buffer
338 fData = malloc(fLen+1);
339 VFS_Read(fp, fLen, fData);
345 file = System_Int_ParseFile(fData);
348 for( i = 0; i < file->nLines; i++ )
350 line = &file->Lines[i];
351 if( line->nParts == 0 ) continue; // Skip blank
353 if(line->Parts[0][0] == ':') continue; // Ignore labels
355 // Prescan and eliminate variables
356 for( j = 1; j < line->nParts; j++ )
358 LOG("Arg #%i is '%s'", j, line->Parts[j]);
360 if( line->Parts[j][0] != '$' ) continue;
361 if( line->Parts[j][1] == '?' ) {
365 val = atoi( &line->Parts[j][1] );
366 if( val < 0 || val > N_VARIABLES ) continue;
367 val = variables[ val ];
369 LOG("Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
370 line->Parts[j] = malloc( BITS/8+2+1 );
371 sprintf(line->Parts[j], "0x%x", val);
375 // Find the command name
376 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
378 const char *args[N_MAX_ARGS];
380 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
382 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
384 // Check against minimum argument count
385 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
386 Log_Warning("Config",
387 "Configuration command '%s' requires at least %i arguments, %i given",
388 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
393 // Check for extra arguments
394 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
395 Log_Warning("Config",
396 "Configuration command '%s' takes at most %i arguments, %i given",
397 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
403 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
404 args[k] = caConfigCommands[j].OptDefaults[k];
407 // Convert arguments to integers
408 for( k = line->nParts-1; k--; )
410 if( k < 32 && (caConfigCommands[j].IntArgs & (1 << k)) ) {
411 args[k] = (const char *)(Uint)atoi(line->Parts[k+1]);
414 args[k] = (char *)line->Parts[k+1];
416 LOG("args[%i] = %p", k, args[k]);
418 switch( (enum eConfigCommands) caConfigCommands[j].Index )
421 result = Module_LoadFile( args[0], args[1] );
424 result = Proc_Spawn( args[0] );
427 result = VFS_Mount( args[0], args[1], args[2], args[3] );
430 result = VFS_Symlink( args[0], args[1] );
433 result = VFS_Open( args[0], (Uint)args[1] );
436 VFS_Close( (Uint)args[0] );
440 result = VFS_MkDir( args[0] );
443 result = VFS_IOCtl( (Uint)args[0], (Uint)args[1], (void *)args[2] );
446 LOG("Config", "result = %i", result);
449 if( j < NUM_CONFIG_COMMANDS ) continue;
451 // --- State and Variables ---
452 if(strcmp(line->Parts[0], "set") == 0)
455 if( line->nParts-1 != 2 ) {
456 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
461 to = atoi(line->Parts[1]);
462 value = atoi(line->Parts[2]);
464 variables[to] = value;
467 // if <val1> <op> <val2> <dest>
468 else if(strcmp(line->Parts[0], "if") == 0)
470 if( line->nParts-1 != 4 ) {
471 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
475 result = atoi(line->Parts[1]);
476 val = atoi(line->Parts[3]);
478 jmpTarget = line->Parts[4];
480 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
481 result, line->Parts[2], val, jmpTarget);
483 if( strcmp(line->Parts[2], "<" ) == 0 ) {
484 if( result < val ) goto jumpToLabel;
486 else if( strcmp(line->Parts[2], "<=") == 0 ) {
487 if( result <= val ) goto jumpToLabel;
489 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
490 if (result > val ) goto jumpToLabel;
492 else if( strcmp(line->Parts[2], ">=") == 0 ) {
493 if( result >= val ) goto jumpToLabel;
495 else if( strcmp(line->Parts[2], "=") == 0 ) {
496 if( result == val ) goto jumpToLabel;
498 else if( strcmp(line->Parts[2], "!=") == 0 ) {
499 if( result != val ) goto jumpToLabel;
502 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
506 else if(strcmp(line->Parts[0], "goto") == 0) {
507 if( line->nParts-1 != 1 ) {
508 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
511 jmpTarget = line->Parts[1];
514 for( j = 0; j < file->nLines; j ++ )
516 if(file->Lines[j].nParts == 0)
518 if(file->Lines[j].Parts[0][0] != ':')
520 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
523 if( j == file->nLines )
524 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
529 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
536 // Clean up after ourselves
537 for( i = 0; i < file->nLines; i++ ) {
538 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
539 for( j = 0; j < file->Lines[i].nParts; j++ ) {
540 if(IsHeap(file->Lines[i].Parts[j]))
541 free(file->Lines[i].Parts[j]);
543 free( file->Lines[i].Parts );
554 * \brief Parses a config file
555 * \param FileData Read/Write buffer containing the config file data
557 * \return ::tConfigFile structure that represents the original contents
560 tConfigFile *System_Int_ParseFile(char *FileData)
568 ENTER("pFileData", FileData);
570 // Prescan and count the number of lines
571 for(ptr = FileData; *ptr; ptr++)
573 if(*ptr != '\n') continue;
575 if(ptr == FileData) {
581 if(ptr[-1] == '\\') continue;
586 LOG("nLines = %i", nLines);
588 // Ok so we have `nLines` lines, now to allocate our return
589 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
590 ret->nLines = nLines;
592 // Read the file for real
594 ptr = FileData, i = 0;
601 ret->Lines[i].nParts = 0;
602 ret->Lines[i].Parts = NULL;
607 // Read leading whitespace
608 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
611 if( *ptr == '\0' || *ptr == '\n' ) {
612 if(*ptr == '\n') ptr ++;
616 if( *ptr == '#' || *ptr == ';' ) {
617 while( *ptr && *ptr != '\n' ) ptr ++;
618 if(*ptr == '\n') ptr ++;
622 ret->Lines[i].nParts ++;
626 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
631 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
635 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
637 if( ret->Lines[i].nParts == 0 ) {
638 ret->Lines[i].Parts = NULL;
642 // Allocate part list
643 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
646 for( ptr = start, j = 0; ; j++ )
648 // Read leading whitespace
649 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
652 if( *ptr == '\0' || *ptr == '\n' ) {
653 if(*ptr == '\n') ptr ++;
657 if( *ptr == '#' || *ptr == ';' ) {
658 while( *ptr && *ptr != '\n' ) ptr ++;
659 if(*ptr == '\n') ptr ++;
663 ret->Lines[i].Parts[j] = ptr;
668 ret->Lines[i].Parts[j] = ptr;
669 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
674 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
678 // Break if we have reached NULL
680 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
685 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
689 *ptr = '\0'; // Cap off string
690 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
691 ptr ++; // And increment for the next round
695 if( i < ret->nLines ) {
696 ret->Lines[i].nParts = 0;
697 ret->Lines[i].Parts = NULL;
698 LOG("Cleaning up final empty line");