From 9aaf4286207330cafbfe61c3b272bc27af0d9f5d Mon Sep 17 00:00:00 2001 From: gabriel Date: Tue, 30 Apr 2024 08:48:44 +0200 Subject: [PATCH] further semihosting support --- libwrap/semihosting/semihosting.c | 43 ++++++++++++++++++++----------- libwrap/semihosting/semihosting.h | 3 +++ libwrap/sys/_exit.c | 4 +-- libwrap/sys/isatty.c | 7 +++-- libwrap/sys/lseek.c | 2 +- libwrap/sys/printf.c | 6 +---- libwrap/sys/puts.c | 4 ++- libwrap/sys/read.c | 2 ++ libwrap/sys/write.c | 18 ++++++++++--- 9 files changed, 57 insertions(+), 32 deletions(-) diff --git a/libwrap/semihosting/semihosting.c b/libwrap/semihosting/semihosting.c index 2492d4c..772a6ed 100644 --- a/libwrap/semihosting/semihosting.c +++ b/libwrap/semihosting/semihosting.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "semihosting.h" @@ -33,7 +34,7 @@ #define RISCV_SEMIHOSTING_CALL_NUMBER 7 -/*typedef struct { +typedef struct { char* str; int mode; size_t length; @@ -48,9 +49,13 @@ typedef struct { typedef struct { char* path; - int len; + size_t len; } RemoveVector; -*/ + +typedef struct{ + int fd; + off_t pos; +} SeekVector; static inline int __attribute__ ((always_inline)) call_host(int reason, void* arg) { #if 1 @@ -86,17 +91,22 @@ static inline int __attribute__ ((always_inline)) call_host(int reason, void* ar #endif } -/* int sh_errno(void) { return call_host(SEMIHOSTING_SYS_ERRNO, (void*)NULL); } int sh_time(void) { return call_host(SEMIHOSTING_SYS_TIME, (void*)NULL); -} +} -void sh_seek(int file_handle, int pos) { - int vec[2] = {file_handle, pos}; +int sh_remove(char* path) { + size_t len = strlen(path); + RemoveVector vec = {path, len}; + return call_host(SEMIHOSTING_SYS_REMOVE, &vec); +} + +void sh_seek(int file_handle, off_t pos) { + SeekVector vec = {file_handle, pos}; call_host(SEMIHOSTING_SYS_SEEK, &vec); return; } @@ -135,13 +145,12 @@ int sh_iserror(int num) { int sh_istty(int file_handle) { return call_host(SEMIHOSTING_SYS_ISTTY, file_handle); } - +/* int sh_remove(char* path) { - int len = strlen(path); + size_t len = strlen(path); RemoveVector vec = {path, len}; return call_host(SEMIHOSTING_SYS_REMOVE, &vec); - -} +}*/ void sh_rename(char* old, char* new) { int old_len = strlen(old); @@ -175,18 +184,22 @@ int sh_open(char* str, int mode) { //int length = 44; size_t length = strlen(str); OpenVector vec = {str, mode, length}; - int i = call_host(SEMIHOSTING_SYS_OPEN, &vec); - return i; + return call_host(SEMIHOSTING_SYS_OPEN, &vec); } void sh_read(char* buf, int file_handle, size_t length) { OpenVector vec = {buf, file_handle, length}; int i = call_host(SEMIHOSTING_SYS_READ, &vec); return i; -} */ +} int sh_clock(void) { int clock = call_host(SEMIHOSTING_SYS_CLOCK, (void*)NULL); return clock; -} \ No newline at end of file +} + +/* +void sh_write(char* str, int file_handle) { + return; +}*/ \ No newline at end of file diff --git a/libwrap/semihosting/semihosting.h b/libwrap/semihosting/semihosting.h index 57d80f4..e02b01a 100644 --- a/libwrap/semihosting/semihosting.h +++ b/libwrap/semihosting/semihosting.h @@ -1,8 +1,11 @@ #ifndef SEMIHOSTING_H #define SEMIHOSTING_H +#include +#include //int32_t trace_write(const char* buf, uint32_t nbyte); +void sh_seek(int, off_t); void sh_write0(const char* buf); void sh_writec(char c); char sh_readc(void); diff --git a/libwrap/sys/_exit.c b/libwrap/sys/_exit.c index ce3d487..0dac450 100644 --- a/libwrap/sys/_exit.c +++ b/libwrap/sys/_exit.c @@ -17,10 +17,10 @@ void write_hex(int fd, uint32_t hex); void __wrap_exit(int code) { -#if defined(SEMIHOSTING) +/*#if defined(SEMIHOSTING) sh_exit(); return; -#endif +#endif*/ //volatile uint32_t* leds = (uint32_t*) (GPIO_BASE_ADDR + GPIO_OUT_OFFSET); const char message[] = "\nProgam has exited with code:"; //*leds = (~(code)); diff --git a/libwrap/sys/isatty.c b/libwrap/sys/isatty.c index 8d526f1..44a805a 100644 --- a/libwrap/sys/isatty.c +++ b/libwrap/sys/isatty.c @@ -1,11 +1,10 @@ /* See LICENSE of license details. */ -#include -#include "weak_under_alias.h" #include "semihosting.h" +#include "weak_under_alias.h" +#include -int __wrap_isatty(int fd) -{ +int __wrap_isatty(int fd) { #if defined(SEMIHOSTING) int i = sh_istty(fd); return i; diff --git a/libwrap/sys/lseek.c b/libwrap/sys/lseek.c index 4d8544a..f7e629c 100644 --- a/libwrap/sys/lseek.c +++ b/libwrap/sys/lseek.c @@ -12,7 +12,7 @@ off_t __wrap_lseek(int fd, off_t ptr, int dir) #if defined(SEMIHOSTING) if(sh_istty(fd)) return 0; - sh_seek(); + sh_seek(fd, ptr); return ptr; #endif if (isatty(fd)) diff --git a/libwrap/sys/printf.c b/libwrap/sys/printf.c index 3a74305..139116f 100644 --- a/libwrap/sys/printf.c +++ b/libwrap/sys/printf.c @@ -7,7 +7,7 @@ #include #include #include -#include "semihosting.h" +//#include "semihosting.h" #undef putchar int putchar(int ch) @@ -115,11 +115,7 @@ static void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt if (ch == '\0') return; fmt++; -#if defined(SEMIHOSTING) - sh_writec(ch); -#else putch(ch, putdat); -#endif } fmt++; diff --git a/libwrap/sys/puts.c b/libwrap/sys/puts.c index 7469a46..bb07468 100644 --- a/libwrap/sys/puts.c +++ b/libwrap/sys/puts.c @@ -5,6 +5,8 @@ #include #include +#include "../../include/tgc-vp/devices/uart.h" +#include "../../env/tgc-vp/platform.h" #include "platform.h" #include "stub.h" #include "weak_under_alias.h" @@ -15,7 +17,7 @@ int __wrap_puts(const char *s) #if defined(SEMIHOSTING) sh_write0(s); return 0; -#endif +#endif while (*s != '\0') { #if defined(BOARD_ehrenberg) while (uart_get_tx_free(uart)==0) ; diff --git a/libwrap/sys/read.c b/libwrap/sys/read.c index aaf98ce..6fb9469 100644 --- a/libwrap/sys/read.c +++ b/libwrap/sys/read.c @@ -7,6 +7,8 @@ #if defined(BOARD_ehrenberg) #include "platform.h" #endif +#include "../../include/tgc-vp/devices/uart.h" +#include "/scratch/gabriel/repos/Firmwares/bare-metal-bsp/env/tgc-vp/platform.h" #include "platform.h" #include "stub.h" #include "weak_under_alias.h" diff --git a/libwrap/sys/write.c b/libwrap/sys/write.c index 192c355..96b3013 100644 --- a/libwrap/sys/write.c +++ b/libwrap/sys/write.c @@ -5,6 +5,8 @@ #include #include +#include "../../include/tgc-vp/devices/uart.h" +#include "../../env/tgc-vp/platform.h" #include "platform.h" #include "stub.h" #include "weak_under_alias.h" @@ -12,11 +14,19 @@ ssize_t __wrap_write(int fd, const void* ptr, size_t len) { - const uint8_t * current = (const uint8_t *)ptr; + const uint8_t * current = (const uint8_t *)ptr; #if defined(SEMIHOSTING) - sh_write(current, fd); - return len; -#endif + if(isatty(fd)) { + for (size_t jj = 0; jj < len; jj++) { + sh_writec(current[jj]); + } + return len; + } else { + sh_write(current, fd); + return len; + } + //return len; +#endif if (isatty(fd)) { for (size_t jj = 0; jj < len; jj++) { #if defined(BOARD_ehrenberg)