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 = 1; 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 = 1; i < argc; i++ )
160 // Mount /System=ext2:/Devices/ATA/A1
161 // Symlink /Acess=/System/Acess2
163 System_ParseVFS( argv[i] );
170 * \fn void System_ParseVFS(char *Arg)
172 void System_ParseVFS(char *Arg)
178 // Search for the '=' token
179 while( *value && *value != '=' )
182 // Check if the equals was found
183 if( *value == '\0' ) {
184 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
189 *value = '\0'; value ++;
191 // Check assignment type
192 // - Symbolic Link <link>=<destination>
195 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
196 VFS_Symlink(Arg, value);
198 // - Mount <mountpoint>=<fs>:<device>
203 while(*dev && *dev != ':') dev++;
209 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
210 Log_Log("Config", "Creating directory '%s'", Arg, value);
216 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
217 VFS_Mount(dev, Arg, value, "");
222 * \biref Parse a module argument string
224 void System_ParseModuleArgs(char *Arg)
232 // Find the start of the args
233 i = strpos(name, ':');
235 Log_Warning("Config", "Module spec with no arguments");
248 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
249 Modules_SetBuiltinParams(name, args);
253 * \fn void System_ParseSetting(char *Arg)
255 void System_ParseSetting(char *Arg)
260 // Search for the '=' token
261 while( *value && *value != '=' )
264 // Check for boolean/flag (no '=')
267 //if(strcmp(Arg, "") == 0) {
269 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
274 *value = '\0'; // Remove '='
275 value ++; // and eat it's position
277 if(strcmp(Arg, "SCRIPT") == 0) {
278 Log_Log("Config", "Config Script: '%s'", value);
279 if(strlen(value) == 0)
280 gsConfigScript = NULL;
282 gsConfigScript = value;
284 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
291 * \fn void System_ExecuteScript()
292 * \brief Reads and parses the boot configuration script
294 void System_ExecuteScript(void)
301 int variables[N_VARIABLES];
302 int bReplaced[N_MAX_ARGS];
309 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
311 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
316 VFS_Seek(fp, 0, SEEK_END);
318 VFS_Seek(fp, 0, SEEK_SET);
319 // Read into memory buffer
320 fData = malloc(fLen+1);
321 VFS_Read(fp, fLen, fData);
327 file = System_Int_ParseFile(fData);
330 for( i = 0; i < file->nLines; i++ )
332 line = &file->Lines[i];
333 if( line->nParts == 0 ) continue; // Skip blank
335 if(line->Parts[0][0] == ':') continue; // Ignore labels
337 // Prescan and eliminate variables
338 for( j = 1; j < line->nParts; j++ ) {
339 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
341 if( line->Parts[j][0] != '$' ) continue;
342 if( line->Parts[j][1] == '?' ) {
346 val = atoi( &line->Parts[j][1] );
347 if( val < 0 || val > N_VARIABLES ) continue;
348 val = variables[ val ];
350 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
351 line->Parts[j] = malloc( BITS/8+2+1 );
352 sprintf(line->Parts[j], "0x%x", val);
356 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
358 Uint args[N_MAX_ARGS];
359 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
361 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
363 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
364 Log_Warning("Config",
365 "Configuration command '%s' requires at least %i arguments, %i given",
366 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
371 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
372 Log_Warning("Config",
373 "Configuration command '%s' takes at most %i arguments, %i given",
374 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
379 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
380 args[k] = caConfigCommands[j].OptDefaults[k];
383 for( k = line->nParts-1; k--; )
385 if( caConfigCommands[j].IntArgs & (1 << k) ) {
386 args[k] = atoi(line->Parts[k+1]);
389 args[k] = (Uint)line->Parts[k+1];
391 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
393 result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
394 Log_Debug("Config", "result = %i", result);
397 if( j < NUM_CONFIG_COMMANDS ) continue;
399 // --- State and Variables ---
400 if(strcmp(line->Parts[0], "set") == 0)
403 if( line->nParts-1 != 2 ) {
404 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
409 to = atoi(line->Parts[1]);
410 value = atoi(line->Parts[2]);
412 variables[to] = value;
415 // if <val1> <op> <val2> <dest>
416 else if(strcmp(line->Parts[0], "if") == 0)
418 if( line->nParts-1 != 4 ) {
419 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
423 result = atoi(line->Parts[1]);
424 val = atoi(line->Parts[3]);
426 jmpTarget = line->Parts[4];
428 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
429 result, line->Parts[2], val, jmpTarget);
431 if( strcmp(line->Parts[2], "<" ) == 0 ) {
432 if( result < val ) goto jumpToLabel;
434 else if( strcmp(line->Parts[2], "<=") == 0 ) {
435 if( result <= val ) goto jumpToLabel;
437 else if( strcmp(line->Parts[2], ">" ) == 0 ) {
438 if (result > val ) goto jumpToLabel;
440 else if( strcmp(line->Parts[2], ">=") == 0 ) {
441 if( result >= val ) goto jumpToLabel;
443 else if( strcmp(line->Parts[2], "=") == 0 ) {
444 if( result == val ) goto jumpToLabel;
446 else if( strcmp(line->Parts[2], "!=") == 0 ) {
447 if( result != val ) goto jumpToLabel;
450 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
454 else if(strcmp(line->Parts[0], "goto") == 0) {
455 if( line->nParts-1 != 1 ) {
456 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
459 jmpTarget = line->Parts[1];
462 for( j = 0; j < file->nLines; j ++ )
464 if(file->Lines[j].nParts == 0)
466 if(file->Lines[j].Parts[0][0] != ':')
468 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
471 if( j == file->nLines )
472 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
477 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
484 // Clean up after ourselves
485 for( i = 0; i < file->nLines; i++ ) {
486 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
487 for( j = 0; j < file->Lines[i].nParts; j++ ) {
488 if(IsHeap(file->Lines[i].Parts[j]))
489 free(file->Lines[i].Parts[j]);
491 free( file->Lines[i].Parts );
500 * \brief Parses a config file
501 * \param FileData Read/Write buffer containing the config file data
503 * \return ::tConfigFile structure that represents the original contents
506 tConfigFile *System_Int_ParseFile(char *FileData)
514 ENTER("pFileData", FileData);
516 // Prescan and count the number of lines
517 for(ptr = FileData; *ptr; ptr++)
519 if(*ptr != '\n') continue;
521 if(ptr == FileData) {
527 if(ptr[-1] == '\\') continue;
532 LOG("nLines = %i", nLines);
534 // Ok so we have `nLines` lines, now to allocate our return
535 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
536 ret->nLines = nLines;
538 // Read the file for real
540 ptr = FileData, i = 0;
547 ret->Lines[i].nParts = 0;
552 // Read leading whitespace
553 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
556 if( *ptr == '\0' || *ptr == '\n' ) {
557 if(*ptr == '\n') ptr ++;
561 if( *ptr == '#' || *ptr == ';' ) {
562 while( *ptr && *ptr != '\n' ) ptr ++;
563 if(*ptr == '\n') ptr ++;
567 ret->Lines[i].nParts ++;
571 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
576 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
580 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
582 if( ret->Lines[i].nParts == 0 ) {
583 ret->Lines[i].Parts = NULL;
587 // Allocate part list
588 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
591 for( ptr = start, j = 0; ; j++ )
593 // Read leading whitespace
594 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
597 if( *ptr == '\0' || *ptr == '\n' ) {
598 if(*ptr == '\n') ptr ++;
602 if( *ptr == '#' || *ptr == ';' ) {
603 while( *ptr && *ptr != '\n' ) ptr ++;
604 if(*ptr == '\n') ptr ++;
608 ret->Lines[i].Parts[j] = ptr;
613 ret->Lines[i].Parts[j] = ptr;
614 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
619 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
623 // Break if we have reached NULL
625 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
630 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
634 *ptr = '\0'; // Cap off string
635 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
636 ptr ++; // And increment for the next round