U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] board: stemmy: Read persistent radio addresses
@ 2026-11-24  9:42 Linus Walleij
  2026-11-27 19:00 ` Stephan Gerhold
  0 siblings, 1 reply; 2+ messages in thread
From: Linus Walleij @ 2026-11-24  9:42 UTC (permalink / raw)
  To: u-boot, Tom Rini, Stefan Hansson, Stephan Gerhold; +Cc: Linus Walleij

Samsung stores persistent Wi-Fi and Bluetooth addresses in several
locations depending on the device and firmware. The eMMC partition
numbers also differ between devices.

Locate the EFS and DATAFS partitions by name. Search the known Samsung
Wi-Fi address files in priority order and support both Bluetooth EFS
layouts. Accept colon-separated and compact addresses, including Codina's
bt_macaddr: record, while rejecting known firmware placeholders.

Pass valid addresses to the operating system through the radio device
tree nodes from ft_board_setup().

Signed-off-by: Linus Walleij <linusw@kernel.org>
---
Changes in v2:
- The code was overly cautious, use exact EFS and DATAFS names and
  drop both partition lookup fallbacks.
- Clarify that fs_read() closes the filesystem even on failure.
- Use log_warning() and log_info() for radio address messages.
- Link to v1: https://patch.msgid.link/20261120-ux500-efs-v1-1-58147e2eae41@kernel.org
---
 MAINTAINERS                     |   1 +
 board/ste/stemmy/Makefile       |   2 +-
 board/ste/stemmy/stemmy-radio.c | 287 ++++++++++++++++++++++++++++++++++++++++
 board/ste/stemmy/stemmy-radio.h |   8 ++
 board/ste/stemmy/stemmy.c       |   4 +
 5 files changed, 301 insertions(+), 1 deletion(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 6edf740a9ceb..ceb474751906 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -772,6 +772,7 @@ R:	Linus Walleij <linusw@kernel.org>
 S:	Maintained
 F:	arch/arm/dts/ste-*
 F:	arch/arm/mach-u8500/
+F:	board/ste/stemmy/
 F:	drivers/gpio/nmk_gpio.c
 F:	drivers/pinctrl/pinctrl-nomadik.c
 F:	drivers/phy/phy-ab8500-usb.c
diff --git a/board/ste/stemmy/Makefile b/board/ste/stemmy/Makefile
index 1245099bc918..f4e97c9d9972 100644
--- a/board/ste/stemmy/Makefile
+++ b/board/ste/stemmy/Makefile
@@ -1,2 +1,2 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
-obj-y	:= stemmy.o
+obj-y	:= stemmy.o stemmy-radio.o
diff --git a/board/ste/stemmy/stemmy-radio.c b/board/ste/stemmy/stemmy-radio.c
new file mode 100644
index 000000000000..4a09ebcfe994
--- /dev/null
+++ b/board/ste/stemmy/stemmy-radio.c
@@ -0,0 +1,287 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Samsung stores persistent radio addresses in several locations depending
+ * on the firmware and device. Pass them to the operating system through the
+ * device tree when they can be found on the internal eMMC.
+ *
+ * Copyright (C) 2026 Linus Walleij <linusw@kernel.org>
+ */
+
+#include <blk.h>
+#include <dm.h>
+#include <fdt_support.h>
+#include <fs.h>
+#include <log.h>
+#include <mmc.h>
+#include <net-common.h>
+#include <part.h>
+#include <dm/device-internal.h>
+#include <dm/read.h>
+#include <dm/uclass.h>
+#include <linux/ctype.h>
+
+#include "stemmy-radio.h"
+
+#define RADIO_ADDR_BUF_SIZE	512
+
+enum stemmy_radio_partition {
+	STEMMY_PART_EFS,
+	STEMMY_PART_DATAFS,
+	STEMMY_PART_COUNT,
+};
+
+struct stemmy_radio_source {
+	enum stemmy_radio_partition partition;
+	const char *path;
+};
+
+struct stemmy_radio_storage {
+	struct blk_desc *desc;
+	int part[STEMMY_PART_COUNT];
+};
+
+static const char * const stemmy_partition_names[STEMMY_PART_COUNT] = {
+	[STEMMY_PART_EFS] = "EFS",
+	[STEMMY_PART_DATAFS] = "DATAFS",
+};
+
+static const struct stemmy_radio_source stemmy_wifi_sources[] = {
+	{ STEMMY_PART_EFS, "/wifi/.mac.info" },
+	{ STEMMY_PART_EFS, "/wifi/.mac.cob" },
+	{ STEMMY_PART_EFS, "/imei/.nvmac.info" },
+	{ STEMMY_PART_DATAFS, "/.nvmac.info" },
+	{ STEMMY_PART_DATAFS, "/.mac.info" },
+	{ STEMMY_PART_DATAFS, "/misc/wifi/.mac.info" },
+};
+
+static const struct stemmy_radio_source stemmy_bt_sources[] = {
+	{ STEMMY_PART_EFS, "/bluetooth/bt_addr" },
+	{ STEMMY_PART_EFS, "/bluetooth/addr.txt" },
+};
+
+static struct blk_desc *stemmy_radio_device(void)
+{
+	struct blk_desc *desc;
+	struct udevice *dev;
+	struct uclass *uc;
+	struct mmc *mmc;
+
+	if (uclass_get(UCLASS_MMC, &uc))
+		return NULL;
+
+	uclass_foreach_dev(dev, uc) {
+		/* Android's mmcblk0 is not necessarily U-Boot's mmc 0. */
+		if (!dev_read_bool(dev, "non-removable") || device_probe(dev))
+			continue;
+
+		mmc = mmc_get_mmc_dev(dev);
+		if (!mmc || mmc_init(mmc) || !IS_MMC(mmc))
+			continue;
+
+		/* Persistent data is in the user area, not an eMMC boot area. */
+		desc = mmc_get_blk_desc(mmc);
+		if (!desc || blk_dselect_hwpart(desc, 0))
+			continue;
+
+		return desc;
+	}
+
+	return NULL;
+}
+
+static bool stemmy_addr_tail_valid(const char *buf, size_t len, size_t pos)
+{
+	for (; pos < len; pos++) {
+		if (buf[pos] && !isspace((unsigned char)buf[pos]))
+			return false;
+	}
+
+	return true;
+}
+
+static int stemmy_parse_radio_addr(char *buf, size_t len, u8 *addr)
+{
+	const char *prefix = "bt_macaddr:";
+	const char *str = buf;
+	size_t pos = 0;
+	bool prefixed = false;
+	int i;
+
+	str = strstr(buf, prefix);
+	if (str) {
+		str += strlen(prefix);
+		pos = str - buf;
+		prefixed = true;
+	} else {
+		str = buf;
+		while (pos < len && isspace((unsigned char)str[pos]))
+			pos++;
+		str += pos;
+	}
+
+	if (pos + 17 <= len && str[2] == ':' && str[5] == ':' &&
+	    str[8] == ':' && str[11] == ':' && str[14] == ':') {
+		for (i = 0; i < ETH_ALEN; i++) {
+			if (hex2bin(addr + i, str + 3 * i, 1))
+				return -EINVAL;
+		}
+		pos += 17;
+	} else if (pos + 12 <= len) {
+		if (hex2bin(addr, str, ETH_ALEN))
+			return -EINVAL;
+		pos += 12;
+	} else {
+		return -EINVAL;
+	}
+
+	if (!prefixed && !stemmy_addr_tail_valid(buf, len, pos))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int stemmy_read_radio_addr(struct blk_desc *desc, int part,
+				  const char *path, u8 *addr)
+{
+	char buf[RADIO_ADDR_BUF_SIZE + 1];
+	loff_t len;
+	int ret;
+
+	ret = fs_set_blk_dev_with_part(desc, part);
+	if (ret)
+		return ret;
+
+	if (fs_get_type() != FS_TYPE_EXT) {
+		fs_close();
+		return -EINVAL;
+	}
+
+	/* fs_read() closes the filesystem even when the read fails. */
+	ret = fs_read(path, (ulong)buf, 0, RADIO_ADDR_BUF_SIZE, &len);
+	if (ret)
+		return ret;
+	if (!len || len > RADIO_ADDR_BUF_SIZE)
+		return -EINVAL;
+
+	buf[len] = '\0';
+
+	return stemmy_parse_radio_addr(buf, len, addr);
+}
+
+static bool stemmy_radio_addr_valid(const u8 *addr, bool bluetooth)
+{
+	static const u8 wifi_placeholders[][ETH_ALEN] = {
+		{ 0x00, 0x90, 0x4c, 0xc5, 0x12, 0x38 },
+		{ 0x00, 0x12, 0x34, 0x00, 0x0d, 0xef },
+	};
+	static const u8 bluetooth_placeholder[ETH_ALEN] = {
+		0x00, 0x12, 0x47, 0x01, 0x23, 0x45,
+	};
+	size_t i;
+
+	if (is_zero_ethaddr(addr) || is_broadcast_ethaddr(addr))
+		return false;
+	if (bluetooth)
+		return !!memcmp(addr, bluetooth_placeholder, ETH_ALEN);
+	if (is_multicast_ethaddr(addr))
+		return false;
+
+	for (i = 0; i < ARRAY_SIZE(wifi_placeholders); i++) {
+		if (!memcmp(addr, wifi_placeholders[i], ETH_ALEN))
+			return false;
+	}
+
+	return true;
+}
+
+static int stemmy_find_radio_addr(struct stemmy_radio_storage *storage,
+				  const struct stemmy_radio_source *sources,
+				  size_t count, bool bluetooth, u8 *addr)
+{
+	size_t i;
+	int part, ret;
+
+	for (i = 0; i < count; i++) {
+		part = storage->part[sources[i].partition];
+		if (part < 0)
+			continue;
+
+		ret = stemmy_read_radio_addr(storage->desc, part,
+					     sources[i].path, addr);
+		if (ret || !stemmy_radio_addr_valid(addr, bluetooth))
+			continue;
+
+		log_debug("Read radio address from %s:%s\n",
+			  stemmy_partition_names[sources[i].partition],
+			  sources[i].path);
+		return 0;
+	}
+
+	return -ENOENT;
+}
+
+static void stemmy_fixup_radio_addr(void *fdt,
+				    struct stemmy_radio_storage *storage,
+				    const struct stemmy_radio_source *sources,
+				    size_t count, const char *compatible,
+				    bool bluetooth)
+{
+	const char *property = bluetooth ? "local-bd-address" :
+					       "local-mac-address";
+	const char *name = bluetooth ? "Bluetooth BD" : "WiFi MAC";
+	u8 addr[ETH_ALEN];
+	int node, ret, i;
+
+	ret = stemmy_find_radio_addr(storage, sources, count, bluetooth, addr);
+	if (ret) {
+		log_warning("Persistent %s missing\n", name);
+		return;
+	}
+
+	log_info("Persistent %s: %pM\n", name, addr);
+
+	node = fdt_node_offset_by_compatible(fdt, -1, compatible);
+	if (node < 0)
+		return;
+
+	/* local-bd-address uses Bluetooth (least significant byte first) order. */
+	if (bluetooth) {
+		for (i = 0; i < ETH_ALEN / 2; i++)
+			swap(addr[i], addr[ETH_ALEN - i - 1]);
+	} else if (fdt_getprop(fdt, node, "mac-address", NULL)) {
+		/* Linux prefers mac-address if it is already present. */
+		ret = fdt_setprop(fdt, node, "mac-address", addr, sizeof(addr));
+		if (ret)
+			goto err;
+	}
+
+	ret = fdt_setprop(fdt, node, property, addr, sizeof(addr));
+	if (!ret)
+		return;
+
+err:
+	log_warning("Cannot set %s: %s\n", property, fdt_strerror(ret));
+}
+
+void stemmy_radio_fixup(void *fdt)
+{
+	struct stemmy_radio_storage storage;
+	struct disk_partition info;
+	int i;
+
+	storage.desc = stemmy_radio_device();
+	if (!storage.desc)
+		return;
+
+	for (i = 0; i < STEMMY_PART_COUNT; i++)
+		storage.part[i] = part_get_info_by_name(storage.desc,
+							stemmy_partition_names[i],
+							&info);
+
+	stemmy_fixup_radio_addr(fdt, &storage, stemmy_wifi_sources,
+				ARRAY_SIZE(stemmy_wifi_sources),
+				"brcm,bcm4329-fmac", false);
+	stemmy_fixup_radio_addr(fdt, &storage, stemmy_bt_sources,
+				ARRAY_SIZE(stemmy_bt_sources),
+				"brcm,bcm4330-bt", true);
+}
diff --git a/board/ste/stemmy/stemmy-radio.h b/board/ste/stemmy/stemmy-radio.h
new file mode 100644
index 000000000000..3c3ce03669b8
--- /dev/null
+++ b/board/ste/stemmy/stemmy-radio.h
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __STEMMY_RADIO_H
+#define __STEMMY_RADIO_H
+
+void stemmy_radio_fixup(void *fdt);
+
+#endif
diff --git a/board/ste/stemmy/stemmy.c b/board/ste/stemmy/stemmy.c
index 8526deeb29bc..97957e12d5b0 100644
--- a/board/ste/stemmy/stemmy.c
+++ b/board/ste/stemmy/stemmy.c
@@ -12,6 +12,8 @@
 #include <asm/setup.h>
 #include <asm/system.h>
 
+#include "stemmy-radio.h"
+
 DECLARE_GLOBAL_DATA_PTR;
 
 /* Parse atags provided by Samsung bootloader to get available memory */
@@ -545,5 +547,7 @@ int ft_board_setup(void *fdt, struct bd_info *bd)
 		skomer_kyle_patch_display(fdt);
 	}
 
+	stemmy_radio_fixup(fdt);
+
 	return 0;
 }

---
base-commit: f2e99724255470a12119f696a0c579fd873cbef9
change-id: 20261110-ux500-efs-cb818375e8f6

Best regards,
--  
Linus Walleij <linusw@kernel.org>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27 19:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  9:42 [PATCH v2] board: stemmy: Read persistent radio addresses Linus Walleij
2026-11-27 19:00 ` Stephan Gerhold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox