summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/user/app/init/driver/tar.h4
-rw-r--r--src/user/app/init/main.c16
-rw-r--r--src/user_bootstrap/main.c63
-rw-r--r--src/user_bootstrap/tar.c (renamed from src/user/app/init/driver/tar.c)7
-rw-r--r--src/user_bootstrap/tar.h5
5 files changed, 31 insertions, 64 deletions
diff --git a/src/user/app/init/driver/tar.h b/src/user/app/init/driver/tar.h
deleted file mode 100644
index c1dee78..0000000
--- a/src/user/app/init/driver/tar.h
+++ /dev/null
@@ -1,4 +0,0 @@
-#pragma once
-#include <shared/types.h>
-
-_Noreturn void tar_driver(void *base);
diff --git a/src/user/app/init/main.c b/src/user/app/init/main.c
index a2c5e46..fe06fab 100644
--- a/src/user/app/init/main.c
+++ b/src/user/app/init/main.c
@@ -4,7 +4,6 @@
#include <user/app/init/shell.h>
#include <user/app/init/driver/driver.h>
#include <user/lib/fs/misc.h>
-#include <user/app/init/driver/tar.h>
#include <user/lib/elfload.h>
#include <user/lib/stdlib.h>
#include <user/app/init/tests/main.h>
@@ -15,21 +14,12 @@ extern char _image_base[];
void read_file(const char *path, size_t len);
__attribute__((section(".text.startup")))
-int main(void *initrd) {
+int main(void) {
elf_selfreloc();
- file_reopen(stdout, "/com1", 0);
- printf("in init, loaded at 0x%x\n", &_image_base);
+ file_reopen(stdout, "/kdev/com1", 0);
+ printf("in init (stage 2), loaded at 0x%x\n", &_image_base);
- /* move everything provided by the kernel to /kdev */
- MOUNT("/kdev/", fs_passthru(NULL));
- if (!fork2_n_mount("/")) {
- const char *l[] = {"/kdev/", NULL};
- fs_whitelist(l);
- }
- if (!fork2_n_mount("/")) fs_dir_inject("/kdev/"); // TODO should be part of fs_whitelist
-
- MOUNT("/init/", tar_driver(initrd));
MOUNT("/tmp/", tmpfs_drv());
MOUNT("/keyboard", ps2_drv());
MOUNT("/vga_tty", ansiterm_drv());
diff --git a/src/user_bootstrap/main.c b/src/user_bootstrap/main.c
index 8fed3ce..5f36aa1 100644
--- a/src/user_bootstrap/main.c
+++ b/src/user_bootstrap/main.c
@@ -2,60 +2,35 @@
#include <shared/mem.h>
#include <shared/syscalls.h>
#include <user/lib/elfload.h>
+#include <user/lib/fs/misc.h>
+
+#include "tar.h"
extern char _bss_start;
extern char _bss_end;
extern char _initrd;
-static void *tar_find(void *tar, const char *path);
-static int oct_parse(char *str, size_t len);
-
-static void *tar_find(void *tar, const char *path) {
- size_t path_len = strlen(path);
- int size;
- if (path_len > 100) return NULL;
-
- for (size_t off = 0;;) {
- if (0 != memcmp(tar + off + 257, "ustar", 5))
- return NULL; // not a metadata sector, end of archive
-
- _klogf("%s", tar + off);
- size = oct_parse(tar + off + 124, 11);
- if (0 == memcmp(tar + off, path, path_len) &&
- *(char*)(tar + off + path_len) == '\0')
- {
- return tar + off + 512;
- }
- off += 512;
- off += (size + 511) & ~511;
- }
-}
-
-static int oct_parse(char *str, size_t len) {
- int res = 0;
- for (size_t i = 0; i < len; i++) {
- res *= 8;
- res += str[i] - '0'; // no format checking
- }
- return res;
-}
-
-
__attribute__((section(".text.startup")))
int main(void) {
_syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT);
- void *init = tar_find(&_initrd, "init.elf");
- if (!init) {
- _klogf("couldn't find init.elf\n");
- _syscall_exit(0);
+ /* move everything provided by the kernel to /kdev */
+ MOUNT("/kdev/", fs_passthru(NULL));
+ if (!fork2_n_mount("/")) {
+ const char *l[] = {"/kdev/", NULL};
+ fs_whitelist(l);
}
- void (*entry)(void*) = elf_partialexec(init);
- if (entry) {
- // TODO dynamically link initrd
- entry(&_initrd);
+ if (!fork2_n_mount("/")) fs_dir_inject("/kdev/"); // TODO should be part of fs_whitelist
+
+ MOUNT("/init/", tar_driver(&_initrd));
+
+ void *init = tar_find("init.elf", 8, &_initrd, ~0) + 512;
+ if (init) {
+ _klogf("execing init.elf");
+ elf_exec(init);
+ _klogf("elf_exec failed");
} else {
- _klogf("couldn't execute init.elf\n");
+ _klogf("couldn't find init.elf");
}
- _syscall_exit(0);
+ _syscall_exit(1);
}
diff --git a/src/user/app/init/driver/tar.c b/src/user_bootstrap/tar.c
index 60deccd..40da437 100644
--- a/src/user/app/init/driver/tar.c
+++ b/src/user_bootstrap/tar.c
@@ -1,14 +1,15 @@
-#include <user/lib/stdlib.h>
#include <shared/flags.h>
+#include <shared/mem.h>
#include <shared/syscalls.h>
#include <stdint.h>
+#include "tar.h"
+
#define BUF_SIZE 64
static void *tar_open(const char *path, int len, void *base, size_t base_len);
static void tar_read(struct fs_wait_response *res, void *base, size_t base_len);
static int tar_size(void *sector);
-static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len);
static int oct_parse(char *str, size_t len);
@@ -130,7 +131,7 @@ static int tar_size(void *sector) {
return oct_parse(sector + 124, 11);
}
-static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len) {
+void *tar_find(const char *path, size_t path_len, void *base, size_t base_len) {
int size;
if (path_len > 100) return NULL; // illegal path
diff --git a/src/user_bootstrap/tar.h b/src/user_bootstrap/tar.h
new file mode 100644
index 0000000..43aa9ed
--- /dev/null
+++ b/src/user_bootstrap/tar.h
@@ -0,0 +1,5 @@
+#pragma once
+#include <shared/types.h>
+
+_Noreturn void tar_driver(void *base);
+void *tar_find(const char *path, size_t path_len, void *base, size_t base_len);