From 1e08b64b43bf9c50b644da3f76d5a8bcc73f62da Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 19 Sep 2018 01:52:14 +0200 Subject: addding sysfs and pipes etc --- fs/ext2.c | 3 +++ fs/fd.c | 59 ++++++++++++++++++++++++++++++++++++++++++++----- fs/fd.h | 22 ++++-------------- fs/mount.c | 8 +++---- fs/pipe.c | 37 +++++++++++++++++++++++++++++++ fs/pipe.h | 1 + fs/sysfs.c | 16 +++++++++----- kernel/interrupts.c | 5 +++-- kernel/kernel.c | 18 +++++++-------- kernel/kernel.h | 1 + kernel/log.h | 8 +++++++ kernel/mem.c | 9 ++++++++ kernel/mem.h | 4 ++++ kernel/syscalls.c | 7 +++++- userspace/cat.c | 17 +++++++++----- userspace/ls.c | 24 +++++++++++--------- userspace/sysfs_write.c | 9 ++++++++ 17 files changed, 186 insertions(+), 62 deletions(-) create mode 100644 fs/pipe.c create mode 100644 fs/pipe.h create mode 100644 userspace/sysfs_write.c diff --git a/fs/ext2.c b/fs/ext2.c index b518bd9..9cc5b34 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -315,6 +315,9 @@ fd ext2_mount_file_open(mount *m,char *path) int ext2_mount_read_dir(mount *m,char *path, fs_dirent *dirs, uint32_t *pos) { uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path); + if(inode==0)return -1; + ext2_inode *inode_current=ext2_get_inode(VMEM_EXT2_RAMIMAGE,inode); + if(!(inode_current->permissions&0x4000))return -1; return ext2_read_dir(m->data, inode, dirs, pos); } diff --git a/fs/fd.c b/fs/fd.c index 43ffb0d..fe34003 100644 --- a/fs/fd.c +++ b/fs/fd.c @@ -40,11 +40,10 @@ void* ext2_init(char *path) 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) +void ext2_write(void* x,uint8_t b) { kpanic("not impl"); } @@ -79,6 +78,21 @@ 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; @@ -90,6 +104,12 @@ bool sysfs_eof(uint32_t *data) } void sysfs_close(uint32_t *data) +{ + ringbuffer_free(data); // free ringbuffer buffer + kbfree(data); // free memory holding ringbuffer information +} + +void pipe_close(uint32_t *data) { ringbuffer *r=data; ringbuffer_free(data); // free ringbuffer buffer @@ -114,6 +134,8 @@ bool fifo_eof(uint32_t* data) { return false; } + +// TODO remove and use fd_from_ringbuffer? fd fd_from_fifo(fifo* fif) { fd f; @@ -127,6 +149,27 @@ fd fd_from_fifo(fifo* fif) return f; } +fd fd_from_pipe() +{ + fd f; + f.type=FD_TYPE_PIPE; + f.data=kballoc(1); + ringbuffer r=ringbuffer_init(1); + + memcpy(f.data,&r,sizeof(ringbuffer)); + + /* + f.read=sysfs_get; + f.write=0; + f.has=sysfs_has; + f.eof=sysfs_eof; + */ + + f.close=pipe_close; + + return f; +} + fd fd_from_path(char* path) { fd f; @@ -140,22 +183,28 @@ fd fd_from_path(char* path) return f; } -fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...))) +fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...)),void (*h)(uint32_t in)) { fd f; f.type=FD_TYPE_SYSFS; f.data=kballoc(1); + + uint32_t **pp=f.data+sizeof(ringbuffer); + *pp=h; + + uint32_t *val=f.data+sizeof(ringbuffer)+4; + val[0]=1; // waiting for first byte of the 32bit value + ringbuffer r=ringbuffer_init(1); g(&r,get_sysfs_data); memcpy(f.data,&r,sizeof(ringbuffer)); f.read=sysfs_get; - f.write=0; + f.write=sysfs_write; f.close=sysfs_close; f.has=sysfs_has; f.eof=sysfs_eof; return f; } - diff --git a/fs/fd.h b/fs/fd.h index 35e28d9..c9b7fd8 100644 --- a/fs/fd.h +++ b/fs/fd.h @@ -9,27 +9,12 @@ #include "fifo.h" #include "ringbuffer.h" -/* -typedef struct -{ - int(* seek)(int offset, int whence); - - int(* read)(char *buf, int len); - int(* wrtie)(char *buf, int len); - - int(* close)(); - int(* stat)(void *buf); - - void *data; //opaque - -}file; -*/ - enum FD_TYPE{ FD_TYPE_FIFO_BUFFERED=1, FD_TYPE_EXT2_FILE=2, FD_TYPE_EXT2_DIR=3, - FD_TYPE_SYSFS=4 + FD_TYPE_SYSFS=4, + FD_TYPE_PIPE=5 }; typedef struct fd_struct @@ -52,5 +37,6 @@ bool fd_close(fd*); fd fd_from_fifo(fifo* f); fd fd_from_path(char *path); -fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...))); +fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...)),void(*h)(uint32_t in)); + #endif diff --git a/fs/mount.c b/fs/mount.c index 009647a..01a90b2 100644 --- a/fs/mount.c +++ b/fs/mount.c @@ -27,7 +27,7 @@ void mount_add(mount mnt) { if(mounts_count==MAX_MOUNTS)kpanic("maxium number of mounts exceeded. increase MAX_MOUNTS in kernel.h and recomplie kernel."); mounts[mounts_count++]=mnt; - klog("mounted %s",mnt.path); + klog("Mounted %s at %s",mount_type_to_str(mnt.type),mnt.path); } void mount_dump() @@ -70,8 +70,9 @@ static char* get_mount_for_path(char *path,mount *mnt) { if(path[0]!='/')kpanic("this works only for absolute paths!"); + // start with root as default uint32_t best=0; - uint32_t best_len=0; + uint32_t best_len=1; for(int i=0;i=count)return 0; + memcpy(dirs->name,names[*pos],strlen(names[*pos])+1); + *pos+=1; + return 1; + */ + return 0; +} + +void pipe_mount(char* path) +{ + mount m; + m.type=MOUNT_TYPE_PIPES; + memcpy(m.path,path,strlen(path)+1); + m.mount_file_open=pipe_file_open; + m.mount_read_dir=pipe_read_dir; + mount_add(m); +} diff --git a/fs/pipe.h b/fs/pipe.h new file mode 100644 index 0000000..49f7f13 --- /dev/null +++ b/fs/pipe.h @@ -0,0 +1 @@ +void pipe_mount(char* path); diff --git a/fs/sysfs.c b/fs/sysfs.c index 57a56c8..de679f2 100644 --- a/fs/sysfs.c +++ b/fs/sysfs.c @@ -1,6 +1,6 @@ #include "mount.h" +#include #include "sysfs.h" - #include "mem.h" #include "kmalloc.h" #include "mount.h" @@ -9,10 +9,13 @@ #include "lib/string/string.h" -static const char* names[] = {"/mem","/kmalloc","/mount"}; -static uint32_t map[]={mem_sysfs,kmalloc_sysfs,mount_sysfs}; -static uint32_t count=3; +static const char* names[] = {"mem","kmalloc","mount"}; +static uint32_t map[]={mem_sysfs,mem_sysfs_set, + kmalloc_sysfs,NULL, + mount_sysfs,NULL, + }; +static uint32_t count=3; /* mount interface */ @@ -22,15 +25,16 @@ fd sysfs_file_open(mount *m,char *path) for (int i=0;i=count)return 0; memcpy(dirs->name,names[*pos],strlen(names[*pos])+1); + dirs->inode=0; *pos+=1; return 1; } diff --git a/kernel/interrupts.c b/kernel/interrupts.c index 94c1bb1..94eda2e 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -106,11 +106,12 @@ uint32_t interrupt_handler(uint32_t esp, uint32_t irq) */ void interrupts_init(uint16_t sel) { - klog("initializing Mouse and Kb input buffers"); + klog("Initializing Mouse and Kb input buffers"); + fixme("use a regular pipe-file"); kb_in=ringbuffer_init(1);// 4096 bytes ringbuffer; mouse_in=ringbuffer_init(1);// 4096 bytes ringbuffer; - klog("initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd); + klog("Initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd); // Default interrupt handling for(int i=0; iframebuffer_type==2) // EGA-standard text mode diff --git a/kernel/kernel.h b/kernel/kernel.h index e3d7590..5f21bc6 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -25,6 +25,7 @@ REFERENCES //#define FOOLOS_UNIT_TESTING // Run Unit Tests //#define FOOLOS_LOG_OFF // Turn off logging //#define FOOLOS_COLORLESS // Turn off colors in log +#define HIDE_FIXME #define MAX_MOUNTS 10 diff --git a/kernel/log.h b/kernel/log.h index 74f8776..3a8c746 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -34,7 +34,13 @@ void log(bool color,char *module_name, int prio, char *format_string, ...); #define klog(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__), 10, LOG_LABEL_INFO __VA_ARGS__) #define kpanic(...) {log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__) ,0, LOG_LABEL_PANIC __VA_ARGS__ ); while(1);} + +#ifdef HIDE_FIXME +#define fixme(...) {} +#else #define fixme(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__) , 10, LOG_LABEL_FIX __VA_ARGS__) +#endif + #define testlog(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__) , 10,LOG_LABEL_TEST __VA_ARGS__) #endif @@ -46,4 +52,6 @@ void log(bool color,char *module_name, int prio, char *format_string, ...); #define fixme(...) {} #endif + + #endif diff --git a/kernel/mem.c b/kernel/mem.c index 97450d4..d8ac66a 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -15,6 +15,9 @@ extern uint32_t kernel_end[]; extern uint32_t stack_top[]; extern uint32_t stack_bottom[]; +// sysfs inpue +uint32_t sysfs_in=128; + //memory map bit array. Each bit represents a 4KB memory block, //so uint32_t represents 8*4 blocks static uint32_t _mmngr_memory_map[PMMNGR_MAP_SIZE]; //128KiB @@ -233,4 +236,10 @@ void mem_sysfs(ringbuffer *r, void (*f)(ringbuffer *r,char *fmt, ...)) f(r,"physical memory manager"); f(r,"free 4096kb blocks : %d",mem_free_blocks); f(r,"free bytes : %d",mem_free_blocks*4096); + f(r,"in value : 0x%08X",sysfs_in); +} + +void mem_sysfs_set(uint32_t set) +{ + sysfs_in=set; } diff --git a/kernel/mem.h b/kernel/mem.h index 5de95a9..6223f0a 100644 --- a/kernel/mem.h +++ b/kernel/mem.h @@ -27,7 +27,11 @@ * This is required for memory map et al. */ uint32_t mem_init(multiboot_information *info); + void* mem_alloc_block (); void mem_free_block(void* p); + uint32_t mem_get_free_blocks_count(); + void mem_sysfs(ringbuffer *r, void (*f)(ringbuffer *r, char *fmt, ...)); +void mem_sysfs_set(uint32_t in); diff --git a/kernel/syscalls.c b/kernel/syscalls.c index aa20616..4f6508a 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -165,7 +165,12 @@ int syscall_read(int file, char *buf, int len) //TODO: replace with dirent! int syscall_readdir(const char *name,fs_dirent *dirs,int *pos) { - return mount_read_dir(name, dirs, pos); + int ret=mount_read_dir(name, dirs, pos); + if(ret==-1) + { + set_errno(ENOENT); + } + return ret; } // for non blocking io? diff --git a/userspace/cat.c b/userspace/cat.c index 5b419ca..d0b018b 100644 --- a/userspace/cat.c +++ b/userspace/cat.c @@ -5,8 +5,17 @@ int main(int argc, char **argv) FILE *f; if(argc>1){ char buf[256]; - sprintf(buf,"%s/%s",getenv("PWD"),argv[1]); - f=fopen(buf,"r"); + if(argv[1][0]!='/') + { + sprintf(buf,"%s/%s",getenv("PWD"),argv[1]); + printf("cat %s\n",buf); + f=fopen(buf,"r"); + } + else + { + printf("cat %s\n",argv[1]); + f=fopen(argv[1],"r"); + } } else f=stdin; @@ -15,14 +24,10 @@ int main(int argc, char **argv) char c; - printf("-- reading from file byte by byte --\n\n"); - while(fread(&c,1,1,f)) { printf("%c",c); } - printf("\n-- no more data on this file --\n"); - return 0; } diff --git a/userspace/ls.c b/userspace/ls.c index 416e139..b3a17ef 100644 --- a/userspace/ls.c +++ b/userspace/ls.c @@ -1,10 +1,5 @@ #include "interface/fs.h" -void usage() -{ - puts("ls [inode_nr]"); -} - int main(int argc, char **argv) { char *dir=getenv("PWD"); @@ -13,8 +8,9 @@ int main(int argc, char **argv) { if(argv[1][0]!='/') { + if(!strcmp(dir,"/"))dir++; char buf[256]; - sprintf(buf,"%s/%s",dir,argv[1]); + sprintf(buf,"%s/%s",dir,argv[1]); dir=buf; } else @@ -23,16 +19,22 @@ int main(int argc, char **argv) } } + printf("listing %s\n",dir); + fs_dirent dirs; uint32_t pos=0; - int cnt=0; while(1) { - cnt =_readdir(dir,&dirs,&pos); - if(cnt<1) break; - printf("% 12i %s%c\n",dirs.inode, dirs.name, ((dirs.type==FS_FILE_TYPE_DIR)?'/':' ')); - } + uint32_t ret=_readdir(dir,&dirs,&pos); + if(ret==-1) + { + printf("directory not found!\n"); + break; + } + if(ret==0)break; + printf("% 12i %s%c\n",dirs.inode, dirs.name, ((dirs.type==FS_FILE_TYPE_DIR)?'/':' ')); + } return 0; } diff --git a/userspace/sysfs_write.c b/userspace/sysfs_write.c new file mode 100644 index 0000000..b35f063 --- /dev/null +++ b/userspace/sysfs_write.c @@ -0,0 +1,9 @@ +#include +int main() +{ + FILE *f=fopen("/sys/mem","w"); + uint32_t data=0xaabbccdd; + fwrite(&data,4,1,f); +// fclose(f); // not automatically by newlib? + fflush(f); +} -- cgit v1.2.3