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 int Modules_LoadBuiltins();
36 //extern int PCI_Install();
37 extern void DMA_Install();
38 extern void Debug_SetKTerminal(char *File);
39 extern void StartupPrint(char *Str);
42 void System_Init(char *ArgString);
43 void System_ParseCommandLine(char *ArgString);
44 void System_ParseVFS(char *Arg);
45 void System_ParseSetting(char *Arg);
46 void System_ExecuteScript();
47 tConfigFile *System_Int_ParseFile(char *File);
50 const tConfigCommand caConfigCommands[] = {
51 {"module", 1,2, 0, Module_LoadFile, {(Uint)"",0}}, // Load a module from a file
52 {"spawn", 1,1, 0, Proc_Spawn, {0}}, // Spawn a process
54 {"mount", 3,4, 0, VFS_Mount, {(Uint)"",0}}, // Mount a device
55 {"symlink", 2,2, 0, VFS_Symlink, {0}}, // Create a Symbolic Link
56 {"mkdir", 1,1, 0, VFS_MkDir, {0}}, // Create a Directory
57 {"open", 1,2, 0, VFS_Open, {VFS_OPENFLAG_READ,0}}, // Open a file
58 {"close", 1,1, 0x1, VFS_Close, {0}}, // Close an open file
59 {"ioctl", 3,3, 0x3, VFS_IOCtl, {0}}, // Call an IOCtl
61 {"", 0,0, 0, NULL, {0}}
63 #define NUM_CONFIG_COMMANDS (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
66 char *gsConfigScript = "/Acess/Conf/BootConf.cfg";
69 void System_Init(char *ArgString)
72 // - Parse Kernel's Command Line
73 System_ParseCommandLine(ArgString);
75 // - Execute the Config Script
76 Log_Log("Config", "Executing config script...");
77 System_ExecuteScript();
79 // Set the debug to be echoed to the terminal
80 Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
81 Debug_SetKTerminal("/Devices/VTerm/7");
85 * \fn void System_ParseCommandLine(char *ArgString)
86 * \brief Parses the kernel's command line and sets the environment
88 void System_ParseCommandLine(char *ArgString)
95 Log_Log("Config", "Kernel Invocation \"%s\"", ArgString);
97 // --- Get Arguments ---
99 for( argc = 0; argc < 32; argc++ )
102 while(*str == ' ') str++;
103 // Check for the end of the string
104 if(*str == '\0') { argc--; break;}
106 while(*str && *str != ' ')
109 while(*str && !(*str == '"' && str[-1] != '\\'))
114 if(*str == '\0') break; // Check for EOS
115 *str = '\0'; // Cap off argument string
116 str ++; // and increment the string pointer
119 argc ++; // Count last argument
121 // --- Parse Arguments ---
122 for( i = 1; i < argc; i++ )
124 if( argv[i][0] == '/' )
125 System_ParseVFS( argv[i] );
127 System_ParseSetting( argv[i] );
132 * \fn void System_ParseVFS(char *Arg)
134 void System_ParseVFS(char *Arg)
140 // Search for the '=' token
141 while( *value && *value != '=' )
144 // Check if the equals was found
145 if( *value == '\0' ) {
146 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
151 *value = '\0'; value ++;
153 // Check assignment type
154 // - Symbolic Link <link>=<destination>
157 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
158 VFS_Symlink(Arg, value);
160 // - Mount <mountpoint>=<fs>:<device>
165 while(*dev && *dev != ':') dev++;
171 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
172 Log_Log("Config", "Creating directory '%s'", Arg, value);
178 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
179 VFS_Mount(dev, Arg, value, "");
184 * \fn void System_ParseSetting(char *Arg)
186 void System_ParseSetting(char *Arg)
191 // Search for the '=' token
192 while( *value && *value != '=' )
195 // Check for boolean/flag (no '=')
198 //if(strcmp(Arg, "") == 0) {
200 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
205 *value = '\0'; // Remove '='
206 value ++; // and eat it's position
208 if(strcmp(Arg, "SCRIPT") == 0) {
209 Log_Log("Config", "Config Script: '%s'", value);
210 if(strlen(value) == 0)
211 gsConfigScript = NULL;
213 gsConfigScript = value;
215 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
222 * \fn void System_ExecuteScript()
224 void System_ExecuteScript()
231 int variables[N_VARIABLES];
232 int bReplaced[N_MAX_ARGS];
239 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
241 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
245 // Read into memory buffer
246 VFS_Seek(fp, 0, SEEK_END);
248 VFS_Seek(fp, 0, SEEK_SET);
249 fData = malloc(fLen+1);
250 VFS_Read(fp, fLen, fData);
256 file = System_Int_ParseFile(fData);
259 for( i = 0; i < file->nLines; i++ )
261 line = &file->Lines[i];
262 if( line->nParts == 0 ) continue; // Skip blank
264 if(line->Parts[0][0] == ':') continue; // Ignore labels
266 // Prescan and eliminate variables
267 for( j = 1; j < line->nParts; j++ ) {
268 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
270 if( line->Parts[j][0] != '$' ) continue;
271 if( line->Parts[j][1] == '?' ) {
275 val = atoi( &line->Parts[j][1] );
276 if( val < 0 || val > N_VARIABLES ) continue;
277 val = variables[ val ];
279 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
280 line->Parts[j] = malloc( BITS/8+2+1 );
281 sprintf(line->Parts[j], "0x%x", val);
285 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
287 Uint args[N_MAX_ARGS];
288 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
290 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
292 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
293 Log_Warning("Config",
294 "Configuration command '%s' requires at least %i arguments, %i given",
295 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
300 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
301 Log_Warning("Config",
302 "Configuration command '%s' takes at most %i arguments, %i given",
303 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
308 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
309 args[k] = caConfigCommands[j].OptDefaults[k];
312 for( k = line->nParts-1; k--; )
314 if( caConfigCommands[j].IntArgs & (1 << k) ) {
315 args[k] = atoi(line->Parts[k+1]);
318 args[k] = (Uint)line->Parts[k+1];
320 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
322 result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
323 Log_Debug("Config", "result = %i", result);
326 if( j < NUM_CONFIG_COMMANDS ) continue;
328 // --- State and Variables ---
329 if(strcmp(line->Parts[0], "set") == 0)
332 if( line->nParts-1 != 2 ) {
333 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
338 to = atoi(line->Parts[1]);
339 value = atoi(line->Parts[2]);
341 variables[to] = value;
344 // if <val1> <op> <val2> <dest>
345 else if(strcmp(line->Parts[0], "if") == 0)
347 if( line->nParts-1 != 4 ) {
348 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
352 result = atoi(line->Parts[1]);
353 val = atoi(line->Parts[3]);
355 jmpTarget = line->Parts[4];
357 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
358 result, line->Parts[2], val, jmpTarget);
360 if( strcmp(line->Parts[2], "<" ) == 0 ) {
361 if( result < val ) goto jumpToLabel;
363 else if( strcmp(line->Parts[2], "<=") == 0 ) {
364 if( result <= val ) goto jumpToLabel;
366 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
367 if (result > val ) goto jumpToLabel;
369 else if( strcmp(line->Parts[2], ">=") == 0 ) {
370 if( result >= val ) goto jumpToLabel;
372 else if( strcmp(line->Parts[2], "=") == 0 ) {
373 if( result == val ) goto jumpToLabel;
375 else if( strcmp(line->Parts[2], "!=") == 0 ) {
376 if( result != val ) goto jumpToLabel;
379 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
383 else if(strcmp(line->Parts[0], "goto") == 0) {
384 if( line->nParts-1 != 1 ) {
385 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
388 jmpTarget = line->Parts[1];
391 for( j = 0; j < file->nLines; j ++ )
393 if(file->Lines[j].nParts == 0)
395 if(file->Lines[j].Parts[0][0] != ':')
397 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
400 if( j == file->nLines )
401 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
406 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
413 // Clean up after ourselves
414 for( i = 0; i < file->nLines; i++ ) {
415 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
416 for( j = 0; j < file->Lines[i].nParts; j++ ) {
417 if(IsHeap(file->Lines[i].Parts[j]))
418 free(file->Lines[i].Parts[j]);
420 free( file->Lines[i].Parts );
427 * \brief Parses a config file
428 * \param FileData Read/Write buffer containing the config file data
430 * \return ::tConfigFile structure that represents the original contents
433 tConfigFile *System_Int_ParseFile(char *FileData)
441 ENTER("pFileData", FileData);
443 // Prescan and count the number of lines
444 for(ptr = FileData; *ptr; ptr++)
446 if(*ptr != '\n') continue;
448 if(ptr == FileData) {
454 if(ptr[-1] == '\\') continue;
459 LOG("nLines = %i", nLines);
461 // Ok so we have `nLines` lines, now to allocate our return
462 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
463 ret->nLines = nLines;
465 // Read the file for real
467 ptr = FileData, i = 0;
474 ret->Lines[i].nParts = 0;
479 // Read leading whitespace
480 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
483 if( *ptr == '\0' || *ptr == '\n' ) {
484 if(*ptr == '\n') ptr ++;
488 if( *ptr == '#' || *ptr == ';' ) {
489 while( *ptr && *ptr != '\n' ) ptr ++;
490 if(*ptr == '\n') ptr ++;
494 ret->Lines[i].nParts ++;
498 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
503 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
507 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
509 if( ret->Lines[i].nParts == 0 ) {
510 ret->Lines[i].Parts = NULL;
514 // Allocate part list
515 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
518 for( ptr = start, j = 0; ; j++ )
520 // Read leading whitespace
521 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
524 if( *ptr == '\0' || *ptr == '\n' ) {
525 if(*ptr == '\n') ptr ++;
529 if( *ptr == '#' || *ptr == ';' ) {
530 while( *ptr && *ptr != '\n' ) ptr ++;
531 if(*ptr == '\n') ptr ++;
535 ret->Lines[i].Parts[j] = ptr;
540 ret->Lines[i].Parts[j] = ptr;
541 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
546 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
550 // Break if we have reached NULL
552 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
557 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
561 *ptr = '\0'; // Cap off string
562 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
563 ptr ++; // And increment for the next round