Skip to contentSkip to conversation

vsprintf: add support for printing raw buffers as hex (%*ph)

PATCH In review Ahmad Fatoum 3 participants 8 messages Dec 9, 2021 – Jan 3, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 vsprintf: add support for printing raw buffers as hex (%*ph)

Import from Linux support for printing buffers as a hex string with a certain separator. For larger buffers consider using print_hex_dump(). Examples:

%*ph 00 01 02 ... 3f
%*phC 00:01:02: ... :3f
%*phD 00-01-02- ... -3f
%*phN 000102 ... 3f

---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
lib/Kconfig +3 -0
index 718033e56e64..27e7bea6852f 100644
@@ -166,6 +166,9 @@ config PRINTF_UUID
 config PRINTF_WCHAR
 	bool
 
+config PRINTF_HEXSTR
+	bool
+
 config GENERIC_LIB_ASHLDI3
 	bool
 
lib/vsprintf.c +50 -0

62 lines not on this page · raw message

Signature · 7 lines
2.30.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/3 common: add new PRINTF_FULL option

We now got three compile-time selectable printf format specifiers. For debugging, it can be useful to enable all of them, thus add a new PRINTF_FULL option.

---
 common/Kconfig | 7 +++++++
 lib/Kconfig    | 3 +++
 2 files changed, 10 insertions(+)
common/Kconfig +7 -0
index e540cba7ebaa..814b820e2a54 100644
@@ -1512,6 +1512,13 @@ config PBL_BREAK
 	  If enabled, barebox will be compiled with BKPT instruction
 	  on early pbl init. This option should be used only with JTAG debugger!
 
+config PRINTF_FULL
+	bool "Support all extended printf format specifiers"
+	help
+	  Adds support for lesser used format specifiers like UUIDs and
+	  hex strings. Code requiring them should select it directly,
+	  so this is mainly for debugging. If unsure, say no.
+
 source "lib/Kconfig.ubsan"
 source "lib/kasan/Kconfig"
 
lib/Kconfig +3 -0
index 27e7bea6852f..915ae7a28755 100644
@@ -162,12 +162,15 @@ config PROGRESS_NOTIFIER
 
 config PRINTF_UUID
 	bool
+	default y if PRINTF_FULL
 
 config PRINTF_WCHAR
 	bool
+	default y if PRINTF_FULL
 
 config PRINTF_HEXSTR
 	bool
+	default y if PRINTF_FULL
 
 config GENERIC_LIB_ASHLDI3
 	bool
Signature · 7 lines
2.30.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 3/3 of: implement new of_property_sprintf

In reply to Ahmad Fatoum

Board code may compute values for device tree properties and write them as strings. Make this easier by adding a of_property_write_string variant that does formatted output. This also saves an allocation, because asprintf buffer is reused.

---
 drivers/of/base.c | 57 ++++++++++++++++++++++++++++++++++++++---------
 include/of.h      |  2 ++
 2 files changed, 49 insertions(+), 10 deletions(-)
drivers/of/base.c +47 -10
index 065265ec9756..321022f2b391 100644
@@ -2157,6 +2157,21 @@ struct device_node *of_new_node(struct device_node *parent, const char *name)
 	return node;
 }
 
+static struct property *__of_new_property(struct device_node *node, const char *name,
+		void *data, int len)
+{
+	struct property *prop;
+
+	prop = xzalloc(sizeof(*prop));
+	prop->name = xstrdup(name);
+	prop->length = len;
+	prop->value = data;
+
+	list_add_tail(&prop->list, &node->properties);
+
+	return prop;
+}
+
 /**
  * of_new_property - Add a new property to a node
  * @node:	device node to which the property is added
@@ -2172,19 +2187,13 @@ struct device_node *of_new_node(struct device_node *parent, const char *name)
 struct property *of_new_property(struct device_node *node, const char *name,
 		const void *data, int len)
 {
-	struct property *prop;
-
-	prop = xzalloc(sizeof(*prop));
-	prop->name = xstrdup(name);
-	prop->length = len;
-	prop->value = xzalloc(len);
+	char *buf;
 
+	buf = xzalloc(len);
 	if (data)
-		memcpy(prop->value, data, len);
+		memcpy(buf, data, len);
 
-	list_add_tail(&prop->list, &node->properties);
-
-	return prop;
+	return __of_new_property(node, name, buf, len);
 }
 
 /**
@@ -2256,6 +2265,34 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
 	return 0;
 }
 
+int of_property_sprintf(struct device_node *np,
+			const char *propname, const char *fmt, ...)
+{
+	struct property *pp;
+	struct va_format vaf;
+	char *buf = NULL;
+	va_list args;
+	int len;
+
+	if (!np)
+		return -ENOENT;
+
+	va_start(args, fmt);
+	vaf.fmt = fmt;
+	vaf.va = &args;
+	len = asprintf(&buf, "%pV", &vaf);
+	va_end(args);
+
+	if (len < 0)
+		return -ENOMEM;
+
+	pp = of_find_property(np, propname, NULL);
+	of_delete_property(pp);
+
+	__of_new_property(np, propname, buf, len);
+	return len;
+}
+
 static int mem_bank_num;
 
 int of_add_memory(struct device_node *node, bool dump)
include/of.h +2 -0
index 1b7392cdb3b9..e5df4cab4afc 100644
@@ -241,6 +241,8 @@ extern int of_property_write_string(struct device_node *np, const char *propname
 				    const char *value);
 extern int of_property_write_strings(struct device_node *np, const char *propname,
 				    ...) __attribute__((__sentinel__));
+int of_property_sprintf(struct device_node *np, const char *propname, const char *fmt, ...)
+	__attribute__ ((format(__printf__, 3, 4)));
 
 extern struct device_node *of_parse_phandle(const struct device_node *np,
 					    const char *phandle_name,
Signature · 7 lines
2.30.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 1/1 fixup! of: implement new of_property_sprintf

Otherwise, tools like of_dump will consider the output to be byte arrays.

---
 drivers/of/base.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/of/base.c +2 -0
index 06e37b0fca4a..80465d6d5062 100644
@@ -2285,6 +2285,8 @@ int of_property_sprintf(struct device_node *np,
 	if (len < 0)
 		return -ENOMEM;
 
+	len++; /* trailing NUL */
+
 	pp = of_find_property(np, propname, NULL);
 	of_delete_property(pp);
 
