Date: Thu, 17 May 2012 07:00:51 +0000 (UTC) From: Grzegorz Bernacki <gber@FreeBSD.org> To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r235531 - projects/nand/usr.sbin/nandtool Message-ID: <201205170700.q4H70pux083536@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: gber Date: Thu May 17 07:00:51 2012 New Revision: 235531 URL: http://svn.freebsd.org/changeset/base/235531 Log: nandtool: various style fixes and improvements Obtained from: Semihalf Supported by: FreeBSD Foundation, Juniper Networks Modified: projects/nand/usr.sbin/nandtool/nand_erase.c projects/nand/usr.sbin/nandtool/nand_info.c projects/nand/usr.sbin/nandtool/nand_read.c projects/nand/usr.sbin/nandtool/nand_readoob.c projects/nand/usr.sbin/nandtool/nand_write.c projects/nand/usr.sbin/nandtool/nand_writeoob.c projects/nand/usr.sbin/nandtool/nandtool.c projects/nand/usr.sbin/nandtool/nandtool.h Modified: projects/nand/usr.sbin/nandtool/nand_erase.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nand_erase.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nand_erase.c Thu May 17 07:00:51 2012 (r235531) @@ -29,7 +29,6 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> #include <stdlib.h> -#include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/disk.h> @@ -41,46 +40,47 @@ int nand_erase(struct cmd_param *params) { struct chip_param_io chip_params; char *dev; - int fd = -1; - off_t pos, count, err = 0; + int fd = -1, ret = 0; + off_t pos, count; off_t start, nblocks, i; int block_size, mult; if (!(dev = param_get_string(params, "dev"))) { fprintf(stderr, "Please supply valid 'dev' parameter.\n"); - return (EINVAL); + return (1); } + if (param_has_value(params, "count")) + count = param_get_intx(params, "count"); + else + count = 1; + if ((fd = g_open(dev, 1)) < 0) { perrorf("Cannot open %s", dev); - return (errno); + return (1); } - if ((count = param_get_int(params, "count")) < 0) - count = 1; - if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) { perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)"); - err = errno; + ret = 1; goto out; } block_size = chip_params.page_size * chip_params.pages_per_block; if (param_has_value(params, "page")) { - pos = chip_params.page_size * param_get_int(params, "page"); + pos = chip_params.page_size * param_get_intx(params, "page"); mult = chip_params.page_size; } else if (param_has_value(params, "block")) { - pos = block_size * param_get_int(params, "block"); + pos = block_size * param_get_intx(params, "block"); mult = block_size; } else if (param_has_value(params, "pos")) { - pos = param_get_int(params, "pos"); + pos = param_get_intx(params, "pos"); mult = 1; - } else { - /* Erase all chip */ + /* Erase whole chip */ if (ioctl(fd, DIOCGMEDIASIZE, &count) == -1) { - err = errno; + ret = 1; goto out; } @@ -90,7 +90,7 @@ int nand_erase(struct cmd_param *params) if (pos % block_size) { fprintf(stderr, "Position must be block-size aligned!\n"); - err = errno; + ret = 1; goto out; } @@ -99,16 +99,16 @@ int nand_erase(struct cmd_param *params) nblocks = count / block_size; for (i = 0; i < nblocks; i++) { - if (g_delete(fd, (start + i) * block_size, block_size) < 0) { + if (g_delete(fd, (start + i) * block_size, block_size) == -1) { perrorf("Cannot erase block %d - probably a bad block", start + i); + ret = 1; } } out: - if (fd) - g_close(fd); + g_close(fd); - return (err); + return (ret); } Modified: projects/nand/usr.sbin/nandtool/nand_info.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nand_info.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nand_info.c Thu May 17 07:00:51 2012 (r235531) @@ -31,7 +31,6 @@ __FBSDID("$FreeBSD$"); #include <stdlib.h> #include <stdint.h> #include <string.h> -#include <errno.h> #include <libgeom.h> #include <sys/disk.h> #include <dev/nand/nand_dev.h> @@ -40,30 +39,31 @@ __FBSDID("$FreeBSD$"); int nand_info(struct cmd_param *params) { struct chip_param_io chip_params; - int fd = -1, err = 0, block_size; + int fd = -1, ret = 0; + int block_size; off_t chip_size, media_size; const char *dev; if ((dev = param_get_string(params, "dev")) == NULL) { fprintf(stderr, "Please supply 'dev' parameter, eg. " "'dev=/dev/gnand0'\n"); - return (EINVAL); + return (1); } - if ((fd = g_open(dev, 1)) < 0) { + if ((fd = g_open(dev, 1)) == -1) { perrorf("Cannot open %s", dev); - return (errno); + return (1); } if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) { perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)"); - err = errno; + ret = 1; goto out; } if (ioctl(fd, DIOCGMEDIASIZE, &media_size) == -1) { perrorf("Cannot ioctl(DIOCGMEDIASIZE)"); - err = errno; + ret = 1; goto out; } @@ -80,8 +80,7 @@ int nand_info(struct cmd_param *params) (uintmax_t)(media_size / 1024 / 1024)); out: - if (fd != -1) - g_close(fd); + g_close(fd); - return (err); + return (ret); } Modified: projects/nand/usr.sbin/nandtool/nand_read.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nand_read.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nand_read.c Thu May 17 07:00:51 2012 (r235531) @@ -30,7 +30,6 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <errno.h> #include <fcntl.h> #include <libgeom.h> #include <sys/disk.h> @@ -40,34 +39,33 @@ __FBSDID("$FreeBSD$"); int nand_read(struct cmd_param *params) { struct chip_param_io chip_params; - int fd = -1, out_fd = -1, ret; + int fd = -1, out_fd = -1, done = 0, ret = 0; char *dev, *out; + int pos, count, mult, block_size; uint8_t *buf = NULL; - int pos, done = 0, count, mult, block_size; - int err = 0; if (!(dev = param_get_string(params, "dev"))) { fprintf(stderr, "You must specify 'dev' parameter\n"); - return (EINVAL); + return (1); } if ((out = param_get_string(params, "out"))) { out_fd = open(out, O_WRONLY|O_CREAT); - if (out_fd < 0) { + if (out_fd == -1) { perrorf("Cannot open %s for writing", out); - return (EINVAL); + return (1); } } - if ((fd = g_open(dev, 1)) < 0) { + if ((fd = g_open(dev, 1)) == -1) { perrorf("Cannot open %s", dev); - err = errno; + ret = 1; goto out; } if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) { perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)"); - err = errno; + ret = 1; goto out; } @@ -84,13 +82,13 @@ int nand_read(struct cmd_param *params) mult = 1; if (pos % chip_params.page_size) { fprintf(stderr, "Position must be page-size aligned!\n"); - err = errno; + ret = 1; goto out; } } else { fprintf(stderr, "You must specify one of: 'block', 'page'," "'pos' arguments\n"); - err = errno; + ret = 1; goto out; } @@ -102,7 +100,7 @@ int nand_read(struct cmd_param *params) if (!(buf = malloc(chip_params.page_size))) { perrorf("Cannot allocate buffer [size %x]", chip_params.page_size); - err = errno; + ret = 1; goto out; } @@ -115,27 +113,27 @@ int nand_read(struct cmd_param *params) goto out; } - done += ret; - if (out_fd != -1) { + done += ret; if ((ret = write(out_fd, buf, chip_params.page_size)) != (int32_t)chip_params.page_size) { perrorf("write error (written %d bytes)", ret); - err = errno; + ret = 1; goto out; } - } else - hexdump(buf, chip_params.page_size); + } else { + hexdumpoffset(buf, chip_params.page_size, done); + done += ret; + } } out: - if (fd != -1) - g_close(fd); + g_close(fd); if (out_fd != -1) close(out_fd); if (buf) free(buf); - return (err); + return (ret); } Modified: projects/nand/usr.sbin/nandtool/nand_readoob.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nand_readoob.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nand_readoob.c Thu May 17 07:00:51 2012 (r235531) @@ -30,7 +30,6 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <errno.h> #include <fcntl.h> #include <libgeom.h> #include <sys/types.h> @@ -43,44 +42,44 @@ int nand_read_oob(struct cmd_param *para struct chip_param_io chip_params; struct nand_oob_rw req; char *dev, *out; - int fd = -1, fd_out = -1; - int page, err = 0; + int fd = -1, fd_out = -1, ret = 0; + int page; uint8_t *buf = NULL; if ((page = param_get_int(params, "page")) < 0) { fprintf(stderr, "You must supply valid 'page' argument.\n"); - return (EINVAL); + return (1); } if (!(dev = param_get_string(params, "dev"))) { fprintf(stderr, "You must supply 'dev' argument.\n"); - return (EINVAL); + return (1); } if ((out = param_get_string(params, "out"))) { - if ((fd_out = open(out, O_WRONLY | O_CREAT)) < 0) { + if ((fd_out = open(out, O_WRONLY | O_CREAT)) == -1) { perrorf("Cannot open %s", out); - err = errno; + ret = 1; goto out; } } - if ((fd = g_open(dev, 1)) < 0) { + if ((fd = g_open(dev, 1)) == -1) { perrorf("Cannot open %s", dev); - err = errno; + ret = 1; goto out; } if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) { perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)"); - err = errno; + ret = 1; goto out; } buf = malloc(chip_params.oob_size); if (buf == NULL) { perrorf("Cannot allocate %d bytes\n", chip_params.oob_size); - err = errno; + ret = 1; goto out; } @@ -90,7 +89,7 @@ int nand_read_oob(struct cmd_param *para if (ioctl(fd, NAND_IO_OOB_READ, &req) == -1) { perrorf("Cannot read OOB from %s", dev); - err = errno; + ret = 1; goto out; } @@ -100,13 +99,13 @@ int nand_read_oob(struct cmd_param *para hexdump(buf, chip_params.oob_size); out: + close(fd_out); + if (fd != -1) - close(fd); - if (fd_out != -1) - g_close(fd_out); + g_close(fd); if (buf) free(buf); - return (err); + return (ret); } Modified: projects/nand/usr.sbin/nandtool/nand_write.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nand_write.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nand_write.c Thu May 17 07:00:51 2012 (r235531) @@ -30,7 +30,6 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <errno.h> #include <fcntl.h> #include <libgeom.h> #include <sys/disk.h> @@ -41,36 +40,34 @@ int nand_write(struct cmd_param *params) { struct chip_param_io chip_params; char *dev, *file; - int fd = -1, in_fd = -1, ret, err = 0; + int in_fd = -1, ret = 0, done = 0; + int fd, block_size, mult, pos, count; uint8_t *buf = NULL; - int block_size, mult, pos, done = 0, count, raw; - - raw = param_get_boolean(params, "raw"); if (!(dev = param_get_string(params, "dev"))) { fprintf(stderr, "Please supply 'dev' argument.\n"); - return (EINVAL); + return (1); } if (!(file = param_get_string(params, "in"))) { fprintf(stderr, "Please supply 'in' argument.\n"); - return (EINVAL); + return (1); } - if ((fd = g_open(dev, 1)) < 0) { + if ((fd = g_open(dev, 1)) == -1) { perrorf("Cannot open %s", dev); - return (errno); + return (1); } - if ((in_fd = open(file, O_RDONLY)) < 0) { + if ((in_fd = open(file, O_RDONLY)) == -1) { perrorf("Cannot open file %s", file); - err = errno; + ret = 1; goto out; } if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) { perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)"); - err = errno; + ret = 1; goto out; } @@ -88,13 +85,13 @@ int nand_write(struct cmd_param *params) if (pos % chip_params.page_size) { fprintf(stderr, "Position must be page-size " "aligned!\n"); - errno = EINVAL; + ret = 1; goto out; } } else { fprintf(stderr, "You must specify one of: 'block', 'page'," "'pos' arguments\n"); - errno = EINVAL; + ret = 1; goto out; } @@ -106,7 +103,7 @@ int nand_write(struct cmd_param *params) if (!(buf = malloc(chip_params.page_size))) { perrorf("Cannot allocate buffer [size %x]", chip_params.page_size); - err = errno; + ret = 1; goto out; } @@ -120,14 +117,14 @@ int nand_write(struct cmd_param *params) break; } else { perrorf("Cannot read from %s", file); - err = errno; + ret = 1; goto out; } } if ((ret = write(fd, buf, chip_params.page_size)) != (int32_t)chip_params.page_size) { - err = errno; + ret = 1; goto out; } @@ -135,13 +132,12 @@ int nand_write(struct cmd_param *params) } out: - if (fd != -1) - g_close(fd); + g_close(fd); if (in_fd != -1) close(in_fd); if (buf) free(buf); - return (0); + return (ret); } Modified: projects/nand/usr.sbin/nandtool/nand_writeoob.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nand_writeoob.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nand_writeoob.c Thu May 17 07:00:51 2012 (r235531) @@ -30,7 +30,6 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <errno.h> #include <fcntl.h> #include <libgeom.h> #include <sys/disk.h> @@ -42,52 +41,52 @@ int nand_write_oob(struct cmd_param *par struct chip_param_io chip_params; struct nand_oob_rw req; char *dev, *in; - int fd = -1, fd_in = -1; + int fd = -1, fd_in = -1, ret = 0; uint8_t *buf = NULL; - int page, err = 0; + int page; if (!(dev = param_get_string(params, "dev"))) { fprintf(stderr, "Please supply valid 'dev' parameter.\n"); - return (EINVAL); + return (1); } if (!(in = param_get_string(params, "in"))) { fprintf(stderr, "Please supply valid 'in' parameter.\n"); - return (EINVAL); + return (1); } if ((page = param_get_int(params, "page")) < 0) { fprintf(stderr, "Please supply valid 'page' parameter.\n"); - return (EINVAL); + return (1); } - if ((fd = g_open(dev, 1)) < 0) { + if ((fd = g_open(dev, 1)) == -1) { perrorf("Cannot open %s", dev); - return (errno); + return (1); } - if ((fd_in = open(in, O_RDONLY)) < 0) { + if ((fd_in = open(in, O_RDONLY)) == -1) { perrorf("Cannot open %s", in); - err = errno; + ret = 1; goto out; } if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) { perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)"); - err = errno; + ret = 1; goto out; } - buf = xmalloc(chip_params.oob_size); + buf = malloc(chip_params.oob_size); if (buf == NULL) { perrorf("Cannot allocate %d bytes\n", chip_params.oob_size); - err = errno; + ret = 1; goto out; } - if (read(fd_in, buf, chip_params.oob_size) < 0) { + if (read(fd_in, buf, chip_params.oob_size) == -1) { perrorf("Cannot read from %s", in); - err = errno; + ret = 1; goto out; } @@ -97,19 +96,18 @@ int nand_write_oob(struct cmd_param *par if (ioctl(fd, NAND_IO_OOB_PROG, &req) == -1) { perrorf("Cannot write OOB to %s", dev); - err = errno; + ret = 1; goto out; } out: - if (fd != -1) - g_close(fd); + g_close(fd); if (fd_in != -1) close(fd_in); if (buf) free(buf); - return (err); + return (ret); } Modified: projects/nand/usr.sbin/nandtool/nandtool.c ============================================================================== --- projects/nand/usr.sbin/nandtool/nandtool.c Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nandtool.c Thu May 17 07:00:51 2012 (r235531) @@ -27,10 +27,10 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <errno.h> #include <stdarg.h> #include <ctype.h> #include <sysexits.h> @@ -55,8 +55,8 @@ static const struct { { NULL, NULL, NULL }, }; -char * -param_get_string(struct cmd_param *params, const char *name) +static char * +_param_get_stringx(struct cmd_param *params, const char *name, int doexit) { int i; @@ -65,29 +65,57 @@ param_get_string(struct cmd_param *param return params[i].value; } + if (doexit) { + perrorf("Missing parameter %s", name); + exit(1); + } return (NULL); } -int -param_get_int(struct cmd_param *params, const char *name) +char * +param_get_string(struct cmd_param *params, const char *name) { - unsigned long ret; - char *str = param_get_string(params, name); - if (!str) { - errno = EINVAL; - return (-1); - } + return (_param_get_stringx(params, name, 0)); +} - ret = strtoul(str, NULL, 0); +static int +_param_get_intx(struct cmd_param *params, const char *name, int doexit) +{ + int ret; + char *str = _param_get_stringx(params, name, doexit); - if (!ret && errno == EINVAL) + if (!str) return (-1); + errno = 0; + ret = (int)strtol(str, (char **)NULL, 10); + if (errno) { + if (doexit) { + perrorf("Invalid value for parameter %s", name); + exit(1); + } + return (-1); + } + return (ret); } int +param_get_intx(struct cmd_param *params, const char *name) +{ + + return (_param_get_intx(params, name, 1)); +} + +int +param_get_int(struct cmd_param *params, const char *name) +{ + + return (_param_get_intx(params, name, 0)); +} + +int param_get_boolean(struct cmd_param *params, const char *name) { char *str = param_get_string(params, name); @@ -125,11 +153,11 @@ param_get_count(struct cmd_param *params } void -hexdump(uint8_t *buf, int length) +hexdumpoffset(uint8_t *buf, int length, int off) { int i, j; for (i = 0; i < length; i += 16) { - printf("%08x: ", i); + printf("%08x: ", off + i); for (j = 0; j < 16; j++) printf("%02x ", buf[i+j]); @@ -146,6 +174,13 @@ hexdump(uint8_t *buf, int length) } } +void +hexdump(uint8_t *buf, int length) +{ + + hexdumpoffset(buf, length, 0); +} + void * xmalloc(size_t len) { Modified: projects/nand/usr.sbin/nandtool/nandtool.h ============================================================================== --- projects/nand/usr.sbin/nandtool/nandtool.h Thu May 17 05:11:57 2012 (r235530) +++ projects/nand/usr.sbin/nandtool/nandtool.h Thu May 17 07:00:51 2012 (r235531) @@ -37,10 +37,12 @@ struct cmd_param char *param_get_string(struct cmd_param *, const char *); int param_get_int(struct cmd_param *, const char *); +int param_get_intx(struct cmd_param *, const char *); int param_get_boolean(struct cmd_param *, const char *); int param_has_value(struct cmd_param *, const char *); int param_get_count(struct cmd_param *); void perrorf(const char *, ...); +void hexdumpoffset(uint8_t *, int, int); void hexdump(uint8_t *, int); void *xmalloc(size_t);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201205170700.q4H70pux083536>