This commit adds 'file' subcommand to handle firmware files.
Signed-off-by: Takashi Sakamoto o-takashi@sakamocchi.jp --- efw-downloader/src/meson.build | 1 + efw-downloader/src/subcmd-file.c | 85 ++++++++++++++++++++++++++++++++ efw-downloader/src/subcmds.h | 2 + 3 files changed, 88 insertions(+) create mode 100644 efw-downloader/src/subcmd-file.c
diff --git a/efw-downloader/src/meson.build b/efw-downloader/src/meson.build index 05d491d..7d11332 100644 --- a/efw-downloader/src/meson.build +++ b/efw-downloader/src/meson.build @@ -22,6 +22,7 @@ sources = [ 'file-cntr.c', 'subcmd-device.c', 'op-device-read.c', + 'subcmd-file.c', ]
headers = [ diff --git a/efw-downloader/src/subcmd-file.c b/efw-downloader/src/subcmd-file.c new file mode 100644 index 0000000..0ee7653 --- /dev/null +++ b/efw-downloader/src/subcmd-file.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// Copyright (c) 2020 Takashi Sakamoto +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <assert.h> + +#include "subcmds.h" + +static void print_help() +{ + printf("Usage\n" + " efw-downloader file FILEPATH OPERATION ARGUMENTS\n" + "\n" + "where:\n" + " FILEPATH: The path to file.\n" + " OPERATION:\n" + " help: print this help message\n" + " ARGUMENTS:\n" + " depending on the OPERATION\n"); +} + +static int parse_args(int argc, char **argv, const char **filepath, const char **op_name) +{ + if (argc < 2) + return -EINVAL; + assert(strncmp(argv[1], "file", sizeof("file")) == 0); + + if (argc < 3) + return -EINVAL; + *filepath = argv[2]; + + if (argc < 4) + return -EINVAL; + *op_name = argv[3]; + + return 0; +} + +int subcmd_file(int argc, char **argv) +{ + struct { + const char *name; + size_t size; + int (*op)(int argc, char **argv, struct file_cntr *cntr); + } *entry, entries[] = { + }; + const char *op_name; + const char *filepath; + struct file_cntr cntr = {0}; + int err; + int i; + + err = parse_args(argc, argv, &filepath, &op_name); + if (err < 0) { + print_help(); + return EXIT_FAILURE; + } + + for (i = 0; i < sizeof(entries) / sizeof(entries[0]); ++i) { + entry = entries + i; + if (strncmp(op_name, entry->name, entry->size) == 0) + break; + } + if (i == sizeof(entries) / sizeof(entries[0])) { + print_help(); + return EXIT_FAILURE; + } + + err = file_cntr_parse(&cntr, filepath); + if (err < 0) { + printf("Fail to parse: %s\n", strerror(-err)); + return EXIT_FAILURE; + } + + err = entry->op(argc, argv, &cntr); + + file_cntr_release(&cntr); + + if (err < 0) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} diff --git a/efw-downloader/src/subcmds.h b/efw-downloader/src/subcmds.h index 70cbb5a..b1810b3 100644 --- a/efw-downloader/src/subcmds.h +++ b/efw-downloader/src/subcmds.h @@ -4,8 +4,10 @@ #define __SUBCMDS_H__
#include "efw-proto.h" +#include "file-cntr.h"
int subcmd_device(int argc, char **argv); +int subcmd_file(int argc, char **argv);
void op_device_read(int argc, char **argv, EfwProto *proto, GError **error);