[OpenWrt-Devel,4/5] target/generic: add ascii search option to mtd-mac-address helper
diff mbox series

Message ID 20191021123214.2252-5-avalentin@marcant.net
State Changes Requested
Headers show
Series
  • ZyXEL NBG6716 update ar71xx, add ath79, add mtd mac parser
Related show

Commit Message

André Valentin Oct. 21, 2019, 12:32 p.m. UTC
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@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

Comments

Petr Štetiar Oct. 21, 2019, 1:02 p.m. UTC | #1
André Valentin <avalentin@marcant.net> [2019-10-21 14:32:13]:

Hi,

> This improves the function of_get_mac_address_mtd...

but on the other hand its also introducing something which would never be
accepted upstream, for details see my other comment[1].

> Code has been taken from ar71xx.

It makes me wonder from where exactly as I can't seem to find it.

1. http://patchwork.ozlabs.org/patch/1112495/#2189030

-- ynezz
André Valentin Oct. 21, 2019, 1:13 p.m. UTC | #2
Hi Petr!

Am 21.10.19 um 15:02 schrieb Petr Štetiar:
> André Valentin <avalentin@marcant.net> [2019-10-21 14:32:13]:
> 
> Hi,
> 
>> This improves the function of_get_mac_address_mtd...
> 
> but on the other hand its also introducing something which would never be
> accepted upstream, for details see my other comment[1].

I also saw that. I could move it into a separate "driver"? I also noticed the nvmem provider, but that is a bit to tricky for me.


> 
>> Code has been taken from ar71xx.
> 
> It makes me wonder from where exactly as I can't seem to find it.
> 
> 1. http://patchwork.ozlabs.org/patch/1112495/#2189030
> 
> -- ynezz
> 

Kind regards,

André
Petr Štetiar Oct. 21, 2019, 2:06 p.m. UTC | #3
André Valentin <avalentin@marcant.net> [2019-10-21 15:13:55]:

> Am 21.10.19 um 15:02 schrieb Petr Štetiar:
> > André Valentin <avalentin@marcant.net> [2019-10-21 14:32:13]:
> > 
> >> This improves the function of_get_mac_address_mtd...
> > 
> > but on the other hand its also introducing something which would never be
> > accepted upstream, for details see my other comment[1].
>
> I also saw that. I could move it into a separate "driver"?

It won't help, we certainly don't want to read complete flash partition into
RAM just to search for some needle, your code is happy with first needle
occurence (smeling hard to reproduce issues) so you should aim for fixed
offset, so in the end it's Yousong's mtd-mac-address-ascii[1] what you're
probably looking for.

> I also noticed the nvmem provider, but that is a bit to tricky for me.

Indeed, making something accepted upstream is tedious and time consuming
process, but its usually worth this effort in the longterm and should be
preferred.

> >> Code has been taken from ar71xx.
> > 
> > It makes me wonder from where exactly as I can't seem to find it.

so where is it originating from? :-)

1. http://patchwork.ozlabs.org/patch/1112495/#2189030

-- ynezz
André Valentin Oct. 21, 2019, 2:20 p.m. UTC | #4
Hi Petr,

I got it from here:
lede/target/linux/ar71xx/files/arch/mips/ath79/nvram.c


Am 21.10.19 um 16:06 schrieb Petr Štetiar:
> André Valentin <avalentin@marcant.net> [2019-10-21 15:13:55]:
> 
>> Am 21.10.19 um 15:02 schrieb Petr Štetiar:
>>> André Valentin <avalentin@marcant.net> [2019-10-21 14:32:13]:
>>>
>>>> This improves the function of_get_mac_address_mtd...
>>>
>>> but on the other hand its also introducing something which would never be
>>> accepted upstream, for details see my other comment[1].
>>
>> I also saw that. I could move it into a separate "driver"?
> 
> It won't help, we certainly don't want to read complete flash partition into
> RAM just to search for some needle, your code is happy with first needle
> occurence (smeling hard to reproduce issues) so you should aim for fixed
> offset, so in the end it's Yousong's mtd-mac-address-ascii[1] what you're
> probably looking for.
> 
>> I also noticed the nvmem provider, but that is a bit to tricky for me.
> 
> Indeed, making something accepted upstream is tedious and time consuming
> process, but its usually worth this effort in the longterm and should be
> preferred.

In that case I will drop it. It's nice, but not really needed.

> 
>>>> Code has been taken from ar71xx.
>>>
>>> It makes me wonder from where exactly as I can't seem to find it.
> 
> so where is it originating from? :-)
> 
> 1. http://patchwork.ozlabs.org/patch/1112495/#2189030
> 
> -- ynezz
> 
Kind regards,

André

Patch
diff mbox series

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;