mtd/config: erase block between block read and write

This commit is contained in:
Juha Niskanen 2017-05-10 08:25:39 -06:00 committed by Gregory Nutt
parent 591f35be87
commit 4f18b40429
4 changed files with 90 additions and 17 deletions

View File

@ -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 */

View File

@ -0,0 +1,59 @@
/************************************************************************************
* arch/arm/src/tiva/tiva_flash.h
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 */

View File

@ -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)

View File

@ -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 <gnutt@nuttx.org>
*
* 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