mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph)
@ 2021-12-09 10:57 Ahmad Fatoum
  2021-12-09 10:57 ` [PATCH 2/3] common: add new PRINTF_FULL option Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2021-12-09 10:57 UTC (permalink / raw)
  To: barebox; +Cc: 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

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/lib/Kconfig b/lib/Kconfig
index 718033e56e64..27e7bea6852f 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -166,6 +166,9 @@ config PRINTF_UUID
 config PRINTF_WCHAR
 	bool
 
+config PRINTF_HEXSTR
+	bool
+
 config GENERIC_LIB_ASHLDI3
 	bool
 
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 85147e8d2e25..ce92787c58ef 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -320,6 +320,52 @@ char *uuid_string(char *buf, const char *end, const u8 *addr, int field_width,
 	return string(buf, end, uuid, field_width, precision, flags);
 }
 
+static noinline_for_stack
+char *hex_string(char *buf, const char *end, const u8 *addr, int field_width,
+		 int precision, int flags, const char *fmt)
+{
+	char separator;
+	int i, len;
+
+	if (field_width == 0)
+		/* nothing to print */
+		return buf;
+
+	switch (fmt[1]) {
+	case 'C':
+		separator = ':';
+		break;
+	case 'D':
+		separator = '-';
+		break;
+	case 'N':
+		separator = 0;
+		break;
+	default:
+		separator = ' ';
+		break;
+	}
+
+	len = field_width > 0 ? field_width : 1;
+
+	for (i = 0; i < len; ++i) {
+		if (buf < end)
+			*buf = hex_asc_hi(addr[i]);
+		++buf;
+		if (buf < end)
+			*buf = hex_asc_lo(addr[i]);
+		++buf;
+
+		if (separator && i != len - 1) {
+			if (buf < end)
+				*buf = separator;
+			++buf;
+		}
+	}
+
+	return buf;
+}
+
 static noinline_for_stack
 char *address_val(char *buf, const char *end, const void *addr,
 		  int field_width, int precision, int flags, const char *fmt)
@@ -406,6 +452,10 @@ static char *pointer(const char *fmt, char *buf, const char *end, const void *pt
 		break;
 	case 'e':
 		return error_string(buf, end, ptr, field_width, precision, flags, fmt);
+	case 'h':
+		if (IS_ENABLED(CONFIG_PRINTF_HEXSTR))
+			return hex_string(buf, end, ptr, field_width, precision, flags, fmt);
+		break;
 	}
 
 	return raw_pointer(buf, end, ptr, field_width, precision, flags);
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* [PATCH 2/3] common: add new PRINTF_FULL option
  2021-12-09 10:57 [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Ahmad Fatoum
@ 2021-12-09 10:57 ` Ahmad Fatoum
  2021-12-09 10:57 ` [PATCH 3/3] of: implement new of_property_sprintf Ahmad Fatoum
  2021-12-13 22:28 ` [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Sascha Hauer
  2 siblings, 0 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2021-12-09 10:57 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/Kconfig | 7 +++++++
 lib/Kconfig    | 3 +++
 2 files changed, 10 insertions(+)

diff --git a/common/Kconfig b/common/Kconfig
index e540cba7ebaa..814b820e2a54 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -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"
 
diff --git a/lib/Kconfig b/lib/Kconfig
index 27e7bea6852f..915ae7a28755 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -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
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* [PATCH 3/3] of: implement new of_property_sprintf
  2021-12-09 10:57 [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Ahmad Fatoum
  2021-12-09 10:57 ` [PATCH 2/3] common: add new PRINTF_FULL option Ahmad Fatoum
@ 2021-12-09 10:57 ` Ahmad Fatoum
  2021-12-15 20:16   ` [PATCH] fixup! " Ahmad Fatoum
  2021-12-13 22:28 ` [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Sascha Hauer
  2 siblings, 1 reply; 8+ messages in thread
From: Ahmad Fatoum @ 2021-12-09 10:57 UTC (permalink / raw)
  To: barebox; +Cc: 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.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/base.c | 57 ++++++++++++++++++++++++++++++++++++++---------
 include/of.h      |  2 ++
 2 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 065265ec9756..321022f2b391 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -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)
diff --git a/include/of.h b/include/of.h
index 1b7392cdb3b9..e5df4cab4afc 100644
--- a/include/of.h
+++ b/include/of.h
@@ -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,
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph)
  2021-12-09 10:57 [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Ahmad Fatoum
  2021-12-09 10:57 ` [PATCH 2/3] common: add new PRINTF_FULL option Ahmad Fatoum
  2021-12-09 10:57 ` [PATCH 3/3] of: implement new of_property_sprintf Ahmad Fatoum
@ 2021-12-13 22:28 ` Sascha Hauer
  2021-12-14  6:57   ` Ahmad Fatoum
  2 siblings, 1 reply; 8+ messages in thread
From: Sascha Hauer @ 2021-12-13 22:28 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Thu, Dec 09, 2021 at 11:57:06AM +0100, Ahmad Fatoum wrote:
> 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
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  lib/Kconfig    |  3 +++
>  lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 53 insertions(+)
> 
> diff --git a/lib/Kconfig b/lib/Kconfig
> index 718033e56e64..27e7bea6852f 100644
> --- a/lib/Kconfig
> +++ b/lib/Kconfig
> @@ -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


-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph)
  2021-12-13 22:28 ` [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Sascha Hauer
@ 2021-12-14  6:57   ` Ahmad Fatoum
  2021-12-14  7:27     ` Sascha Hauer
  0 siblings, 1 reply; 8+ messages in thread
From: Ahmad Fatoum @ 2021-12-14  6:57 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 13.12.21 23:28, Sascha Hauer wrote:
> On Thu, Dec 09, 2021 at 11:57:06AM +0100, Ahmad Fatoum wrote:
>> 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
>>
>> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
>> ---
>>  lib/Kconfig    |  3 +++
>>  lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 53 insertions(+)
>>
>> diff --git a/lib/Kconfig b/lib/Kconfig
>> index 718033e56e64..27e7bea6852f 100644
>> --- a/lib/Kconfig
>> +++ b/lib/Kconfig
>> @@ -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.

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

> 
> Sascha
> 
> 


-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph)
  2021-12-14  6:57   ` Ahmad Fatoum
@ 2021-12-14  7:27     ` Sascha Hauer
  2022-01-03 10:40       ` Ahmad Fatoum
  0 siblings, 1 reply; 8+ messages in thread
From: Sascha Hauer @ 2021-12-14  7:27 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Tue, Dec 14, 2021 at 07:57:01AM +0100, Ahmad Fatoum wrote:
> On 13.12.21 23:28, Sascha Hauer wrote:
> > On Thu, Dec 09, 2021 at 11:57:06AM +0100, Ahmad Fatoum wrote:
> >> 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
> >>
> >> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> >> ---
> >>  lib/Kconfig    |  3 +++
> >>  lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
> >>  2 files changed, 53 insertions(+)
> >>
> >> diff --git a/lib/Kconfig b/lib/Kconfig
> >> index 718033e56e64..27e7bea6852f 100644
> >> --- a/lib/Kconfig
> >> +++ b/lib/Kconfig
> >> @@ -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.
> 
> 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


-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* [PATCH] fixup! of: implement new of_property_sprintf
  2021-12-09 10:57 ` [PATCH 3/3] of: implement new of_property_sprintf Ahmad Fatoum
@ 2021-12-15 20:16   ` Ahmad Fatoum
  0 siblings, 0 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2021-12-15 20:16 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 drivers/of/base.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 06e37b0fca4a..80465d6d5062 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -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);
 
