syscalls.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "esp_rom_uart.h"
  24. static int syscall_not_implemented(void)
  25. {
  26. errno = ENOSYS;
  27. return -1;
  28. }
  29. static int syscall_not_implemented_aborts(void)
  30. {
  31. abort();
  32. }
  33. ssize_t _write_r_console(struct _reent *r, int fd, const void * data, size_t size)
  34. {
  35. const char* cdata = (const char*) data;
  36. if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
  37. for (size_t i = 0; i < size; ++i) {
  38. esp_rom_uart_tx_one_char(cdata[i]);
  39. }
  40. return size;
  41. }
  42. errno = EBADF;
  43. return -1;
  44. }
  45. ssize_t _read_r_console(struct _reent *r, int fd, void * data, size_t size)
  46. {
  47. char* cdata = (char*) data;
  48. if (fd == STDIN_FILENO) {
  49. size_t received;
  50. for (received = 0; received < size; ++received) {
  51. int status = esp_rom_uart_rx_one_char((uint8_t*) &cdata[received]);
  52. if (status != 0) {
  53. break;
  54. }
  55. }
  56. return received;
  57. }
  58. errno = EBADF;
  59. return -1;
  60. }
  61. /* The following weak definitions of syscalls will be used unless
  62. * another definition is provided. That definition may come from
  63. * VFS, LWIP, or the application.
  64. */
  65. ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size)
  66. __attribute__((weak,alias("_read_r_console")));
  67. ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size)
  68. __attribute__((weak,alias("_write_r_console")));
  69. /* The aliases below are to "syscall_not_implemented", which
  70. * doesn't have the same signature as the original function.
  71. * Disable type mismatch warnings for this reason.
  72. */
  73. #pragma GCC diagnostic push
  74. #pragma GCC diagnostic ignored "-Wattribute-alias"
  75. int _open_r(struct _reent *r, const char * path, int flags, int mode)
  76. __attribute__((weak,alias("syscall_not_implemented")));
  77. int _close_r(struct _reent *r, int fd)
  78. __attribute__((weak,alias("syscall_not_implemented")));
  79. off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode)
  80. __attribute__((weak,alias("syscall_not_implemented")));
  81. int _fcntl_r(struct _reent *r, int fd, int cmd, int arg)
  82. __attribute__((weak,alias("syscall_not_implemented")));
  83. int _fstat_r(struct _reent *r, int fd, struct stat * st)
  84. __attribute__((weak,alias("syscall_not_implemented")));
  85. int _stat_r(struct _reent *r, const char * path, struct stat * st)
  86. __attribute__((weak,alias("syscall_not_implemented")));
  87. int _link_r(struct _reent *r, const char* n1, const char* n2)
  88. __attribute__((weak,alias("syscall_not_implemented")));
  89. int _unlink_r(struct _reent *r, const char *path)
  90. __attribute__((weak,alias("syscall_not_implemented")));
  91. int _rename_r(struct _reent *r, const char *src, const char *dst)
  92. __attribute__((weak,alias("syscall_not_implemented")));
  93. int _isatty_r(struct _reent *r, int fd)
  94. __attribute__((weak,alias("syscall_not_implemented")));
  95. /* These functions are not expected to be overridden */
  96. int system(const char* str)
  97. __attribute__((alias("syscall_not_implemented")));
  98. int _system_r(struct _reent *r, const char *str)
  99. __attribute__((alias("syscall_not_implemented")));
  100. int raise(int sig)
  101. __attribute__((alias("syscall_not_implemented_aborts")));
  102. int _raise_r(struct _reent *r, int sig)
  103. __attribute__((alias("syscall_not_implemented_aborts")));
  104. void* _sbrk_r(struct _reent *r, ptrdiff_t sz)
  105. __attribute__((alias("syscall_not_implemented_aborts")));
  106. int _getpid_r(struct _reent *r)
  107. __attribute__((alias("syscall_not_implemented")));
  108. int _kill_r(struct _reent *r, int pid, int sig)
  109. __attribute__((alias("syscall_not_implemented")));
  110. void _exit(int __status)
  111. __attribute__((alias("syscall_not_implemented_aborts")));
  112. #pragma GCC diagnostic pop
  113. /* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */
  114. int fcntl(int fd, int cmd, ...)
  115. {
  116. va_list args;
  117. va_start(args, cmd);
  118. int arg = va_arg(args, int);
  119. va_end(args);
  120. struct _reent* r = __getreent();
  121. return _fcntl_r(r, fd, cmd, arg);
  122. }
  123. /* No-op function, used to force linking this file,
  124. instead of the syscalls implementation from libgloss.
  125. */
  126. void newlib_include_syscalls_impl(void)
  127. {
  128. }