From b74c5c8fb4de3b2847bc942e57dcf8f0dea705be Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Wed, 13 May 2015 23:58:39 +0200 Subject: fixed libc and 'userspace' --- Makefile | 9 +++++---- README.md | 38 ++++++++++++++++++++++++++++++------ fs/elf.c | 3 ++- fs/fs.h | 2 +- kernel/config.h | 2 +- kernel/console.c | 2 +- kernel/kernel.c | 3 +-- kernel/mem.c | 4 +++- kernel/syscalls.c | 49 +++++++++++++++++++---------------------------- kernel/task.c | 2 ++ userspace/Makefile | 10 +++++++--- userspace/cat.c | 17 ++++++++++++++++ userspace/foolshell.c | 9 +++++---- userspace/init.c | 2 +- userspace/sys/Makefile | 41 +++++++++++++++++++++------------------ userspace/sys/crt0.o | Bin 0 -> 660 bytes userspace/sys/libfool.a | Bin 0 -> 7420 bytes userspace/sys/sys.c | 2 +- userspace/sys/sys.o | Bin 0 -> 3744 bytes userspace/sys/syscalls.o | Bin 0 -> 2948 bytes userspace/sys/termios.h | 2 ++ 21 files changed, 123 insertions(+), 74 deletions(-) create mode 100644 userspace/cat.c create mode 100644 userspace/sys/crt0.o create mode 100644 userspace/sys/libfool.a create mode 100644 userspace/sys/sys.o create mode 100644 userspace/sys/syscalls.o create mode 100644 userspace/sys/termios.h diff --git a/Makefile b/Makefile index 33fa87f..8d8b957 100644 --- a/Makefile +++ b/Makefile @@ -19,13 +19,13 @@ FOOLOS=FoolOS.img FOOLOS_VDI=FoolOS.vdi #submodules -SUBDIRS=boot1 boot2 font userspace +SUBDIRS=font userspace #take care to set this properly! USB_STICK=/dev/sdf #use our cross compiler -CC=i686-elf-gcc +CC=i686-foolos-gcc ############ compiler flags ############ CFLAGS= @@ -85,7 +85,7 @@ ASM_MULTIBOOT_OBJ=$(patsubst %.s, %.o, $(ASM_MULTIBOOT)) ############ Final Targets ############ -all: $(FOOLOS) $(FOOLOS_VDI) +all: $(FOOLOS) tags # $(FOOLOS_VDI) new: clean all run: run-qemu-debug newrun: clean run @@ -134,8 +134,9 @@ $(KERNEL_IMG): $(ASM_MULTIBOOT_OBJ) $(ASM_OBJECTS) $(OBJECTS) ############ assembling of final image ############ # master boot record, kernel binary and fool-font -$(FOOLOS): $(KERNEL_IMG) # $(SUBDIRS) +$(FOOLOS): $(KERNEL_IMG) $(SUBDIRS) sudo cp $(KERNEL_IMG) /mnt/vdi/boot + sudo cp $(IMG_USERSPACE) /mnt/vdi/boot sync # dd if=$(BIN_MBR) of=$@ bs=512 seek=0 conv=notrunc diff --git a/README.md b/README.md index 2bc78a3..eb8cea2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,35 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Building +-------- + +Adapt $SYSROOT in userspace/sys/Makefile + +Requirements: + +* OS Specific Toolchain [1] + + binutils-2.25 + gcc-5.1.0 + +* Newlib 2.2.0 [2] + + adapt config.sub + + $ ./configure --target=i686-foolos --prefix=/home/miguel/opt/gcc-5.1.0-foolos/ + $ make + $ make install + + /* + adapt libgloss/libnosys + uncomment environ.c and add stubwarnings for sbrk and exit + */ + +* GRUB2 + + + Usage ----- @@ -34,12 +63,6 @@ following way: $ cat FoolOS.img > /dev/sdX -You will require some dependencies (as newlib) in order to build. - -adapt libgloss/libnosys - -uncomment environ.c and add stubwarnings for sbrk and exit -Build newlib with: --target=i686-elf to use the crosscompiler Supported Platforms @@ -145,6 +168,9 @@ REFERENCES * http://wiki.xomb.org/index.php?title=ACPI_Tables * and many many more... +[1] http://wiki.osdev.org/Hosted_GCC_Cross-Compiler +[2] https://sourceware.org/newlib/ + NOTES ===== in freestanding mode we can just use this c lib headers: diff --git a/fs/elf.c b/fs/elf.c index f5262dd..241b3ae 100644 --- a/fs/elf.c +++ b/fs/elf.c @@ -76,7 +76,8 @@ uint32_t load_elf(char *name, uint32_t *alloc) //TODO: load ELF binary and move this to own compilation unit //load binary - uint32_t vaddr=0x08048000; +// uint32_t vaddr=0x08048000; + uint32_t vaddr=0x08000000; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"loading %s",name); ext2_check(EXT2_RAM_ADDRESS); diff --git a/fs/fs.h b/fs/fs.h index 0194093..fe35f3f 100644 --- a/fs/fs.h +++ b/fs/fs.h @@ -4,7 +4,7 @@ #include -#define EXT2_RAM_ADDRESS 0x120000 // here the bootloader puts our image +#define EXT2_RAM_ADDRESS 0x124000 // here the bootloader puts our image enum FS_FILE_TYPE{ diff --git a/kernel/config.h b/kernel/config.h index 216641c..2e2a169 100644 --- a/kernel/config.h +++ b/kernel/config.h @@ -8,7 +8,7 @@ #define FOOLOS_CONFIG_H #define FOOLOS_CONSOLE_AUTOBREAK // add newline automatically at end of line -//#define FOOLOS_LOG_OFF // do not log anything +#define FOOLOS_LOG_OFF // do not log anything #define FOOLOS_CONSOLE // otherwise VESA will be used! #define FOOLSOS_SHOW_VESAMODES #define MEM_PRINT_MEMORYMAP diff --git a/kernel/console.c b/kernel/console.c index 0a208bb..9db1c0b 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -16,7 +16,7 @@ uint32_t console_init(){ scr_clear(); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"init console"); - // int inode_nr=ext2_filename_to_inode(EXT2_RAM_ADDRESS,"/binfont.bin"); +//int inode_nr=ext2_filename_to_inode(EXT2_RAM_ADDRESS,"/binfont.bin"); // if(inode_nr!=-1)ext2_inode_content(EXT2_RAM_ADDRESS,inode_nr,0x8000000,0xffff); // load font; return 0; } diff --git a/kernel/kernel.c b/kernel/kernel.c index 76ae8d7..97847e6 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -118,7 +118,6 @@ void kernel_main(uint32_t eax,uint32_t ebx) int_init(0x08); - //while(1); // // Scan the PCI Bus @@ -151,7 +150,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) // // Initialize Multitasking // - //task_init(dir); //; this will never return! + task_init(dir); //; this will never return! // while(1); //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"."); diff --git a/kernel/mem.c b/kernel/mem.c index 72b92af..f29c34f 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -102,10 +102,11 @@ int mmap_first_free_s (uint32_t x) void pmmngr_init_region (uint32_t base, uint32_t size) { - if(base<0x100000)return; uint32_t align = base / PMMNGR_BLOCK_SIZE +1; // TODO: calc properly uint32_t blocks = size / PMMNGR_BLOCK_SIZE -1; + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"init blocks: 0x%08X 0x%08X",base,size); + for (; blocks>0; blocks--) { mmap_unset (align++); @@ -253,6 +254,7 @@ void mem_init(uint32_t memmap,uint32_t length) // deinitialize kernel + memory (TODO: modules) pmmngr_deinit_region(kernel_start,((uint32_t)kernel_end-(uint32_t)kernel_start)); pmmngr_deinit_region(_mmngr_memory_map,mem_array_size*4+PMMNGR_BLOCK_SIZE); + pmmngr_deinit_region(0,0x700000); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Usable Mem: %d (0x%X) bytes. (~%d MB)",total_mem,total_mem,total_mem/1024/1024); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Currently Free 4K blocks: %d blocks.",mem_free_blocks); diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 6d46f1a..38f7161 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -14,7 +14,7 @@ int syscall_unhandled(int nr) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"syscall: %d", nr); - panic(FOOLOS_MODULE_NAME,"unhandled syscall"); + panic(FOOLOS_MODULE_NAME,"unhandled syscall (generic handler)"); } int syscall_lseek(int file,int ptr,int dir) @@ -23,7 +23,7 @@ int syscall_lseek(int file,int ptr,int dir) log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"lseek (file=%d, ptr=%d, dir=%d)", file,ptr,dir); #endif - panic(FOOLOS_MODULE_NAME,"unhandled syscall"); + panic(FOOLOS_MODULE_NAME,"unhandled syscall: lseek"); return 0; @@ -39,7 +39,7 @@ int syscall_write(int file, char *buf, int len) log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"[%d] write(file=%d, buf=0x%08X, len=%d)", task_get_current_pid(),file,buf,len); #endif - if(file!=1&&file!=2) panic(FOOLOS_MODULE_NAME,"unhandled syscall"); + if(file!=1&&file!=2) panic(FOOLOS_MODULE_NAME,"unhandled syscall: write (only stdout and stderr)"); //stderr and stdout go to console for(int i=0;i + +int main() +{ + FILE *f = fopen("README", "r"); + + if (f == NULL) + { + perror("unable to open file"); + return 1; + } + + puts("open success"); + fclose(f); + + return 0; +} diff --git a/userspace/foolshell.c b/userspace/foolshell.c index 3a87158..da05e96 100644 --- a/userspace/foolshell.c +++ b/userspace/foolshell.c @@ -45,6 +45,7 @@ int main(int argc, char **argv) if(!silent)hello(); char *buf=malloc(256); + // printf("malloc returned: 0x%08X\n",buf); while(1) { @@ -87,8 +88,6 @@ char **tokenize(char *buf) i++; } token[c][t]=0; - - // printf("token %i : <%s>\n",c, token[c]); c++; @@ -216,15 +215,17 @@ int process(char *buf) int pid=fork(); if(pid!=0) { - //printf("new task pid: %i \n",pid); + printf("new task pid: %i \n",pid); } if(pid==0) { char buf[256]; sprintf(buf,"%s",token[0]); execve(buf,token,environ); - sprintf(buf,"%s/%s",getenv("PATH"),token[0]); + //sprintf(buf,"%s/%s",getenv("PATH"),token[0]); + sprintf(buf,"%s/%s","/bin",token[0]); execve(buf,token,environ); + puts("foolshell: command not found"); exit(1); } diff --git a/userspace/init.c b/userspace/init.c index f588875..5d3658c 100644 --- a/userspace/init.c +++ b/userspace/init.c @@ -1,7 +1,7 @@ int main(int argc, char **argv) { - //printf("fool-init: spawning a Fool's Shell\n"); + printf("fool-init: spawning a Fool's Shell\n"); // loop forever and spawn shells if the top-shell exits while(1) diff --git a/userspace/sys/Makefile b/userspace/sys/Makefile index 5e217fa..cdcd3b7 100644 --- a/userspace/sys/Makefile +++ b/userspace/sys/Makefile @@ -1,29 +1,32 @@ +SYSROOT=/home/miguel/opt/foolos CC=i686-foolos-gcc CFLAGS= -CFLAGS+=-I../.. CFLAGS+=-w - +CFLAGS+=-I../.. +CFLAGS+=-gstabs OBJECTS=sys.o syscalls.o crt0.o -full: clean library_install crt_install + +full: clean library_install crt_install header_install + all: $(OBJECTS) -library_install: all - mkdir temp - mv *.o temp - cp /home/miguel/temp/sysroot/usr/lib/libc.a . - ar x libc.a - rm libc.a -# rm lib_a-environ.o - cp temp/sys*.o . - ar rs libc.a *.o - mv libc.a /home/miguel/temp/sysroot/usr/lib/libc.a - cp temp/*.o . - rm temp -rf - -crt_install: all - cp crt0.o /home/miguel/temp/sysroot/usr/lib/ +crt0.o: crt0.S + +crt_install: crt0.o + cp crt0.o $(SYSROOT)/usr/lib/ + + +header_install: + cp termios.h $(SYSROOT)/usr/include/sys/ + +libfool.a: sys.o syscalls.o + ar rcs libfool.a sys.o syscalls.o + +library_install: libfool.a + cp libfool.a $(SYSROOT)/usr/lib/ + clean: - -rm *.o + -rm *.o *.a diff --git a/userspace/sys/crt0.o b/userspace/sys/crt0.o new file mode 100644 index 0000000..db292b0 Binary files /dev/null and b/userspace/sys/crt0.o differ diff --git a/userspace/sys/libfool.a b/userspace/sys/libfool.a new file mode 100644 index 0000000..31fee46 Binary files /dev/null and b/userspace/sys/libfool.a differ diff --git a/userspace/sys/sys.c b/userspace/sys/sys.c index 25dff7b..adf6ff9 100644 --- a/userspace/sys/sys.c +++ b/userspace/sys/sys.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include // CODE FOR Stack Smashing Protector, TODO: MOVE / and do not duplicate diff --git a/userspace/sys/sys.o b/userspace/sys/sys.o new file mode 100644 index 0000000..14413b2 Binary files /dev/null and b/userspace/sys/sys.o differ diff --git a/userspace/sys/syscalls.o b/userspace/sys/syscalls.o new file mode 100644 index 0000000..371b8af Binary files /dev/null and b/userspace/sys/syscalls.o differ diff --git a/userspace/sys/termios.h b/userspace/sys/termios.h new file mode 100644 index 0000000..ff1e838 --- /dev/null +++ b/userspace/sys/termios.h @@ -0,0 +1,2 @@ +typedef uint32_t speed_t; +typedef uint32_t DIR; -- cgit v1.2.3