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
15 #include "sdkconfig.h"
16 #include <string.h>
17 #include <stdbool.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/signal.h>
24 #include <sys/unistd.h>
25 #include <sys/reent.h>
26 #include "esp_newlib.h"
27 #include "esp_attr.h"
28 #include "soc/soc_caps.h"
29 #include "esp_rom_caps.h"
30
31 #if CONFIG_IDF_TARGET_ESP32
32 #include "esp32/rom/libc_stubs.h"
33 #elif CONFIG_IDF_TARGET_ESP32S2
34 #include "esp32s2/rom/libc_stubs.h"
35 #elif CONFIG_IDF_TARGET_ESP32S3
36 #include "esp32s3/rom/libc_stubs.h"
37 #elif CONFIG_IDF_TARGET_ESP32C3
38 #include "esp32c3/rom/libc_stubs.h"
39 #endif
40
41 static struct _reent s_reent;
42
43 extern int _printf_float(struct _reent *rptr,
44 void *pdata,
45 FILE * fp,
46 int (*pfunc) (struct _reent *, FILE *, const char *, size_t len),
47 va_list * ap);
48
49
50 extern int _scanf_float(struct _reent *rptr,
51 void *pdata,
52 FILE *fp,
53 va_list *ap);
54
raise_r_stub(struct _reent * rptr)55 static void raise_r_stub(struct _reent *rptr)
56 {
57 _raise_r(rptr, 0);
58 }
59
60 static struct syscall_stub_table s_stub_table = {
61 .__getreent = &__getreent,
62 ._malloc_r = &_malloc_r,
63 ._free_r = &_free_r,
64 ._realloc_r = &_realloc_r,
65 ._calloc_r = &_calloc_r,
66 ._abort = &abort,
67 ._system_r = &_system_r,
68 ._rename_r = &_rename_r,
69 ._times_r = &_times_r,
70 ._gettimeofday_r = &_gettimeofday_r,
71 ._raise_r = &raise_r_stub,
72 ._unlink_r = &_unlink_r,
73 ._link_r = &_link_r,
74 ._stat_r = &_stat_r,
75 ._fstat_r = &_fstat_r,
76 ._sbrk_r = &_sbrk_r,
77 ._getpid_r = &_getpid_r,
78 ._kill_r = &_kill_r,
79 ._exit_r = NULL, // never called in ROM
80 ._close_r = &_close_r,
81 ._open_r = &_open_r,
82 ._write_r = (int (*)(struct _reent *r, int, const void *, int)) &_write_r,
83 ._lseek_r = (int (*)(struct _reent *r, int, int, int)) &_lseek_r,
84 ._read_r = (int (*)(struct _reent *r, int, void *, int)) &_read_r,
85 #if ESP_ROM_HAS_RETARGETABLE_LOCKING
86 ._retarget_lock_init = &__retarget_lock_init,
87 ._retarget_lock_init_recursive = &__retarget_lock_init_recursive,
88 ._retarget_lock_close = &__retarget_lock_close,
89 ._retarget_lock_close_recursive = &__retarget_lock_close_recursive,
90 ._retarget_lock_acquire = &__retarget_lock_acquire,
91 ._retarget_lock_acquire_recursive = &__retarget_lock_acquire_recursive,
92 ._retarget_lock_try_acquire = &__retarget_lock_try_acquire,
93 ._retarget_lock_try_acquire_recursive = &__retarget_lock_try_acquire_recursive,
94 ._retarget_lock_release = &__retarget_lock_release,
95 ._retarget_lock_release_recursive = &__retarget_lock_release_recursive,
96 #else
97 ._lock_init = &_lock_init,
98 ._lock_init_recursive = &_lock_init_recursive,
99 ._lock_close = &_lock_close,
100 ._lock_close_recursive = &_lock_close_recursive,
101 ._lock_acquire = &_lock_acquire,
102 ._lock_acquire_recursive = &_lock_acquire_recursive,
103 ._lock_try_acquire = &_lock_try_acquire,
104 ._lock_try_acquire_recursive = &_lock_try_acquire_recursive,
105 ._lock_release = &_lock_release,
106 ._lock_release_recursive = &_lock_release_recursive,
107 #endif
108 #ifdef CONFIG_NEWLIB_NANO_FORMAT
109 ._printf_float = &_printf_float,
110 ._scanf_float = &_scanf_float,
111 #else
112 ._printf_float = NULL,
113 ._scanf_float = NULL,
114 #endif
115 #ifdef CONFIG_IDF_TARGET_ESP32C3
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 syscall_table_ptr = &s_stub_table;
140 #endif
141
142 _GLOBAL_REENT = &s_reent;
143
144 environ = malloc(sizeof(char*));
145 environ[0] = NULL;
146
147 esp_newlib_locks_init();
148 }
149
150 void esp_setup_newlib_syscalls(void) __attribute__((alias("esp_newlib_init")));
151