Finish fopen in the vfs and hopefully add a working fopen in stdio.c

This commit is contained in:
pjht 2019-07-01 15:13:47 -05:00
parent 2ae02f4d8c
commit aef1d74750
6 changed files with 164 additions and 48 deletions

View File

@ -132,19 +132,9 @@ vfs_message* make_msg(vfs_message_type type, char* mode, char* path) {
return msg_data; return msg_data;
} }
int main(char* initrd, uint32_t initrd_sz) { void test_vfs(char* path,uint32_t box,uint32_t fs_box) {
text_fb_info info; vga_write_string("Sending test message\n");
info.address=map_phys((void*)0xB8000,10); vfs_message* msg_data=make_msg(VFS_OPEN,"r",path);
info.width=80;
info.height=25;
vga_init(info);
vga_write_string("INIT VGA\n");
uint32_t datapos=find_loc("vfs",initrd);
load_task(datapos,initrd);
uint32_t box=mailbox_new(16);
yield();
vga_write_string("Sending first test message\n");
vfs_message* msg_data=make_msg(VFS_OPEN,"r","/dev/sda");
Message msg; Message msg;
msg.from=box; msg.from=box;
msg.to=1; msg.to=1;
@ -153,24 +143,47 @@ int main(char* initrd, uint32_t initrd_sz) {
mailbox_send_msg(&msg); mailbox_send_msg(&msg);
free(msg.msg); free(msg.msg);
yield(); yield();
vga_write_string("Getting message\n"); vga_write_string("Getting fs_box message\n");
msg.msg=malloc(sizeof(vfs_message)); msg.msg=malloc(sizeof(vfs_message));
mailbox_get_msg(box,&msg,sizeof(vfs_message)); mailbox_get_msg(fs_box,&msg,sizeof(vfs_message));
if (msg.from==0) {
vga_write_string("No message\n");
} else {
vfs_message* vfs_msg=(vfs_message*)msg.msg; vfs_message* vfs_msg=(vfs_message*)msg.msg;
display_msg(vfs_msg); display_msg(vfs_msg);
vga_write_string("Sending second test message\n"); msg.to=msg.from;
msg_data=make_msg(VFS_OPEN,"r","/dev/sdb"); msg.from=fs_box;
msg.from=box; vfs_msg->flags=13;
msg.to=1;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg); mailbox_send_msg(&msg);
}
free(msg.msg); free(msg.msg);
yield(); yield();
vga_write_string("Getting message\n"); vga_write_string("Getting message\n");
msg.msg=malloc(sizeof(vfs_message)); msg.msg=malloc(sizeof(vfs_message));
mailbox_get_msg(box,&msg,sizeof(vfs_message)); mailbox_get_msg(box,&msg,sizeof(vfs_message));
vfs_msg=(vfs_message*)msg.msg; if (msg.from==0) {
vga_write_string("No message\n");
} else {
vfs_message* vfs_msg=(vfs_message*)msg.msg;
display_msg(vfs_msg); display_msg(vfs_msg);
for(;;); }
free(msg.msg);
}
int main(char* initrd, uint32_t initrd_sz) {
text_fb_info info;
info.address=map_phys((void*)0xB8000,10);
info.width=80;
info.height=25;
vga_init(info);
// uint32_t datapos=find_loc("vfs",initrd);
// load_task(datapos,initrd);
// uint32_t box=mailbox_new(16);
// yield();
// // uint32_t fs_box=mailbox_new(16);
// // test_vfs("/dev/sda",box,fs_box);
// // test_vfs("/dev/sdb",box,fs_box);
for(;;) {
yield();
}
} }

View File

