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);
113 Log_Log("Config", "Kernel Invocation '0x%x 0x%x'", ArgString[0], ArgString[1]);
115 // --- Get Arguments ---
117 for( argc = 0; argc < 32; argc++ )
120 while(*str == ' ') str++;
121 // Check for the end of the string
122 if(*str == '\0') { argc--; break;}
125 while(*str && !(*str == '"' && str[-1] != '\\'))
129 while(*str && *str != ' ')
132 if(*str == '\0') break; // Check for EOS
133 *str = '\0'; // Cap off argument string
134 str ++; // and increment the string pointer
137 argc ++; // Count last argument
139 // --- Parse Arguments (Pass 1) ---
140 for( i = 0; i < argc; i++ )
145 // Ignored on this pass
149 // --- Module Paramaters ---
150 // -VTerm:Width=640,Height=480,Scrollback=2
152 System_ParseModuleArgs( argv[i] );
154 // --- Config Options ---
155 // SCRIPT=/Acess/Conf/BootConf.cfg
157 System_ParseSetting( argv[i] );
163 void System_ExecuteCommandLine(void)
166 for( i = 0; i < argc; i++ )
168 Log("argv[%i] = '%s'", i, argv[i]);
172 // Mount /System=ext2:/Devices/ATA/A1
173 // Symlink /Acess=/System/Acess2
175 System_ParseVFS( argv[i] );
182 * \fn void System_ParseVFS(char *Arg)
184 void System_ParseVFS(char *Arg)
190 // Search for the '=' token
191 while( *value && *value != '=' )
194 // Check if the equals was found
195 if( *value == '\0' ) {
196 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
201 *value = '\0'; value ++;
203 // Check assignment type
204 // - Symbolic Link <link>=<destination>
207 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
208 VFS_Symlink(Arg, value);
210 // - Mount <mountpoint>=<fs>:<device>
215 while(*dev && *dev != ':') dev++;
221 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
222 Log_Log("Config", "Creating directory '%s'", Arg, value);
228 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
229 VFS_Mount(dev, Arg, value, "");
234 * \brief Parse a module argument string
235 * \param Arg Argument string
237 void System_ParseModuleArgs(char *Arg)
245 // Find the start of the args
246 i = strpos(name, ':');
248 Log_Warning("Config", "Module spec with no arguments");
261 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
262 Modules_SetBuiltinParams(name, args);
266 * \fn void System_ParseSetting(char *Arg)
268 void System_ParseSetting(char *Arg)
273 // Search for the '=' token
274 while( *value && *value != '=' )
277 // Check for boolean/flag (no '=')
280 //if(strcmp(Arg, "") == 0) {
282 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
287 *value = '\0'; // Remove '='
288 value ++; // and eat it's position
290 if(strcmp(Arg, "SCRIPT") == 0) {
291 Log_Log("Config", "Config Script: '%s'", value);
292 if(strlen(value) == 0)
293 gsConfigScript = NULL;
295 gsConfigScript = value;
297 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
304 * \fn void System_ExecuteScript()
305 * \brief Reads and parses the boot configuration script
307 void System_ExecuteScript(void)
314 int variables[N_VARIABLES];
315 int bReplaced[N_MAX_ARGS];
322 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
324 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
329 VFS_Seek(fp, 0, SEEK_END);
331 Log_Debug("System", "VFS_Tell(%i) = %i", fp, fLen);
332 VFS_Seek(fp, 0, SEEK_SET);
333 // Read into memory buffer
334 fData = malloc(fLen+1);
335 VFS_Read(fp, fLen, fData);
341 file = System_Int_ParseFile(fData);
344 for( i = 0; i < file->nLines; i++ )
346 line = &file->Lines[i];
347 if( line->nParts == 0 ) continue; // Skip blank
349 if(line->Parts[0][0] == ':') continue; // Ignore labels
351 // Prescan and eliminate variables
352 for( j = 1; j < line->nParts; j++ )
354 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
356 if( line->Parts[j][0] != '$' ) continue;
357 if( line->Parts[j][1] == '?' ) {
361 val = atoi( &line->Parts[j][1] );
362 if( val < 0 || val > N_VARIABLES ) continue;
363 val = variables[ val ];
365 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
366 line->Parts[j] = malloc( BITS/8+2+1 );
367 sprintf(line->Parts[j], "0x%x", val);
371 // Find the command name
372 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
374 const char *args[N_MAX_ARGS];
376 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
378 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
380 // Check against minimum argument count
381 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
382 Log_Warning("Config",
383 "Configuration command '%s' requires at least %i arguments, %i given",
384 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
389 // Check for extra arguments
390 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
391 Log_Warning("Config",
392 "Configuration command '%s' takes at most %i arguments, %i given",
393 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
399 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
400 args[k] = caConfigCommands[j].OptDefaults[k];
403 // Convert arguments to integers
404 for( k = line->nParts-1; k--; )
406 if( k < 32 && (caConfigCommands[j].IntArgs & (1 << k)) ) {
407 args[k] = (const char *)(Uint)atoi(line->Parts[k+1]);
410 args[k] = (char *)line->Parts[k+1];
412 Log_Debug("Config", "args[%i] = %p", k, args[k]);
414 switch( (enum eConfigCommands) caConfigCommands[j].Index )
417 result = Module_LoadFile( args[0], args[1] );
420 result = Proc_Spawn( args[0] );
423 result = VFS_Mount( args[0], args[1], args[2], args[3] );
426 result = VFS_Symlink( args[0], args[1] );
429 result = VFS_Open( args[0], (Uint)args[1] );
432 VFS_Close( (Uint)args[0] );
436 result = VFS_MkDir( args[0] );
439 result = VFS_IOCtl( (Uint)args[0], (Uint)args[1], (void *)args[2] );
442 Log_Debug("Config", "result = %i", result);
445 if( j < NUM_CONFIG_COMMANDS ) continue;
447 // --- State and Variables ---
448 if(strcmp(line->Parts[0], "set") == 0)
451 if( line->nParts-1 != 2 ) {
452 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
457 to = atoi(line->Parts[1]);
458 value = atoi(line->Parts[2]);
460 variables[to] = value;
463 // if <val1> <op> <val2> <dest>
464 else if(strcmp(line->Parts[0], "if") == 0)
466 if( line->nParts-1 != 4 ) {
467 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
471 result = atoi(line->Parts[1]);
472 val = atoi(line->Parts[3]);
474 jmpTarget = line->Parts[4];
476 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
477 result, line->Parts[2], val, jmpTarget);
479 if( strcmp(line->Parts[2], "<" ) == 0 ) {
480 if( result < val ) goto jumpToLabel;
482 else if( strcmp(line->Parts[2], "<=") == 0 ) {
483 if( result <= val ) goto jumpToLabel;
485 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
486 if (result > val ) goto jumpToLabel;
488 else if( strcmp(line->Parts[2], ">=") == 0 ) {
489 if( result >= val ) goto jumpToLabel;
491 else if( strcmp(line->Parts[2], "=") == 0 ) {
492 if( result == val ) goto jumpToLabel;
494 else if( strcmp(line->Parts[2], "!=") == 0 ) {
495 if( result != val ) goto jumpToLabel;
498 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
502 else if(strcmp(line->Parts[0], "goto") == 0) {
503 if( line->nParts-1 != 1 ) {
504 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
507 jmpTarget = line->Parts[1];
510 for( j = 0; j < file->nLines; j ++ )
512 if(file->Lines[j].nParts == 0)
514 if(file->Lines[j].Parts[0][0] != ':')
516 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
519 if( j == file->nLines )
520 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
525 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
532 // Clean up after ourselves
533 for( i = 0; i < file->nLines; i++ ) {
534 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
535 for( j = 0; j < file->Lines[i].nParts; j++ ) {
536 if(IsHeap(file->Lines[i].Parts[j]))
537 free(file->Lines[i].Parts[j]);
539 free( file->Lines[i].Parts );
548 * \brief Parses a config file
549 * \param FileData Read/Write buffer containing the config file data
551 * \return ::tConfigFile structure that represents the original contents
554 tConfigFile *System_Int_ParseFile(char *FileData)
562 ENTER("pFileData", FileData);
564 // Prescan and count the number of lines
565 for(ptr = FileData; *ptr; ptr++)
567 if(*ptr != '\n') continue;
569 if(ptr == FileData) {
575 if(ptr[-1] == '\\') continue;
580 LOG("nLines = %i", nLines);
582 // Ok so we have `nLines` lines, now to allocate our return
583 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
584 ret->nLines = nLines;
586 // Read the file for real
588 ptr = FileData, i = 0;
595 ret->Lines[i].nParts = 0;
596 ret->Lines[i].Parts = NULL;
601 // Read leading whitespace
602 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
605 if( *ptr == '\0' || *ptr == '\n' ) {
606 if(*ptr == '\n') ptr ++;
610 if( *ptr == '#' || *ptr == ';' ) {
611 while( *ptr && *ptr != '\n' ) ptr ++;
612 if(*ptr == '\n') ptr ++;
616 ret->Lines[i].nParts ++;
620 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
625 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
629 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
631 if( ret->Lines[i].nParts == 0 ) {
632 ret->Lines[i].Parts = NULL;
636 // Allocate part list
637 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
640 for( ptr = start, j = 0; ; j++ )
642 // Read leading whitespace
643 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
646 if( *ptr == '\0' || *ptr == '\n' ) {
647 if(*ptr == '\n') ptr ++;
651 if( *ptr == '#' || *ptr == ';' ) {
652 while( *ptr && *ptr != '\n' ) ptr ++;
653 if(*ptr == '\n') ptr ++;
657 ret->Lines[i].Parts[j] = ptr;
662 ret->Lines[i].Parts[j] = ptr;
663 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
668 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
672 // Break if we have reached NULL
674 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
679 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
683 *ptr = '\0'; // Cap off string
684 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
685 ptr ++; // And increment for the next round
689 if( i < ret->nLines ) {
690 ret->Lines[i].nParts = 0;
691 ret->Lines[i].Parts = NULL;
692 Log_Log("System", "Cleaning up final empty line");