diff --git a/components/libc/compilers/armlibc/libc.h b/components/libc/compilers/armlibc/libc.h index aecf8cdc50a158b49c67f3b0685909d4a67e7f11..5c1613728e3040033f22b7c15c17aba8f4429c21 100644 --- a/components/libc/compilers/armlibc/libc.h +++ b/components/libc/compilers/armlibc/libc.h @@ -10,17 +10,14 @@ #ifndef __RTT_LIBC_H__ #define __RTT_LIBC_H__ -#include - #ifdef __cplusplus extern "C" { #endif -int libc_system_init(void); -int libc_stdio_set_console(const char* device_name, int mode); +int libc_system_init(void); int libc_stdio_get_console(void); -int libc_stdio_read(void* buffer, size_t size); -int libc_stdio_write(const void* buffer, size_t size); +int libc_stdio_set_console(const char* device_name, int mode); + #ifdef __cplusplus } #endif diff --git a/components/libc/compilers/armlibc/stdio.c b/components/libc/compilers/armlibc/stdio.c index c786c78aa53ff3424423368bd8e772209985b6e5..7dd9dbfe3f84471a82fccc08fd64a12745ad6485 100644 --- a/components/libc/compilers/armlibc/stdio.c +++ b/components/libc/compilers/armlibc/stdio.c @@ -21,6 +21,7 @@ #define STDIO_DEVICE_NAME_MAX 32 static int std_fd = -1; + int libc_stdio_set_console(const char* device_name, int mode) { int fd; @@ -47,29 +48,4 @@ int libc_stdio_get_console(void) return std_fd; } -int libc_stdio_read(void *buffer, size_t size) -{ - if (std_fd >= 0) - { - return read(std_fd, buffer, size); - } - else - { - rt_kprintf("Illegal stdio input!\n"); - return 0; - } -} - -int libc_stdio_write(const void *buffer, size_t size) -{ - if (std_fd >= 0) - { - return write(std_fd, buffer, size); - } - else - { - rt_kprintf("Illegal stdio output!\n"); - return size; - } -} #endif diff --git a/components/libc/compilers/armlibc/syscalls.c b/components/libc/compilers/armlibc/syscalls.c index be6041a6630b69f1d1bfc0219de2067fc3ecc6e9..7d8ec2e8b44e5e0501aa2daf9cf5512f5a47e0c6 100644 --- a/components/libc/compilers/armlibc/syscalls.c +++ b/components/libc/compilers/armlibc/syscalls.c @@ -25,6 +25,10 @@ #include #endif +#define DBG_TAG "armlibc.syscalls" +#define DBG_LVL DBG_INFO +#include + #ifdef __CLANG_ARM __asm(".global __use_no_semihosting\n\t"); #else @@ -146,16 +150,22 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) if (fh == STDIN) { #ifdef RT_USING_POSIX - size = libc_stdio_read(buf, len); + if (libc_stdio_get_console() < 0) + { + LOG_W("Do not invoke standard output before initializing libc"); + return 0; + } + size = read(STDIN_FILENO, buf, len); return len - size; #else /* no stdin */ return -1; #endif } - - if ((fh == STDOUT) || (fh == STDERR)) + else if ((fh == STDOUT) || (fh == STDERR)) + { return -1; + } #ifndef RT_USING_DFS return 0; @@ -185,7 +195,12 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) return 0; #else #ifdef RT_USING_POSIX - size = libc_stdio_write(buf, len); + if (libc_stdio_get_console() < 0) + { + LOG_W("Do not invoke standard input before initializing libc"); + return 0; + } + size = write(STDOUT_FILENO, buf, len); return len - size; #else if (rt_console_get_device()) @@ -198,8 +213,10 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) #endif #endif } - - if (fh == STDIN) return -1; + else if (fh == STDIN) + { + return -1; + } #ifndef RT_USING_DFS return 0; diff --git a/components/libc/compilers/dlib/libc.h b/components/libc/compilers/dlib/libc.h index fc3cafca7864ea8135f6cdcdadfeeb64202083f6..9643c1161f31cd3825bf9199f4800f6dae6bc47f 100644 --- a/components/libc/compilers/dlib/libc.h +++ b/components/libc/compilers/dlib/libc.h @@ -11,17 +11,14 @@ #ifndef __RTT_LIBC_H__ #define __RTT_LIBC_H__ -#include - #ifdef __cplusplus extern "C" { #endif -int libc_system_init(void); -int libc_stdio_set_console(const char* device_name, int mode); +int libc_system_init(void); int libc_stdio_get_console(void); -int libc_stdio_read(void* buffer, size_t size); -int libc_stdio_write(const void* buffer, size_t size); +int libc_stdio_set_console(const char* device_name, int mode); + #ifdef __cplusplus } #endif diff --git a/components/libc/compilers/dlib/stdio.c b/components/libc/compilers/dlib/stdio.c index c11157d7593c61c74c2223f192b53dd85f6950c9..ed3f5dfef9832d2d28f1611ca36819e802e996f5 100644 --- a/components/libc/compilers/dlib/stdio.c +++ b/components/libc/compilers/dlib/stdio.c @@ -46,13 +46,4 @@ int libc_stdio_get_console(void) { return std_fd; } -int libc_stdio_read(void *buffer, size_t size) -{ - return read(std_fd, buffer, size); -} - -int libc_stdio_write(const void *buffer, size_t size) -{ - return write(std_fd, buffer, size); -} #endif diff --git a/components/libc/compilers/dlib/syscall_read.c b/components/libc/compilers/dlib/syscall_read.c index afb661bd936935f17dea4f30da9453ca486d220e..ae980be6ca08d132d3fca24129829e167816b7ce 100644 --- a/components/libc/compilers/dlib/syscall_read.c +++ b/components/libc/compilers/dlib/syscall_read.c @@ -15,6 +15,10 @@ #include #include "libc.h" +#define DBG_TAG "dlib.syscall_read" +#define DBG_LVL DBG_INFO +#include + #pragma module_name = "?__read" size_t __read(int handle, unsigned char *buf, size_t len) { @@ -25,14 +29,20 @@ size_t __read(int handle, unsigned char *buf, size_t len) if (handle == _LLIO_STDIN) { #ifdef RT_USING_POSIX - return libc_stdio_read(buf, len); + if (libc_stdio_get_console() < 0) + { + LOG_W("Do not invoke standard input before initializing libc"); + return 0; + } + return read(STDIN_FILENO, buf, len); #else return _LLIO_ERROR; #endif } - - if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) + else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) + { return _LLIO_ERROR; + } #ifndef RT_USING_DFS return _LLIO_ERROR; diff --git a/components/libc/compilers/dlib/syscall_write.c b/components/libc/compilers/dlib/syscall_write.c index de464530b8b0e5e84e97468e7d53ef7acafcf04e..ffee51882d405bc5fd22dbd499c318c4dae3e21e 100644 --- a/components/libc/compilers/dlib/syscall_write.c +++ b/components/libc/compilers/dlib/syscall_write.c @@ -15,6 +15,10 @@ #include #include "libc.h" +#define DBG_TAG "dlib.syscall_write" +#define DBG_LVL DBG_INFO +#include + #pragma module_name = "?__write" size_t __write(int handle, const unsigned char *buf, size_t len) @@ -30,19 +34,29 @@ size_t __write(int handle, const unsigned char *buf, size_t len) #else #ifdef RT_USING_POSIX - return libc_stdio_write((void*)buf, len); + if (libc_stdio_get_console() < 0) + { + LOG_W("Do not invoke standard output before initializing libc"); + return 0; + } + return write(STDOUT_FILENO, (void*)buf, len); #else rt_device_t console_device; console_device = rt_console_get_device(); - if (console_device != 0) rt_device_write(console_device, 0, buf, len); + if (console_device != 0) + { + rt_device_write(console_device, 0, buf, len); + } return len; #endif #endif } - - if (handle == _LLIO_STDIN) return _LLIO_ERROR; + else if (handle == _LLIO_STDIN) + { + return _LLIO_ERROR; + } #ifndef RT_USING_DFS return _LLIO_ERROR; diff --git a/components/libc/compilers/newlib/libc.h b/components/libc/compilers/newlib/libc.h index c1f76d7605a9f186a89b7d19c60a9a2ef2742f1a..c470698aa600f90336e41ad8f199918afeed748d 100644 --- a/components/libc/compilers/newlib/libc.h +++ b/components/libc/compilers/newlib/libc.h @@ -18,8 +18,8 @@ extern "C" { #endif int libc_system_init(void); -int libc_stdio_set_console(const char* device_name, int mode); int libc_stdio_get_console(void); +int libc_stdio_set_console(const char* device_name, int mode); #ifdef __cplusplus } diff --git a/components/libc/compilers/newlib/syscalls.c b/components/libc/compilers/newlib/syscalls.c index 29ffd41ac1a2f9551fd1e32f5e3b13df1ac22d28..4c1dc82ad376313b69e20b59d9009dcf0e6bf673 100644 --- a/components/libc/compilers/newlib/syscalls.c +++ b/components/libc/compilers/newlib/syscalls.c @@ -11,8 +11,10 @@ * 2020-02-24 Meco Man fix bug of _isatty_r() */ +#include #include #include +#include #include #define DBG_TAG "newlib.syscalls" @@ -90,6 +92,10 @@ void __libc_init_array(void) #include #endif +#define DBG_TAG "newlib.syscalls" +#define DBG_LVL DBG_INFO +#include + /* Reentrant versions of system calls. */ #ifndef _REENT_ONLY @@ -219,7 +225,11 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) return -1; #else _ssize_t rc; - + if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO) + { + LOG_W("Do not invoke standard input before initializing libc"); + return 0; + } rc = read(fd, buf, nbytes); return rc; #endif @@ -275,7 +285,7 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { #ifndef RT_USING_DFS #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) - if (fileno(stdout) == fd) + if (STDOUT_FILENO == fd) { rt_device_t console; @@ -291,7 +301,11 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) #endif /*RT_USING_DEVICE*/ #else _ssize_t rc; - + if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO) + { + LOG_W("Do not invoke standard output before initializing libc"); + return 0; + } rc = write(fd, buf, nbytes); return rc; #endif