diff --git a/fs/Makefile b/fs/Makefile index bf064136bc5b906185553672c77e633ed12cccc9..1fc1c1f352dca7686d66fd67535f4f9c322b280f 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -3,7 +3,7 @@ LD := ld CCFLAG := -I../include -nostdinc -ffreestanding -Wall -fomit-frame-pointer -fno-stack-protector -fno-pic -c -m32 LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include -OBJS := read_write.o buffer.o super.o open.o file_table.o inode.o namei.o +OBJS := read_write.o buffer.o super.o open.o file_table.o inode.o namei.o fcntl.o char_dev.o fs.o : $(OBJS) $(LD) -m elf_i386 -r -o $@ $^ @@ -29,6 +29,12 @@ inode.o : inode.c namei.o : namei.c $(GCC) $(CCFLAG) -o $@ $< +fcntl.o : fcntl.c + $(GCC) $(CCFLAG) -o $@ $< + +char_dev.o : char_dev.c + $(GCC) $(CCFLAG) -o $@ $< + clean : rm *.o diff --git a/fs/char_dev.c b/fs/char_dev.c new file mode 100644 index 0000000000000000000000000000000000000000..3335e27fad5c03ca3222f7b93954366d0a1d2168 --- /dev/null +++ b/fs/char_dev.c @@ -0,0 +1,53 @@ +#include +#include + +#include +#include + +#include +#include + +//extern int tty_read(unsigned minor,char * buf,int count); +extern int tty_write(unsigned minor,char * buf,int count); + +typedef (*crw_ptr)(int rw,unsigned minor,char * buf,int count,off_t * pos); + +int rw_ttyx(int rw,unsigned minor,char * buf,int count,off_t * pos) { + if (rw == WRITE) { + return tty_write(minor, buf, count); + } + else if (rw == READ) { + return -EINVAL; + } + else + return -EINVAL; +} + +static int rw_tty(int rw,unsigned minor,char * buf,int count, off_t * pos) { + if (current->tty<0) + return -EPERM; + return rw_ttyx(rw,current->tty,buf,count,pos); +} + +#define NRDEVS ((sizeof (crw_table))/(sizeof (crw_ptr))) + +static crw_ptr crw_table[]={ + NULL, + NULL, + NULL, + NULL, + rw_ttyx, + rw_tty, + NULL, + NULL, +}; + +int rw_char(int rw,int dev, char * buf, int count, off_t * pos) { + crw_ptr call_addr; + if (MAJOR(dev)>=NRDEVS) + return -ENODEV; + if (!(call_addr=crw_table[MAJOR(dev)])) + return -ENODEV; + return call_addr(rw,MINOR(dev),buf,count,pos); +} + diff --git a/fs/fcntl.c b/fs/fcntl.c new file mode 100644 index 0000000000000000000000000000000000000000..b7eb2d0dc0d8390d342e6abeb795461ba0790719 --- /dev/null +++ b/fs/fcntl.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +#include +#include + +static int dupfd(unsigned int fd, unsigned int arg) { + if (fd >= NR_OPEN || !current->filp[fd]) + return -EBADF; + if (arg >= NR_OPEN) + return -EINVAL; + + while (arg < NR_OPEN) { + if (current->filp[arg]) + arg++; + else + break; + } + + if (arg >= NR_OPEN) + return -EMFILE; + + current->close_on_exec &= ~(1<filp[arg] = current->filp[fd])->f_count++; + return arg; +} + +int sys_dup(unsigned int fildes) { + return dupfd(fildes, 0); +} + diff --git a/fs/read_write.c b/fs/read_write.c index bfe0317bb6d139a4c5fd472b4107c1e9aa77432b..9e62a67250a69d6fe05fb092939e3129313a02bb 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -5,13 +5,28 @@ #include #include -extern int tty_write(int channel, const char* buf, int count); +extern int rw_char(int rw,int dev, char * buf, int count, off_t * pos); int sys_write(unsigned int fd,char * buf,int count) { - if (fd == 1) { - return tty_write(0, buf, count); + struct file * file; + struct m_inode * inode; + int i; + + if (fd>=NR_OPEN || count <0 || !(file=current->filp[fd])) + return -EINVAL; + + if (!count) + return 0; + + inode=file->f_inode; + + if (S_ISCHR(inode->i_mode)) { + printk("dev is %d\n", inode->i_zone[0]); + return rw_char(WRITE,inode->i_zone[0],buf,count,&file->f_pos); } - return 0; + printk("(Write)inode->i_mode=%06o\n\r",inode->i_mode); + + return -EINVAL; } diff --git a/include/linux/fs.h b/include/linux/fs.h index 8d045395faa62df5fbaad0e88bff6685aa474c13..7a3876a924c5fd8efb3ecbf0dc89ed5771d5cd33 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -139,6 +139,9 @@ extern void ll_rw_block(int rw, struct buffer_head * bh); extern void brelse(struct buffer_head * buf); extern struct buffer_head * bread(int dev,int block); +extern int open_namei(const char * pathname, int flag, int mode, + struct m_inode ** res_inode); + extern int ROOT_DEV; void mount_root(); diff --git a/include/linux/sched.h b/include/linux/sched.h index 217d5cb889e0c1f399b1440917ece01f4337e0ae..3784cd0dc2ffa4c1ee3dbdae21f6df4d63cf352c 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -100,7 +100,7 @@ struct task_struct { 15, \ 0, \ &init_task.task, 0, 0, 0,\ - -1,0022,NULL,NULL,NULL,NULL,0, \ + 0,0022,NULL,NULL,NULL,NULL,0, \ {NULL,}, \ { \ {0, 0}, \ diff --git a/include/linux/sys.h b/include/linux/sys.h index ec2fac4e66933b51564c205b1e619c6c46b08698..2bf2d9e31d7189fbbafbc90e94203edc9366a4aa 100644 --- a/include/linux/sys.h +++ b/include/linux/sys.h @@ -39,7 +39,7 @@ extern int sys_open(); //extern int sys_rename(); //extern int sys_mkdir(); //extern int sys_rmdir(); -//extern int sys_dup(); +extern int sys_dup(); //extern int sys_pipe(); //extern int sys_times(); //extern int sys_prof(); @@ -93,48 +93,48 @@ fn_ptr sys_call_table[] = { 0, /*sys_read,*/ sys_write, sys_open, -// sys_close, -// sys_waitpid, -// sys_creat, -// sys_link, -// -// sys_unlink, -// sys_execve, -// sys_chdir, -// sys_time, -// sys_mknod, -// sys_chmod, -// -// sys_chown, -// sys_break, -// sys_stat, -// sys_lseek, -// sys_getpid, -// sys_mount, -// -// sys_umount, -// sys_setuid, -// sys_getuid, -// sys_stime, -// sys_ptrace, -// sys_alarm, -// -// sys_fstat, -// sys_pause, -// sys_utime, -// sys_stty, -// sys_gtty, -// sys_access, -// -// sys_nice, -// sys_ftime, -// sys_sync, -// sys_kill, -// sys_rename, -// sys_mkdir, -// -// sys_rmdir, -// sys_dup, + 0, /*sys_close,*/ + 0, //sys_waitpid, + 0, //sys_creat, + 0, //sys_link, + + 0, //sys_unlink, + 0, //sys_execve, + 0, //sys_chdir, + 0, //sys_time, + 0, //sys_mknod, + 0, //sys_chmod, + + 0, //sys_chown, + 0, //sys_break, + 0, //sys_stat, + 0, //sys_lseek, + 0, //sys_getpid, + 0, //sys_mount, + + 0, //sys_umount, + 0, //sys_setuid, + 0, //sys_getuid, + 0, //sys_stime, + 0, //sys_ptrace, + 0, //sys_alarm, + + 0, //sys_fstat, + 0, //sys_pause, + 0, //sys_utime, + 0, //sys_stty, + 0, //sys_gtty, + 0, //sys_access, + + 0, //sys_nice, + 0, //sys_ftime, + 0, //sys_sync, + 0, //sys_kill, + 0, //sys_rename, + 0, //sys_mkdir, + + 0, //sys_rmdir, + sys_dup, // sys_pipe, // sys_times, // sys_prof, diff --git a/include/unistd.h b/include/unistd.h index 2a51b5f4cbad82e6ba67b37488133f831543f4ca..6418f678c87f3a85a0cceea0acf16f1fcea9ffca 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -137,6 +137,7 @@ __asm__ volatile("int $0x80\n\r"\ extern int errno; +int dup(int fildes); int fork(); int open(const char * filename, int flag, ...); int write(int fildes, const char * buf, off_t count); diff --git a/kernel/blk_drv/floppy.c b/kernel/blk_drv/floppy.c index 947b216aff31c4895d854a3eeaeaecabc04a308d..154e75189dcef9799a48be6a8bab680251cd9a34 100644 --- a/kernel/blk_drv/floppy.c +++ b/kernel/blk_drv/floppy.c @@ -297,8 +297,8 @@ void floppy_on_interrupt() { } void do_fd_request() { - printk("hinus debug: do_fd_request, reset: %d, recal: %d, request sect:%d\n", - reset, recalibrate, CURRENT ? CURRENT->sector : 0); + //printk("hinus debug: do_fd_request, reset: %d, recal: %d, request sect:%d\n", + // reset, recalibrate, CURRENT ? CURRENT->sector : 0); unsigned int block; seek = 0; diff --git a/kernel/blk_drv/hd.c b/kernel/blk_drv/hd.c index cd9b9e7068084305e955af125bc5ba7f6ba2e818..8a0c2bed7f229435221e74e29b4abd9714d2a3cf 100644 --- a/kernel/blk_drv/hd.c +++ b/kernel/blk_drv/hd.c @@ -137,7 +137,7 @@ int sys_setup(void * BIOS) { for (i = 0; i < NR_HD; i++) { - printf("sectors for hd %d is %d\n", i, hd[i].nr_sects); + printk("sectors for hd %d is %d\n", i, hd[i].nr_sects); } //hd_identity(); diff --git a/kernel/main.c b/kernel/main.c index b233fab74871516ba87a09d9dae4bd075fac9e3d..fdadb1cc8141532354d0f9ad3dea365467164a53 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -11,13 +11,17 @@ int errno; #include #include -#include #include +#include +#include + +#include +#include #include #include -static char printbuf[1024]; +char printbuf[1024]; int printf(const char* fmt, ...); @@ -99,6 +103,10 @@ int printf(const char* fmt, ...) { void init() { setup((void *) &drive_info); - (void)open("/dev/tty1", O_RDWR, 0); + (void)open("/dev/tty0", O_RDWR, 0); + (void) dup(0); + (void) dup(0); + + printf("Free mem: %d bytes\n\r",memory_end-main_memory_start); } diff --git a/lib/Makefile b/lib/Makefile index 9b97393dac9ff5541fe7817fedc7c7a4a604a484..60cd88d4cf878ca538e622d2d71ae94335d1f663 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,7 +2,7 @@ AR := ar LD := ld GCC := gcc CCFLAG := -m32 -I../include -nostdinc -ffreestanding -fno-pic -Wall -fomit-frame-pointer -fno-stack-protector -c -OBJS := ctype.o write.o string.o open.o +OBJS := ctype.o write.o string.o open.o dup.o lib.a : $(OBJS) $(AR) rcs $@ $^ @@ -20,6 +20,9 @@ string.o : string.c open.o : open.c $(GCC) $(CCFLAG) -o $@ $< +dup.o : dup.c + $(GCC) $(CCFLAG) -o $@ $< + clean : rm *.o rm lib.a diff --git a/lib/dup.c b/lib/dup.c new file mode 100644 index 0000000000000000000000000000000000000000..a6e54742ced5e7f9fafa93584b33b325a168ec57 --- /dev/null +++ b/lib/dup.c @@ -0,0 +1,6 @@ +#define __LIBRARY__ + +#include + +_syscall1(int,dup,int,fd) +