From 412db850a0ae8f261381acdb4bf5972b3cce43db Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Thu, 11 Dec 2025 02:05:38 +0100 Subject: [PATCH] Parse ramdisk tar file --- Makefile | 3 +- distrib/etc/file.txt | 1 + distrib/make.distrib | 8 +++ kernel/make.src | 1 + kernel/platform/i386_pc/boot/bootmain.c | 12 ++++- kernel/ramdisk/.gitignore | 1 + kernel/ramdisk/make.src | 5 ++ kernel/ramdisk/tar.c | 68 +++++++++++++++++++++++++ kernel/ramdisk/tar.h | 53 +++++++++++++++++++ platform/i386_pc/grub.cfg | 1 + platform/make.i386_pc | 1 + 11 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 distrib/etc/file.txt create mode 100644 distrib/make.distrib create mode 100644 kernel/ramdisk/.gitignore create mode 100644 kernel/ramdisk/make.src create mode 100644 kernel/ramdisk/tar.c create mode 100644 kernel/ramdisk/tar.h diff --git a/Makefile b/Makefile index fb8c101..9e3e35d 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,12 @@ include build/make.help srctree := $(PWD) include platform/make.$(platform) +include distrib/make.distrib kernel: $(make) -C $(srctree)/kernel srctree=$(srctree) all -clean: platform_clean +clean: platform_clean distrib_clean $(make) -C $(srctree)/kernel srctree=$(srctree) clean .hidden: diff --git a/distrib/etc/file.txt b/distrib/etc/file.txt new file mode 100644 index 0000000..8c96cab --- /dev/null +++ b/distrib/etc/file.txt @@ -0,0 +1 @@ +Hello file diff --git a/distrib/make.distrib b/distrib/make.distrib new file mode 100644 index 0000000..529368f --- /dev/null +++ b/distrib/make.distrib @@ -0,0 +1,8 @@ + +distrib_prepare: + tar -cvf $(srctree)/out/ramdisk.tar -C $(srctree)/distrib . + +distrib_clean: + rm -f $(srctree)/out/ramdisk.tar + +.PHONY: distrib_prepare distrib_clean diff --git a/kernel/make.src b/kernel/make.src index 73075c5..e8afb02 100644 --- a/kernel/make.src +++ b/kernel/make.src @@ -7,3 +7,4 @@ include libk/make.src include sync/make.src include mm/make.src include irq/make.src +include ramdisk/make.src diff --git a/kernel/platform/i386_pc/boot/bootmain.c b/kernel/platform/i386_pc/boot/bootmain.c index 2d9a1a3..f7161b4 100644 --- a/kernel/platform/i386_pc/boot/bootmain.c +++ b/kernel/platform/i386_pc/boot/bootmain.c @@ -43,6 +43,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include extern byte_t __kernel_end; @@ -94,9 +95,17 @@ static void mregmap_init1(void) { } } -static void pmm_init1(void) { +static void ramdisk_init(void) { struct multiboot *mb = multiboot_get(); + /* TODO: for now we assert that the first module is the ramdisk */ + struct multiboot_mod_list *module = (struct multiboot_mod_list *)((uptr_t)mb->mods_addr + VIRT_BASE); + uptr_t module_start = (uptr_t)module->mod_start + VIRT_BASE; + usize_t total = tar_parse(module_start); + dbgf("ramdisk: total=%zu\n", total); +} + +static void pmm_init1(void) { struct mreg *avail_ram = mregmap_first_avail(); usize_t usable_ram_bytes = avail_ram->size; @@ -135,6 +144,7 @@ void bootmain(void *mbootptr) { pmm_init1(); bba_init(); mm_init(); + ramdisk_init(); pit_init(); __asm__ volatile("sti"); diff --git a/kernel/ramdisk/.gitignore b/kernel/ramdisk/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/kernel/ramdisk/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/kernel/ramdisk/make.src b/kernel/ramdisk/make.src new file mode 100644 index 0000000..23f7fdc --- /dev/null +++ b/kernel/ramdisk/make.src @@ -0,0 +1,5 @@ +dir_tar1 := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))) + +c_files += $(dir_tar1)/tar.c + +o_files += $(dir_tar1)/tar.o diff --git a/kernel/ramdisk/tar.c b/kernel/ramdisk/tar.c new file mode 100644 index 0000000..b792b6e --- /dev/null +++ b/kernel/ramdisk/tar.c @@ -0,0 +1,68 @@ +/* +Copyright 2025 Kamil Kowalczyk + +Redistribution and use in source and binary forms, with or +without modification, are permitted provided that the following +conditions are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include + +static struct tar_header *ramdisk_tar_headers[TAR_MAX]; + +usize_t tar_get_size(const byte_t *input) { + usize_t size = 0; + usize_t j; + usize_t count = 1; + + for (j = 11; j > 0; j--, count *= 8) + size += ((input[j - 1] - '0') * count); + + return size; +} + +usize_t tar_parse(uptr_t addr) { + usize_t i; + + for (i = 0; ; i++) { + struct tar_header *header = (struct tar_header *)addr; + + if (header->filename[i] == '\0') + break; + + usize_t size = tar_get_size(header->size); + + ramdisk_tar_headers[i] = header; + + addr += ((size / 512) + 1) * 512; + + if (size % 512) + addr += 512; + } + + return i; +} diff --git a/kernel/ramdisk/tar.h b/kernel/ramdisk/tar.h new file mode 100644 index 0000000..4b2fe82 --- /dev/null +++ b/kernel/ramdisk/tar.h @@ -0,0 +1,53 @@ +/* +Copyright 2025 Kamil Kowalczyk + +Redistribution and use in source and binary forms, with or +without modification, are permitted provided that the following +conditions are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef _RAMDISK_TAR_H +#define _RAMDISK_TAR_H + +#include + +#define TAR_MAX 64 + +struct tar_header { + byte_t filename[100]; + byte_t mode[8]; + byte_t uid[8]; + byte_t gid[8]; + byte_t size[12]; + byte_t mtime[12]; + byte_t checksum[8]; + byte_t typeflag[1]; +}; + +usize_t tar_get_size(const byte_t *input); +usize_t tar_parse(uptr_t addr); + +#endif // _RAMDISK_TAR_H diff --git a/platform/i386_pc/grub.cfg b/platform/i386_pc/grub.cfg index 0815c0a..19e00f4 100644 --- a/platform/i386_pc/grub.cfg +++ b/platform/i386_pc/grub.cfg @@ -3,5 +3,6 @@ set default=0 menuentry "mop3" { multiboot /boot/kernel.elf + module /boot/ramdisk.tar boot } diff --git a/platform/make.i386_pc b/platform/make.i386_pc index 1e29dfb..9015038 100644 --- a/platform/make.i386_pc +++ b/platform/make.i386_pc @@ -1,6 +1,7 @@ $(srctree)/out/i386_pc/mop3.iso: $(srctree)/out/kernel.elf mkdir -p $(srctree)/out/i386_pc/tmp/iso/boot/grub cp $(srctree)/out/kernel.elf $(srctree)/out/i386_pc/tmp/iso/boot + cp $(srctree)/out/ramdisk.tar $(srctree)/out/i386_pc/tmp/iso/boot cp $(srctree)/platform/i386_pc/grub.cfg $(srctree)/out/i386_pc/tmp/iso/boot/grub grub-mkrescue -o $(srctree)/out/i386_pc/mop3.iso $(srctree)/out/i386_pc/tmp/iso