2 * Copyright (c) 2010-2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
41 int usage(struct cmd_param *);
46 int (*handler)(struct cmd_param *);
48 { "help", nand_help_usage, usage },
49 { "read", nand_read_usage, nand_read },
50 { "write", nand_write_usage, nand_write },
51 { "erase", nand_erase_usage, nand_erase },
52 { "readoob", nand_read_oob_usage, nand_read_oob },
53 { "writeoob", nand_write_oob_usage, nand_write_oob },
54 { "info", nand_info_usage, nand_info },
59 _param_get_stringx(struct cmd_param *params, const char *name, int doexit)
63 for (i = 0; params[i].name[0] != '\0'; i++) {
64 if (!strcmp(params[i].name, name))
65 return params[i].value;
69 perrorf("Missing parameter %s", name);
76 param_get_string(struct cmd_param *params, const char *name)
79 return (_param_get_stringx(params, name, 0));
83 _param_get_intx(struct cmd_param *params, const char *name, int doexit)
86 char *str = _param_get_stringx(params, name, doexit);
92 ret = (int)strtol(str, (char **)NULL, 10);
95 perrorf("Invalid value for parameter %s", name);
105 param_get_intx(struct cmd_param *params, const char *name)
108 return (_param_get_intx(params, name, 1));
112 param_get_int(struct cmd_param *params, const char *name)
115 return (_param_get_intx(params, name, 0));
119 param_get_boolean(struct cmd_param *params, const char *name)
121 char *str = param_get_string(params, name);
126 if (!strcmp(str, "true") || !strcmp(str, "yes"))
133 param_has_value(struct cmd_param *params, const char *name)
137 for (i = 0; params[i].name[0] != '\0'; i++) {
138 if (!strcmp(params[i].name, name))
146 param_get_count(struct cmd_param *params)
150 for (i = 0; params[i].name[0] != '\0'; i++);
156 hexdumpoffset(uint8_t *buf, int length, int off)
159 for (i = 0; i < length; i += 16) {
160 printf("%08x: ", off + i);
162 for (j = 0; j < 16; j++)
163 printf("%02x ", buf[i+j]);
167 for (j = 0; j < 16; j++) {
168 printf("%c", isalnum(buf[i+j])
178 hexdump(uint8_t *buf, int length)
181 hexdumpoffset(buf, length, 0);
187 void *ret = malloc(len);
190 fprintf(stderr, "Cannot allocate buffer of %zd bytes. "
199 perrorf(const char *format, ...)
203 va_start(args, format);
204 vfprintf(stderr, format, args);
206 fprintf(stderr, ": %s\n", strerror(errno));
210 usage(struct cmd_param *params)
214 if (!params || !param_get_count(params)) {
215 fprintf(stderr, "Usage: nandtool <command> [arguments...]\n");
216 fprintf(stderr, "Arguments are in form 'name=value'.\n\n");
217 fprintf(stderr, "Available commands:\n");
219 for (i = 0; commands[i].name != NULL; i++)
220 fprintf(stderr, "\t%s\n", commands[i].name);
222 fprintf(stderr, "\n");
223 fprintf(stderr, "For information about particular command, "
225 fprintf(stderr, "'nandtool help topic=<command>'\n");
226 } else if (param_has_value(params, "topic")) {
227 for (i = 0; commands[i].name != NULL; i++) {
228 if (!strcmp(param_get_string(params, "topic"),
230 fprintf(stderr, commands[i].usage, "nandtool");
235 fprintf(stderr, "No such command\n");
236 return (EX_SOFTWARE);
238 fprintf(stderr, "Wrong arguments given. Try: 'nandtool help'\n");
245 main(int argc, const char *argv[])
247 struct cmd_param *params;
255 params = malloc(sizeof(struct cmd_param) * (argc - 1));
257 for (i = 2, idx = 0; i < argc; i++, idx++) {
258 if (sscanf(argv[i], "%63[^=]=%63s", params[idx].name,
259 params[idx].value) < 2) {
260 fprintf(stderr, "Syntax error in argument %d. "
261 "Argument should be in form 'name=value'.\n", i);
267 params[idx].name[0] = '\0';
268 params[idx].value[0] = '\0';
270 for (i = 0; commands[i].name != NULL; i++) {
271 if (!strcmp(commands[i].name, argv[1])) {
272 ret = commands[i].handler(params);
279 fprintf(stderr, "Unknown command. Try '%s help'\n", argv[0]);