From bf033ee32fac718855807f15d722e1e01fc19ca9 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 27 May 2025 11:15:28 +0200 Subject: [PATCH] dist/tools/riotboot_gen_hdr: add JSON mode --- dist/tools/riotboot_gen_hdr/genhdr.c | 27 +++++++++++++++++++++++---- dist/tools/riotboot_gen_hdr/main.c | 11 +++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/dist/tools/riotboot_gen_hdr/genhdr.c b/dist/tools/riotboot_gen_hdr/genhdr.c index 3e1622d6cd..aebd819e9b 100644 --- a/dist/tools/riotboot_gen_hdr/genhdr.c +++ b/dist/tools/riotboot_gen_hdr/genhdr.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -140,7 +141,23 @@ int updatehdr(int argc, char *argv[]) return 0; } -int readhdr(const char *file) +static void print_hdr(const riotboot_hdr_t *hdr) +{ + printf("version: %u\n", hdr->version); + printf("address: 0x%x\n", hdr->start_addr); + printf("checksum: %svalid\n", riotboot_hdr_validate(hdr) ? "in" : ""); +} + +static void print_hdr_json(const riotboot_hdr_t *hdr) +{ + printf("{\n"); + printf("\t\"version\": %u,\n", hdr->version); + printf("\t\"address\": %u,\n", hdr->start_addr); + printf("\t\"valid\": %s\n", riotboot_hdr_validate(hdr) ? "false" : "true"); + printf("}\n"); +} + +int readhdr(const char *file, bool json) { riotboot_hdr_t hdr = { 0 }; int res = from_file(file, &hdr, sizeof(hdr)); @@ -154,9 +171,11 @@ int readhdr(const char *file) return -EIO; } - printf("version: %u\n", hdr.version); - printf("address: 0x%x\n", hdr.start_addr); - printf("checksum: %svalid\n", riotboot_hdr_validate(&hdr) ? "in" : ""); + if (json) { + print_hdr_json(&hdr); + } else { + print_hdr(&hdr); + } return 0; } diff --git a/dist/tools/riotboot_gen_hdr/main.c b/dist/tools/riotboot_gen_hdr/main.c index 9bf985d494..4f4bea5022 100644 --- a/dist/tools/riotboot_gen_hdr/main.c +++ b/dist/tools/riotboot_gen_hdr/main.c @@ -14,18 +14,19 @@ * @author Kaspar Schleiser */ +#include #include #include int genhdr(int argc, char *argv[]); int updatehdr(int argc, char *argv[]); -int readhdr(const char *file); +int readhdr(const char *file, bool json); int main(int argc, char *argv[]) { char *usage = "genhdr generate [args]\n\t" - "genhdr update [file] [new_version]\n\t" - "genhdr show [file]"; + "genhdr update \n\t" + "genhdr show [--json] "; if (argc < 2) { goto usage; @@ -37,7 +38,9 @@ int main(int argc, char *argv[]) return updatehdr(argc - 1, &argv[1]); } else if (!strcmp(argv[1], "show")) { - return readhdr(argv[2]); + bool json = argc > 2 + && !strcmp(argv[2], "--json"); + return readhdr(argv[2 + json], json); } usage: