syscalls.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdbool.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <reent.h>
  21. #include <sys/fcntl.h>
  22. #include "sdkconfig.h"
  23. #if CONFIG_IDF_TARGET_ESP32
  24. #include "esp32/rom/uart.h"
  25. #elif CONFIG_IDF_TARGET_ESP32S2
  26. #include "esp32s2/rom/uart.h"
  27. #endif
  28. static int syscall_not_implemented(void)
  29. {
  30. errno = ENOSYS;
  31. return -1;
  32. }
  33. static int syscall_not_implemented_aborts(void)
  34. {
  35. abort();
  36. }
  37. ssize_t _write_r_console(struct _reent *r, int fd, const void * data, size_t size)
  38. {
  39. const char* cdata = (const char*) data;
  40. if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
  41. for (size_t i = 0; i < size; ++i) {
  42. uart_tx_one_char(cdata[i]);
  43. }
  44. return size;
  45. }
  46. errno = EBADF;
  47. return -1;
  48. }
  49. ssize_t _read_r_console(struct _reent *r, int fd, void * data, size_t size)
  50. {
  51. char* cdata = (char*) data;
  52. if (fd == STDIN_FILENO) {
  53. size_t received;
  54. for (received = 0; received < size; ++received) {
  55. int status = uart_rx_one_char((uint8_t*) &cdata[received]);
  56. if (status != 0) {
  57. break;
  58. }
  59. }
  60. return received;
  61. }
  62. errno = EBADF;
  63. return -1;
  64. }
  65. /* The following weak definitions of syscalls will be used unless
  66. * another definition is provided. That definition may come from
  67. * VFS, LWIP, or the application.
  68. */
  69. ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size)
  70. __attribute__((weak,alias("_read_r_console")));
  71. ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size)
  72. __attribute__((weak,alias("_write_r_console")));
  73. /* The aliases below are to "syscall_not_implemented", which
  74. * doesn't have the same signature as the original function.
  75. * Disable type mismatch warnings for this reason.
  76. */
  77. #pragma GCC diagnostic push
  78. #pragma GCC diagnostic ignored "-Wattribute-alias"
  79. int _open_r(struct _reent *r, const char * path, int flags, int mode)
  80. __attribute__((weak,alias("syscall_not_implemented")));
  81. int _close_r(struct _reent *r, int fd)
  82. __attribute__((weak,alias("syscall_not_implemented")));
  83. off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode)
  84. __attribute__((weak,alias("syscall_not_implemented")));
  85. int _fcntl_r(struct _reent *r, int fd, int cmd, int arg)
  86. __attribute__((weak,alias("syscall_not_implemented")));
  87. int _fstat_r(struct _reent *r, int fd, struct stat * st)
  88. __attribute__((weak,alias("syscall_not_implemented")));
  89. int _stat_r(struct _reent *r, const char * path, struct stat * st)
  90. __attribute__((weak,alias("syscall_not_implemented")));
  91. int _link_r(struct _reent *r, const char* n1, const char* n2)
  92. __attribute__((weak,alias("syscall_not_implemented")));
  93. int _unlink_r(struct _reent *r, const char *path)
  94. __attribute__((weak,alias("syscall_not_implemented")));
  95. int _rename_r(struct _reent *r, const char *src, const char *dst)
  96. __attribute__((weak,alias("syscall_not_implemented")));
  97. /* These functions are not expected to be overridden */
  98. int system(const char* str)
  99. __attribute__((alias("syscall_not_implemented")));
  100. int _system_r(struct _reent *r, const char *str)
  101. __attribute__((alias("syscall_not_implemented")));
  102. int raise(int sig)
  103. __attribute__((alias("syscall_not_implemented_aborts")));
  104. int _raise_r(struct _reent *r, int sig)
  105. __attribute__((alias("syscall_not_implemented_aborts")));
  106. void* _sbrk_r(struct _reent *r, ptrdiff_t sz)
  107. __attribute__((alias("syscall_not_implemented_aborts")));
  108. int _getpid_r(struct _reent *r)
  109. __attribute__((alias("syscall_not_implemented")));
  110. int _kill_r(struct _reent *r, int pid, int sig)
  111. __attribute__((alias("syscall_not_implemented")));
  112. void _exit(int __status)
  113. __attribute__((alias("syscall_not_implemented_aborts")));
  114. #pragma GCC diagnostic pop
  115. /* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */
  116. int fcntl(int fd, int cmd, ...)
  117. {
  118. va_list args;
  119. va_start(args, cmd);
  120. int arg = va_arg(args, int);
  121. va_end(args);
  122. struct _reent* r = __getreent();
  123. return _fcntl_r(r, fd, cmd, arg);
  124. }
  125. /* No-op function, used to force linking this file,
  126. instead of the syscalls implementation from libgloss.
  127. */
  128. void newlib_include_syscalls_impl(void)
  129. {
  130. }