|
|
@@ -39,8 +39,6 @@
|
|
|
#include "esp32p4/rom/libc_stubs.h"
|
|
|
#endif
|
|
|
|
|
|
-static struct _reent s_reent;
|
|
|
-
|
|
|
extern int _printf_float(struct _reent *rptr,
|
|
|
void *pdata,
|
|
|
FILE * fp,
|
|
|
@@ -58,6 +56,21 @@ static void raise_r_stub(struct _reent *rptr)
|
|
|
_raise_r(rptr, 0);
|
|
|
}
|
|
|
|
|
|
+static void esp_cleanup_r (struct _reent *rptr)
|
|
|
+{
|
|
|
+ if (_REENT_STDIN(rptr) != _REENT_STDIN(_GLOBAL_REENT)) {
|
|
|
+ _fclose_r(rptr, _REENT_STDIN(rptr));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_REENT_STDOUT(rptr) != _REENT_STDOUT(_GLOBAL_REENT)) {
|
|
|
+ _fclose_r(rptr, _REENT_STDOUT(rptr));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_REENT_STDERR(rptr) !=_REENT_STDERR(_GLOBAL_REENT)) {
|
|
|
+ _fclose_r(rptr, _REENT_STDERR(rptr));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
static struct syscall_stub_table s_stub_table = {
|
|
|
.__getreent = &__getreent,
|
|
|
._malloc_r = &_malloc_r,
|
|
|
@@ -120,14 +133,14 @@ static struct syscall_stub_table s_stub_table = {
|
|
|
*/
|
|
|
.__assert_func = __assert_func,
|
|
|
|
|
|
- /* We don't expect either ROM code or IDF to ever call __sinit, so it's implemented as abort() for now.
|
|
|
+ /* We don't expect either ROM code to ever call __sinit, so it's implemented as abort() for now.
|
|
|
|
|
|
- esp_reent_init() does this job inside IDF.
|
|
|
-
|
|
|
- Kept in the syscall table in case we find a need for it later.
|
|
|
+ __sinit may be called in IDF side only if /dev/console used as input/output. It called only
|
|
|
+ once for _GLOBAL_REENT. Then reuse std file pointers from _GLOBAL_REENT in another reents.
|
|
|
+ See esp_newlib_init() and esp_reent_init() for details.
|
|
|
*/
|
|
|
.__sinit = (void *)abort,
|
|
|
- ._cleanup_r = &_cleanup_r,
|
|
|
+ ._cleanup_r = &esp_cleanup_r,
|
|
|
#endif
|
|
|
};
|
|
|
|
|
|
@@ -141,7 +154,17 @@ void esp_newlib_init(void)
|
|
|
syscall_table_ptr = &s_stub_table;
|
|
|
#endif
|
|
|
|
|
|
+#if __NEWLIB__ > 4 || ( __NEWLIB__ == 4 && __NEWLIB_MINOR__ > 1 ) /* TODO: IDF-8134 */
|
|
|
+ memset(&__sglue, 0, sizeof(__sglue));
|
|
|
+ _global_impure_ptr = _GLOBAL_REENT;
|
|
|
+#else
|
|
|
+ static struct _reent s_reent;
|
|
|
_GLOBAL_REENT = &s_reent;
|
|
|
+#endif
|
|
|
+
|
|
|
+ /* Ensure that the initialization of sfp is prevented until esp_newlib_init_global_stdio() is explicitly invoked. */
|
|
|
+ _GLOBAL_REENT->__cleanup = esp_cleanup_r;
|
|
|
+ _REENT_SDIDINIT(_GLOBAL_REENT) = 1;
|
|
|
|
|
|
environ = malloc(sizeof(char*));
|
|
|
if (environ == 0) {
|
|
|
@@ -154,3 +177,33 @@ void esp_newlib_init(void)
|
|
|
}
|
|
|
|
|
|
void esp_setup_newlib_syscalls(void) __attribute__((alias("esp_newlib_init")));
|
|
|
+
|
|
|
+void esp_newlib_init_global_stdio(const char *stdio_dev)
|
|
|
+{
|
|
|
+ if (stdio_dev == NULL)
|
|
|
+ {
|
|
|
+ _GLOBAL_REENT->__cleanup = NULL;
|
|
|
+ _REENT_SDIDINIT(_GLOBAL_REENT) = 0;
|
|
|
+ __sinit(_GLOBAL_REENT);
|
|
|
+ _GLOBAL_REENT->__cleanup = esp_cleanup_r;
|
|
|
+ _REENT_SDIDINIT(_GLOBAL_REENT) = 1;
|
|
|
+ } else {
|
|
|
+ _REENT_STDIN(_GLOBAL_REENT) = fopen(stdio_dev, "r");
|
|
|
+ _REENT_STDOUT(_GLOBAL_REENT) = fopen(stdio_dev, "w");
|
|
|
+ _REENT_STDERR(_GLOBAL_REENT) = fopen(stdio_dev, "w");
|
|
|
+#if ESP_ROM_NEEDS_SWSETUP_WORKAROUND
|
|
|
+ /*
|
|
|
+ - This workaround for printf functions using 32-bit time_t after the 64-bit time_t upgrade
|
|
|
+ - The 32-bit time_t usage is triggered through ROM Newlib functions printf related functions calling __swsetup_r() on
|
|
|
+ the first call to a particular file pointer (i.e., stdin, stdout, stderr)
|
|
|
+ - Thus, we call the toolchain version of __swsetup_r() now (before any printf calls are made) to setup all of the
|
|
|
+ file pointers. Thus, the ROM newlib code will never call the ROM version of __swsetup_r().
|
|
|
+ - See IDFGH-7728 for more details
|
|
|
+ */
|
|
|
+ extern int __swsetup_r (struct _reent *, FILE *);
|
|
|
+ __swsetup_r(_GLOBAL_REENT, _REENT_STDIN(_GLOBAL_REENT));
|
|
|
+ __swsetup_r(_GLOBAL_REENT, _REENT_STDOUT(_GLOBAL_REENT));
|
|
|
+ __swsetup_r(_GLOBAL_REENT, _REENT_STDERR(_GLOBAL_REENT));
|
|
|
+#endif /* ESP_ROM_NEEDS_SWSETUP_WORKAROUND */
|
|
|
+ }
|
|
|
+}
|