refactor(core): the backlight driver functions 'API has been reviewed. The data types have been adapted to fit the actual data passed/used/returned.
What changed, and why it matters
This commit is a routine code cleanup of the screen backlight driver in Trezor hardware wallets. It changes function return types from plain integers to booleans and from signed to unsigned 8-bit values, and removes redundant range checks. There is no indication this fixes a security vulnerability; it appears to be a refactoring for code clarity and type correctness.
No immediate action required. Treat as normal refactoring. If integrating, verify downstream callers handle the new bool return values correctly and that uint8_t narrowing does not alter behavior on any platform-specific code paths.
Security signals we found
Type narrowing: int changed to uint8_t for backlight levels
Return type change: functions now return bool success instead of current level
Removed explicit range checks that become redundant with uint8_t parameter
No changelog entry, suggesting internal refactoring
Evidence from the diff
The patch refactors the backlight driver API across three STM32 implementations and one header. Key changes: backlight_init now returns bool (always true in current implementations); backlight_set and backlight_set_max_level return bool success instead of the current level; backlight_get returns uint8_t instead of int; internal state variables become uint8_t. The tps61043.c and tps61062.c implementations drop explicit negative/out-of-range checks because the uint8_t parameter already constrains values to 0-255. No functional behavior changes are evident for valid inputs, and the return values are not consumed by callers in this diff.
Changed components
core/embed/io/backlight/inc/io/backlight.hcore/embed/io/backlight/stm32/backlight_pin.ccore/embed/io/backlight/stm32/tps61043.ccore/embed/io/backlight/stm32u5/tps61062.cInspect captured patch +60 / −63
diff --git a/core/embed/io/backlight/inc/io/backlight.h b/core/embed/io/backlight/inc/io/backlight.h
index 42c7580e..be4fb2a1 100644
--- a/core/embed/io/backlight/inc/io/backlight.h
+++ b/core/embed/io/backlight/inc/io/backlight.h
@@ -19,6 +19,8 @@
#pragma once
+#include <trezor_types.h>
+
#define BACKLIGHT_MAX_LEVEL 255
#define BACKLIGHT_MIN_LEVEL 0
@@ -34,7 +36,9 @@ typedef enum {
// If the action is set to `BACKLIGHT_RESET`, the backlight level
// is set to zero level. If the action is set to `BACKLIGHT_RETAIN`,
// the backlight level is not changed (if possible).
-void backlight_init(backlight_action_t action);
+//
+// Returns `true` if the initialization was successful.
+bool backlight_init(backlight_action_t action);
// Deinitialize the backlight driver
//
@@ -44,20 +48,20 @@ void backlight_init(backlight_action_t action);
// is kept on.
void backlight_deinit(backlight_action_t action);
-// Request the backlight level in range 0-255 and returns the actual level set.
-// The requested level may be limited if its above the max_level limit.
+// Requests the backlight level in range 0-255 and returns whether
+// the function executed successfully or not. The requested level
+// may be limited if it's above the max_level limit.
//
-// If the level is outside the range, the function has no effect
-// and just returns the actual level set. If the backlight driver
-// is not initialized, the function returns 0.
-int backlight_set(int val);
+// If the backlight driver is not initialized, the function
+// returns "False".
+bool backlight_set(uint8_t val);
// Gets the backlight level in range 0-255
//
// Returns 0 if the backlight driver is not initialized.
-int backlight_get(void);
+uint8_t backlight_get(void);
// Set maximal backlight limit, limits the requested level to max_level value.
//
-// Returns 0 if the backlight driver is not initialized.
-int backlight_set_max_level(int max_level);
+// Returns "False" if the backlight driver is not initialized.
+bool backlight_set_max_level(uint8_t max_level);
diff --git a/core/embed/io/backlight/stm32/backlight_pin.c b/core/embed/io/backlight/stm32/backlight_pin.c
index 1006c75f..b7bf73d1 100644
--- a/core/embed/io/backlight/stm32/backlight_pin.c
+++ b/core/embed/io/backlight/stm32/backlight_pin.c
@@ -26,7 +26,7 @@ typedef struct {
// Set if driver is initialized
bool initialized;
// Current backlight level in range 0-255
- int current_level;
+ uint8_t current_level;
} backlight_driver_t;
@@ -52,11 +52,11 @@ static void backlight_off(void) {
HAL_GPIO_Init(BACKLIGHT_PIN_PORT, &GPIO_InitStructure);
}
-void backlight_init(backlight_action_t action) {
+bool backlight_init(backlight_action_t action) {
backlight_driver_t *drv = &g_backlight_driver;
if (drv->initialized) {
- return;
+ return true;
}
BACKLIGHT_PIN_CLK_ENABLE();
@@ -66,6 +66,8 @@ void backlight_init(backlight_action_t action) {
};
drv->initialized = true;
+
+ return true;
}
void backlight_deinit(backlight_action_t action) {
@@ -79,10 +81,11 @@ void backlight_deinit(backlight_action_t action) {
}
}
-int backlight_set(int val) {
+bool backlight_set(uint8_t val) {
backlight_driver_t *drv = &g_backlight_driver;
+
if (!drv->initialized) {
- return 0;
+ return false;
}
if (val > 0) {
@@ -90,11 +93,13 @@ int backlight_set(int val) {
} else {
backlight_off();
}
+
drv->current_level = val;
- return val;
+
+ return true;
}
-int backlight_get(void) {
+uint8_t backlight_get(void) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
return 0;
diff --git a/core/embed/io/backlight/stm32/tps61043.c b/core/embed/io/backlight/stm32/tps61043.c
index 1748acf4..148c30c2 100644
--- a/core/embed/io/backlight/stm32/tps61043.c
+++ b/core/embed/io/backlight/stm32/tps61043.c
@@ -36,7 +36,7 @@ typedef struct {
// Set if driver is initialized
bool initialized;
// Current backlight level in range 0-255
- int current_level;
+ uint8_t current_level;
} backlight_driver_t;
@@ -45,11 +45,11 @@ static backlight_driver_t g_backlight_driver = {
.initialized = false,
};
-void backlight_init(backlight_action_t action) {
+bool backlight_init(backlight_action_t action) {
backlight_driver_t *drv = &g_backlight_driver;
if (drv->initialized) {
- return;
+ return true;
}
memset(drv, 0, sizeof(backlight_driver_t));
@@ -188,6 +188,8 @@ void backlight_init(backlight_action_t action) {
drv->initialized = true;
backlight_set(initial_level);
+
+ return true;
}
void backlight_deinit(backlight_action_t action) {
@@ -265,30 +267,28 @@ static void backlight_wakeup_pulse(void) {
HAL_GPIO_Init(TPS61043_PORT, &GPIO_InitStructure);
}
-int backlight_set(int level) {
+bool backlight_set(uint8_t level) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
- return 0;
+ return false;
}
- if (level >= 0 && level <= 255) {
- // TPS61043 goes to shutdown when duty cycle is 0 (after 32ms),
- // so we need to set GPIO to high for at least 500us
- // to wake it up.
- if (TPS61043_TIM->TPS61043_TIM_CCR == 0 && level != 0) {
- backlight_wakeup_pulse();
- }
+ // TPS61043 goes to shutdown when duty cycle is 0 (after 32ms),
+ // so we need to set GPIO to high for at least 500us
+ // to wake it up.
+ if (TPS61043_TIM->TPS61043_TIM_CCR == 0 && level != 0) {
+ backlight_wakeup_pulse();
+ }
- TPS61043_TIM->CCR1 = (LED_PWM_TIM_PERIOD * level) / 255;
+ TPS61043_TIM->CCR1 = (LED_PWM_TIM_PERIOD * level) / 255;
- drv->current_level = level;
- }
+ drv->current_level = level;
- return drv->current_level;
+ return true;
}
-int backlight_get(void) {
+uint8_t backlight_get(void) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
diff --git a/core/embed/io/backlight/stm32u5/tps61062.c b/core/embed/io/backlight/stm32u5/tps61062.c
index 0fd3ae26..99d9df25 100644
--- a/core/embed/io/backlight/stm32u5/tps61062.c
+++ b/core/embed/io/backlight/stm32u5/tps61062.c
@@ -49,14 +49,14 @@ typedef struct {
bool initialized;
// Level requested (0-255)
- int requested_level;
- int current_level;
+ uint8_t requested_level;
+ uint8_t current_level;
// Current step in range 0-32
int current_step;
// Max backlight level
- int max_level;
+ uint8_t max_level;
DMA_HandleTypeDef dma;
TIM_HandleTypeDef tim;
@@ -73,11 +73,11 @@ static void backlight_control_up(uint32_t *data, int steps);
static void backlight_control_down(uint32_t *data, int steps);
static void backlight_shutdown();
-void backlight_init(backlight_action_t action) {
+bool backlight_init(backlight_action_t action) {
backlight_driver_t *drv = &g_backlight_driver;
if (drv->initialized) {
- return;
+ return true;
}
memset(drv, 0, sizeof(backlight_driver_t));
@@ -161,6 +161,8 @@ void backlight_init(backlight_action_t action) {
drv->requested_level = BACKLIGHT_MIN_LEVEL;
drv->initialized = true;
+
+ return true;
}
void backlight_deinit(backlight_action_t action) {
@@ -189,29 +191,24 @@ void backlight_deinit(backlight_action_t action) {
drv->initialized = false;
}
-int backlight_set(int val) {
+bool backlight_set(uint8_t val) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
- return 0;
- }
-
- // Requested level out of range
- if (val < BACKLIGHT_MIN_LEVEL || val > BACKLIGHT_MAX_LEVEL) {
- return drv->current_level;
+ return false;
}
// Capture requested level.
drv->requested_level = val;
- int requested_level_limited = drv->requested_level;
+ uint8_t requested_level_limited = drv->requested_level;
if (drv->requested_level > drv->max_level) {
requested_level_limited = drv->max_level;
}
// No action required
if (requested_level_limited == drv->current_level) {
- return drv->current_level;
+ return true;
}
// New backlight level
@@ -222,7 +219,7 @@ int backlight_set(int val) {
if (set_step == 0) {
backlight_shutdown();
drv->current_step = 0;
- return drv->current_level;
+ return true;
}
if (HAL_DMA_GetState(&drv->dma) == HAL_DMA_STATE_BUSY) {
@@ -261,10 +258,10 @@ int backlight_set(int val) {
drv->current_step = set_step;
- return drv->current_level;
+ return true;
}
-int backlight_get(void) {
+uint8_t backlight_get(void) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
@@ -275,25 +272,16 @@ int backlight_get(void) {
}
// Set maximal backlight level
-int backlight_set_max_level(int max_level) {
+bool backlight_set_max_level(uint8_t max_level) {
backlight_driver_t *drv = &g_backlight_driver;
if (!drv->initialized) {
- return 0;
- }
-
- if (max_level < BACKLIGHT_MIN_LEVEL) {
- max_level = BACKLIGHT_MIN_LEVEL;
- }
-
- if (max_level > BACKLIGHT_MAX_LEVEL) {
- max_level = BACKLIGHT_MAX_LEVEL;
+ return false;
}
drv->max_level = max_level;
- backlight_set(drv->requested_level);
- return drv->current_level;
+ return backlight_set(drv->requested_level);
}
static void backlight_control_up(uint32_t *data, int steps) {
Why this scored 17/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.