From: AceVest Date: Tue, 8 Jul 2014 15:54:15 +0000 (+0800) Subject: read ext2 super block X-Git-Url: http://zhaoyanbai.com/repos/%22http:/static/doc/zpipe.c?a=commitdiff_plain;h=a32adfac2f23c19407ff25527b84eea6425a7525;p=kernel.git read ext2 super block --- diff --git a/Makefile b/Makefile index a698a4d..b7b3542 100644 --- a/Makefile +++ b/Makefile @@ -37,5 +37,5 @@ install: md5sum /boot/KERNEL.BIN md5sum KERNEL.BIN -copy: +cp: ./scripts/copy.sh diff --git a/boot/cmdline.c b/boot/cmdline.c index b37eaaf..2476d30 100644 --- a/boot/cmdline.c +++ b/boot/cmdline.c @@ -41,6 +41,6 @@ void parse_cmdline(const char *cmdline) get_value("root", value); printk("root device %s\n", value); assert(value[0]=='h' && value[1]=='d' && value[2] == 'a'); - system.root_dev = MAKE_DEV(DEV_MAJOR_HD, atoi(value+3)); + system.root_dev = MAKE_DEV(DEV_MAJOR_HDA, atoi(value+3)); printk("root device %08x\n", system.root_dev); } diff --git a/drivers/blk_rw.c b/drivers/blk_rw.c new file mode 100644 index 0000000..2b7865f --- /dev/null +++ b/drivers/blk_rw.c @@ -0,0 +1,33 @@ +/* + * ------------------------------------------------------------------------ + * File Name: blk_rw.c + * Author: Zhao Yanbai + * Tue Jul 8 22:21:37 2014 + * Description: none + * ------------------------------------------------------------------------ + */ + +#include +#include +#include + +// only support read +void blk_rw(dev_t dev, u64_t offset, u32_t size, char *buf) +{ + assert(DEV_MAJOR(dev) == DEV_MAJOR_HDA); + assert(offset % SECT_SIZE == 0); + assert(size % SECT_SIZE == 0); + + const part_t *p = ide_get_part(dev); + + u64_t lba = p->lba_start; + lba += offset / SECT_SIZE; + + assert(lba < p->lba_end); + + u32_t scnt = size / SECT_SIZE; + + printk("%s lba %u scnt %u\n", __func__, (u32_t)lba, scnt); + + ide_do_read(lba, scnt, buf); +} diff --git a/drivers/ide.c b/drivers/ide.c index 56f8902..1e4be54 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -50,6 +49,7 @@ typedef struct prd typedef struct { u64_t lba; u32_t scnt; + u32_t read_scnt; char *buf; bool finish; wait_queue_head_t wait; @@ -80,6 +80,7 @@ void ide_printl() void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) { + printk("sect_cnt %u sect_nr %u \n", sect_cnt, (u32)sect_nr); drv.pio_cnt++; drv.read_mode = cmd; @@ -101,6 +102,14 @@ void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) outb(cmd, REG_CMD(dev)); } +part_t *ide_get_part(dev_t dev) +{ + assert(DEV_MAJOR(dev) == DEV_MAJOR_HDA); + assert(DEV_MINOR(dev) < MAX_SUPPORT_PARTITION_CNT); + + return drv.part + DEV_MINOR(dev); +} + void ide_do_read(u64_t lba, u32_t scnt, char *buf) { bool finish = false; @@ -112,6 +121,7 @@ void ide_do_read(u64_t lba, u32_t scnt, char *buf) r->lba = lba; r->scnt= scnt; + r->read_scnt= 0; r->buf = buf; r->finish = false; init_wait_queue(&r->wait); @@ -220,6 +230,7 @@ void init_pci_controller(unsigned int classcode) void ide_default_intr() { + printk("%s\n", __func__); u8_t status = inb(REG_STATUS(0)); drv.irq_cnt++; @@ -237,7 +248,8 @@ void ide_default_intr() u16_t sig = 0; if(drv.read_mode == HD_CMD_READ_EXT) { - insl(REG_DATA(0), ide_request.buf, ((ide_request.scnt*SECT_SIZE)>>2)); + insl(REG_DATA(0), ide_request.buf + ide_request.read_scnt*(SECT_SIZE), (SECT_SIZE)>>2); + ide_request.read_scnt++; sig = *((u16_t *) (ide_request.buf+510)); } @@ -254,7 +266,11 @@ void ide_default_intr() outb(PCI_IDE_CMD_STOP, drv.bus_cmd); wake_up(&ide_request.wait); - ide_request.finish = true; + if(drv.read_mode == HD_CMD_READ_EXT) + { + if(ide_request.read_scnt == ide_request.scnt) + ide_request.finish = true; + } up(&mutex); } diff --git a/drivers/ide.h b/drivers/ide.h index 9f66dd9..fecae71 100644 --- a/drivers/ide.h +++ b/drivers/ide.h @@ -9,6 +9,8 @@ #pragma once +#include + extern unsigned int HD_CHL0_CMD_BASE; extern unsigned int HD_CHL1_CMD_BASE; extern unsigned int HD_CHL0_CTL_BASE; @@ -126,3 +128,6 @@ typedef struct { u64_t lba_start; u64_t lba_end; } part_t; + +void ide_do_read(u64_t lba, u32_t scnt, char *buf); +part_t *ide_get_part(dev_t dev); diff --git a/fs/bak.ext2.c b/fs/bak.ext2.c index bc0df52..11f7641 100644 --- a/fs/bak.ext2.c +++ b/fs/bak.ext2.c @@ -10,6 +10,7 @@ *-------------------------------------------------------------------------- */ +#if 0 #include #include #include @@ -315,3 +316,4 @@ static void ext2_print_inode(pInode p) printk("i_mode:%04x i_size:%d i_blocks:%d\n", p->i_mode, p->i_size, p->i_blocks); } +#endif diff --git a/fs/ext2.c b/fs/ext2.c index b815ed7..ce08dec 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -6,4 +6,50 @@ * Description: none * ------------------------------------------------------------------------ */ +#include "system.h" +#include "fs.h" #include "ext2.h" + +struct { + ext2_sb_t ext2_sb; +} ext2_fs; + +extern void blk_rw(dev_t dev, u64_t offset, u32_t scnt, char *buf); + +#define BLKRW(offset, blkcnt, buf) do { blk_rw(system.root_dev, offset, (blkcnt)*EXT2_BLOCK_SIZE, buf); } while(0) + +void ext2_setup_fs() +{ + memset(&ext2_fs, 0, sizeof(ext2_fs)); + + char *buf = kmalloc(EXT2_BLOCK_SIZE, 0); + if(buf == 0) + panic("out of memory"); + + printk("EXT2_BLOCK_SIZE %u\n", EXT2_BLOCK_SIZE); + + BLKRW(EXT2_SB_OFFSET, 1, buf); + + memcpy(EXT2_SB, buf, sizeof(*(EXT2_SB))); + + if(EXT2_SB->s_magic != EXT2_SUPER_MAGIC) + { + printk("file system magic %04x\n", EXT2_SB->s_magic); + panic("only support ext2 file system..."); + } + + printk("Ext2 File System Information:\n"); + printk("inodes cnt %u blocks cnt %u free blocks %u free inodes %u\n", + EXT2_SB->s_inodes_count, EXT2_SB->s_blocks_count, EXT2_SB->s_free_blocks_count, EXT2_SB->s_free_inodes_count); + printk("block size %u log block size %u first data block %u\n", + EXT2_BLOCK_SIZE, EXT2_SB->s_log_block_size, EXT2_SB->s_first_data_block); + printk("blocks per group %u inodes per group %u\n", EXT2_SB->s_blocks_per_group, EXT2_SB->s_inodes_per_group); +} + + + + +void setup_fs() +{ + ext2_setup_fs(); +} diff --git a/fs/fs.c b/fs/fs.c index c98d4ad..5b03ed2 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -15,13 +15,13 @@ #include #define SECT_SIZE 512 +#if 0 File file_table[NR_FILES] __attribute__ ((__aligned__(PAGE_SIZE))) = {{0,},}; extern unsigned int ext2_start_sect; void hd_read(dev_t dev, u64 sect_nr, void *buf, u32 count); void init_file_table(); void save_boot_part(int n, pPartition p, u32 base_sect); void read_ext_part(u32 base, u32 offset); - void setup_fs() { int i, minor; @@ -122,3 +122,4 @@ void read_ext_part(u32 base, u32 offset) free_virt_pages(buf); } +#endif diff --git a/fs/open.c b/fs/open.c index fb2c192..73fb164 100644 --- a/fs/open.c +++ b/fs/open.c @@ -20,6 +20,7 @@ int sysc_open(const char *path, int flags, mode_t mode) { +#if 0 assert(mode == 0); // unsupport now... assert(flags == O_RDONLY); // support only... @@ -90,4 +91,5 @@ int sysc_open(const char *path, int flags, mode_t mode) current->fps[fd] = pf; return fd; +#endif } diff --git a/fs/read.c b/fs/read.c index 3e2fc06..21db990 100644 --- a/fs/read.c +++ b/fs/read.c @@ -17,6 +17,7 @@ int sysc_read(int fd, void *buf, size_t count) { +#if 0 if(fd<0 || fd>=NR_OPENS) return -EBADF; @@ -30,4 +31,5 @@ int sysc_read(int fd, void *buf, size_t count) return read_file(inode, buf, count); +#endif } diff --git a/fs/stat.c b/fs/stat.c index be4af97..ce412d1 100644 --- a/fs/stat.c +++ b/fs/stat.c @@ -17,6 +17,7 @@ #include int sysc_stat(int fd, struct stat *stat) { +#if 0 if(fd<0 || fd>=NR_OPENS) return -EBADF; @@ -39,6 +40,7 @@ int sysc_stat(int fd, struct stat *stat) stat->st_atime = inode->i_atime; stat->st_mtime = inode->i_mtime; stat->st_ctime = inode->i_ctime; +#endif return 0; } diff --git a/include/ext2.h b/include/ext2.h index befc6b2..4a4d525 100644 --- a/include/ext2.h +++ b/include/ext2.h @@ -19,6 +19,10 @@ #include +#define EXT2_SUPER_MAGIC 0xEF53 + +#define EXT2_SB_OFFSET 1024 + #define EXT2_BAD_INO 1 #define EXT2_ROOT_INO 2 #define EXT2_BOOT_LOADER_INO 5 @@ -28,6 +32,7 @@ #define EXT2_MAX_BLOCK_SIZE 4096 #define EXT2_MIN_BLOCK_LOG_SIZE 10 +#define EXT2_SB (&ext2_fs.ext2_sb) #define EXT2_BLOCK_SIZE (EXT2_MIN_BLOCK_SIZE << (EXT2_SB)->s_log_block_size) #define EXT2_SECT_PER_BLOCK (EXT2_BLOCK_SIZE/512) diff --git a/include/fs.h b/include/fs.h index fb4676c..1361a51 100644 --- a/include/fs.h +++ b/include/fs.h @@ -15,20 +15,6 @@ #include #include -#include - -typedef struct partition -{ - u8 State; - u8 Head; - u16 StartSC; - u8 Type; - u8 EndHead; - u16 EndSC; - u32 AbsoluteSectNo; - u32 PartitionSize; -} Partition, *pPartition; - /* 分区表开始的位置 */ #define PARTS_POS 0x1BE @@ -38,7 +24,7 @@ typedef struct partition #define DEV_MAJOR_MEM 0x0001 #define DEV_MAJOR_TTY 0x0002 #define DEV_MAJOR_IDE0 0x0003 -#define DEV_MAJOR_HD DEV_MAJOR_IDE0 +#define DEV_MAJOR_HDA DEV_MAJOR_IDE0 #define DEV_MAJOR_IDE1 0x0004 #define DEV_MAJOR_SCSI0 0x0005 #define DEV_MAJOR_SCSI2 0x0006 @@ -53,13 +39,11 @@ typedef struct partition -typedef struct -{ - int count; - int ino_nr; - pInode inode; -} File, *pFile; +#define MAX_SUPT_FILE_SIZE (1) +#define NR_FILES (1) +#define NR_OPENS (1) +#if 0 #define MAX_SUPT_FILE_SIZE (EXT2_IND_BLOCK*EXT2_BLOCK_SIZE) #define NR_FILES (PAGE_SIZE/sizeof(File)) #define NR_INODES (2*NR_FILES) @@ -68,6 +52,12 @@ extern File file_table[NR_FILES]; extern Inode inode_table[NR_INODES]; +typedef struct +{ + int count; + int ino_nr; + pInode inode; +} File, *pFile; static inline int get_inode_nr(const char *path) @@ -103,5 +93,6 @@ static inline pInode find_empty_inode() return NULL; } +#endif #endif //_FS_H diff --git a/include/task.h b/include/task.h index 3873c0e..13351a9 100644 --- a/include/task.h +++ b/include/task.h @@ -61,7 +61,7 @@ typedef union task_union list_head_t list; - pFile fps[NR_OPENS]; + //pFile fps[NR_OPENS]; }; diff --git a/kernel/sched.c b/kernel/sched.c index fd9d475..ee480da 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -64,8 +64,10 @@ void init_root_tsk() strcpy(root_task.name, "root_task"); INIT_LIST_HEAD(&root_task.list); - for(i=0; i> 24); - printk("root device: %08x\n", system.root_dev); -} - int sysc_reboot(int mode) { diff --git a/kernel/test.c b/kernel/test.c deleted file mode 100644 index 45f467f..0000000 --- a/kernel/test.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - *-------------------------------------------------------------------------- - * File Name: test.c - * - * Author: Zhao Yanbai [zhaoyanbai@126.com] - * Tue Feb 23 17:47:43 2010 - * - * Description: sysc_test - * - *-------------------------------------------------------------------------- - */ -#include -#include -#include - -void dump_fd() -{ - int i; - for(i=0; ifps[i]; - if(pf == NULL) continue; - printk("fd[%d]: %08x\n", i, pf); - - - pInode inode = pf->inode; - printk("file_desc ino_nr: %04d inode:%08x\n", - pf->ino_nr, inode); - - printk("inode: size: %d\n", inode->i_size); - } -} - -int sysc_test() -{ - //dump_fd(); - - printk("."); - - return 0; -}