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