From 22615096dee294f63c6940c17f2a448da51d9197 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 20 Nov 2018 10:43:33 -0500 Subject: [PATCH] fs work, initrd/stdio --- Makefile | 8 +++---- initrd/Makefile | 6 +++-- initrd/test | Bin 8060 -> 37024 bytes initrd/test.c | 8 +++++++ link.ld | 20 ---------------- src/fs/initrd.c | 13 +++++++++-- src/kernel/gpio.c | 2 +- src/kernel/startup_stm32l476xx.s | 2 +- src/kernel/task.c | 2 +- src/kernel/vfs.c | 23 ++++++++++++------- src/kernel/vfs.h | 4 ++-- src/pdclib/platform/stmos/include/syscalls.h | 7 ++++++ src/user/user.c | 5 ++-- 13 files changed, 56 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index d497420..488f95f 100644 --- a/Makefile +++ b/Makefile @@ -25,11 +25,9 @@ OBJCOPY = objcopy MCUFLAGS = -mthumb -mcpu=cortex-m4 #-mfloat-abi=hard -mfpu=fpv4-sp-d16 AFLAGS = $(MCUFLAGS) -CFLAGS = $(MCUFLAGS) -ggdb --specs=nosys.specs \ - -I.. \ - -fno-builtin -fsigned-char -ffreestanding \ - -Wall -Werror -Wextra -pedantic \ - -Wno-overlength-strings -Wno-discarded-qualifiers +CFLAGS = $(MCUFLAGS) -ggdb -ffreestanding -nostdlib -fsigned-char -I.. \ + -Wall -Werror -Wextra -pedantic + #-Wno-overlength-strings -Wno-discarded-qualifiers LFLAGS = -T link.ld OUT = main.elf diff --git a/initrd/Makefile b/initrd/Makefile index b2ca46f..705ac6c 100644 --- a/initrd/Makefile +++ b/initrd/Makefile @@ -1,4 +1,6 @@ all: - @arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -fsigned-char -Os -nostdinc \ + @arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -fsigned-char -Os \ + -nostdinc -nostdlib \ + -fdata-sections -ffunction-sections -Wl,--gc-sections \ -I../src/pdclib/include -I../src/pdclib/platform/stmos/include \ - test.c ../src/pdclib/pdclib.a -s -o test -pie + test.c ../src/pdclib/pdclib.a -s -o test diff --git a/initrd/test b/initrd/test index 8b9d8a40dbfee1286e3021a2df2403bf06aab0d2..7bf8246d7cf1a393b86c12d860796678907245a4 100755 GIT binary patch delta 2130 zcmeH}Ur5tY6vxl~ZR+MSbW{5|O>K%v=_nHdOGDCMg;^2(A&}Z^!U93jJ&dgUQR1I4 zo85>ZK3Gp9!UXe88G#Qy_@E*r(Sr{{7J?H5x6tf+f18Wd0vjJ8FhLJ~Em5|u(bo}-W`u9R}23dukTS{&X#veHE)h|@ewi-%ATbuo=V zJkg|?2&%u|!lx}Ad+hDO-W~Wa?|`kqFs3_Cq4+RfjL_XM1znv)zOg4G^xo?zs>yWA z3WYvL_?v_(c9nlixNbfi)+w|=d9t$ajzsO#aE3`fj&ep1i?hdMz)U$=Nl_#>lT zvPbx^u}pdx;XjNIQY#{tLt7_gMMmPplCPVBfq~I4NwbX48C+6efR`Dq=@-JM+-;6F zHyvG@+3wTT!|x})wMI5|%INCW8EHoJQ?bzIj!v*C%qvVqQgfI$ny#d~dKd);7^!2A zwPkqS6gnQ}vnJ)*yItxVDty zlM-r;l3Ab1(%1o=%rcmjW$SH>?5*xrPm9M{Pj;kw$llWAa(bLRm|4qBS;mrhu^Va> zmsSxDB7Ia*3bfcxaRbB;%GJq-52lxA#N0aQ=;szo_^Y`AejzzP*dov0hcoUY%Hpfp tmhyafM90T~5D~c8nj737`Zl{tBFHvB~>=ZCT^zX8g~4XpqG literal 8060 zcmd5>dwf*Ywf-h^5{5h^B!Gt`>Oc|<210l!k3l6NOfm@pYT^|EWhTkMnGljVNi<}z zri#i`+orxs0WB(8+FAuARt;&W(iSb%4A_cjHEnH0u{9`4Ak^G%&Dp~v+FS4McmKHi z_xsMbA8W6*_TFpnIcLu;^X5A(77HTDiY!R!vlw6+`Mp#=q7Xd|cEqsle2kLz`t3-VnoH8gtt%UAe4tG&y8wJYkd+*Mw>Jkn5JI=7~-)?4nWsPUqqZbjow9>2)>8kZY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{?*_i}9pJm)1-|z^ zpsfw~{`Y}8*D zfGt~qhaUzWc?5X$QDEy<;IYSm#~%lN{A1qUYy*Ds6X2&m1%CE3-r_vL6Y}T4lTQLq zJ;ht-?ZA#5z|&6y&pZR{+zGU|1J6DS{NfkDbI$?KKM%a{0?^UHQ*;;5*$Mpemq1q+ zPto1LuYLu*_#*JqOT1y<1N{2ez;Av7{PwrN-o3!fF9WZ<0{rfGymi_K{QmdAtFHpD zy#{o51ECPGe?M^G0Py=fLsfyzS}(PMiQvp5&?B51cv${Oe!97heDa z17OM}W)gXXlkgBLhza}_n>HMn@ilPEIf-{~ykk5&iH_MsIbjfz){)l}cN1;IeZ&T0 zGqHu(N^B#ZAhr`biCx5AqMH!;gG3K;nCK-=5n2*|(}*;}PGk^S!~`Ojm`2PX<`6~1 zB4Qa~5cR}b;%;ICv6a|HJV6xBojc2(HTlw6(q6v|c_b9<^=B6v+dg0Hbu$hljZ#^4CX~n#v^t` z&OwN7ioni?AfEY15;;ki5fQn_dzhR9ktA}0JR%~ujJgE7Vk1(fjFbw`lGEvs;H&JG zDmfW%24w_yi2;=&{$0&7tLtwiZ|B|<$oOQgBt_)h%u9FCC->ljNE@q8=c}1NkC6E? zzl+Yau_`Pl{ca?N5h5q=*So($pU8<{oQklV`Jb%$e{96Iu=slyA^wRzdEeg5KpVk) zB#E4$jflv}|Ek+5C;xLqlIW8303mtNC-?G;Qy8abO?1TmCbo;d$eM~Ae=tucX%lQE fsH;BG5R(dA5-E^HMv}1DjHF!3?Ti%25xM^c`h^~` diff --git a/initrd/test.c b/initrd/test.c index f673ce0..b79f315 100644 --- a/initrd/test.c +++ b/initrd/test.c @@ -1,4 +1,12 @@ #include +#include + +int main(void); + +void _start(void) +{ + exit(main()); +} int main(void) { diff --git a/link.ld b/link.ld index e00d450..092032e 100644 --- a/link.ld +++ b/link.ld @@ -56,26 +56,6 @@ SECTIONS { . = ALIGN(8); } > FLASH - /* ARM stuff */ - .ARM.exidx : { - *(.ARM.exidx) - } > FLASH - - /* init_array/fini_array (TODO understand this) */ - .init_array : { - __init_array_start = .; - KEEP(*(.init_array.*)) - KEEP(*(.init_array*)) - __init_array_end = .; - } > FLASH - - .fini_array : { - __fini_array_start = .; - KEEP(*(.fini_array.*)) - KEEP(*(.fini_array*)) - __fini_array_end = .; - } > FLASH - /* initialized data */ _sidata = LOADADDR(.data); .data : { diff --git a/src/fs/initrd.c b/src/fs/initrd.c index 5731ab5..44e5493 100644 --- a/src/fs/initrd.c +++ b/src/fs/initrd.c @@ -1,5 +1,4 @@ #include -#include #include #include @@ -30,6 +29,16 @@ static const vfs_volume_funcs initrd_funcs = { 0 // readdir }; +int initrd_strncmp(const char *a, const char *b, unsigned int n) +{ + for (unsigned int i = 0; i < n; i++) { + if (a[i] != b[i]) + return 1; + } + + return 0; +} + void initrd_init(void) { vfs_mount(&initrd_funcs, VFS_READONLY); @@ -40,7 +49,7 @@ void *initrd_open(const char *file) char *ptr; for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) { uint32_t len = *((uint32_t *)ptr); - if (!strncmp(file, ptr + 4, len)) { + if (!initrd_strncmp(file, ptr + 4, len)) { initrd_info *file = (initrd_info *)malloc( sizeof(initrd_info)); file->address = ptr + len + 8; diff --git a/src/kernel/gpio.c b/src/kernel/gpio.c index 1c81d11..0932e51 100644 --- a/src/kernel/gpio.c +++ b/src/kernel/gpio.c @@ -20,7 +20,7 @@ #include "gpio.h" -static const GPIO_TypeDef *gpio_ports[8] = { +static GPIO_TypeDef *gpio_ports[8] = { GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH }; diff --git a/src/kernel/startup_stm32l476xx.s b/src/kernel/startup_stm32l476xx.s index b26ced5..d60fa49 100644 --- a/src/kernel/startup_stm32l476xx.s +++ b/src/kernel/startup_stm32l476xx.s @@ -105,7 +105,7 @@ LoopFillZerobss: bcc FillZerobss /* Call static constructors */ - bl __libc_init_array + //bl __libc_init_array /* Call the application's entry point.*/ bl main diff --git a/src/kernel/task.c b/src/kernel/task.c index dfe50a7..c5e5e96 100644 --- a/src/kernel/task.c +++ b/src/kernel/task.c @@ -87,7 +87,7 @@ void task_sleep(uint32_t ms) pid_t task_getpid(void) { - return task_current->pid; + return task_current != 0 ? task_current->pid : 0; } pid_t task_waitpid(pid_t pid, int *wstatus, int options) diff --git a/src/kernel/vfs.c b/src/kernel/vfs.c index da1d641..6236691 100644 --- a/src/kernel/vfs.c +++ b/src/kernel/vfs.c @@ -43,12 +43,12 @@ void vfs_init(void) vfs_mount(&stdio_funcs, 0); // order is crucial - vfs_open("in", VFS_FILE_READ); - vfs_open("out", VFS_FILE_WRITE); - vfs_open("err", VFS_FILE_WRITE); + vfs_open(" in", VFS_FILE_READ); + vfs_open(" out", VFS_FILE_WRITE); + vfs_open(" err", VFS_FILE_WRITE); } -int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags) +int vfs_mount(const vfs_volume_funcs *funcs, uint32_t flags) { for (int i = 0; i < VFS_MAX_VOLS; i++) { if (!(vfs_volumes[i].flags && VFS_MOUNTED)) { @@ -64,10 +64,14 @@ int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags) int vfs_get_drive(const char *path) { // Validate parameters - if (path[0] == '\0' || path[1] == '\0' || path[1] != ':' || - path[2] == '\0' || path[2] != '/') + if (path[0] == '\0') return -1; + // Default to 'A' if no drive specified (A gives stdio) + if (path[1] == '\0' || path[1] != ':' || + path[2] == '\0' || path[2] != '/') + return 0; + // Find chosen drive int drive = -1; for (int i = 0; i < VFS_MAX_VOLS; i++) { @@ -103,11 +107,14 @@ int vfs_open(const char *path, uint32_t flags) if (file == -1) return -1; + void *fsinfo = vfs_volumes[drive].funcs->open(path + 3); + if (fsinfo == 0) + return -1; + vfs_files[file].flags = VFS_FILE_OPEN | flags; vfs_files[file].vol = drive; vfs_files[file].pid = task_getpid(); - vfs_files[file].fsinfo = - vfs_volumes[drive].funcs->open(path + 3); + vfs_files[file].fsinfo = fsinfo; return file; } diff --git a/src/kernel/vfs.h b/src/kernel/vfs.h index 29763a5..89213f9 100644 --- a/src/kernel/vfs.h +++ b/src/kernel/vfs.h @@ -22,7 +22,7 @@ typedef struct vfs_volume_funcs_t { typedef struct { uint32_t flags; - vfs_volume_funcs *funcs; + const vfs_volume_funcs *funcs; } vfs_volume; // Indicates an opened file @@ -43,7 +43,7 @@ typedef struct { void vfs_init(void); -int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags); +int vfs_mount(const vfs_volume_funcs *funcs, uint32_t flags); int vfs_open(const char *path, uint32_t flags); int vfs_close(int fd); uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer); diff --git a/src/pdclib/platform/stmos/include/syscalls.h b/src/pdclib/platform/stmos/include/syscalls.h index 2acb59c..b301b31 100644 --- a/src/pdclib/platform/stmos/include/syscalls.h +++ b/src/pdclib/platform/stmos/include/syscalls.h @@ -6,6 +6,11 @@ // // Task-related calls +#define WEXITSTATUS(s) ((s) & 0xFF) +#define WIFEXITED(s) ((s) & (1 << 8)) +#define WIFSIGNALED(s) ((s) & (1 << 9)) +#define WTERMSIG(s) ((s) & (1 << 10)) + void _exit(int code); int fork(void); @@ -35,7 +40,9 @@ unsigned int ticks(void); // Set if EOF has been reached #define VFS_EOF (1 << 3) +#ifndef EOF #define EOF (-1) +#endif // EOF struct dirent { char name[32]; diff --git a/src/user/user.c b/src/user/user.c index dd1aa54..b5ef789 100644 --- a/src/user/user.c +++ b/src/user/user.c @@ -19,10 +19,11 @@ void user_main(void) { gpio(GPIO_MODE, 5, OUTPUT); - int test = vfs_open("A:/hello", VFS_FILE_READ); + int test = vfs_open("B:/hello", VFS_FILE_READ); char *buf = malloc(20); int count = vfs_read(test, 20, (uint8_t *)buf); - (void)count; + buf[count] = '\0'; + vfs_close(test); // if (fork() == 0) { // while (1) {