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 8b9d8a4..7bf8246 100755 Binary files a/initrd/test and b/initrd/test differ 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) {