Fixes for running on an InitRD, moved kernel echo switch to later
[tpg/acess2.git] / Kernel / system.c
1 /*
2  * Acess 2
3  * Architecture Independent System Init
4  * system.c
5  */
6 #define DEBUG   0
7 #include <acess.h>
8
9 #define N_VARIABLES     16
10 #define N_MAX_ARGS      BITS
11
12 // === TYPES ===
13 typedef struct
14 {
15          int    TrueLine;
16          int    nParts;
17         char    **Parts;
18 }       tConfigLine;
19 typedef struct
20 {
21          int    nLines;
22         tConfigLine     Lines[];
23 }       tConfigFile;
24 typedef struct
25 {
26         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
32 }       tConfigCommand;
33
34 // === IMPORTS ===
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);
40
41 // === PROTOTYPES ===
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);
48
49 // === CONSTANTS ===
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
53         // --- VFS ---
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
60         
61         {"", 0,0, 0, NULL, {0}}
62 };
63 #define NUM_CONFIG_COMMANDS     (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
64
65 // === GLOBALS ===
66 char    *gsConfigScript = "/Acess/Conf/BootConf.cfg";
67
68 // === CODE ===
69 void System_Init(char *ArgString)
70 {
71         
72         // - Parse Kernel's Command Line
73         System_ParseCommandLine(ArgString);
74         
75         // - Execute the Config Script
76         Log_Log("Config", "Executing config script...");
77         System_ExecuteScript();
78         
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");
82 }
83
84 /**
85  * \fn void System_ParseCommandLine(char *ArgString)
86  * \brief Parses the kernel's command line and sets the environment
87  */
88 void System_ParseCommandLine(char *ArgString)
89 {
90         char    *argv[32];
91          int    argc;
92          int    i;
93         char    *str;
94         
95         Log_Log("Config", "Kernel Invocation \"%s\"", ArgString);
96         
97         // --- Get Arguments ---
98         str = ArgString;
99         for( argc = 0; argc < 32; argc++ )
100         {
101                 // Eat Whitespace
102                 while(*str == ' ')      str++;
103                 // Check for the end of the string
104                 if(*str == '\0') {      argc--; break;} 
105                 argv[argc] = str;
106                 while(*str && *str != ' ')
107                 {
108                         /*if(*str == '"') {
109                                 while(*str && !(*str == '"' && str[-1] != '\\'))
110                                         str ++;
111                         }*/
112                         str++;
113                 }
114                 if(*str == '\0')        break;  // Check for EOS
115                 *str = '\0';    // Cap off argument string
116                 str ++; // and increment the string pointer
117         }
118         if(argc < 32)
119                 argc ++;        // Count last argument
120         
121         // --- Parse Arguments ---
122         for( i = 1; i < argc; i++ )
123         {
124                 if( argv[i][0] == '/' )
125                         System_ParseVFS( argv[i] );
126                 else
127                         System_ParseSetting( argv[i] );
128         }
129 }
130
131 /**
132  * \fn void System_ParseVFS(char *Arg)
133  */
134 void System_ParseVFS(char *Arg)
135 {
136         char    *value;
137          int    fd;
138         
139         value = Arg;
140         // Search for the '=' token
141         while( *value && *value != '=' )
142                 value++;
143         
144         // Check if the equals was found
145         if( *value == '\0' ) {
146                 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
147                 return ;
148         }
149         
150         // Edit string
151         *value = '\0';  value ++;
152         
153         // Check assignment type
154         // - Symbolic Link <link>=<destination>
155         if(value[0] == '/')
156         {
157                 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
158                 VFS_Symlink(Arg, value);
159         }
160         // - Mount <mountpoint>=<fs>:<device>
161         else
162         {
163                 char    *dev = value;
164                 // Find colon
165                 while(*dev && *dev != ':')      dev++;
166                 if(*dev) {
167                         *dev = '\0';
168                         dev++;  // Eat ':'
169                 }
170                 // Create Mountpoint
171                 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
172                         Log_Log("Config", "Creating directory '%s'", Arg, value);
173                         VFS_MkDir( Arg );
174                 } else {
175                         VFS_Close(fd);
176                 }
177                 // Mount
178                 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
179                 VFS_Mount(dev, Arg, value, "");
180         }
181 }
182
183 /**
184  * \fn void System_ParseSetting(char *Arg)
185  */
186 void System_ParseSetting(char *Arg)
187 {
188         char    *value;
189         value = Arg;
190
191         // Search for the '=' token
192         while( *value && *value != '=' )
193                 value++;
194         
195         // Check for boolean/flag (no '=')
196         if(*value == '\0')
197         {
198                 //if(strcmp(Arg, "") == 0) {
199                 //} else {
200                         Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
201                 //}
202         }
203         else
204         {
205                 *value = '\0';  // Remove '='
206                 value ++;       // and eat it's position
207                 
208                 if(strcmp(Arg, "SCRIPT") == 0) {
209                         Log_Log("Config", "Config Script: '%s'", value);
210                         if(strlen(value) == 0)
211                                 gsConfigScript = NULL;
212                         else
213                                 gsConfigScript = value;
214                 } else {
215                         Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
216                 }
217                 
218         }
219 }
220
221 /**
222  * \fn void System_ExecuteScript()
223  */
224 void System_ExecuteScript()
225 {
226          int    fp;
227          int    fLen = 0;
228          int    i, j, k;
229          int    val;
230          int    result = 0;
231          int    variables[N_VARIABLES];
232          int    bReplaced[N_MAX_ARGS];
233         char    *fData;
234         char    *jmpTarget;
235         tConfigFile     *file;
236         tConfigLine     *line;
237         
238         // Open Script
239         fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
240         if(fp == -1) {
241                 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
242                 return;
243         }
244         
245         // Read into memory buffer
246         VFS_Seek(fp, 0, SEEK_END);
247         fLen = VFS_Tell(fp);
248         VFS_Seek(fp, 0, SEEK_SET);
249         fData = malloc(fLen+1);
250         VFS_Read(fp, fLen, fData);
251         fData[fLen] = '\0';
252         VFS_Close(fp);
253         
254         
255         // Parse File
256         file = System_Int_ParseFile(fData);
257         
258         // Parse each line
259         for( i = 0; i < file->nLines; i++ )
260         {
261                 line = &file->Lines[i];
262                 if( line->nParts == 0 ) continue;       // Skip blank
263                 
264                 if(line->Parts[0][0] == ':')    continue;       // Ignore labels
265                 
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]);
269                         bReplaced[j] = 0;
270                         if( line->Parts[j][0] != '$' )  continue;
271                         if( line->Parts[j][1] == '?' ) {
272                                 val = result;
273                         }
274                         else {
275                                 val = atoi( &line->Parts[j][1] );
276                                 if( val < 0 || val > N_VARIABLES )      continue;
277                                 val = variables[ val ];
278                         }
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);
282                         bReplaced[j] = 1;
283                 }
284                 
285                 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
286                 {
287                         Uint    args[N_MAX_ARGS];
288                         if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0)       continue;
289                         
290                         Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
291                         
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
296                                         );
297                                 break;
298                         }
299                         
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
304                                         );
305                                 break;
306                         }
307                         
308                         for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
309                                 args[k] = caConfigCommands[j].OptDefaults[k];
310                         }
311                         
312                         for( k = line->nParts-1; k--; )
313                         {
314                                 if( caConfigCommands[j].IntArgs & (1 << k) ) {
315                                         args[k] = atoi(line->Parts[k+1]);
316                                 }
317                                 else {
318                                         args[k] = (Uint)line->Parts[k+1];
319                                 }
320                                 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
321                         }
322                         result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
323                         Log_Debug("Config", "result = %i", result);
324                         break;
325                 }
326                 if( j < NUM_CONFIG_COMMANDS )   continue;
327                         
328                 // --- State and Variables ---
329                 if(strcmp(line->Parts[0], "set") == 0)
330                 {
331                          int    to, value;
332                         if( line->nParts-1 != 2 ) {
333                                 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
334                                         line->nParts-1);
335                                 continue;
336                         }
337                         
338                         to = atoi(line->Parts[1]);
339                         value = atoi(line->Parts[2]);
340                         
341                         variables[to] = value;
342                         result = value;
343                 }
344                 // if <val1> <op> <val2> <dest>
345                 else if(strcmp(line->Parts[0], "if") == 0)
346                 {
347                         if( line->nParts-1 != 4 ) {
348                                 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
349                                         line->nParts-1);
350                         }
351                         
352                         result = atoi(line->Parts[1]);
353                         val = atoi(line->Parts[3]);
354                         
355                         jmpTarget = line->Parts[4];
356                         
357                         Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
358                                 result, line->Parts[2], val, jmpTarget);
359                         
360                         if( strcmp(line->Parts[2], "<" ) == 0 ) {
361                                 if( result < val )      goto jumpToLabel;
362                         }
363                         else if( strcmp(line->Parts[2], "<=") == 0 ) {
364                                 if( result <= val )     goto jumpToLabel;
365                         }
366                         else if( strcmp(line->Parts[2], ">" ) == 0 ) {
367                                 if (result > val )      goto jumpToLabel;
368                         }
369                         else if( strcmp(line->Parts[2], ">=") == 0 ) {
370                                 if( result >= val )     goto jumpToLabel;
371                         }
372                         else if( strcmp(line->Parts[2],  "=") == 0 ) {
373                                 if( result == val )     goto jumpToLabel;
374                         }
375                         else if( strcmp(line->Parts[2], "!=") == 0 ) {
376                                 if( result != val )     goto jumpToLabel;
377                         }
378                         else {
379                                 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
380                         }
381                         
382                 }
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",
386                                         line->nParts-1);
387                         }
388                         jmpTarget = line->Parts[1];
389                 
390                 jumpToLabel:
391                         for( j = 0; j < file->nLines; j ++ )
392                         {
393                                 if(file->Lines[j].nParts == 0)
394                                         continue;
395                                 if(file->Lines[j].Parts[0][0] != ':')
396                                         continue;
397                                 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
398                                         break;
399                         }
400                         if( j == file->nLines )
401                                 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
402                         else
403                                 i = j;
404                 }
405                 else {
406                         Log_Warning("Config", "Unknown configuration command '%s' on line %i",
407                                 line->Parts[0],
408                                 line->TrueLine
409                                 );
410                 }
411         }
412         
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]);
419                 }
420                 free( file->Lines[i].Parts );
421         }
422         free( file );
423         free( fData );
424 }
425
426 /**
427  * \brief Parses a config file
428  * \param FileData      Read/Write buffer containing the config file data
429  *                  (will be modified)
430  * \return ::tConfigFile structure that represents the original contents
431  *         of \a FileData
432  */
433 tConfigFile     *System_Int_ParseFile(char *FileData)
434 {
435         char    *ptr;
436         char    *start;
437          int    nLines = 1;
438          int    i, j;
439         tConfigFile     *ret;
440         
441         ENTER("pFileData", FileData);
442         
443         // Prescan and count the number of lines
444         for(ptr = FileData; *ptr; ptr++)
445         {               
446                 if(*ptr != '\n')        continue;
447                 
448                 if(ptr == FileData) {
449                         nLines ++;
450                         continue;
451                 }
452                 
453                 // Escaped EOL
454                 if(ptr[-1] == '\\')     continue;
455                 
456                 nLines ++;
457         }
458         
459         LOG("nLines = %i", nLines);
460         
461         // Ok so we have `nLines` lines, now to allocate our return
462         ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
463         ret->nLines = nLines;
464         
465         // Read the file for real
466         for(
467                 ptr = FileData, i = 0;
468                 *ptr;
469                 i++
470                 )
471         {
472                 start = ptr;
473                 
474                 ret->Lines[i].nParts = 0;
475                 
476                 // Count parts
477                 for(;;)
478                 {
479                         // Read leading whitespace
480                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
481                         
482                         // End of line/file
483                         if( *ptr == '\0' || *ptr == '\n' ) {
484                                 if(*ptr == '\n')        ptr ++;
485                                 break;
486                         }
487                         // Comment
488                         if( *ptr == '#' || *ptr == ';' ) {
489                                 while( *ptr && *ptr != '\n' )   ptr ++;
490                                 if(*ptr == '\n')        ptr ++;
491                                 break;
492                         }
493                         
494                         ret->Lines[i].nParts ++;
495                         // Quoted
496                         if( *ptr == '"' ) {
497                                 ptr ++;
498                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
499                                         ptr++;
500                                 continue;
501                         }
502                         // Unquoted
503                         while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
504                                 ptr++;
505                 }
506                 
507                 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
508                 
509                 if( ret->Lines[i].nParts == 0 ) {
510                         ret->Lines[i].Parts = NULL;
511                         continue;
512                 }
513                 
514                 // Allocate part list
515                 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
516                 
517                 // Fill list
518                 for( ptr = start, j = 0; ; j++ )
519                 {
520                         // Read leading whitespace
521                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
522                         
523                         // End of line/file
524                         if( *ptr == '\0' || *ptr == '\n' ) {
525                                 if(*ptr == '\n')        ptr ++;
526                                 break;
527                         }
528                         // Comment
529                         if( *ptr == '#' || *ptr == ';' ) {
530                                 while( *ptr && *ptr != '\n' )   ptr ++;
531                                 if(*ptr == '\n')        ptr ++;
532                                 break;
533                         }
534                         
535                         ret->Lines[i].Parts[j] = ptr;
536                         
537                         // Quoted
538                         if( *ptr == '"' ) {
539                                 ptr ++;
540                                 ret->Lines[i].Parts[j] = ptr;
541                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
542                                         ptr++;
543                         }
544                         // Unquoted
545                         else {
546                                 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
547                                         ptr++;
548                         }
549                         
550                         // Break if we have reached NULL
551                         if( *ptr == '\0' ) {
552                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
553                                 break;
554                         }
555                         if( *ptr == '\n' ) {
556                                 *ptr = '\0';
557                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
558                                 ptr ++;
559                                 break;
560                         }
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
564                 }
565         }
566         
567         LEAVE('p', ret);
568         return ret;
569 }

UCC git Repository :: git.ucc.asn.au