Signature · 7 lines
2.33.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
In reply to Ahmad Fatoum
Quoted 20 lines · Ahmad Fatoum

Import from Linux support for printing buffers as a hex string with a certain separator. For larger buffers consider using print_hex_dump(). Examples:

%*ph 00 01 02 ... 3f
%*phC 00:01:02: ... :3f
%*phD 00-01-02- ... -3f
%*phN 000102 ... 3f

---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
lib/Kconfig +0 -0
index 718033e56e64..27e7bea6852f 100644

lib/Kconfig config PRINTF_UUID · line 170

Quoted 5 lines
lib/Kconfig +2 -0
@@ -166,6 +166,9 @@ config PRINTF_UUID
 config PRINTF_WCHAR
 	bool
 
+config PRINTF_HEXSTR
+	bool

So hexdumps are silently ignored when this option is disabled. Not sure if this should really be configurable, but I think when it is configurable it should be default y to avoid surprises.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw
Quoted 24 lines · Sascha Hauer
Quoted 23 lines · Ahmad Fatoum

Import from Linux support for printing buffers as a hex string with a certain separator. For larger buffers consider using print_hex_dump(). Examples:

%*ph 00 01 02 ... 3f
%*phC 00:01:02: ... :3f
%*phD 00-01-02- ... -3f
%*phN 000102 ... 3f

---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
lib/Kconfig +0 -0
index 718033e56e64..27e7bea6852f 100644
@@ -166,6 +166,9 @@ config PRINTF_UUID
 config PRINTF_WCHAR
 	bool
 
Quoted 6 lines

lib/Kconfig config PRINTF_UUID · line 170

Quoted 2 lines
lib/Kconfig +2 -0
+config PRINTF_HEXSTR
+	bool

So hexdumps are silently ignored when this option is disabled. Not sure if this should really be configurable, but I think when it is configurable it should be default y to avoid surprises.

It will be printed as a raw pointer when the option is disabled. There's no code in-tree that uses this format specifier yet, so I prefer to not make this the default and increase binary size.

Cheers,
Ahmad

Quoted 4 lines

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw
Quoted 32 lines · Ahmad Fatoum
Quoted 24 lines · Sascha Hauer
Quoted 23 lines · Ahmad Fatoum

Import from Linux support for printing buffers as a hex string with a certain separator. For larger buffers consider using print_hex_dump(). Examples:

%*ph 00 01 02 ... 3f
%*phC 00:01:02: ... :3f
%*phD 00-01-02- ... -3f
%*phN 000102 ... 3f

---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
lib/Kconfig +0 -0
index 718033e56e64..27e7bea6852f 100644
@@ -166,6 +166,9 @@ config PRINTF_UUID
 config PRINTF_WCHAR
 	bool
 
Quoted 6 lines

lib/Kconfig config PRINTF_UUID · line 170

Quoted 2 lines
lib/Kconfig +2 -0
+config PRINTF_HEXSTR
+	bool

So hexdumps are silently ignored when this option is disabled. Not sure if this should really be configurable, but I think when it is configurable it should be default y to avoid surprises.

Quoted 3 lines

It will be printed as a raw pointer when the option is disabled. There's no code in-tree that uses this format specifier yet, so I prefer to not make this the default and increase binary size.

Ok for now, but we should revise it once this actually gets used.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

Hello Sascha,

Quoted 33 lines · Sascha Hauer
Quoted 32 lines · Ahmad Fatoum
Quoted 30 lines · Sascha Hauer
Quoted 20 lines · Ahmad Fatoum

Import from Linux support for printing buffers as a hex string with a certain separator. For larger buffers consider using print_hex_dump(). Examples:

%*ph 00 01 02 ... 3f
%*phC 00:01:02: ... :3f
%*phD 00-01-02- ... -3f
%*phN 000102 ... 3f

---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
lib/Kconfig +0 -0
index 718033e56e64..27e7bea6852f 100644

lib/Kconfig config PRINTF_UUID · line 170

Quoted 5 lines
lib/Kconfig +2 -0
@@ -166,6 +166,9 @@ config PRINTF_UUID
 config PRINTF_WCHAR
 	bool
 
+config PRINTF_HEXSTR
+	bool

So hexdumps are silently ignored when this option is disabled. Not sure if this should really be configurable, but I think when it is configurable it should be default y to avoid surprises.

Quoted 5 lines
Quoted 3 lines

It will be printed as a raw pointer when the option is disabled. There's no code in-tree that uses this format specifier yet, so I prefer to not make this the default and increase binary size.

Ok for now, but we should revise it once this actually gets used.

This is still left after a rebase.

Cheers,
Ahmad

Quoted 4 lines

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox