logging: unify esp_log_set_vprintf calls and conditionally compile all esp log functions
What changed, and why it matters
This commit is mostly a cleanup of how logging is set up on the device. It moves where the event loop is created, makes some log functions disappear when logging is completely disabled, and changes one error message to a less alarming one. The visible changes do not appear to fix a clear security bug, but they do remove a few rough edges that could make the device behave unexpectedly during boot or when logging is configured in unusual ways.
Treat as a routine maintenance/hardening commit. Reviewers should verify that moving event-loop creation earlier in boot does not change ordering assumptions for other subsystems, and that the CONFIG_LOG_DEFAULT_LEVEL_NONE guards do not hide legitimate runtime log-level changes needed by downstream code.
Security signals we found
Conditional compilation of logging functions to prevent use when logging is disabled
Centralized event-loop creation to avoid duplicate initialization
Changed duplicate event-loop creation from error to benign idempotent return
Renamed saved log-level variable for clarity in USB mass-storage path
Added comment linking disabled logging to OTA update stability
Evidence from the diff
The patch unifies calls to esp_log_set_vprintf and wraps several ESP-IDF logging helpers in CONFIG_LOG_DEFAULT_LEVEL_NONE guards so they are only compiled when logging is enabled. It also moves default event-loop creation from gui_init() into boot_process() and changes esp_event_loop_create_default() to return ESP_OK when the loop already exists, downgrading the log from JADE_LOGE to JADE_LOGI. gui_init() now takes a create_event_loop flag so callers can avoid creating it twice. A variable rename (initial_log_level -> usbstorage_saved_log_level) and a comment about OS lockups during OTA are included. No direct vulnerability is patched; the changes are architectural hardening and build-time consistency.
Changed components
libjade/esp_event.clibjade/gui.clibjade/include/esp_log.hmain/gui.cmain/gui.hmain/main.cmain/storage.cmain/usbhmsc/usbhmsc.cInspect captured patch +42 / −27
diff --git a/libjade/esp_event.c b/libjade/esp_event.c
index 1c75f6c..d8facfe 100644
--- a/libjade/esp_event.c
+++ b/libjade/esp_event.c
@@ -95,8 +95,8 @@ void* _default_event_loop(void* params)
esp_err_t esp_event_loop_create_default(void)
{
if (_default_event_loop_task) {
- JADE_LOGE("Default event loop already created");
- return ESP_ERR_INVALID_STATE;
+ JADE_LOGI("Default event loop already created");
+ return ESP_OK;
}
// init event handlers map
int ret = wally_map_init(1000, NULL, &_event_handlers);
diff --git a/libjade/gui.c b/libjade/gui.c
index 4933600..e8feda4 100644
--- a/libjade/gui.c
+++ b/libjade/gui.c
@@ -82,7 +82,7 @@ bool gui_get_flipped_orientation(void) { return false; }
bool gui_set_flipped_orientation(const bool flipped_orientation) { return false; }
-void gui_init(TaskHandle_t* gui_h)
+void gui_init(TaskHandle_t* gui_h, const bool create_event_loop)
{
// create a blank activity
current_activity = gui_make_activity();
diff --git a/libjade/include/esp_log.h b/libjade/include/esp_log.h
index 7f337fc..f632269 100644
--- a/libjade/include/esp_log.h
+++ b/libjade/include/esp_log.h
@@ -39,10 +39,15 @@ extern esp_log_level_t _libjade_log_level;
fprintf(stderr, "ERROR:" f ":" fmt "\n", __VA_ARGS__); \
} while (0)
+#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE
static inline void esp_log_level_set(const char* tag, esp_log_level_t level)
{
// Do nothing, so our internal call to this function doesn't
- // overwrite the callers desired log level
+ // overwrite the callers desired log level.
+ // We only provide an implementation when DEFAULT_LEVEL_NONE is not set,
+ // so that any use of this function without that guard can be caught
+ // at compile time.
}
+#endif
#endif // __LIBJADE_ESP_LOG__
diff --git a/main/gui.c b/main/gui.c
index 5860b7c..4fd5582 100644
--- a/main/gui.c
+++ b/main/gui.c
@@ -260,7 +260,7 @@ bool gui_set_flipped_orientation(const bool flipped_orientation)
return gui_orientation_flipped;
}
-void gui_init(TaskHandle_t* gui_h)
+void gui_init(TaskHandle_t* gui_h, const bool create_event_loop)
{
#ifdef CONFIG_LIBJADE
if (gui_mutex) {
@@ -281,9 +281,11 @@ void gui_init(TaskHandle_t* gui_h)
// create a blank activity
current_activity = gui_make_activity();
- // create the default event loop used by btns
- const esp_err_t rc = esp_event_loop_create_default();
- JADE_ASSERT(rc == ESP_OK);
+ if (create_event_loop) {
+ // create the default event loop used by btns
+ const esp_err_t rc = esp_event_loop_create_default();
+ JADE_ASSERT(rc == ESP_OK);
+ }
// Create main input queue (ringbuffer)
gui_input_queue = xRingbufferCreate(32 * sizeof(gui_task_job_t), RINGBUF_TYPE_NOSPLIT);
diff --git a/main/gui.h b/main/gui.h
index 2c03fab..6ec2e75 100644
--- a/main/gui.h
+++ b/main/gui.h
@@ -422,7 +422,7 @@ void gui_next_qrcode_color(void);
bool gui_get_flipped_orientation(void);
bool gui_set_flipped_orientation(bool flipped_orientation);
-void gui_init(TaskHandle_t* gui_h);
+void gui_init(TaskHandle_t* gui_h, bool create_event_loop);
bool gui_initialized(void);
void gui_make_activity_ex(gui_activity_t** ppact, const bool has_status_bar, const char* title, const bool managed);
diff --git a/main/main.c b/main/main.c
index a620289..fcdb045 100644
--- a/main/main.c
+++ b/main/main.c
@@ -179,9 +179,23 @@ static void boot_process(void)
JADE_ABORT();
}
-#ifdef CONFIG_LOG_CBOR
+ if (!serial_init(serial_handle)) {
+ JADE_ABORT();
+ }
+
+ // Create the default event loop here as multiple components depend on it
+ JADE_ASSERT(esp_event_loop_create_default() == ESP_OK);
+
+#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE
+#if defined(CONFIG_LOG_CBOR)
esp_log_set_vprintf(serial_logger);
+#elif defined(CONFIG_LOG_WIFI)
+ JADE_ASSERT(wifi_socket_server_logger_start() == ESP_OK);
+ esp_log_set_vprintf(wifi_socket_server_logger);
+#elif defined(CONFIG_BOARD_TYPE_QEMU)
+ esp_log_set_vprintf(qemu_uart0_logger);
#endif
+#endif // CONFIG_LOG_DEFAULT_LEVEL_NONE
const esp_err_t rc = power_init();
JADE_ASSERT(rc == ESP_OK);
@@ -192,7 +206,8 @@ static void boot_process(void)
keychain_init_cache();
display_init(gui_handle);
- gui_init(gui_handle);
+ const bool create_event_loop = false;
+ gui_init(gui_handle, create_event_loop);
// Display splash screen with Blockstream logo. Carry out further initialisation
// while that screen is shown for a short time. Then test to see whether the
@@ -218,15 +233,6 @@ static void boot_process(void)
usbstorage_init();
#endif
- if (!serial_init(serial_handle)) {
- JADE_ABORT();
- }
-
-#ifdef CONFIG_LOG_WIFI
- JADE_ASSERT(wifi_socket_server_logger_start() == ESP_OK);
- esp_log_set_vprintf(wifi_socket_server_logger);
-#endif // CONFIG_LOG_WIFI
-
#ifdef CONFIG_ETH_USE_OPENETH
if (!qemu_tcp_init(qemu_tcp_handle)) {
JADE_LOGI("Failed to start qemu tcp handler");
@@ -240,10 +246,6 @@ static void boot_process(void)
#endif
#endif
-#if defined(CONFIG_BOARD_TYPE_QEMU) && !defined(CONFIG_LOG_DEFAULT_LEVEL_NONE)
- esp_log_set_vprintf(qemu_uart0_logger);
-#endif
-
sensitive_init();
temp_stack_init();
diff --git a/main/storage.c b/main/storage.c
index c8e1876..9543cf8 100644
--- a/main/storage.c
+++ b/main/storage.c
@@ -349,7 +349,9 @@ bool storage_init(void)
}
}
+#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE
esp_log_level_set("nvs", ESP_LOG_ERROR);
+#endif
// Erase any now-deprecated keys
erase_key(DEFAULT_NAMESPACE, CLICK_EVENT_FIELD);
diff --git a/main/usbhmsc/usbhmsc.c b/main/usbhmsc/usbhmsc.c
index 38a987e..813e1b5 100644
--- a/main/usbhmsc/usbhmsc.c
+++ b/main/usbhmsc/usbhmsc.c
@@ -31,9 +31,13 @@ static EventGroupHandle_t usbstorage_flags = NULL;
static QueueHandle_t usbstorage_msc_queue = NULL;
static usbstorage_state_t usbstorage_state = USBSTATE_NONE;
// Disable logging when switching from USB serial to USB storage
+// to work around O/S lockups that freeze OTA updates.
+// Only needed when logging is not disabled completely.
+#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE
#define USBSTORAGE_DISABLE_LOGGING
+#endif
#ifdef USBSTORAGE_DISABLE_LOGGING
-static esp_log_level_t initial_log_level;
+static esp_log_level_t usbstorage_saved_log_level;
#endif
static usbstorage_state_t usbstorage_state_get(void)
@@ -253,7 +257,7 @@ EventGroupHandle_t usbstorage_start(void)
#ifdef USBSTORAGE_DISABLE_LOGGING
// Record initial log level and set logging to NONE
- initial_log_level = esp_log_level_get(NULL);
+ usbstorage_saved_log_level = esp_log_level_get(NULL);
esp_log_level_set("*", ESP_LOG_NONE);
#endif
@@ -301,7 +305,7 @@ void usbstorage_stop(void)
#ifdef USBSTORAGE_DISABLE_LOGGING
// Return to initial log level
- esp_log_level_set("*", initial_log_level);
+ esp_log_level_set("*", usbstorage_saved_log_level);
esp_log_level_set("nvs", ESP_LOG_ERROR); // As per storage_init()
#endif
}
Why this scored 25/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.