newlib_init.c 5.0 KB

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