From b93303899f898ff5911f60345301567f7ce92caa Mon Sep 17 00:00:00 2001 From: Ted Meyer Date: Sat, 20 Jan 2018 20:04:18 -0800 Subject: [PATCH] cleaned up all includes, and removed shitty code --- Makefile | 22 +- src/include/kio.h => include/arch/io.h | 11 +- {src/include => include/arch}/lock.h | 2 +- include/arch/misc.h | 16 ++ {src/include => include/arch}/registers.h | 4 +- {src/include => include/arch}/time.h | 0 {src/include => include/kernel}/multiboot.h | 2 +- {src/include => include/mem}/alloc.h | 3 + .../include => include/mem}/chunk_allocator.h | 2 +- {src/include => include/mmu}/mmu.h | 3 +- {src/include => include/pci}/ata.h | 2 +- {src/include => include/pci}/pci.h | 2 +- {src/include => include/pic}/interrupts.h | 2 +- {src/include => include/pic}/keyboard.h | 2 +- {src/include => include/pic}/timer.h | 4 +- src/include/libk.h => include/shell/shell.h | 56 ++--- include/shell/tty.h | 33 +++ src/include/ktype.h => include/std/int.h | 0 include/std/string.h | 14 ++ src/alloc/alloc.c | 6 +- src/{lib => arch}/lock.c | 2 +- src/arch/misc.c | 5 + src/{lib => arch}/registers.c | 4 +- src/{lib => arch}/time.c | 3 +- src/fs/dsfs/fs.c | 0 src/fs/fat32/fs.c | 226 ------------------ src/fs/mounts.c | 90 ------- src/include/ctree.h | 19 -- src/include/devfs.h | 11 - src/include/fs_api.h | 53 ---- src/include/kshell.h | 8 - src/include/path.h | 21 -- src/include/sorts.h | 20 -- src/kernel/kernel.c | 22 +- src/kernel/kshell.c | 13 +- src/kernel/multiboot.c | 4 +- src/lib/ctree.c | 24 -- src/lib/libk.c | 41 ---- src/lib/path.c | 26 -- src/lib/sorts.c | 76 ------ src/mmu/chunk_allocator.c | 6 +- src/mmu/mmu.c | 9 +- src/pci/ata.c | 18 +- src/pci/pci.c | 9 +- src/pic/interrupts.c | 14 +- src/pic/keyboard.c | 10 +- src/pic/timer.c | 8 +- src/shell/shell.c | 67 ++++++ src/{lib/kio.c => shell/tty.c} | 152 +----------- src/std/string.c | 88 +++++++ 50 files changed, 334 insertions(+), 901 deletions(-) rename src/include/kio.h => include/arch/io.h (75%) rename {src/include => include/arch}/lock.h (87%) create mode 100644 include/arch/misc.h rename {src/include => include/arch}/registers.h (80%) rename {src/include => include/arch}/time.h (100%) rename {src/include => include/kernel}/multiboot.h (98%) rename {src/include => include/mem}/alloc.h (55%) rename {src/include => include/mem}/chunk_allocator.h (95%) rename {src/include => include/mmu}/mmu.h (98%) rename {src/include => include/pci}/ata.h (99%) rename {src/include => include/pci}/pci.h (98%) rename {src/include => include/pic}/interrupts.h (98%) rename {src/include => include/pic}/keyboard.h (89%) rename {src/include => include/pic}/timer.h (96%) rename src/include/libk.h => include/shell/shell.h (50%) create mode 100644 include/shell/tty.h rename src/include/ktype.h => include/std/int.h (100%) create mode 100644 include/std/string.h rename src/{lib => arch}/lock.c (92%) create mode 100644 src/arch/misc.c rename src/{lib => arch}/registers.c (69%) rename src/{lib => arch}/time.c (91%) delete mode 100644 src/fs/dsfs/fs.c delete mode 100644 src/fs/fat32/fs.c delete mode 100644 src/fs/mounts.c delete mode 100644 src/include/ctree.h delete mode 100644 src/include/devfs.h delete mode 100644 src/include/fs_api.h delete mode 100644 src/include/kshell.h delete mode 100644 src/include/path.h delete mode 100644 src/include/sorts.h delete mode 100644 src/lib/ctree.c delete mode 100644 src/lib/libk.c delete mode 100644 src/lib/path.c delete mode 100644 src/lib/sorts.c create mode 100644 src/shell/shell.c rename src/{lib/kio.c => shell/tty.c} (60%) create mode 100644 src/std/string.c diff --git a/Makefile b/Makefile index 74ab4e9..dc211ee 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # This stuff is all for building the kernel itself # ###### -kernel_flags := -nostdinc -Isrc/include -fno-stack-protector -m64 -g +kernel_flags := -nostdinc -Iinclude -fno-stack-protector -m64 -g arch ?= x86_64 BUILD := build @@ -53,22 +53,4 @@ $(ISO): $(KERNEL) $(GRUB_CFG) @echo grub-mkrescue -o $(ISO) clean: - rm -rf build - - - - - - - - - - -##### -# -# This stuff is for building the fuse system -# -##### -fuse: src/fs/shittyfs.c src/include/shittyfs.h - mkdir -p $(BUILD) - gcc -DSTDLIB src/fs/shittyfs.c -o $(BUILD)/fuse + rm -rf build \ No newline at end of file diff --git a/src/include/kio.h b/include/arch/io.h similarity index 75% rename from src/include/kio.h rename to include/arch/io.h index 90a87a6..fbaaf2e 100644 --- a/src/include/kio.h +++ b/include/arch/io.h @@ -1,7 +1,7 @@ #ifndef kio_h #define kio_h -#include "ktype.h" +#include static inline void outb(uint16_t port, uint8_t val){ asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) ); @@ -37,11 +37,4 @@ static inline uint32_t inl(uint16_t port) { return ret; } -int strncmp(char *a, char *b, size_t len); -uint64_t hex2int(char *x); -uint32_t backspace(); -char *strfnd(char *, char); -void force_char(char, uint8_t, uint16_t, uint16_t); -void force_simple_string(char *, uint8_t, uint16_t, uint16_t); -uint32_t write_string(char *, uint8_t); -#endif +#endif \ No newline at end of file diff --git a/src/include/lock.h b/include/arch/lock.h similarity index 87% rename from src/include/lock.h rename to include/arch/lock.h index 2e964d7..5566617 100644 --- a/src/include/lock.h +++ b/include/arch/lock.h @@ -1,7 +1,7 @@ #ifndef lock_h #define lock_h -#include "ktype.h" +#include typedef int lock_t; diff --git a/include/arch/misc.h b/include/arch/misc.h new file mode 100644 index 0000000..1aee463 --- /dev/null +++ b/include/arch/misc.h @@ -0,0 +1,16 @@ +/* yeah, this is a _really_ bad file name */ + +#ifndef misc_h +#define misc_h + +#define PAGE 1 +#define FRAME 0 + +#define min(x, y) (((x)>(y))?(y):(x)) +#define max(a, b) (((a)>(b))?(a):(b)) + + +void wait(void); + + +#endif diff --git a/src/include/registers.h b/include/arch/registers.h similarity index 80% rename from src/include/registers.h rename to include/arch/registers.h index cb85197..4b2b19a 100644 --- a/src/include/registers.h +++ b/include/arch/registers.h @@ -1,8 +1,8 @@ -#include "libk.h" - #ifndef registers_h #define registers_h +#include + uint16_t cs(); uint64_t rip(void); diff --git a/src/include/time.h b/include/arch/time.h similarity index 100% rename from src/include/time.h rename to include/arch/time.h diff --git a/src/include/multiboot.h b/include/kernel/multiboot.h similarity index 98% rename from src/include/multiboot.h rename to include/kernel/multiboot.h index 545e196..9cda598 100644 --- a/src/include/multiboot.h +++ b/include/kernel/multiboot.h @@ -1,7 +1,7 @@ #ifndef multiboot_h #define multiboot_h -#include "libk.h" +#include #define ELF_TAG_TYPE 0x09 #define MMAP_TAG_TYPE 0x06 diff --git a/src/include/alloc.h b/include/mem/alloc.h similarity index 55% rename from src/include/alloc.h rename to include/mem/alloc.h index f045bad..b4d90cc 100644 --- a/src/include/alloc.h +++ b/include/mem/alloc.h @@ -5,4 +5,7 @@ void *kmalloc_init(void); void *kmalloc(uint64_t); void kfree(void *_ptr); +void *memset(void *p, int b, size_t n); +void *memcpy(void *dest, void *src, size_t bytes); + #endif \ No newline at end of file diff --git a/src/include/chunk_allocator.h b/include/mem/chunk_allocator.h similarity index 95% rename from src/include/chunk_allocator.h rename to include/mem/chunk_allocator.h index 0a6ac75..9de66f7 100644 --- a/src/include/chunk_allocator.h +++ b/include/mem/chunk_allocator.h @@ -1,7 +1,7 @@ #ifndef chunk_allocator_h #define chunk_allocator_h -#include "ktype.h" +#include struct segment_list { uint8_t allocated : 1; diff --git a/src/include/mmu.h b/include/mmu/mmu.h similarity index 98% rename from src/include/mmu.h rename to include/mmu/mmu.h index f32aacd..aecddf6 100644 --- a/src/include/mmu.h +++ b/include/mmu/mmu.h @@ -1,7 +1,8 @@ #ifndef mmu_h #define mmu_h -#include +#include +#include #define PAGE_SIZE 4096 #define PAGE_ENTRIES 512 diff --git a/src/include/ata.h b/include/pci/ata.h similarity index 99% rename from src/include/ata.h rename to include/pci/ata.h index 34e74c5..6c63828 100644 --- a/src/include/ata.h +++ b/include/pci/ata.h @@ -1,7 +1,7 @@ #ifndef ata_h #define ata_h -#include +#include #define ATA_REG_ALTSTATUS 0x0C #define ATA_REG_HDDEVSEL 0x06 diff --git a/src/include/pci.h b/include/pci/pci.h similarity index 98% rename from src/include/pci.h rename to include/pci/pci.h index 1182b26..e96125c 100644 --- a/src/include/pci.h +++ b/include/pci/pci.h @@ -1,7 +1,7 @@ #ifndef pci_h #define pci_h -#include "ktype.h" +#include #define PCI_ADDRESS_PORT 0xCF8 #define PCI_VALUE_PORT 0xCFC diff --git a/src/include/interrupts.h b/include/pic/interrupts.h similarity index 98% rename from src/include/interrupts.h rename to include/pic/interrupts.h index 6db3094..0c2e12f 100644 --- a/src/include/interrupts.h +++ b/include/pic/interrupts.h @@ -1,7 +1,7 @@ #ifndef interrupts_h #define interrupts_h -#include "ktype.h" +#include #define PIC1 0x20 // Master PIC #define PIC2 0xA0 // Slave PIC diff --git a/src/include/keyboard.h b/include/pic/keyboard.h similarity index 89% rename from src/include/keyboard.h rename to include/pic/keyboard.h index c6c288b..26ec133 100644 --- a/src/include/keyboard.h +++ b/include/pic/keyboard.h @@ -1,7 +1,7 @@ #ifndef keyboard_h #define keyboard_h -#include "ktype.h" +#include #define KR_size 128 #define KEYBOARD_STATUS_PORT 0x64 diff --git a/src/include/timer.h b/include/pic/timer.h similarity index 96% rename from src/include/timer.h rename to include/pic/timer.h index f8208ad..be1cc54 100644 --- a/src/include/timer.h +++ b/include/pic/timer.h @@ -1,7 +1,7 @@ #ifndef timer_h #define timer_h -#include "ktype.h" +#include #define PIT_CH0 0x40 #define PIT_CH1 0x41 @@ -34,6 +34,4 @@ union { void init_timer(); - - #endif diff --git a/src/include/libk.h b/include/shell/shell.h similarity index 50% rename from src/include/libk.h rename to include/shell/shell.h index ce8c6c7..eb2f69f 100644 --- a/src/include/libk.h +++ b/include/shell/shell.h @@ -1,46 +1,22 @@ -#ifndef libk_h -#define libk_h - -#include "ktype.h" - -#define ROWS 25 -#define COLS 80 -#define VIDB 2 - -#define PAGE 1 -#define FRAME 0 - -enum vga_color { - COLOR_BLACK = 0, - COLOR_BLUE = 1, - COLOR_GREEN = 2, - COLOR_CYAN = 3, - COLOR_RED = 4, - COLOR_MAGENTA = 5, - COLOR_BROWN = 6, - COLOR_LIGHT_GREY = 7, - COLOR_DARK_GREY = 8, - COLOR_LIGHT_BLUE = 9, - COLOR_LIGHT_GREEN = 10, - COLOR_LIGHT_CYAN = 11, - COLOR_LIGHT_RED = 12, - COLOR_LIGHT_MAGENTA = 13, - COLOR_LIGHT_BROWN = 14, - COLOR_WHITE = 15, -}; - -#define min(x, y) (((x)>(y))?(y):(x)) -#define max(a, b) (((a)>(b))?(a):(b)) - -void *memset(void *p, int b, size_t n); -void *memcpy(void *dest, void *src, size_t bytes); -uint64_t strlen(char *c); +#ifndef kshell_h +#define kshell_h + +#include + +#define RDL_SIZE 256 + +void kshell(unsigned char key); +int strncmp(char *a, char *b, size_t len); +uint64_t hex2int(char *x); +uint32_t backspace(); +char *strfnd(char *, char); +void force_char(char, uint8_t, uint16_t, uint16_t); +void force_simple_string(char *, uint8_t, uint16_t, uint16_t); +uint32_t write_string(char *, uint8_t); + uint64_t stringlen(char *); -void wait(void); -void kio_init(void); -void clear_screen(void); #define sprintf(...) _sprintf (0,0,0,0,0,0,0,0,0,0,__VA_ARGS__) uint32_t _sprintf(int,int,int,int,int,int,int,int,int,int, char *dest, const char fmt[], ...); #define kprintf(...) _kprintf (0,0,0,0,0,0,0,0,0,0,__VA_ARGS__) diff --git a/include/shell/tty.h b/include/shell/tty.h new file mode 100644 index 0000000..9aca032 --- /dev/null +++ b/include/shell/tty.h @@ -0,0 +1,33 @@ +#ifndef tty_h +#define tty_h + +#include + +#define ROWS 25 +#define COLS 80 +#define VIDB 2 + +enum vga_color { + COLOR_BLACK = 0, + COLOR_BLUE = 1, + COLOR_GREEN = 2, + COLOR_CYAN = 3, + COLOR_RED = 4, + COLOR_MAGENTA = 5, + COLOR_BROWN = 6, + COLOR_LIGHT_GREY = 7, + COLOR_DARK_GREY = 8, + COLOR_LIGHT_BLUE = 9, + COLOR_LIGHT_GREEN = 10, + COLOR_LIGHT_CYAN = 11, + COLOR_LIGHT_RED = 12, + COLOR_LIGHT_MAGENTA = 13, + COLOR_LIGHT_BROWN = 14, + COLOR_WHITE = 15, +}; + +void clear_screen(void); + +void kio_init(void); + +#endif diff --git a/src/include/ktype.h b/include/std/int.h similarity index 100% rename from src/include/ktype.h rename to include/std/int.h diff --git a/include/std/string.h b/include/std/string.h new file mode 100644 index 0000000..4b82355 --- /dev/null +++ b/include/std/string.h @@ -0,0 +1,14 @@ +#ifndef string_h +#define string_h + +#include + +int strncmp(char *, char *, size_t); +char *strfnd(char *, char); +uint64_t hex2int(char *); +uint64_t strlen(char *); +void *memcpy(void *, void *, size_t); +void *memset(void *, int b, size_t); +char hexr(char); + +#endif \ No newline at end of file diff --git a/src/alloc/alloc.c b/src/alloc/alloc.c index 875ae8b..1620119 100644 --- a/src/alloc/alloc.c +++ b/src/alloc/alloc.c @@ -1,5 +1,7 @@ -#include -#include +#include +#include +#include +#include typedef struct huge_alloc_info_s { uint64_t starting_page; diff --git a/src/lib/lock.c b/src/arch/lock.c similarity index 92% rename from src/lib/lock.c rename to src/arch/lock.c index 519280d..b0653c0 100644 --- a/src/lib/lock.c +++ b/src/arch/lock.c @@ -1,4 +1,4 @@ -#include "lock.h" +#include bool try_lock(lock_t *p) { if (__sync_bool_compare_and_swap(p, 0, 1)) { diff --git a/src/arch/misc.c b/src/arch/misc.c new file mode 100644 index 0000000..2ca7af9 --- /dev/null +++ b/src/arch/misc.c @@ -0,0 +1,5 @@ +#include + +void wait(void) { + for(int i=0;i<10000000;i++); +} \ No newline at end of file diff --git a/src/lib/registers.c b/src/arch/registers.c similarity index 69% rename from src/lib/registers.c rename to src/arch/registers.c index a05c6bf..194371e 100644 --- a/src/lib/registers.c +++ b/src/arch/registers.c @@ -1,5 +1,5 @@ -#include "libk.h" -#include "registers.h" +#include +#include inline uint16_t cs(void) { uint16_t val; diff --git a/src/lib/time.c b/src/arch/time.c similarity index 91% rename from src/lib/time.c rename to src/arch/time.c index 56d0a41..8fa85e7 100644 --- a/src/lib/time.c +++ b/src/arch/time.c @@ -1,4 +1,5 @@ -#include "libk.h" +#include +#include #define outb_p(value,port) \ __asm__ ("outb %%al,%%dx\n" \ diff --git a/src/fs/dsfs/fs.c b/src/fs/dsfs/fs.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/fs/fat32/fs.c b/src/fs/fat32/fs.c deleted file mode 100644 index 24ffbc1..0000000 --- a/src/fs/fat32/fs.c +++ /dev/null @@ -1,226 +0,0 @@ -/* -#include -#include -#include -#include -#include -#include -#include - -#include "fat32.h" - -void get_fat_info(fat_bootsector *fat_boot, fat_type *type, unsigned int *root_sector, unsigned int *fds) { - unsigned long total_sectors = (fat_boot->total_sectors_16 == 0)? fat_boot->total_sectors_32 : fat_boot->total_sectors_16; - unsigned long fat_size = (fat_boot->table_size_16 == 0)? fat_boot->fat32.table_size_32 : fat_boot->table_size_16; - unsigned long root_dir_sectors = ((fat_boot->root_entry_count * 32) + (fat_boot->bytes_per_sector - 1)) / fat_boot->bytes_per_sector; - unsigned int first_data_sector = fat_boot->reserved_sector_count + (fat_boot->table_count * fat_size) + root_dir_sectors; - unsigned int first_fat_sector = fat_boot->reserved_sector_count; - unsigned int data_sectors = total_sectors - (fat_boot->reserved_sector_count + (fat_boot->table_count * fat_size) + root_dir_sectors); - unsigned int total_clusters = data_sectors / fat_boot->sectors_per_cluster; - - if(total_clusters < 4085) { - *type = FAT12; - *root_sector = first_data_sector - root_dir_sectors; - } - else if(total_clusters < 65525) { - *type = FAT16; - *root_sector = first_data_sector - root_dir_sectors; - } - else if (total_clusters < 268435445) { - *type = FAT32; - *root_sector = fat_boot->fat32.root_cluster; - } - else { - *type = ExFAT; - *root_sector = 0; - return; - } - *fds = first_data_sector; - *root_sector = ((*root_sector - 2) * fat_boot->sectors_per_cluster) + first_data_sector; -} - - - - - - - - - - - - - - - - -sector_buffer create_buffer(void *data) { - sector_buffer res; - res.data = data; - res.index = 0; - return res; -} - -void bread(sector_buffer *buf, void *_dest, unsigned long len) { - unsigned char *dest = _dest; - while(len) { - *(dest++) = buf->data[buf->index++]; - len--; - } -} -void blookahead(sector_buffer *buf, void *_dest, unsigned long len) { - unsigned char *dest = _dest; - unsigned int index = buf->index; - while(len) { - *(dest++) = buf->data[index++]; - len--; - } -} - -void parse_headers(sector_buffer *buf, char *filename, fat_directory_entry *dir) { - long_name_entry _entry; - long_name_entry *entry = &_entry; - unsigned char entry_number = 100; - - while(entry_number > 1) { - bread(buf, entry, sizeof(_entry)); - entry_number = (entry->entry_order)&0xF; - unsigned char offset = (entry_number - 1)*13; - - for(int i=0; i<5; i++) { - unsigned char c = entry->first_chars[i].byte_1; - if (c==0xFF || c==0x00) break; - else filename[offset++] = c; - } - for(int i=0; i<6; i++) { - unsigned char c = entry->second_chars[i].byte_1; - if (c==0xFF || c==0x00) break; - else filename[offset++] = c; - } - for(int i=0; i<2; i++) { - unsigned char c = entry->third_chars[i].byte_1; - if (c==0xFF || c==0x00) break; - else filename[offset++] = c; - } - } - bread(buf, dir, sizeof(fat_directory_entry)); -} - - -int _iterate_dir_entries(sector_buffer *buf, char **filename, fat_directory_entry *dir_e, void *(*alloc)(size_t)) { - unsigned char entry[32]; -try_again: - blookahead(buf, entry, sizeof(entry)); - if (entry[0] == 0x00) { // no entry here - return 0; - } - if (entry[0] == 0xE5) { // unused entry - buf->index += sizeof(entry); - goto try_again; - } - if (entry[11] == 0x0F) { // long name entry - *filename = alloc((entry[0]&0x0F) * 13); - parse_headers(buf, *filename, dir_e); - return 1; - } - bread(buf, dir_e, sizeof(fat_directory_entry)); - *filename = alloc(12); - memcpy(*filename, dir_e->file_name, 11); - for(int i=10; i; i--) { - if ((*filename)[i] != ' ') { - break; - } - (*filename)[i] = 0; - } - return 1; -} - -directory_ll *iterate_dir_entries(sector_buffer *buf, void *(*alloc)(size_t), fat_bootsector *bootsector) { - directory_ll *tail = NULL; - char *name = NULL; - fat_directory_entry dir; - while(_iterate_dir_entries(buf, &name, &dir, alloc)) { - directory_ll *next = alloc(sizeof(directory_ll)); - next->next = tail; - next->name = name; - next->entry = dir; - - unsigned int entry_cluster = - dir.entry_cluster_high_bits[1] << 24 | - dir.entry_cluster_high_bits[0] << 16 | - dir.entry_cluster_low_bits[1] << 8 | - dir.entry_cluster_low_bits[0]; - next->file_sector_start = get_sector_offset(entry_cluster & 0x0FFFFFFF, bootsector); - - tail = next; - } - return tail; -} - -unsigned long get_sector_offset(unsigned int sector, fat_bootsector *bs) { - unsigned long total_sectors = (bs->total_sectors_16 == 0)? bs->total_sectors_32 : bs->total_sectors_16; - unsigned long fat_size = (bs->table_size_16 == 0)? bs->fat32.table_size_32 : bs->table_size_16; - unsigned long root_dir_sectors = ((bs->root_entry_count * 32) + (bs->bytes_per_sector - 1)) / bs->bytes_per_sector; - unsigned int first_data_sector = bs->reserved_sector_count + (bs->table_count * fat_size) + root_dir_sectors; - return (((sector - 2) * bs->sectors_per_cluster) + first_data_sector); -} - -int is_directory(fat_directory_entry entry) { - return entry.file_attributes & 0x10; -} - -void tree(unsigned int indent, unsigned int dirsector, sector_buffer buf, fat_bootsector bootsector) { - buf.index = dirsector * 512; - directory_ll *entries = iterate_dir_entries(&buf, malloc, &bootsector); - - while(entries) { - for(int i=0; ientry)) { - printf("%s", entries->name); - if (!strncmp(entries->name, "..", 3)) { - printf(" (parent)\n"); - } else if (!strncmp(entries->name, ".", 2)) { - printf(" (self)\n"); - } else { - printf(":\n"); - tree(indent+1, entries->file_sector_start, buf, bootsector); - } - } else { - printf("%s[%i] size = %i\n", - entries->name, - entries->file_sector_start, - entries->entry.file_size); - } - entries = entries->next; - for(int i = 0; i<100000000; i++); - } -} - -int main(int argc, char **argv) { - int fd = open(argv[1], O_RDONLY); - void *__buf = malloc(1024*1024*100); - read(fd, __buf, 1024*1024*100); - sector_buffer buf = create_buffer(__buf); - - - - fat_bootsector bootsector; - fat_type type; - unsigned int root_sector; - unsigned int first_data_sector; - fat_directory_entry entry; - - - - bread(&buf, &bootsector, sizeof(fat_bootsector)); - if (bootsector.token != 0xAA55) { - return 1; - } - get_fat_info(&bootsector, &type, &root_sector, &first_data_sector); - - - tree(0, root_sector, buf, bootsector); -} -*/ \ No newline at end of file diff --git a/src/fs/mounts.c b/src/fs/mounts.c deleted file mode 100644 index da2b2e6..0000000 --- a/src/fs/mounts.c +++ /dev/null @@ -1,90 +0,0 @@ -/* -/* File operations -F_err f_open(F *file, char *name, uint16_t mode); -F_err f_close(F *file); -F_err f_read(F *file, void *dest, uint64_t to_read, uint64_t *actually_read); -F_err f_write(F *file, void *src, uint64_t to_write, uint64_t *actually_wrote); -F_err f_lseek(F *file, uint64_t seek_to); -F_err f_truncate(F *file); -F_err f_sync(F *file); -F_err f_tell(F *file, uint64_t *buffer_position); -F_err f_eof(F *file); -F_err f_size(F *file, uint64_t *file_size); - -/* Directory operations -F_err d_open(D *dir, char *name, uint16_t mode); -F_err d_close(D *dir); -F_err d_next(D *dir, char **name); -F_err d_rewind(D *dir); -F_err d_mkdir(char *name); - -F_err i_stat(S *stat, char *path); -F_err i_unlink(char *path); -F_err i_rename(char *old_path, char *new_path); -*/ - -#include -#include -#include -#include -#include - -#define fail_on(e) do {\ - F_err __ERROR_GEN=(e); \ - if(__ERROR_GEN) return __ERROR_GEN; \ -} while(0) - - -ctree_t *ROOT = NULL; - -F_err root_f_read(F *, void *, uint64_t, uint64_t *); -F_err root_f_close(F *); -F_err root_f_open(F *, char *, uint16_t); - - -filesystem_t *create_root_filesystem(void) { - filesystem_t *result = kmalloc(sizeof(filesystem_t *)); - result->f_open = root_f_open; - result->f_close = root_f_close; - result->f_read = root_f_read; - - ROOT = new_ctree(create_memory_filesystem()); - - return result; -} - -F_err _highest_mount(path_t *path, filesystem_t *fs) { - char *segment; - ctree_t *tree = ROOT; - path_for_each(path, segment) { - ctree_t *next = ctree_get_element(tree, segment, ctree_string_compare); - if (next) { - tree = next; - } else { - path->section--; - *fs = *((filesystem_t *)tree->data); - return NO_ERROR; - } - } - return FILE_NOT_FOUND; -} - -F_err root_f_open(F *file, char *name, uint16_t mode) { - path_t *path = _allocate_path(name); - filesystem_t fs; - - fail_on(_highest_mount(path, &fs)); - fail_on(fs.f_open(file, name, mode)); - return NO_ERROR; -} - -F_err root_f_close(F *file) { - fail_on(file->__fs__->f_close(file)); - return NO_ERROR; -} - -F_err root_f_read(F *file, void *dest, uint64_t to_read, uint64_t *did_read) { - fail_on(file->__fs__->f_read(file, dest, to_read, did_read)); - return NO_ERROR; -} - diff --git a/src/include/ctree.h b/src/include/ctree.h deleted file mode 100644 index 082cb8f..0000000 --- a/src/include/ctree.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef ctree_h -#define ctree_h - -typedef -struct _ctree_s { - void *data; - struct _ctree_l *branches; -} ctree_t; - -struct _ctree_l { - void *key; - ctree_t *branch; - struct _ctree_l *next; -}; - -ctree_t *ctree_get_element(ctree_t *, void *, int (*)(void *, void *)); -int ctree_string_compare(void *a, void *b); - -#endif \ No newline at end of file diff --git a/src/include/devfs.h b/src/include/devfs.h deleted file mode 100644 index ed40c5b..0000000 --- a/src/include/devfs.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef devfs_h -#define devfs_h - -#include -#include - -void devfs_put_device(char *, struct ata_device *); -void devfs_init(void); -struct ata_device *get_ata_by_dev_dir(char *); - -#endif diff --git a/src/include/fs_api.h b/src/include/fs_api.h deleted file mode 100644 index c46e60c..0000000 --- a/src/include/fs_api.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef fs_api_h -#define fs_api_h - -#include - -typedef enum { - NO_ERROR = 0, - FILE_NOT_FOUND = 1, -} F_err; - -typedef -struct _file{ - uint64_t __position__; - struct _filesystem_s *__fs__; -} F; - -typedef -struct _directory { - uint64_t __position__; -} D; - -typedef -struct _stat { - uint64_t __position__; -} S; - -typedef struct _filesystem_s { - /* File operations */ - F_err (*f_open)(F *file, char *name, uint16_t mode); - F_err (*f_close)(F *file); - F_err (*f_read)(F *file, void *dest, uint64_t to_read, uint64_t *actually_read); - F_err (*f_write)(F *file, void *src, uint64_t to_write, uint64_t *actually_wrote); - F_err (*f_lseek)(F *file, uint64_t seek_to); - F_err (*f_truncate)(F *file); - F_err (*f_sync)(F *file); - F_err (*f_tell)(F *file, uint64_t *buffer_position); - F_err (*f_eof)(F *file); - F_err (*f_size)(F *file, uint64_t *file_size); - - /* Directory operations */ - F_err (*d_open)(D *dir, char *name, uint16_t mode); - F_err (*d_close)(D *dir); - F_err (*d_next)(D *dir, char **name); - F_err (*d_rewind)(D *dir); - F_err (*d_mkdir)(char *name); - - /* Status operations */ - F_err (*i_stat)(S *stat, char *path); - F_err (*i_unlink)(char *path); - F_err (*i_rename)(char *old_path, char *new_path); -} filesystem_t; - -#endif \ No newline at end of file diff --git a/src/include/kshell.h b/src/include/kshell.h deleted file mode 100644 index afcef42..0000000 --- a/src/include/kshell.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef kshell_h -#define kshell_h - -#define RDL_SIZE 256 - -void kshell(unsigned char key); - -#endif diff --git a/src/include/path.h b/src/include/path.h deleted file mode 100644 index a7e6901..0000000 --- a/src/include/path.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef path_h -#define path_h - -#include - -typedef struct { - uint16_t section; - uint16_t total_sections; - char *__inside_string; - char *string; -} path_t; - -path_t *_allocate_path(char *); -void __path_next(path_t *); - -#define path_for_each(path, seg) \ - for(seg=path->__inside_string; \ - path->sectiontotal_sections; \ - __path_next(path),seg=path->__inside_string) - -#endif diff --git a/src/include/sorts.h b/src/include/sorts.h deleted file mode 100644 index 031ae39..0000000 --- a/src/include/sorts.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef sorts_h -#define sorts_h - -#include - -typedef -struct bsl_s { - uint64_t __size; - uint64_t __used; - struct { - void *data; - uint64_t id; - } *__store; -} bsl_t; - -bsl_t *new_bsl(); -uint64_t bsl_add(bsl_t *, void *); -void *bsl_remove(bsl_t *, uint64_t); -void *bsl_get(bsl_t *, uint64_t); -#endif diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index d4b49bc..8b37eaf 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,13 +1,15 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + extern void load_idt(void); int kmain(struct multiboot_header *); diff --git a/src/kernel/kshell.c b/src/kernel/kshell.c index 3ec0dee..ff1aee6 100644 --- a/src/kernel/kshell.c +++ b/src/kernel/kshell.c @@ -1,10 +1,9 @@ -#include -#include -#include -#include -#include -#include -#include +#include +#include + +#include +#include +#include uint8_t keymap[][128] = { {0}, diff --git a/src/kernel/multiboot.c b/src/kernel/multiboot.c index ab2c62f..e5a84b4 100644 --- a/src/kernel/multiboot.c +++ b/src/kernel/multiboot.c @@ -1,5 +1,5 @@ -#include "libk.h" -#include "multiboot.h" +#include +#include int valid_multiboot(struct multiboot_header *header) { const struct tag END = {.type=0, .flags=0, .size=8}; diff --git a/src/lib/ctree.c b/src/lib/ctree.c deleted file mode 100644 index 04d5435..0000000 --- a/src/lib/ctree.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include - -ctree_t *ctree_get_element(ctree_t *parent, void *search, int (*compare)(void *, void *)) { - if (parent == NULL) { - return NULL; - } - struct _ctree_l *lstart = parent->branches; - while(lstart) { - if (compare(search, lstart->key)) { - return lstart->branch; - } - lstart = lstart->next; - } - return NULL; -} - -int ctree_string_compare(void *_a, void *_b) { - char *a = _a; - char *b = _b; - return !strncmp(a, b, strlen(a)); -} \ No newline at end of file diff --git a/src/lib/libk.c b/src/lib/libk.c deleted file mode 100644 index cbe93d5..0000000 --- a/src/lib/libk.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "libk.h" -#include "mmu.h" - -void *memset(void *p, int b, size_t n) { - char *_p = (char *)p; - while(n --> 0) { - *_p++ = b; - } - return p; -} - -void wait(void) { - for(int i=0;i<10000000;i++); -} - -void *memcpy(void *_dest, void *_src, size_t bytes) { - char *dest = (char *)_dest; - char *src = (char *)_src; - while(bytes --> 0) { - *dest++ = *src++; - } -} - -uint64_t strlen(char *c) { - uint64_t res = 0; - while(*c) { - res++; - c++; - } - return res; -} - -uint64_t stringlen(char *c) { - uint64_t i = 0; - if (c) { - while(c[i]) { - i++; - } - } - return i; -} diff --git a/src/lib/path.c b/src/lib/path.c deleted file mode 100644 index 1132ed4..0000000 --- a/src/lib/path.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -path_t *_allocate_path(char *fromstring) { - path_t *result = kmalloc(sizeof(path_t)); - result->section = 0; - result->string = fromstring; - result->total_sections = 0; - result->__inside_string = fromstring; - while(*fromstring) { - if (*fromstring == '/') { - *fromstring = 0; - result->total_sections++; - } - fromstring++; - } - return result; -} - -void __path_next(path_t *path) { - while(*(path->__inside_string)) { - path->__inside_string++; - } - path->__inside_string++; - path->section++; -} \ No newline at end of file diff --git a/src/lib/sorts.c b/src/lib/sorts.c deleted file mode 100644 index 043a21b..0000000 --- a/src/lib/sorts.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -#include - -bsl_t *new_bsl() { - bsl_t *result = kmalloc(sizeof(bsl_t)); - result->__size = 0; - result->__used = 0; - result->__store = NULL; - return result; -} - -uint64_t bsl_add(bsl_t *bsl, void *item) { - if (bsl->__size == bsl->__used) { - uint64_t newsize = bsl->__size + 7; - uint64_t strucsz = sizeof(void *) + sizeof(uint64_t); - void *new_data = kmalloc(strucsz * newsize); - if (bsl->__store) { - memcpy(new_data, bsl->__store, strucsz * bsl->__size); - kfree(bsl->__store); - } - bsl->__store = new_data; - bsl->__size = newsize; - } - if (bsl->__used == 0) { - bsl->__store[0].id = 1; - } else { - bsl->__store[bsl->__used].id = bsl->__store[bsl->__used-1].id+1; - } - bsl->__store[bsl->__used].data = item; - bsl->__used ++; - return bsl->__store[bsl->__used-1].id; -} - -uint64_t _bsl_index(bsl_t *bsl, uint64_t id) { - uint64_t s_min = 0; - uint64_t s_max = bsl->__used; - while(s_min < s_max) { - uint64_t s_check = (s_min + s_max) / 2; - uint64_t id_check = bsl->__store[s_check].id; - if (id_check == id) { - return s_check; - } - if (id_check < id) { - s_min = s_check; - } - if (id_check > id) { - s_max = s_check; - } - } - uint64_t fail = 0; - return ~fail; -} - -void *bsl_remove(bsl_t *bsl, uint64_t id) { - uint64_t index = _bsl_index(bsl, id); - if (!~index) { - return NULL; - } - void *res = bsl->__store[index].data; - for(uint64_t i=index; i__used-1; i++) { - bsl->__store[i].id = bsl->__store[i+1].id; - bsl->__store[i].data = bsl->__store[i+1].data; - } - bsl->__used --; - return res; -} - -void *bsl_get(bsl_t *bsl, uint64_t id) { - uint64_t index = _bsl_index(bsl, id); - if (!~index) { - return NULL; - } - return bsl->__store[index].data; -} diff --git a/src/mmu/chunk_allocator.c b/src/mmu/chunk_allocator.c index 5e12fab..055f4af 100644 --- a/src/mmu/chunk_allocator.c +++ b/src/mmu/chunk_allocator.c @@ -1,6 +1,6 @@ -#include "ktype.h" -#include "libk.h" -#include "chunk_allocator.h" +#include +#include +#include struct segment_list *create_head(void *memory, uint64_t start, uint64_t end) { struct segment_list *result = memory; diff --git a/src/mmu/mmu.c b/src/mmu/mmu.c index 155af42..d4debb0 100644 --- a/src/mmu/mmu.c +++ b/src/mmu/mmu.c @@ -1,7 +1,8 @@ -#include -#include -#include -#include +#include +#include +#include +#include +#include /* * Static variables diff --git a/src/pci/ata.c b/src/pci/ata.c index 3cb455d..83fb64c 100644 --- a/src/pci/ata.c +++ b/src/pci/ata.c @@ -1,11 +1,13 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include + +#include +#include +#include +#include + +#include static struct ata_device ata_primary_master = {.io_base = 0x1F0, .control = 0x3F6, .slave = 0}; static struct ata_device ata_primary_slave = {.io_base = 0x1F0, .control = 0x3F6, .slave = 1}; diff --git a/src/pci/pci.c b/src/pci/pci.c index 518b60a..b2cb518 100644 --- a/src/pci/pci.c +++ b/src/pci/pci.c @@ -1,8 +1,9 @@ -#include "pci.h" -#include "ktype.h" -#include "kio.h" +#include +#include +#include +#include + #include "pci_vendor.h" -#include "libk.h" static inline int pci_extract_bus(uint32_t device) { diff --git a/src/pic/interrupts.c b/src/pic/interrupts.c index a07ed1c..24f8a2f 100644 --- a/src/pic/interrupts.c +++ b/src/pic/interrupts.c @@ -1,11 +1,9 @@ -#include "ktype.h" -#include "interrupts.h" -#include "libk.h" -#include "kshell.h" -#include "kio.h" -#include "registers.h" -#include "timer.h" -#include "lock.h" +#include +#include +#include +#include +#include +#include // static memory for the interrupt descriptor table idt_entry_t IDT[INTERRUPTS]; diff --git a/src/pic/keyboard.c b/src/pic/keyboard.c index f321ce2..ff231e9 100644 --- a/src/pic/keyboard.c +++ b/src/pic/keyboard.c @@ -1,9 +1,7 @@ -#include "interrupts.h" -#include "keyboard.h" -#include "ktype.h" -#include "kio.h" -#include "libk.h" - +#include +#include +#include +#include #define RINGSIZE 32 uint64_t readdex = 0; diff --git a/src/pic/timer.c b/src/pic/timer.c index 77a0746..d8e698c 100644 --- a/src/pic/timer.c +++ b/src/pic/timer.c @@ -1,7 +1,7 @@ -#include "timer.h" -#include "kio.h" -#include "libk.h" -#include "interrupts.h" + +#include +#include +#include int i = 0; int j = 0; diff --git a/src/shell/shell.c b/src/shell/shell.c new file mode 100644 index 0000000..16e55ff --- /dev/null +++ b/src/shell/shell.c @@ -0,0 +1,67 @@ +#include +#include + +uint32_t copy_num_to_buf(uint64_t num, char *buf) { + uint8_t digits = 0; + char nums[25]; + do { + nums[digits++] = (num%10) + '0'; + num /= 10; + } while(num); + uint8_t d_r = digits; + while(digits --> 0) { + *buf = nums[digits]; + *buf++; + } + return d_r; +} + +uint32_t _sprintf(int _a,int _b,int _c,int _d,int _e,int _f,int _g,int _h,int _i,int _j, char *dest, const char fmt[], ...) { + int chars = 0; + void **arg; + char c; + arg = (void **) &fmt; + arg++; + + c = *fmt; + while (c) { + switch (c) { + case '%': + fmt++; + c = *fmt; + if (arg) { + if (c) { + switch(c) { + case 's': + (void) "Copying a string"; + char *str = *((char **) arg); + uint64_t len = strlen(str); + memcpy(dest+chars, str, len); + chars += len; + break; + + case 'c': + dest[chars++] = *((char *)arg); + break; + + case 'i': + (void) "Copying an int."; + uint64_t num = *((uint64_t *) arg); + chars += copy_num_to_buf(num, dest+chars); + break; + + } + arg++; + } + } + break; + default: + dest[chars++] = c; + } + if (c) { + fmt++; + c = *fmt; + } + } + return chars; +} \ No newline at end of file diff --git a/src/lib/kio.c b/src/shell/tty.c similarity index 60% rename from src/lib/kio.c rename to src/shell/tty.c index 6137e2f..3b0c2fa 100644 --- a/src/lib/kio.c +++ b/src/shell/tty.c @@ -1,11 +1,13 @@ -#include "libk.h" -#include "kio.h" +#include +#include +#include +#include -char hexr(char c); char *VGA = (char *)0xb8000; uint8_t X; uint8_t Y; + void force_char(char c, uint8_t color, uint16_t x, uint16_t y) { uint32_t l = x + y*COLS; l *= VIDB; @@ -19,40 +21,6 @@ void force_simple_string(char *str, uint8_t color, uint16_t x, uint16_t y) { } } -int strncmp(char *a, char *b, size_t len) { - while(len-- && *a && *b) { - if (*a != *b) { - return *a - *b; - } - b++; - a++; - } - if (len != -1) { - return *a-*b; - } - return 0; -} - -char *strfnd(char *str, char fnd) { - while(*str && *str!=fnd) { - str++; - } - if (str) { - return str; - } - return (char *)0; -} - -uint64_t hex2int(char *x) { - uint64_t res = 0; - while(*x && ((*x>='0'&&*x<='9')||(*x>='A'&&*x<='Z')||(*x>='a'&&*x<='z'))) { - res *= 0x10; - res += hexr(*x); - x++; - } - return res; -} - void update_cursor(){ unsigned short position=(Y*COLS + X); outb(0x3D4, 0x0F); @@ -61,12 +29,6 @@ void update_cursor(){ outb(0x3D5, (unsigned char )((position>>8)&0xFF)); } -void kio_init(void) { - Y = (ROWS-1); - X = 0; - update_cursor(); -} - void clear_screen(void) { unsigned int i = 0; while (i < ROWS * COLS * VIDB) { @@ -75,16 +37,14 @@ void clear_screen(void) { } } -void mm_copy(void *dest, void *src, uint64_t bytes) { - char *d = (char *)dest; - char *s = (char *)src; - for(uint64_t i=0;i 0) { - *buf = nums[digits]; - *buf++; - } - return d_r; -} - -uint32_t _sprintf(int _a,int _b,int _c,int _d,int _e,int _f,int _g,int _h,int _i,int _j, char *dest, const char fmt[], ...) { - int chars = 0; - void **arg; - char c; - arg = (void **) &fmt; - arg++; - - c = *fmt; - while (c) { - switch (c) { - case '%': - fmt++; - c = *fmt; - if (arg) { - if (c) { - switch(c) { - case 's': - (void) "Copying a string"; - char *str = *((char **) arg); - uint64_t len = strlen(str); - memcpy(dest+chars, str, len); - chars += len; - break; - - case 'c': - dest[chars++] = *((char *)arg); - break; - - case 'i': - (void) "Copying an int."; - uint64_t num = *((uint64_t *) arg); - chars += copy_num_to_buf(num, dest+chars); - break; - - } - arg++; - } - } - break; - default: - dest[chars++] = c; - } - if (c) { - fmt++; - c = *fmt; - } - } - return chars; -} - uint32_t _kprintf(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a0, const char fmt[], ...) { int chars = 0; void **arg; diff --git a/src/std/string.c b/src/std/string.c new file mode 100644 index 0000000..519f1fa --- /dev/null +++ b/src/std/string.c @@ -0,0 +1,88 @@ +#include +#include + +char hexr(char c) { + switch(c) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'a': return 0xa; + case 'b': return 0xb; + case 'c': return 0xc; + case 'd': return 0xd; + case 'e': return 0xe; + case 'f': return 0xf; + case 'A': return 0xa; + case 'B': return 0xb; + case 'C': return 0xc; + case 'D': return 0xd; + case 'E': return 0xe; + case 'F': return 0xf; + } +} + +int strncmp(char *a, char *b, size_t len) { + while(len-- && *a && *b) { + if (*a != *b) { + return *a - *b; + } + b++; + a++; + } + if (len != -1) { + return *a-*b; + } + return 0; +} + +char *strfnd(char *str, char fnd) { + while(*str && *str!=fnd) { + str++; + } + if (str) { + return str; + } + return (char *)0; +} + +uint64_t hex2int(char *x) { + uint64_t res = 0; + while(*x && ((*x>='0'&&*x<='9')||(*x>='A'&&*x<='Z')||(*x>='a'&&*x<='z'))) { + res *= 0x10; + res += hexr(*x); + x++; + } + return res; +} + +uint64_t strlen(char *c) { + uint64_t res = 0; + while(*c) { + res++; + c++; + } + return res; +} + +void *memcpy(void *_dest, void *_src, size_t bytes) { + char *dest = (char *)_dest; + char *src = (char *)_src; + while(bytes --> 0) { + *dest++ = *src++; + } +} + +void *memset(void *p, int b, size_t n) { + char *_p = (char *)p; + while(n --> 0) { + *_p++ = b; + } + return p; +} \ No newline at end of file