From 509531f1d1e709fb096f15d5ccf3a7946bed8c0b Mon Sep 17 00:00:00 2001 From: wolfwood Date: Sat, 1 May 2010 21:25:44 -0400 Subject: [PATCH] argv[] -> argv[][] and working printf, read, open... write should work but is untested --- newlib-files/crt0.c | 46 ++++++++++++---- newlib-files/syscalls.c | 114 ++++++++++++++++++++++++++++++---------- 2 files changed, 121 insertions(+), 39 deletions(-) diff --git a/newlib-files/crt0.c b/newlib-files/crt0.c index 9f01029..3dd0628 100644 --- a/newlib-files/crt0.c +++ b/newlib-files/crt0.c @@ -1,23 +1,49 @@ extern int main(int argc, char **argv, char **environ); -extern char __bss_start, _end; // BSS should be the last think before _end +extern char __bss_start, _end; // BSS should be the last thing before _end // XXX: environment char *__env[1] = { 0 }; char **environ = __env; -_start(){ - char *i; +void _start(){ + asm("popq %rdi; popq %rsi; callq start;"); +} - // zero BSS - for(i = &__bss_start; i < &_end; i++){ - *i = 0; +void start(unsigned long long len, char* args){ + int substrings = 1, i = 0; + char* tmp = args; + + // find the number of arguments + while((*tmp) != '\0'){ + if(*tmp == ' '){substrings++;} + tmp++; + } + + // this will become argv, its allocated on the stack + char* argv[substrings]; + + // build argv and turn spaces into terminators + tmp = args; + argv[0] = args; + + while((*tmp) != '\0'){ + if(*tmp == ' '){ + i++; + *tmp = '\0'; + tmp++; + argv[i] = tmp; + }else{ + tmp++; + } + } + + // zero BSS + for(tmp = &__bss_start; tmp < &_end; tmp++){ + *tmp = 0; } - // XXX: get argc and argv - - - exit(main(0,0, __env)); + exit(main(substrings,&argv[0], __env)); } diff --git a/newlib-files/syscalls.c b/newlib-files/syscalls.c index 1919990..f8318cc 100644 --- a/newlib-files/syscalls.c +++ b/newlib-files/syscalls.c @@ -71,42 +71,100 @@ wait(int *status) { * returns 0 if not. Since we're hooked up to a * serial port, we'll say yes and return a 1. */ + + +int gibOpen(const char* name, unsigned int nameLen, char readOnly); +int gibRead(int fd, void* buf, unsigned int len); +int gibWrite(int fd, void* buf, unsigned int len); + + int isatty(fd) int fd; { - return (1); -} - - -int -close(int file) { - return -1; -} - -int -link(char *old, char *new) { - errno = EMLINK; - return -1; -} - -int -lseek(int file, int ptr, int dir) { - return 0; + if(fd < 3){ + return 1; + }else{ + return 0; + } } int open(const char *name, int flags, ...) { - return -1; + int nameLen = strlen(name); + unsigned char readOnly = 0; + int fd; + + if( flags & O_RDONLY ){ + readOnly = 1; + } + + fd = gibOpen(name, nameLen, readOnly); + + if(fd == -1){ + errno = ENFILE; + return -1; + } + + printf("new fd assigned: %d\n", fd); + + return fd; +} + +int +close(int file) { + int err = gibClose(file); + + if(err < 0){ + errno = EBADF; + return -1; + }else{ + return 0; + } } int read(int file, char *ptr, int len) { // XXX: keyboard support + if(file == 0){ + return -1; + } + int err = gibRead(file, ptr, len); + + if(err == -1){ + errno = EBADF; + } + + return err; +} + +int +write(int file, char *ptr, int len) { + if(file == 1 || file == 2){ + wconsole(ptr, len); + + return len; + } + + int err = gibWrite(file, ptr, len); + + if(err >= 0){ + return err; + }else{ + errno = EBADF; + return -1; + } +} + + +/* XXX: implement these */ +int +lseek(int file, int ptr, int dir) { return 0; } + int fstat(int file, struct stat *st) { st->st_mode = S_IFCHR; @@ -119,6 +177,12 @@ stat(const char *file, struct stat *st){ return 0; } +int +link(char *old, char *new) { + errno = EMLINK; + return -1; +} + int unlink(char *name) { errno = ENOENT; @@ -126,16 +190,6 @@ unlink(char *name) { } -int -write(int file, char *ptr, int len) { - //XXX: write to stdout - if(file == 1){ - wconsole(ptr, len); - } - - return -1; -} - // --- Memory --- /* _end is set in the linker command file */ @@ -184,6 +238,7 @@ sbrk(int nbytes){ } while(nbytes > PAGE_SIZE){ + //???: could skip and use allocate-on-fault pagefault handler allocPage(heap_ptr); nbytes -= (int) PAGE_SIZE; @@ -191,6 +246,7 @@ sbrk(int nbytes){ } if( nbytes > 0){ + //???: could skip and use allocate-on-fault pagefault handler allocPage(heap_ptr); heap_ptr += nbytes;