devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Support 'b' format for printing raw bytes with fdtget
@ 2021-12-06 15:49 Rafał Miłecki
       [not found] ` <20211206154953.17089-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 12+ messages in thread
From: Rafał Miłecki @ 2021-12-06 15:49 UTC (permalink / raw)
  To: David Gibson, Jon Loeliger
  Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
	Rafał Miłecki

From: Rafał Miłecki <rafal-g1n6cQUeyibVItvQsEIGlw@public.gmane.org>

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

Signed-off-by: Rafał Miłecki <rafal-g1n6cQUeyibVItvQsEIGlw@public.gmane.org>
---
 fdtget.c |  5 +++++
 util.c   | 24 ++++++++++++++----------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/fdtget.c b/fdtget.c
index 54fc6a0..6e4abf1 100644
--- a/fdtget.c
+++ b/fdtget.c
@@ -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) {
diff --git a/util.c b/util.c
index 40274fb..4fa76c5 100644
--- a/util.c
+++ b/util.c
@@ -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;
-- 
2.31.1


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

end of thread, other threads:[~2021-12-09  7:09 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-06 15:49 [PATCH] Support 'b' format for printing raw bytes with fdtget Rafał Miłecki
     [not found] ` <20211206154953.17089-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-12-07 15:07   ` Simon Glass
2021-12-08 17:00   ` [PATCH V2] " Rafał Miłecki
     [not found]     ` <20211208170055.13811-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-12-09  2:22       ` David Gibson
2021-12-09  5:30       ` [PATCH V3] Support 'r' " Rafał Miłecki
     [not found]         ` <20211209053041.17984-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-12-09  5:52           ` David Gibson
2021-12-09  6:06             ` Rafał Miłecki
     [not found]               ` <c59e41fd-bdfc-c419-e68f-0f6086c9b61e-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-12-09  6:17                 ` Rafał Miłecki
2021-12-09  6:23                 ` David Gibson
2021-12-09  6:14           ` [PATCH V4] " Rafał Miłecki
     [not found]             ` <20211209061420.29466-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-12-09  6:52               ` David Gibson
2021-12-09  7:09                 ` Rafał Miłecki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).