From: AceVest Date: Mon, 14 Jul 2014 01:52:39 +0000 (+0800) Subject: add ext2_read_file X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=cbd684cd2fcddce94801ab1bf2817c7add9f3830;p=kernel.git add ext2_read_file --- diff --git a/bin/shell.c b/bin/shell.c index 4389260..cde33b0 100644 --- a/bin/shell.c +++ b/bin/shell.c @@ -12,106 +12,15 @@ #include #include #include -#define CMD_SIZE 256 -char cmd[CMD_SIZE]; - -void get_cmd() -{ -#if 0 - int i; - - i=0; - printf("#"); - while(1) - { - int k; - char ch; - extern char ParseKbdInput(int k); - extern unsigned char read_kbd(); -reinput: - k = read_kbd(); - ch = ParseKbdInput(k); - if(ch == -1) - continue; - - if(ch == '\b') - { - i = --i < 0 ? 0 : i; - cmd[i] = 0; - continue; - } - - if(ch == '\n') - { - cmd[i++] = 0; - break; - } - - cmd[i++] = ch; - - if(i == CMD_SIZE - 1) - { - printf("shell buffer is full..." - "reset buffer...\n"); - i = 0; - } - } - - int len = strlen(cmd); - int j; - int flag = 0; - for(j = len - 1; j>=0; j--) { - if(cmd[j] != '\n') { - flag = 1; - break; - } - } - - if(flag == 0) { - i = 0; - goto reinput; - } - - cmd[CMD_SIZE-1] = 0; -#endif -} +int systest(); int main() { - char buf[CMD_SIZE]; - while(1) { - get_cmd(); - - //printf("\nCMD: %s\n", cmd); - - if(cmd[0] == 0) continue; - if(cmd[0] != '/') - { - strcpy(buf, cmd); - strcpy(cmd, "/bin/"); - strcat(cmd, buf); - } - - pid_t pid; - pid = fork(); - - if(pid<0) - { - printf("shit happens in shell\n"); - while(1); - } - else if(pid == 0) - { - execv(cmd, NULL); - } - - int i = 100000; - while(i--); + systest(); + asm("nop;nop;nop;"); } - - return 0; } diff --git a/fs/ext2.c b/fs/ext2.c index c6744e2..25b09ac 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -75,16 +75,37 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) ext2_free_block(blk); } -unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode, unsigned int io) +void ext2_read_file(const ext2_inode_t *inode, char *buf) +{ + assert(inode != 0); + assert(inode->i_size > 0 && inode->i_size<=MAX_SUPT_FILE_SIZE); + + unsigned int blkcnt = inode->i_size / EXT2_BLOCK_SIZE; + int i; + for(i=0; ii_block[i], 1, buf+i*EXT2_BLOCK_SIZE); + printk("read block\n"); + } + + unsigned int left = inode->i_size % EXT2_BLOCK_SIZE; + if(left) + { + void *blk = ext2_alloc_block(); + + memcpy(buf+i*EXT2_BLOCK_SIZE, blk, left); + + ext2_free_block(blk); + } +} + +unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode) { unsigned int ino = 0; void *blk = ext2_alloc_block(); assert(blk != 0); - - printk("ext2_search_indir inode no %u search name %s block %u\n", io, name, inode->i_block[0]); - BLKRW(inode->i_block[0], 1, blk); // only support the first direct blocks ext2_dirent_t *dirent = (ext2_dirent_t *) blk; @@ -146,7 +167,7 @@ unsigned int ext2_search_inpath(const char *path) while((len=get_filename_from_path(path, file)) != 0) { printk("name len %u\n", len); - ino = ext2_search_indir(file, inode, ino); + ino = ext2_search_indir(file, inode); assert(ino != 0); printk("FILE:%s inode %u\n", file, ino); @@ -214,7 +235,7 @@ void ext2_setup_fs() } ext2_read_inode(2, &ext2_root_inode); -#if 0 +#if 1 printk("root inode.i_size %u \n", ext2_root_inode.i_size); printk("root blocks %u \n", ext2_root_inode.i_blocks); @@ -238,3 +259,8 @@ void setup_fs() { ext2_setup_fs(); } + +unsigned int namei(const char *path) +{ + return ext2_search_inpath(path); +} diff --git a/include/ext2.h b/include/ext2.h index 7583166..fa487fb 100644 --- a/include/ext2.h +++ b/include/ext2.h @@ -176,10 +176,10 @@ typedef struct ext2_inode #define EXT2_NAME_LEN 255 typedef struct ext2_dir_ent { - u32 inode; - u16 rec_len; - u8 name_len; - u8 file_type; /* 目录类型 */ + u32 inode; + u16 rec_len; + u8 name_len; + u8 file_type; char name[EXT2_NAME_LEN]; } ext2_dirent_t; @@ -207,5 +207,7 @@ enum #define EXT2_MAX_REC_LEN ((1<<16)-1) +void ext2_read_inode(unsigned int ino, ext2_inode_t *inode); +void ext2_read_file(const ext2_inode_t *inode, char *buf); #endif //_EXT2_H diff --git a/include/fs.h b/include/fs.h index 1361a51..43337f2 100644 --- a/include/fs.h +++ b/include/fs.h @@ -39,12 +39,14 @@ -#define MAX_SUPT_FILE_SIZE (1) +//#define MAX_SUPT_FILE_SIZE (1) #define NR_FILES (1) #define NR_OPENS (1) -#if 0 +unsigned int namei(const char *path); + #define MAX_SUPT_FILE_SIZE (EXT2_IND_BLOCK*EXT2_BLOCK_SIZE) +#if 0 #define NR_FILES (PAGE_SIZE/sizeof(File)) #define NR_INODES (2*NR_FILES) #define NR_OPENS (2) /* 一个进程同时打开文件的限制数 */ diff --git a/include/printk.h b/include/printk.h index db25352..9527a9f 100644 --- a/include/printk.h +++ b/include/printk.h @@ -37,6 +37,7 @@ enum { MPL_ROOT, MPL_TASK_1, MPL_TASK_2, + MPL_TEST, MPL_END }; diff --git a/kernel/exec.c b/kernel/exec.c index 294e544..ae677cc 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -16,53 +16,43 @@ #include #include #include -#include +#include #include +#include +#include int sysc_exec(const char *path, char *const argv[]) { assert(argv == NULL); // unsupport now - int fd; - int i; + unsigned int ino = namei(path); + if(ino == 0) + return -ENOENT; - fd = sysc_open(path, O_RDONLY, 0); - if(fd == -1) - { - panic("can not find file"); - } + ext2_inode_t inode; - int filesz; - Stat stat; - char *buf; + ext2_read_inode(ino, &inode); - sysc_stat(fd, &stat); - filesz = stat.st_size; - if(stat.st_size <=0 || stat.st_size>MAX_SUPT_FILE_SIZE) - { - printk("file %s is not exist\n", path); - return -ENOENT; - } - buf = (void*)kmalloc_old(filesz); - sysc_read(fd, buf, filesz); + //void *buf = (void*)kmalloc(inode.i_size, 0); + void *buf = (void *) alloc_pages(0, 5); + assert(buf != 0); + + ext2_read_file(&inode, buf); -#if 0 - for(i=0; ie_ident, sizeof(ELFMAG)-1) == 0); if(strncmp(ELFMAG, ehdr->e_ident, sizeof(ELFMAG)-1) != 0) { printk("file %s can not execute\n", path); - kfree_old(buf); + kfree(buf); return -ENOEXEC; } //printk("Entry: %08x phnum:%d\n", ehdr->e_entry, ehdr->e_phnum); int size = 0; char *pv = NULL; // phdr 中第一个的VirtAddr + int i; for(i=0; ie_phnum; i++) { pElf32_Phdr phdr; @@ -76,7 +66,7 @@ int sysc_exec(const char *path, char *const argv[]) } } - char *exe = (char *) kmalloc_old(size); + char *exe = (char *) kmalloc(size, 0); for(i=0; ie_phnum; i++) { pElf32_Phdr phdr; @@ -123,16 +113,17 @@ int sysc_exec(const char *path, char *const argv[]) * 即12K~48K之间 * 所以就以一个页目录项来简化处理 */ - u32 *pd = (u32*) pa2va(current->cr3); + u32 *pd = (u32*) current->cr3; u32 *pt; u32 pa_exe; u32 npd, npt; - pa_exe = va2pa(exe); - npd = get_npd(ehdr->e_entry); - pt = get_phys_pages(1); + pa_exe = va2pa(exe); + npd = get_npd(ehdr->e_entry); + pt = (u32*)va2pa(alloc_one_page(0)); if(pt == NULL) panic("out of memory"); + //printk("npd: %d pt:%08x\n", npd, pt); memset(pa2va(pt), 0, PAGE_SIZE); pd[npd] = (u32) pt | 7; @@ -160,7 +151,6 @@ int sysc_exec(const char *path, char *const argv[]) //printk("exe : %08x cr3:%08x\n", exe, pd); - /* 准备内核栈的数据并从ret_from_fork返回 */ pt_regs_t * regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1; extern void ret_from_fork_user(); @@ -176,26 +166,13 @@ int sysc_exec(const char *path, char *const argv[]) regs->eip = (unsigned long)ehdr->e_entry; current->esp = (unsigned long) regs; current->eip = (unsigned long)ret_from_fork_user; + *((unsigned long *)regs->esp) = (unsigned long)ehdr->e_entry; -#if 0 /* 写完之后发现貌似不用 */ - /* 准备用户栈数据 */ - /* 先找到用户栈的位置 */ - u32 pde = pd[get_npd(KRNLADDR)-1] & PAGE_MASK; - pt = pa2va(pde); - u32 *stack = (u32*)pa2va(pt[1023]); - stack[1023] = 0x00; - stack[1022] = 0x00; /* ebp */ - stack[1021] = 0x00; /* edx */ - stack[1020] = 0x00; /* ecx */ - printk("stack pt: %08x pde:%08x %08x %08x\n", - pt, pde, pd[get_npd(KRNLADDR)-1]); -#endif - kfree_old(buf); - + //kfree(buf); //printk("eip: %08x \n", regs->eip); - load_cr3(current); + //load_cr3(current); return 0; } diff --git a/kernel/init.c b/kernel/init.c index 141370a..741c6a4 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -27,6 +27,11 @@ void init_task_entry() int cnt = 0; pid_t id = sysc_getpid(); + if(id == 1) + { + int r = sysc_exec("/bin/shell", 0); + } + while(1) { printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); @@ -47,7 +52,7 @@ void kernel_task(void *entry) void root_task_entry() { kernel_task(init_task_entry); - kernel_task(init_task_entry); + //kernel_task(init_task_entry); int cnt = 0; while(1) diff --git a/kernel/syscall.c b/kernel/syscall.c index 70bb508..21570e7 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -48,7 +48,11 @@ int sysc_pause() return 0; } -int sysc_test() { } +int sysc_test() +{ + static unsigned int cnt; + printl(MPL_TEST, "sysc_test %u", cnt++); +} void init_sysc_handler_table() { diff --git a/lib/exec.c b/lib/exec.c index 208352c..d693fe1 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -15,3 +15,8 @@ int execv(const char *path, char *const argv[]) { return syscall2(SYSC_EXEC, path, argv); } + +int systest() +{ + return syscall0(SYSC_TEST); +}