diff options
Diffstat (limited to 'fs')
| -rw-r--r-- | fs/fd.c | 101 | ||||
| -rw-r--r-- | fs/fd.h | 8 | ||||
| -rw-r--r-- | fs/mount.c | 13 | ||||
| -rw-r--r-- | fs/mount.h | 3 | ||||
| -rw-r--r-- | fs/sysfs.c | 46 | ||||
| -rw-r--r-- | fs/sysfs.h | 1 |
6 files changed, 155 insertions, 17 deletions
@@ -4,6 +4,9 @@ #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) { @@ -20,21 +23,14 @@ bool fd_has(fd* f) return f->has(f->data); } -bool fd_close(fd* f) +bool fd_eof(fd* f) { - return f->close(f->data); + return f->eof(f->data); } -fd fd_from_fifo(fifo* fif) +bool fd_close(fd* f) { - fd f; - f.type=FD_TYPE_FIFO_BUFFERED; - f.data=fif; - f.read=fifo_get; - f.write=fifo_put; -// f.close=fd_close; - f.has=fifo_has; - return f; + return f->close(f->data); } //---- @@ -62,17 +58,75 @@ uint8_t ext2_read(uint32_t* data) 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); - + return (c==0); } + void ext2_close(void* x) { kbfree(x); } +uint8_t sysfs_get(uint32_t *data) +{ + return ringbuffer_get(data); +} + +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 *r=data; + ringbuffer_free(data); // free ringbuffer buffer + kbfree(data); // free memory holding ringbuffer information +} + +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;i<strlen(buf);i++) + ringbuffer_put(r,buf[i]); + + ringbuffer_put(r,'\n'); +} + +bool fifo_eof(uint32_t* data) +{ + return false; +} +fd fd_from_fifo(fifo* fif) +{ + fd f; + f.type=FD_TYPE_FIFO_BUFFERED; + f.data=fif; + f.read=fifo_get; + f.write=fifo_put; + f.eof=fifo_eof; +// f.close=fd_close; //TODO.. etc also otehr and cleanups + f.has=fifo_has; + return f; +} + fd fd_from_path(char* path) { fd f; @@ -81,6 +135,27 @@ fd fd_from_path(char* path) f.read=ext2_read; f.write=ext2_write; f.close=ext2_close; + f.eof=ext2_eof; f.has=ext2_has; return f; } + +fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...))) +{ + fd f; + f.type=FD_TYPE_SYSFS; + f.data=kballoc(1); + ringbuffer r=ringbuffer_init(1); + + g(&r,get_sysfs_data); + memcpy(f.data,&r,sizeof(ringbuffer)); + + f.read=sysfs_get; + f.write=0; + f.close=sysfs_close; + f.has=sysfs_has; + f.eof=sysfs_eof; + + return f; +} + @@ -7,6 +7,7 @@ #include <stdbool.h> #include "fifo.h" +#include "ringbuffer.h" /* typedef struct @@ -27,7 +28,8 @@ typedef struct enum FD_TYPE{ FD_TYPE_FIFO_BUFFERED=1, FD_TYPE_EXT2_FILE=2, - FD_TYPE_EXT2_DIR=3 + FD_TYPE_EXT2_DIR=3, + FD_TYPE_SYSFS=4 }; typedef struct fd_struct @@ -35,6 +37,7 @@ typedef struct fd_struct bool (*write)(struct fd_struct*,uint8_t); uint8_t (*read)(struct fd_struct*); bool (*has)(struct fd_struct*); + bool (*eof)(struct fd_struct*); bool (*close)(struct fd_struct*); uint8_t type; @@ -43,10 +46,11 @@ typedef struct fd_struct uint8_t fd_read(fd*); bool fd_has(fd*); +bool fd_eof(fd*); bool fd_write(fd*,uint8_t); bool fd_close(fd*); fd fd_from_fifo(fifo* f); fd fd_from_path(char *path); -//fd fd_from_sysfs(void(*g)(void (*f)(char *fmt, ...))); +fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...))); #endif @@ -18,7 +18,7 @@ char *mount_type_to_str(uint32_t t) { case MOUNT_TYPE_EXT2: return "EXT2"; case MOUNT_TYPE_PIPES: return "PIPES"; - case MOUNT_TYPE_SYS: return "SYS"; + case MOUNT_TYPE_SYS: return "SYSFS"; } return "UNKNOWN"; } @@ -39,6 +39,16 @@ void mount_dump() } } +void mount_sysfs(ringbuffer *r, void (*f)(ringbuffer *r,char *fmt, ...)) +{ + f(r,"mounts:"); + for(int i=0;i<mounts_count;i++) + { + mount *m=&mounts[i]; + f(r,"%s mounted at: %s ",mount_type_to_str(m->type),m->path); + } +} + static uint32_t check_match(char *p1, char *p2) { uint32_t c=0; @@ -84,7 +94,6 @@ fd mount_file_open(char *path) { mount m; char *p=get_mount_for_path(path,&m); - klog("%s in %s",p,m.path); return m.mount_file_open(&m,p); } @@ -50,4 +50,7 @@ fd mount_file_open(char *path); /** TODO: should use fd number instead of PATH on each call*/ int mount_read_dir (char *path, fs_dirent *dirs, uint32_t *pos); +/** sysfs interface */ +void mount_sysfs(ringbuffer *r, void (*f)(ringbuffer *r,char *fmt, ...)); + #endif diff --git a/fs/sysfs.c b/fs/sysfs.c new file mode 100644 index 0000000..57a56c8 --- /dev/null +++ b/fs/sysfs.c @@ -0,0 +1,46 @@ +#include "mount.h" +#include "sysfs.h" + +#include "mem.h" +#include "kmalloc.h" +#include "mount.h" + +#include "log.h" + +#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; + + +/* mount interface */ + +fd sysfs_file_open(mount *m,char *path) +{ + klog("sysfs file open: %s",path); + for (int i=0;i<count;i++) + { + if(!strcmp(path,names[i])) + return fd_from_sysfs(map[i]); + } + return fd_from_sysfs(map[0]); +} + +int sysfs_read_dir(mount *m,char *path, fs_dirent *dirs, uint32_t *pos) +{ + if(*pos>=count)return 0; + memcpy(dirs->name,names[*pos],strlen(names[*pos])+1); + *pos+=1; + return 1; +} + +void sysfs_mount(char* path) +{ + mount m; + m.type=MOUNT_TYPE_SYS; + memcpy(m.path,path,strlen(path)+1); + m.mount_file_open=sysfs_file_open; + m.mount_read_dir=sysfs_read_dir; + mount_add(m); +} diff --git a/fs/sysfs.h b/fs/sysfs.h new file mode 100644 index 0000000..7d09395 --- /dev/null +++ b/fs/sysfs.h @@ -0,0 +1 @@ +void sysfs_mount(char* path); |