@ -11,6 +11,8 @@ static int x;
static int y; static int y;
static vga_colors fg_color; static vga_colors fg_color;
static vga_colors bg_color; static vga_colors bg_color;
static char* scroll_buf[0xfa0];
static void set_char(int x,int y,char c) { static void set_char(int x,int y,char c) {
screen[xy_to_indx(x,y)]=c; screen[xy_to_indx(x,y)]=c;
@ -55,6 +57,9 @@ void vga_write_string(const char* string) {
if (c=='\n') { if (c=='\n') {
x=0; x=0;
y++; y++;
for (int i=0;i<67108864;i++) {
1+1;
}
} else { } else {
set_char(x,y,c); set_char(x,y,c);
x++; x++;
@ -65,11 +70,10 @@ void vga_write_string(const char* string) {
} }
if (y==height) { if (y==height) {
x=0; x=0;
y=0; y=24;
// char* pg1=(char*)((uint32_t)screen+0xfa0); memcpy(scroll_buf,&screen[xy_to_indx(0,1)],xy_to_indx(0,24));
// memcpy(pg1,&screen[xy_to_indx(0,1)],xy_to_indx(0,24)); vga_clear();
// vga_clear(); memcpy(screen,scroll_buf,xy_to_indx(0,25));
// memcpy(&screen,pg1,xy_to_indx(0,25));
} }
} }
set_cursor(x,y); set_cursor(x,y);

View File

@ -1,6 +1,8 @@
extern main extern main
extern __stdio_init
global _start global _start
_start: _start:
call __stdio_init
call main call main
ret ret

56
libc/stdio.c Normal file
View File

@ -0,0 +1,56 @@
#include <mailboxes.h>
#include <string.h>
#include <stdlib.h>
#include <ipc/vfs.h>
#include <stdio.h>
#include <tasking.h>
#define VFS_PID 1
static uint32_t box;
void __stdio_init() {
box=mailbox_new(16);
}
static vfs_message* make_msg(vfs_message_type type, char* mode, char* path) {
static uint32_t id=0;
vfs_message* msg_data=malloc(sizeof(vfs_message));
msg_data->type=type;
msg_data->id=id;
id++;
strcpy(&msg_data->mode[0],mode);
strcpy(&msg_data->path[0],path);
return msg_data;
}
FILE* fopen(char* filename,char* mode) {
if (strlen(filename)>4096 || strlen(mode)>10) {
return NULL;
}
vfs_message* msg_data=make_msg(VFS_OPEN,mode,filename);
Message msg;
msg.from=box;
msg.to=VFS_PID;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
free(msg.msg);
yield();
msg.msg=malloc(sizeof(vfs_message));
mailbox_get_msg(box,&msg,sizeof(vfs_message));
while (msg.from==0) {
yield();
mailbox_get_msg(box,&msg,sizeof(vfs_message));
}
vfs_message* vfs_msg=(vfs_message*)msg.msg;
if (vfs_msg->flags) {
free(msg.msg);
return NULL;
} else {
FILE* file=malloc(sizeof(FILE));
*file=vfs_msg->fd; //We're using pointers to FILE even though it's a uint32_t so we can expand to a struct if needed
free(msg.msg);
return file;
}
}

View File

@ -5,8 +5,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#define FILE uint32_t #define FILE uint32_t //We're using pointers to FILE even though it's a uint32_t so we can expand to a struct if needed
#define SEEK_CUR 1 #define SEEK_CUR 1
#define SEEK_END 2 #define SEEK_END 2
#define SEEK_SET 3 #define SEEK_SET 3
@ -17,7 +16,7 @@
#define stderr 2 #define stderr 2
FILE* fopen(const char* filename,const char* mode); FILE* fopen(char* filename,char* mode);
int fgetc(FILE* stream); int fgetc(FILE* stream);
int getc(); int getc();
char* fgets(char* str,int count,FILE* stream); char* fgets(char* str,int count,FILE* stream);

View File

@ -30,6 +30,10 @@ uint16_t open_fds[32768];
vfs_message* get_message(Message* msg,uint32_t box) { vfs_message* get_message(Message* msg,uint32_t box) {
msg->msg=malloc(sizeof(vfs_message)); msg->msg=malloc(sizeof(vfs_message));
mailbox_get_msg(box,msg,sizeof(vfs_message)); mailbox_get_msg(box,msg,sizeof(vfs_message));
while (msg->from==0 && msg->size==0) {
mailbox_get_msg(box,msg,sizeof(vfs_message));
yield();
}
vfs_message* vfs_msg=(vfs_message*)msg->msg; vfs_message* vfs_msg=(vfs_message*)msg->msg;
msg->to=msg->from; msg->to=msg->from;
msg->from=box; msg->from=box;
@ -47,10 +51,14 @@ static int vfsstrcmp(const char* s1,const char* s2) {
void init_vfs() { void init_vfs() {
drvs=malloc(sizeof(uint32_t)*32); drvs=malloc(sizeof(uint32_t)*32);
drvs[0]=2;
drv_names=malloc(sizeof(const char**)*32); drv_names=malloc(sizeof(const char**)*32);
max_drvs=32; max_drvs=32;
next_drv_indx=0; next_drv_indx=0;
head_mapping=NULL; head_mapping=malloc(sizeof(vfs_mapping));
head_mapping->mntpnt="/dev/";
head_mapping->type=0;
head_mapping->next=NULL;
tail_mapping=NULL; tail_mapping=NULL;
} }
@ -66,10 +74,42 @@ uint32_t register_fs(uint32_t drv,const char* type) {
return next_drv_indx-1; return next_drv_indx-1;
} }
char fopen(vfs_message* vfs_msg,uint32_t from) { char vfs_fopen(vfs_message* vfs_msg,uint32_t from) {
vfs_mapping* mnt=head_mapping;
vfs_mapping* mntpnt=NULL;
const char* path;
uint32_t mntpnt_len=0;
for (;mnt!=NULL;mnt=mnt->next) {
char* root=mnt->mntpnt;
if (strlen(root)>mntpnt_len) {
if (vfsstrcmp(root,vfs_msg->path)==0) {
mntpnt=mnt;
mntpnt_len=strlen(root);
}
}
}
if (mntpnt) {
Message msg;
char* path_buf=malloc(sizeof(char)*4096);
strcpy(path_buf,&(vfs_msg->path[0]));
memset(&(vfs_msg->path[0]),0,sizeof(char)*4096);
for (int i=0;i<strlen(path_buf)+1-mntpnt_len;i++) {
vfs_msg->path[i]=path_buf[i+mntpnt_len];
}
free(path_buf);
msg.from=1;
msg.to=drvs[mntpnt->type];
msg.size=sizeof(vfs_message);
msg.msg=vfs_msg;
mailbox_send_msg(&msg);
yield();
vfs_message* vfs_msg=get_message(&msg,1);
if (vfs_msg->flags!=0) {
return vfs_msg->flags;
}
if (fd_tables[from]==NULL) { if (fd_tables[from]==NULL) {
fd_tables[from]=malloc(PROC_FD_LIMIT*sizeof(vfs_file)); fd_tables[from]=malloc(PROC_FD_LIMIT*sizeof(vfs_file));
open_fds[from]=0; open_fds[from]=1;
} else { } else {
if (open_fds[from]==PROC_FD_LIMIT) { if (open_fds[from]==PROC_FD_LIMIT) {
return 4; return 4;
@ -79,6 +119,8 @@ char fopen(vfs_message* vfs_msg,uint32_t from) {
open_fds[from]++; open_fds[from]++;
vfs_msg->fd=fd; vfs_msg->fd=fd;
return 0; return 0;
}
return 1;
} }
int main() { int main() {
@ -90,7 +132,7 @@ int main() {
vfs_message* vfs_msg=get_message(&msg,box); vfs_message* vfs_msg=get_message(&msg,box);
switch (vfs_msg->type) { switch (vfs_msg->type) {
case VFS_OPEN: case VFS_OPEN:
vfs_msg->flags=fopen(vfs_msg,msg.from); vfs_msg->flags=vfs_fopen(vfs_msg,msg.from);
break; break;
case VFS_PUTC: case VFS_PUTC:
vfs_msg->flags=1; vfs_msg->flags=1;