[OpenWrt-Devel] [PATCH 4/5] target/generic: add ascii search option to mtd-mac-address helper
André Valentin
avalentin at marcant.net
Mon Oct 21 08:32:13 EDT 2019
This improves the function of_get_mac_address_mtd the be able to handle
u-boot environement. It is now possible to read a MAC address from the env
and set this in the dt.
Code has been taken from ar71xx.
Signed-off-by: André Valentin <avalentin at marcant.net>
---
...ET-add-of_get_mac_address_mtd_search.patch | 96 +++++++++++++++++++
...ET-add-of_get_mac_address_mtd_search.patch | 96 +++++++++++++++++++
2 files changed, 192 insertions(+)
create mode 100644 target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch
create mode 100644 target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch
diff --git a/target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch b/target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch
new file mode 100644
index 0000000000..bf8ff608c0
--- /dev/null
+++ b/target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch
@@ -0,0 +1,96 @@
+--- a/drivers/of/of_net.c
++++ b/drivers/of/of_net.c
+@@ -12,6 +12,7 @@
+ #include <linux/phy.h>
+ #include <linux/export.h>
+ #include <linux/mtd/mtd.h>
++#include <linux/vmalloc.h>
+
+ /**
+ * of_get_phy_mode - Get phy mode for given device_node
+@@ -49,6 +50,28 @@ static void *of_get_mac_addr(struct devi
+ return NULL;
+ }
+
++char *of_get_mac_address_mtd_helper(const char *name, const char *buf, unsigned buf_len)
++{
++ unsigned len = strlen(name);
++ char *cur, *last;
++
++ if (buf_len == 0 || len == 0)
++ return NULL;
++
++ if (buf_len < len)
++ return NULL;
++
++ if (len == 1)
++ return memchr(buf, (int) *name, buf_len);
++
++ last = (char *) buf + buf_len - len;
++ for (cur = (char *) buf; cur <= last; cur++)
++ if (cur[0] == name[0] && memcmp(cur, name, len) == 0)
++ return cur + len;
++
++ return NULL;
++}
++
+ static const void *of_get_mac_address_mtd(struct device_node *np)
+ {
+ #ifdef CONFIG_MTD
+@@ -58,6 +81,7 @@ static const void *of_get_mac_address_mt
+ int size, ret;
+ struct mtd_info *mtd;
+ const char *part;
++ const char *search;
+ const __be32 *list;
+ phandle phandle;
+ u32 mac_inc = 0;
+@@ -84,8 +109,46 @@ static const void *of_get_mac_address_mt
+ if (IS_ERR(mtd))
+ return NULL;
+
+- ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
+- put_mtd_device(mtd);
++ if (of_property_read_string(np, "mtd-mac-search", &search) == 0) {
++ // try to find mac address in mtd
++ char *buf;
++ char *mac_str;
++ int t;
++
++ buf = vmalloc(mtd->size);
++
++ ret = mtd_read(mtd, be32_to_cpup(list), mtd->size-be32_to_cpup(list), &retlen, buf);
++ put_mtd_device(mtd);
++
++ buf[retlen - 1] = '\0';
++ mac_str = of_get_mac_address_mtd_helper(search, buf, retlen);
++
++ if (!mac_str) {
++ vfree(buf);
++ return NULL;;
++ }
++
++ if (strlen(mac_str) == 19 && mac_str[0] == '"' && mac_str[18] == '"') {
++ mac_str[18] = 0;
++ mac_str++;
++ }
++
++ t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
++
++ if (t != ETH_ALEN)
++ t = sscanf(mac_str, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
++
++ if (t != ETH_ALEN) {
++ vfree(buf);
++ return NULL;
++ }
++ vfree(buf);
++ } else {
++ ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
++ put_mtd_device(mtd);
++ }
+
+ if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx))
+ inc_idx = 5;
diff --git a/target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch b/target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch
new file mode 100644
index 0000000000..bf8ff608c0
--- /dev/null
+++ b/target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch
@@ -0,0 +1,96 @@
+--- a/drivers/of/of_net.c
++++ b/drivers/of/of_net.c
+@@ -12,6 +12,7 @@
+ #include <linux/phy.h>
+ #include <linux/export.h>
+ #include <linux/mtd/mtd.h>
++#include <linux/vmalloc.h>
+
+ /**
+ * of_get_phy_mode - Get phy mode for given device_node
+@@ -49,6 +50,28 @@ static void *of_get_mac_addr(struct devi
+ return NULL;
+ }
+
++char *of_get_mac_address_mtd_helper(const char *name, const char *buf, unsigned buf_len)
++{
++ unsigned len = strlen(name);
++ char *cur, *last;
++
++ if (buf_len == 0 || len == 0)
++ return NULL;
++
++ if (buf_len < len)
++ return NULL;
++
++ if (len == 1)
++ return memchr(buf, (int) *name, buf_len);
++
++ last = (char *) buf + buf_len - len;
++ for (cur = (char *) buf; cur <= last; cur++)
++ if (cur[0] == name[0] && memcmp(cur, name, len) == 0)
++ return cur + len;
++
++ return NULL;
++}
++
+ static const void *of_get_mac_address_mtd(struct device_node *np)
+ {
+ #ifdef CONFIG_MTD
+@@ -58,6 +81,7 @@ static const void *of_get_mac_address_mt
+ int size, ret;
+ struct mtd_info *mtd;
+ const char *part;
++ const char *search;
+ const __be32 *list;
+ phandle phandle;
+ u32 mac_inc = 0;
+@@ -84,8 +109,46 @@ static const void *of_get_mac_address_mt
+ if (IS_ERR(mtd))
+ return NULL;
+
+- ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
+- put_mtd_device(mtd);
++ if (of_property_read_string(np, "mtd-mac-search", &search) == 0) {
++ // try to find mac address in mtd
++ char *buf;
++ char *mac_str;
++ int t;
++
++ buf = vmalloc(mtd->size);
++
++ ret = mtd_read(mtd, be32_to_cpup(list), mtd->size-be32_to_cpup(list), &retlen, buf);
++ put_mtd_device(mtd);
++
++ buf[retlen - 1] = '\0';
++ mac_str = of_get_mac_address_mtd_helper(search, buf, retlen);
++
++ if (!mac_str) {
++ vfree(buf);
++ return NULL;;
++ }
++
++ if (strlen(mac_str) == 19 && mac_str[0] == '"' && mac_str[18] == '"') {
++ mac_str[18] = 0;
++ mac_str++;
++ }
++
++ t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
++
++ if (t != ETH_ALEN)
++ t = sscanf(mac_str, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
++
++ if (t != ETH_ALEN) {
++ vfree(buf);
++ return NULL;
++ }
++ vfree(buf);
++ } else {
++ ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
++ put_mtd_device(mtd);
++ }
+
+ if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx))
+ inc_idx = 5;
--
2.20.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list