Add a tar fs driver and posix_spawn

This commit is contained in:
pjht 2020-08-23 14:05:38 -05:00
parent b5f5aa8ed0
commit ff2dbb91e1
12 changed files with 4165 additions and 35 deletions

View File

@ -34,7 +34,7 @@ debug: os.iso kernel/kernel.elf
gdb
#gdbgui -g i386-elf-gdb --project $(CWD)
os.iso: kernel/kernel.elf init vfs devfs vga_drv initrd_drv sysroot/usr/share/man # vfs devfs initrd vga_drv initrd_drv pci
os.iso: kernel/kernel.elf init vfs devfs vga_drv initrd_drv tar_fs sysroot/usr/share/man # vfs devfs initrd vga_drv initrd_drv pci
@cp kernel/kernel.elf sysroot/boot
@cd initrd; tar -f ../sysroot/boot/initrd.tar -c *
@grub-mkrescue -o $@ sysroot >/dev/null 2>/dev/null
@ -66,6 +66,10 @@ initrd_drv: crts libc
@cd $@ && make
@cp $@/$@ initrd/$@
tar_fs: crts libc
@cd $@ && make
@cp $@/$@ initrd/$@
kernel/kernel.elf: $(OBJ) $(ASM_OBJ) $(S_ASM_OBJ) sysroot/usr/lib/libc.a
@$(CC) -z max-page-size=4096 -Xlinker -n -T kernel/cpu/$(PLAT)/linker.ld -o $@ $(CFLAGS) -nostdlib $^ -lgcc

View File

