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

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