2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010-2012 Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
43 int usage(struct cmd_param *);
48 int (*handler)(struct cmd_param *);
50 { "help", nand_help_usage, usage },
51 { "read", nand_read_usage, nand_read },
52 { "write", nand_write_usage, nand_write },
53 { "erase", nand_erase_usage, nand_erase },
54 { "readoob", nand_read_oob_usage, nand_read_oob },
55 { "writeoob", nand_write_oob_usage, nand_write_oob },
56 { "info", nand_info_usage, nand_info },
61 _param_get_stringx(struct cmd_param *params, const char *name, int doexit)
65 for (i = 0; params[i].name[0] != '\0'; i++) {
66 if (!strcmp(params[i].name, name))
67 return params[i].value;
71 perrorf("Missing parameter %s", name);
78 param_get_string(struct cmd_param *params, const char *name)
81 return (_param_get_stringx(params, name, 0));
85 _param_get_intx(struct cmd_param *params, const char *name, int doexit)
88 char *str = _param_get_stringx(params, name, doexit);
94 ret = (int)strtol(str, (char **)NULL, 10);
97 perrorf("Invalid value for parameter %s", name);
107 param_get_intx(struct cmd_param *params, const char *name)
110 return (_param_get_intx(params, name, 1));
114 param_get_int(struct cmd_param *params, const char *name)
117 return (_param_get_intx(params, name, 0));
121 param_get_boolean(struct cmd_param *params, const char *name)
123 char *str = param_get_string(params, name);
128 if (!strcmp(str, "true") || !strcmp(str, "yes"))
135 param_has_value(struct cmd_param *params, const char *name)
139 for (i = 0; params[i].name[0] != '\0'; i++) {
140 if (!strcmp(params[i].name, name))
148 param_get_count(struct cmd_param *params)
152 for (i = 0; params[i].name[0] != '\0'; i++);
158 hexdumpoffset(uint8_t *buf, int length, int off)
161 for (i = 0; i < length; i += 16) {
162 printf("%08x: ", off + i);
164 for (j = 0; j < 16; j++)
165 printf("%02x ", buf[i+j]);
169 for (j = 0; j < 16; j++) {
170 printf("%c", isalnum(buf[i+j])
180 hexdump(uint8_t *buf, int length)
183 hexdumpoffset(buf, length, 0);
189 void *ret = malloc(len);
192 fprintf(stderr, "Cannot allocate buffer of %zd bytes. "
201 perrorf(const char *format, ...)
205 va_start(args, format);
206 vfprintf(stderr, format, args);
208 fprintf(stderr, ": %s\n", strerror(errno));
212 usage(struct cmd_param *params)
216 if (!params || !param_get_count(params)) {
217 fprintf(stderr, "Usage: nandtool <command> [arguments...]\n");
218 fprintf(stderr, "Arguments are in form 'name=value'.\n\n");
219 fprintf(stderr, "Available commands:\n");
221 for (i = 0; commands[i].name != NULL; i++)
222 fprintf(stderr, "\t%s\n", commands[i].name);
224 fprintf(stderr, "\n");
225 fprintf(stderr, "For information about particular command, "
227 fprintf(stderr, "'nandtool help topic=<command>'\n");
228 } else if (param_has_value(params, "topic")) {
229 for (i = 0; commands[i].name != NULL; i++) {
230 if (!strcmp(param_get_string(params, "topic"),
232 fprintf(stderr, commands[i].usage, "nandtool");
237 fprintf(stderr, "No such command\n");
238 return (EX_SOFTWARE);
240 fprintf(stderr, "Wrong arguments given. Try: 'nandtool help'\n");
247 main(int argc, const char *argv[])
249 struct cmd_param *params;
257 params = malloc(sizeof(struct cmd_param) * (argc - 1));
259 for (i = 2, idx = 0; i < argc; i++, idx++) {
260 if (sscanf(argv[i], "%63[^=]=%63s", params[idx].name,
261 params[idx].value) < 2) {
262 fprintf(stderr, "Syntax error in argument %d. "
263 "Argument should be in form 'name=value'.\n", i);
269 params[idx].name[0] = '\0';
270 params[idx].value[0] = '\0';
272 for (i = 0; commands[i].name != NULL; i++) {
273 if (!strcmp(commands[i].name, argv[1])) {
274 ret = commands[i].handler(params);
281 fprintf(stderr, "Unknown command. Try '%s help'\n", argv[0]);