summaryrefslogtreecommitdiff
path: root/src/init/main.c
blob: 5b106df0b2a86a613db9b9161563a42e31985dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <init/driver/driver.h>
#include <init/fs/misc.h>
#include <init/shell.h>
#include <init/stdlib.h>
#include <init/tar.h>
#include <init/tests/main.h>
#include <shared/flags.h>
#include <shared/syscalls.h>
#include <stdint.h>

// TODO move to shared header file
#define argify(str) str, sizeof(str) - 1

extern char _bss_start; // provided by the linker
extern char _bss_end;
extern char _initrd;

void read_file(const char *path, size_t len);

__attribute__((section(".text.startup")))
int main(void) {
	// allocate bss
	_syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT);


	MOUNT("/init", tar_driver(&_initrd));
	MOUNT("/keyboard", ps2_drv());
	MOUNT("/vga_tty", ansiterm_drv());

	MOUNT("/bind", fs_passthru(NULL));

	if (!_syscall_fork()) {
		__stdin = __stdout = _syscall_open(argify("/com1"));
		if (__stdout < 0) _syscall_exit(1);

		shell_loop();
		_syscall_exit(1);
	}

	if (!_syscall_fork()) {
		__stdout = _syscall_open(argify("/vga_tty"));
		if (__stdout < 0) _syscall_exit(1);

		__stdin = _syscall_open(argify("/keyboard"));
		if (__stdin < 0) {
			printf("couldn't open /keyboard\n");
			_syscall_exit(1);
		}

		shell_loop();
		_syscall_exit(1);
	}

	// try to find any working output
	__stdout = _syscall_open(argify("/com1"));
	if (__stdout < 0) __stdout = _syscall_open(argify("/vga_tty"));

	for (;;) {
		_syscall_await();
		printf("init: something quit\n");
	}

	_syscall_exit(0);
}