diff options
Diffstat (limited to 'futility/cmd_update.c')
-rw-r--r-- | futility/cmd_update.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/futility/cmd_update.c b/futility/cmd_update.c new file mode 100644 index 00000000..ac1246e4 --- /dev/null +++ b/futility/cmd_update.c @@ -0,0 +1,104 @@ +/* + * Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * A reference implementation for AP (and supporting images) firmware updater. + */ + +#include <getopt.h> +#include <stdio.h> +#include <stdlib.h> + +#include "futility.h" +#include "utility.h" + +struct updater_config { +}; + +enum updater_error_codes { + UPDATE_ERR_DONE, + UPDATE_ERR_UNKNOWN, +}; + +static const char * const updater_error_messages[] = { + [UPDATE_ERR_DONE] = "Done (no error)", + [UPDATE_ERR_UNKNOWN] = "Unknown error.", +}; + +/* + * The main updater to update system firmware using the configuration parameter. + * Returns UPDATE_ERR_DONE if success, otherwise failure. + */ +static enum updater_error_codes update_firmware(struct updater_config *cfg) +{ + return UPDATE_ERR_DONE; +} + +/* Command line options */ +static struct option const long_opts[] = { + /* name has_arg *flag val */ + {"help", 0, NULL, 'h'}, + {NULL, 0, NULL, 0}, +}; + +static const char * const short_opts = "h"; + +static void print_help(int argc, char *argv[]) +{ + printf("\n" + "Usage: " MYNAME " %s [OPTIONS]\n" + "\n" + "", + argv[0]); +} + +static int do_update(int argc, char *argv[]) +{ + int i, errorcnt = 0; + struct updater_config cfg = {}; + + printf(">> Firmware updater started.\n"); + + opterr = 0; + while ((i = getopt_long(argc, argv, short_opts, long_opts, 0)) != -1) { + switch (i) { + case 'h': + print_help(argc, argv); + return !!errorcnt; + case '?': + errorcnt++; + if (optopt) + Error("Unrecognized option: -%c\n", optopt); + else if (argv[optind - 1]) + Error("Unrecognized option (possibly '%s')\n", + argv[optind - 1]); + else + Error("Unrecognized option.\n"); + break; + default: + errorcnt++; + Error("Failed parsing options.\n"); + } + } + if (optind < argc) { + errorcnt++; + Error("Unexpected arguments.\n"); + } + if (!errorcnt) { + int r = update_firmware(&cfg); + if (r != UPDATE_ERR_DONE) { + r = Min(r, UPDATE_ERR_UNKNOWN); + Error("%s\n", updater_error_messages[r]); + errorcnt++; + } + } + printf(">> %s: Firmware updater %s.\n", + errorcnt ? "FAILED": "DONE", + errorcnt ? "stopped due to error" : "exited successfully"); + + return !!errorcnt; +} + +DECLARE_FUTIL_COMMAND(update, do_update, VBOOT_VERSION_ALL, + "Update system firmware"); |