mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] vsprintf: add support for printing MAC addresses
@ 2024-02-02 15:11 Sascha Hauer
  2024-02-02 15:11 ` [PATCH 2/2] eth: replace ethaddr_to_string() with %pM Sascha Hauer
  2024-02-05  7:47 ` [PATCH 1/2] vsprintf: add support for printing MAC addresses Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-02-02 15:11 UTC (permalink / raw)
  To: Barebox List

Linux can print MAC addresses using the %pM format specifier. Implement
the same for barebox.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/vsprintf.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 7d943706dd..3dda158683 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -266,6 +266,26 @@ static char *symbol_string(char *buf, const char *end, const void *ptr, int fiel
 #endif
 }
 
+static noinline_for_stack
+char *mac_address_string(char *buf, const char *end, const u8 *addr, int field_width,
+		      int precision, int flags, const char *fmt)
+{
+	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
+	char *p = mac_addr;
+	int i;
+	char separator = ':';
+
+	for (i = 0; i < 6; i++) {
+		p = hex_byte_pack(p, addr[i]);
+
+		if (i != 5)
+			*p++ = separator;
+	}
+	*p = '\0';
+
+	return string(buf, end, mac_addr, field_width, precision, flags);
+}
+
 static noinline_for_stack
 char *ip4_addr_string(char *buf, const char *end, const u8 *addr, int field_width,
 		      int precision, int flags, const char *fmt)
