3 * Architecture Independent System Init
10 #define N_MAX_ARGS BITS
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
30 void *Func; // Function pointer
31 Uint 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(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 const tConfigCommand caConfigCommands[] = {
52 {"module", 1,2, 00, Module_LoadFile, {(Uint)"",0}}, // Load a module from a file
53 {"spawn", 1,1, 00, Proc_Spawn, {0}}, // Spawn a process
55 {"mount", 3,4, 00, VFS_Mount, {(Uint)"",0}}, // Mount a device
56 {"symlink", 2,2, 00, VFS_Symlink, {0}}, // Create a Symbolic Link
57 {"mkdir", 1,1, 00, VFS_MkDir, {0}}, // Create a Directory
58 {"open", 1,2, 00, VFS_Open, {VFS_OPENFLAG_READ,0}}, // Open a file
59 {"close", 1,1, 01, VFS_Close, {0}}, // Close an open file
60 {"ioctl", 3,3, 03, VFS_IOCtl, {0}}, // Call an IOCtl
62 {"", 0,0, 0, NULL, {0}}
64 #define NUM_CONFIG_COMMANDS (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
67 char *gsConfigScript = "/Acess/Conf/BootConf.cfg";
72 void System_Init(char *CommandLine)
74 // Parse Kernel's Command Line
75 System_ParseCommandLine(CommandLine);
78 Log_Log("Config", "Initialising builtin modules...");
79 Modules_LoadBuiltins();
80 Arch_LoadBootModules();
82 System_ExecuteCommandLine();
84 // - Execute the Config Script
85 Log_Log("Config", "Executing config script...");
86 System_ExecuteScript();
88 // Set the debug to be echoed to the terminal
89 Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
90 Debug_SetKTerminal("/Devices/VTerm/7");
94 * \fn void System_ParseCommandLine(char *ArgString)
95 * \brief Parses the kernel's command line and sets the environment
97 void System_ParseCommandLine(char *ArgString)
102 Log_Log("Config", "Kernel Invocation \"%s\"", ArgString);
104 // --- Get Arguments ---
106 for( argc = 0; argc < 32; argc++ )
109 while(*str == ' ') str++;
110 // Check for the end of the string
111 if(*str == '\0') { argc--; break;}
114 while(*str && !(*str == '"' && str[-1] != '\\'))
118 while(*str && *str != ' ')
121 if(*str == '\0') break; // Check for EOS
122 *str = '\0'; // Cap off argument string
123 str ++; // and increment the string pointer
126 argc ++; // Count last argument
128 // --- Parse Arguments (Pass 1) ---
129 for( i = 0; i < argc; i++ )
134 // Ignored on this pass
138 // --- Module Paramaters ---
139 // -VTerm:Width=640,Height=480,Scrollback=2
141 System_ParseModuleArgs( argv[i] );
143 // --- Config Options ---
144 // SCRIPT=/Acess/Conf/BootConf.cfg
146 System_ParseSetting( argv[i] );
152 void System_ExecuteCommandLine(void)
155 for( i = 0; i < argc; i++ )
157 Log("argv[%i] = '%s'", i, argv[i]);
161 // Mount /System=ext2:/Devices/ATA/A1
162 // Symlink /Acess=/System/Acess2
164 System_ParseVFS( argv[i] );
171 * \fn void System_ParseVFS(char *Arg)
173 void System_ParseVFS(char *Arg)
179 // Search for the '=' token
180 while( *value && *value != '=' )
183 // Check if the equals was found
184 if( *value == '\0' ) {
185 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
190 *value = '\0'; value ++;
192 // Check assignment type
193 // - Symbolic Link <link>=<destination>
196 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
197 VFS_Symlink(Arg, value);
199 // - Mount <mountpoint>=<fs>:<device>
204 while(*dev && *dev != ':') dev++;
210 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
211 Log_Log("Config", "Creating directory '%s'", Arg, value);
217 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
218 VFS_Mount(dev, Arg, value, "");
223 * \biref Parse a module argument string
225 void System_ParseModuleArgs(char *Arg)
233 // Find the start of the args
234 i = strpos(name, ':');
236 Log_Warning("Config", "Module spec with no arguments");
249 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
250 Modules_SetBuiltinParams(name, args);
254 * \fn void System_ParseSetting(char *Arg)
256 void System_ParseSetting(char *Arg)
261 // Search for the '=' token
262 while( *value && *value != '=' )
265 // Check for boolean/flag (no '=')
268 //if(strcmp(Arg, "") == 0) {
270 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
275 *value = '\0'; // Remove '='
276 value ++; // and eat it's position
278 if(strcmp(Arg, "SCRIPT") == 0) {
279 Log_Log("Config", "Config Script: '%s'", value);
280 if(strlen(value) == 0)
281 gsConfigScript = NULL;
283 gsConfigScript = value;
285 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
292 * \fn void System_ExecuteScript()
293 * \brief Reads and parses the boot configuration script
295 void System_ExecuteScript(void)
302 int variables[N_VARIABLES];
303 int bReplaced[N_MAX_ARGS];
310 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
312 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
317 VFS_Seek(fp, 0, SEEK_END);
319 VFS_Seek(fp, 0, SEEK_SET);
320 // Read into memory buffer
321 fData = malloc(fLen+1);
322 VFS_Read(fp, fLen, fData);
328 file = System_Int_ParseFile(fData);
331 for( i = 0; i < file->nLines; i++ )
333 line = &file->Lines[i];
334 if( line->nParts == 0 ) continue; // Skip blank
336 if(line->Parts[0][0] == ':') continue; // Ignore labels
338 // Prescan and eliminate variables
339 for( j = 1; j < line->nParts; j++ ) {
340 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
342 if( line->Parts[j][0] != '$' ) continue;
343 if( line->Parts[j][1] == '?' ) {
347 val = atoi( &line->Parts[j][1] );
348 if( val < 0 || val > N_VARIABLES ) continue;
349 val = variables[ val ];
351 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
352 line->Parts[j] = malloc( BITS/8+2+1 );
353 sprintf(line->Parts[j], "0x%x", val);
357 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
359 Uint args[N_MAX_ARGS];
360 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
362 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
364 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
365 Log_Warning("Config",
366 "Configuration command '%s' requires at least %i arguments, %i given",
367 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
372 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
373 Log_Warning("Config",
374 "Configuration command '%s' takes at most %i arguments, %i given",
375 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
380 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
381 args[k] = caConfigCommands[j].OptDefaults[k];
384 for( k = line->nParts-1; k--; )
386 if( caConfigCommands[j].IntArgs & (1 << k) ) {
387 args[k] = atoi(line->Parts[k+1]);
390 args[k] = (Uint)line->Parts[k+1];
392 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
394 result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
395 Log_Debug("Config", "result = %i", result);
398 if( j < NUM_CONFIG_COMMANDS ) continue;
400 // --- State and Variables ---
401 if(strcmp(line->Parts[0], "set") == 0)
404 if( line->nParts-1 != 2 ) {
405 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
410 to = atoi(line->Parts[1]);
411 value = atoi(line->Parts[2]);
413 variables[to] = value;
416 // if <val1> <op> <val2> <dest>
417 else if(strcmp(line->Parts[0], "if") == 0)
419 if( line->nParts-1 != 4 ) {
420 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
424 result = atoi(line->Parts[1]);
425 val = atoi(line->Parts[3]);
427 jmpTarget = line->Parts[4];
429 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
430 result, line->Parts[2], val, jmpTarget);
432 if( strcmp(line->Parts[2], "<" ) == 0 ) {
433 if( result < val ) goto jumpToLabel;
435 else if( strcmp(line->Parts[2], "<=") == 0 ) {
436 if( result <= val ) goto jumpToLabel;
438 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
439 if (result > val ) goto jumpToLabel;
441 else if( strcmp(line->Parts[2], ">=") == 0 ) {
442 if( result >= val ) goto jumpToLabel;
444 else if( strcmp(line->Parts[2], "=") == 0 ) {
445 if( result == val ) goto jumpToLabel;
447 else if( strcmp(line->Parts[2], "!=") == 0 ) {
448 if( result != val ) goto jumpToLabel;
451 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
455 else if(strcmp(line->Parts[0], "goto") == 0) {
456 if( line->nParts-1 != 1 ) {
457 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
460 jmpTarget = line->Parts[1];
463 for( j = 0; j < file->nLines; j ++ )
465 if(file->Lines[j].nParts == 0)
467 if(file->Lines[j].Parts[0][0] != ':')
469 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
472 if( j == file->nLines )
473 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
478 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
485 // Clean up after ourselves
486 for( i = 0; i < file->nLines; i++ ) {
487 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
488 for( j = 0; j < file->Lines[i].nParts; j++ ) {
489 if(IsHeap(file->Lines[i].Parts[j]))
490 free(file->Lines[i].Parts[j]);
492 free( file->Lines[i].Parts );
501 * \brief Parses a config file
502 * \param FileData Read/Write buffer containing the config file data
504 * \return ::tConfigFile structure that represents the original contents
507 tConfigFile *System_Int_ParseFile(char *FileData)
515 ENTER("pFileData", FileData);
517 // Prescan and count the number of lines
518 for(ptr = FileData; *ptr; ptr++)
520 if(*ptr != '\n') continue;
522 if(ptr == FileData) {
528 if(ptr[-1] == '\\') continue;
533 LOG("nLines = %i", nLines);
535 // Ok so we have `nLines` lines, now to allocate our return
536 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
537 ret->nLines = nLines;
539 // Read the file for real
541 ptr = FileData, i = 0;
548 ret->Lines[i].nParts = 0;
553 // Read leading whitespace
554 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
557 if( *ptr == '\0' || *ptr == '\n' ) {
558 if(*ptr == '\n') ptr ++;
562 if( *ptr == '#' || *ptr == ';' ) {
563 while( *ptr && *ptr != '\n' ) ptr ++;
564 if(*ptr == '\n') ptr ++;
568 ret->Lines[i].nParts ++;
572 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
577 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
581 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
583 if( ret->Lines[i].nParts == 0 ) {
584 ret->Lines[i].Parts = NULL;
588 // Allocate part list
589 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
592 for( ptr = start, j = 0; ; j++ )
594 // Read leading whitespace
595 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
598 if( *ptr == '\0' || *ptr == '\n' ) {
599 if(*ptr == '\n') ptr ++;
603 if( *ptr == '#' || *ptr == ';' ) {
604 while( *ptr && *ptr != '\n' ) ptr ++;
605 if(*ptr == '\n') ptr ++;
609 ret->Lines[i].Parts[j] = ptr;
614 ret->Lines[i].Parts[j] = ptr;
615 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
620 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
624 // Break if we have reached NULL
626 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
631 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
635 *ptr = '\0'; // Cap off string
636 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
637 ptr ++; // And increment for the next round