| 
									
										
										
										
											2022-05-02 20:43:40 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-01 15:21:12 +08:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-10 13:50:04 +01:00
										 |  |  | #include <stdarg.h>
 | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-15 14:49:57 +02:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-10 13:50:04 +01:00
										 |  |  | #include <reent.h>
 | 
					
						
							|  |  |  | #include <sys/fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | #include "sdkconfig.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  | #include "esp_rom_uart.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 14:56:31 +02:00
										 |  |  | static int syscall_not_implemented(struct _reent *r, ...) | 
					
						
							| 
									
										
										
										
											2019-12-01 19:20:52 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-15 14:56:31 +02:00
										 |  |  |     __errno_r(r) = ENOSYS; | 
					
						
							| 
									
										
										
										
											2019-12-01 19:20:52 +01:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | static int syscall_not_implemented_aborts(void) | 
					
						
							| 
									
										
										
										
											2019-12-01 19:20:52 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     abort(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | ssize_t _write_r_console(struct _reent *r, int fd, const void * data, size_t size) | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  |     const char* cdata = (const char*) data; | 
					
						
							|  |  |  |     if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { | 
					
						
							|  |  |  |         for (size_t i = 0; i < size; ++i) { | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |             esp_rom_uart_tx_one_char(cdata[i]); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return size; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-15 14:56:31 +02:00
										 |  |  |     __errno_r(r) = EBADF; | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | ssize_t _read_r_console(struct _reent *r, int fd, void * data, size_t size) | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  |     char* cdata = (char*) data; | 
					
						
							|  |  |  |     if (fd == STDIN_FILENO) { | 
					
						
							|  |  |  |         size_t received; | 
					
						
							|  |  |  |         for (received = 0; received < size; ++received) { | 
					
						
							| 
									
										
										
										
											2020-07-13 21:33:23 +08:00
										 |  |  |             int status = esp_rom_uart_rx_one_char((uint8_t*) &cdata[received]); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  |             if (status != 0) { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-05-02 20:43:40 +02:00
										 |  |  |         if (received == 0) { | 
					
						
							|  |  |  |             errno = EWOULDBLOCK; | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  |         return received; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-15 14:56:31 +02:00
										 |  |  |     __errno_r(r) = EBADF; | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 14:49:57 +02:00
										 |  |  | static ssize_t _fstat_r_console(struct _reent *r, int fd, struct stat * st) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { | 
					
						
							|  |  |  |         memset(st, 0, sizeof(*st)); | 
					
						
							|  |  |  |         /* This needs to be set so that stdout and stderr are line buffered. */ | 
					
						
							|  |  |  |         st->st_mode = S_IFCHR; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     __errno_r(r) = EBADF; | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 13:55:12 +01:00
										 |  |  | static int _fsync_console(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { | 
					
						
							|  |  |  | #ifdef CONFIG_ESP_CONSOLE_UART
 | 
					
						
							|  |  |  |         esp_rom_uart_flush_tx(CONFIG_ESP_CONSOLE_UART_NUM); | 
					
						
							|  |  |  | #elif defined(CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
 | 
					
						
							|  |  |  |         esp_rom_uart_flush_tx(CONFIG_ESP_ROM_USB_SERIAL_DEVICE_NUM); | 
					
						
							|  |  |  | #elif defined(CONFIG_ESP_CONSOLE_USB_CDC)
 | 
					
						
							|  |  |  |         esp_rom_uart_flush_tx(CONFIG_ESP_ROM_USB_OTG_NUM); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     errno = EBADF; | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* The following weak definitions of syscalls will be used unless
 | 
					
						
							|  |  |  |  * another definition is provided. That definition may come from | 
					
						
							|  |  |  |  * VFS, LWIP, or the application. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size) | 
					
						
							|  |  |  |     __attribute__((weak,alias("_read_r_console"))); | 
					
						
							|  |  |  | ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size) | 
					
						
							|  |  |  |     __attribute__((weak,alias("_write_r_console"))); | 
					
						
							| 
									
										
										
										
											2021-09-15 14:49:57 +02:00
										 |  |  | int _fstat_r (struct _reent *r, int fd, struct stat *st) | 
					
						
							|  |  |  |     __attribute__((weak,alias("_fstat_r_console"))); | 
					
						
							| 
									
										
										
										
											2022-12-01 13:55:12 +01:00
										 |  |  | int fsync(int fd) | 
					
						
							|  |  |  |     __attribute__((weak,alias("_fsync_console"))); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* The aliases below are to "syscall_not_implemented", which
 | 
					
						
							|  |  |  |  * doesn't have the same signature as the original function. | 
					
						
							|  |  |  |  * Disable type mismatch warnings for this reason. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-09-10 19:23:42 +02:00
										 |  |  | #if defined(__GNUC__) && !defined(__clang__)
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | #pragma GCC diagnostic push
 | 
					
						
							|  |  |  | #pragma GCC diagnostic ignored "-Wattribute-alias"
 | 
					
						
							| 
									
										
										
										
											2021-09-10 19:23:42 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | int _open_r(struct _reent *r, const char * path, int flags, int mode) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _close_r(struct _reent *r, int fd) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _fcntl_r(struct _reent *r, int fd, int cmd, int arg) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _stat_r(struct _reent *r, const char * path, struct stat * st) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _link_r(struct _reent *r, const char* n1, const char* n2) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _unlink_r(struct _reent *r, const char *path) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _rename_r(struct _reent *r, const char *src, const char *dst) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							| 
									
										
										
										
											2020-11-06 15:00:07 +11:00
										 |  |  | int _isatty_r(struct _reent *r, int fd) | 
					
						
							|  |  |  |     __attribute__((weak,alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* These functions are not expected to be overridden */ | 
					
						
							|  |  |  | int _system_r(struct _reent *r, const char *str) | 
					
						
							|  |  |  |     __attribute__((alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int raise(int sig) | 
					
						
							|  |  |  |     __attribute__((alias("syscall_not_implemented_aborts"))); | 
					
						
							|  |  |  | int _raise_r(struct _reent *r, int sig) | 
					
						
							|  |  |  |     __attribute__((alias("syscall_not_implemented_aborts"))); | 
					
						
							|  |  |  | void* _sbrk_r(struct _reent *r, ptrdiff_t sz) | 
					
						
							|  |  |  |     __attribute__((alias("syscall_not_implemented_aborts"))); | 
					
						
							|  |  |  | int _getpid_r(struct _reent *r) | 
					
						
							|  |  |  |     __attribute__((alias("syscall_not_implemented"))); | 
					
						
							|  |  |  | int _kill_r(struct _reent *r, int pid, int sig) | 
					
						
							|  |  |  |     __attribute__((alias("syscall_not_implemented"))); | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | void _exit(int __status) | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  |     __attribute__((alias("syscall_not_implemented_aborts"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 19:23:42 +02:00
										 |  |  | #if defined(__GNUC__) && !defined(__clang__)
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:40:18 +01:00
										 |  |  | #pragma GCC diagnostic pop
 | 
					
						
							| 
									
										
										
										
											2021-09-10 19:23:42 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-25 22:12:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 14:56:31 +02:00
										 |  |  | /* Similar to syscall_not_implemented, but not taking struct _reent argument */ | 
					
						
							|  |  |  | int system(const char* str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     errno = ENOSYS; | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 13:50:04 +01:00
										 |  |  | /* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */ | 
					
						
							|  |  |  | int fcntl(int fd, int cmd, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     va_list args; | 
					
						
							|  |  |  |     va_start(args, cmd); | 
					
						
							|  |  |  |     int arg = va_arg(args, int); | 
					
						
							|  |  |  |     va_end(args); | 
					
						
							|  |  |  |     struct _reent* r = __getreent(); | 
					
						
							|  |  |  |     return _fcntl_r(r, fd, cmd, arg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 15:21:12 +08:00
										 |  |  | /* No-op function, used to force linking this file,
 | 
					
						
							|  |  |  |    instead of the syscalls implementation from libgloss. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | void newlib_include_syscalls_impl(void) | 
					
						
							| 
									
										
										
										
											2019-04-01 15:21:12 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } |