Kernel - Changed PTY code to not expose userland server node as a file
[tpg/acess2.git] / KernelLand / Kernel / system.c
1 /*
2  * Acess 2 Kernel
3  * - By John Hodge (thePowersGang)
4  * system.c
5  * - Architecture Independent System Init
6  */
7 #define DEBUG   1
8 #include <acess.h>
9 #include <hal_proc.h>
10
11 // === IMPORTS ===
12 extern void     Arch_LoadBootModules(void);
13 extern int      Modules_LoadBuiltins(void);
14 extern void     Modules_SetBuiltinParams(char *Name, char *ArgString);
15 extern void     Debug_SetKTerminal(const char *File);
16 extern void     Timer_CallbackThread(void *);
17
18 // === PROTOTYPES ===
19 void    System_Init(char *Commandline);
20 void    System_ParseCommandLine(char *ArgString);
21 void    System_ExecuteCommandLine(void);
22 void    System_ParseVFS(char *Arg);
23 void    System_ParseModuleArgs(char *Arg);
24 void    System_ParseSetting(char *Arg);
25
26 // === GLOBALS ===
27 const char      *gsInitBinary = "/Acess/SBin/init";
28 char    *argv[32];
29  int    argc;
30
31 // === CODE ===
32 void System_Init(char *CommandLine)
33 {
34         Proc_SpawnWorker(Timer_CallbackThread, NULL);
35
36         // Parse Kernel's Command Line
37         System_ParseCommandLine(CommandLine);
38         
39         // Initialise modules
40         Log_Log("Config", "Initialising builtin modules...");
41         Modules_LoadBuiltins();
42         Arch_LoadBootModules();
43         
44         System_ExecuteCommandLine();
45         
46         // - Execute the Config Script
47         Log_Log("Config", "Spawning init '%s'", gsInitBinary);
48         if(Proc_Clone(CLONE_VM|CLONE_NOUSER) == 0)
49         {
50                 const char      *args[] = {gsInitBinary, 0};
51                 VFS_Open("/Devices/pts/vt0", VFS_OPENFLAG_READ|VFS_OPENFLAG_USER);      // 0: stdin
52                 VFS_Open("/Devices/pts/vt0", VFS_OPENFLAG_WRITE|VFS_OPENFLAG_USER);     // 1: stdout
53                 VFS_DuplicateFD(1, 2);  // 2: stderr
54                 Proc_Execve(gsInitBinary, args, &args[1], 0);
55                 Log_KernelPanic("System", "Unable to spawn init '%s'", gsInitBinary);
56         }
57         
58         // Set the debug to be echoed to the terminal
59         Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
60         Debug_SetKTerminal("/Devices/pts/vt7");
61 }
62
63 /**
64  * \fn void System_ParseCommandLine(char *ArgString)
65  * \brief Parses the kernel's command line and sets the environment
66  */
67 void System_ParseCommandLine(char *ArgString)
68 {
69          int    i;
70         char    *str;
71         
72         Log_Log("Config", "Kernel Invocation (%p) \"%s\"", ArgString, ArgString);
73         
74         // --- Get Arguments ---
75         str = ArgString;
76         for( argc = 0; argc < 32; argc++ )
77         {
78                 // Eat Whitespace
79                 while(*str == ' ')      str++;
80                 // Check for the end of the string
81                 if(*str == '\0') {      argc--; break;} 
82                 argv[argc] = str;
83                 if(*str == '"') {
84                         while(*str && !(*str == '"' && str[-1] != '\\'))
85                                 str ++;
86                 }
87                 else {
88                         while(*str && *str != ' ')
89                                 str++;
90                 }
91                 if(*str == '\0')        break;  // Check for EOS
92                 *str = '\0';    // Cap off argument string
93                 str ++; // and increment the string pointer
94         }
95         if(argc < 32)
96                 argc ++;        // Count last argument
97         
98         // --- Parse Arguments (Pass 1) ---
99         for( i = 1; i < argc; i++ )
100         {
101                 switch(argv[i][0])
102                 {
103                 // --- VFS ---
104                 // Ignored on this pass
105                 case '/':
106                         break;
107                 
108                 // --- Module Paramaters ---
109                 // -VTerm:Width=640,Height=480,Scrollback=2
110                 case '-':
111                         System_ParseModuleArgs( argv[i] );
112                         break;
113                 // --- Config Options ---
114                 // SCRIPT=/Acess/Conf/BootConf.cfg
115                 default:
116                         System_ParseSetting( argv[i] );
117                         break;
118                 }
119         }
120 }
121
122 void System_ExecuteCommandLine(void)
123 {
124          int    i;
125         if(argc > 0)
126                 LOG("Invocation '%s'", argv[0]);
127         for( i = 1; i < argc; i++ )
128         {
129                 LOG("argv[%i] = '%s'", i, argv[i]);
130                 switch(argv[i][0])
131                 {
132                 // --- VFS ---
133                 // Mount    /System=ext2:/Devices/ATA/A1
134                 // Symlink  /Acess=/System/Acess2
135                 case '/':
136                         System_ParseVFS( argv[i] );
137                         break;
138                 }
139         }
140 }
141
142 /**
143  * \fn void System_ParseVFS(char *Arg)
144  */
145 void System_ParseVFS(char *Arg)
146 {
147         char    *value;
148          int    fd;
149         
150         value = Arg;
151         // Search for the '=' token
152         while( *value && *value != '=' )
153                 value++;
154         
155         // Check if the equals was found
156         if( *value == '\0' ) {
157                 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
158                 return ;
159         }
160         
161         // Edit string
162         *value = '\0';  value ++;
163         
164         // Check assignment type
165         // - Symbolic Link <link>=<destination>
166         if(value[0] == '/')
167         {
168 //              Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
169                 VFS_Symlink(Arg, value);
170         }
171         // - Mount <mountpoint>=<fs>:<device>
172         else
173         {
174                 char    *dev = value;
175                 // Find colon
176                 while(*dev && *dev != ':')      dev++;
177                 if(*dev) {
178                         *dev = '\0';
179                         dev++;  // Eat ':'
180                 }
181                 // Create Mountpoint
182                 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
183 //                      Log_Log("Config", "Creating directory '%s'", Arg, value);
184                         VFS_MkDir( Arg );
185                 } else {
186                         VFS_Close(fd);
187                 }
188                 // Mount
189 //              Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
190                 VFS_Mount(dev, Arg, value, "");
191         }
192 }
193
194 /**
195  * \brief Parse a module argument string
196  * \param Arg   Argument string
197  */
198 void System_ParseModuleArgs(char *Arg)
199 {
200         char    *name, *args;
201          int    i;
202         
203         // Remove '-'   
204         name = Arg + 1;
205         
206         // Find the start of the args
207         i = strpos(name, ':');
208         if( i == -1 ) {
209                 Log_Warning("Config", "Module spec with no arguments");
210                 #if 1
211                 return ;
212                 #else
213                 i = strlen(name);
214                 args = name + i;
215                 #endif
216         }
217         else {
218                 name[i] = '\0';
219                 args = name + i + 1;
220         }
221         
222         Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
223         Modules_SetBuiltinParams(name, args);
224 }
225
226 /**
227  * \fn void System_ParseSetting(char *Arg)
228  */
229 void System_ParseSetting(char *Arg)
230 {
231         char    *value;
232         value = Arg;
233
234         // Search for the '=' token
235         while( *value && *value != '=' )
236                 value++;
237         
238         // Check for boolean/flag (no '=')
239         if(*value == '\0')
240         {
241                 //if(strcmp(Arg, "") == 0) {
242                 //} else {
243                         Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
244                 //}
245         }
246         else
247         {
248                 *value = '\0';  // Remove '='
249                 value ++;       // and eat it's position
250                 
251                 if(strcmp(Arg, "INIT") == 0) {
252                         Log_Log("Config", "Init binary: '%s'", value);
253                         if(strlen(value) == 0)
254                                 gsInitBinary = NULL;
255                         else
256                                 gsInitBinary = value;
257                 }
258                 else {
259                         Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
260                 }
261                 
262         }
263 }
264

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