@@ -469,6 +489,8 @@ char *device_node_string(char *buf, const char *end, const struct device_node *n
  *       correctness of the format string and va_list arguments.
  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  *           (default assumed to be phys_addr_t, passed by reference)
+ * - 'M' For a 6-byte MAC address, it prints the address in the
+ *       usual colon-separated hex notation
  *
  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  * function pointers are really function descriptors, which contain a
@@ -515,6 +537,9 @@ static char *pointer(const char *fmt, char *buf, const char *end, const void *pt
 	case 'J':
 		if (fmt[1] == 'P' && IS_ENABLED(CONFIG_JSMN))
 			return jsonpath_string(buf, end, ptr, field_width, precision, flags, fmt);
+        case 'M':
+		/* Colon separated: 00:01:02:03:04:05 */
+		return mac_address_string(buf, end, ptr, field_width, precision, flags, fmt);
 	}
 
 	return raw_pointer(buf, end, ptr, field_width, precision, flags);
-- 
2.39.2




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

* [PATCH 2/2] eth: replace ethaddr_to_string() with %pM
  2024-02-02 15:11 [PATCH 1/2] vsprintf: add support for printing MAC addresses Sascha Hauer
@ 2024-02-02 15:11 ` Sascha Hauer
  2024-02-05  7:47 ` [PATCH 1/2] vsprintf: add support for printing MAC addresses Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-02-02 15:11 UTC (permalink / raw)
  To: Barebox List

Now that we can print MAC addresses using the %pM format specifier we
can get rid of ethaddr_to_string(). Do this treewide.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/boards/protonic-imx6/board.c |  5 +----
 arch/arm/boards/skov-imx6/board.c     |  5 +----
 include/net.h                         |  1 -
 lib/parameter.c                       |  3 +--
 net/eth.c                             | 13 +++++--------
 net/lib.c                             |  7 -------
 net/net.c                             |  8 ++------
 7 files changed, 10 insertions(+), 32 deletions(-)

diff --git a/arch/arm/boards/protonic-imx6/board.c b/arch/arm/boards/protonic-imx6/board.c
index 5c5e7d6155..9e62dc1544 100644
--- a/arch/arm/boards/protonic-imx6/board.c
+++ b/arch/arm/boards/protonic-imx6/board.c
@@ -200,10 +200,7 @@ static int prt_imx6_set_mac(struct prt_imx6_priv *priv,
 		return 0;
 
 	if (!is_valid_ether_addr(&rfid->mac[0])) {
-		unsigned char ethaddr_str[sizeof("xx:xx:xx:xx:xx:xx")];
-
-		ethaddr_to_string(&rfid->mac[0], ethaddr_str);
-		dev_err(dev, "bad MAC addr: %s\n", ethaddr_str);
+		dev_err(dev, "bad MAC addr: %pM\n", &rfid->mac[0]);
 
 		return -EILSEQ;
 	}
diff --git a/arch/arm/boards/skov-imx6/board.c b/arch/arm/boards/skov-imx6/board.c
index b3f170cfdf..8ebb4a6e58 100644
--- a/arch/arm/boards/skov-imx6/board.c
+++ b/arch/arm/boards/skov-imx6/board.c
@@ -30,10 +30,7 @@ static int eth_of_fixup_node(struct device_node *root, const char *node_path,
 	int ret;
 
 	if (!is_valid_ether_addr(ethaddr)) {
-		unsigned char ethaddr_str[sizeof("xx:xx:xx:xx:xx:xx")];
-
-		ethaddr_to_string(ethaddr, ethaddr_str);
-		dev_err(skov_priv->dev, "The mac-address %s is invalid.\n", ethaddr_str);
+		dev_err(skov_priv->dev, "The mac-address %pM is invalid.\n", ethaddr);
 		return -EINVAL;
 	}
 
diff --git a/include/net.h b/include/net.h
index 40287a3503..ffc1093ae6 100644
--- a/include/net.h
+++ b/include/net.h
@@ -358,7 +358,6 @@ IPaddr_t getenv_ip(const char *name);
 int setenv_ip(const char *name, IPaddr_t ip);
 
 int string_to_ethaddr(const char *str, u8 enetaddr[6]);
-void ethaddr_to_string(const u8 enetaddr[6], char *str);
 
 #ifdef CONFIG_NET_RESOLV
 int resolv(const char *host, IPaddr_t *ip);
diff --git a/lib/parameter.c b/lib/parameter.c
index aac33e3a19..dc80f3f858 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -846,7 +846,6 @@ struct param_mac {
 };
 
 int string_to_ethaddr(const char *str, u8 enetaddr[6]);
-void ethaddr_to_string(const u8 enetaddr[6], char *str);
 
 static inline struct param_mac *to_param_mac(struct param_d *p)
 {
@@ -894,7 +893,7 @@ static const char *param_mac_get(struct device *dev, struct param_d *p)
 			return NULL;
 	}
 
-	ethaddr_to_string(pm->mac, p->value);
+	sprintf(p->value, "%pM", pm->mac);
 
 	return p->value;
 }
diff --git a/net/eth.c b/net/eth.c
index d1474ec57d..28961e868b 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -55,11 +55,8 @@ int eth_set_ethaddr(struct eth_device *edev, const char *ethaddr)
 
 static void register_preset_mac_address(struct eth_device *edev, const char *ethaddr)
 {
-	unsigned char ethaddr_str[sizeof("xx:xx:xx:xx:xx:xx")];
-
 	if (is_valid_ether_addr(ethaddr)) {
-		ethaddr_to_string(ethaddr, ethaddr_str);
-		dev_info(&edev->dev, "got preset MAC address: %s\n", ethaddr_str);
+		dev_info(&edev->dev, "got preset MAC address: %pM\n", ethaddr);
 		eth_set_ethaddr(edev, ethaddr);
 	}
 }
@@ -557,7 +554,6 @@ void eth_open_all(void)
 
 static int populate_ethaddr(void)
 {
-	char str[sizeof("xx:xx:xx:xx:xx:xx")];
 	struct eth_device *edev;
 	bool generated = false;
 	int ret;
@@ -575,11 +571,12 @@ static int populate_ethaddr(void)
 		if (ret)
 			continue;
 
-		ethaddr_to_string(edev->ethaddr, str);
 		if (generated)
-			dev_notice(&edev->dev, "Generated MAC address from unique id: %s\n", str);
+			dev_notice(&edev->dev, "Generated MAC address from unique id: %pM\n",
+				   edev->ethaddr);
 		else
-			dev_info(&edev->dev, "Got preset MAC address from NVMEM: %s\n", str);
+			dev_info(&edev->dev, "Got preset MAC address from NVMEM: %pM\n",
+				 edev->ethaddr);
 		eth_set_ethaddr(edev, edev->ethaddr);
 	}
 
diff --git a/net/lib.c b/net/lib.c
index d4536441bd..dc6e138f39 100644
--- a/net/lib.c
+++ b/net/lib.c
@@ -37,13 +37,6 @@ int string_to_ethaddr(const char *str, u8 enetaddr[ETH_ALEN])
 	return 0;
 }
 
-void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
-{
-	sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
-		 enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3],
-		 enetaddr[4], enetaddr[5]);
-}
-
 int string_to_ip(const char *s, IPaddr_t *ip)
 {
 	IPaddr_t addr = 0;
diff --git a/net/net.c b/net/net.c
index e38179491d..d72ed81e10 100644
--- a/net/net.c
+++ b/net/net.c
@@ -418,17 +418,13 @@ static struct net_connection *net_new(struct eth_device *edev, IPaddr_t dest,
 	}
 
 	if (!is_valid_ether_addr(edev->ethaddr)) {
-		char str[sizeof("xx:xx:xx:xx:xx:xx")];
-
 		ret = generate_ether_addr(edev->ethaddr, edev->dev.id);
 		if (ret)
 			random_ether_addr(edev->ethaddr);
 
-		ethaddr_to_string(edev->ethaddr, str);
-
-		dev_warn(&edev->dev, "No MAC address set. Using %s %s\n",
+		dev_warn(&edev->dev, "No MAC address set. Using %s %pM\n",
 			 ret == 1 ? "address computed from unique ID" : "random address",
-			 str);
+			 edev->ethaddr);
 		eth_set_ethaddr(edev, edev->ethaddr);
 	}
 
-- 
2.39.2




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

* Re: [PATCH 1/2] vsprintf: add support for printing MAC addresses
  2024-02-02 15:11 [PATCH 1/2] vsprintf: add support for printing MAC addresses Sascha Hauer
  2024-02-02 15:11 ` [PATCH 2/2] eth: replace ethaddr_to_string() with %pM Sascha Hauer
@ 2024-02-05  7:47 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-02-05  7:47 UTC (permalink / raw)
  To: Barebox List, Sascha Hauer


On Fri, 02 Feb 2024 16:11:46 +0100, Sascha Hauer wrote:
> Linux can print MAC addresses using the %pM format specifier. Implement
> the same for barebox.
> 
> 

Applied, thanks!

[1/2] vsprintf: add support for printing MAC addresses
      https://git.pengutronix.de/cgit/barebox/commit/?id=107eeef8f2a9 (link may not be stable)
[2/2] eth: replace ethaddr_to_string() with %pM
      https://git.pengutronix.de/cgit/barebox/commit/?id=96a5f863fbda (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




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

end of thread, other threads:[~2024-02-05  7:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-02 15:11 [PATCH 1/2] vsprintf: add support for printing MAC addresses Sascha Hauer
2024-02-02 15:11 ` [PATCH 2/2] eth: replace ethaddr_to_string() with %pM Sascha Hauer
2024-02-05  7:47 ` [PATCH 1/2] vsprintf: add support for printing MAC addresses Sascha Hauer

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