From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH] commands: iomem: add JSON output
Date: Wed, 6 Aug 2025 14:53:03 +0200 [thread overview]
Message-ID: <20250806125303.2804664-1-a.fatoum@barebox.org> (raw)
This will make it easier to consume the results in tests.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
commands/iomemport.c | 62 ++++++++++++++++++++++++++++++++++----------
1 file changed, 48 insertions(+), 14 deletions(-)
diff --git a/commands/iomemport.c b/commands/iomemport.c
index d7c960d15b41..fe1273ba7451 100644
--- a/commands/iomemport.c
+++ b/commands/iomemport.c
@@ -11,6 +11,17 @@
#define FLAG_VERBOSE BIT(0)
#define FLAG_IOPORT BIT(1)
+#define FLAG_JSON BIT(2)
+
+static inline bool json_puts(const char *str, int flags)
+{
+ if (flags & FLAG_JSON) {
+ puts(str);
+ return true;
+ }
+
+ return false;
+}
static void __print_resources(struct resource *res, int indent,
ulong *addr, unsigned flags)
@@ -24,32 +35,48 @@ static void __print_resources(struct resource *res, int indent,
if (addr && !region_overlap_end_inclusive(*addr, *addr, res->start, res->end))
return;
- if ((flags & FLAG_VERBOSE) && !(flags & FLAG_IOPORT))
+ if ((flags & FLAG_VERBOSE) && !(flags & (FLAG_IOPORT | FLAG_JSON)))
printf("%-58s", resource_typeattr_format(buf, sizeof(buf), res) ?: "");
for (i = 0; i < indent; i++)
printf(" ");
- if (flags & (FLAG_VERBOSE | FLAG_IOPORT)) {
- snprintf(buf, sizeof(buf), "%pa", &size);
- size_str = buf;
+ if (flags & FLAG_JSON) {
+ printf("{ \"name\": \"%s\", \"start\": \"%pa\", "
+ "\"end\": \"%pa\", \"reserved\": %s",
+ res->name, &res->start, &res->end,
+ is_reserved_resource(res) ? "true" : "false");
} else {
- size_str = size_human_readable(size);
+ if (flags & (FLAG_VERBOSE | FLAG_IOPORT)) {
+ snprintf(buf, sizeof(buf), "%pa", &size);
+ size_str = buf;
+ } else {
+ size_str = size_human_readable(size);
+ }
+
+ printf("%pa - %pa (size %9s) %s%s\n",
+ &res->start, &res->end, size_str,
+ is_reserved_resource(res) ? "[R] " : "",
+ res->name);
}
- printf("%pa - %pa (size %9s) %s%s\n",
- &res->start, &res->end, size_str,
- is_reserved_resource(res) ? "[R] " : "",
- res->name);
-
- list_for_each_entry(r, &res->children, sibling) {
- __print_resources(r, indent + 1, addr, flags);
+ if (!list_empty(&res->children)) {
+ json_puts(", \"children\": [\n", flags);
+ list_for_each_entry(r, &res->children, sibling) {
+ __print_resources(r, indent + 1, addr, flags);
+ if (r->sibling.next != &res->children)
+ json_puts(",\n", flags);
+ }
+ json_puts("]", flags);
}
+
+ json_puts("}", flags);
}
static void print_resources(struct resource *res, ulong *addr, unsigned flags)
{
__print_resources(res, 0, addr, flags);
+ json_puts("\n", flags);
}
static int do_iomem(int argc, char *argv[])
@@ -58,11 +85,14 @@ static int do_iomem(int argc, char *argv[])
unsigned flags = 0;
int opt, ret;
- while((opt = getopt(argc, argv, "v")) > 0) {
+ while((opt = getopt(argc, argv, "vj")) > 0) {
switch(opt) {
case 'v':
flags |= FLAG_VERBOSE;
break;
+ case 'j':
+ flags |= FLAG_JSON;
+ break;
default:
return COMMAND_ERROR_USAGE;
}
@@ -90,12 +120,16 @@ BAREBOX_CMD_HELP_START(iomem)
BAREBOX_CMD_HELP_TEXT("Print barebox view of the physical address space.")
BAREBOX_CMD_HELP_TEXT("An optional ADDRESS can be specified to get information")
BAREBOX_CMD_HELP_TEXT("about its region in particular")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-v", "verbose output")
+BAREBOX_CMD_HELP_OPT ("-j", "JSON output")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(iomem)
.cmd = do_iomem,
BAREBOX_CMD_DESC("show IO memory usage")
- BAREBOX_CMD_OPTS("[-v] [ADDRESS]")
+ BAREBOX_CMD_OPTS("[-vj] [ADDRESS]")
BAREBOX_CMD_GROUP(CMD_GRP_INFO)
BAREBOX_CMD_HELP(cmd_iomem_help)
BAREBOX_CMD_END
--
2.39.5
next reply other threads:[~2025-08-06 12:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-06 12:53 Ahmad Fatoum [this message]
2025-08-07 7:58 ` Sascha Hauer
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=20250806125303.2804664-1-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox