newlib_init.c 5.1 KB

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