[PATCH v2 2/2] i2c: sandbox: Avoid calling dev_read_*() if CONFIG_OF_PLATDATA=y

Marek Vasut marek.vasut+renesas at mailbox.org
Fri Nov 21 21:55:46 CET 2025


If CONFIG_OF_PLATDATA=y , then the udevice has no valid OF node associated
with it and ofnode_valid(node) evaluates to 0. The dev_read_u32_default()
call ultimately reaches ofnode_read_u32_index() which invokes fdt_getprop()
and passes result of ofnode_to_offset(node) as an offset parameter into it.

The ofnode_to_offset(node) returns -1 for invalid node, which leads to an
fdt_getprop(..., -1, ...) invocation, which will crash sandbox with SIGSEGV
because libfdt can not handle negative node offsets without full tree check,
which U-Boot inhibits to keep size lower.

Since i2c_child_post_bind() already calls dev_has_ofnode(dev), reuse the
same call and assign i2c->speed_hz = I2C_SPEED_STANDARD_RATE in case the
device has no valid node associated with it, and do not call any of the
dev_read_*() functions for devices without valid nodes.

Signed-off-by: Marek Vasut <marek.vasut+renesas at mailbox.org>
---
Cc: Heiko Schocher <hs at nabladev.com>
Cc: Tom Rini <trini at konsulko.com>
Cc: u-boot at lists.denx.de
---
V2: Fix up i2c_pre_probe() using dev_has_ofnode() and drop remaining
    if CONFIG_IS_ENABLED(OF_REAL) ifdeffery
---
 drivers/i2c/i2c-uclass.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/i2c/i2c-uclass.c b/drivers/i2c/i2c-uclass.c
index 97466597d77..bb32ebd9981 100644
--- a/drivers/i2c/i2c-uclass.c
+++ b/drivers/i2c/i2c-uclass.c
@@ -703,12 +703,14 @@ int i2c_deblock(struct udevice *bus)
 
 static int i2c_pre_probe(struct udevice *dev)
 {
-#if CONFIG_IS_ENABLED(OF_REAL)
 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
 	unsigned int max = 0;
 	ofnode node;
 	int ret;
 
+	if (!dev_has_ofnode(dev))
+		return 0;
+
 	i2c->max_transaction_bytes = 0;
 	dev_for_each_subnode(node, dev) {
 		ret = ofnode_read_u32(node,
@@ -720,22 +722,22 @@ static int i2c_pre_probe(struct udevice *dev)
 
 	debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
 	      dev->name, i2c->max_transaction_bytes);
-#endif
+
 	return 0;
 }
 
 static int i2c_post_probe(struct udevice *dev)
 {
-#if CONFIG_IS_ENABLED(OF_REAL)
 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
 
-	i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
-					     I2C_SPEED_STANDARD_RATE);
+	if (dev_has_ofnode(dev)) {
+		i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
+						     I2C_SPEED_STANDARD_RATE);
+	} else {
+		i2c->speed_hz = I2C_SPEED_STANDARD_RATE;
+	}
 
 	return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
-#else
-	return 0;
-#endif
 }
 
 static int i2c_child_post_bind(struct udevice *dev)
-- 
2.51.0



More information about the U-Boot mailing list