Skip to contentSkip to conversation

Support 'b' format for printing raw bytes with fdtget

PATCH Superseded Rafał Miłecki 4 participants 12 messages Dec 6, 2021 – Dec 9, 2021 5 messages not in the archive Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Support 'b' format for printing raw bytes with fdtget

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'b' (binary) format. Example usage:
fdtget -t b firmware.itb /images/foo data > image.raw

---
 fdtget.c |  5 +++++
 util.c   | 24 ++++++++++++++----------
 2 files changed, 19 insertions(+), 10 deletions(-)
fdtget.c +5 -0
index 54fc6a0..6e4abf1 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'b') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
util.c +14 -10
index 40274fb..4fa76c5 100644
@@ -340,24 +340,28 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 
 	/* get the conversion qualifier */
 	*size = -1;
-	if (strchr("hlLb", *fmt)) {
-		qualifier = *fmt++;
-		if (qualifier == *fmt) {
-			switch (*fmt++) {
-/* TODO:		case 'l': qualifier = 'L'; break;*/
-			case 'h':
+	for (; *(fmt + 1); fmt++) {
+		if (!strchr("hlLb", *fmt))
+			return -1;
+		if (qualifier) {
+			if (*fmt == 'h' && qualifier == 'h')
 				qualifier = 'b';
-				break;
-			}
+			else
+				return -1;
+		} else {
+			qualifier = *fmt;
 		}
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if (!strchr("iuxsb", *fmt)) {
+		if (*fmt)
+			fprintf(stderr, "invalid type: %c\n", *fmt);
 		return -1;
+	}
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'b')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
Signature · 1 line
2.31.1

A message that is not in the archive (sent off-list or not yet received): 20211206154953.17089-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

Hi Rafał,

Quoted 10 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

Quoted 8 lines

This commit adds support for 'b' (binary) format. Example usage:
fdtget -t b firmware.itb /images/foo data > image.raw

---
 fdtget.c |  5 +++++
 util.c   | 24 ++++++++++++++----------
 2 files changed, 19 insertions(+), 10 deletions(-)

I think this is a good idea.

I'm worried about using 'b' which we already use as a modifier. Won't that be ambiguous? Perhaps not, but it needs a doc update, at least.

Please add to fdtput also

Please add some tests too.

Regards,
SImon

#3 lore raw

PATCH v2 1/1 Support 'b' format for printing raw bytes with fdtget

In reply to a message not in the archive

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'b' (binary) format. Example usage:
fdtget -t b firmware.itb /images/foo data > image.raw

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

---
V2: Update usage info & add tests

Alternatively I could use e.g. "r" (raw) instead of "b" (binary) if you find it more accurate. Please let me know.

---
 Documentation/manual.txt |  2 +-
 fdtget.c                 |  5 +++++
 fdtput.c                 |  2 ++
 tests/run_tests.sh       |  2 ++
 tests/utilfdt_test.c     |  5 ++++-
 util.c                   | 21 +++++++++++----------
 util.h                   |  3 ++-
 7 files changed, 27 insertions(+), 13 deletions(-)
Documentation/manual.txt +1 -1
index 97e53b9..73e0593 100644
@@ -712,7 +712,7 @@ The syntax of the fdtget command is:
 
 where options are:
 
-    <type>    s=string, i=int, u=unsigned, x=hex
+    <type>    s=string, i=int, u=unsigned, x=hex, b=binary
         Optional modifier prefix:
             hh or b=byte, h=2 byte, l=4 byte (default)
 
fdtget.c +5 -0
index 54fc6a0..6e4abf1 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'b') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
fdtput.c +2 -0
index 428745a..99b0bf9 100644
@@ -433,6 +433,8 @@ int main(int argc, char *argv[])
 			if (utilfdt_decode_type(optarg, &disp.type,
 					&disp.size))
 				usage("Invalid type string");
+			if (disp.type == 'b')
+				usage("Unsupported binary data type");
 			break;
 
 		case 'v':
tests/run_tests.sh +2 -0
index d100d5a..484be18 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
     run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
     run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
     run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tb $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tb $dtb /randomnode blob
 
     # Here the property size is not a multiple of 4 bytes, so it should fail
     run_wrap_error_test $DTGET -tlx $dtb /randomnode mixed
tests/utilfdt_test.c +4 -1
index c621759..906f250 100644
@@ -73,6 +73,9 @@ static void check_sizes(char *modifier, int expected_size)
 
 	*ptr = 's';
 	check(fmt, 's', -1);
+
+	*ptr = 'b';
+	check(fmt, 'b', -1);
 }
 
 static void test_utilfdt_decode_type(void)
