diff --git a/libwrap/CMakeLists.txt b/libwrap/CMakeLists.txt index 7af5136..dc3796a 100644 --- a/libwrap/CMakeLists.txt +++ b/libwrap/CMakeLists.txt @@ -27,6 +27,7 @@ SET(LIBWRAP_SRCS ${LIBWRAP_DIR}/sys/_exit.c ${LIBWRAP_DIR}/misc/write_hex.c ${LIBWRAP_DIR}/sys/printf.c + ${LIBWRAP_DIR}/sys/puts.c ) IF(${SEMIHOSTING}) SET(LIBWRAP_SRCS ${LIBWRAP_SRCS} ${LIBWRAP_DIR}/semihosting/semihosting.c ${LIBWRAP_DIR}/semihosting/trap.c) @@ -49,6 +50,6 @@ FOREACH(SYM ${LIBWRAP_SYMS}) LIST(APPEND WRAP_LDFLAGS "-Wl,--wrap=${SYM}") ENDFOREACH() -SET(LIBWRAP_TGC_LDFLAGS ${WRAP_LDFLAGS} "-Wl,--start-group" "-Wl,--end-group") +SET(LIBWRAP_TGC_LDFLAGS ${WRAP_LDFLAGS} "-Wl,--start-group" "-Wl,--end-group" "-L. -lLIBWRAP_TGC") ENDIF(NOT DEFINED _MK_LIBWRAP) diff --git a/libwrap/semihosting/semihosting.c b/libwrap/semihosting/semihosting.c index 5246a11..2492d4c 100644 --- a/libwrap/semihosting/semihosting.c +++ b/libwrap/semihosting/semihosting.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "semihosting.h" @@ -25,12 +26,31 @@ #define SEMIHOSTING_SYS_GET_CMDLINE 0x15 #define SEMIHOSTING_SYS_HEAPINFO 0x16 #define SEMIHOSTING_EnterSVC 0x17 -#define SEMIHOSTING_ReportException 0x18 +#define SEMIHOSTING_SYS_EXIT 0x18 +#define SEMIHOSTING_SYS_EXIT_EXTENDED 0x20 #define SEMIHOSTING_SYS_ELAPSED 0x30 #define SEMIHOSTING_SYS_TICKFREQ 0x31 #define RISCV_SEMIHOSTING_CALL_NUMBER 7 +/*typedef struct { + char* str; + int mode; + size_t length; +} OpenVector; + +typedef struct { + char* old; + int old_len; + char* new; + int new_len; +} RenameVector; + +typedef struct { + char* path; + int len; +} RemoveVector; +*/ static inline int __attribute__ ((always_inline)) call_host(int reason, void* arg) { #if 1 @@ -66,6 +86,71 @@ 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}; + call_host(SEMIHOSTING_SYS_SEEK, &vec); + return; +} + +void sh_write(char* str, int file_handle) { + size_t length = strlen(str); + OpenVector vec = {str, file_handle, length}; + call_host(SEMIHOSTING_SYS_WRITE, &vec); + return; +} + +int sh_close(int file_handle) { + return call_host(SEMIHOSTING_SYS_CLOSE, file_handle); +} + +void sh_exit(void) { + call_host(SEMIHOSTING_SYS_EXIT, (void*)NULL); + return; +} + +void sh_exit_extended(void) { + call_host(SEMIHOSTING_SYS_EXIT_EXTENDED, (void*)NULL); + return; +} + +int sh_flen(int file_handle) { + return call_host(SEMIHOSTING_SYS_FLEN, file_handle); + +} + +int sh_iserror(int num) { + return call_host(SEMIHOSTING_SYS_ISERROR, num); + +} + +int sh_istty(int file_handle) { + return call_host(SEMIHOSTING_SYS_ISTTY, file_handle); +} + +int sh_remove(char* path) { + int 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); + int new_len = strlen(new); + RenameVector vec = {old, old_len, new, new_len}; + call_host(SEMIHOSTING_SYS_RENAME, &vec); + return; +} + void sh_write0(const char* buf) { // Print zero-terminated string @@ -82,9 +167,26 @@ char sh_readc(void) { // Read character from keyboard. (Blocking operation!) char c = call_host(SEMIHOSTING_SYS_READC, (void*)NULL); + return c; +} - if (sh_missing_host) - return 0; - else - return c; +int sh_open(char* str, int mode) { + //mode = 0; + //int length = 44; + size_t length = strlen(str); + OpenVector vec = {str, mode, length}; + int i = call_host(SEMIHOSTING_SYS_OPEN, &vec); + return i; +} + +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 diff --git a/libwrap/semihosting/semihosting.h b/libwrap/semihosting/semihosting.h index f5b0317..57d80f4 100644 --- a/libwrap/semihosting/semihosting.h +++ b/libwrap/semihosting/semihosting.h @@ -6,6 +6,20 @@ void sh_write0(const char* buf); void sh_writec(char c); char sh_readc(void); +int sh_clock(void); +void sh_read(char*, int, size_t); +void sh_write(char*, int); +int sh_open(char*, int); +void sh_rename(char*, char*); +int sh_remove(char*); +int sh_istty(int); +int sh_iserror(int); +int sh_flen(int); +void sh_exit(void); +void sh_exit_extended(void); +int sh_close(int); +int sh_time(void); +int sh_errno(void); int getchar(void); diff --git a/libwrap/semihosting/trap.c b/libwrap/semihosting/trap.c index 122dae8..7b0c8ee 100644 --- a/libwrap/semihosting/trap.c +++ b/libwrap/semihosting/trap.c @@ -13,7 +13,7 @@ int sh_missing_host = 0; void trap() -{ +{ //ToDo: Check why macro CSR_MEPC and others are not resolved uint32_t mepc = read_csr(0x341); // Address of trap uint32_t mtval = read_csr(0x343); // Instruction value of trap uint32_t mcause = read_csr(0x342); // Reason for the trap diff --git a/libwrap/sys/_exit.c b/libwrap/sys/_exit.c index 19d3a58..ce3d487 100644 --- a/libwrap/sys/_exit.c +++ b/libwrap/sys/_exit.c @@ -3,6 +3,7 @@ #include #include "platform.h" #include "weak_under_alias.h" +#include "semihosting.h" #if defined(BOARD_hifive1) static volatile uint32_t tohost; @@ -16,6 +17,10 @@ void write_hex(int fd, uint32_t hex); void __wrap_exit(int code) { +#if defined(SEMIHOSTING) + sh_exit(); + return; +#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/close.c b/libwrap/sys/close.c index 5f047c8..1af2577 100644 --- a/libwrap/sys/close.c +++ b/libwrap/sys/close.c @@ -3,9 +3,14 @@ #include #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" int __wrap_close(int fd) { +#if defined(SEMIHOTING) + int i = sh_close(fd); + return i; +#endif return _stub(EBADF); } diff --git a/libwrap/sys/isatty.c b/libwrap/sys/isatty.c index 7bb82ab..8d526f1 100644 --- a/libwrap/sys/isatty.c +++ b/libwrap/sys/isatty.c @@ -2,9 +2,14 @@ #include #include "weak_under_alias.h" +#include "semihosting.h" int __wrap_isatty(int fd) { +#if defined(SEMIHOSTING) + int i = sh_istty(fd); + return i; +#endif if (fd == STDOUT_FILENO || fd == STDERR_FILENO) return 1; diff --git a/libwrap/sys/lseek.c b/libwrap/sys/lseek.c index 74488c7..4d8544a 100644 --- a/libwrap/sys/lseek.c +++ b/libwrap/sys/lseek.c @@ -5,9 +5,16 @@ #include #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" off_t __wrap_lseek(int fd, off_t ptr, int dir) { +#if defined(SEMIHOSTING) + if(sh_istty(fd)) + return 0; + sh_seek(); + return ptr; +#endif if (isatty(fd)) return 0; diff --git a/libwrap/sys/open.c b/libwrap/sys/open.c index c61415a..2ecad62 100644 --- a/libwrap/sys/open.c +++ b/libwrap/sys/open.c @@ -3,9 +3,14 @@ #include #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" int __wrap_open(const char* name, int flags, int mode) { +#if defined(SEMIHOSTING) + int fd = sh_open(name, mode); + return fd; +#endif return _stub(ENOENT); } weak_under_alias(open); diff --git a/libwrap/sys/printf.c b/libwrap/sys/printf.c index ee94a79..3a74305 100644 --- a/libwrap/sys/printf.c +++ b/libwrap/sys/printf.c @@ -7,6 +7,7 @@ #include #include #include +#include "semihosting.h" #undef putchar int putchar(int ch) @@ -114,7 +115,11 @@ 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 fd82e4f..7469a46 100644 --- a/libwrap/sys/puts.c +++ b/libwrap/sys/puts.c @@ -8,9 +8,14 @@ #include "platform.h" #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" int __wrap_puts(const char *s) { +#if defined(SEMIHOSTING) + sh_write0(s); + return 0; +#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 c6e1a0d..aaf98ce 100644 --- a/libwrap/sys/read.c +++ b/libwrap/sys/read.c @@ -10,10 +10,15 @@ #include "platform.h" #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" ssize_t __wrap_read(int fd, void* ptr, size_t len) { uint8_t * current = (uint8_t *)ptr; +#if defined(SEMIHOSTING) + sh_read(current, fd, len); + return; +#endif #if defined(BOARD_hifive1) volatile uint32_t * uart_rx = (uint32_t *)(UART0_CTRL_ADDR + UART_REG_RXFIFO); volatile uint8_t * uart_rx_cnt = (uint8_t *)(UART0_CTRL_ADDR + UART_REG_RXCTRL + 2); diff --git a/libwrap/sys/unlink.c b/libwrap/sys/unlink.c index 09f4da7..5355a66 100644 --- a/libwrap/sys/unlink.c +++ b/libwrap/sys/unlink.c @@ -3,9 +3,13 @@ #include #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" int __wrap_unlink(const char* name) { +#if defined(SEMIHOSTING) + return sh_remove(name); +#endif return _stub(ENOENT); } weak_under_alias(unlink); diff --git a/libwrap/sys/write.c b/libwrap/sys/write.c index 46e0788..192c355 100644 --- a/libwrap/sys/write.c +++ b/libwrap/sys/write.c @@ -8,10 +8,15 @@ #include "platform.h" #include "stub.h" #include "weak_under_alias.h" +#include "semihosting.h" ssize_t __wrap_write(int fd, const void* ptr, size_t len) { 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++) { #if defined(BOARD_ehrenberg)