From 13674294d4f34863899d350a2977af5c4cdf94a1 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 27 Aug 2011 16:31:41 +0000 Subject: [PATCH] Add a flash_eraseall() function git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@3918 7fd9a85b-ad96-42d3-883c-3090e2eb8679 --- nuttx/ChangeLog | 5 ++ nuttx/drivers/mtd/Make.defs | 2 +- nuttx/drivers/mtd/flash_eraseall.c | 113 +++++++++++++++++++++++++++++ nuttx/drivers/mtd/ftl.c | 43 ++++++++--- nuttx/include/nuttx/mtd.h | 11 +++ 5 files changed, 164 insertions(+), 10 deletions(-) create mode 100755 nuttx/drivers/mtd/flash_eraseall.c diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 3468a5b62..3f5fa7e6d 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -2036,3 +2036,8 @@ * Scripts/makefiles/documents. Several adjustments, corrections and typo fixes so that NuttX will build correctly on FreeBSD using the ASH shell (submitted by Kurt Lidl). + * drivers/mtd/flash_eraseall.c: Add a callable function that accepts + the path to a block driver and then erases the underlying FLASH memory + (assuming that the block driver is an MTD driver wrapped in the FTL + layer). + diff --git a/nuttx/drivers/mtd/Make.defs b/nuttx/drivers/mtd/Make.defs index 58870521b..2a1143158 100644 --- a/nuttx/drivers/mtd/Make.defs +++ b/nuttx/drivers/mtd/Make.defs @@ -36,7 +36,7 @@ # Include MTD drivers -CSRCS += ftl.c m25px.c at45db.c ramtron.c rammtd.c +CSRCS += at45db.c flash_eraseall.c ftl.c m25px.c rammtd.c ramtron.c # Include MTD driver support diff --git a/nuttx/drivers/mtd/flash_eraseall.c b/nuttx/drivers/mtd/flash_eraseall.c new file mode 100755 index 000000000..7d42fe9d4 --- /dev/null +++ b/nuttx/drivers/mtd/flash_eraseall.c @@ -0,0 +1,113 @@ +/**************************************************************************** + * drivers/mtd/flash_eraseall.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include + +/**************************************************************************** + * Private Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: flash_eraseall + * + * Description: + * Call a block driver with the MDIOC_BULKERASE ioctl command. This will + * cause the MTD driver to erase all of the flash. + * + ****************************************************************************/ + +int flash_eraseall(FAR const char *driver) +{ + FAR struct inode *inode; + FAR const struct block_operations *ops; + int ret; + + /* Open the block driver */ + + ret = open_blockdriver(driver ,0, &inode); + if (ret < 0) + { + fdbg("ERROR: Failed to open '%s': %d\n", driver, ret); + return ret; + } + + /* Get the block operations */ + + ops = inode->u.i_bops; + + /* Invoke the block driver ioctl method */ + + ret = -EPERM; + if (ops->ioctl) + { + ret = ops->ioctl(inode, MTDIOC_BULKERASE, 0); + } + + /* Close the block driver */ + + close_blockdriver(inode); + return ret; +} diff --git a/nuttx/drivers/mtd/ftl.c b/nuttx/drivers/mtd/ftl.c index ff3c19345..f01a19470 100755 --- a/nuttx/drivers/mtd/ftl.c +++ b/nuttx/drivers/mtd/ftl.c @@ -420,21 +420,46 @@ static int ftl_geometry(FAR struct inode *inode, struct geometry *geometry) static int ftl_ioctl(FAR struct inode *inode, int cmd, unsigned long arg) { struct ftl_struct_s *dev ; - int ret = -ENOTTY; + int ret; fvdbg("Entry\n"); - - /* Only one ioctl command is supported (and that is just passed on to MTD */ - DEBUGASSERT(inode && inode->i_private); - if (cmd == BIOC_XIPBASE && arg != 0) + + /* Only one block driver ioctl command is supported by this driver (and + * that command is just passed on to the MTD driver in a slightly + * different form). + */ + + if (cmd == BIOC_XIPBASE) { - dev = (struct ftl_struct_s *)inode->i_private; - ret = MTD_IOCTL(dev->mtd, MTDIOC_XIPBASE, arg); - if (ret < 0) + /* The argument accompanying the BIOC_XIPBASE should be non-NULL. If + * DEBUG is enabled, we will catch it here instead of in the MTD + * driver. + */ + +#ifdef CONFIG_DEBUG + if (arg == 0) { - fdbg("MTD ioctl(MTDIOC_XIPBASE) failed: %d\n", ret); + fdbg("ERROR: BIOC_XIPBASE argument is NULL\n"); + return -EINVAL; } +#endif + + /* Just change the BIOC_XIPBASE command to the MTDIOC_XIPBASE command. */ + + cmd = MTDIOC_XIPBASE; + } + + /* No other block driver ioctl commmands are not recognized by this + * driver. Other possible MTD driver ioctl commands are passed through + * to the MTD driver (unchanged). + */ + + dev = (struct ftl_struct_s *)inode->i_private; + ret = MTD_IOCTL(dev->mtd, cmd, arg); + if (ret < 0) + { + fdbg("ERROR: MTD ioctl(%04x) failed: %d\n", cmd, ret); } return ret; diff --git a/nuttx/include/nuttx/mtd.h b/nuttx/include/nuttx/mtd.h index 1acf0f530..95286dee5 100644 --- a/nuttx/include/nuttx/mtd.h +++ b/nuttx/include/nuttx/mtd.h @@ -147,6 +147,17 @@ extern "C" { EXTERN int ftl_initialize(int minor, FAR struct mtd_dev_s *mtd); +/**************************************************************************** + * Name: flash_eraseall + * + * Description: + * Call a block driver with the MDIOC_BULKERASE ioctl command. This will + * cause the MTD driver to erase all of the flash. + * + ****************************************************************************/ + +EXTERN int flash_eraseall(FAR const char *driver); + /**************************************************************************** * Name: rammtd_initialize *