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
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(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 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 const 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 (%p) \"%s\"", ArgString, ArgString);
103 Log_Log("Config", "Kernel Invocation '0x%x 0x%x'", ArgString[0], ArgString[1]);
105 // --- Get Arguments ---
107 for( argc = 0; argc < 32; argc++ )
110 while(*str == ' ') str++;
111 // Check for the end of the string
112 if(*str == '\0') { argc--; break;}
115 while(*str && !(*str == '"' && str[-1] != '\\'))
119 while(*str && *str != ' ')
122 if(*str == '\0') break; // Check for EOS
123 *str = '\0'; // Cap off argument string
124 str ++; // and increment the string pointer
127 argc ++; // Count last argument
129 // --- Parse Arguments (Pass 1) ---
130 for( i = 0; i < argc; i++ )
135 // Ignored on this pass
139 // --- Module Paramaters ---
140 // -VTerm:Width=640,Height=480,Scrollback=2
142 System_ParseModuleArgs( argv[i] );
144 // --- Config Options ---
145 // SCRIPT=/Acess/Conf/BootConf.cfg
147 System_ParseSetting( argv[i] );
153 void System_ExecuteCommandLine(void)
156 for( i = 0; i < argc; i++ )
158 Log("argv[%i] = '%s'", i, argv[i]);
162 // Mount /System=ext2:/Devices/ATA/A1
163 // Symlink /Acess=/System/Acess2
165 System_ParseVFS( argv[i] );
172 * \fn void System_ParseVFS(char *Arg)
174 void System_ParseVFS(char *Arg)
180 // Search for the '=' token
181 while( *value && *value != '=' )
184 // Check if the equals was found
185 if( *value == '\0' ) {
186 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
191 *value = '\0'; value ++;
193 // Check assignment type
194 // - Symbolic Link <link>=<destination>
197 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
198 VFS_Symlink(Arg, value);
200 // - Mount <mountpoint>=<fs>:<device>
205 while(*dev && *dev != ':') dev++;
211 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
212 Log_Log("Config", "Creating directory '%s'", Arg, value);
218 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
219 VFS_Mount(dev, Arg, value, "");
224 * \brief Parse a module argument string
225 * \param Arg Argument string
227 void System_ParseModuleArgs(char *Arg)
235 // Find the start of the args
236 i = strpos(name, ':');
238 Log_Warning("Config", "Module spec with no arguments");
251 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
252 Modules_SetBuiltinParams(name, args);
256 * \fn void System_ParseSetting(char *Arg)
258 void System_ParseSetting(char *Arg)
263 // Search for the '=' token
264 while( *value && *value != '=' )
267 // Check for boolean/flag (no '=')
270 //if(strcmp(Arg, "") == 0) {
272 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
277 *value = '\0'; // Remove '='
278 value ++; // and eat it's position
280 if(strcmp(Arg, "SCRIPT") == 0) {
281 Log_Log("Config", "Config Script: '%s'", value);
282 if(strlen(value) == 0)
283 gsConfigScript = NULL;
285 gsConfigScript = value;
287 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
294 * \fn void System_ExecuteScript()
295 * \brief Reads and parses the boot configuration script
297 void System_ExecuteScript(void)
304 int variables[N_VARIABLES];
305 int bReplaced[N_MAX_ARGS];
312 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
314 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
319 VFS_Seek(fp, 0, SEEK_END);
321 VFS_Seek(fp, 0, SEEK_SET);
322 // Read into memory buffer
323 fData = malloc(fLen+1);
324 VFS_Read(fp, fLen, fData);
330 file = System_Int_ParseFile(fData);
333 for( i = 0; i < file->nLines; i++ )
335 line = &file->Lines[i];
336 if( line->nParts == 0 ) continue; // Skip blank
338 if(line->Parts[0][0] == ':') continue; // Ignore labels
340 // Prescan and eliminate variables
341 for( j = 1; j < line->nParts; j++ )
343 Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
345 if( line->Parts[j][0] != '$' ) continue;
346 if( line->Parts[j][1] == '?' ) {
350 val = atoi( &line->Parts[j][1] );
351 if( val < 0 || val > N_VARIABLES ) continue;
352 val = variables[ val ];
354 Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
355 line->Parts[j] = malloc( BITS/8+2+1 );
356 sprintf(line->Parts[j], "0x%x", val);
360 // Find the command name
361 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
363 Uint args[N_MAX_ARGS];
365 if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue;
367 Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
369 // Check against minimum argument count
370 if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
371 Log_Warning("Config",
372 "Configuration command '%s' requires at least %i arguments, %i given",
373 caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
378 // Check for extra arguments
379 if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
380 Log_Warning("Config",
381 "Configuration command '%s' takes at most %i arguments, %i given",
382 caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
388 for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
389 args[k] = caConfigCommands[j].OptDefaults[k];
392 // Convert arguments to integers
393 for( k = line->nParts-1; k--; )
395 if( caConfigCommands[j].IntArgs & (1 << k) ) {
396 args[k] = atoi(line->Parts[k+1]);
399 args[k] = (Uint)line->Parts[k+1];
401 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
403 result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
404 Log_Debug("Config", "result = %i", result);
407 if( j < NUM_CONFIG_COMMANDS ) continue;
409 // --- State and Variables ---
410 if(strcmp(line->Parts[0], "set") == 0)
413 if( line->nParts-1 != 2 ) {
414 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
419 to = atoi(line->Parts[1]);
420 value = atoi(line->Parts[2]);
422 variables[to] = value;
425 // if <val1> <op> <val2> <dest>
426 else if(strcmp(line->Parts[0], "if") == 0)
428 if( line->nParts-1 != 4 ) {
429 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
433 result = atoi(line->Parts[1]);
434 val = atoi(line->Parts[3]);
436 jmpTarget = line->Parts[4];
438 Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
439 result, line->Parts[2], val, jmpTarget);
441 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;
450 else if( strcmp(line->Parts[2], ">=") == 0 ) {
451 if( result >= val ) goto jumpToLabel;
453 else if( strcmp(line->Parts[2], "=") == 0 ) {
454 if( result == val ) goto jumpToLabel;
456 else if( strcmp(line->Parts[2], "!=") == 0 ) {
457 if( result != val ) goto jumpToLabel;
460 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
464 else if(strcmp(line->Parts[0], "goto") == 0) {
465 if( line->nParts-1 != 1 ) {
466 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
469 jmpTarget = line->Parts[1];
472 for( j = 0; j < file->nLines; j ++ )
474 if(file->Lines[j].nParts == 0)
476 if(file->Lines[j].Parts[0][0] != ':')
478 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
481 if( j == file->nLines )
482 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
487 Log_Warning("Config", "Unknown configuration command '%s' on line %i",
494 // Clean up after ourselves
495 for( i = 0; i < file->nLines; i++ ) {
496 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
497 for( j = 0; j < file->Lines[i].nParts; j++ ) {
498 if(IsHeap(file->Lines[i].Parts[j]))
499 free(file->Lines[i].Parts[j]);
501 free( file->Lines[i].Parts );
510 * \brief Parses a config file
511 * \param FileData Read/Write buffer containing the config file data
513 * \return ::tConfigFile structure that represents the original contents
516 tConfigFile *System_Int_ParseFile(char *FileData)
524 ENTER("pFileData", FileData);
526 // Prescan and count the number of lines
527 for(ptr = FileData; *ptr; ptr++)
529 if(*ptr != '\n') continue;
531 if(ptr == FileData) {
537 if(ptr[-1] == '\\') continue;
542 LOG("nLines = %i", nLines);
544 // Ok so we have `nLines` lines, now to allocate our return
545 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
546 ret->nLines = nLines;
548 // Read the file for real
550 ptr = FileData, i = 0;
557 ret->Lines[i].nParts = 0;
558 ret->Lines[i].Parts = NULL;
563 // Read leading whitespace
564 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
567 if( *ptr == '\0' || *ptr == '\n' ) {
568 if(*ptr == '\n') ptr ++;
572 if( *ptr == '#' || *ptr == ';' ) {
573 while( *ptr && *ptr != '\n' ) ptr ++;
574 if(*ptr == '\n') ptr ++;
578 ret->Lines[i].nParts ++;
582 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
587 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
591 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
593 if( ret->Lines[i].nParts == 0 ) {
594 ret->Lines[i].Parts = NULL;
598 // Allocate part list
599 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
602 for( ptr = start, j = 0; ; j++ )
604 // Read leading whitespace
605 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
608 if( *ptr == '\0' || *ptr == '\n' ) {
609 if(*ptr == '\n') ptr ++;
613 if( *ptr == '#' || *ptr == ';' ) {
614 while( *ptr && *ptr != '\n' ) ptr ++;
615 if(*ptr == '\n') ptr ++;
619 ret->Lines[i].Parts[j] = ptr;
624 ret->Lines[i].Parts[j] = ptr;
625 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
630 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
634 // Break if we have reached NULL
636 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
641 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
645 *ptr = '\0'; // Cap off string
646 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
647 ptr ++; // And increment for the next round
651 if( i < ret->nLines ) {
652 ret->Lines[i].nParts = 0;
653 ret->Lines[i].Parts = NULL;
654 Log_Log("System", "Cleaning up final empty line");