refactor program mode selection

This commit is contained in:
Olaf Rempel 2014-12-26 12:08:48 +01:00
parent 50574d6f16
commit 25e66d9993
1 changed files with 26 additions and 9 deletions

View File

@ -33,6 +33,17 @@
#define ACTION_READ 0x01 #define ACTION_READ 0x01
#define ACTION_WRITE 0x02 #define ACTION_WRITE 0x02
struct prog_mode {
char *progname;
struct multiboot_ops *ops;
};
static struct prog_mode prog_modes[] =
{
{ "twiboot", &twi_ops },
{ "mpmboot", &mpm_ops },
};
struct mboot_action { struct mboot_action {
struct list_head list; struct list_head list;
@ -203,22 +214,28 @@ static int main_optarg_cb(int val, const char *arg, void *privdata)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct multiboot *mboot; struct multiboot *mboot = NULL;
char *progname = strrchr(argv[0], '/'); char *progname = strrchr(argv[0], '/');
progname = (progname != NULL) ? (progname +1) : argv[0]; progname = (progname != NULL) ? (progname +1) : argv[0];
if (strcmp(progname, "twiboot") == 0) { int i;
mboot = twi_ops.alloc(); for (i = 0; i < ARRAY_SIZE(prog_modes); i++) {
} else if (strcmp(progname, "mpmboot") == 0) { struct prog_mode *mode = &prog_modes[i];
mboot = mpm_ops.alloc();
} else { if (strcmp(progname, mode->progname) == 0) {
fprintf(stderr, "invalid progname, use 'twiboot' or 'mpmboot'\n"); mboot = mode->ops->alloc();
return -1; if (mboot == NULL) {
fprintf(stderr, "failed to allocate '%s'\n", progname);
return -1;
}
}
} }
if (mboot == NULL) if (mboot == NULL) {
fprintf(stderr, "invalid progname\n");
return -1; return -1;
}
mboot->verify = 1; mboot->verify = 1;
mboot->progress_cb = progress_mode1_cb; mboot->progress_cb = progress_mode1_cb;