diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2024-02-02 16:11:46 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2024-02-05 08:47:09 +0100 |
commit | 107eeef8f2a95142e2118bf4783cecc82778814a (patch) | |
tree | 3c26e6474e209095d1d253db99abc62cab79ac04 | |
parent | 0b94be2390ec2e58e77d103a890a91008b4c4ddf (diff) | |
download | barebox-107eeef8f2a9.tar.gz barebox-107eeef8f2a9.tar.xz |
vsprintf: add support for printing MAC addresses
Linux can print MAC addresses using the %pM format specifier. Implement
the same for barebox.
Link: https://lore.barebox.org/20240202151147.226876-1-s.hauer@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r-- | lib/vsprintf.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 7d943706dd..3dda158683 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -267,6 +267,26 @@ static char *symbol_string(char *buf, const char *end, const void *ptr, int fiel } 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); |