newlib_init.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015-2019 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 "sdkconfig.h"
  15. #include <string.h>
  16. #include <stdbool.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/signal.h>
  23. #include <sys/unistd.h>
  24. #include <sys/reent.h>
  25. #include "esp_newlib.h"
  26. #include "esp_attr.h"
  27. #include "soc/soc_caps.h"
  28. #include "esp_rom_caps.h"
  29. #if CONFIG_IDF_TARGET_ESP32
  30. #include "esp32/rom/libc_stubs.h"
  31. #elif CONFIG_IDF_TARGET_ESP32S2
  32. #include "esp32s2/rom/libc_stubs.h"
  33. #elif CONFIG_IDF_TARGET_ESP32S3
  34. #include "esp32s3/rom/libc_stubs.h"
  35. #elif CONFIG_IDF_TARGET_ESP32C3
  36. #include "esp32c3/rom/libc_stubs.h"
  37. #endif
  38. static struct _reent s_reent;
  39. extern int _printf_float(struct _reent *rptr,
  40. void *pdata,
  41. FILE * fp,
  42. int (*pfunc) (struct _reent *, FILE *, const char *, size_t len),
  43. va_list * ap);
  44. extern int _scanf_float(struct _reent *rptr,
  45. void *pdata,
  46. FILE *fp,
  47. va_list *ap);
  48. static void raise_r_stub(struct _reent *rptr)
  49. {
  50. _raise_r(rptr, 0);
  51. }
  52. static struct syscall_stub_table s_stub_table = {
  53. .__getreent = &__getreent,
  54. ._malloc_r = &_malloc_r,
  55. ._free_r = &_free_r,
  56. ._realloc_r = &_realloc_r,
  57. ._calloc_r = &_calloc_r,
  58. ._abort = &abort,
  59. ._system_r = &_system_r,
  60. ._rename_r = &_rename_r,
  61. ._times_r = &_times_r,
  62. ._gettimeofday_r = &_gettimeofday_r,
  63. ._raise_r = &raise_r_stub,
  64. ._unlink_r = &_unlink_r,
  65. ._link_r = &_link_r,
  66. ._stat_r = &_stat_r,
  67. ._fstat_r = &_fstat_r,
  68. ._sbrk_r = &_sbrk_r,
  69. ._getpid_r = &_getpid_r,
  70. ._kill_r = &_kill_r,
  71. ._exit_r = NULL, // never called in ROM
  72. ._close_r = &_close_r,
  73. ._open_r = &_open_r,
  74. ._write_r = (int (*)(struct _reent *r, int, const void *, int)) &_write_r,
  75. ._lseek_r = (int (*)(struct _reent *r, int, int, int)) &_lseek_r,
  76. ._read_r = (int (*)(struct _reent *r, int, void *, int)) &_read_r,
  77. #if ESP_ROM_HAS_RETARGETABLE_LOCKING
  78. ._retarget_lock_init = &__retarget_lock_init,
  79. ._retarget_lock_init_recursive = &__retarget_lock_init_recursive,
  80. ._retarget_lock_close = &__retarget_lock_close,
  81. ._retarget_lock_close_recursive = &__retarget_lock_close_recursive,
  82. ._retarget_lock_acquire = &__retarget_lock_acquire,
  83. ._retarget_lock_acquire_recursive = &__retarget_lock_acquire_recursive,
  84. ._retarget_lock_try_acquire = &__retarget_lock_try_acquire,
  85. ._retarget_lock_try_acquire_recursive = &__retarget_lock_try_acquire_recursive,
  86. ._retarget_lock_release = &__retarget_lock_release,
  87. ._retarget_lock_release_recursive = &__retarget_lock_release_recursive,
  88. #else
  89. ._lock_init = &_lock_init,
  90. ._lock_init_recursive = &_lock_init_recursive,
  91. ._lock_close = &_lock_close,
  92. ._lock_close_recursive = &_lock_close_recursive,
  93. ._lock_acquire = &_lock_acquire,
  94. ._lock_acquire_recursive = &_lock_acquire_recursive,
  95. ._lock_try_acquire = &_lock_try_acquire,
  96. ._lock_try_acquire_recursive = &_lock_try_acquire_recursive,
  97. ._lock_release = &_lock_release,
  98. ._lock_release_recursive = &_lock_release_recursive,
  99. #endif
  100. #ifdef CONFIG_NEWLIB_NANO_FORMAT
  101. ._printf_float = &_printf_float,
  102. ._scanf_float = &_scanf_float,
  103. #else
  104. ._printf_float = NULL,
  105. ._scanf_float = NULL,
  106. #endif
  107. #ifdef CONFIG_IDF_TARGET_ESP32C3
  108. /* TODO IDF-2570 : mark that this assert failed in ROM, to avoid confusion between IDF & ROM
  109. assertion failures (as function names & source file names will be similar)
  110. */
  111. .__assert_func = __assert_func,
  112. /* We don't expect either ROM code or IDF to ever call __sinit, so it's implemented as abort() for now.
  113. esp_reent_init() does this job inside IDF.
  114. Kept in the syscall table in case we find a need for it later.
  115. */
  116. .__sinit = (void *)abort,
  117. ._cleanup_r = &_cleanup_r,
  118. #endif
  119. };
  120. void esp_newlib_init(void)
  121. {
  122. #if CONFIG_IDF_TARGET_ESP32
  123. syscall_table_ptr_pro = syscall_table_ptr_app = &s_stub_table;
  124. #elif CONFIG_IDF_TARGET_ESP32S2
  125. syscall_table_ptr_pro = &s_stub_table;
  126. #elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  127. syscall_table_ptr = &s_stub_table;
  128. #endif
  129. _GLOBAL_REENT = &s_reent;
  130. environ = malloc(sizeof(char*));
  131. environ[0] = NULL;
  132. esp_newlib_locks_init();
  133. }
  134. void esp_setup_newlib_syscalls(void) __attribute__((alias("esp_newlib_init")));