From 836e4eba09e73612b00f268e124750e78aeaa225 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 30 Oct 2019 21:18:08 +0100 Subject: Added kind of working initrd support --- Makefile | 21 ++++-------------- src/bootloader/grub.cfg | 7 ++++++ src/bootloader/make_initrd.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/bootloader/test.txt | 1 + src/kernel/graphics/vesa.c | 2 ++ src/kernel/graphics/vesa.h | 2 ++ src/kernel/grub.cfg | 7 ------ src/kernel/kernel.c | 30 ++++++++++++++++++++----- src/utils/make_initrd.c | 51 ------------------------------------------ src/utils/test.txt | 1 - 10 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 src/bootloader/grub.cfg create mode 100644 src/bootloader/make_initrd.c create mode 100644 src/bootloader/test.txt delete mode 100644 src/kernel/grub.cfg delete mode 100644 src/utils/make_initrd.c delete mode 100644 src/utils/test.txt diff --git a/Makefile b/Makefile index 9f28fbb..cbb23b0 100644 --- a/Makefile +++ b/Makefile @@ -40,25 +40,12 @@ build: clean # Create ISO mkdir -p ./iso/boot/grub; \ cp ./build/melvix.bin ./iso/boot/; \ - cp ./src/kernel/grub.cfg ./iso/boot/grub/; \ + cp ./src/bootloader/grub.cfg ./iso/boot/grub/; \ + gcc -w ./src/bootloader/make_initrd.c -o ./build/make_initrd || exit; \ + ./build/make_initrd ./src/bootloader/test.txt test.txt || exit; \ + mv initrd.img ./iso/boot/melvix.initrd || exit; \ grub-mkrescue -o ./build/melvix.iso ./iso/; -image: build - @set -e; \ - gcc -w ./src/utils/make_initrd.c -o ./build/make_initrd || exit; \ - ./build/make_initrd ./src/utils/test.txt test.txt || exit; \ - mv initrd.img ./build/initrd.img || exit; \ - dd if=/dev/zero of=./build/image.img iflag=fullblock bs=1M count=10 && sync; \ - mkfs.ext2 ./build/image.img; \ - device=$$(sudo losetup -f); \ - sudo losetup $$device ./build/image.img || exit; \ - mkdir ./build/mnt/ || exit; \ - sudo mount $$device ./build/mnt/ || exit; \ - sudo cp ./build/melvix.iso ./build/mnt/kernel || exit; \ - sudo cp ./build/initrd.img ./build/mnt/initrd || exit; \ - sudo umount $$device || exit; \ - sudo losetup -d $$device; - cross: @set -e; \ [ -d "./cross/" ] && echo "Please remove ./cross/ and try again" && exit; \ diff --git a/src/bootloader/grub.cfg b/src/bootloader/grub.cfg new file mode 100644 index 0000000..cb265b1 --- /dev/null +++ b/src/bootloader/grub.cfg @@ -0,0 +1,7 @@ +set timeout=0 +set default=0 +menuentry "Melvix" { + multiboot /boot/melvix.bin + module /boot/melvix.initrd + boot +} \ No newline at end of file diff --git a/src/bootloader/make_initrd.c b/src/bootloader/make_initrd.c new file mode 100644 index 0000000..d7e0487 --- /dev/null +++ b/src/bootloader/make_initrd.c @@ -0,0 +1,53 @@ +#include + +struct initrd_header { + unsigned char magic; + char name[64]; + unsigned int offset; + unsigned int length; +}; + +int main(char argc, char **argv) { + int nheaders = (argc - 1) / 2; + struct initrd_header headers[64]; + printf("size of header: %d\n", sizeof(struct initrd_header)); + unsigned int off = sizeof(struct initrd_header) * 64 + sizeof(int); + + for (int i = 0; i < nheaders; i++) { + printf("writing file %s->%s at 0x%x\n", argv[i * 2 + 1], argv[i * 2 + 2], off); + strcpy(headers[i].name, argv[i * 2 + 2]); + headers[i].name[strlen(argv[i * 2 + 2])] = '\0'; + headers[i].offset = off; + FILE *stream = fopen(argv[i * 2 + 1], "r"); + if (stream == 0) { + printf("Error: file not found: %s\n", argv[i * 2 + 1]); + return 1; + } + fseek(stream, 0, SEEK_END); + headers[i].length = ftell(stream); + off += headers[i].length; + fclose(stream); + headers[i].magic = 0xBF; + } + + printf(headers[1].name); + + FILE *wstream = fopen("./initrd.img", "w"); + unsigned char *data = (unsigned char *) malloc(off); + fwrite(&nheaders, sizeof(int), 1, wstream); + fwrite(headers, sizeof(struct initrd_header), 64, wstream); + + for (int i = 0; i < nheaders; i++) { + FILE *stream = fopen(argv[i * 2 + 1], "r"); + unsigned char *buf = (unsigned char *) malloc(headers[i].length); + fread(buf, 1, headers[i].length, stream); + fwrite(buf, 1, headers[i].length, wstream); + fclose(stream); + free(buf); + } + + fclose(wstream); + free(data); + + return 0; +} diff --git a/src/bootloader/test.txt b/src/bootloader/test.txt new file mode 100644 index 0000000..9033296 --- /dev/null +++ b/src/bootloader/test.txt @@ -0,0 +1 @@ +hallo \ No newline at end of file diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c index 94f53c6..3f8a9ba 100644 --- a/src/kernel/graphics/vesa.c +++ b/src/kernel/graphics/vesa.c @@ -287,6 +287,8 @@ void vesa_draw_char(char ch) { } else if (ch == '\n') { terminal_x = 0; terminal_y += font_height + 2; + } else if (ch == '\t') { + terminal_x += 4 * (font_width + 2); } if (terminal_x >= vbe_width) { diff --git a/src/kernel/graphics/vesa.h b/src/kernel/graphics/vesa.h index 649c6ee..748f8d0 100644 --- a/src/kernel/graphics/vesa.h +++ b/src/kernel/graphics/vesa.h @@ -128,6 +128,8 @@ void vesa_clear(); */ void vesa_set_font(int height); +void vesa_draw_char(char ch); + /** * Draw a char from keyboard * @param ch The character diff --git a/src/kernel/grub.cfg b/src/kernel/grub.cfg deleted file mode 100644 index 8a452ed..0000000 --- a/src/kernel/grub.cfg +++ /dev/null @@ -1,7 +0,0 @@ -set timeout=0 -set default=0 -menuentry "Melvix" { - multiboot /boot/melvix.bin - set gfxpayload=640x480x32 - boot -} \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cba4f6e..dd49949 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -30,16 +30,36 @@ void init() { void kernel_main(struct multiboot *mboot_ptr) { init(); - info("Kernel size in KiB: "); - vesa_draw_number((int) end >> 10); - assert(mboot_ptr->mods_count > 0); uint32_t initrd_location = *((uint32_t *) mboot_ptr->mods_addr); uint32_t initrd_end = *(uint32_t *) (mboot_ptr->mods_addr + 4); - // Don't trample our module with placement accesses, please! - // placement_address = initrd_end; + paging_set_used(0, (initrd_end >> 12) + 1); + fs_root = initialise_initrd(initrd_location); + int i = 0; + struct dirent *node = 0; + vesa_draw_string("\n"); + while ((node = readdir_fs(fs_root, i)) != 0) { + vesa_draw_string("Found file: "); + vesa_draw_string(node->name); + vesa_draw_string("\n"); + fs_node_t *fsnode = finddir_fs(fs_root, node->name); + + if ((fsnode->flags & 0x7) == FS_DIRECTORY) + vesa_draw_string("\t (directory)\n"); + else { + vesa_draw_string("\t contents: \""); + uint8_t buf[fsnode->length]; + uint32_t sz = read_fs(fsnode, 0, fsnode->length, buf); + for (uint32_t j = 0; j < sz; j++) + vesa_draw_char(buf[j]); + + vesa_draw_string("\"\n"); + } + i++; + } + // asm volatile ("div %0" :: "r"(0)); // Exception testing x/0 loop: asm volatile ("hlt"); diff --git a/src/utils/make_initrd.c b/src/utils/make_initrd.c deleted file mode 100644 index f6e504b..0000000 --- a/src/utils/make_initrd.c +++ /dev/null @@ -1,51 +0,0 @@ -#include - -struct initrd_header { - unsigned char magic; - char name[64]; - unsigned int offset; - unsigned int length; -}; - -int main(char argc, char **argv) { - int nheaders = (argc - 1) / 2; - struct initrd_header headers[64]; - printf("size of header: %d\n", sizeof(struct initrd_header)); - unsigned int off = sizeof(struct initrd_header) * 64 + sizeof(int); - - for (int i = 0; i < nheaders; i++) { - printf("writing file %s->%s at 0x%x\n", argv[i * 2 + 1], argv[i * 2 + 2], off); - strcpy(headers[i].name, argv[i * 2 + 2]); - headers[i].name[strlen(argv[i * 2 + 2])] = '\0'; - headers[i].offset = off; - FILE *stream = fopen(argv[i * 2 + 1], "r"); - if (stream == 0) { - printf("Error: file not found: %s\n", argv[i * 2 + 1]); - return 1; - } - fseek(stream, 0, SEEK_END); - headers[i].length = ftell(stream); - off += headers[i].length; - fclose(stream); - headers[i].magic = 0xBF; - } - - FILE *wstream = fopen("./initrd.img", "w"); - unsigned char *data = (unsigned char *) malloc(off); - fwrite(&nheaders, sizeof(int), 1, wstream); - fwrite(headers, sizeof(struct initrd_header), 64, wstream); - - for (int i = 0; i < nheaders; i++) { - FILE *stream = fopen(argv[i * 2 + 1], "r"); - unsigned char *buf = (unsigned char *) malloc(headers[i].length); - fread(buf, 1, headers[i].length, stream); - fwrite(buf, 1, headers[i].length, wstream); - fclose(stream); - free(buf); - } - - fclose(wstream); - free(data); - - return 0; -} diff --git a/src/utils/test.txt b/src/utils/test.txt deleted file mode 100644 index 9116450..0000000 --- a/src/utils/test.txt +++ /dev/null @@ -1 +0,0 @@ -heey -- cgit v1.2.3