KraKern

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

commit 3f814a6b69c4903ac635e00e200a5ffa10ff4237
parent 1f13ddbbba83f2d66b5f6bbb999b25699690658b
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue,  9 Feb 2016 19:46:13 +0100

Work(ext2): Not working

Diffstat:
MMakefile | 2++
Aclean.img | 0
Dkern.img | 0
Mkernel/IDE.cpp | 2++
Mmodules/x86/Ext2File.cpp | 2+-
Mmodules/x86/ModuleExt2.cpp | 94+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mmodules/x86/ModuleExt2.hpp | 17++++++++++-------
7 files changed, 66 insertions(+), 51 deletions(-)

diff --git a/Makefile b/Makefile @@ -39,6 +39,8 @@ $(KERNEL): $(OBJS) $(ASM) $(ASMFLAGS) -c $< -o $@ install: $(KERNEL) + rm -f kern.img + cp clean.img kern.img sudo losetup -o 32256 /dev/loop1 ./kern.img sudo mount /dev/loop1 /mnt sudo cp $(KERNEL) /mnt/boot/. diff --git a/clean.img b/clean.img Binary files differ. diff --git a/kern.img b/kern.img Binary files differ. diff --git a/kernel/IDE.cpp b/kernel/IDE.cpp @@ -82,5 +82,7 @@ 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]) + __kern.io.panic("The requested partition doesn't exist !"); return this->_parts[id]; } diff --git a/modules/x86/Ext2File.cpp b/modules/x86/Ext2File.cpp @@ -11,7 +11,7 @@ Ext2File::~Ext2File(void) { u32 Ext2File::open(u32 flag) { (void)flag; - ext2_inode *inode = this->_read_inode(this->disk, this->ext2inode); + ext2_inode *inode = this->_read_inode(this->disk, this->ext2inode); this->map = this->_read_file(this->disk, inode); __kern.mem.kfree(inode); return 0; diff --git a/modules/x86/ModuleExt2.cpp b/modules/x86/ModuleExt2.cpp @@ -2,9 +2,9 @@ ModuleExt2::ModuleExt2(char *name, u8 t, int is_root) : Module(name, t) { if (is_root) { - Ext2File *dir = new Ext2File("/boot"); + Ext2File *dir = new Ext2File("/"); + dir->disk = this->_get_disk_info(0, 0); dir->ext2inode = EXT2_INUM_ROOT; - this->_get_disk_info(__kern.fs.path("/dev/sda1"), dir); dir->scan(); } } @@ -13,46 +13,48 @@ ModuleExt2::~ModuleExt2(void) { } -void ModuleExt2::_get_disk_info(File *dev, Ext2File *fp) { +struct disk *ModuleExt2::_get_disk_info(int device, int partition) { struct disk *hd; int i, j; + (void)device; hd = (struct disk *)__kern.mem.kmalloc(sizeof(struct disk)); 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"); - this->_read_sb(dev, hd->sb); + hd->sb = this->_read_sb(hd, PART_OFFSET(partition)); + if (hd->sb->s_magic != 0xEF53) + __kern.io.panic("Not an ext2 drive."); 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; - this->_read_gd(hd->dev, hd->gd, hd); - fp->disk = hd; + hd->gd = this->_read_gd(hd, PART_OFFSET(partition)); + hd->part = partition; + return hd; } -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_super_block *ModuleExt2::_read_sb(struct disk *hd, int s_part) { + struct ext2_super_block *sb; + + (void)hd; + sb = (struct ext2_super_block *)__kern.mem.kmalloc(sizeof(struct ext2_super_block)); + __kern.ide.read(s_part + 1024, (u8 *)sb, sizeof(struct ext2_super_block)); + return sb; } -int ModuleExt2::_read_gd(File *fdev, struct ext2_group_desc *gd, struct disk *info) { - u32 offset; - int gd_size; +struct ext2_group_desc *ModuleExt2::_read_gd(struct disk *hd, int s_part) { + struct ext2_group_desc *gd; + int gd_size, offset; - 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; + 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); + __kern.ide.read(s_part + offset, (u8 *)gd, gd_size); + gd += 29; + return gd; } struct ext2_inode *ModuleExt2::_read_inode(struct disk *d, int i_num) { @@ -63,16 +65,15 @@ struct ext2_inode *ModuleExt2::_read_inode(struct disk *d, int i_num) { if (!inode) __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 = d->gd[gr_num].bg_inode_table * d->blocksize + index * d->sb->s_inode_size; - __kern.ide.read(__kern.ide.get_part(1)->s_lba * 512 + offset, (u8 *)inode, d->sb->s_inode_size); + 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); 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); @@ -82,17 +83,20 @@ 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 && inode->i_block[i]; i++) { - 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; + for (int i = 0; i < 12; 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); + mmap_head += n; + size -= n; + } } if (inode->i_block[12]) { - dev->read(inode->i_block[12] * hd->blocksize, (u8 *)p, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + inode->i_block[12] * hd->blocksize, (u8 *)p, hd->blocksize); for (int i = 0; i < (int)hd->blocksize / 4 && p[i]; i++) { - dev->read(p[i] * hd->blocksize, (u8 *)buf, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + p[i] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; @@ -100,11 +104,11 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { } } if (inode->i_block[13]) { - dev->read(inode->i_block[12] * hd->blocksize, (u8 *)p, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + inode->i_block[13] * hd->blocksize, (u8 *)p, hd->blocksize); for (int i = 0; i < (int)hd->blocksize / 4 && p[i]; i++) { - dev->read(p[i] * hd->blocksize, (u8 *)pp, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + p[i] * hd->blocksize, (u8 *)pp, hd->blocksize); for (int j = 0; j < (int)hd->blocksize / 4 && pp[j]; j++) { - dev->read(pp[j] * hd->blocksize, (u8 *)buf, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + pp[j] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; @@ -113,13 +117,13 @@ char *ModuleExt2::_read_file(struct disk *hd, struct ext2_inode *inode) { } } if (inode->i_block[14]) { - dev->read(inode->i_block[14] * hd->blocksize, (u8 *)p, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + inode->i_block[14] * hd->blocksize, (u8 *)p, hd->blocksize); for (int i = 0; i < (int)hd->blocksize / 4 && p[i]; i++) { - dev->read(p[i] * hd->blocksize, (u8 *)pp, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + p[i] * hd->blocksize, (u8 *)pp, hd->blocksize); for (int j = 0; j < (int)hd->blocksize / 4 && pp[j]; j++) { - dev->read(pp[j] * hd->blocksize, (u8 *)ppp, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + pp[j] * hd->blocksize, (u8 *)buf, hd->blocksize); for (int k = 0; k < (int)hd->blocksize / 4 && ppp[k]; k++) { - dev->read(ppp[k] * hd->blocksize, (u8 *)buf, hd->blocksize); + __kern.ide.read(PART_OFFSET(hd->part) + ppp[k] * hd->blocksize, (u8 *)buf, hd->blocksize); n = ((size > (int)hd->blocksize) ? hd->blocksize : size); memcpy(mmap_head, buf, n); mmap_head += n; @@ -160,7 +164,10 @@ 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 !"); filename = (char *)__kern.mem.kmalloc(dentry->name_len + 1); memcpy(filename, (char *)&dentry->name, dentry->name_len); filename[dentry->name_len] = 0x0; @@ -192,3 +199,4 @@ void ModuleExt2::_scan(Ext2File *dir) { dir->map = 0; } } + diff --git a/modules/x86/ModuleExt2.hpp b/modules/x86/ModuleExt2.hpp @@ -9,6 +9,8 @@ struct ext2_inode; struct disk; class Ext2File; +# define PART_OFFSET(num) (__kern.ide.get_part(num)->s_lba * 512) + class ModuleExt2 : public Module { public: ModuleExt2(char *name, u8 t, int is_root = 1); @@ -17,12 +19,12 @@ class ModuleExt2 : public Module { int is_directory(Ext2File *fp); protected: - 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); + struct disk *_get_disk_info(int device, int partition); + struct ext2_super_block *_read_sb(struct disk *hd, int s_part); + struct ext2_group_desc *_read_gd(struct disk *hd, int s_part); + struct ext2_inode *_read_inode(struct disk *d, int i_num); + char *_read_file(struct disk *hd, struct ext2_inode *inode); + void _scan(Ext2File *dir); }; class Ext2File : public ModuleExt2 { @@ -89,7 +91,8 @@ struct disk { u32 blocksize; u16 groups; /* Total number of groups */ struct ext2_group_desc *gd; - File *dev; + u32 device; + int part; }; struct ext2_group_desc {