From ec0ba7bc40854eab6a1cdb41364f41f9c11407e1 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Sun, 23 Nov 2014 23:26:26 +0100 Subject: foolshell and syscalls improvememets --- Makefile | 2 +- asm/int_syscall_handler.asm | 2 +- fs/ext2.c | 48 +++++++++++++- fs/fs.c | 4 +- fs/fs.h | 3 +- kernel/kernel.c | 4 +- kernel/keyboard.c | 6 ++ kernel/syscalls.c | 16 +++-- userspace/Makefile | 16 +++-- userspace/foolshell.c | 151 ++++++++++++++++++++++++++++++++++++++++++ userspace/ls.c | 8 ++- userspace/shell.c | 158 -------------------------------------------- 12 files changed, 238 insertions(+), 180 deletions(-) create mode 100644 userspace/foolshell.c delete mode 100644 userspace/shell.c diff --git a/Makefile b/Makefile index ecd38ec..e33c0fb 100644 --- a/Makefile +++ b/Makefile @@ -144,7 +144,7 @@ $(FOOLOS): $(IMG_FILLUP) $(BIN_KERNEL) $(SUBDIRS) #data starts at 0x168000 dd if=$(BIN_MP) of=$@ bs=512 seek=842 conv=notrunc dd if=$(BIN_FOOLFONT) of=$@ bs=512 seek=843 conv=notrunc - dd if=$(IMG_USERSPACE) of=$@ bs=512 seek=846 conv=notrunc + dd if=$(IMG_USERSPACE) of=$@ bs=512 seek=2894 conv=notrunc ############ virtual machines stuff ############ diff --git a/asm/int_syscall_handler.asm b/asm/int_syscall_handler.asm index f90ba51..50202d3 100644 --- a/asm/int_syscall_handler.asm +++ b/asm/int_syscall_handler.asm @@ -156,5 +156,5 @@ call_execve: sti call syscall_execve - jmp $ ; this should never return TODO: pop stack!? + jmp done_blocking diff --git a/fs/ext2.c b/fs/ext2.c index 6920b40..4db858f 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -3,6 +3,8 @@ #define FOOLOS_MODULE_NAME "ext2" +#include + #include "lib/int/stdint.h" #include "lib/logger/log.h" #include "fs.h" @@ -240,11 +242,53 @@ int ext2_inode_content(char *ram,int inode_nr,uint8_t *ramdest,int max) } -//TODO!!! -int ext2_filename_to_inode(char *path) +int ext2_filename_to_inode_traverse(uint8_t *ram, char *path,int inode_start) { + bool final=false; + + // skip leading slashes + while(*path=='/')path++; + char *first=path; + + while(*path!='/'&&*path!=0)path++; + char *last=path; + + if(*path==0)final=true; + else(*path=0); + + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"looking for %s '%s' in inode: %d",final?"file":"dir",first,inode_start); + + fs_dirent dirs[15]; + int count= ext2_read_dir(ram, inode_start,dirs,15); // get dir + + for(int i=0;i -#define EXT2_RAM_ADDRESS 0x168800 + +#define EXT2_RAM_ADDRESS (0x168800+0x100000) enum FS_FILE_TYPE{ diff --git a/kernel/kernel.c b/kernel/kernel.c index 66ca21a..684b70d 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -103,9 +103,9 @@ void kernel_main(uint32_t initial_stack, int mp) // ringbuffer for stdin! ringbuffer_init(); - // load and run inode 15 (shell) + // load and run foolshell // we will come back into the kernel only on interrupts... - syscall_execve(15,0,0); + syscall_execve("/bin/foolshell",0,0); // diff --git a/kernel/keyboard.c b/kernel/keyboard.c index 49c2fa0..074c495 100644 --- a/kernel/keyboard.c +++ b/kernel/keyboard.c @@ -112,6 +112,7 @@ void keyboard_handle(uint8_t in) uint8_t break_key_shift_r=0xb6; uint8_t break_caps_lock=0xba; + uint8_t break_slash=0xb5; if(make_key_shift_l==in)shift_l=true; if(break_key_shift_l==in)shift_l=false;; @@ -126,6 +127,11 @@ void keyboard_handle(uint8_t in) bool match=false; // optimize this! + if(break_slash==in) + { + ascii='/'; + match=true; + } if(break_key_space==in) { ascii=' '; diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 401a5aa..e4cb6cc 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -158,9 +158,12 @@ int syscall_readdir(const char *name,fs_dirent *dirs,int max) int syscall_execve(char *name, char **argv, char **env) { #ifdef LOG_SYSCALLS - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"execve (name=0x%08X, argvs=0x%08X, env=0x%08X)", name,argv,env); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"execve (name=0x%08X(%s), argvs=0x%08X, env=0x%08X)", name,name,argv,env); #endif + int inode_nr=ext2_filename_to_inode(EXT2_RAM_ADDRESS,name); + if(inode_nr<1)return -1; + char arg1[100]; char arg2[100]; @@ -173,15 +176,15 @@ int syscall_execve(char *name, char **argv, char **env) if(argv!=NULL) { - while(argv[argc+1]!=NULL) + while(argv[argc]!=NULL) { //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"arg at 0x%08X: %s", argv[argc],argv[argc]); int i=-1; do{ i++; - argvcopy[argc][i]=argv[argc+1][i]; - }while(argv[argc+1][i]!=0); + argvcopy[argc][i]=argv[argc][i]; + }while(argv[argc][i]!=0); argc++; } @@ -193,7 +196,7 @@ int syscall_execve(char *name, char **argv, char **env) //load binary ext2_check(EXT2_RAM_ADDRESS); - ext2_inode_content(EXT2_RAM_ADDRESS,name,0x800000,0x100000); + ext2_inode_content(EXT2_RAM_ADDRESS,inode_nr,0x800000,0x100000); Elf32_Ehdr *elf; elf=0x800000; @@ -276,6 +279,7 @@ int syscall_execve(char *name, char **argv, char **env) } // argv / argc + //asm("mov $0xf00000,%esp"); // set stack pointer asm("push %0" :: "r" (argvcopy)); asm("push %0" :: "r" (argc)); @@ -345,7 +349,7 @@ int syscall_exit(int ret, int none1, int none2) #endif static char *argv[]={"shell","--silent",NULL}; - syscall_execve(15,argv,0); // start shell + syscall_execve("/bin/foolshell",argv,0); // start shell } diff --git a/userspace/Makefile b/userspace/Makefile index 55e78bc..3327375 100644 --- a/userspace/Makefile +++ b/userspace/Makefile @@ -4,7 +4,7 @@ CFLAGS= CFLAGS+=-w CFLAGS+=-std=gnu11 -PROGS=shell simple brainfuck add checker ls clear +PROGS=foolshell simple brainfuck add checker ls clear ext2.img: $(PROGS) dd if=/dev/zero of=ext2.img bs=512 count=5000 @@ -12,17 +12,19 @@ ext2.img: $(PROGS) mkdir mnt sudo mount ext2.img mnt sudo chown miguel mnt - mkdir mnt/miguel - echo "hello one" > mnt/miguel/test1.txt - echo "hello two" > mnt/test2.txt - cp $^ mnt - cp ~/temp/fool-os-stuff/binutils-build-host-foolos/binutils/readelf mnt + mkdir -p mnt/home/miguel + echo "hello one" > mnt/home/miguel/test1.txt + mkdir -p mnt/bin + cp $^ mnt/bin + cp ~/temp/fool-os-stuff/binutils-build-host-foolos/binutils/readelf mnt/bin + mkdir -p mnt/etc + echo "127.0.0.1 localhost" > mnt/etc/hosts sync sudo umount mnt rm mnt -rf brainfuck: brainfuck.o -shell: shell.o +foolshell: foolshell.o simple: simple.o add: add.o checker: checker.o diff --git a/userspace/foolshell.c b/userspace/foolshell.c new file mode 100644 index 0000000..819c5d6 --- /dev/null +++ b/userspace/foolshell.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +// +void hello() +{ + // ascci art: http://patorjk.com/software/taag/#p=testall&f=Cards&t=Fool%20OS + puts( + + " ______ __ ____ _____ \n" + " / ____/___ ____ / / / __ \\/ ___/ \n" + " / /_ / __ \\/ __ \\/ / / / / /\\__ \\ \n" + " / __/ / /_/ / /_/ / / / /_/ /___/ / \n" + " /_/ \\____/\\____/_/ \\____//____/ \n" + " \n" + "Welcome to FoolShell v0.2 (Compiled on " __DATE__ " at " __TIME__ "\n" + "--------------------------------------------------------------------\n\n" + "type 'help' anytime to show shell built-ins\n" + "or execute user programms that are in the '/bin' directory (e.g. ls)\n" + ); +} + +void prompt() +{ + printf( + "$ " + ); +} + +int main(int argc, char **argv) +{ + + bool silent=false; + for(int i=0;i\n",c, token[c]); + c++; + token[c]=NULL; + + } + + return token; + +} + +int process(char *buf) +{ + + char **token=tokenize(buf); + char *command=token[0]; + // puts(command); + // copied from trottelshell + + if(!strcmp(command,"help")) + { + puts("foolshell: supported built-in commands: 'help', 'echo [string]', 'malloc [bytes]', 'free [address]', 'getenv [var]', 'putenv [var] [val]'"); + } + else if(!strcmp(command,"echo")) + { + printf("\"%s\"\n",token[1]); + + } + else if(!strcmp(command,"malloc")) + { + uint8_t *mall=malloc(atoi(token[1])); + printf("allocated %d bytes at 0x%08X (%i).\n",atoi(token[1]),mall,mall); + } + else if(!strcmp(command,"free")) + { + free(atoi(token[1])); + printf("called free(0x%08X).\n",atoi(token[1])); + } + else if(!strcmp(command,"getenv")) + { + printf("%s = %s \n",token[1],getenv(token[1])); + } + else if(!strcmp(command,"putenv")) + { + char buf[256]; + sprintf(buf,"%s=%s",token[1],token[2]); + putenv(buf); + printf("set: %s = %s \n",token[1],getenv(token[1])); + } + else + { + execve(token[0],token,0); + char buf[256]; + sprintf(buf,"/bin/%s",token[0]); + execve(buf,token,0); + puts("foolshell: command not found"); + } + +} + + + + + diff --git a/userspace/ls.c b/userspace/ls.c index 50c733b..5b5034b 100644 --- a/userspace/ls.c +++ b/userspace/ls.c @@ -15,7 +15,13 @@ int main(int argc, char **argv) return 0; } - int ls=readdir(atoi(argv[1]),dirs,25); + char *dir=argv[1]; + int ls=readdir(dir,dirs,25); + if(ls==-1) + { + printf("%s: file or directory '%s' not found.\n",argv[0],dir); + return 0; + } int i; diff --git a/userspace/shell.c b/userspace/shell.c deleted file mode 100644 index bb8b873..0000000 --- a/userspace/shell.c +++ /dev/null @@ -1,158 +0,0 @@ -#include -#include -#include -#include - -// ascci art: http://patorjk.com/software/taag/#p=testall&f=Cards&t=Fool%20OS -// -void hello() -{ - puts( - - " ______ __ ____ _____\n" - " / ____/___ ____ / / / __ \\/ ___/\n" - " / /_ / __ \\/ __ \\/ / / / / /\\__ \\ \n" - " / __/ / /_/ / /_/ / / / /_/ /___/ / \n" - " /_/ \\____/\\____/_/ \\____//____/ \n" - "\n" - - - - - - - "Welcome to FoolShell v0.2 (Compiled on " __DATE__ " at " __TIME__ "\n" - "--------------------------------------------------------------------\n\n" - "type 'help' anytime to show shell built-ins\n" - "or execute programms/commands that are on your $PATH (e.g. ls)\n" - ); -} - -void prompt() -{ - printf( - "$ " - ); -} - -int main(int argc, char **argv) -{ - - bool silent=false; - for(int i=0;i\n",c, token[c]); - c++; - token[c]=NULL; - - } - - return token; - -} - -int process(char *buf) -{ - - char **token=tokenize(buf); - char *command=token[0]; - // puts(command); - // copied from trottelshell - - if(!strcmp(command,"help")) - { - puts("foolshell: supported built-in commands: 'help', 'echo [string]', exec [inode_nr],'malloc [bytes]', 'free [address]', 'getenv [var]', 'putenv [var] [val]'"); - } - else if(!strcmp(command,"exec")) - { - execve(atoi(token[1]),token,0); - - } - else if(!strcmp(command,"echo")) - { - printf("\"%s\"\n",token[1]); - - } - else if(!strcmp(command,"malloc")) - { - uint8_t *mall=malloc(atoi(token[1])); - printf("allocated %d bytes at 0x%08X (%i).\n",atoi(token[1]),mall,mall); - } - else if(!strcmp(command,"free")) - { - free(atoi(token[1])); - printf("called free(0x%08X).\n",atoi(token[1])); - } - else if(!strcmp(command,"getenv")) - { - printf("%s = %s \n",token[1],getenv(token[1])); - } - else if(!strcmp(command,"putenv")) - { - char buf[256]; - sprintf(buf,"%s=%s",token[1],token[2]); - putenv(buf); - printf("set: %s = %s \n",token[1],getenv(token[1])); - } - else - { - puts("foolshell: command not found"); - } - -} - - - - - -- cgit v1.2.3