3 * Architecture Independent System Init
23 extern int Modules_LoadBuiltins();
24 //extern int PCI_Install();
25 extern void DMA_Install();
26 extern void Debug_SetKTerminal(char *File);
27 extern void StartupPrint(char *Str);
30 void System_Init(char *ArgString);
31 void System_ParseCommandLine(char *ArgString);
32 void System_ParseVFS(char *Arg);
33 void System_ParseSetting(char *Arg);
34 void System_ExecuteScript();
35 tConfigFile *System_Int_ParseFile(char *File);
38 char *gsConfigScript = "/Acess/Conf/BootConf.cfg";
41 void System_Init(char *ArgString)
43 // Set the debug to be echoed to the terminal
44 StartupPrint("Kernel now echoes to VT7 (Ctrl-Alt-F8)");
45 Debug_SetKTerminal("/Devices/VTerm/7");
47 // - Parse Kernel's Command Line
48 System_ParseCommandLine(ArgString);
50 // - Execute the Config Script
51 Log("Executing config script...");
52 System_ExecuteScript();
56 * \fn void System_ParseCommandLine(char *ArgString)
57 * \brief Parses the kernel's command line and sets the environment
59 void System_ParseCommandLine(char *ArgString)
66 Log("Kernel Command Line: \"%s\"", ArgString);
68 // --- Get Arguments ---
70 for( argc = 0; argc < 32; argc++ )
73 while(*str == ' ') str++;
74 // Check for the end of the string
75 if(*str == '\0') { argc--; break;}
77 while(*str && *str != ' ')
80 while(*str && !(*str == '"' && str[-1] != '\\'))
85 if(*str == '\0') break; // Check for EOS
86 *str = '\0'; // Cap off argument string
87 str ++; // and increment the string pointer
90 argc ++; // Count last argument
92 // --- Parse Arguments ---
93 for( i = 1; i < argc; i++ )
95 if( argv[i][0] == '/' )
96 System_ParseVFS( argv[i] );
98 System_ParseSetting( argv[i] );
103 * \fn void System_ParseVFS(char *Arg)
105 void System_ParseVFS(char *Arg)
111 // Search for the '=' token
112 while( *value && *value != '=' )
115 // Check if the equals was found
116 if( *value == '\0' ) {
117 Warning("Expected '=' in the string '%s'", Arg);
122 *value = '\0'; value ++;
124 // Check assignment type
125 // - Symbolic Link <link>=<destination>
128 Log("Symbolic link '%s' pointing to '%s'", Arg, value);
129 VFS_Symlink(Arg, value);
131 // - Mount <mountpoint>=<fs>:<device>
136 while(*dev && *dev != ':') dev++;
142 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
143 Log("Creating directory '%s'", Arg, value);
149 Log("Mounting '%s' to '%s' ('%s')", dev, Arg, value);
150 VFS_Mount(dev, Arg, value, "");
155 * \fn void System_ParseSetting(char *Arg)
157 void System_ParseSetting(char *Arg)
162 // Search for the '=' token
163 while( *value && *value != '=' )
166 // Check for boolean/flag (no '=')
169 //if(strcmp(Arg, "") == 0) {
171 Warning("Kernel flag '%s' is not recognised", Arg);
176 *value = '\0'; // Remove '='
177 value ++; // and eat it's position
179 if(strcmp(Arg, "SCRIPT") == 0) {
180 Log("Config Script: '%s'", value);
181 gsConfigScript = value;
183 Warning("Kernel config setting '%s' is not recognised", Arg);
190 * \fn void System_ExecuteScript()
192 void System_ExecuteScript()
202 fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
204 Warning("[CFG] Passed script '%s' does not exist", gsConfigScript);
208 // Read into memory buffer
209 VFS_Seek(fp, 0, SEEK_END);
211 VFS_Seek(fp, 0, SEEK_SET);
212 fData = malloc(fLen+1);
213 VFS_Read(fp, fLen, fData);
220 file = System_Int_ParseFile(fData);
223 for( i = 0; i < file->nLines; i++ )
225 line = &file->Lines[i];
226 if( line->nParts == 0 ) continue; // Skip blank
229 if( strcmp(line->Parts[0], "mount") == 0 )
231 if( line->nParts != 4 ) {
232 Warning("Configuration command 'mount' requires 3 arguments, %i given",
236 //Log("[CFG ] Mount '%s' to '%s' (%s)",
237 // line->Parts[1], line->Parts[2], line->Parts[3]);
238 //! \todo Use an optional 4th argument for the options string
239 VFS_Mount(line->Parts[1], line->Parts[2], line->Parts[3], "");
242 else if(strcmp(line->Parts[0], "module") == 0)
244 if( line->nParts < 2 || line->nParts > 3 ) {
245 Warning("Configuration command 'module' requires 1 or 2 arguments, %i given",
249 if( line->nParts == 3 )
250 Module_LoadFile(line->Parts[1], line->Parts[2]);
252 Module_LoadFile(line->Parts[1], "");
255 else if(strcmp(line->Parts[0], "udimod") == 0)
257 if( line->nParts != 2 ) {
258 Warning("Configuration command 'udimod' requires 1 argument, %i given",
262 Log("[CFG ] Load UDI Module '%s'", line->Parts[1]);
263 Module_LoadFile(line->Parts[1], "");
266 else if(strcmp(line->Parts[0], "edimod") == 0)
268 if( line->nParts != 2 ) {
269 Warning("Configuration command 'edimod' requires 1 argument, %i given",
273 Log("[CFG ] Load EDI Module '%s'", line->Parts[1]);
274 Module_LoadFile(line->Parts[1], "");
276 // Create a Symbolic Link
277 else if(strcmp(line->Parts[0], "symlink") == 0)
279 if( line->nParts != 3 ) {
280 Warning("Configuration command 'symlink' requires 2 arguments, %i given",
284 Log("[CFG ] Symlink '%s' pointing to '%s'",
285 line->Parts[1], line->Parts[2]);
286 VFS_Symlink(line->Parts[1], line->Parts[2]);
288 // Create a Directory
289 else if(strcmp(line->Parts[0], "mkdir") == 0)
291 if( line->nParts != 2 ) {
292 Warning("Configuration command 'mkdir' requires 1 argument, %i given",
296 Log("[CFG ] New Directory '%s'", line->Parts[1]);
297 VFS_MkDir(line->Parts[1]);
300 else if(strcmp(line->Parts[0], "spawn") == 0)
302 if( line->nParts != 2 ) {
303 Warning("Configuration command 'spawn' requires 1 argument, %i given",
307 Log("[CFG ] Starting '%s' as a new task", line->Parts[1]);
308 Proc_Spawn(line->Parts[1]);
311 Warning("Unknown configuration command '%s' on line %i",
318 // Clean up after ourselves
319 for( i = 0; i < file->nLines; i++ ) {
320 if( file->Lines[i].nParts == 0 ) continue; // Skip blank
321 free( file->Lines[i].Parts );
328 * \brief Parses a config file
329 * \param FileData Read/Write buffer containing the config file data
331 * \return ::tConfigFile structure that represents the original contents
334 tConfigFile *System_Int_ParseFile(char *FileData)
342 ENTER("pFileData", FileData);
344 // Prescan and count the number of lines
345 for(ptr = FileData; *ptr; ptr++)
347 if(*ptr != '\n') continue;
349 if(ptr == FileData) {
355 if(ptr[-1] == '\\') continue;
360 LOG("nLines = %i", nLines);
362 // Ok so we have `nLines` lines, now to allocate our return
363 ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
364 ret->nLines = nLines;
366 // Read the file for real
368 ptr = FileData, i = 0;
375 ret->Lines[i].nParts = 0;
380 // Read leading whitespace
381 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
384 if( *ptr == '\0' || *ptr == '\n' ) {
385 if(*ptr == '\n') ptr ++;
389 if( *ptr == '#' || *ptr == ';' ) {
390 while( *ptr && *ptr != '\n' ) ptr ++;
391 if(*ptr == '\n') ptr ++;
395 ret->Lines[i].nParts ++;
399 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
404 while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
408 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
410 if( ret->Lines[i].nParts == 0 ) {
411 ret->Lines[i].Parts = NULL;
415 // Allocate part list
416 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
419 for( ptr = start, j = 0; ; j++ )
421 // Read leading whitespace
422 while( *ptr == '\t' || *ptr == ' ' ) ptr++;
425 if( *ptr == '\0' || *ptr == '\n' ) {
426 if(*ptr == '\n') ptr ++;
430 if( *ptr == '#' || *ptr == ';' ) {
431 while( *ptr && *ptr != '\n' ) ptr ++;
432 if(*ptr == '\n') ptr ++;
436 ret->Lines[i].Parts[j] = ptr;
441 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
446 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
450 // Break if we have reached NULL
452 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
457 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
461 *ptr = '\0'; // Cap off string
462 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
463 ptr ++; // And increment for the next round