@@ -90,7 +93,7 @@ static void test_utilfdt_decode_type(void)
 	/* try every other character */
 	checkfail("");
 	for (ch = ' '; ch < 127; ch++) {
-		if (!strchr("iuxs", ch)) {
+		if (!strchr("iuxsb", ch)) {
 			*fmt = ch;
 			fmt[1] = '\0';
 			checkfail(fmt);
util.c +11 -10
index 40274fb..b9e88f1 100644
@@ -340,24 +340,25 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 
 	/* get the conversion qualifier */
 	*size = -1;
-	if (strchr("hlLb", *fmt)) {
-		qualifier = *fmt++;
-		if (qualifier == *fmt) {
-			switch (*fmt++) {
-/* TODO:		case 'l': qualifier = 'L'; break;*/
-			case 'h':
+	for (; *(fmt + 1); fmt++) {
+		if (!strchr("hlLb", *fmt))
+			return -1;
+		if (qualifier) {
+			if (*fmt == 'h' && qualifier == 'h')
 				qualifier = 'b';
-				break;
-			}
+			else
+				return -1;
+		} else {
+			qualifier = *fmt;
 		}
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if (!strchr("iuxsb", *fmt))
 		return -1;
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'b')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
util.h +2 -1
index c45b2c2..fa381fc 100644
@@ -143,6 +143,7 @@ int utilfdt_write_err(const char *filename, const void *blob);
  *		i	signed integer
  *		u	unsigned integer
  *		x	hex
+ *		b	binary
  *
  * TODO: Implement ll modifier (8 bytes)
  * TODO: Implement o type (octal)
@@ -160,7 +161,7 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size);
  */
 
 #define USAGE_TYPE_MSG \
-	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
+	"<type>\ts=string, i=int, u=unsigned, x=hex, b=binary\n" \
 	"\tOptional modifier prefix:\n" \
 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
 
Signature · 1 line
2.31.1

A message that is not in the archive (sent off-list or not yet received): 20211208170055.13811-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#5 lore raw
Quoted 36 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'b' (binary) format. Example usage:
fdtget -t b firmware.itb /images/foo data > image.raw

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

---
V2: Update usage info & add tests

Alternatively I could use e.g. "r" (raw) instead of "b" (binary) if you find it more accurate. Please let me know.

---
 Documentation/manual.txt |  2 +-
 fdtget.c                 |  5 +++++
 fdtput.c                 |  2 ++
 tests/run_tests.sh       |  2 ++
 tests/utilfdt_test.c     |  5 ++++-
 util.c                   | 21 +++++++++++----------
 util.h                   |  3 ++-
 7 files changed, 27 insertions(+), 13 deletions(-)
Documentation/manual.txt +0 -0
index 97e53b9..73e0593 100644

Documentation/manual.txt The syntax of the fdtget command is: · line 717

Quoted 7 lines
Documentation/manual.txt +1 -1
@@ -712,7 +712,7 @@ The syntax of the fdtget command is:
 
 where options are:
 
-    <type>    s=string, i=int, u=unsigned, x=hex
+    <type>    s=string, i=int, u=unsigned, x=hex, b=binary
         Optional modifier prefix:
             hh or b=byte, h=2 byte, l=4 byte (default)

I don't think using "b" as both a type specifier, and a length modifier is a good idea. You can probably disambiguate them in parsing, but it's still likely to cause confusion.

How about "r" for "raw"?

Quoted 127 lines
fdtget.c +5 -0
index 54fc6a0..6e4abf1 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'b') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
fdtput.c +2 -0
index 428745a..99b0bf9 100644
@@ -433,6 +433,8 @@ int main(int argc, char *argv[])
 			if (utilfdt_decode_type(optarg, &disp.type,
 					&disp.size))
 				usage("Invalid type string");
+			if (disp.type == 'b')
+				usage("Unsupported binary data type");
 			break;
 
 		case 'v':
tests/run_tests.sh +2 -0
index d100d5a..484be18 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
     run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
     run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
     run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tb $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tb $dtb /randomnode blob
 
     # Here the property size is not a multiple of 4 bytes, so it should fail
     run_wrap_error_test $DTGET -tlx $dtb /randomnode mixed
tests/utilfdt_test.c +4 -1
index c621759..906f250 100644
@@ -73,6 +73,9 @@ static void check_sizes(char *modifier, int expected_size)
 
 	*ptr = 's';
 	check(fmt, 's', -1);
+
+	*ptr = 'b';
+	check(fmt, 'b', -1);
 }
 
 static void test_utilfdt_decode_type(void)
@@ -90,7 +93,7 @@ static void test_utilfdt_decode_type(void)
 	/* try every other character */
 	checkfail("");
 	for (ch = ' '; ch < 127; ch++) {
-		if (!strchr("iuxs", ch)) {
+		if (!strchr("iuxsb", ch)) {
 			*fmt = ch;
 			fmt[1] = '\0';
 			checkfail(fmt);
util.c +11 -10
index 40274fb..b9e88f1 100644
@@ -340,24 +340,25 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 
 	/* get the conversion qualifier */
 	*size = -1;
-	if (strchr("hlLb", *fmt)) {
-		qualifier = *fmt++;
-		if (qualifier == *fmt) {
-			switch (*fmt++) {
-/* TODO:		case 'l': qualifier = 'L'; break;*/
-			case 'h':
+	for (; *(fmt + 1); fmt++) {
+		if (!strchr("hlLb", *fmt))
+			return -1;
+		if (qualifier) {
+			if (*fmt == 'h' && qualifier == 'h')
 				qualifier = 'b';
-				break;
-			}
+			else
+				return -1;
+		} else {
+			qualifier = *fmt;
 		}
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if (!strchr("iuxsb", *fmt))
 		return -1;
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'b')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
util.h +2 -1
index c45b2c2..fa381fc 100644
@@ -143,6 +143,7 @@ int utilfdt_write_err(const char *filename, const void *blob);
  *		i	signed integer
  *		u	unsigned integer
  *		x	hex
+ *		b	binary
  *
  * TODO: Implement ll modifier (8 bytes)
  * TODO: Implement o type (octal)
@@ -160,7 +161,7 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size);
  */
 
 #define USAGE_TYPE_MSG \
-	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
+	"<type>\ts=string, i=int, u=unsigned, x=hex, b=binary\n" \
 	"\tOptional modifier prefix:\n" \
 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#6 lore raw

PATCH v3 1/1 Support 'r' format for printing raw bytes with fdtget

In reply to a message not in the archive

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'r' (raw) format. Example usage:
fdtget -t r firmware.itb /images/foo data > image.raw

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

---
V2: Update usage info & add tests
V3: Use "r" instead of "b" to avoid confusiong with qualifier

---
 Documentation/manual.txt |  2 +-
 fdtget.c                 |  5 +++++
 fdtput.c                 |  2 ++
 tests/run_tests.sh       |  2 ++
 tests/utilfdt_test.c     |  5 ++++-
 util.c                   | 21 +++++++++++----------
 util.h                   |  3 ++-
 7 files changed, 27 insertions(+), 13 deletions(-)
Documentation/manual.txt +1 -1
index 97e53b9..cf4b253 100644
@@ -712,7 +712,7 @@ The syntax of the fdtget command is:
 
 where options are:
 
-    <type>    s=string, i=int, u=unsigned, x=hex
+    <type>    s=string, i=int, u=unsigned, x=hex, r=raw
         Optional modifier prefix:
             hh or b=byte, h=2 byte, l=4 byte (default)
 
fdtget.c +5 -0
index 54fc6a0..dd70985 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'r') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
fdtput.c +2 -0
index 428745a..c2fecf4 100644
@@ -433,6 +433,8 @@ int main(int argc, char *argv[])
 			if (utilfdt_decode_type(optarg, &disp.type,
 					&disp.size))
 				usage("Invalid type string");
+			if (disp.type == 'r')
+				usage("Unsupported raw data type");
 			break;
 
 		case 'v':
tests/run_tests.sh +2 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
     run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
     run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
     run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob
 
     # Here the property size is not a multiple of 4 bytes, so it should fail
     run_wrap_error_test $DTGET -tlx $dtb /randomnode mixed
tests/utilfdt_test.c +4 -1
index c621759..ba6462f 100644
@@ -73,6 +73,9 @@ static void check_sizes(char *modifier, int expected_size)
 
 	*ptr = 's';
 	check(fmt, 's', -1);
+
+	*ptr = 'r';
+	check(fmt, 'r', -1);
 }
 
 static void test_utilfdt_decode_type(void)
@@ -90,7 +93,7 @@ static void test_utilfdt_decode_type(void)
 	/* try every other character */
 	checkfail("");
 	for (ch = ' '; ch < 127; ch++) {
-		if (!strchr("iuxs", ch)) {
+		if (!strchr("iuxsr", ch)) {
 			*fmt = ch;
 			fmt[1] = '\0';
 			checkfail(fmt);
util.c +11 -10
index 40274fb..f4625a0 100644
@@ -340,24 +340,25 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 
 	/* get the conversion qualifier */
 	*size = -1;
-	if (strchr("hlLb", *fmt)) {
-		qualifier = *fmt++;
-		if (qualifier == *fmt) {
-			switch (*fmt++) {
-/* TODO:		case 'l': qualifier = 'L'; break;*/
-			case 'h':
+	for (; *(fmt + 1); fmt++) {
+		if (!strchr("hlLb", *fmt))
+			return -1;
+		if (qualifier) {
+			if (*fmt == 'h' && qualifier == 'h')
 				qualifier = 'b';
-				break;
-			}
+			else
+				return -1;
+		} else {
+			qualifier = *fmt;
 		}
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if (!strchr("iuxsr", *fmt))
 		return -1;
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'r')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
util.h +2 -1
index c45b2c2..7a4e910 100644
@@ -143,6 +143,7 @@ int utilfdt_write_err(const char *filename, const void *blob);
  *		i	signed integer
  *		u	unsigned integer
  *		x	hex
+ *		r	raw
  *
  * TODO: Implement ll modifier (8 bytes)
  * TODO: Implement o type (octal)
@@ -160,7 +161,7 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size);
  */
 
 #define USAGE_TYPE_MSG \
-	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
+	"<type>\ts=string, i=int, u=unsigned, x=hex, r=raw\n" \
 	"\tOptional modifier prefix:\n" \
 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
 
Signature · 1 line
2.31.1

A message that is not in the archive (sent off-list or not yet received): 20211209053041.17984-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#8 lore raw

Re: [PATCH V3] Support 'r' format for printing raw bytes with fdtget

Quoted 12 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'r' (raw) format. Example usage:
fdtget -t r firmware.itb /images/foo data > image.raw

Quoted 7 lines

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

---
V2: Update usage info & add tests
V3: Use "r" instead of "b" to avoid confusiong with qualifier

Thanks for the update.

Quoted 44 lines
---
 Documentation/manual.txt |  2 +-
 fdtget.c                 |  5 +++++
 fdtput.c                 |  2 ++
 tests/run_tests.sh       |  2 ++
 tests/utilfdt_test.c     |  5 ++++-
 util.c                   | 21 +++++++++++----------
 util.h                   |  3 ++-
 7 files changed, 27 insertions(+), 13 deletions(-)
Documentation/manual.txt +1 -1
index 97e53b9..cf4b253 100644
@@ -712,7 +712,7 @@ The syntax of the fdtget command is:
 
 where options are:
 
-    <type>    s=string, i=int, u=unsigned, x=hex
+    <type>    s=string, i=int, u=unsigned, x=hex, r=raw
         Optional modifier prefix:
             hh or b=byte, h=2 byte, l=4 byte (default)
 
fdtget.c +5 -0
index 54fc6a0..dd70985 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'r') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
fdtput.c +0 -0
index 428745a..c2fecf4 100644

fdtput.c int main(int argc, char *argv[]) · line 437

Quoted 5 lines
fdtput.c +2 -0
@@ -433,6 +433,8 @@ int main(int argc, char *argv[])
 			if (utilfdt_decode_type(optarg, &disp.type,
 					&disp.size))
 				usage("Invalid type string");
+			if (disp.type == 'r')
+				usage("Unsupported raw data type");

It would be nice to allow this for fdtput as well as a follow up.

tests/run_tests.sh fdtget_tests () { · line 859

Quoted 13 lines
fdtput.c +0 -0
 			break;
 
 		case 'v':
tests/run_tests.sh +2 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
     run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
     run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
     run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob

I think using \ escapes in strings will be shell dependent behaviour. Have you tested this in shells other than bash?

Quoted 30 lines
tests/run_tests.sh +0 -0
     # Here the property size is not a multiple of 4 bytes, so it should fail
     run_wrap_error_test $DTGET -tlx $dtb /randomnode mixed
tests/utilfdt_test.c +4 -1
index c621759..ba6462f 100644
@@ -73,6 +73,9 @@ static void check_sizes(char *modifier, int expected_size)
 
 	*ptr = 's';
 	check(fmt, 's', -1);
+
+	*ptr = 'r';
+	check(fmt, 'r', -1);
 }
 
 static void test_utilfdt_decode_type(void)
@@ -90,7 +93,7 @@ static void test_utilfdt_decode_type(void)
 	/* try every other character */
 	checkfail("");
 	for (ch = ' '; ch < 127; ch++) {
-		if (!strchr("iuxs", ch)) {
+		if (!strchr("iuxsr", ch)) {
 			*fmt = ch;
 			fmt[1] = '\0';
 			checkfail(fmt);
util.c +0 -0
index 40274fb..f4625a0 100644

util.c int utilfdt_decode_type(const char *fmt, int *type, int *size) · line 343

Quoted 10 lines
util.c +1 -6
@@ -340,24 +340,25 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 
 	/* get the conversion qualifier */
 	*size = -1;
-	if (strchr("hlLb", *fmt)) {
-		qualifier = *fmt++;
-		if (qualifier == *fmt) {
-			switch (*fmt++) {
-/* TODO:		case 'l': qualifier = 'L'; break;*/
-			case 'h':
+	for (; *(fmt + 1); fmt++) {

With 'r' instead of 'b', I'm not sure you need this extra loop any more.

Quoted 46 lines
util.c +10 -4
+		if (!strchr("hlLb", *fmt))
+			return -1;
+		if (qualifier) {
+			if (*fmt == 'h' && qualifier == 'h')
 				qualifier = 'b';
-				break;
-			}
+			else
+				return -1;
+		} else {
+			qualifier = *fmt;
 		}
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if (!strchr("iuxsr", *fmt))
 		return -1;
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'r')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
util.h +2 -1
index c45b2c2..7a4e910 100644
@@ -143,6 +143,7 @@ int utilfdt_write_err(const char *filename, const void *blob);
  *		i	signed integer
  *		u	unsigned integer
  *		x	hex
+ *		r	raw
  *
  * TODO: Implement ll modifier (8 bytes)
  * TODO: Implement o type (octal)
@@ -160,7 +161,7 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size);
  */
 
 #define USAGE_TYPE_MSG \
-	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
+	"<type>\ts=string, i=int, u=unsigned, x=hex, r=raw\n" \
 	"\tOptional modifier prefix:\n" \
 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#9 lore raw

Re: [PATCH V3] Support 'r' format for printing raw bytes with fdtget

Quoted 5 lines · David Gibson
Quoted 5 lines
tests/run_tests.sh +0 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
Quoted 8 lines

tests/run_tests.sh fdtget_tests () { · line 859

Quoted 5 lines
tests/run_tests.sh +2 -0
      run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
      run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
      run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob

I think using \ escapes in strings will be shell dependent behaviour. Have you tested this in shells other than bash?

It isn't back dependent but printf dependent. See fdtget-runtest.sh :
printf '%b\n' "$expect" > $EXPECT

It seems even busybox's printf supports %b:

# printf "%b" "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" | hexdump -C
00000000  0a 0b 0c 0d de ea ad be  ef                       |.........|
00000009

A message that is not in the archive (sent off-list or not yet received): c59e41fd-bdfc-c419-e68f-0f6086c9b61e-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#11 lore raw

Re: [PATCH V3] Support 'r' format for printing raw bytes with fdtget

Quoted 15 lines · Rafał Miłecki
Quoted 5 lines · David Gibson
Quoted 5 lines
tests/run_tests.sh +0 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
Quoted 8 lines
Quoted 5 lines

      run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1 run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob +    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible +    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob

I think using \ escapes in strings will be shell dependent behaviour. Have you tested this in shells other than bash?

Quoted 7 lines

It isn't back dependent but printf dependent. See fdtget-runtest.sh :
printf '%b\n' "$expect" > $EXPECT

It seems even busybox's printf supports %b:

# printf "%b" "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" | hexdump -C
00000000  0a 0b 0c 0d de ea ad be  ef                       |.........|
00000009

busybox proof:

# busybox printf 2>&1 | head -n 1
BusyBox v1.30.1 () multi-call binary.
# busybox printf "%b" "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" | hexdump -C
00000000  0a 0b 0c 0d de ea ad be  ef                       |.........|
00000009
#12 lore raw

Re: [PATCH V3] Support 'r' format for printing raw bytes with fdtget

In reply to a message not in the archive
Quoted 15 lines · Rafał Miłecki
Quoted 5 lines · David Gibson
Quoted 5 lines
tests/run_tests.sh +0 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
Quoted 8 lines

tests/run_tests.sh fdtget_tests () { · line 859

Quoted 5 lines
tests/run_tests.sh +2 -0
      run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
      run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
      run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob

I think using \ escapes in strings will be shell dependent behaviour. Have you tested this in shells other than bash?

Quoted 7 lines

It isn't back dependent but printf dependent. See fdtget-runtest.sh :
printf '%b\n' "$expect" > $EXPECT

It seems even busybox's printf supports %b:

# printf "%b" "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" | hexdump -C
00000000  0a 0b 0c 0d de ea ad be  ef                       |.........|
00000009

Ah, sorry, I'd forgotten that fdtget-runtest.sh invoked printf. In that case, it should be fine.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#13 lore raw

PATCH v4 1/1 Support 'r' format for printing raw bytes with fdtget

In reply to a message not in the archive

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'r' (raw) format. Example usage:
fdtget -t r firmware.itb /images/foo data > image.raw

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

---
V2: Update usage info & add tests
V3: Use "r" instead of "b" to avoid confusiong with qualifier
V4: Don't rework utilfdt_decode_type() code handling qualifiers

---
 Documentation/manual.txt | 2 +-
 fdtget.c                 | 5 +++++
 fdtput.c                 | 2 ++
 tests/run_tests.sh       | 2 ++
 tests/utilfdt_test.c     | 5 ++++-
 util.c                   | 4 ++--
 util.h                   | 3 ++-
 7 files changed, 18 insertions(+), 5 deletions(-)
Documentation/manual.txt +1 -1
index 97e53b9..cf4b253 100644
@@ -712,7 +712,7 @@ The syntax of the fdtget command is:
 
 where options are:
 
-    <type>    s=string, i=int, u=unsigned, x=hex
+    <type>    s=string, i=int, u=unsigned, x=hex, r=raw
         Optional modifier prefix:
             hh or b=byte, h=2 byte, l=4 byte (default)
 
fdtget.c +5 -0
index 54fc6a0..dd70985 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'r') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
fdtput.c +2 -0
index 428745a..c2fecf4 100644
@@ -433,6 +433,8 @@ int main(int argc, char *argv[])
 			if (utilfdt_decode_type(optarg, &disp.type,
 					&disp.size))
 				usage("Invalid type string");
+			if (disp.type == 'r')
+				usage("Unsupported raw data type");
 			break;
 
 		case 'v':
tests/run_tests.sh +2 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
     run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
     run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
     run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob
 
     # Here the property size is not a multiple of 4 bytes, so it should fail
     run_wrap_error_test $DTGET -tlx $dtb /randomnode mixed
tests/utilfdt_test.c +4 -1
index c621759..ba6462f 100644
@@ -73,6 +73,9 @@ static void check_sizes(char *modifier, int expected_size)
 
 	*ptr = 's';
 	check(fmt, 's', -1);
+
+	*ptr = 'r';
+	check(fmt, 'r', -1);
 }
 
 static void test_utilfdt_decode_type(void)
@@ -90,7 +93,7 @@ static void test_utilfdt_decode_type(void)
 	/* try every other character */
 	checkfail("");
 	for (ch = ' '; ch < 127; ch++) {
-		if (!strchr("iuxs", ch)) {
+		if (!strchr("iuxsr", ch)) {
 			*fmt = ch;
 			fmt[1] = '\0';
 			checkfail(fmt);
util.c +2 -2
index 40274fb..14d3868 100644
@@ -353,11 +353,11 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if ((*fmt == '\0') || !strchr("iuxsr", *fmt))
 		return -1;
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'r')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
util.h +2 -1
index c45b2c2..7a4e910 100644
@@ -143,6 +143,7 @@ int utilfdt_write_err(const char *filename, const void *blob);
  *		i	signed integer
  *		u	unsigned integer
  *		x	hex
+ *		r	raw
  *
  * TODO: Implement ll modifier (8 bytes)
  * TODO: Implement o type (octal)
@@ -160,7 +161,7 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size);
  */
 
 #define USAGE_TYPE_MSG \
-	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
+	"<type>\ts=string, i=int, u=unsigned, x=hex, r=raw\n" \
 	"\tOptional modifier prefix:\n" \
 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
 
Signature · 1 line
2.31.1

A message that is not in the archive (sent off-list or not yet received): 20211209061420.29466-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

Applied
#15 lore raw

Re: [PATCH V4] Support 'r' format for printing raw bytes with fdtget

Quoted 9 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

Quoted 7 lines

This commit adds support for 'r' (raw) format. Example usage:
fdtget -t r firmware.itb /images/foo data > image.raw

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

Applied, thanks.

Quoted 131 lines

---
V2: Update usage info & add tests
V3: Use "r" instead of "b" to avoid confusiong with qualifier
V4: Don't rework utilfdt_decode_type() code handling qualifiers

---
 Documentation/manual.txt | 2 +-
 fdtget.c                 | 5 +++++
 fdtput.c                 | 2 ++
 tests/run_tests.sh       | 2 ++
 tests/utilfdt_test.c     | 5 ++++-
 util.c                   | 4 ++--
 util.h                   | 3 ++-
 7 files changed, 18 insertions(+), 5 deletions(-)
Documentation/manual.txt +1 -1
index 97e53b9..cf4b253 100644
@@ -712,7 +712,7 @@ The syntax of the fdtget command is:
 
 where options are:
 
-    <type>    s=string, i=int, u=unsigned, x=hex
+    <type>    s=string, i=int, u=unsigned, x=hex, r=raw
         Optional modifier prefix:
             hh or b=byte, h=2 byte, l=4 byte (default)
 
fdtget.c +5 -0
index 54fc6a0..dd70985 100644
@@ -97,6 +97,11 @@ static int show_data(struct display_info *disp, const char *data, int len)
 	if (len == 0)
 		return 0;
 
+	if (disp->type == 'r') {
+		fwrite(data, 1, len, stdout);
+		return 0;
+	}
+
 	is_string = (disp->type) == 's' ||
 		(!disp->type && util_is_printable_string(data, len));
 	if (is_string) {
fdtput.c +2 -0
index 428745a..c2fecf4 100644
@@ -433,6 +433,8 @@ int main(int argc, char *argv[])
 			if (utilfdt_decode_type(optarg, &disp.type,
 					&disp.size))
 				usage("Invalid type string");
+			if (disp.type == 'r')
+				usage("Unsupported raw data type");
 			break;
 
 		case 'v':
tests/run_tests.sh +2 -0
index d100d5a..11068e1 100755
@@ -855,6 +855,8 @@ fdtget_tests () {
     run_fdtget_test 8000 -tx $dtb /cpus/PowerPC,970@1 d-cache-size
     run_fdtget_test "61 62 63 0" -tbx $dtb /randomnode tricky1
     run_fdtget_test "a b c d de ea ad be ef" -tbx $dtb /randomnode blob
+    run_fdtget_test "MyBoardName\0MyBoardFamilyName\0" -tr $dtb / compatible
+    run_fdtget_test "\x0a\x0b\x0c\x0d\xde\xea\xad\xbe\xef" -tr $dtb /randomnode blob
 
     # Here the property size is not a multiple of 4 bytes, so it should fail
     run_wrap_error_test $DTGET -tlx $dtb /randomnode mixed
tests/utilfdt_test.c +4 -1
index c621759..ba6462f 100644
@@ -73,6 +73,9 @@ static void check_sizes(char *modifier, int expected_size)
 
 	*ptr = 's';
 	check(fmt, 's', -1);
+
+	*ptr = 'r';
+	check(fmt, 'r', -1);
 }
 
 static void test_utilfdt_decode_type(void)
@@ -90,7 +93,7 @@ static void test_utilfdt_decode_type(void)
 	/* try every other character */
 	checkfail("");
 	for (ch = ' '; ch < 127; ch++) {
-		if (!strchr("iuxs", ch)) {
+		if (!strchr("iuxsr", ch)) {
 			*fmt = ch;
 			fmt[1] = '\0';
 			checkfail(fmt);
util.c +2 -2
index 40274fb..14d3868 100644
@@ -353,11 +353,11 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 	}
 
 	/* we should now have a type */
-	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
+	if ((*fmt == '\0') || !strchr("iuxsr", *fmt))
 		return -1;
 
 	/* convert qualifier (bhL) to byte size */
-	if (*fmt != 's')
+	if (*fmt != 's' && *fmt != 'r')
 		*size = qualifier == 'b' ? 1 :
 				qualifier == 'h' ? 2 :
 				qualifier == 'l' ? 4 : -1;
util.h +2 -1
index c45b2c2..7a4e910 100644
@@ -143,6 +143,7 @@ int utilfdt_write_err(const char *filename, const void *blob);
  *		i	signed integer
  *		u	unsigned integer
  *		x	hex
+ *		r	raw
  *
  * TODO: Implement ll modifier (8 bytes)
  * TODO: Implement o type (octal)
@@ -160,7 +161,7 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size);
  */
 
 #define USAGE_TYPE_MSG \
-	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
+	"<type>\ts=string, i=int, u=unsigned, x=hex, r=raw\n" \
 	"\tOptional modifier prefix:\n" \
 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#16 lore raw

Re: [PATCH V4] Support 'r' format for printing raw bytes with fdtget

Quoted 13 lines · David Gibson
Quoted 12 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

FT is sometimes used for storing raw data. That is quite common for U-Boot FIT images.

Extracting such data is not trivial currently. Using type 's' (string) will replace every 0x00 (NUL) with 0x20 (space). Using type 'x' will print bytes but in xxd incompatible format.

This commit adds support for 'r' (raw) format. Example usage:
fdtget -t r firmware.itb /images/foo data > image.raw

Quoted 6 lines
Quoted 4 lines

Support for encoding isn't added as there isn't any clean way of passing binary data as command line argument.

Applied, thanks.

Thank you!