diff --git a/Makefile b/Makefile index dff642fc..74b5aa2c 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ bootloader/stage1.bin: bootloader/stage1/main.o bootloader/stage1/start.o bootlo # Stage 2 bootloader/stage2/main.o: bootloader/stage2/main.c @${ECHO} -n "\033[32m CC $<\033[0m" - @${GCC} ${CFLAGS} -c -o $@ $< + @${GCC} ${CFLAGS} -I./kernel/include -c -o $@ $< @${ECHO} "\r\033[32;1m CC $<\033[0m" bootloader/stage2/start.o: bootloader/stage2/start.s diff --git a/bootloader/stage1/main.c b/bootloader/stage1/main.c index e0d67305..27ef0da5 100644 --- a/bootloader/stage1/main.c +++ b/bootloader/stage1/main.c @@ -33,7 +33,7 @@ void read(unsigned char count, unsigned char sector, short segment, short offset void main() { PRINT("Loading... "); - read(2,2,0,0x7e00); + read(6,2,0,0x7e00); PRINT("Ready.\r\n"); /* Let's do this... */ diff --git a/bootloader/stage2/link.ld b/bootloader/stage2/link.ld index 203507e2..f3cc48a5 100644 --- a/bootloader/stage2/link.ld +++ b/bootloader/stage2/link.ld @@ -21,7 +21,7 @@ SECTIONS .padding : AT (phys + SIZEOF(.text) + SIZEOF(.data) + SIZEOF(.rodata)) { /* Pad so we have at least one sector */ - . = ALIGN(4096); + . = ALIGN(512); /* Add an additional sector for good measure */ . = ALIGN(508); /* End it with a sad face */ diff --git a/bootloader/stage2/main.c b/bootloader/stage2/main.c index d1046a7a..c3314360 100644 --- a/bootloader/stage2/main.c +++ b/bootloader/stage2/main.c @@ -7,15 +7,88 @@ */ __asm__(".code16gcc\n"); +#define BOOTLOADER +#include + +#define EXT2_SUPER_OFFSET 0x1000 +#define EXT2_START (EXT2_SUPER_OFFSET - 0x400) + +#define ext2_get_block(block) (EXT2_START + (0x400 << sblock->log_block_size) * block) + #define PRINT(s) __asm__ ("movw %0, %%si\ncall _print" : : "l"((short)(int)s)) +void read(unsigned char count, unsigned char sector, short segment, short offset) +{ + __asm__ __volatile__ ( + "movw %0, %%es\n" + "movw %1, %%bx\n" + "movb %2, %%al\n" + "movb $0, %%ch\n" + "movb %3, %%cl\n" + "movb $0, %%dh\n" + "movb $0x80, %%dl\n" + "movb $0x02, %%ah\n" + "int $0x13" : : + "l" (segment), + "l" (offset), + "m" (count), + "m" (sector)); +} + /* * Main entry point */ void main() { - PRINT("== Mr. Boots Stage 2 Bootloader ==\r\n"); + PRINT("M"); + /* + * Load up the superblock + */ + read(16,3,0,EXT2_SUPER_OFFSET); + ext2_superblock_t * sblock = (ext2_superblock_t *)(EXT2_START + 0x400); + if (sblock->magic != EXT2_SUPER_MAGIC) { + goto failure; + } + if (sblock->inode_size == 0) { + sblock->inode_size = 128; + } + ext2_bgdescriptor_t * rblock = (ext2_bgdescriptor_t *)(EXT2_START + 0x400 + 0x400); + ext2_inodetable_t * itable = (ext2_inodetable_t *)(EXT2_START + (0x400 << sblock->log_block_size) * rblock->inode_table); + ext2_inodetable_t * rnode = (ext2_inodetable_t *)((unsigned short)(unsigned int)itable + (unsigned short)(unsigned int)sblock->inode_size); + PRINT("r"); + /* + * Grab the first inode's data... + */ + read(2,9 + (ext2_get_block((rnode->block[0]))) / 0x200,0,EXT2_SUPER_OFFSET + 0xc00); + void * block; + ext2_dir_t * direntry = NULL; + block = (void *)ext2_get_block((rnode->block[0])); + + PRINT(". "); + + uint32_t dir_offset = 0; + dir_offset = 0; + while (dir_offset < rnode->size) { + ext2_dir_t * d_ent = (ext2_dir_t *)((unsigned short)(unsigned int)block + dir_offset); + if (((char *)&d_ent->name)[0] == 'k'){ + direntry = d_ent; + goto success; + } + dir_offset += d_ent->rec_len; + } + goto failure; +success: + PRINT("B"); + + + PRINT("o"); + PRINT("o"); + PRINT("t"); + PRINT("s"); + +failure: + PRINT("\023"); /* And that's it for now... */ __asm__ __volatile__ ("hlt"); while (1) {}; diff --git a/kernel/core/fs/ext2_initrd.c b/kernel/core/fs/ext2_initrd.c index b8fecb64..4521fcf7 100644 --- a/kernel/core/fs/ext2_initrd.c +++ b/kernel/core/fs/ext2_initrd.c @@ -376,6 +376,10 @@ initrd_mount( initrd_root_block = (ext2_bgdescriptor_t *)((uintptr_t)initrd_start + 1024 + 1024); initrd_inode_table = (ext2_inodetable_t *)((uintptr_t)initrd_start + (1024 << initrd_superblock->log_block_size) * initrd_root_block->inode_table); ext2_inodetable_t * root_inode = ext2_get_inode(2); + kprintf("[DEBUG] root block 0 is %d\n", root_inode->block[0]); + kprintf("[DEBUG] root size is %d\n", root_inode->size); + kprintf("[DEBUG] uid is %d\n", root_inode->uid); + kprintf("[DEBUG] atime is %d\n", root_inode->atime); initrd_root = (fs_node_t *)malloc(sizeof(fs_node_t)); assert(initrd_node_root(root_inode, initrd_root)); fs_root = initrd_root; diff --git a/kernel/include/ext2.h b/kernel/include/ext2.h index b164c35c..3077903b 100644 --- a/kernel/include/ext2.h +++ b/kernel/include/ext2.h @@ -1,8 +1,7 @@ #ifndef EXT2_H #define EXT2_h -#include -#include +#include #define EXT2_SUPER_MAGIC 0xEF53 diff --git a/kernel/include/system.h b/kernel/include/system.h index 0dbc315d..4802c38f 100644 --- a/kernel/include/system.h +++ b/kernel/include/system.h @@ -1,25 +1,12 @@ #ifndef __SYSTEM_H #define __SYSTEM_H -/* Types */ - -#define NULL ((void *)0UL) - -typedef unsigned long uintptr_t; -typedef long size_t; -typedef unsigned int uint32_t; -typedef unsigned short uint16_t; -typedef unsigned char uint8_t; -typedef unsigned long long uint64_t; +#include /* Unimportant Kernel Strings */ #define KERNEL_UNAME "ToAruOS" #define KERNEL_VERSION_STRING "0.0.1" -#define CHAR_BIT 8 -#define INT32_MAX 0x7fffffffL -#define UINT32_MAX 0xffffffffL - extern void *sbrk(uintptr_t increment); /* Kernel Main */ diff --git a/kernel/include/types.h b/kernel/include/types.h new file mode 100644 index 00000000..197d99a1 --- /dev/null +++ b/kernel/include/types.h @@ -0,0 +1,24 @@ +#ifndef TYPES_H +#define TYPES_H + +/* Types */ + +#define NULL ((void *)0UL) + +#ifndef BOOTLOADER +typedef unsigned long uintptr_t; +#else +typedef unsigned short uintptr_t; +#endif +typedef unsigned long size_t; +typedef unsigned int uint32_t; +typedef unsigned short uint16_t; +typedef unsigned char uint8_t; +typedef unsigned long long uint64_t; + + +#define CHAR_BIT 8 +#define INT32_MAX 0x7fffffffL +#define UINT32_MAX 0xffffffffL + +#endif