diff --git a/Makefile b/Makefile index bffc286..3bb6f15 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o \ bin/keyboard.o bin/paging.o bin/page_alloc.o bin/page_tables.o \ bin/fat.o bin/task.o bin/switch.o bin/sys_exit.o bin/gdt.o bin/isr-asm.o \ bin/kheap.o bin/liballoc.o bin/exec_from_file.o bin/syscalls.o bin/atapi.o \ - bin/lib9660.o + bin/lib9660.o bin/vfs.o # Define the compiler and assembler #CC = i686-elf-gcc -D__is_katauos @@ -118,7 +118,7 @@ bin/page_alloc.o: src/mm/page_alloc.c bin/page_tables.o: src/mm/page_tables.c $(CC) $(CFLAGS) -c $< -o $@ -bin/fat.o: src/drivers/fat.c +bin/fat.o: src/fs/fat.c $(CC) $(CFLAGS) -c $< -o $@ bin/switch.o: src/tasking/switch.asm @@ -151,7 +151,10 @@ bin/syscalls.o: src/tasking/syscalls.c bin/atapi.o: src/drivers/atapi.c $(CC) $(CFLAGS) -c $< -o $@ -bin/lib9660.o: src/drivers/lib9660.c +bin/lib9660.o: src/fs/lib9660.c + $(CC) $(CFLAGS) -c $< -o $@ + +bin/vfs.o: src/fs/vfs.c $(CC) $(CFLAGS) -c $< -o $@ clean: diff --git a/include/disk.h b/include/disk.h index b89ed84..21d75a4 100644 --- a/include/disk.h +++ b/include/disk.h @@ -11,7 +11,6 @@ typedef struct { } disk_info; void get_disk_info(disk_info *info); -int ide_check_disk_present(); bool ata_read(uint8_t *buffer, uint32_t lba); bool ata_write(const uint8_t *buffer, uint32_t lba); diff --git a/include/exec_from_file.h b/include/exec_from_file.h index 3eeecd9..6a3c862 100644 --- a/include/exec_from_file.h +++ b/include/exec_from_file.h @@ -1,5 +1,6 @@ -#include "../include/fat.h" #include "../include/elf.h" #include "../include/stdio.h" +#include "../include/vfs.h" +#include "../include/lib9660.h" void exec_from_file(const char* filename, const char* dirname); \ No newline at end of file diff --git a/include/task.h b/include/task.h index a56f01f..cbb9e15 100644 --- a/include/task.h +++ b/include/task.h @@ -2,7 +2,7 @@ #define TASK_H #include -#include "../include/fat.h" +#include "../include/lib9660.h" // Сегменты для ядра и пользователя #define SEG_KCODE 0x08 // Сегмент кода ядра @@ -64,8 +64,8 @@ typedef struct Process uint32_t ring; // Уровень привилегий (0 или 3) struct Process* next; // Следующий процесс - file_t* file_descriptors[MAX_OPEN_FILES]; - dir_t* cwd; + l9660_file* file_descriptors[MAX_OPEN_FILES]; + l9660_dir* cwd; void* brk; } Process; diff --git a/include/vfs.h b/include/vfs.h new file mode 100644 index 0000000..41c3745 --- /dev/null +++ b/include/vfs.h @@ -0,0 +1,16 @@ +#ifndef VFS_H +#define VFS_H + +#include +#include "../include/atapi.h" +#include "../include/lib9660.h" + +extern l9660_fs global_fs; + +bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector); +void mount_fs(); +void list_files(l9660_dir *dir); + +size_t vfs_read_file(l9660_file *file, uint8_t* buffer); + +#endif \ No newline at end of file diff --git a/src/drivers/fat.c b/src/fs/fat.c similarity index 100% rename from src/drivers/fat.c rename to src/fs/fat.c diff --git a/src/drivers/lib9660.c b/src/fs/lib9660.c similarity index 100% rename from src/drivers/lib9660.c rename to src/fs/lib9660.c diff --git a/src/fs/vfs.c b/src/fs/vfs.c new file mode 100644 index 0000000..6c9186e --- /dev/null +++ b/src/fs/vfs.c @@ -0,0 +1,72 @@ +#include "../include/vfs.h" +#include "../include/stdio.h" +#include "../include/string.h" + +l9660_fs global_fs; + +bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector) { + // The 'fs' parameter is not needed for our simple case, so we can ignore it. + (void)fs; + + // Call your existing CD-ROM read function to read 1 sector. + // We assume the CD-ROM is the secondary master (0x170). + int result = read_cdrom(0x170, false, sector, 1, (uint16_t*)buf); + + // lib9660 expects 'true' for success and 'false' for failure. + // Your read_cdrom returns 0 for success. + return (result == 0); +} + +void mount_fs() +{ + l9660_status status = l9660_openfs(&global_fs, read_sector_callback); + + if (status != L9660_OK) { + printf("Error opening ISO9660 filesystem!\n"); + return; + } + printf("FS mounted\n"); +} + +void list_files(l9660_dir *dir) +{ + debug_log("----------CONTENTS----------"); + l9660_status status; + for (;;) { + l9660_dirent *dent; + status = l9660_readdir(dir, &dent); + + if (dent == NULL || status != L9660_OK) { + break; // End of directory or error + } + + // Print the filename. It's not null-terminated, so use the length field. + for (int i = 0; i < dent->name_len; ++i) { + printf("%c", dent->name[i]); + } + printf("\n"); + } +} + + +size_t vfs_read_file(l9660_file *file, uint8_t* buffer) +{ + size_t total_read = 0; + for(;;) { + char buf[128]; + size_t read; + l9660_read(file, buf, 128, &read); + + //check for EOF + if (read == 0) + break; + + memcpy(buffer, buf, 128); + buffer += 128; + total_read += read; + } + + //reset the current position to the original position + buffer -= total_read; + return total_read; +} \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index bd961de..236e02a 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -15,6 +15,7 @@ #include "../include/exec_from_file.h" #include "../include/atapi.h" #include "../include/lib9660.h" +#include "../include/vfs.h" #include @@ -25,34 +26,14 @@ void apply_pic_masks() outb(0xa1,0xff); } -static l9660_fs g_iso_fs; -static l9660_dir g_root_dir; -static l9660_file g_test_file; - -bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector) { - // The 'fs' parameter is not needed for our simple case, so we can ignore it. - (void)fs; - - // Call your existing CD-ROM read function to read 1 sector. - // We assume the CD-ROM is the secondary master (0x170). - int result = read_cdrom(0x170, false, sector, 1, (uint16_t*)buf); - - // lib9660 expects 'true' for success and 'false' for failure. - // Your read_cdrom returns 0 for success. - return (result == 0); -} - void test_iso9660() { - l9660_status status = l9660_openfs(&g_iso_fs, read_sector_callback); - - if (status != L9660_OK) { - printf("Error opening ISO9660 filesystem!\n"); - return; - } + l9660_dir g_root_dir; + l9660_file g_test_file; + l9660_status status; // 2. Open the root directory - status = l9660_fs_open_root(&g_root_dir, &g_iso_fs); + status = l9660_fs_open_root(&g_root_dir, &global_fs); if (status != L9660_OK) { printf("Error opening root directory!\n"); return; @@ -77,10 +58,16 @@ void test_iso9660() printf("\nReading content of /TEST.TXT:\n"); // Re-open the root directory to reset its position before searching - l9660_fs_open_root(&g_root_dir, &g_iso_fs); + l9660_fs_open_root(&g_root_dir, &global_fs); - // Note: ISO9660 filenames are often uppercase. - status = l9660_openat(&g_test_file, &g_root_dir, "test.txt;1"); + l9660_dir boot_dir; + l9660_dir grub_dir; + status = l9660_opendirat(&boot_dir, &g_root_dir, "boot"); + printf("STATUS %X\n", status); + status = l9660_opendirat(&grub_dir, &boot_dir, "grub"); + printf("STATUS %X\n", status); + + status = l9660_openat(&g_test_file, &grub_dir, "grub.cfg"); if (status != L9660_OK) { printf("Error opening TEST.TXT;1. Error code: "); @@ -197,9 +184,11 @@ void kmain(unsigned int magic, unsigned int info) printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2); printf("huy1: %s\nhuy2: %s", huy, huy2); + mount_fs(); + test_iso9660(); - //exec_from_file("/ebalo/main", "/ebalo/"); + exec_from_file("main.", "."); while(1) { diff --git a/src/tasking/exec_from_file.c b/src/tasking/exec_from_file.c index 471bcf0..97648b2 100644 --- a/src/tasking/exec_from_file.c +++ b/src/tasking/exec_from_file.c @@ -12,46 +12,64 @@ #define USER_STACK_SIZE 0x4000 // 16KB stack (4 pages) #define USER_STACK_BOTTOM (USER_STACK_TOP - USER_STACK_SIZE) -disk_ops_t ops1 = +int read_file(const char* path, uint8_t** buffer, l9660_dir* dir) { - .read = ata_read, - .write = ata_write, -}; + //file_t file; + l9660_file file; -static fat_t g_fat; + l9660_status err; -int read_file(const char* path, uint8_t** buffer, dir_t* dir) -{ - file_t file; - int err = fat_fopen(&file, path, "r"); - if (err) + err = l9660_openat(&file, dir, path);//fat_fopen(&file, path, "r"); + if (err != L9660_OK) + { + debug_log("read_file: err: 0x%X\n", err); return err; + } - uint32_t file_size = fat_fsize(&file); + l9660_seek(&file, L9660_SEEK_END, 0); + uint32_t file_size = l9660_tell(&file);//fat_fsize(&file); debug_log("file_size: %X\n", file_size); + l9660_seek(&file, L9660_SEEK_SET, 0); uint8_t buf[512]; debug_log("heh\n"); - *buffer = (uint8_t*)malloc(file_size); + *buffer = (uint8_t*)malloc(file_size+1); uint8_t* current = *buffer; debug_log("eeee\n"); - int cnt = fat_fread(&file, *buffer, file_size); + size_t read; - *dir = file.dir; + debug_log("info: buffer: %X, *buffer: %X, &buffer: %X\n", buffer, *buffer, &buffer); + + //err = l9660_read(&file, *buffer, file_size, &read);//fat_fread(&file, *buffer, file_size); + read = vfs_read_file(&file, *buffer); + debug_log("bytes read: 0x%X\n", read); + + while(1){} - return fat_fclose(&file); + return 0;//fat_fclose(&file); } void exec_from_file(const char* filename, const char* dirname) { asm volatile("cli"); int err, cnt; - err = fat_mount(&ops1, 0, &g_fat, "ebalo"); + //err = fat_mount(&ops1, 0, &g_fat, "ebalo"); uint8_t* file; - dir_t dir; + //dir_t dir; + l9660_dir dir; + l9660_dir root_dir; + l9660_status status; + + status = l9660_fs_open_root(&root_dir, &global_fs); + debug_log("l9660_fs_open_root status: 0x%X\n", status); + + status = l9660_opendirat(&dir, &root_dir, dirname); + debug_log("l9660_opendirat status: 0x%X\n", status); + + list_files(&dir); uint32_t bss_start, bss_end;