From 4f18b4042975fefc5bb67a924c7ddf17a46049b4 Mon Sep 17 00:00:00 2001 From: Juha Niskanen Date: Wed, 10 May 2017 08:25:39 -0600 Subject: [PATCH] mtd/config: erase block between block read and write --- arch/arm/src/tiva/tiva_flash.c | 3 +- arch/arm/src/tiva/tiva_flash.h | 59 ++++++++++++++++++++++++++++++++++ drivers/mtd/mtd_config.c | 29 +++++++++++++++-- include/nuttx/mtd/mtd.h | 16 ++------- 4 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 arch/arm/src/tiva/tiva_flash.h diff --git a/arch/arm/src/tiva/tiva_flash.c b/arch/arm/src/tiva/tiva_flash.c index c606b18887..952cf215cd 100644 --- a/arch/arm/src/tiva/tiva_flash.c +++ b/arch/arm/src/tiva/tiva_flash.c @@ -57,6 +57,7 @@ #include "up_arch.h" #include "chip.h" +#include "tiva_flash.h" /**************************************************************************** * Pre-processor Definitions @@ -340,7 +341,7 @@ static int tiva_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg) * ****************************************************************************/ -FAR struct mtd_dev_s *up_flashinitialize(void) +FAR struct mtd_dev_s *tiva_flash_initialize(void) { /* Return the implementation-specific state structure as the MTD device */ diff --git a/arch/arm/src/tiva/tiva_flash.h b/arch/arm/src/tiva/tiva_flash.h new file mode 100644 index 0000000000..3f2b542569 --- /dev/null +++ b/arch/arm/src/tiva/tiva_flash.h @@ -0,0 +1,59 @@ +/************************************************************************************ + * arch/arm/src/tiva/tiva_flash.h + * + * Copyright (C) 2017 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. + * + ************************************************************************************/ + +#ifndef __ARCH_ARM_SRC_TIVA_TIVA_FLASH_H +#define __ARCH_ARM_SRC_TIVA_TIVA_FLASH_H + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +struct mtd_dev_s; /* Forward Reference */ + +/**************************************************************************** + * Name: tiva_flash_initialize + * + * Description: + * Create an initialized MTD device instance for internal flash. + * + ****************************************************************************/ + +struct mtd_dev_s *tiva_flash_initialize(void); + +#endif /* __ARCH_ARM_SRC_TIVA_TIVA_FLASH_H */ diff --git a/drivers/mtd/mtd_config.c b/drivers/mtd/mtd_config.c index ca6654b0e6..1502679548 100644 --- a/drivers/mtd/mtd_config.c +++ b/drivers/mtd/mtd_config.c @@ -245,7 +245,7 @@ errout: /**************************************************************************** * Name: mtdconfig_writebytes * - * Writes bytes to the contained MTD device. This will either usee + * Writes bytes to the contained MTD device. This will either use * the byte write function or if that is not available, the bwrite. * ****************************************************************************/ @@ -286,6 +286,17 @@ static int mtdconfig_writebytes(FAR struct mtdconfig_struct_s *dev, int offset, goto errout; } + /* Now erase the block */ + + ret = MTD_ERASE(dev->mtd, block, 1); + if (ret < 0) + { + /* Error erasing the block */ + + ret = -EIO; + goto errout; + } + index = offset - block * dev->blocksize; bytes_this_block = dev->blocksize - index; if (bytes_this_block > writelen) @@ -1035,8 +1046,12 @@ static int mtdconfig_setconfig(FAR struct mtdconfig_struct_s *dev, /* Allocate a temp block buffer */ dev->buffer = (FAR uint8_t *) kmm_malloc(dev->blocksize); + if (dev->buffer == NULL) + { + return -ENOMEM; + } - /* Read and vaidate the signature bytes */ + /* Read and validate the signature bytes */ retry: offset = mtdconfig_findfirstentry(dev, &hdr); @@ -1180,7 +1195,15 @@ retry_find: hdr.len = pdata->len; hdr.flags = MTD_ERASED_FLAGS; - mtdconfig_writebytes(dev, offset, (uint8_t *)&hdr, sizeof(hdr)); + ret = mtdconfig_writebytes(dev, offset, (uint8_t *)&hdr, sizeof(hdr)); + if (ret < 0) + { + /* Cannot write even header! */ + + ret = -EIO; + goto errout; + } + bytes = mtdconfig_writebytes(dev, offset + sizeof(hdr), pdata->configdata, pdata->len); if (bytes != pdata->len) diff --git a/include/nuttx/mtd/mtd.h b/include/nuttx/mtd/mtd.h index d0b8ab3ed7..859411b811 100644 --- a/include/nuttx/mtd/mtd.h +++ b/include/nuttx/mtd/mtd.h @@ -2,7 +2,7 @@ * include/nuttx/mtd/mtd.h * Memory Technology Device (MTD) interface * - * Copyright (C) 2009-2013, 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2009-2013, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -99,7 +99,7 @@ #endif #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MTD) -#define CONFIG_MTD_REGISTRATION 1 +# define CONFIG_MTD_REGISTRATION 1 #endif /**************************************************************************** @@ -548,16 +548,6 @@ struct qspi_dev_s; /* Forward reference */ FAR struct mtd_dev_s *n25qxxx_initialize(FAR struct qspi_dev_s *qspi, bool unprotect); -/**************************************************************************** - * Name: up_flashinitialize - * - * Description: - * Create an initialized MTD device instance for internal flash. - * - ****************************************************************************/ - -FAR struct mtd_dev_s *up_flashinitialize(void); - /**************************************************************************** * Name: filemtd_initialize * @@ -594,7 +584,7 @@ bool filemtd_isfilemtd(FAR struct mtd_dev_s* mtd); * * Description: * Registers MTD device with the procfs file system. This assigns a unique - * MTD number and associates the given device name, then add adds it to + * MTD number and associates the given device name, then adds it to * the list of registered devices. * * In an embedded system, this all is really unnecessary, but is provided