#include "kernel.h" #include "fd.h" #include "fifo.h" #include "kmalloc.h" #include "ext2.h" #include "log.h" #include "lib/string/string.h" #include "lib/printf/printf.h" #include "ringbuffer.h" bool fd_write(fd* f,uint8_t c) { if(!f->write)kpanic("no write func") return f->write(f->data,c); } uint8_t fd_read(fd* f) { if(!f->read)kpanic("no read func") return f->read(f->data); } bool fd_has(fd* f) { if(!f->has)kpanic("no has func") return f->has(f->data); } bool fd_can_write(fd* f) { if(!f->can_write)kpanic("no has func") return f->can_write(f->data); } bool fd_eof(fd* f) { if(!f->eof)kpanic("no eof func") return f->eof(f->data); } bool fd_close(fd* f) { if(!f->close)kpanic("no close func") return f->close(f->data); } fd fd_dupl(fd* f) { if(!f->dupl)kpanic("no dupl func") return f->dupl(f); } static fd default_dupl(fd *f) { return *f; } void default_close(void* x) { // nothin } //---- void* ext2_init(char *path) { uint32_t *data=kballoc(1); uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path); data[0]=inode;// inode data[1]=0; //pos return data; } void ext2_write(void* x,uint8_t b) { kpanic("not impl"); } uint8_t ext2_read(uint32_t* data) { char c; ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1); return c; } bool ext2_has(uint32_t* data) { return 1; } bool ext2_eof(uint32_t* data) { uint32_t save=data[1]; uint32_t c=ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1); data[1]=save; return (c==0); } void ext2_close(void* x) { kbfree(x); } uint8_t sysfs_get(uint32_t *data) { return ringbuffer_get(data); } void sysfs_write(void* x,uint8_t b) { uint32_t **pp=x+sizeof(ringbuffer); void (*p)(uint32_t v)=*pp; uint32_t *val=x+sizeof(ringbuffer)+4; val[val[0]]=b; if(val[0]==4) // 4bytes / 32bit { val[0]=1; p(val[1]+256*val[2]+256*256*val[3]+256*256*256*val[4]); } val[0]+=1; } bool sysfs_has(uint32_t *data) { return true; } bool sysfs_eof(uint32_t *data) { return !ringbuffer_has(data); } void sysfs_close(uint32_t *data) { ringbuffer_free(data); // free ringbuffer buffer kbfree(data); // free memory holding ringbuffer information } void pipe_r_close(uint8_t *data) { uint8_t *mem=data+sizeof(ringbuffer); *mem-=1; klog("closing read end of pipe at 0x%08x, ref count=%d",mem,*mem); /* if(*mem==0) // TODO : check other end too! and close on w_close { ringbuffer *r=data; ringbuffer_free(data); // free ringbuffer buffer kbfree(data); // free memory holding ringbuffer information } */ } void pipe_w_close(uint8_t *data) { uint8_t *mem=data+sizeof(ringbuffer); mem+=4; *mem-=1; klog("closing write end of pipe at 0x%08x, ref count=%d",mem,*mem); } static fd pipe_r_dupl(fd *f) { uint8_t *mem=f->data+sizeof(ringbuffer); *mem+=1; klog("duplicating read end of pipe at 0x%08x, ref count=%d",mem,*mem); return *f; } static fd pipe_w_dupl(fd *f) { uint8_t *mem=f->data+sizeof(ringbuffer); mem+=4; *mem+=1; klog("duplicating write end of pipe at 0x%08x, ref count=%d",mem,*mem); return *f; } void get_sysfs_data(ringbuffer *r,char *format_string, ...) { char buf[256]; va_list va; va_start(va,format_string); tfp_vsprintf(buf,format_string,va); va_end(va); for(int i=0;ifull(f->data)); } fd fd_from_fifo(fifo* fif) { fd f; f.type=FD_TYPE_FIFO; f.data=fif; f.read=fifo_get; f.write=fifo_put; f.eof=fifo_eof; f.has=fifo_has; f.dupl=default_dupl; f.close=default_close; f.can_write=fifo_can_write; return f; }