summaryrefslogtreecommitdiff
path: root/fs/ext2.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext2.c')
-rw-r--r--fs/ext2.c143
1 files changed, 141 insertions, 2 deletions
diff --git a/fs/ext2.c b/fs/ext2.c
index bc83f55..bdfb80b 100644
--- a/fs/ext2.c
+++ b/fs/ext2.c
@@ -1,7 +1,10 @@
// ext2 minidriver
// based on osdev wiki article: http://wiki.osdev.org/Ext2
+#define FOOLOS_MODULE_NAME "ext2"
+
#include "lib/int/stdint.h"
+#include "lib/logger/log.h"
typedef struct ext2_superblock_struct
{
@@ -95,13 +98,149 @@ typedef struct ext2_inode_struct
}ext2_inode;
+void ram_read(char *in,char *out,int size, int count)
+{
+ for(int i=0;i<size*count;i++)
+ {
+ out[i]=in[i];
+ }
- /*
-int main()
+}
+
+int ext2_check(uint8_t *ram)
{
ext2_superblock super;
ext2_superblock_ext super_ext;
+ uint8_t *ptr=ram+1024;
+ ram_read(ptr,&super,sizeof(super),1);
+ if(super.ext2_sig!=0xef53)log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"no ext2 signature found, where ram-image expected!");
+ //check version and req features.
+ //
+}
+
+ext2_inode ext2_get_inode(uint8_t *ram,int inode_num)
+{
+
+ // get basic shit from superblock!
+ ext2_superblock super;
+ ext2_blockgroup_desc desc;
+ ext2_inode inode;
+
+ uint8_t *ptr=ram+1024;
+ ram_read(ptr,&super,sizeof(super),1);
+ if(super.ext2_sig!=0xef53)log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"no ext2 signature found, where ram-image expected!");
+
+ int block_group=(inode_num-1)/super.inodes_per_group;
+
+ // get descrtptor for our blockgroup!
+ //TODO: only correct for 1024bytes/block. read more than one!!!
+ int block_size=1024;
+ int descriptor_start_block=2;
+
+ ptr=ram+block_size*descriptor_start_block;
+ ptr+=sizeof(desc)*block_group; // skip to our descriptor;
+
+ ram_read(ptr,&desc,sizeof(desc),1); // read descriptor
+
+ // read our inode;
+ ptr=ram+block_size*desc.addr_inode_table;
+ ptr+=128*((inode_num-1)%super.inodes_per_group);
+
+ ram_read(ptr,&inode,sizeof(inode),1); //get inode 2 (rood-directory)
+
+ return inode;
+
+}
+
+
+int ext2_inode_content(char *ram,int inode_nr,uint8_t *ramdest,int max)
+{
+ ext2_inode inode=ext2_get_inode(ram,inode_nr);
+
+ int pos=0;
+ int block=0;
+ int block_size=1024;
+ int block_counter=0;
+ uint8_t *ptr=ram+block_size*inode.direct_pointer[0];
+
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Loading %d %d bytes",inode.size_high, inode.size_low);
+ while(pos<inode.size_low) // use size high?
+ {
+ ramdest[pos]=*ptr;
+ ptr++;
+ block_counter++;
+ pos++;
+ if(block_counter>=block_size)
+ {
+ block++;
+
+ if(block>=12)
+ {
+ int indirect_block=block-12;
+ uint32_t *indirect_ptr=ram+block_size*inode.indirect1;
+ ptr=ram+block_size*indirect_ptr[indirect_block];
+
+ }
+ else
+ {
+ ptr=ram+block_size*inode.direct_pointer[block];
+ }
+
+ block_counter=0;
+ }
+
+
+
+ }
+// if(block>=12)panic(FOOLOS_MODULE_NAME,"such big files unsupported yet");
+
+
+}
+
+
+
+int ext2_read_root_dir(uint8_t *ram)
+{
+
+ ext2_inode inode=ext2_get_inode(ram,2);
+
+ char buf[256];
+ int block_size=1024;
+ uint8_t *ptr=ram+block_size*inode.direct_pointer[0]; //use other pointers in future!
+
+ int pos=0;
+ while(pos<inode.size_low) // use size high?
+ {
+
+ ext2_dir dir;
+ ram_read(ptr,&dir,sizeof(dir),1);
+ ptr+=sizeof(dir);
+
+ /*
+ printf("inode: %i\n",dir.inode);
+ printf("total size: %i\n",dir.size);
+ printf("length low/high: %i/%i\n\n",dir.name_length_low,dir.name_length_high);
+ */
+
+// int l=fread(buf,sizeof(char),dir.name_length_low,stdin);
+ ram_read(ptr,&buf,sizeof(char),dir.name_length_low);
+ ptr+=dir.name_length_low;
+
+ buf[dir.name_length_low]=0;
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"%d %s",dir.inode,buf);
+ ptr+=dir.size-8-dir.name_length_low;
+ pos+=dir.size;
+
+ }
+
+}
+
+/*
+int main()
+{
+ ext2_superblock super;
+ ext2_superblock_ext super_ext;
printf("ext2 fools driver\n");
printf("reading superblock...\n\n");