diff options
author | Andrey Smirnov <andrew.smirnov@gmail.com> | 2019-01-28 22:55:31 -0800 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2019-01-29 09:21:28 +0100 |
commit | 60aaeb8be019c75d86cd0aa9615d1ef470983418 (patch) | |
tree | d1ccf2bc2fc4c1a9b9cb3a9abc2a56afa7c00ae7 /lib/misc.c | |
parent | 25082eff77300372316fe85265730d1d3e653287 (diff) | |
download | barebox-60aaeb8be019c75d86cd0aa9615d1ef470983418.tar.gz barebox-60aaeb8be019c75d86cd0aa9615d1ef470983418.tar.xz |
commands: Move mem_parse_options() to lib/misc.c
As a first step of de-cluttering /dev/mem related code, move
mem_parse_options() out of commands/mem.c into lib/misc.c where it
seem to fit better. With this change we no longer explicitly turn this
code off using CONFIG_COMPILE_MEMORY and instead rely on LTO to get
rid of it when it's not being used.
While at it, also fix return value by replacing COMMAND_ERROR_USAGE
with -EINVAL. All of the callers of mem_parse_options() expect
negative error code as a sign of failure and COMMAND_ERROR_USAGE is
not negative.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib/misc.c')
-rw-r--r-- | lib/misc.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/misc.c b/lib/misc.c index 1d20e1b092..cd420a57d8 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -23,6 +23,7 @@ #include <fs.h> #include <string.h> #include <linux/ctype.h> +#include <getopt.h> /* * Like simple_strtoull() but handles an optional G, M, K or k @@ -129,3 +130,44 @@ success: return 0; } EXPORT_SYMBOL(parse_area_spec); + +/* + * Common function for parsing options for the 'md', 'mw', 'memcpy', 'memcmp' + * commands. + */ +int mem_parse_options(int argc, char *argv[], char *optstr, int *mode, + char **sourcefile, char **destfile, int *swab) +{ + int opt; + + while((opt = getopt(argc, argv, optstr)) > 0) { + switch(opt) { + case 'b': + *mode = O_RWSIZE_1; + break; + case 'w': + *mode = O_RWSIZE_2; + break; + case 'l': + *mode = O_RWSIZE_4; + break; + case 'q': + *mode = O_RWSIZE_8; + break; + case 's': + *sourcefile = optarg; + break; + case 'd': + *destfile = optarg; + break; + case 'x': + *swab = 1; + break; + default: + return -EINVAL; + } + } + + return 0; +} + |