refactor(core): remove mini_printf header
What changed, and why it matters
This is a code cleanup change: it removes a thin wrapper header (mini_printf.h) and makes the rest of the firmware include the underlying printf library header directly. The actual formatting functions used are the same; only the names and file paths changed. There is no security fix or behavior change visible in the diff.
No security action required. Treat as a normal refactoring review; verify that the build still resolves the moved header and that no other code still references the deleted mini_printf.h.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors the embedded firmware’s printf usage. It deletes core/embed/rtl/inc/rtl/mini_printf.h (which only #defined mini_vsnprintf -> vsnprintf_ and mini_snprintf -> snprintf_) and moves the third-party printf header from core/embed/rtl/inc/printf/printf.h to core/embed/rtl/inc/rtl/printf.h, adjusting the include path in printf.c accordingly. Call sites are updated to include
Changed components
core/embed/gfx/terminal.ccore/embed/projects/prodtest/cmd/prodtest_otp_variant.ccore/embed/projects/prodtest/cmd/prodtest_power_manager.ccore/embed/rtl/cli.ccore/embed/sys/dbg/dbg_console.ccore/embed/sys/dbg/syslog.ccore/embed/rtl/inc/rtl/printf.hcore/embed/rtl/printf.ccore/embed/rtl/printf_config.hInspect captured patch +295 / −320
diff --git a/core/embed/gfx/inc/gfx/terminal.h b/core/embed/gfx/inc/gfx/terminal.h
index 066a2efff..e1a6d09b8 100644
--- a/core/embed/gfx/inc/gfx/terminal.h
+++ b/core/embed/gfx/inc/gfx/terminal.h
@@ -58,7 +58,7 @@ void term_print_int32(int32_t value);
/**
* Prints printf-style formatted text to the terminal.
*
- * The function internally uses `mini_vsnprintf` to format the text.
+ * The function internally uses `vsnprintf_` to format the text.
*
* @param fmt Format string (printf-style).
* @param ... Additional arguments for formatting.
diff --git a/core/embed/gfx/terminal.c b/core/embed/gfx/terminal.c
index 3dcbe4010..6ecada55c 100644
--- a/core/embed/gfx/terminal.c
+++ b/core/embed/gfx/terminal.c
@@ -23,7 +23,7 @@
#include <gfx/gfx_draw.h>
#include <gfx/terminal.h>
#include <io/display.h>
-#include <rtl/mini_printf.h>
+#include <rtl/printf.h>
#include <rtl/strutils.h>
#include "fonts/font_bitmap.h"
@@ -218,7 +218,7 @@ void term_printf(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
char buf[256] = {0};
- int len = mini_vsnprintf(buf, sizeof(buf), fmt, va);
+ int len = vsnprintf_(buf, sizeof(buf), fmt, va);
term_nprint(buf, len);
va_end(va);
}
diff --git a/core/embed/projects/prodtest/cmd/prodtest_otp_variant.c b/core/embed/projects/prodtest/cmd/prodtest_otp_variant.c
index 249e6f525..05053a989 100644
--- a/core/embed/projects/prodtest/cmd/prodtest_otp_variant.c
+++ b/core/embed/projects/prodtest/cmd/prodtest_otp_variant.c
@@ -22,7 +22,7 @@
#include <trezor_rtl.h>
#include <rtl/cli.h>
-#include <rtl/mini_printf.h>
+#include <rtl/printf.h>
#include <sec/secret.h>
#include <util/flash_otp.h>
@@ -79,7 +79,7 @@ static void prodtest_otp_variant_read(cli_t* cli) {
if (i != 0) {
*dst++ = ' ';
}
- mini_snprintf(dst, buffer_size, "%d", block[i]);
+ snprintf_(dst, buffer_size, "%d", block[i]);
dst += strlen(dst);
buffer_size -= strlen(dst);
}
diff --git a/core/embed/projects/prodtest/cmd/prodtest_power_manager.c b/core/embed/projects/prodtest/cmd/prodtest_power_manager.c
index 37e4fc36f..9b31d1efa 100644
--- a/core/embed/projects/prodtest/cmd/prodtest_power_manager.c
+++ b/core/embed/projects/prodtest/cmd/prodtest_power_manager.c
@@ -24,7 +24,7 @@
#include <stdlib.h>
#include <rtl/cli.h>
-#include <rtl/mini_printf.h>
+#include <rtl/printf.h>
#include <rtl/unit_test.h>
#include <rust_ui_prodtest.h>
#include <sys/backup_ram.h>
@@ -171,13 +171,13 @@ void prodtest_pm_fuel_gauge_monitor(cli_t* cli) {
(int)(report.battery_soc * 100),
(int)(report.battery_soc * 10000) % 100);
- mini_snprintf(screen_text_buf, 100, "%d.%03dV %d.%03dmA %d.%02d ",
- (int)report.battery_voltage_v,
- (int)(report.battery_voltage_v * 1000) % 1000,
- (int)report.battery_current_ma,
- abs((int)(report.battery_current_ma * 1000) % 1000),
- (int)(report.battery_soc * 100),
- (int)(report.battery_soc * 10000) % 100);
+ snprintf_(screen_text_buf, 100, "%d.%03dV %d.%03dmA %d.%02d ",
+ (int)report.battery_voltage_v,
+ (int)(report.battery_voltage_v * 1000) % 1000,
+ (int)report.battery_current_ma,
+ abs((int)(report.battery_current_ma * 1000) % 1000),
+ (int)(report.battery_soc * 100),
+ (int)(report.battery_soc * 10000) % 100);
screen_prodtest_show_text(screen_text_buf, strlen(screen_text_buf));
diff --git a/core/embed/rtl/cli.c b/core/embed/rtl/cli.c
index 2e0d04167..84dd994ba 100644
--- a/core/embed/rtl/cli.c
+++ b/core/embed/rtl/cli.c
@@ -1,7 +1,7 @@
#include <trezor_rtl.h>
#include <rtl/cli.h>
-#include <rtl/mini_printf.h>
+#include <rtl/printf.h>
#include <ctype.h>
#include <stdarg.h>
@@ -29,7 +29,7 @@ void cli_set_commands(cli_t* cli, const cli_command_t* cmd_array,
static void cli_vprintf(cli_t* cli, const char* format, va_list args) {
char buffer[CLI_LINE_BUFFER_SIZE];
- mini_vsnprintf(buffer, sizeof(buffer), format, args);
+ vsnprintf_(buffer, sizeof(buffer), format, args);
cli->write(cli->callback_context, buffer, strlen(buffer));
}
diff --git a/core/embed/rtl/inc/printf/printf.h b/core/embed/rtl/inc/printf/printf.h
deleted file mode 100644
index 13e0585b9..000000000
--- a/core/embed/rtl/inc/printf/printf.h
+++ /dev/null
@@ -1,245 +0,0 @@
-// clang-format off
-// source: https://github.com/eyalroz/printf/blob/v6.3.0/src/printf/printf.h
-
-/**
- * @author (c) Eyal Rozenberg <eyalroz1@gmx.com>
- * 2021-2024, Haifa, Palestine/Israel
- * @author (c) Marco Paland (info@paland.com)
- * 2014-2019, PALANDesign Hannover, Germany
- *
- * @note Others have made smaller contributions to this file: see the
- * contributors page at https://github.com/eyalroz/printf/graphs/contributors
- * or ask one of the authors.
- *
- * @brief Small stand-alone implementation of the printf family of functions
- * (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems
- * with a very limited resources.
- *
- * @note the implementations are thread-safe; re-entrant; use no functions from
- * the standard library; and do not dynamically allocate any memory.
- *
- * @license The MIT License (MIT)
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef PRINTF_H_
-#define PRINTF_H_
-
-//#ifdef PRINTF_INCLUDE_CONFIG_H
-#include "printf_config.h"
-//#endif
-
-#ifdef __cplusplus
-# include <cstdarg>
-# include <cstddef>
-extern "C" {
-#else
-# include <stdarg.h>
-# include <stddef.h>
-#endif
-
-#ifdef __GNUC__
-# if ((__GNUC__ == 4 && __GNUC_MINOR__>= 4) || __GNUC__ > 4)
-# define ATTR_PRINTF(one_based_format_index, first_arg) \
-__attribute__((format(gnu_printf, (one_based_format_index), (first_arg))))
-# else
-# define ATTR_PRINTF(one_based_format_index, first_arg) \
-__attribute__((format(printf, (one_based_format_index), (first_arg))))
-# endif
-# define ATTR_VPRINTF(one_based_format_index) \
-ATTR_PRINTF((one_based_format_index), 0)
-#else
-# define ATTR_PRINTF(one_based_format_index, first_arg)
-# define ATTR_VPRINTF(one_based_format_index)
-#endif
-
-#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
-#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT 0
-#endif
-
-#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
-#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 0
-#endif
-
-#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
-# define printf_ printf
-# define sprintf_ sprintf
-# define vsprintf_ vsprintf
-# define snprintf_ snprintf
-# define vsnprintf_ vsnprintf
-# define vprintf_ vprintf
-#endif
-
-/*
- * If you want to include this implementation file directly rather than
- * link against it, this will let you control the functions' visibility,
- * e.g. make them static so as not to clash with other objects also
- * using them.
- */
-#ifndef PRINTF_VISIBILITY
-#define PRINTF_VISIBILITY
-#endif
-
-/**
- * Prints/send a single character to some opaque output entity
- *
- * @note This function is not implemented by the library, only declared; you
- * must provide an implementation if you wish to use the @ref printf / @ref
- * vprintf function (and possibly for linking against the library, if your
- * toolchain does not support discarding unused functions)
- *
- * @note The output could be as simple as a wrapper for the `write()` system
- * call on a Unix-like * system, or even libc's @ref putchar , for replicating
- * actual functionality of libc's @ref printf * function; but on an embedded
- * system it may involve interaction with a special output device, like a UART,
- * etc.
- *
- * @note in libc's @ref putchar, the parameter type is an int; this was intended
- * to support the representation of either a proper character or EOF in a
- * variable - but this is really not meaningful to pass into @ref putchar and is
- * discouraged today. See further discussion in:
- * @link https://stackoverflow.com/q/17452847/1593077
- *
- * @param c the single character to print
- */
-PRINTF_VISIBILITY
-void putchar_(char c);
-
-
-/**
- * An implementation of the C standard's printf/vprintf
- *
- * @note you must implement a @ref putchar_ function for using this function -
- * it invokes @ref putchar_ * rather than directly performing any I/O (which
- * insulates it from any dependence on the operating system * and external
- * libraries).
- *
- * @param format A string specifying the format of the output, with %-marked
- * specifiers of how to interpret additional arguments.
- * @param arg Additional arguments to the function, one for each %-specifier in
- * @p format
- * @return The number of characters written into @p s, not counting the
- * terminating null character
- */
-/* @{ */
-PRINTF_VISIBILITY
-int printf_(const char* format, ...) ATTR_PRINTF(1, 2);
-PRINTF_VISIBILITY
-int vprintf_(const char* format, va_list arg) ATTR_VPRINTF(1);
-/* @} */
-
-
-/**
- * An implementation of the C standard's sprintf/vsprintf
- *
- * @note For security considerations (the potential for exceeding the buffer
- * bounds), please consider using the size-constrained variant, @ref snprintf /
- * @ref vsnprintf, instead.
- *
- * @param s An array in which to store the formatted string. It must be large
- * enough to fit the formatted output!
- * @param format A string specifying the format of the output, with %-marked
- * specifiers of how to interpret additional arguments
- * @param arg Additional arguments to the function, one for each specifier in
- * @p format
- * @return The number of characters written into @p s, not counting the
- * terminating null character
- */
-/* @{ */
-PRINTF_VISIBILITY
-int sprintf_(char* s, const char* format, ...) ATTR_PRINTF(2, 3);
-PRINTF_VISIBILITY
-int vsprintf_(char* s, const char* format, va_list arg) ATTR_VPRINTF(2);
-/* @} */
-
-
-/**
- * An implementation of the C standard's snprintf/vsnprintf
- *
- * @param s An array in which to store the formatted string. It must be large
- * enough to fit either the entire formatted output, or at least @p n
- * characters. Alternatively, it can be NULL, in which case nothing will
- * be printed, and only the number of characters which _could_ have been
- * printed is tallied and returned.
- * @param n The maximum number of characters to write to the array, including
- * a terminating null character
- * @param format A string specifying the format of the output, with %-marked
- * specifiers of how to interpret additional arguments.
- * @param arg Additional arguments to the function, one for each specifier in
- * @p format
- * @return The number of characters that COULD have been written into @p s, not
- * counting the terminating null character. A value equal or larger than
- * @p n indicates truncation. Only when the returned value is non-negative
- * and less than @p n, the null-terminated string has been fully and
- * successfully printed.
- */
-/* @{ */
-PRINTF_VISIBILITY
-int snprintf_(char* s, size_t count, const char* format, ...) ATTR_PRINTF(3, 4);
-PRINTF_VISIBILITY
-int vsnprintf_(char* s, size_t count, const char* format, va_list arg) ATTR_VPRINTF(3);
-/* @} */
-
-/**
- * printf/vprintf with user-specified output function
- *
- * An alternative to @ref printf_, in which the output function is specified
- * dynamically (rather than @ref putchar_ being used)
- *
- * @param out An output function which takes one character and a type-erased
- * additional parameters
- * @param extra_arg The type-erased argument to pass to the output function @p
- * out with each call
- * @param format A string specifying the format of the output, with %-marked
- * specifiers of how to interpret additional arguments.
- * @param arg Additional arguments to the function, one for each specifier in
- * @p format
- * @return The number of characters for which the output f unction was invoked,
- * not counting the terminating null character
- *
- */
-PRINTF_VISIBILITY
-int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...) ATTR_PRINTF(3, 4);
-PRINTF_VISIBILITY
-int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg) ATTR_VPRINTF(3);
-
-#ifdef __cplusplus
-} /* extern "C" */
-#endif
-
-#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
-# undef printf_
-# undef sprintf_
-# undef vsprintf_
-# undef snprintf_
-# undef vsnprintf_
-# undef vprintf_
-#else
-#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
-# define printf printf_
-# define sprintf sprintf_
-# define vsprintf vsprintf_
-# define snprintf snprintf_
-# define vsnprintf vsnprintf_
-# define vprintf vprintf_
-#endif
-#endif
-
-#endif /* PRINTF_H_ */
diff --git a/core/embed/rtl/inc/printf_config.h b/core/embed/rtl/inc/printf_config.h
deleted file mode 100644
index 8a315e253..000000000
--- a/core/embed/rtl/inc/printf_config.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * This file is part of the Trezor project, https://trezor.io/
- *
- * Copyright (c) SatoshiLabs
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-
-#define PRINTF_SUPPORT_DECIMAL_SPECIFIERS 0
-#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 0
-#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER 1
-#define PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS 0
-#define PRINTF_USE_DOUBLE_INTERNALLY 0
-#define PRINTF_INTEGER_BUFFER_SIZE 32
-#define PRINTF_DECIMAL_BUFFER_SIZE 0
-
-static inline void putchar_(char c) {}
diff --git a/core/embed/rtl/inc/rtl/mini_printf.h b/core/embed/rtl/inc/rtl/mini_printf.h
deleted file mode 100644
index d9bbdd8ae..000000000
--- a/core/embed/rtl/inc/rtl/mini_printf.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * This file is part of the Trezor project, https://trezor.io/
- *
- * Copyright (c) SatoshiLabs
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-
-#include <printf/printf.h>
-
-#define mini_vsnprintf vsnprintf_
-#define mini_snprintf snprintf_
diff --git a/core/embed/rtl/inc/rtl/printf.h b/core/embed/rtl/inc/rtl/printf.h
new file mode 100644
index 000000000..bfc5993a6
--- /dev/null
+++ b/core/embed/rtl/inc/rtl/printf.h
@@ -0,0 +1,245 @@
+// clang-format off
+// source: https://github.com/eyalroz/printf/blob/v6.3.0/src/printf/printf.h
+
+/**
+ * @author (c) Eyal Rozenberg <eyalroz1@gmx.com>
+ * 2021-2024, Haifa, Palestine/Israel
+ * @author (c) Marco Paland (info@paland.com)
+ * 2014-2019, PALANDesign Hannover, Germany
+ *
+ * @note Others have made smaller contributions to this file: see the
+ * contributors page at https://github.com/eyalroz/printf/graphs/contributors
+ * or ask one of the authors.
+ *
+ * @brief Small stand-alone implementation of the printf family of functions
+ * (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems
+ * with a very limited resources.
+ *
+ * @note the implementations are thread-safe; re-entrant; use no functions from
+ * the standard library; and do not dynamically allocate any memory.
+ *
+ * @license The MIT License (MIT)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef PRINTF_H_
+#define PRINTF_H_
+
+//#ifdef PRINTF_INCLUDE_CONFIG_H
+#include "../../printf_config.h"
+//#endif
+
+#ifdef __cplusplus
+# include <cstdarg>
+# include <cstddef>
+extern "C" {
+#else
+# include <stdarg.h>
+# include <stddef.h>
+#endif
+
+#ifdef __GNUC__
+# if ((__GNUC__ == 4 && __GNUC_MINOR__>= 4) || __GNUC__ > 4)
+# define ATTR_PRINTF(one_based_format_index, first_arg) \
+__attribute__((format(gnu_printf, (one_based_format_index), (first_arg))))
+# else
+# define ATTR_PRINTF(one_based_format_index, first_arg) \
+__attribute__((format(printf, (one_based_format_index), (first_arg))))
+# endif
+# define ATTR_VPRINTF(one_based_format_index) \
+ATTR_PRINTF((one_based_format_index), 0)
+#else
+# define ATTR_PRINTF(one_based_format_index, first_arg)
+# define ATTR_VPRINTF(one_based_format_index)
+#endif
+
+#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
+#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT 0
+#endif
+
+#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
+#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 0
+#endif
+
+#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
+# define printf_ printf
+# define sprintf_ sprintf
+# define vsprintf_ vsprintf
+# define snprintf_ snprintf
+# define vsnprintf_ vsnprintf
+# define vprintf_ vprintf
+#endif
+
+/*
+ * If you want to include this implementation file directly rather than
+ * link against it, this will let you control the functions' visibility,
+ * e.g. make them static so as not to clash with other objects also
+ * using them.
+ */
+#ifndef PRINTF_VISIBILITY
+#define PRINTF_VISIBILITY
+#endif
+
+/**
+ * Prints/send a single character to some opaque output entity
+ *
+ * @note This function is not implemented by the library, only declared; you
+ * must provide an implementation if you wish to use the @ref printf / @ref
+ * vprintf function (and possibly for linking against the library, if your
+ * toolchain does not support discarding unused functions)
+ *
+ * @note The output could be as simple as a wrapper for the `write()` system
+ * call on a Unix-like * system, or even libc's @ref putchar , for replicating
+ * actual functionality of libc's @ref printf * function; but on an embedded
+ * system it may involve interaction with a special output device, like a UART,
+ * etc.
+ *
+ * @note in libc's @ref putchar, the parameter type is an int; this was intended
+ * to support the representation of either a proper character or EOF in a
+ * variable - but this is really not meaningful to pass into @ref putchar and is
+ * discouraged today. See further discussion in:
+ * @link https://stackoverflow.com/q/17452847/1593077
+ *
+ * @param c the single character to print
+ */
+PRINTF_VISIBILITY
+void putchar_(char c);
+
+
+/**
+ * An implementation of the C standard's printf/vprintf
+ *
+ * @note you must implement a @ref putchar_ function for using this function -
+ * it invokes @ref putchar_ * rather than directly performing any I/O (which
+ * insulates it from any dependence on the operating system * and external
+ * libraries).
+ *
+ * @param format A string specifying the format of the output, with %-marked
+ * specifiers of how to interpret additional arguments.
+ * @param arg Additional arguments to the function, one for each %-specifier in
+ * @p format
+ * @return The number of characters written into @p s, not counting the
+ * terminating null character
+ */
+/* @{ */
+PRINTF_VISIBILITY
+int printf_(const char* format, ...) ATTR_PRINTF(1, 2);
+PRINTF_VISIBILITY
+int vprintf_(const char* format, va_list arg) ATTR_VPRINTF(1);
+/* @} */
+
+
+/**
+ * An implementation of the C standard's sprintf/vsprintf
+ *
+ * @note For security considerations (the potential for exceeding the buffer
+ * bounds), please consider using the size-constrained variant, @ref snprintf /
+ * @ref vsnprintf, instead.
+ *
+ * @param s An array in which to store the formatted string. It must be large
+ * enough to fit the formatted output!
+ * @param format A string specifying the format of the output, with %-marked
+ * specifiers of how to interpret additional arguments
+ * @param arg Additional arguments to the function, one for each specifier in
+ * @p format
+ * @return The number of characters written into @p s, not counting the
+ * terminating null character
+ */
+/* @{ */
+PRINTF_VISIBILITY
+int sprintf_(char* s, const char* format, ...) ATTR_PRINTF(2, 3);
+PRINTF_VISIBILITY
+int vsprintf_(char* s, const char* format, va_list arg) ATTR_VPRINTF(2);
+/* @} */
+
+
+/**
+ * An implementation of the C standard's snprintf/vsnprintf
+ *
+ * @param s An array in which to store the formatted string. It must be large
+ * enough to fit either the entire formatted output, or at least @p n
+ * characters. Alternatively, it can be NULL, in which case nothing will
+ * be printed, and only the number of characters which _could_ have been
+ * printed is tallied and returned.
+ * @param n The maximum number of characters to write to the array, including
+ * a terminating null character
+ * @param format A string specifying the format of the output, with %-marked
+ * specifiers of how to interpret additional arguments.
+ * @param arg Additional arguments to the function, one for each specifier in
+ * @p format
+ * @return The number of characters that COULD have been written into @p s, not
+ * counting the terminating null character. A value equal or larger than
+ * @p n indicates truncation. Only when the returned value is non-negative
+ * and less than @p n, the null-terminated string has been fully and
+ * successfully printed.
+ */
+/* @{ */
+PRINTF_VISIBILITY
+int snprintf_(char* s, size_t count, const char* format, ...) ATTR_PRINTF(3, 4);
+PRINTF_VISIBILITY
+int vsnprintf_(char* s, size_t count, const char* format, va_list arg) ATTR_VPRINTF(3);
+/* @} */
+
+/**
+ * printf/vprintf with user-specified output function
+ *
+ * An alternative to @ref printf_, in which the output function is specified
+ * dynamically (rather than @ref putchar_ being used)
+ *
+ * @param out An output function which takes one character and a type-erased
+ * additional parameters
+ * @param extra_arg The type-erased argument to pass to the output function @p
+ * out with each call
+ * @param format A string specifying the format of the output, with %-marked
+ * specifiers of how to interpret additional arguments.
+ * @param arg Additional arguments to the function, one for each specifier in
+ * @p format
+ * @return The number of characters for which the output f unction was invoked,
+ * not counting the terminating null character
+ *
+ */
+PRINTF_VISIBILITY
+int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...) ATTR_PRINTF(3, 4);
+PRINTF_VISIBILITY
+int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg) ATTR_VPRINTF(3);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
+# undef printf_
+# undef sprintf_
+# undef vsprintf_
+# undef snprintf_
+# undef vsnprintf_
+# undef vprintf_
+#else
+#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
+# define printf printf_
+# define sprintf sprintf_
+# define vsprintf vsprintf_
+# define snprintf snprintf_
+# define vsnprintf vsnprintf_
+# define vprintf vprintf_
+#endif
+#endif
+
+#endif /* PRINTF_H_ */
diff --git a/core/embed/rtl/printf.c b/core/embed/rtl/printf.c
index 0fa9319a8..bdf943887 100644
--- a/core/embed/rtl/printf.c
+++ b/core/embed/rtl/printf.c
@@ -52,7 +52,7 @@
#include "printf_config.h"
//#endif
-#include <printf/printf.h>
+#include <rtl/printf.h>
#ifdef __cplusplus
#include <cstdint>
diff --git a/core/embed/rtl/printf_config.h b/core/embed/rtl/printf_config.h
new file mode 100644
index 000000000..8a315e253
--- /dev/null
+++ b/core/embed/rtl/printf_config.h
@@ -0,0 +1,30 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#define PRINTF_SUPPORT_DECIMAL_SPECIFIERS 0
+#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 0
+#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER 1
+#define PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS 0
+#define PRINTF_USE_DOUBLE_INTERNALLY 0
+#define PRINTF_INTEGER_BUFFER_SIZE 32
+#define PRINTF_DECIMAL_BUFFER_SIZE 0
+
+static inline void putchar_(char c) {}
diff --git a/core/embed/sys/dbg/dbg_console.c b/core/embed/sys/dbg/dbg_console.c
index ceefb0fe9..ef297fa86 100644
--- a/core/embed/sys/dbg/dbg_console.c
+++ b/core/embed/sys/dbg/dbg_console.c
@@ -19,12 +19,12 @@
#include <trezor_rtl.h>
-#include <rtl/mini_printf.h>
+#include <rtl/printf.h>
#include <sys/dbg_console.h>
void dbg_console_vprintf(const char *fmt, va_list args) {
char temp[160];
- mini_vsnprintf(temp, sizeof(temp), fmt, args);
+ vsnprintf_(temp, sizeof(temp), fmt, args);
dbg_console_write(temp, strnlen(temp, sizeof(temp)));
}
diff --git a/core/embed/sys/dbg/syslog.c b/core/embed/sys/dbg/syslog.c
index 6302df008..e91189e8a 100644
--- a/core/embed/sys/dbg/syslog.c
+++ b/core/embed/sys/dbg/syslog.c
@@ -20,7 +20,7 @@
#include <trezor_rtl.h>
#include <rtl/logging.h>
-#include <rtl/mini_printf.h>
+#include <rtl/printf.h>
#include <rtl/strutils.h>
#include <sys/dbg_console.h>
#include <sys/systick.h>
@@ -226,7 +226,7 @@ void syslog_vprintf(const log_source_t* source, log_level_t level,
const char* fmt, va_list args) {
if (syslog_start_record(source, level)) {
char msg[160];
- size_t msg_len = mini_vsnprintf(msg, sizeof(msg), fmt, args);
+ size_t msg_len = vsnprintf_(msg, sizeof(msg), fmt, args);
syslog_write_chunk(msg, msg_len, true);
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.