blob: 49e0c60d43af47e43da51b41c7d0450ceee4d38c [file] [log] [blame]
Nico Hubera7050432023-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_prefixes[] = { "flashprog-", "flash", "f" };
23
24static const struct {
25 const char *name;
26 int (*main)(int argc, char *argv[]);
27} commands[] = {
28 { "prog", flashprog_classic_main },
Nico Huber1f693db2023-02-11 18:28:33 +010029 { "cfg", flashprog_config_main },
30 { "config", flashprog_config_main },
Nico Huber8f7122c2023-02-11 18:28:33 +010031 { "wp", flashprog_wp_main },
32 { "write-protect", flashprog_wp_main },
Nico Hubera7050432023-02-11 18:01:26 +010033};
34
35static void usage(const char *const name)
36{
37 fprintf(stderr, "\nUsage: %s [<command>] [<argument>...]\n", name);
38 fprintf(stderr, "\nWhere <command> can be\n\n"
39 " prog Standard memory operations\n"
40 " (read/erase/write/verify)\n"
Nico Huber1f693db2023-02-11 18:28:33 +010041 " cfg | config Status/config register operations\n"
Nico Huber8f7122c2023-02-11 18:28:33 +010042 " wp | write-protect Write-protection operations\n"
Nico Hubera7050432023-02-11 18:01:26 +010043 "\n"
44 "The default is 'prog'. See `%s <command> --help`\n"
45 "for further instructions.\n\n", name);
46 exit(1);
47}
48
49static int combine_argv01(char *argv[])
50{
51 const size_t len = strlen(argv[0]) + 1 + strlen(argv[1]) + 1;
52 char *const argv0 = malloc(len);
53 if (!argv0) {
54 fprintf(stderr, "Out of memory!\n");
55 return 1;
56 }
57 snprintf(argv0, len, "%s %s", argv[0], argv[1]);
58 argv[1] = argv0;
59 return 0;
60}
61
62int main(int argc, char *argv[])
63{
64 const char *cmd;
65 size_t i;
66
67 print_version();
68 print_banner();
69
70 if (argc < 1)
71 usage("flashprog");
72
73 /* Turn something like `./flashprog-cmd` into `flashprog-cmd`: */
74 const char *const slash = strrchr(argv[0], '/');
75 if (slash)
76 cmd = slash + 1;
77 else
78 cmd = argv[0];
79
80 if (strcmp(cmd, "flashprog")) {
81 /* Strip command prefix, i.e. `flashprog-cmd` becomes `cmd`: */
82 for (i = 0; i < ARRAY_SIZE(command_prefixes); ++i) {
83 if (!strncmp(cmd, command_prefixes[i],
84 strlen(command_prefixes[i]))) {
85 cmd += strlen(command_prefixes[i]);
86 break;
87 }
88 }
89 }
90
91 /* Run `cmd` if found: */
92 for (i = 0; i < ARRAY_SIZE(commands); ++i) {
93 if (!strcmp(cmd, commands[i].name))
94 return commands[i].main(argc, argv);
95 }
96
97 /* Try to find command as first argument in argv[1]: */
98 for (i = 0; argc >= 2 && i < ARRAY_SIZE(commands); ++i) {
99 if (!strcmp(argv[1], commands[i].name)) {
100 /* Squash argv[0] into argv[1]: */
101 if (combine_argv01(argv))
102 return 1;
103 return commands[i].main(argc - 1, argv + 1);
104 }
105 }
106
107 /* Bail if first argument looks like an
108 unknown command (i.e. not starting with `-'): */
109 if (argc >= 2 && argv[1][0] != '-')
110 usage(argv[0]);
111
112 /* We're still here? Fall back to classic cli: */
113 return flashprog_classic_main(argc, argv);
114}