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)
71 // Set the debug to be echoed to the terminal
72 StartupPrint("Kernel now echoes to VT7 (Ctrl-Alt-F8)");
73 Debug_SetKTerminal("/Devices/VTerm/7");
75 // - Parse Kernel's Command Line
76 System_ParseCommandLine(ArgString);
78 // - Execute the Config Script
79 Log_Log("Config", "Executing config script...");
80 System_ExecuteScript();
84 * \fn void System_ParseCommandLine(char *ArgString)
85 * \brief Parses the kernel's command line and sets the environment
87 void System_ParseCommandLine(char *ArgString)
94 Log_Log("Config", "Kernel Invocation \"%s\"", ArgString);
96 // --- Get Arguments ---
98 for( argc = 0; argc < 32; argc++ )
101 while(*str == ' ') str++;
102 // Check for the end of the string
103 if(*str == '\0') { argc--; break;}
105 while(*str && *str != ' ')
108 while(*str && !(*str == '"' && str[-1] != '\\'))
113 if(*str == '\0') break; // Check for EOS
114 *str = '\0'; // Cap off argument string
115 str ++; // and increment the string pointer
118 argc ++; // Count last argument
120 // --- Parse Arguments ---
121 for( i = 1; i < argc; i++ )
123 if( argv[i][0] == '/' )
124 System_ParseVFS( argv[i] );
126 System_ParseSetting( argv[i] );
131 * \fn void System_ParseVFS(char *Arg)
133 void System_ParseVFS(char *Arg)
139 // Search for the '=' token
140 while( *value && *value != '=' )
143 // Check if the equals was found
144 if( *value == '\0' ) {
145 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
150 *value = '\0'; value ++;
152 // Check assignment type
153 // - Symbolic Link <link>=<destination>
156 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
157 VFS_Symlink(Arg, value);
159 // - Mount <mountpoint>=<fs>:<device>
164 while(*dev && *dev != ':') dev++;
170 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
171 Log_Log("Config", "Creating directory '%s'", Arg, value);
177 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
178 VFS_Mount(dev, Arg, value, "");
183 * \fn void System_ParseSetting(char *Arg)
185 void System_ParseSetting(char *Arg)
190 // Search for the '=' token
191 while( *value && *value != '=' )
194 // Check for boolean/flag (no '=')
197 //if(strcmp(Arg, "") == 0) {
199 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
204 *value = '\0'; // Remove '='
205 value ++; // and eat it's position
207 if(strcmp(Arg, "SCRIPT") == 0) {
208 Log_Log("Config", "Config Script: '%s'", value);
209 gsConfigScript = value;
211 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
218 * \fn void System_ExecuteScript()
220 void System_ExecuteScript()
227 int variables[N_VARIABLES];
228 int bReplaced[N_MAX_ARGS];
235 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
237 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
241 // Read into memory buffer
242 VFS_Seek(fp, 0, SEEK_END);
244 VFS_Seek(fp, 0, SEEK_SET);
245 fData = malloc(fLen+1);
246 VFS_Read(fp, fLen, fData);
252 file = System_Int_ParseFile(fData);
255 for( i = 0; i < file->nLines; i++ )
257 line = &file->Lines[i];
258 if( line->nParts == 0 ) continue; // Skip blank
260 if(line->Parts[0][0] == ':') continue; // Ignore labels
262 // Prescan and eliminate variables
263 for( j = 1; j < line->nParts; j++ ) {
264 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
266 if( line->Parts[j][0] != '$' ) continue;
267 if( line->Parts[j][1] == '?' ) {
271 val = atoi( &line->Parts[j][1] );
272 if( val < 0 || val > N_VARIABLES ) continue;
273 val = variables[ val ];
275 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
276 line->Parts[j] = malloc( BITS/8+2+1 );
277 sprintf(line->Parts[j], "0x%x", val);
281 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
283 Uint args[N_MAX_ARGS];
284 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
286 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
288 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
289 Log_Warning("Config",
290 "Configuration command '%s' requires at least %i arguments, %i given",
291 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
296 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
297 Log_Warning("Config",
298 "Configuration command '%s' takes at most %i arguments, %i given",
299 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
304 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
305 args[k] = caConfigCommands[j].OptDefaults[k];
308 for( k = line->nParts-1; k--; )
310 if( caConfigCommands[j].IntArgs & (1 << k) ) {
311 args[k] = atoi(line->Parts[k+1]);
314 args[k] = (Uint)line->Parts[k+1];
316 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
318 result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
319 Log_Debug("Config", "result = %i", result);
322 if( j < NUM_CONFIG_COMMANDS ) continue;
324 // --- State and Variables ---
325 if(strcmp(line->Parts[0], "set") == 0)
328 if( line->nParts-1 != 2 ) {
329 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
334 to = atoi(line->Parts[1]);
335 value = atoi(line->Parts[2]);
337 variables[to] = value;
340 // if <val1> <op> <val2> <dest>
341 else if(strcmp(line->Parts[0], "if") == 0)
343 if( line->nParts-1 != 4 ) {
344 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
348 result = atoi(line->Parts[1]);
349 val = atoi(line->Parts[3]);
351 jmpTarget = line->Parts[4];
353 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
354 result, line->Parts[2], val, jmpTarget);
356 if( strcmp(line->Parts[2], "<" ) == 0 ) {
357 if( result < val ) goto jumpToLabel;
359 else if( strcmp(line->Parts[2], "<=") == 0 ) {
360 if( result <= val ) goto jumpToLabel;
362 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
363 if (result > val ) goto jumpToLabel;
365 else if( strcmp(line->Parts[2], ">=") == 0 ) {
366 if( result >= val ) goto jumpToLabel;
368 else if( strcmp(line->Parts[2], "=") == 0 ) {
369 if( result == val ) goto jumpToLabel;
371 else if( strcmp(line->Parts[2], "!=") == 0 ) {
372 if( result != val ) goto jumpToLabel;
375 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
379 else if(strcmp(line->Parts[0], "goto") == 0) {
380 if( line->nParts-1 != 1 ) {
381 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
384 jmpTarget = line->Parts[1];
387 for( j = 0; j < file->nLines; j ++ )
389 if(file->Lines[j].nParts == 0)
391 if(file->Lines[j].Parts[0][0] != ':')
393 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
396 if( j == file->nLines )
397 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
402 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
409 // Clean up after ourselves
410 for( i = 0; i < file->nLines; i++ ) {
411 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
412 for( j = 0; j < file->Lines[i].nParts; j++ ) {
413 if(IsHeap(file->Lines[i].Parts[j]))
414 free(file->Lines[i].Parts[j]);
416 free( file->Lines[i].Parts );
423 * \brief Parses a config file
424 * \param FileData Read/Write buffer containing the config file data
426 * \return ::tConfigFile structure that represents the original contents
429 tConfigFile *System_Int_ParseFile(char *FileData)
437 ENTER("pFileData", FileData);
439 // Prescan and count the number of lines
440 for(ptr = FileData; *ptr; ptr++)
442 if(*ptr != '\n') continue;
444 if(ptr == FileData) {
450 if(ptr[-1] == '\\') continue;
455 LOG("nLines = %i", nLines);
457 // Ok so we have `nLines` lines, now to allocate our return
458 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
459 ret->nLines = nLines;
461 // Read the file for real
463 ptr = FileData, i = 0;
470 ret->Lines[i].nParts = 0;
475 // Read leading whitespace
476 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
479 if( *ptr == '\0' || *ptr == '\n' ) {
480 if(*ptr == '\n') ptr ++;
484 if( *ptr == '#' || *ptr == ';' ) {
485 while( *ptr && *ptr != '\n' ) ptr ++;
486 if(*ptr == '\n') ptr ++;
490 ret->Lines[i].nParts ++;
494 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
499 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
503 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
505 if( ret->Lines[i].nParts == 0 ) {
506 ret->Lines[i].Parts = NULL;
510 // Allocate part list
511 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
514 for( ptr = start, j = 0; ; j++ )
516 // Read leading whitespace
517 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
520 if( *ptr == '\0' || *ptr == '\n' ) {
521 if(*ptr == '\n') ptr ++;
525 if( *ptr == '#' || *ptr == ';' ) {
526 while( *ptr && *ptr != '\n' ) ptr ++;
527 if(*ptr == '\n') ptr ++;
531 ret->Lines[i].Parts[j] = ptr;
536 ret->Lines[i].Parts[j] = ptr;
537 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
542 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
546 // Break if we have reached NULL
548 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
553 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
557 *ptr = '\0'; // Cap off string
558 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
559 ptr ++; // And increment for the next round