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