@ -26,6 +26,7 @@ void open(void* args) {
serdes_state state;
start_deserialize(args,&state);
char* path=deserialize_str(&state);
deserialize_ptr(&state);
rpc_deallocate_buf(args,state.sizeorpos);
int i;
char found=0;

3944
init/dump Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@
#include <tasking.h>
#include <rpc.h>
#include <serdes.h>
#include <spawn.h>
typedef struct {
char filename[100];
@ -91,36 +92,6 @@ char load_proc(size_t datapos,char* initrd) {
return 1;
}
char load_proc_devfs(size_t datapos) {
FILE* initrd=fopen("/dev/initrd","r");
elf_header header;
fseek(initrd,datapos,SEEK_SET);
fread(&header,sizeof(elf_header),1,initrd);
if (header.magic!=ELF_MAGIC) {
serial_print("Bad magic number (");
char str[32];
hex_to_ascii(header.magic,str);
serial_print(str);
serial_print(")\n");
return 0;
} else {
void* address_space=new_address_space();
for (int i=0;i<header.pheader_ent_nm;i++) {
elf_pheader pheader;
fseek(initrd,(header.prog_hdr)+(header.pheader_ent_sz*i)+datapos,SEEK_SET);
fread(&pheader,sizeof(elf_pheader),1,initrd);
char* ptr=alloc_memory(((pheader.memsz)/4096)+1);
memset(ptr,0,pheader.memsz);
if (pheader.filesz>0) {
fseek(initrd,pheader.offset+datapos,SEEK_SET);
fread(ptr,sizeof(char),pheader.filesz,initrd);
}
copy_data(address_space,ptr,pheader.memsz,(void*)pheader.vaddr);
}
create_proc((void*)header.entry,address_space,NULL,NULL);
}
return 1;
}
int main() {
serial_print("Init running\n");
@ -141,10 +112,15 @@ int main() {
datapos=find_loc("initrd_drv",initrd);
load_proc(datapos,initrd);
while(rpc_is_init(4)==0);
serial_print("Loading VGA driver\n");
datapos=find_loc("vga_drv",initrd);
load_proc_devfs(datapos);
serial_print("Loading tar_fs\n");
datapos=find_loc("tar_fs",initrd);
load_proc(datapos,initrd);
while(rpc_is_init(5)==0);
serial_print("Mounting initrd\n");
mount("/dev/initrd","tarfs","/initrd");
serial_print("Loading VGA driver\n");
posix_spawn(NULL,"/initrd/vga_drv",NULL,NULL,NULL,NULL);
while(rpc_is_init(6)==0);
serial_print("Opening /dev/vga\n");
stdout=fopen("/dev/vga","w");
if (!stdout) {

36
libc/spawn.c Normal file
View File

@ -0,0 +1,36 @@
#include <spawn.h>
#include <elf.h>
#include <tasking.h>
#include <memory.h>
#include <stdio.h>
int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* attrp,
char* const argv[], char* const envp[]) {
FILE* image=fopen(path,"r");
elf_header header;
fread(&header,sizeof(elf_header),1,image);
if (header.magic!=ELF_MAGIC) {
serial_print("Bad magic number (");
char str[32];
hex_to_ascii(header.magic,str);
serial_print(str);
serial_print(")\n");
return 0;
} else {
void* address_space=new_address_space();
for (int i=0;i<header.pheader_ent_nm;i++) {
elf_pheader pheader;
fseek(image,(header.prog_hdr)+(header.pheader_ent_sz*i),SEEK_SET);
fread(&pheader,sizeof(elf_pheader),1,image);
char* ptr=alloc_memory(((pheader.memsz)/4096)+1);
memset(ptr,0,pheader.memsz);
if (pheader.filesz>0) {
fseek(image,pheader.offset,SEEK_SET);
fread(ptr,sizeof(char),pheader.filesz,image);
}
copy_data(address_space,ptr,pheader.memsz,(void*)pheader.vaddr);
}
create_proc((void*)header.entry,address_space,NULL,NULL);
}
return 1;
}

12
libc/spawn.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef SPAWN_H
#define SPAWN_H
#include <sys/types.h>
#define posix_spawn_file_actions_t int
#define posix_spawnattr_t int
int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* attrp,
char* const argv[], char* const envp[]);
#endif

View File

@ -142,8 +142,11 @@ size_t fwrite(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
void register_fs(const char* name,pid_t pid) {
serdes_state state={0};
serial_print("libc register fs 1\n");
serialize_str((char*)name,&state);
serial_print("libc register fs 2\n");
serialize_int(pid,&state);
serial_print("libc register fs 3\n");
rpc_call(2,"register_fs",state.buf,state.sizeorpos);
}

View File

@ -156,7 +156,7 @@ void* realloc(void *mem, size_t new_sz) {
}
size_t num_4b_grps=*((size_t*)((char*)mem-12));
memcpy(ptr,mem,num_4b_grps*4);
free(mem);
//free(mem);
mem=ptr;
return ptr;
}

13
tar_fs/Makefile Normal file
View File

@ -0,0 +1,13 @@
C_SOURCES = $(wildcard *.c)
OBJ = $(C_SOURCES:.c=.o )
CFLAGS = -Wall -g
CC = i386-myos-gcc
tar_fs: $(OBJ) ../libc/*
@$(CC) -o $@ $(CFLAGS) $(OBJ)
%.o: %.c
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@rm -rf *.o initrd_drv

139
tar_fs/main.c Normal file
View File

@ -0,0 +1,139 @@
#include <stddef.h>
#include <string.h>
#include <serdes.h>
#include <rpc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dbg.h>
typedef struct {
char filename[100];
char mode[8];
char uid[8];
char gid[8];
char size[12];
char mtime[12];
char chksum[8];
char typeflag[1];
} tar_header;
typedef struct tar_file {
int pos;
char name[100];
char* dev;
struct tar_file* next;
} tar_file;
typedef struct {
FILE* access_file;
int base_pos;
} open_file_info;
size_t getsize(const char *in) {
size_t size=0;
size_t j;
size_t count=1;
for (j=11;j>0;j--,count*=8) {
size+=((in[j-1]-'0')*count);
}
return size;
}
tar_file* get_file_list(char* dev) {
FILE* tar_archive=fopen(dev,"r");
size_t pos=0;
tar_header tar_hdr;
tar_file* list=NULL;
for (int i=0;;i++) {
fseek(tar_archive,pos,SEEK_SET);
fread(&tar_hdr,sizeof(tar_header),1,tar_archive);
if (tar_hdr.filename[0]=='\0') break;
size_t size=getsize(tar_hdr.size);
pos+=512;
tar_file* list_entry=malloc(sizeof(tar_file));
list_entry->pos=pos;
list_entry->dev=dev;
strcpy(list_entry->name,tar_hdr.filename);
list_entry->next=list;
list=list_entry;
pos+=size;
if (pos%512!=0) {
pos+=512-(pos%512);
}
}
return list;
}
void tar_fs_mount(void* args) {
char* dev=(char*)args;
tar_file* file_list=get_file_list(dev);
serdes_state state;
serialize_int(0,&state);
serialize_ptr(file_list,&state);
rpc_return(state.buf,state.sizeorpos);
free(state.buf);
pthread_exit(NULL);
}
void open(void* args) {
serdes_state state;
start_deserialize(args,&state);
char* path=deserialize_str(&state);
tar_file* file_list=deserialize_ptr(&state);
for (;file_list!=NULL;file_list=file_list->next) {
if (strcmp(path,file_list->name)==0) {
break;
}
}
if (file_list) {
open_file_info* info=malloc(sizeof(open_file_info));
info->access_file=fopen(file_list->dev,"r");
info->base_pos=file_list->pos;
state.buf=NULL;
state.sizeorpos=0;
serialize_int(0,&state);
serialize_ptr(info,&state);
serialize_int(0,&state);
rpc_return(state.buf,state.sizeorpos);
pthread_exit(NULL);
} else {
state.buf=NULL;
state.sizeorpos=0;
serialize_int(1,&state);
serialize_ptr(NULL,&state);
serialize_int(0,&state);
rpc_return(state.buf,state.sizeorpos);
pthread_exit(NULL);
}
}
void read(void* args) {
serdes_state state;
start_deserialize(args,&state);
open_file_info* info=deserialize_ptr(&state);
size_t size=deserialize_int(&state);
int pos=deserialize_int(&state);
rpc_deallocate_buf(args,state.sizeorpos);
char* data=malloc(sizeof(char)*size);
fseek(info->access_file,info->base_pos+pos,SEEK_SET);
fread(data,size,1,info->access_file);
state.buf=NULL;
state.sizeorpos=0;
serialize_int(size,&state);
serialize_ary(data,size,&state);
rpc_return(state.buf,state.sizeorpos);
free(state.buf);
pthread_exit(NULL);
}
int main() {
rpc_register_func("mount",&tar_fs_mount);
rpc_register_func("open",&open);
rpc_register_func("read",&read);
serial_print("Registering tarfs filesystem\n");
register_fs("tarfs",getpid());
serial_print("Initialized tarfs\n");
rpc_mark_as_init();
}

BIN
tar_fs/tar_fs Executable file

Binary file not shown.

View File

@ -74,6 +74,7 @@ void vfs_mount(void* args) {
}
void vfs_register_fs(void* args) {
serial_print("register fs\n");
serdes_state state;
start_deserialize(args,&state);
char* name=deserialize_str(&state);
@ -119,6 +120,7 @@ void open(void* args) {
state.buf=NULL;
state.sizeorpos=0;
serialize_str(path+(strlen(mnt_pnt->path)+1),&state);
serialize_ptr(mnt_pnt->fs_data,&state);
char* retbuf=rpc_call(mnt_pnt->fs_pid,"open",state.buf,state.sizeorpos);
free(state.buf);
start_deserialize(retbuf,&state);