diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2011-05-06 11:35:08 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2011-05-06 11:35:08 +0200 |
commit | 30eb4cdf17e5cc0d42bcda5de6a5dccecb06bcf0 (patch) | |
tree | 38ebcb0dc08fc4cd35065b7cf4ce776315e03543 /commands | |
parent | 3e19d858760a138cb8cba92a2395036bd70937e3 (diff) | |
parent | 64476d2177a53228319c4ab5b99cfb66ec8cc365 (diff) | |
download | barebox-30eb4cdf17e5cc0d42bcda5de6a5dccecb06bcf0.tar.gz barebox-30eb4cdf17e5cc0d42bcda5de6a5dccecb06bcf0.tar.xz |
Merge branch 'next'
Diffstat (limited to 'commands')
-rw-r--r-- | commands/Kconfig | 27 | ||||
-rw-r--r-- | commands/Makefile | 3 | ||||
-rw-r--r-- | commands/cp.c | 3 | ||||
-rw-r--r-- | commands/loadenv.c | 2 | ||||
-rw-r--r-- | commands/nand.c | 258 | ||||
-rw-r--r-- | commands/saveenv.c | 2 | ||||
-rw-r--r-- | commands/usb.c | 41 |
7 files changed, 75 insertions, 261 deletions
diff --git a/commands/Kconfig b/commands/Kconfig index f137f47324..f192d30978 100644 --- a/commands/Kconfig +++ b/commands/Kconfig @@ -1,7 +1,14 @@ config REGINFO bool -menu "Commands " +config COMMAND_SUPPORT + bool + depends on !SHELL_NONE + default y + +if COMMAND_SUPPORT + +menu "commands " menu "scripting " @@ -29,11 +36,13 @@ config CMD_LOADENV prompt "loadenv" config CMD_EXPORT + depends on ENVIRONMENT_VARIABLES tristate prompt "export" config CMD_PRINTENV tristate + depends on ENVIRONMENT_VARIABLES prompt "printenv" config CMD_READLINE @@ -142,6 +151,12 @@ config CMD_UMOUNT default y prompt "umount" +config CMD_NAND + tristate + default y + depends on NAND + prompt "nand" + endmenu menu "console " @@ -387,4 +402,14 @@ config CMD_LED_TRIGGER The trigger command allows to control LED triggers from the command line. +config CMD_USB + bool + depends on USB + prompt "usb command" + default y + help + The usb command allows to rescan for USB devices. + endmenu + +endif diff --git a/commands/Makefile b/commands/Makefile index c89adcfff0..f7ef9a8a2b 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -37,7 +37,7 @@ obj-$(CONFIG_CMD_EXPORT) += export.o obj-$(CONFIG_CMD_PRINTENV) += printenv.o obj-$(CONFIG_CMD_SAVEENV) += saveenv.o obj-$(CONFIG_CMD_LOADENV) += loadenv.o -obj-$(CONFIG_NAND) += nand.o +obj-$(CONFIG_CMD_NAND) += nand.o obj-$(CONFIG_CMD_TRUE) += true.o obj-$(CONFIG_CMD_FALSE) += false.o obj-$(CONFIG_CMD_VERSION) += version.o @@ -55,3 +55,4 @@ obj-$(CONFIG_CMD_PASSWD) += passwd.o obj-$(CONFIG_CMD_LOGIN) += login.o obj-$(CONFIG_CMD_LED) += led.o obj-$(CONFIG_CMD_LED_TRIGGER) += trigger.o +obj-$(CONFIG_CMD_USB) += usb.o diff --git a/commands/cp.c b/commands/cp.c index ae8719b24b..342810528b 100644 --- a/commands/cp.c +++ b/commands/cp.c @@ -31,6 +31,7 @@ #include <libbb.h> #include <fs.h> #include <malloc.h> +#include <libgen.h> /** * @param[in] cmdtp FIXME @@ -60,7 +61,7 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[]) for (i = 1; i < argc - 1; i++) { if (last_is_dir) { char *dst; - dst = concat_path_file(argv[argc - 1], argv[i]); + dst = concat_path_file(argv[argc - 1], basename(argv[i])); ret = copy_file(argv[i], dst); if (ret) goto out; diff --git a/commands/loadenv.c b/commands/loadenv.c index c33c34fcee..5568aced88 100644 --- a/commands/loadenv.c +++ b/commands/loadenv.c @@ -36,7 +36,7 @@ static int do_loadenv(struct command *cmdtp, int argc, char *argv[]) else dirname = argv[2]; if (argc < 2) - filename = "/dev/env0"; + filename = default_environment_path; else filename = argv[1]; printf("loading environment from %s\n", filename); diff --git a/commands/nand.c b/commands/nand.c index d3921b9f2d..88f242df0f 100644 --- a/commands/nand.c +++ b/commands/nand.c @@ -32,249 +32,6 @@ #include <fcntl.h> #include <libgen.h> -struct nand_bb { - char *devname; - char *name; - int open; - int needs_write; - - struct mtd_info_user info; - - size_t raw_size; - size_t size; - int fd; - off_t offset; - void *writebuf; - - struct cdev cdev; -}; - -static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count, - unsigned long offset, ulong flags) -{ - struct nand_bb *bb = cdev->priv; - int ret, bytes = 0, now; - - debug("%s %d %d\n", __func__, offset, count); - - while(count) { - ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)bb->offset); - if (ret < 0) - return ret; - - if (ret) { - printf("skipping bad block at 0x%08lx\n", bb->offset); - bb->offset += bb->info.erasesize; - continue; - } - - now = min(count, (size_t)(bb->info.erasesize - - (bb->offset % bb->info.erasesize))); - lseek(bb->fd, bb->offset, SEEK_SET); - ret = read(bb->fd, buf, now); - if (ret < 0) - return ret; - buf += now; - count -= now; - bb->offset += now; - bytes += now; - }; - - return bytes; -} - -/* Must be a multiple of the largest NAND page size */ -#define BB_WRITEBUF_SIZE 4096 - -static int nand_bb_write_buf(struct nand_bb *bb, size_t count) -{ - int ret, now; - void *buf = bb->writebuf; - int cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1); - - while (count) { - ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)cur_ofs); - if (ret < 0) - return ret; - - if (ret) { - debug("skipping bad block at 0x%08x\n", cur_ofs); - bb->offset += bb->info.erasesize; - cur_ofs += bb->info.erasesize; - continue; - } - - now = min(count, (size_t)(bb->info.erasesize)); - lseek(bb->fd, cur_ofs, SEEK_SET); - ret = write(bb->fd, buf, now); - if (ret < 0) - return ret; - buf += now; - count -= now; - cur_ofs += now; - }; - - return 0; -} - -static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count, - unsigned long offset, ulong flags) -{ - struct nand_bb *bb = cdev->priv; - int bytes = count, now, wroffs, ret; - - debug("%s offset: 0x%08x count: 0x%08x\n", __func__, offset, count); - - while (count) { - wroffs = bb->offset % BB_WRITEBUF_SIZE; - now = min((int)count, BB_WRITEBUF_SIZE - wroffs); - memcpy(bb->writebuf + wroffs, buf, now); - - if (wroffs + now == BB_WRITEBUF_SIZE) { - bb->needs_write = 0; - ret = nand_bb_write_buf(bb, BB_WRITEBUF_SIZE); - if (ret) - return ret; - } else { - bb->needs_write = 1; - } - - bb->offset += now; - count -= now; - buf += now; - } - - return bytes; -} - -static int nand_bb_erase(struct cdev *cdev, size_t count, unsigned long offset) -{ - struct nand_bb *bb = cdev->priv; - - if (offset != 0) { - printf("can only erase from beginning of device\n"); - return -EINVAL; - } - - lseek(bb->fd, 0, SEEK_SET); - - return erase(bb->fd, bb->raw_size, 0); -} - -static int nand_bb_open(struct cdev *cdev, struct filep *f) -{ - struct nand_bb *bb = cdev->priv; - - if (bb->open) - return -EBUSY; - - bb->open = 1; - bb->offset = 0; - bb->needs_write = 0; - bb->writebuf = xmalloc(BB_WRITEBUF_SIZE); - - return 0; -} - -static int nand_bb_close(struct cdev *cdev, struct filep *f) -{ - struct nand_bb *bb = cdev->priv; - - if (bb->needs_write) - nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE); - - bb->open = 0; - free(bb->writebuf); - - return 0; -} - -static int nand_bb_calc_size(struct nand_bb *bb) -{ - ulong pos = 0; - int ret; - - while (pos < bb->raw_size) { - ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)pos); - if (ret < 0) - return ret; - if (!ret) - bb->cdev.size += bb->info.erasesize; - - pos += bb->info.erasesize; - } - - return 0; -} - -static struct file_operations nand_bb_ops = { - .open = nand_bb_open, - .close = nand_bb_close, - .read = nand_bb_read, - .write = nand_bb_write, - .erase = nand_bb_erase, -}; - -/** - * Add a bad block aware device ontop of another (NAND) device - * @param[in] dev The device to add a partition on - * @param[in] name Partition name (can be obtained with devinfo command) - * @return The device representing the new partition. - */ -int dev_add_bb_dev(char *path, const char *name) -{ - struct nand_bb *bb; - int ret = -ENOMEM; - struct stat s; - - bb = xzalloc(sizeof(*bb)); - bb->devname = asprintf("/dev/%s", basename(path)); - if (!bb->devname) - goto out1; - - if (name) - bb->cdev.name = strdup(name); - else - bb->cdev.name = asprintf("%s.bb", basename(path)); - - if (!bb->cdev.name) - goto out2; - - ret = stat(bb->devname, &s); - if (ret) - goto out3; - - bb->raw_size = s.st_size; - - bb->fd = open(bb->devname, O_RDWR); - if (bb->fd < 0) { - ret = -ENODEV; - goto out3; - } - - ret = ioctl(bb->fd, MEMGETINFO, &bb->info); - if (ret) - goto out4; - - nand_bb_calc_size(bb); - bb->cdev.ops = &nand_bb_ops; - bb->cdev.priv = bb; - - devfs_create(&bb->cdev); - - return 0; - -out4: - close(bb->fd); -out3: - free(bb->cdev.name); -out2: - free(bb->devname); -out1: - free(bb); - return ret; -} - #define NAND_ADD (1 << 0) #define NAND_DEL (1 << 1) #define NAND_MARKBAD (1 << 2) @@ -282,7 +39,6 @@ out1: static int do_nand(struct command *cmdtp, int argc, char *argv[]) { int opt; - struct nand_bb *bb; int command = 0, badblock = 0; while((opt = getopt(argc, argv, "adb:")) > 0) { @@ -306,7 +62,7 @@ static int do_nand(struct command *cmdtp, int argc, char *argv[]) if (command & NAND_ADD) { while (optind < argc) { - if (dev_add_bb_dev(argv[optind], NULL)) + if (dev_add_bb_dev(basename(argv[optind]), NULL)) return 1; optind++; @@ -315,17 +71,7 @@ static int do_nand(struct command *cmdtp, int argc, char *argv[]) if (command & NAND_DEL) { while (optind < argc) { - struct cdev *cdev; - - cdev = cdev_by_name(basename(argv[optind])); - if (!cdev) { - printf("no such device: %s\n", argv[optind]); - return 1; - } - bb = cdev->priv; - close(bb->fd); - devfs_remove(cdev); - free(bb); + dev_remove_bb_dev(basename(argv[optind])); optind++; } } diff --git a/commands/saveenv.c b/commands/saveenv.c index 2f969fe7b9..11a9fee54f 100644 --- a/commands/saveenv.c +++ b/commands/saveenv.c @@ -41,7 +41,7 @@ static int do_saveenv(struct command *cmdtp, int argc, char *argv[]) else dirname = argv[2]; if (argc < 2) - filename = "/dev/env0"; + filename = default_environment_path; else filename = argv[1]; diff --git a/commands/usb.c b/commands/usb.c new file mode 100644 index 0000000000..0aac78ef09 --- /dev/null +++ b/commands/usb.c @@ -0,0 +1,41 @@ +/* + * usb.c - rescan for USB devices + * + * Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <common.h> +#include <command.h> +#include <usb/usb.h> + +static int do_usb(struct command *cmdtp, int argc, char *argv[]) +{ + usb_rescan(); + + return 0; +} + +static const __maybe_unused char cmd_usb_help[] = +"Usage: usb\n" +"(re-)detect USB devices\n"; + +BAREBOX_CMD_START(usb) + .cmd = do_usb, + .usage = "(re-)detect USB devices", + BAREBOX_CMD_HELP(cmd_usb_help) +BAREBOX_CMD_END |