From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: "Rafał Miłecki" <zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: "Jon Loeliger" <loeliger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
"Rafał Miłecki" <rafal-g1n6cQUeyibVItvQsEIGlw@public.gmane.org>
Subject: Re: [PATCH V2] Support 'b' format for printing raw bytes with fdtget
Date: Thu, 9 Dec 2021 13:22:56 +1100 [thread overview]
Message-ID: <YbFogDZI5x5MJktR@yekko> (raw)
In-Reply-To: <20211208170055.13811-1-zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 6188 bytes --]
On Wed, Dec 08, 2021 at 06:00:55PM +0100, Rafał Miłecki wrote:
> 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
>
> Support for encoding isn't added as there isn't any clean way of passing
> binary data as command line argument.
>
> Signed-off-by: Rafał Miłecki <rafal-g1n6cQUeyibVItvQsEIGlw@public.gmane.org>
> ---
> 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(-)
>
> diff --git a/Documentation/manual.txt b/Documentation/manual.txt
> index 97e53b9..73e0593 100644
> --- a/Documentation/manual.txt
> +++ b/Documentation/manual.txt
> @@ -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"?
>
> 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/fdtput.c b/fdtput.c
> index 428745a..99b0bf9 100644
> --- a/fdtput.c
> +++ b/fdtput.c
> @@ -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':
> diff --git a/tests/run_tests.sh b/tests/run_tests.sh
> index d100d5a..484be18 100755
> --- a/tests/run_tests.sh
> +++ b/tests/run_tests.sh
> @@ -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
> diff --git a/tests/utilfdt_test.c b/tests/utilfdt_test.c
> index c621759..906f250 100644
> --- a/tests/utilfdt_test.c
> +++ b/tests/utilfdt_test.c
> @@ -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);
> diff --git a/util.c b/util.c
> index 40274fb..b9e88f1 100644
> --- a/util.c
> +++ b/util.c
> @@ -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;
> diff --git a/util.h b/util.h
> index c45b2c2..fa381fc 100644
> --- a/util.h
> +++ b/util.h
> @@ -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)";
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2021-12-09 2:22 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YbFogDZI5x5MJktR@yekko \
--to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
--cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=loeliger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=rafal-g1n6cQUeyibVItvQsEIGlw@public.gmane.org \
--cc=zajec5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.