-- 
2.33.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph)
  2021-12-14  7:27     ` Sascha Hauer
@ 2022-01-03 10:40       ` Ahmad Fatoum
  0 siblings, 0 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2022-01-03 10:40 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

Hello Sascha,

On 14.12.21 08:27, Sascha Hauer wrote:
> On Tue, Dec 14, 2021 at 07:57:01AM +0100, Ahmad Fatoum wrote:
>> On 13.12.21 23:28, Sascha Hauer wrote:
>>> On Thu, Dec 09, 2021 at 11:57:06AM +0100, Ahmad Fatoum wrote:
>>>> 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
>>>>
>>>> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
>>>> ---
>>>>  lib/Kconfig    |  3 +++
>>>>  lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
>>>>  2 files changed, 53 insertions(+)
>>>>
>>>> diff --git a/lib/Kconfig b/lib/Kconfig
>>>> index 718033e56e64..27e7bea6852f 100644
>>>> --- a/lib/Kconfig
>>>> +++ b/lib/Kconfig
>>>> @@ -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.
>>
>> 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

> 
> Sascha
> 
> 


-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

end of thread, other threads:[~2022-01-03 10:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09 10:57 [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Ahmad Fatoum
2021-12-09 10:57 ` [PATCH 2/3] common: add new PRINTF_FULL option Ahmad Fatoum
2021-12-09 10:57 ` [PATCH 3/3] of: implement new of_property_sprintf Ahmad Fatoum
2021-12-15 20:16   ` [PATCH] fixup! " Ahmad Fatoum
2021-12-13 22:28 ` [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Sascha Hauer
2021-12-14  6:57   ` Ahmad Fatoum
2021-12-14  7:27     ` Sascha Hauer
2022-01-03 10:40       ` Ahmad Fatoum

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