KraKern

C++ Kernel
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/KraKern

commit 768dad6e9bb1ae9cadfd9b5fa93d417c7ddd4f6f
parent 7dc25137972295a6e73fb076c69087d01f5c64c1
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon,  8 Feb 2016 17:33:55 +0100

Work(Partitions): Not working.

Diffstat:
MMakefile | 2+-
Mincludes/core/IDE.hpp | 17++++++++++++++++-
Mkernel/IDE.cpp | 47++++++++++++++++++++++++++++++++++++-----------
Mkernel/Io.cpp | 1+
Mkernel/Kernel.cpp | 2+-
5 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/Makefile b/Makefile @@ -39,7 +39,7 @@ $(KERNEL): $(OBJS) $(ASM) $(ASMFLAGS) -c $< -o $@ install: $(KERNEL) - sudo losetup -o 512 /dev/loop1 ./kern.img + sudo losetup -o 32256 /dev/loop1 ./kern.img sudo mount /dev/loop1 /mnt sudo cp $(KERNEL) /mnt/boot/. sudo umount /mnt diff --git a/includes/core/IDE.hpp b/includes/core/IDE.hpp @@ -3,17 +3,32 @@ # include <os.h> +struct partition { + u8 bootable; /* 0 = no, 0x80 = bootable */ + u8 s_head; /* Starting head */ + u16 s_sector:6; /* Starting sector */ + u16 s_cyl:10; /* Starting cylinder */ + u8 id; /* System ID */ + u8 e_head; /* Ending Head */ + u16 e_sector:6; /* Ending Sector */ + u16 e_cyl:10; /* Ending Cylinder */ + u32 s_lba; /* Starting LBA value */ + u32 size; /* Total Sectors in partition */ +} __attribute__ ((packed)); + class IDE { public: IDE(void); ~IDE(void); - int read(int drive, int offset, char *buf, int count); + int read(u32 pos, u8 *buffer, u32 size); + void get_partitions(void); private: int _bl_write(int drive, int numblock, int count, char *buf); int _bl_read(int drive, int numblock, int count, char *buf); int _bl_common(int drive, int numblock, int count); + struct partition *_parts; }; # include <Kernel.hpp> diff --git a/kernel/IDE.cpp b/kernel/IDE.cpp @@ -1,6 +1,8 @@ # include <IDE.hpp> IDE::IDE(void) { + this->_parts = 0x0; + this->get_partitions(); return ; } @@ -8,21 +10,24 @@ IDE::~IDE(void) { return ; } -int IDE::read(int drive, int offset, char *buf, int count) { - char *bl_buf; - int beg, end, blocks; - - beg = offset / 512; - end = (offset + count) / 512; - blocks = end - beg + 1; - bl_buf = (char *)__kern.mem.kmalloc(blocks * 512); - this->_bl_read(drive, beg, blocks, bl_buf); - memcpy(buf, (char *)(bl_buf + offset % 512), count); - __kern.mem.kfree(bl_buf); +int IDE::read(u32 pos, u8 *buffer, u32 size) { + int count = size, offset = pos, bl_begin, bl_end, blocks; + char *bl_buffer; + + if (!buffer) + return -1; + bl_begin = (offset / 512); + bl_end = ((offset + count) / 512); + blocks = bl_end - bl_begin + 1; + bl_buffer = (char *)__kern.mem.kmalloc(blocks * 512); + this->_bl_read(0, bl_begin, blocks, bl_buffer); + memcpy((char *)buffer, (char *)((int)bl_buffer + (offset % 512)), count); + __kern.mem.kfree(bl_buffer); return count; } int IDE::_bl_common(int drive, int numblock, int count) { + while(__kern.io.ibyte(0x1F0 + 0x206) & 0x80); __kern.io.obyte(0x1F1, 0x00); __kern.io.obyte(0x1F2, count); __kern.io.obyte(0x1F3, numblock); @@ -37,6 +42,7 @@ int IDE::_bl_read(int drive, int numblock, int count, char *buf) { this->_bl_common(drive, numblock, count); __kern.io.obyte(0x1F7, 0x20); + while(__kern.io.ibyte(0x1F0 + 0x206) & 0x80); while (!(__kern.io.ibyte(0x1F7) & 0x08)); for (int idx = 0; idx < 256 * count; idx++) { tmp = __kern.io.iword(0x1F0); @@ -51,6 +57,7 @@ int IDE::_bl_write(int drive, int numblock, int count, char *buf) { this->_bl_common(drive, numblock, count); __kern.io.obyte(0x1F7, 0x30); + while(__kern.io.ibyte(0x1F0 + 0x206) & 0x80); while (!(__kern.io.ibyte(0x1F7) & 0x08)); for (int idx = 0; idx < 256 * count; idx++) { tmp = (buf[idx * 2 + 1] << 8) | buf[idx * 2]; @@ -58,3 +65,21 @@ int IDE::_bl_write(int drive, int numblock, int count, char *buf) { } return count; } + +void IDE::get_partitions(void) { + struct partition *tmp; + int disk[] = {0x01BE, 0x01CE, 0x01DE, 0x1EE}; + char str[15]; + + for (int i = 0; i < 4; i++) { + tmp = (struct partition *)__kern.mem.kmalloc(sizeof(struct partition)); + this->read(disk[i] + i * sizeof(struct partition), (u8 *)tmp, sizeof(struct partition)); + if (tmp->size) { + __kern.io.puts("Found a partition ! size: "); + itoa(str, tmp->size, 10); + __kern.io.puts(str); + __kern.io.puts("\n"); + } + tmp = 0x0; + } +} diff --git a/kernel/Io.cpp b/kernel/Io.cpp @@ -155,6 +155,7 @@ void Io::panic(const char *s) { this->puts(" ]: "); this->puts(s); asm("hlt"); + while (1); // Reboot } diff --git a/kernel/Kernel.cpp b/kernel/Kernel.cpp @@ -38,9 +38,9 @@ void Kernel::init(multiboot_info *mbi) { this->fs.init(); this->pt = *(new ProcessTable()); this->pt.init(); + this->ide = *(new IDE()); this->mods = &__modules; this->mods->init(); - this->ide = *(new IDE()); this->ready(); while (1); }