KraKern

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

commit d79f75cb87ae54f1f0b668155715436935f14ea9
parent 3f814a6b69c4903ac635e00e200a5ffa10ff4237
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 15 Feb 2016 17:23:39 +0100

Fix(Ext2): Now working :)

Diffstat:
Dboot/x86/start.o | 0
Mclean.img | 0
Mincludes/core/File.hpp | 2+-
Mincludes/core/FileSystem.hpp | 1+
Mincludes/core/IDE.hpp | 1+
Mkernel/File.cpp | 5+++--
Mkernel/FileSystem.cpp | 2+-
Mkernel/IDE.cpp | 16+++++++++++++---
Mkernel/Io.cpp | 6+++---
Mkernel/Kernel.cpp | 6++++--
Mmodules/x86/ModuleExt2.cpp | 61++++++++++++++++++++++++++++++++++++++-----------------------
11 files changed, 65 insertions(+), 35 deletions(-)

diff --git a/boot/x86/start.o b/boot/x86/start.o Binary files differ. diff --git a/clean.img b/clean.img Binary files differ. diff --git a/includes/core/File.hpp b/includes/core/File.hpp @@ -37,7 +37,7 @@ class File { void set_next(File *n); void set_prev(File *n); void set_link(File *n); - void set_name(char *n); + void set_name(char *n, int verif = 1); char *get_name(void); File *get_parent(void); diff --git a/includes/core/FileSystem.hpp b/includes/core/FileSystem.hpp @@ -14,6 +14,7 @@ class FileSystem { u32 link(char *fname, char *newf); u32 add_file(char *dir, File *fp); File *get_root(); + void set_root(File *r); private: File *_root; diff --git a/includes/core/IDE.hpp b/includes/core/IDE.hpp @@ -29,6 +29,7 @@ class IDE { 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); + void _wait(unsigned short base); struct partition **_parts; }; diff --git a/kernel/File.cpp b/kernel/File.cpp @@ -99,11 +99,12 @@ void File::set_next(File *f) { this->_next = f; }; void File::set_prev(File *f) { this->_prev = f; }; void File::set_link(File *f) { this->_link = f; }; -void File::set_name(char *n) { +void File::set_name(char *n, int verif) { __kern.mem.kfree(this->_name); this->_name = (char *)__kern.mem.kmalloc(strlen(n) + 1); strcpy(this->_name, n); - this->_check_name(); + if (verif) + this->_check_name(); } u32 File::_inode_system = 0; diff --git a/kernel/FileSystem.cpp b/kernel/FileSystem.cpp @@ -4,7 +4,6 @@ void FileSystem::init(void) { File *sysd; __kern.io.loading("Filesystem ..."); - this->_root = new File("/", TYPE_DIRECTORY); this->_dev = this->_root->create_child("dev", TYPE_DIRECTORY); this->_var = this->_root->create_child("env", TYPE_DIRECTORY); this->_root->create_child("proc", TYPE_DIRECTORY); @@ -64,3 +63,4 @@ u32 FileSystem::add_file(char *dir, File *fp) { } File *FileSystem::get_root(void) { return this->_root; }; +void FileSystem::set_root(File *r) { this->_root = r; }; diff --git a/kernel/IDE.cpp b/kernel/IDE.cpp @@ -25,6 +25,7 @@ int IDE::read(u32 offset, u8 *buf, u32 count) { } int IDE::_bl_common(int drive, int numblock, int count) { + this->_wait(0x1f7); __kern.io.obyte(0x1F1, 0x00); __kern.io.obyte(0x1F2, count); __kern.io.obyte(0x1F3, (unsigned char) numblock); @@ -40,11 +41,12 @@ int IDE::_bl_read(int drive, int numblock, int count, char *buf) { this->_bl_common(drive, numblock, count); __kern.io.obyte(0x1F7, 0x20); + this->_wait(0x1f7); while (!(__kern.io.ibyte(0x1F7) & 0x08)); for (idx = 0; idx < 256 * count; idx++) { tmpword = __kern.io.iword(0x1F0); - buf[idx * 2] = (unsigned char) tmpword; - buf[idx * 2 + 1] = (unsigned char) (tmpword >> 8); + buf[idx * 2] = (unsigned char)tmpword; + buf[idx * 2 + 1] = (unsigned char)(tmpword >> 8); } return count; } @@ -55,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); + this->_wait(0x1F7); while (!(__kern.io.ibyte(0x1F7) & 0x08)); for (idx = 0; idx < 256 * count; idx++) { tmpword = (buf[idx * 2 + 1] << 8) | buf[idx * 2]; @@ -63,6 +66,10 @@ int IDE::_bl_write(int drive, int numblock, int count, char *buf) { return count; } +void IDE::_wait(unsigned short b) { + while((__kern.io.ibyte(b) & (0x80 | 0x40)) != 0x40); +} + void IDE::get_partitions(void) { struct partition *tmp; @@ -74,6 +81,8 @@ void IDE::get_partitions(void) { this->_parts = (struct partition **)__kern.mem.kmalloc(sizeof(struct partition) * 5); } this->_parts[i] = tmp; + } else { + this->_parts[i] = 0x0; } } this->_parts[5] = 0x0; @@ -82,7 +91,8 @@ void IDE::get_partitions(void) { struct partition *IDE::get_part(int id) { if (id < 0 || id > 4) __kern.io.panic("IDE: get_part: Bad argument"); - if (!this->_parts[id]) + if (!this->_parts[id]) { __kern.io.panic("The requested partition doesn't exist !"); + } return this->_parts[id]; } diff --git a/kernel/Io.cpp b/kernel/Io.cpp @@ -31,10 +31,10 @@ void Io::scrollup(u32 n) { *video = 0x0; *(video + 1) = 0x07; } - this->_y -= n; - if (this->_y < 0) - this->_y = 0; } + this->_y -= n; + if (this->_y < 0) + this->_y = 0; } void Io::save_screen(void) { diff --git a/kernel/Kernel.cpp b/kernel/Kernel.cpp @@ -34,14 +34,16 @@ void Kernel::init(multiboot_info *mbi) { this->gdt.init(); this->idt.init(); this->mem.init(mbi->high_mem); - this->fs = *(new FileSystem()); - this->fs.init(); this->pt = *(new ProcessTable()); this->pt.init(); this->ide = *(new IDE()); + this->fs = *(new FileSystem()); this->mods = &__modules; this->mods->init(); + this->fs.init(); this->ready(); + __kern.io.puts("\n"); + list_files(this->fs.get_root(), 0); while (1); } diff --git a/modules/x86/ModuleExt2.cpp b/modules/x86/ModuleExt2.cpp @@ -1,8 +1,12 @@ # include <ModuleExt2.hpp> +struct disk *ext2_get_disk_info(int device); +struct ext2_inode *ext2_read_inode(struct disk *hd, int i_num); + ModuleExt2::ModuleExt2(char *name, u8 t, int is_root) : Module(name, t) { if (is_root) { Ext2File *dir = new Ext2File("/"); + dir->set_name("/", 0); dir->disk = this->_get_disk_info(0, 0); dir->ext2inode = EXT2_INUM_ROOT; dir->scan(); @@ -26,13 +30,13 @@ struct disk *ModuleExt2::_get_disk_info(int device, int partition) { if (hd->sb->s_magic != 0xEF53) __kern.io.panic("Not an ext2 drive."); hd->blocksize = 1024 << hd->sb->s_log_block_size; - i = (hd->sb->s_blocks_count / hd->sb->s_blocks_per_group) + - ((hd->sb->s_blocks_count % hd->sb->s_blocks_per_group) ? 1 : 0); - j = (hd->sb->s_inodes_count / hd->sb->s_inodes_per_group) + - ((hd->sb->s_inodes_count % hd->sb->s_inodes_per_group) ? 1 : 0); + i = (hd->sb->s_blocks_count / hd->sb->s_blocks_per_group) + ((hd->sb->s_blocks_count % hd->sb->s_blocks_per_group) ? 1 : 0); + j = (hd->sb->s_inodes_count / hd->sb->s_inodes_per_group) + ((hd->sb->s_inodes_count % hd->sb->s_inodes_per_group) ? 1 : 0); hd->groups = (i > j) ? i : j; hd->gd = this->_read_gd(hd, PART_OFFSET(partition)); hd->part = partition; + if (!hd->gd->bg_block_bitmap) + __kern.io.panic("Group descriptor empty"); return hd; } @@ -52,8 +56,12 @@ struct ext2_group_desc *ModuleExt2::_read_gd(struct disk *hd, int s_part) { offset = hd->blocksize == 1024 ? 2048 : hd->blocksize; gd_size = hd->groups * sizeof(struct ext2_group_desc); gd = (struct ext2_group_desc *)__kern.mem.kmalloc(gd_size); + if (!gd) + __kern.io.panic("ext2: _read_gd(): Can't allocate group description structure"); +read: __kern.ide.read(s_part + offset, (u8 *)gd, gd_size); - gd += 29; + if (!gd->bg_block_bitmap) + goto read; return gd; } @@ -66,8 +74,8 @@ struct ext2_inode *ModuleExt2::_read_inode(struct disk *d, int i_num) { __kern.io.panic("ext2: _read_inode(): Cannot allocate memory"); gr_num = (i_num - 1) / d->sb->s_inodes_per_group; index = (i_num - 1) % d->sb->s_inodes_per_group; - offset = (index * d->sb->s_inode_size) / d->blocksize; - __kern.ide.read(PART_OFFSET(d->part) + offset + d->gd[gr_num].bg_inode_table, (u8 *)inode, d->sb->s_inode_size); + offset = d->gd[gr_num].bg_inode_table * d->blocksize + index * d->sb->s_inode_size; + __kern.ide.read(PART_OFFSET(d->part) + offset, (u8 *)inode, d->sb->s_inode_size); return inode; } @@ -83,9 +91,8 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { __kern.io.panic("ext2: _read_file(): Cannot allocate memory"); size = inode->i_size; mmap_head = mmap_base = (char *)__kern.mem.kmalloc(size); - for (int i = 0; i < 12; i++) { + for (int i = 0; i < 12 && inode->i_block[i]; i++) { if (inode->i_block[i]) { - __kern.io.puts("Hi there !"); __kern.ide.read(PART_OFFSET(hd->part) + inode->i_block[i] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); @@ -148,13 +155,16 @@ int ModuleExt2::is_directory(Ext2File *fp) { } void ModuleExt2::_scan(Ext2File *dir) { - struct ext2_inode *tmp, *inode = this->_read_inode(dir->disk, dir->ext2inode); + struct ext2_inode *tmp, *inode; struct directory_entry *dentry; Ext2File *leaf; u32 dsize; char *filename; int f_toclose = 0; + inode = this->_read_inode(dir->disk, dir->ext2inode); + if (!inode->i_size && dir->ext2inode == EXT2_INUM_ROOT) + __kern.io.panic("ext2: scan(): Empty root inode !"); if (dir->get_type() != TYPE_DIRECTORY) { __kern.io.panic("ext2: _scan(): Wrong argument (Expecting a directory)"); } @@ -164,39 +174,44 @@ void ModuleExt2::_scan(Ext2File *dir) { } dsize = inode->i_size; dentry = (struct directory_entry *)dir->map; - if (dentry) - __kern.io.puts("Yo !"); - while (dsize && inode) { - __kern.io.puts("Here !"); + while (inode && dsize) { filename = (char *)__kern.mem.kmalloc(dentry->name_len + 1); memcpy(filename, (char *)&dentry->name, dentry->name_len); filename[dentry->name_len] = 0x0; - __kern.io.puts(filename); - __kern.io.puts("\n"); - if (strcmp(".", filename) && strcmp("..", filename)) { + if (strcmp(".", filename) && strcmp("..", filename) && strcmp("", filename)) { if (!dir->find(filename)) { leaf = new Ext2File(filename); leaf->ext2inode = dentry->inode; leaf->disk = dir->disk; - if (this->is_directory(leaf)) - leaf->set_type(TYPE_DIRECTORY); - else - leaf->set_type(TYPE_FILE); + this->is_directory(leaf); dir->add_child(leaf); leaf->map = 0x0; tmp = this->_read_inode(leaf->disk, leaf->ext2inode); leaf->set_size(tmp->i_size); - __kern.mem.kfree(tmp); + if (tmp->i_mode & EXT2_S_IFDIR) { + __kern.mem.kfree(tmp); + leaf->set_type(TYPE_DIRECTORY); + } else { + __kern.mem.kfree(tmp); + leaf->set_type(TYPE_FILE); + } } } __kern.mem.kfree(filename); dsize -= dentry->rec_len; - dentry = dentry + dentry->rec_len; + dentry = (struct directory_entry *)((char *) dentry + dentry->rec_len); } __kern.mem.kfree(inode); if (f_toclose) { __kern.mem.kfree(dir->map); dir->map = 0; } + for (leaf = (Ext2File *)dir->get_child(); leaf; leaf = (Ext2File *)leaf->get_next()) { + if (leaf->get_type() == TYPE_DIRECTORY) + leaf->scan(); + } + if (!strcmp(dir->get_name(), "/")) { + __kern.fs.set_root(dir); + } }