2015-11-25 21:46:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mtd/filemtd.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Ken Pettit. All rights reserved.
|
|
|
|
* Author: Ken Pettit <pettitkd@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-11 18:18:30 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-11-25 21:46:28 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_FILEMTD_BLOCKSIZE
|
|
|
|
# define CONFIG_FILEMTD_BLOCKSIZE 512
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_FILEMTD_ERASESIZE
|
|
|
|
# define CONFIG_FILEMTD_ERASESIZE 4096
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_FILEMTD_ERASESTATE
|
|
|
|
# define CONFIG_FILEMTD_ERASESTATE 0xff
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_FILEMTD_ERASESTATE != 0xff && CONFIG_FILEMTD_ERASESTATE != 0x00
|
|
|
|
# error "Unsupported value for CONFIG_FILEMTD_ERASESTATE"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_FILEMTD_BLOCKSIZE > CONFIG_FILEMTD_ERASESIZE
|
|
|
|
# error "Must have CONFIG_FILEMTD_BLOCKSIZE <= CONFIG_FILEMTD_ERASESIZE"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef FILEMTD_BLKPER
|
|
|
|
#define FILEMTD_BLKPER (CONFIG_FILEMTD_ERASESIZE/CONFIG_FILEMTD_BLOCKSIZE)
|
|
|
|
|
|
|
|
#if FILEMTD_BLKPER*CONFIG_FILEMTD_BLOCKSIZE != CONFIG_FILEMTD_ERASESIZE
|
|
|
|
# error "CONFIG_FILEMTD_ERASESIZE must be an even multiple of CONFIG_FILEMTD_BLOCKSIZE"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This type represents the state of the MTD device. The struct mtd_dev_s
|
|
|
|
* must appear at the beginning of the definition so that you can freely
|
|
|
|
* cast between pointers to struct mtd_dev_s and struct file_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct file_dev_s
|
|
|
|
{
|
2015-11-28 16:00:26 +01:00
|
|
|
struct mtd_dev_s mtd; /* MTD device */
|
2017-11-03 00:07:08 +01:00
|
|
|
struct file mtdfile;
|
2015-11-28 16:00:26 +01:00
|
|
|
size_t nblocks; /* Number of erase blocks */
|
|
|
|
size_t offset; /* Offset from start of file */
|
|
|
|
size_t erasesize; /* Offset from start of file */
|
|
|
|
size_t blocksize; /* Offset from start of file */
|
2015-11-25 21:46:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-25 21:57:25 +01:00
|
|
|
static ssize_t filemtd_read(FAR struct file_dev_s *priv,
|
|
|
|
FAR unsigned char *buffer, size_t offsetbytes,
|
|
|
|
unsigned int nbytes);
|
|
|
|
static ssize_t filemtd_write(FAR struct file_dev_s *priv, size_t offset,
|
|
|
|
FAR const void *src, size_t len);
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
static int filemtd_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
2015-11-25 21:57:25 +01:00
|
|
|
size_t nblocks);
|
2016-07-18 04:19:44 +02:00
|
|
|
static ssize_t filemtd_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
2015-11-25 21:57:25 +01:00
|
|
|
size_t nblocks, FAR uint8_t *buf);
|
2016-07-18 04:19:44 +02:00
|
|
|
static ssize_t filemtd_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
2015-11-25 21:57:25 +01:00
|
|
|
size_t nblocks, FAR const uint8_t *buf);
|
2016-07-18 04:19:44 +02:00
|
|
|
static ssize_t filemtd_byteread(FAR struct mtd_dev_s *dev, off_t offset,
|
2015-11-25 21:57:25 +01:00
|
|
|
size_t nbytes, FAR uint8_t *buf);
|
2015-11-25 21:46:28 +01:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static ssize_t file_bytewrite(FAR struct mtd_dev_s *dev, off_t offset,
|
2015-11-25 21:57:25 +01:00
|
|
|
size_t nbytes, FAR const uint8_t *buf);
|
2015-11-25 21:46:28 +01:00
|
|
|
#endif
|
2017-10-19 17:53:41 +02:00
|
|
|
static int filemtd_ioctl(FAR struct mtd_dev_s *dev, int cmd,
|
2015-11-25 21:57:25 +01:00
|
|
|
unsigned long arg);
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: filemtd_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-28 21:17:17 +02:00
|
|
|
static ssize_t filemtd_write(FAR struct file_dev_s *priv, size_t offset,
|
2015-11-25 21:57:25 +01:00
|
|
|
FAR const void *src, size_t len)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR const uint8_t *pin = (FAR const uint8_t *)src;
|
|
|
|
FAR uint8_t *pout;
|
|
|
|
char buf[128];
|
|
|
|
int buflen = 0;
|
|
|
|
uint8_t oldvalue;
|
|
|
|
uint8_t srcvalue;
|
|
|
|
uint8_t newvalue;
|
|
|
|
size_t seekpos;
|
|
|
|
|
|
|
|
/* Set the starting location in the file */
|
|
|
|
|
|
|
|
seekpos = priv->offset + offset;
|
|
|
|
|
|
|
|
while (len-- > 0)
|
|
|
|
{
|
|
|
|
if (buflen == 0)
|
|
|
|
{
|
|
|
|
/* Read more data from the file */
|
|
|
|
|
2017-11-03 00:07:08 +01:00
|
|
|
file_seek(&priv->mtdfile, seekpos, SEEK_SET);
|
|
|
|
buflen = file_read(&priv->mtdfile, buf, sizeof(buf));
|
2017-10-11 20:13:41 +02:00
|
|
|
pout = (FAR uint8_t *) buf;
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the source and destination values */
|
|
|
|
|
|
|
|
oldvalue = *pout;
|
|
|
|
srcvalue = *pin++;
|
|
|
|
|
|
|
|
/* Get the new destination value, accounting for bits that cannot be
|
|
|
|
* changes because they are not in the erased state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_FILEMTD_ERASESTATE == 0xff
|
|
|
|
newvalue = oldvalue & srcvalue; /* We can only clear bits */
|
|
|
|
#else /* CONFIG_FILEMTD_ERASESTATE == 0x00 */
|
|
|
|
newvalue = oldvalue | srcvalue; /* We can only set bits */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Report any attempt to change the value of bits that are not in the
|
|
|
|
* erased state.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2015-11-25 21:46:28 +01:00
|
|
|
if (newvalue != srcvalue)
|
|
|
|
{
|
2017-10-19 17:53:41 +02:00
|
|
|
ferr("ERROR: Bad write: source=%02x dest=%02x result=%02x\n",
|
2015-11-25 21:46:28 +01:00
|
|
|
srcvalue, oldvalue, newvalue);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Write the modified value to simulated FLASH */
|
|
|
|
|
|
|
|
*pout++ = newvalue;
|
|
|
|
buflen--;
|
|
|
|
|
2017-06-28 21:17:17 +02:00
|
|
|
/* If our buffer is full, then seek back to beginning of
|
|
|
|
* the file and write the buffer contents
|
2015-11-25 21:46:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen == 0)
|
2017-06-28 21:17:17 +02:00
|
|
|
{
|
2017-11-03 00:07:08 +01:00
|
|
|
file_seek(&priv->mtdfile, seekpos, SEEK_SET);
|
|
|
|
(void)file_write(&priv->mtdfile, buf, sizeof(buf));
|
2015-11-25 21:46:28 +01:00
|
|
|
seekpos += sizeof(buf);
|
2017-06-28 21:17:17 +02:00
|
|
|
}
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write remaining bytes */
|
|
|
|
|
|
|
|
if (buflen != 0)
|
|
|
|
{
|
2017-11-03 00:07:08 +01:00
|
|
|
file_seek(&priv->mtdfile, seekpos, SEEK_SET);
|
|
|
|
(void)file_write(&priv->mtdfile, buf, sizeof(buf));
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: filemtd_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-25 21:57:25 +01:00
|
|
|
static ssize_t filemtd_read(FAR struct file_dev_s *priv,
|
|
|
|
FAR unsigned char *buffer, size_t offsetbytes,
|
2017-10-19 17:53:41 +02:00
|
|
|
unsigned int nbytes)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
/* Set the starting location in the file */
|
|
|
|
|
2017-11-03 00:07:08 +01:00
|
|
|
(void)file_seek(&priv->mtdfile, priv->offset + offsetbytes, SEEK_SET);
|
2015-11-25 21:46:28 +01:00
|
|
|
|
2017-11-03 00:07:08 +01:00
|
|
|
return file_read(&priv->mtdfile, buffer, nbytes);
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-18 04:19:44 +02:00
|
|
|
* Name: filemtd_erase
|
2015-11-25 21:46:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-18 04:19:44 +02:00
|
|
|
static int filemtd_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
2017-10-19 17:53:41 +02:00
|
|
|
size_t nblocks)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *)dev;
|
|
|
|
size_t nbytes;
|
|
|
|
size_t offset;
|
|
|
|
char buffer[128];
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
|
|
|
/* Don't let the erase exceed the original size of the file */
|
|
|
|
|
|
|
|
if (startblock >= priv->nblocks)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startblock + nblocks > priv->nblocks)
|
|
|
|
{
|
|
|
|
nblocks = priv->nblocks - startblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the erase block to a logical block and the number of blocks
|
|
|
|
* in logical block numbers
|
|
|
|
*/
|
|
|
|
|
|
|
|
startblock *= FILEMTD_BLKPER;
|
|
|
|
nblocks *= FILEMTD_BLKPER;
|
|
|
|
|
|
|
|
/* Get the offset corresponding to the first block and the size
|
|
|
|
* corresponding to the number of blocks.
|
|
|
|
*/
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
offset = startblock * priv->blocksize;
|
|
|
|
nbytes = nblocks * priv->blocksize;
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
/* Then erase the data in the file */
|
|
|
|
|
2017-11-03 00:07:08 +01:00
|
|
|
file_seek(&priv->mtdfile, priv->offset + offset, SEEK_SET);
|
2015-11-25 21:46:28 +01:00
|
|
|
memset(buffer, CONFIG_FILEMTD_ERASESTATE, sizeof(buffer));
|
|
|
|
while (nbytes)
|
|
|
|
{
|
2017-11-03 00:07:08 +01:00
|
|
|
(void)file_write(&priv->mtdfile, buffer, sizeof(buffer));
|
2017-06-28 21:17:17 +02:00
|
|
|
nbytes -= sizeof(buffer);
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-18 04:19:44 +02:00
|
|
|
* Name: filemtd_bread
|
2015-11-25 21:46:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-18 04:19:44 +02:00
|
|
|
static ssize_t filemtd_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
2017-10-19 17:53:41 +02:00
|
|
|
size_t nblocks, FAR uint8_t *buf)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *)dev;
|
|
|
|
off_t offset;
|
|
|
|
off_t maxblock;
|
|
|
|
size_t nbytes;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev && buf);
|
|
|
|
|
|
|
|
/* Don't let the read exceed the original size of the file */
|
|
|
|
|
|
|
|
maxblock = priv->nblocks * FILEMTD_BLKPER;
|
|
|
|
if (startblock >= maxblock)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startblock + nblocks > maxblock)
|
|
|
|
{
|
|
|
|
nblocks = maxblock - startblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the offset corresponding to the first block and the size
|
|
|
|
* corresponding to the number of blocks.
|
|
|
|
*/
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
offset = startblock * priv->blocksize;
|
|
|
|
nbytes = nblocks * priv->blocksize;
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
/* Then read the data from the file */
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
(void)filemtd_read(priv, buf, offset, nbytes);
|
2015-11-25 21:46:28 +01:00
|
|
|
return nblocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-18 04:19:44 +02:00
|
|
|
* Name: filemtd_bwrite
|
2015-11-25 21:46:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-18 04:19:44 +02:00
|
|
|
static ssize_t filemtd_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
2017-10-11 18:18:30 +02:00
|
|
|
size_t nblocks, FAR const uint8_t *buf)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *)dev;
|
|
|
|
off_t offset;
|
|
|
|
off_t maxblock;
|
|
|
|
size_t nbytes;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev && buf);
|
|
|
|
|
|
|
|
/* Don't let the write exceed the original size of the file */
|
|
|
|
|
|
|
|
maxblock = priv->nblocks * FILEMTD_BLKPER;
|
|
|
|
if (startblock >= maxblock)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startblock + nblocks > maxblock)
|
|
|
|
{
|
|
|
|
nblocks = maxblock - startblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the offset corresponding to the first block and the size
|
|
|
|
* corresponding to the number of blocks.
|
|
|
|
*/
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
offset = startblock * priv->blocksize;
|
|
|
|
nbytes = nblocks * priv->blocksize;
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
/* Then write the data to the file */
|
|
|
|
|
|
|
|
filemtd_write(priv, offset, buf, nbytes);
|
|
|
|
return nblocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-18 04:19:44 +02:00
|
|
|
* Name: filemtd_byteread
|
2015-11-25 21:46:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-18 04:19:44 +02:00
|
|
|
static ssize_t filemtd_byteread(FAR struct mtd_dev_s *dev, off_t offset,
|
2017-10-19 17:53:41 +02:00
|
|
|
size_t nbytes, FAR uint8_t *buf)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *)dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev && buf);
|
|
|
|
|
|
|
|
/* Don't let read read past end of buffer */
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
if (offset + nbytes > priv->nblocks * priv->erasesize)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
(void)filemtd_read(priv, buf, offset, nbytes);
|
2015-11-25 21:46:28 +01:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_bytewrite
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static ssize_t file_bytewrite(FAR struct mtd_dev_s *dev, off_t offset,
|
2017-10-11 18:18:30 +02:00
|
|
|
size_t nbytes, FAR const uint8_t *buf)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *)dev;
|
|
|
|
off_t maxoffset;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev && buf);
|
|
|
|
|
|
|
|
/* Don't let the write exceed the original size of the file */
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
maxoffset = priv->nblocks * priv->erasesize;
|
2015-11-25 21:46:28 +01:00
|
|
|
if (offset + nbytes > maxoffset)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then write the data to the file */
|
|
|
|
|
|
|
|
filemtd_write(priv, offset, buf, nbytes);
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-18 04:19:44 +02:00
|
|
|
* Name: filemtd_ioctl
|
2015-11-25 21:46:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
static int filemtd_ioctl(FAR struct mtd_dev_s *dev, int cmd,
|
|
|
|
unsigned long arg)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2015-11-25 21:57:25 +01:00
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
|
|
|
|
2015-11-25 21:46:28 +01:00
|
|
|
if (geo)
|
|
|
|
{
|
|
|
|
/* Populate the geometry structure with information need to know
|
|
|
|
* the capacity and how to access the device.
|
|
|
|
*/
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
geo->blocksize = priv->blocksize;
|
|
|
|
geo->erasesize = priv->erasesize;
|
2015-11-25 21:46:28 +01:00
|
|
|
geo->neraseblocks = priv->nblocks;
|
|
|
|
ret = OK;
|
2015-11-25 21:57:25 +01:00
|
|
|
}
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_XIPBASE:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
2015-11-25 21:57:25 +01:00
|
|
|
/* Erase the entire device */
|
2015-11-25 21:46:28 +01:00
|
|
|
|
2016-07-18 04:19:44 +02:00
|
|
|
filemtd_erase(dev, 0, priv->nblocks);
|
2015-11-25 21:57:25 +01:00
|
|
|
ret = OK;
|
2015-11-25 21:46:28 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-19 17:53:41 +02:00
|
|
|
* Name: blockmtd_initialize
|
2015-11-25 21:46:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-19 17:53:41 +02:00
|
|
|
* Create and initialize a BLOCK MTD device instance.
|
2015-11-25 21:46:28 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-10-19 17:53:41 +02:00
|
|
|
* path - Path name of the block device backing the MTD device
|
2015-11-25 21:46:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
FAR struct mtd_dev_s *blockmtd_initialize(FAR const char *path, size_t offset,
|
|
|
|
size_t mtdlen, int16_t sectsize,
|
|
|
|
int32_t erasesize)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv;
|
|
|
|
size_t nblocks;
|
2017-11-03 00:07:08 +01:00
|
|
|
int mode;
|
|
|
|
int ret;
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
/* Create an instance of the FILE MTD device state structure */
|
|
|
|
|
|
|
|
priv = (FAR struct file_dev_s *)kmm_zalloc(sizeof(struct file_dev_s));
|
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed to allocate the FILE MTD state structure\n");
|
2015-11-25 21:46:28 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the file open mode */
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
mode = O_RDOK;
|
2015-11-25 21:46:28 +01:00
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
mode |= O_WROK;
|
|
|
|
#endif
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
/* Try to open the file. NOTE that block devices will use a character
|
|
|
|
* driver proxy.
|
|
|
|
*/
|
2015-11-25 21:46:28 +01:00
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = file_open(&priv->mtdfile, path, mode);
|
2017-11-03 00:07:08 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-09-15 18:49:41 +02:00
|
|
|
ferr("ERROR: Failed to open the FILE MTD file %s: %d\n", path, ret);
|
2017-11-03 00:07:08 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
/* Set the block size based on the provided sectsize parameter */
|
|
|
|
|
|
|
|
if (sectsize <= 0)
|
|
|
|
{
|
|
|
|
priv->blocksize = CONFIG_FILEMTD_BLOCKSIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->blocksize = sectsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the erase size based on the provided erasesize parameter */
|
|
|
|
|
|
|
|
if (erasesize <= 0)
|
|
|
|
{
|
|
|
|
priv->erasesize = CONFIG_FILEMTD_ERASESIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->erasesize = erasesize;
|
|
|
|
}
|
|
|
|
|
2015-11-25 21:46:28 +01:00
|
|
|
/* Force the size to be an even number of the erase block size */
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
nblocks = mtdlen / priv->erasesize;
|
2015-11-25 21:46:28 +01:00
|
|
|
if (nblocks < 3)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Need to provide at least three full erase block\n");
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&priv->mtdfile);
|
2015-11-25 21:46:28 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform initialization as necessary. (unsupported methods were
|
|
|
|
* nullified by kmm_zalloc).
|
|
|
|
*/
|
|
|
|
|
2016-07-18 04:19:44 +02:00
|
|
|
priv->mtd.erase = filemtd_erase;
|
|
|
|
priv->mtd.bread = filemtd_bread;
|
|
|
|
priv->mtd.bwrite = filemtd_bwrite;
|
|
|
|
priv->mtd.read = filemtd_byteread;
|
2015-11-25 21:46:28 +01:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
priv->mtd.write = file_bytewrite;
|
|
|
|
#endif
|
2016-07-18 04:19:44 +02:00
|
|
|
priv->mtd.ioctl = filemtd_ioctl;
|
2015-11-25 21:46:28 +01:00
|
|
|
priv->offset = offset;
|
|
|
|
priv->nblocks = nblocks;
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
#ifdef CONFIG_MTD_REGISTRATION
|
2015-11-25 21:46:28 +01:00
|
|
|
/* Register the MTD with the procfs system if enabled */
|
|
|
|
|
|
|
|
mtd_register(&priv->mtd, "filemtd");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return &priv->mtd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-19 17:53:41 +02:00
|
|
|
* Name: blockmtd_teardown
|
2015-11-25 21:46:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-19 17:53:41 +02:00
|
|
|
* Teardown a previously created blockmtd device.
|
2015-11-25 21:46:28 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-10-19 19:09:23 +02:00
|
|
|
* dev - Pointer to the mtd driver instance.
|
2015-11-25 21:46:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
void blockmtd_teardown(FAR struct mtd_dev_s *dev)
|
2015-11-25 21:46:28 +01:00
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv;
|
|
|
|
|
|
|
|
/* Close the enclosed file */
|
|
|
|
|
|
|
|
priv = (FAR struct file_dev_s *) dev;
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&priv->mtdfile);
|
2015-11-25 21:46:28 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_REGISTRATION
|
2017-10-19 17:53:41 +02:00
|
|
|
/* Un-register the MTD with the procfs system if enabled */
|
|
|
|
|
2015-11-25 21:46:28 +01:00
|
|
|
mtd_unregister(&priv->mtd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Free the memory */
|
|
|
|
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
2015-11-28 16:00:26 +01:00
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: filemtd_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create and initialize a FILE MTD device instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* path - Path name of the file backing the MTD device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct mtd_dev_s *filemtd_initialize(FAR const char *path, size_t offset,
|
|
|
|
int16_t sectsize, int32_t erasesize)
|
|
|
|
{
|
|
|
|
size_t filelen;
|
|
|
|
struct stat sb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Stat the file */
|
|
|
|
|
|
|
|
ret = stat(path, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to stat %s: %d\n", path, get_errno());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
filelen = sb.st_size;
|
|
|
|
|
|
|
|
if (offset > filelen)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Offset beyond end of file\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockmtd_initialize(path, offset, filelen - offset, sectsize,
|
|
|
|
erasesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: filemtd_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown a previously created filemtd device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-10-19 19:09:23 +02:00
|
|
|
* dev - Pointer to the mtd driver instance.
|
2017-10-19 17:53:41 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void filemtd_teardown(FAR struct mtd_dev_s *dev)
|
|
|
|
{
|
|
|
|
blockmtd_teardown(dev);
|
|
|
|
}
|
|
|
|
|
2015-11-28 16:00:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: filemtd_isfilemtd
|
|
|
|
*
|
|
|
|
* Description:
|
2017-10-19 17:53:41 +02:00
|
|
|
* Tests if the provided mtd is a filemtd or blockmtd device.
|
2015-11-28 16:00:26 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mtd - Pointer to the mtd.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool filemtd_isfilemtd(FAR struct mtd_dev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct file_dev_s *priv = (FAR struct file_dev_s *) dev;
|
|
|
|
|
2017-10-19 17:53:41 +02:00
|
|
|
return (priv->mtd.erase == filemtd_erase);
|
2015-11-28 16:00:26 +01:00
|
|
|
}
|