blob: a4ae75d8b565b131ff6c1f61dae50fbf8ec873ff [file] [log] [blame]
Nico Huber589cea62023-02-11 18:01:26 +01001/*
2 * This file is part of the flashprog project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include "flash.h"
20#include "cli.h"
21
22static const char *const command_prefix = "flashprog-";
23
24static const struct {
25 const char *name;
26 int (*main)(int argc, char *argv[]);
27} commands[] = {
28 { "mem", flashprog_classic_main },
29 { "memory", flashprog_classic_main },
Nico Huber706c37f2023-02-11 18:28:33 +010030 { "cfg", flashprog_config_main },
31 { "config", flashprog_config_main },
Nico Huberab809be2023-02-11 18:28:33 +010032 { "wp", flashprog_wp_main },
33 { "write-protect", flashprog_wp_main },
Nico Huber589cea62023-02-11 18:01:26 +010034};
35
36static void usage(const char *const name)
37{
38 fprintf(stderr, "\nUsage: %s [<command>] [<argument>...]\n", name);
39 fprintf(stderr, "\nWhere <command> can be\n\n"
40 " mem[ory] Standard memory operations\n"
41 " (read/erase/write/verify)\n"
Nico Huber706c37f2023-02-11 18:28:33 +010042 " cfg|config Status/config register operations\n"
Nico Huberab809be2023-02-11 18:28:33 +010043 " wp|write-protect Write-protection operations\n"
Nico Huber589cea62023-02-11 18:01:26 +010044 "\n"
45 "The default is 'memory'. See `%s <command> --help`\n"
46 "for further instructions.\n\n", name);
47 exit(1);
48}
49
50static int combine_argv01(char *argv[])
51{
52 const size_t len = strlen(argv[0]) + 1 + strlen(argv[1]) + 1;
53 char *const argv0 = malloc(len);
54 if (!argv0) {
55 fprintf(stderr, "Out of memory!\n");
56 return 1;
57 }
58 snprintf(argv0, len, "%s %s", argv[0], argv[1]);
59 argv[1] = argv0;
60 return 0;
61}
62
63int main(int argc, char *argv[])
64{
65 const char *cmd;
66 size_t i;
67
68 print_version();
69 print_banner();
70
71 if (argc < 1)
72 usage("flashprog");
73
74 /* Turn something like `./flashprog-cmd` into `flashprog-cmd`: */
75 const char *const slash = strrchr(argv[0], '/');
76 if (slash)
77 cmd = slash + 1;
78 else
79 cmd = argv[0];
80
81 /* Turn `flashprog-cmd` into `cmd`: */
82 if (!strncmp(cmd, command_prefix, strlen(command_prefix)))
83 cmd += strlen(command_prefix);
84
85 /* Run `cmd` if found: */
86 for (i = 0; i < ARRAY_SIZE(commands); ++i) {
87 if (!strcmp(cmd, commands[i].name))
88 return commands[i].main(argc, argv);
89 }
90
91 if (argc < 2)
92 usage(argv[0]);
93
94 /* Try to find command as first argument in argv[1]: */
95 for (i = 0; i < ARRAY_SIZE(commands); ++i) {
96 if (!strcmp(argv[1], commands[i].name)) {
97 /* Squash argv[0] into argv[1]: */
98 if (combine_argv01(argv))
99 return 1;
100 return commands[i].main(argc - 1, argv + 1);
101 }
102 }
103
104 /* We're still here? Fall back to classic cli: */
105 return flashprog_classic_main(argc, argv);
106}