3 * - By John Hodge (thePowersGang)
5 * - Architecture Independent System Init
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 *);
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 void System_EmergencyConsole(void);
28 const char *gsInitBinary = "/Acess/SBin/init";
33 void System_Init(char *CommandLine)
35 Proc_SpawnWorker(Timer_CallbackThread, NULL);
37 // Parse Kernel's Command Line
38 System_ParseCommandLine(CommandLine);
41 Log_Log("Config", "Initialising builtin modules...");
42 Modules_LoadBuiltins();
43 Arch_LoadBootModules();
45 Log_Log("Config", "Running command line '%s", CommandLine);
46 System_ExecuteCommandLine();
48 // - Execute the Config Script
49 Log_Log("Config", "Spawning init '%s'", gsInitBinary);
50 if(Proc_Clone(CLONE_VM|CLONE_NOUSER) == 0)
52 const char *args[] = {gsInitBinary, 0};
53 VFS_Open("/Devices/pts/vt0", VFS_OPENFLAG_READ|VFS_OPENFLAG_USER); // 0: stdin
54 VFS_Open("/Devices/pts/vt0", VFS_OPENFLAG_WRITE|VFS_OPENFLAG_USER); // 1: stdout
55 VFS_DuplicateFD(1, 2); // 2: stderr
56 Proc_Execve(gsInitBinary, args, &args[1], 0);
58 System_EmergencyConsole();
60 Log_KernelPanic("System", "Unable to spawn init '%s'", gsInitBinary);
63 // Set the debug to be echoed to the terminal
64 Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
65 Debug_SetKTerminal("/Devices/pts/vt7");
67 // Run a thread to reap unowned threads
71 // TODO: Inform init when a thread dies
72 int tid = Threads_WaitTID(-1, &status);
73 Log_Debug("Thread0", "Thread %i exited with status %i", tid, status);
78 * \fn void System_ParseCommandLine(char *ArgString)
79 * \brief Parses the kernel's command line and sets the environment
81 void System_ParseCommandLine(char *ArgString)
86 Log_Log("Config", "Kernel Invocation (%p) \"%s\"", ArgString, ArgString);
88 // --- Get Arguments ---
90 for( argc = 0; argc < 32; argc++ )
93 while(*str == ' ') str++;
94 // Check for the end of the string
101 while(*str && !(*str == '"' && str[-1] != '\\'))
105 while(*str && *str != ' ')
108 if(*str == '\0') break; // Check for EOS
109 *str = '\0'; // Cap off argument string
110 str ++; // and increment the string pointer
113 argc ++; // Count last argument
115 // --- Parse Arguments (Pass 1) ---
116 for( i = 1; i < argc; i++ )
121 // Ignored on this pass
125 // --- Module Paramaters ---
126 // -VTerm:Width=640,Height=480,Scrollback=2
128 System_ParseModuleArgs( argv[i] );
130 // --- Config Options ---
131 // SCRIPT=/Acess/Conf/BootConf.cfg
133 System_ParseSetting( argv[i] );
139 void System_ExecuteCommandLine(void)
143 LOG("Invocation '%s'", argv[0]);
144 for( i = 1; i < argc; i++ )
146 LOG("argv[%i] = '%s'", i, argv[i]);
150 // Mount /System=ext2:/Devices/ATA/A1
151 // Symlink /Acess=/System/Acess2
153 System_ParseVFS( argv[i] );
160 * \fn void System_ParseVFS(char *Arg)
162 void System_ParseVFS(char *Arg)
168 // Search for the '=' token
169 while( *value && *value != '=' )
172 // Check if the equals was found
173 if( *value == '\0' ) {
174 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
179 *value = '\0'; value ++;
181 // Check assignment type
182 // - Symbolic Link <link>=<destination>
185 // Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
186 VFS_Symlink(Arg, value);
188 // - Mount <mountpoint>=<fs>:<device>
193 while(*dev && *dev != ':') dev++;
199 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
200 // Log_Log("Config", "Creating directory '%s'", Arg, value);
206 // Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
207 VFS_Mount(dev, Arg, value, "");
212 * \brief Parse a module argument string
213 * \param Arg Argument string
215 void System_ParseModuleArgs(char *Arg)
223 // Find the start of the args
224 i = strpos(name, ':');
226 Log_Warning("Config", "Module spec with no arguments");
239 Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
240 Modules_SetBuiltinParams(name, args);
244 * \fn void System_ParseSetting(char *Arg)
246 void System_ParseSetting(char *Arg)
251 // Search for the '=' token
252 while( *value && *value != '=' )
255 // Check for boolean/flag (no '=')
258 //if(strcmp(Arg, "") == 0) {
260 Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
265 *value = '\0'; // Remove '='
266 value ++; // and eat it's position
268 if(strcmp(Arg, "INIT") == 0) {
269 Log_Log("Config", "Init binary: '%s'", value);
270 if(strlen(value) == 0)
273 gsInitBinary = value;
276 Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
282 void System_EmergencyConsole(void)
284 // TODO: Support an emergency kernel-land console (with FS viewing support)