[PATCH v2 01/32] cmd: fdt: Import is_printable_string() from DTC to fix u32 misprint

Marek Vasut marek.vasut+renesas at mailbox.org
Thu Mar 2 04:08:14 CET 2023


Import is_printable_string() implementation from DTC 1.7.0 as of
DTC commit 039a994 ("Bump version to v1.7.0") . This fixes a print
of u32 property which so far used to be printed as string by U-Boot
fdt print command.

We might see the case where the parsed property value, in this case
it is a 32-bit integer, identified as a printable string or a null byte
(concatenated strings) because of its last character happens to be:
  0x00 (null character), 0xB (vertical tab character) or
  0x10 (line feed character)
In this situation, if the string is identified as printable string,
it will be displayed as character instead of hex value

When the isprint() condition is true, there are two possibilities:
  1) The character is ASCII character (except the first 32)
  2) The character is extended ASCII character

For example,
NG property in device tree:
    clock-frequency = <16640000>;
by default, would be displayed as
    clock-frequency = "", "ýè";
and with this patch applied, would be displayed as
    clock-frequency = <0x00fde800>;

Full investigation was done by Nam and Hai, patch reworked by Marek
to use common code from DTC.

Signed-off-by: Hai Pham <hai.pham.ud at renesas.com>
Signed-off-by: Nam Nguyen <nam.nguyen.yh at renesas.com>
Signed-off-by: Marek Vasut <marek.vasut+renesas at mailbox.org>
Reviewed-by: Simon Glass <sjg at chromium.org>
---
Cc: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
Cc: Simon Glass <sjg at chromium.org>
Cc: Tom Rini <trini at konsulko.com>
---
V2: Add RB from Simon
---
 cmd/fdt.c | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)

diff --git a/cmd/fdt.c b/cmd/fdt.c
index 1972490bdc2..bf2415661e2 100644
--- a/cmd/fdt.c
+++ b/cmd/fdt.c
@@ -878,41 +878,33 @@ static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
 static int is_printable_string(const void *data, int len)
 {
 	const char *s = data;
+	const char *ss, *se;
 
 	/* zero length is not */
 	if (len == 0)
 		return 0;
 
-	/* must terminate with zero or '\n' */
-	if (s[len - 1] != '\0' && s[len - 1] != '\n')
+	/* must terminate with zero */
+	if (s[len - 1] != '\0')
 		return 0;
 
-	/* printable or a null byte (concatenated strings) */
-	while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
-		/*
-		 * If we see a null, there are three possibilities:
-		 * 1) If len == 1, it is the end of the string, printable
-		 * 2) Next character also a null, not printable.
-		 * 3) Next character not a null, continue to check.
-		 */
-		if (s[0] == '\0') {
-			if (len == 1)
-				return 1;
-			if (s[1] == '\0')
-				return 0;
-		}
+	se = s + len;
+
+	while (s < se) {
+		ss = s;
+		while (s < se && *s && isprint((unsigned char)*s))
+			s++;
+
+		/* not zero, or not done yet */
+		if (*s != '\0' || s == ss)
+			return 0;
+
 		s++;
-		len--;
 	}
 
-	/* Not the null termination, or not done yet: not printable */
-	if (*s != '\0' || (len != 0))
-		return 0;
-
 	return 1;
 }
 
-
 /*
  * Print the property in the best format, a heuristic guess.  Print as
  * a string, concatenated strings, a byte, word, double word, or (if all
-- 
2.39.2



More information about the U-Boot mailing list