diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | fake_initrd.txt | 1 | ||||
-rw-r--r-- | initrd/1.txt | 1 | ||||
-rw-r--r-- | initrd/2.txt | 1 | ||||
-rw-r--r-- | initrd/dir/3.txt | 1 | ||||
-rw-r--r-- | src/init/main.c | 3 | ||||
-rw-r--r-- | src/init/tar.c | 44 | ||||
-rw-r--r-- | src/init/tar.h | 3 |
8 files changed, 56 insertions, 3 deletions
@@ -48,7 +48,10 @@ out/raw_init: src/init/linker.ld $(call from_sources, src/init/) @mkdir -p $(@D) @$(CC) $(LFLAGS) -T $^ -o $@ -out/fs/boot/init: out/raw_init fake_initrd.txt +out/initrd.tar: $(shell find initrd/) + tar cf $@ initrd/ + +out/fs/boot/init: out/raw_init out/initrd.tar @mkdir -p $(@D) @cat $^ > $@ diff --git a/fake_initrd.txt b/fake_initrd.txt deleted file mode 100644 index 77a9fda..0000000 --- a/fake_initrd.txt +++ /dev/null @@ -1 +0,0 @@ -i am NOT a real initrd. diff --git a/initrd/1.txt b/initrd/1.txt new file mode 100644 index 0000000..8e08ed0 --- /dev/null +++ b/initrd/1.txt @@ -0,0 +1 @@ +i am /1.txt diff --git a/initrd/2.txt b/initrd/2.txt new file mode 100644 index 0000000..c214a20 --- /dev/null +++ b/initrd/2.txt @@ -0,0 +1 @@ +i am /2.txt diff --git a/initrd/dir/3.txt b/initrd/dir/3.txt new file mode 100644 index 0000000..6ab8da4 --- /dev/null +++ b/initrd/dir/3.txt @@ -0,0 +1 @@ +i am /dir/3.txt diff --git a/src/init/main.c b/src/init/main.c index db94d4a..683d0ca 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -1,3 +1,4 @@ +#include <init/tar.h> #include <init/types.h> #include <shared/flags.h> #include <shared/syscalls.h> @@ -25,8 +26,8 @@ int main(void) { _syscall_exit(argify("couldn't open tty")); fs_test(); + tar_driver(&_initrd); - _syscall_write(tty_fd, &_initrd, 23); _syscall_exit(argify("my job here is done.")); } diff --git a/src/init/tar.c b/src/init/tar.c new file mode 100644 index 0000000..22e4785 --- /dev/null +++ b/src/init/tar.c @@ -0,0 +1,44 @@ +#include <init/types.h> +#include <shared/syscalls.h> +#include <stdint.h> + +extern int tty_fd; + +static int oct_parse(char *str, size_t len); +int memcmp(const void *s1, const void *s2, size_t n); // TODO move to a dedicated file + + +void tar_driver(void *base) { + // iterate over all sectors, printing filenames + while (0 == memcmp(base + 257, "ustar", 5)) { + int size = oct_parse(base + 124, 12); + + _syscall_write(tty_fd, base, 100); + _syscall_write(tty_fd, " ", 1); + + base += 512; // skip metadata sector + base += (size + 511) & ~511; // skip file (size rounded up to 512) + // TODO might pagefault if the last sector was at a page boundary + } + _syscall_write(tty_fd, "done.", 5); +} + +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; +} + +int memcmp(const void *s1, const void *s2, size_t n) { + const unsigned char *c1 = s1, *c2 = s2; + for (size_t i = 0; i < n; i++) { + if (c1[i] != c2[i]) { + if (c1[i] < c2[i]) return -1; + else return 1; + } + } + return 0; +} diff --git a/src/init/tar.h b/src/init/tar.h new file mode 100644 index 0000000..77141b4 --- /dev/null +++ b/src/init/tar.h @@ -0,0 +1,3 @@ +#pragma once + +void tar_driver(void *base); |