KraKern

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

commit 7dc25137972295a6e73fb076c69087d01f5c64c1
parent 45989ac0b1b0ed44e53091e7388086eba83d1487
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sat,  6 Feb 2016 18:12:17 +0100

Fix(Ext2): Not working yet, missing partition handling

Diffstat:
Mincludes/core/Io.hpp | 1+
Mkernel/Io.cpp | 12+++++++++++-
Amodules/x86/Ext2File.cpp | 40++++++++++++++++++++++++++++++++++++++++
Mmodules/x86/Makefile | 2+-
Mmodules/x86/ModuleExt2.cpp | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mmodules/x86/ModuleExt2.hpp | 37++++++++++++++++++++++++++++++-------
6 files changed, 186 insertions(+), 47 deletions(-)

diff --git a/includes/core/Io.hpp b/includes/core/Io.hpp @@ -77,6 +77,7 @@ class Io { int _keypos; int _inlock; int _keystate; + int _in_loading; char _x; char _y; diff --git a/kernel/Io.cpp b/kernel/Io.cpp @@ -127,6 +127,7 @@ u32 Io::read(char *buf, u32 count) { void Io::loading(const char *s) { this->puts(s); for (int padding = 80 - strlen(s) - 10; padding > 0; padding--, this->puts(" ")); + this->_in_loading = 1; } void Io::done(void) { @@ -135,10 +136,19 @@ void Io::done(void) { this->puts(" DONE "); this->set_color(COLOR_WHITE); this->puts("]\n"); + this->_in_loading = 0; } void Io::panic(const char *s) { - this->puts("[ "); + if (this->_in_loading) { + this->puts("["); + this->set_color(COLOR_RED); + this->puts(" FAIL "); + this->set_color(COLOR_WHITE); + this->puts("]"); + this->_in_loading = 0; + } + this->puts("\n[ "); this->set_color(COLOR_RED); this->puts("PANIC"); this->set_color(COLOR_WHITE); diff --git a/modules/x86/Ext2File.cpp b/modules/x86/Ext2File.cpp @@ -0,0 +1,40 @@ +# include <ModuleExt2.hpp> + +Ext2File::Ext2File(char *n) : ModuleExt2(n, TYPE_DIRECTORY, 0) { + this->map = 0x0; +} + +Ext2File::~Ext2File(void) { + +} + +u32 Ext2File::open(u32 flag) { + (void)flag; + + ext2_inode *inode = this->_read_inode(this->disk, this->ext2inode); + this->map = this->_read_file(this->disk, inode); + __kern.mem.kfree(inode); + return 0; +} + +u32 Ext2File::read(u32 pos, u8 *buffer, u32 size) { + u32 bufsize = size; + + if ((pos + bufsize) > size) + bufsize = size - pos; + memcpy(buffer, (char *)(this->map + pos), bufsize); + return bufsize; +} + +u32 Ext2File::remove(void) { + delete this; + return 0; +} + +void Ext2File::scan(void) { + this->_scan(this); +} + +u32 Ext2File::close(void) { return 1; }; +u32 Ext2File::write(u32 pos, u8 *buffer, u32 size) { return 0; }; +u32 Ext2File::ioctl(u32 id, u8 *buffer) { return 0; }; diff --git a/modules/x86/Makefile b/modules/x86/Makefile @@ -1 +1 @@ -OBJS += modules/$(ARCH)/modules_conf.o modules/$(ARCH)/ModuleKeyboard.o modules/$(ARCH)/ModuleTime.o modules/$(ARCH)/ModuleExt2.o +OBJS += modules/$(ARCH)/modules_conf.o modules/$(ARCH)/ModuleKeyboard.o modules/$(ARCH)/ModuleTime.o modules/$(ARCH)/ModuleExt2.o modules/$(ARCH)/Ext2File.o diff --git a/modules/x86/ModuleExt2.cpp b/modules/x86/ModuleExt2.cpp @@ -1,53 +1,58 @@ # include <ModuleExt2.hpp> -ModuleExt2::ModuleExt2(char *name, u8 t) : Module(name, t) { - ; +ModuleExt2::ModuleExt2(char *name, u8 t, int is_root) : Module(name, t) { + if (is_root) { + Ext2File *dir = new Ext2File("/boot"); + dir->ext2inode = EXT2_INUM_ROOT; + this->_get_disk_info(__kern.fs.path("/dev/sda1"), dir); + dir->scan(); + } } ModuleExt2::~ModuleExt2(void) { } -struct disk *ModuleExt2::_get_disk_info(int n) { +void ModuleExt2::_get_disk_info(File *dev, Ext2File *fp) { struct disk *hd; int i, j; hd = (struct disk *)__kern.mem.kmalloc(sizeof(struct disk)); - if (!hd) + hd->sb = (struct ext2_super_block *)__kern.mem.kmalloc(sizeof(struct ext2_super_block)); + if (!hd || !hd->sb) __kern.io.panic("ext2: _get_disk_info(): Cannot allocate disk structure"); - hd->device = n; - hd->sb = this->_read_sb(n); + this->_read_sb(dev, hd->sb); hd->blocksize = 1024 << hd->sb->s_log_block_size; + hd->dev = dev; 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); - return hd; + this->_read_gd(hd->dev, hd->gd, hd); + fp->disk = hd; } -struct ext2_super_block *ModuleExt2::_read_sb(int n) { - struct ext2_super_block *sb; - - sb = (struct ext2_super_block *)__kern.mem.kmalloc(sizeof(struct ext2_super_block)); - if (!sb) - __kern.io.panic("ext2: _read_sb(): Cannot allocate memory"); - __kern.ide.read(n, 1024, (char *)sb, sizeof(struct ext2_super_block)); - return sb; +int ModuleExt2::_read_sb(File *dev, struct ext2_super_block *sb) { + if (dev) { + __kern.io.puts("HEre !"); + dev->read(1024, (u8 *)sb, sizeof(struct ext2_super_block)); + return 0; + } + return 1; } -struct ext2_group_desc *ModuleExt2::_read_gd(struct disk *d) { - struct ext2_group_desc *gd; - int offset, gd_size; - - offset = (d->blocksize == 1024) ? 2048 : d->blocksize; - gd_size = d->groups * sizeof(struct ext2_group_desc); - gd = (struct ext2_group_desc *)__kern.mem.kmalloc(gd_size); - if (!gd) - __kern.io.panic("ext2: _read_gd(): Cannot allocate memory"); - __kern.ide.read(d->device, offset, (char *)gd, gd_size); - return gd; +int ModuleExt2::_read_gd(File *fdev, struct ext2_group_desc *gd, struct disk *info) { + u32 offset; + int gd_size; + + if (fdev) { + offset = ((info->blocksize == 1024) ? 2048 : info->blocksize); + gd_size = (info->groups * (int)sizeof(struct ext2_group_desc)); + fdev->read(offset, (u8 *)gd, gd_size); + return 0; + } + return 1; } struct ext2_inode *ModuleExt2::_read_inode(struct disk *d, int i_num) { @@ -60,13 +65,15 @@ struct ext2_inode *ModuleExt2::_read_inode(struct disk *d, int i_num) { gr_num = (i_num - 1) / d->sb->s_inodes_per_group; index = (i_num - 1) & d->sb->s_inodes_per_group; offset = d->gd[gr_num].bg_inode_table * d->blocksize + index * d->sb->s_inode_size; - __kern.ide.read(d->device, offset, (char *)inode, d->sb->s_inode_size); + if (d->dev) + d->dev->read(offset, (u8 *)inode, d->sb->s_inode_size); return inode; } char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { char *mmap_base, *mmap_head, *buf; int *p, *pp, *ppp, n, size; + File *dev = hd->dev; buf = (char *)__kern.mem.kmalloc(hd->blocksize); p = (int *)__kern.mem.kmalloc(hd->blocksize); @@ -77,16 +84,16 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { size = inode->i_size; mmap_head = mmap_base = (char *)__kern.mem.kmalloc(size); for (int i = 0; i < 12 && inode->i_block[i]; i++) { - __kern.ide.read(hd->device, inode->i_block[i] * hd->blocksize, buf, hd->blocksize); + dev->read(inode->i_block[i] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; size -= n; } if (inode->i_block[12]) { - __kern.ide.read(hd->device, inode->i_block[12] * hd->blocksize, (char *)p, hd->blocksize); + dev->read(inode->i_block[12] * hd->blocksize, (u8 *)p, hd->blocksize); for (int i = 0; i < (int)hd->blocksize / 4 && p[i]; i++) { - __kern.ide.read(hd->device, p[i] * hd->blocksize, buf, hd->blocksize); + dev->read(p[i] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; @@ -94,11 +101,11 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { } } if (inode->i_block[13]) { - __kern.ide.read(hd->device, inode->i_block[12] * hd->blocksize, (char *)p, hd->blocksize); + dev->read(inode->i_block[12] * hd->blocksize, (u8 *)p, hd->blocksize); for (int i = 0; i < (int)hd->blocksize / 4 && p[i]; i++) { - __kern.ide.read(hd->device, p[i] * hd->blocksize, (char *)pp, hd->blocksize); + dev->read(p[i] * hd->blocksize, (u8 *)pp, hd->blocksize); for (int j = 0; j < (int)hd->blocksize / 4 && pp[j]; j++) { - __kern.ide.read(hd->device, pp[j] * hd->blocksize, buf, hd->blocksize); + dev->read(pp[j] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; @@ -107,13 +114,13 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { } } if (inode->i_block[14]) { - __kern.ide.read(hd->device, inode->i_block[14] * hd->blocksize, (char *)p, hd->blocksize); + dev->read(inode->i_block[14] * hd->blocksize, (u8 *)p, hd->blocksize); for (int i = 0; i < (int)hd->blocksize / 4 && p[i]; i++) { - __kern.ide.read(hd->device, p[i] * hd->blocksize, (char *)pp, hd->blocksize); + dev->read(p[i] * hd->blocksize, (u8 *)pp, hd->blocksize); for (int j = 0; j < (int)hd->blocksize / 4 && pp[j]; j++) { - __kern.ide.read(hd->device, pp[j] * hd->blocksize, (char *)ppp, hd->blocksize); + dev->read(pp[j] * hd->blocksize, (u8 *)ppp, hd->blocksize); for (int k = 0; k < (int)hd->blocksize / 4 && ppp[k]; k++) { - __kern.ide.read(hd->device, ppp[k] * hd->blocksize, buf, hd->blocksize); + dev->read(ppp[k] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; @@ -128,3 +135,61 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { __kern.mem.kfree(ppp); return mmap_base; } + +int ModuleExt2::is_directory(Ext2File *fp) { + struct ext2_inode *inode = this->_read_inode(fp->disk, fp->ext2inode); + int ret = (inode->i_mode & EXT2_S_IFDIR) ? 1 : 0; + + __kern.mem.kfree(inode); + return ret; +} + +void ModuleExt2::_scan(Ext2File *dir) { + struct ext2_inode *tmp, *inode = this->_read_inode(dir->disk, dir->ext2inode); + struct directory_entry *dentry; + Ext2File *leaf; + u32 dsize; + char *filename; + int f_toclose = 0; + + if (dir->get_type() != TYPE_DIRECTORY) { + __kern.io.panic("ext2: _scan(): Wrong argument (Expecting a directory)"); + } + if (!dir->map) { + dir->map = this->_read_file(dir->disk, inode); + f_toclose = 1; + } + dsize = inode->i_size; + dentry = (struct directory_entry *)dir->map; + while (dsize && inode) { + 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 (!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); + 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); + } + } + __kern.mem.kfree(filename); + dsize -= dentry->rec_len; + dentry = dentry + dentry->rec_len; + } + __kern.mem.kfree(inode); + if (f_toclose) { + __kern.mem.kfree(dir->map); + dir->map = 0; + } +} diff --git a/modules/x86/ModuleExt2.hpp b/modules/x86/ModuleExt2.hpp @@ -7,18 +7,40 @@ struct ext2_super_block; struct ext2_group_desc; struct ext2_inode; struct disk; +class Ext2File; class ModuleExt2 : public Module { public: - ModuleExt2(char *name, u8 t); + ModuleExt2(char *name, u8 t, int is_root = 1); ~ModuleExt2(void); + int is_directory(Ext2File *fp); + protected: - struct disk *_get_disk_info(int n); - struct ext2_super_block *_read_sb(int n); - struct ext2_group_desc *_read_gd(struct disk *d); - struct ext2_inode *_read_inode(struct disk *d, int i_num); - char *_read_file(struct disk *d, struct ext2_inode *i); + void _get_disk_info(File *dev, Ext2File *fp); + int _read_sb(File *dev, ext2_super_block *sb); + int _read_gd(File *dev, ext2_group_desc *gd, struct disk *info); + struct ext2_inode *_read_inode(struct disk *d, int i_num); + char *_read_file(struct disk *d, struct ext2_inode *i); + void _scan(Ext2File *dir); +}; + +class Ext2File : public ModuleExt2 { + public: + Ext2File(char *n); + ~Ext2File(void); + + virtual u32 open(u32 flag); + virtual u32 close(void); + virtual u32 read(u32 pos,u8* buffer,u32 sizee); + virtual u32 write(u32 pos,u8* buffer,u32 sizee); + virtual u32 ioctl(u32 id,u8* buffer); + virtual u32 remove(void); + virtual void scan(void); + + char *map; + struct disk *disk; + int ext2inode; }; // List of ext2 structures. @@ -63,11 +85,11 @@ struct ext2_super_block { } __attribute__ ((packed)); struct disk { - int device; struct ext2_super_block *sb; u32 blocksize; u16 groups; /* Total number of groups */ struct ext2_group_desc *gd; + File *dev; }; struct ext2_group_desc { @@ -149,5 +171,6 @@ struct directory_entry { # define EXT2_S_IROTH 0x0004 /* read */ # define EXT2_S_IWOTH 0x0002 /* write */ # define EXT2_S_IXOTH 0x0001 /* execute */ +# define EXT2_INUM_ROOT 2 #endif